From 6780389cecc5ce18c88b6e7c06322a9333766cc3 Mon Sep 17 00:00:00 2001 From: Artur Arseniev Date: Tue, 22 Mar 2022 12:18:32 +0100 Subject: [PATCH] Pass editor to onStore/onLoad --- docs/modules/Storage.md | 34 ++++++++++++++++++++++++++++++++-- src/storage_manager/index.js | 10 ++++++---- 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/docs/modules/Storage.md b/docs/modules/Storage.md index 40652c8e2..c6db2e197 100644 --- a/docs/modules/Storage.md +++ b/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 diff --git a/src/storage_manager/index.js b/src/storage_manager/index.js index 6813d20db..2a402b5ab 100644 --- a/src/storage_manager/index.js +++ b/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);