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)
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
},
...
});
@treztreiz did you ever figure out how to do it without registering it as a plugin? I'm also running into this issue.
@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.
Issue #597
storageManager: Loading from remote storage
Hi, I'm trying to read the HTML and CSS from my database. When I try to load it into the editor, nothing displays. I can store the data jus...
Issue #911
Could some one please help me with Custom Load.
What I can use instead of followings- editor.setHtml( arrdata['html'] ), editor.setCss( arrdata['css'] ), In below code.
Issue #1429
Empty content when load from remote storage if components array is not empty
I have problem loading stored remote content into the editor when the JSON contains non empty gjs-components My JSON data looks like this w...
Issue #2106
Traits not appear in edit mode when i update grapes version
Hi artf, i make traits for specific component ,when i saved this component as a json and load again in editor the traits were load correctl...
Paid Plugins That Match This Issue
Curated by issue keywords and label relevance to help you ship faster.
Loading paid plugin recommendations...
Browse Plugin Categories
Jump directly to plugin category pages on the marketplace.