From 9ca06fd0646e380326c5fc952775a14264e12df7 Mon Sep 17 00:00:00 2001
From: philipp lang <philipp@aweos.de>
Date: Tue, 24 Dec 2024 22:43:31 +0100
Subject: [PATCH] Pass initial value to editor function call

---
 app/View/Form/Editor.php        | 12 ++++++++----
 resources/livewire-js/app.js    |  2 +-
 resources/livewire-js/editor.js | 10 +++++-----
 3 files changed, 14 insertions(+), 10 deletions(-)

diff --git a/app/View/Form/Editor.php b/app/View/Form/Editor.php
index 14877977..8a608370 100644
--- a/app/View/Form/Editor.php
+++ b/app/View/Form/Editor.php
@@ -31,14 +31,18 @@ class Editor extends Component
                 @endif
 
                 <div class="relative w-full h-full">
-                    <div class="
+                    <div
+                        class="
                         w-full border-gray-600 border-solid text-gray-300 bg-gray-700 leading-none rounded-lg
                         group-[.size-default]:border-2 group-[.size-sm]:border
                         group-[.size-default]:text-sm group-[.size-sm]:text-xs
                         group-[.size-default]:p-2 group-[.size-sm]:p-1
-                        " @updated="$wire.{{$attributes->wire('model')->value}} = $event.detail" x-bind="editor"x-data="{
-                            value: $wire.{{$attributes->wire('model')->value}},
-                        }" id="{{$id}}" {{$attributes}}></div>
+                        "
+                        @updated="$wire.{{$attributes->wire('model')->value}} = $event.detail"
+                        x-data="editor($wire.{{$attributes->wire('model')->value}})"
+                        id="{{$id}}"
+                        {{$attributes}}
+                    ></div>
                     <x-ui::errors :for="$name" />
                     @if($hint)
                     <x-form::hint>{{$hint}}</x-form::hint>
diff --git a/resources/livewire-js/app.js b/resources/livewire-js/app.js
index 0820e9da..f48b0dd1 100644
--- a/resources/livewire-js/app.js
+++ b/resources/livewire-js/app.js
@@ -18,7 +18,7 @@ Alpine.plugin(
 window.addEventListener('success', (event) => success(event.detail[0]));
 
 document.addEventListener('alpine:init', () => {
-    Alpine.bind('editor', () => editor);
+    Alpine.data('editor', editor);
 });
 
 Livewire.start();
diff --git a/resources/livewire-js/editor.js b/resources/livewire-js/editor.js
index 02d35376..7a0de2b8 100644
--- a/resources/livewire-js/editor.js
+++ b/resources/livewire-js/editor.js
@@ -5,11 +5,11 @@ import Paragraph from '@editorjs/paragraph';
 import NestedList from '@editorjs/nested-list';
 import Alert from 'editorjs-alert';
 
-export default {
-    'editor': null,
-    'value': null,
+export default (initialValue) => ({
+    editor: null,
+    value: initialValue,
 
-    'x-init': function () {
+    init: function () {
         var tools = {
             paragraph: {
                 class: Paragraph,
@@ -57,4 +57,4 @@ export default {
             }, 200),
         });
     },
-};
+});