Browse Source

Pass editor to onStore/onLoad

pull/4223/head
Artur Arseniev 4 years ago
parent
commit
6780389cec
  1. 34
      docs/modules/Storage.md
  2. 10
      src/storage_manager/index.js

34
docs/modules/Storage.md

@ -345,9 +345,39 @@ grapesjs.init({
In case `projectData` is defined, the initial storage load will be automatically skipped.
### HTML code in project data
### HTML code with project data
### Inline data
The project data doesn't contain HTML/CSS of your pages as its main purpose is to collect only the strictly necessary information.
In case you have a strict requirement to execute also other logic connected to the store of your project data (eg. deploy HTML/CSS result to the stage environment) you can enrich your remote calls by using the `onStore` option in your remote storage.
```js
const getPagesHtml = async (editor) => {
const pages = editor.Pages.getAll();
return await Promise.all(pages.map(async (page) => {
return {
id: page.getId(),
html: await editor.getCode('html', { component: page.getMainComponent() }),
};
}));
};
grapesjs.init({
// ...
storageManager: {
type: 'remote',
// Enrich the store call
options: {
remote: {
onStore: (data, editor) => {
return { id: projectID, data };
},
}
},
},
})
```
### Inline project data

10
src/storage_manager/index.js

@ -290,16 +290,18 @@ export default () => {
}
try {
const editor = this.em?.getEditor();
if (data) {
let toStore = (onStore && (await onStore(data))) || data;
toStore = (opts.onStore && (await opts.onStore(toStore))) || toStore;
let toStore = (onStore && (await onStore(data, editor))) || data;
toStore = (opts.onStore && (await opts.onStore(toStore, editor))) || toStore;
await storage.store(toStore, opts);
result = data;
} else {
result = await storage.load(opts);
result = this.__clearKeys(result);
result = (opts.onLoad && (await opts.onLoad(result))) || result;
result = (onLoad && (await onLoad(result))) || result;
result = (opts.onLoad && (await opts.onLoad(result, editor))) || result;
result = (onLoad && (await onLoad(result, editor))) || result;
}
this.onAfter(ev, result);
this.onEnd(ev, result);

Loading…
Cancel
Save