From 2fe425f4d4ca12efc313d53f0ed65afdfda43cb5 Mon Sep 17 00:00:00 2001 From: Artur Arseniev Date: Tue, 1 Feb 2022 13:26:31 +0100 Subject: [PATCH] Load shallow editor --- src/dom_components/index.js | 24 +++++++++++++++++++++--- src/editor/model/Editor.js | 24 +++++++++++++++++------- src/editor/view/EditorView.js | 15 +++++++++++---- src/undo_manager/index.js | 3 +++ 4 files changed, 52 insertions(+), 14 deletions(-) diff --git a/src/dom_components/index.js b/src/dom_components/index.js index e6eace7bd..737fd71ad 100644 --- a/src/dom_components/index.js +++ b/src/dom_components/index.js @@ -53,10 +53,9 @@ * * @module Components */ -import Backbone from 'backbone'; -import { isEmpty, isObject, isArray, isFunction, isString, result } from 'underscore'; +import { isEmpty, isObject, isArray, isFunction, isString, result, debounce } from 'underscore'; import defaults from './config/config'; -import Component from './model/Component'; +import Component, { keyUpdate, keyUpdateInside } from './model/Component'; import Components from './model/Components'; import ComponentView from './view/ComponentView'; import ComponentsView from './view/ComponentsView'; @@ -646,6 +645,25 @@ export default () => { model && isEmpty(model.get('status')) && model.set('status', state); }, + getShallowWrapper() { + let { shallow, em } = this; + + if (!shallow && em) { + const shallowEm = em.get('shallow'); + shallow = shallowEm?.get('DomComponents').getWrapper(); + if (shallow) { + const events = [keyUpdate, keyUpdateInside].join(' '); + shallow.on( + events, + debounce(() => shallow.components(''), 100) + ); + } + this.shallow = shallow; + } + + return shallow; + }, + /** * Check if the component can be moved inside another. * @param {[Component]} target The target Component is the one that is supposed to receive the source one. diff --git a/src/editor/model/Editor.js b/src/editor/model/Editor.js index dc3be3801..5856d28eb 100644 --- a/src/editor/model/Editor.js +++ b/src/editor/model/Editor.js @@ -132,23 +132,20 @@ export default class EditorModel extends Model { } /** - * Should be called after all modules and plugins are loaded + * Should be called once all modules and plugins are loaded * @param {Function} clb * @private */ loadOnStart(clb = null) { const sm = this.get('StorageManager'); - // Generally, with `onLoad`, the module will try to load the data from - // its configurations - this.get('toLoad').forEach(module => { - module.onLoad(); - }); + // In `onLoad`, the module will try to load the data from its configurations. + this.get('toLoad').forEach(mdl => mdl.onLoad()); // Stuff to do post load const postLoad = () => { const modules = this.get('modules'); - modules.forEach(module => module.postLoad && module.postLoad(this)); + modules.forEach(mdl => mdl.postLoad && mdl.postLoad(this)); this.set('readyLoad', 1); clb && clb(); }; @@ -158,6 +155,17 @@ export default class EditorModel extends Model { } else { setTimeout(postLoad); } + + // Create shallow editor. + // Here we can create components/styles without altering/triggering the main EditorModel + const shallow = new EditorModel({ + noticeOnUnload: false, + storageManager: false, + undoManager: false, + }); + // We only need to load a few modules + ['PageManager', 'Canvas'].forEach(key => shallow.get(key).onLoad()); + this.set('shallow', shallow); } /** @@ -779,6 +787,8 @@ export default class EditorModel extends Model { const { config, view } = this; const editor = this.getEditor(); const { editors = [] } = config.grapesjs || {}; + const shallow = this.get('shallow'); + shallow?.destroyAll(); this.stopListening(); this.stopDefault(); this.get('modules') diff --git a/src/editor/view/EditorView.js b/src/editor/view/EditorView.js index c563c14f9..a9c1af266 100644 --- a/src/editor/view/EditorView.js +++ b/src/editor/view/EditorView.js @@ -1,9 +1,10 @@ import Backbone from 'backbone'; +import { View } from 'common'; import { appendStyles } from 'utils/mixins'; const $ = Backbone.$; -export default Backbone.View.extend({ +export default class EditorView extends View { initialize() { const { model } = this; model.view = this; @@ -18,7 +19,7 @@ export default Backbone.View.extend({ model.set('changesCount', 0); }); }); - }, + } render() { const { $el, conf, model } = this; @@ -33,9 +34,15 @@ export default Backbone.View.extend({ $el.append(this.cv.render()); $el.append(this.pn.render()); + // Load shallow editor + const shallow = model.get('shallow'); + const shallowCanvasEl = shallow.get('Canvas').render(); + shallowCanvasEl.style.display = 'none'; + $el.append(shallowCanvasEl); + $el.attr('class', `${pfx}editor ${pfx}one-bg ${pfx}two-color`); contEl.addClass(`${pfx}editor-cont`).empty().append($el); return this; - }, -}); + } +} diff --git a/src/undo_manager/index.js b/src/undo_manager/index.js index 8d45f6e67..9c3f5365c 100644 --- a/src/undo_manager/index.js +++ b/src/undo_manager/index.js @@ -51,6 +51,9 @@ export default () => { config = { ...configDef, ...opts }; em = config.em; this.em = em; + if (config._disable) { + config = { ...config, maximumStackLength: 0 }; + } const fromUndo = true; um = new UndoManager({ track: true, register: [], ...config }); um.changeUndoType('change', {