GrapesJS / grapesjs

Free and Open source Web Builder Framework. Next generation tool for building templates without coding
https://grapesjs.com
BSD 3-Clause "New" or "Revised" License
22.38k stars 4.06k forks source link

Custom Store/Load using a Json object, without using the "Storage" API #2484

Closed electrotype closed 4 years ago

electrotype commented 4 years ago

I'm using the latest version, 0.15.9, and I'm new to this fantastic project! I'm currently learning a lot, starting from scratch. Please let me know if you prefere such questions on Stack Overflow or elsewhere.

I'm trying to export/import the content by myself, without using the "Storage" API. I see the store() function returns the exported content as a JSON object, and this is perfect for my needs! But how can I then programmatically import this JSON back? I think the load() function doesn't take such exported JSON object as a parameter, it forces you to use the "Storage" API, is that correct?

In other words, is there a way to programatically reset the state of a GrapeJs instance using the exported content, as a JSON object?

electrotype commented 4 years ago

I think I got it using:

let exportToLoad;

let editor = grapesjs.init({
    // ...
    storageManager: { 
        type: 'simpleStorage' 
    }
});

editor.StorageManager.add('simpleStorage', {
    load(keys, clb, clbErr) {
        if(exportToLoad) {
            clb(exportToLoad);
        }
    },
    store(data, clb, clbErr) {
      return data;
    }
});

function importFrom(exportContent) {
    exportToLoad = exportContent;
    editor.load(() => {});
},

Then:

let export = editor.store();

And:

importFrom(export);

That said, it doesn't feel totaly "right" to me. I would like a version of the functions where you can specify the exported Json object directly!

artf commented 4 years ago

Using custom storage is actually the best way

I would like a version of the functions where you can specify the exported Json object directly!

Well, no one limits you to update your exportToLoad