Issue #2526Opened January 24, 2020by treztreiz0 reactions

Custom storage : autoload not triggered

Question

version : grapes.min.js 0.15.10

Hi, I'm using a custom storage to load & store json data from/to a textarea. Using a custom storage seems to not support the autoload option. Instead, I have to manually trigger editor.load(), but it seems to update the editor so it messes with the noticeOnUnload option (which get triggered before any changes made by the user).

Here is the storageManager configuration :

storageManager

const editor = grapesjs.init({
...
storageManager: {
    type: 'customStorage',
    autosave: false,
    autoload: true
},
...
});

Here is the the custom storage :

custom storage

editor.StorageManager.add('customStorage', {
        load: function(keys, clb, clbErr) {

            var res = {}, data = storageEl.text(); // get textarea json content

           // custom logic
            if( "" !== data ) {
                try {

                    data = JSON.parse(data);
                    for (var i = 0; i < keys.length; i++) {
                        var v = data[keys[i]];
                        if(v) res[keys[i]] = v;
                    }

                } catch(e) { clbErr( "Error while parsing JSON : " + e.message ); }
            }

            clb( res );

        },
        store(data, clb) {
            var dataString = JSON.stringify( data );
            storageEl.text( dataString ); // updating textarea json content
            clb();
        }
    });

Is there any way to autoload a custom storage ? Thank you !

Answers (3)

treztreizJanuary 24, 20200 reactions

The autoload option actually works while adding the custom storage as a plugin.

custom storage plugin :

grapesjs.plugins.add('customStorage', editor => {
    ... 
    sm.add('customStorage', { 
        // custom storage logic
    });
    ...
});

Editor :

const editor = grapesjs.init({
    ...
    plugins: ['customStorage'],
    storageManager: {
        type: 'customStorage',
        autoload: true
    },
    ...
});
collindutterJanuary 30, 20200 reactions

@treztreiz did you ever figure out how to do it without registering it as a plugin? I'm also running into this issue.

treztreizFebruary 2, 20200 reactions

@collindutter It seems that you must make a plugin. I suppose that the autoload option is used while grapes is instantiated, and unfortunately the function editor.StorageManager.add actually needs that instance... This does not happen with plugins as they are injected in the configuration during the instantiation :) Not sure about what I'm saying tho

Related Questions and Answers

Continue research with similar issue discussions.

Paid Plugins That Match This Issue

Curated by issue keywords and label relevance to help you ship faster.

View all plugins

Loading paid plugin recommendations...

Browse Plugin Categories

Jump directly to plugin category pages on the marketplace.