]> _ Git - cubist_cms-back.git/commitdiff
wip #6693
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 2 Feb 2024 18:32:40 +0000 (19:32 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 2 Feb 2024 18:32:40 +0000 (19:32 +0100)
src/resources/views/fields/pagebuilder-grapesjs.blade.php

index ec1c467eb973c5587d4039942151c172fb29edc2..bc8bbe30aa5f5e6e0c5a85921291553ec771be24 100644 (file)
@@ -1,3 +1,10 @@
+@php
+    $value=old(square_brackets_to_dots($field['name'])) ?? $field['value'] ?? $field['default'] ?? '{}';
+    if(is_string($value)){
+        $value=json_decode($value);
+    }
+@endphp
+
 @include('crud::fields.inc.wrapper_start')
 <label>{!! $field['label'] !!}</label>
 @include('crud::fields.inc.translatable_icon')
     @bassetBlock('cubist/grapesjs-field.js')
     <script>
         jQuery(document).ready(function ($) {
-            $(document).on('cubist.bunchmultiple.added', function () {
-                initUntriggeredGrapesJS($);
-            });
-            initUntriggeredGrapesJS($);
-        });
+            const inlineStorage = (editor) => {
+                editor.Storage.add('inline', {
+                    load() {
+                        let input = $(editor.config.container + '-input');
+                        console.log($(input).val());
+                        let d = JSON.parse($(input).val());
+                        if (d.data !== undefined && d.data !== null) {
+                            return d.data;
+                        }
+                        return {};
+                    },
+                    store(data) {
+                        let input = $(editor.config.container + '-input');
+                        const component = editor.Pages.getSelected().getMainComponent();
+                        let save = {};
+                        save.css = editor.getCss({component});
+                        save.html = editor.getHtml({component});
+                        save.data = data;
+                        $(input).val(JSON.stringify(save));
+                    }
+                });
+            };
 
-        function initUntriggeredGrapesJS($) {
-            // trigger select2 for each untriggered select2 box
-            $('.grapesjs:not(.grapesjs-init)').each(function (i, obj) {
-                let id = 'gjs-' + (Math.round(Math.random() * 10000000));
-                $(obj).attr('id', id);
-
-                if ($(obj).closest('.item.sample').length > 0) {
-                    return;
-                }
-                $(obj).addClass('grapesjs-init');
-                let e = grapesjs.init({
-                    container: '#' + id,
-                    components: '<div class="txt-red">Hello world!</div>',
-                    style: '.txt-red{color: red}',
+            function initUntriggeredGrapesJS() {
+                $('.grapesjs:not(.grapesjs-init)').each(function (i, obj) {
+                    let id = (Math.round(Math.random() * 10000000));
+                    $(obj).attr('id', 'gjs-' + id);
+
+                    if ($(obj).closest('.item.sample').length > 0) {
+                        return;
+                    }
+                    $(obj).addClass('grapesjs-init');
+                    let e = grapesjs.init({
+                        container: '#gjs-' + id,
+                        style: '.txt-red{color: red}',
+                        plugins: [inlineStorage],
+                        storageManager: {type: 'inline'},
+                    });
+                    $(obj).prev('input').attr('id', 'gjs-' + id + '-input')
+                    $(obj).data('grapesjs', e);
                 });
-                $(obj).data('grapesjs', e);
+            }
+
+            $(document).on('cubist.bunchmultiple.added', function () {
+                initUntriggeredGrapesJS();
             });
-        }
+            initUntriggeredGrapesJS();
+        });
     </script>
     @endBassetBlock
 @endpush
 
+<input type="hidden" name="{{ $field['name'] }}"
+       value="@json($value)"/>
 <div class="grapesjs"></div>