From 4294ca30c335dd9311b474aae29ec7c14e671a2d Mon Sep 17 00:00:00 2001 From: Artur Arseniev Date: Fri, 18 Mar 2022 14:30:08 +0100 Subject: [PATCH] Update loadProjectData --- docs/modules/Storage.md | 89 +++++++++++-------------------------- src/common/module.js | 4 +- src/dom_components/index.js | 1 - src/pages/index.js | 2 +- 4 files changed, 28 insertions(+), 68 deletions(-) diff --git a/docs/modules/Storage.md b/docs/modules/Storage.md index 649644c06..9fd33a0ca 100644 --- a/docs/modules/Storage.md +++ b/docs/modules/Storage.md @@ -49,7 +49,7 @@ Check the full list of available options here: [Storage Manager Config](https:// -## Project Data +## Project data The project data is a JSON object containing all the necessary information (styles, pages, etc.) about your project in the editor and is the one used in the storage manager methods in order to store and load your project (locally or remotely in your DB/file). @@ -180,7 +180,7 @@ The default remote storage follows a simple REST API approach with project data -## Store and load templates + @@ -240,58 +219,40 @@ editor.store(res => console.log('Store callback')); ## Storage API -The Storage module has also its own [set of API](https://github.com/artf/grapesjs/wiki/API-Storage-Manager) that allows you to extend and add new functionalities. +The Storage Manager module has also its own [set of APIs](https://github.com/artf/grapesjs/wiki/API-Storage-Manager) that allows you to extend and add new functionalities. ### Define new storage -One of the most useful methods of API is the possibility to add new storages. You might think, we have the `local` and `remote` storages, what else do we need, right? Well, let's take as an example the `local` one. As you already know, it relies on [localStorage API] which is really cool and easy to use but one of his specs might be a big limit, by default it has a limited amount of MB to use per site (something around 5MB-10MB, depends on the browser implementation). As an alternative, we can make use of [IndexedDB] which is also quite [well supported](https://caniuse.com/#search=indexedDB) and allows more space usage (each browser implements its own rules, for a better understanding on how browser storage limits work, check [here](https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API/Browser_storage_limits_and_eviction_criteria)). +One of the most useful methods of API is the possibility to add new storages. +You might think, we have the `local` and `remote` storages, what else do we need, right? Well, let's take as an example the `local` one. As you already know, it relies on [localStorage API] which is really cool and easy to use but one of his specs might be a big limit, by default it has a limited amount of MB to use per site (something around 5MB-10MB, depends on the browser implementation). As an alternative, we can make use of [IndexedDB] which is also quite [well supported](https://caniuse.com/#search=indexedDB) and allows more space usage (each browser implements its own rules, for a better understanding on how browser storage limits work, check [here](https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API/Browser_storage_limits_and_eviction_criteria)). [IndexedDB configuration](https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API/Using_IndexedDB) might be too much verbose for this guide so we decided to create the [grapesjs-indexeddb] plugin, so you can check its source and see how it's implemented. For this guide we are going to see something much simpler but with the same flow, it'll be just a simple javascript object which stores key-value data, not persistent at all but the concept is the same. ```js -const editor = grapesjs.init({ - ... - storageManager: { type: 'simple-storage' }, -}); - -// Here our `simple-storage` implementation -const SimpleStorage = {}; - -editor.StorageManager.add('simple-storage', { - /** - * Load the data - * @param {Array} keys Array containing values to load, eg, ['gjs-components', 'gjs-styles', ...] - * @param {Function} clb Callback function to call when the load is ended - * @param {Function} clbErr Callback function to call in case of errors - */ - load(keys, clb, clbErr) { - const result = {}; +const addMemoryStorage = (editor) => { + const MemoryStorage = {}; - keys.forEach(key => { - const value = SimpleStorage[key]; - if (value) { - result[key] = value; - } - }); + editor.Storage.add('memory', { + async load(options = {}) { + return MemoryStorage[options.key]; + }, - // Might be called inside some async method - clb(result); - }, + async store(data, options = {}) { + MemoryStorage[options.key] = data; + } + }); +}; - /** - * Store the data - * @param {Object} data Data object to store - * @param {Function} clb Callback function to call when the load is ended - * @param {Function} clbErr Callback function to call in case of errors - */ - store(data, clb, clbErr) { - for (let key in data) { - SimpleStorage[key] = data[key]; +const editor = grapesjs.init({ + ... + plugins: [addMemoryStorage], + storageManager: { + type: 'memory', + options: { + memory: { key: 'myKey' } } - // Might be called inside some async method - clb(); - } + }, }); ``` diff --git a/src/common/module.js b/src/common/module.js index 4a4847568..1594b8de9 100644 --- a/src/common/module.js +++ b/src/common/module.js @@ -15,11 +15,11 @@ export default { return obj; }, - loadProjectData(data = {}, { all, def = [], onResult, reset } = {}) { + loadProjectData(data = {}, { all, onResult, reset } = {}) { const key = this.storageKey; const opts = { action: 'load' }; const coll = all || this.getAll(); - let result = data[key] || def; + let result = data[key]; if (typeof result == 'string') { try { diff --git a/src/dom_components/index.js b/src/dom_components/index.js index 49ea8df0b..7674e8dd2 100644 --- a/src/dom_components/index.js +++ b/src/dom_components/index.js @@ -277,7 +277,6 @@ export default () => { load(data) { return this.loadProjectData(data, { - def: null, onResult: result => { const wrapper = this.getWrapper(); if (isArray(result)) { diff --git a/src/pages/index.js b/src/pages/index.js index f69b867cb..829348f46 100644 --- a/src/pages/index.js +++ b/src/pages/index.js @@ -280,7 +280,7 @@ export default () => { }, load(data) { - return this.loadProjectData(data, { all: this.pages, reset: true, def: null }); + return this.loadProjectData(data, { all: this.pages, reset: true }); }, _createId() {