From 89087fffc6780538a9d69ee20e89482f0b23eb8e Mon Sep 17 00:00:00 2001 From: Artur Arseniev Date: Mon, 13 Apr 2020 00:37:45 +0200 Subject: [PATCH] Init refactor --- src/commands/view/SelectComponent.js | 11 ++++++----- src/dom_components/index.js | 4 ++++ src/editor/index.js | 4 ++-- src/editor/model/Editor.js | 18 ++++++++++++------ test/specs/editor/index.js | 27 +++++++++++++++++++++++++++ 5 files changed, 51 insertions(+), 13 deletions(-) create mode 100644 test/specs/editor/index.js diff --git a/src/commands/view/SelectComponent.js b/src/commands/view/SelectComponent.js index a977c9663..da89922e9 100644 --- a/src/commands/view/SelectComponent.js +++ b/src/commands/view/SelectComponent.js @@ -95,7 +95,7 @@ export default { .getFrames() .forEach(frame => { const { view } = frame; - trigger(view.getWindow(), view.getBody()); + view && trigger(view.getWindow(), view.getBody()); }); }, @@ -204,15 +204,16 @@ export default { this.currentDoc = null; this.em.setHovered(0); this.canvas.getFrames().forEach(frame => { - const el = frame.view.getToolsEl(); - this.toggleToolsEl(0, 0, { el }); + const { view } = frame; + const el = view && view.getToolsEl(); + el && this.toggleToolsEl(0, 0, { el }); }); }, toggleToolsEl(on, view, opts = {}) { const el = opts.el || this.canvas.getToolsEl(view); - el.style.opacity = on ? 1 : 0; - return el; + el && (el.style.opacity = on ? 1 : 0); + return el || {}; }, /** diff --git a/src/dom_components/index.js b/src/dom_components/index.js index 42ee9af6d..74d7b4091 100644 --- a/src/dom_components/index.js +++ b/src/dom_components/index.js @@ -727,6 +727,10 @@ export default () => { }); model && isEmpty(model.get('status')) && model.set('status', state); + }, + + allById() { + return componentsById; } }; }; diff --git a/src/editor/index.js b/src/editor/index.js index 3c5dd40b4..79b1d0395 100644 --- a/src/editor/index.js +++ b/src/editor/index.js @@ -278,8 +278,8 @@ export default (config = {}) => { * @return {this} * @private */ - init() { - em.init(this); + init(opts = {}) { + em.init(this, { ...c, ...opts }); // Do post render stuff after the iframe is loaded otherwise it'll // be empty during tests diff --git a/src/editor/model/Editor.js b/src/editor/model/Editor.js index ccb5a22fe..03a0e22f4 100644 --- a/src/editor/model/Editor.js +++ b/src/editor/model/Editor.js @@ -77,6 +77,7 @@ export default Backbone.Model.extend({ this.set('modules', []); this.set('toLoad', []); this.set('storables', []); + this.set('selected', new Collection()); this.set('dmode', c.dragMode); const el = c.el; const log = c.log; @@ -224,7 +225,11 @@ export default Backbone.Model.extend({ * @return {this} * @private */ - init(editor) { + init(editor, opts = {}) { + if (this.destroyed) { + this.initialize(opts); + this.destroyed = 0; + } this.set('Editor', editor); }, @@ -592,7 +597,8 @@ export default Backbone.Model.extend({ * @private */ stopDefault(opts = {}) { - var command = this.get('Commands').get(this.config.defaultCommand); + const commands = this.get('Commands'); + const command = commands.get(this.config.defaultCommand); if (!command) return; command.stop(this, this, opts); this.defaultRunning = 0; @@ -688,8 +694,9 @@ export default Backbone.Model.extend({ * Destroy editor */ destroyAll() { + const { config } = this; const editor = this.getEditor(); - const { editors } = this.config.grapesjs; + const { editors = [] } = config.grapesjs || {}; const { DomComponents, CssComposer, @@ -710,10 +717,9 @@ export default Backbone.Model.extend({ this.view.remove(); this.stopListening(); this.clear({ silent: true }); - this._previousAttributes = {}; - this.attributes = {}; + this.destroyed = 1; editors.splice(editors.indexOf(editor), 1); - $(this.config.el) + $(config.el) .empty() .attr(this.attrsOrig); }, diff --git a/test/specs/editor/index.js b/test/specs/editor/index.js new file mode 100644 index 000000000..8691efdba --- /dev/null +++ b/test/specs/editor/index.js @@ -0,0 +1,27 @@ +import Editor from 'editor'; + +const { keys } = Object; + +describe('Editor', () => { + const editor = new Editor(); + + beforeEach(() => { + editor.init(); + }); + + afterEach(() => { + editor.destroy(); + }); + + test('Object exists', () => { + expect(editor).toBeTruthy(); + }); + + test('Has no components', () => { + const all = editor.Components.allById(); + const allKeys = keys(all); + // By default 1 wrapper components is created + expect(allKeys.length).toBe(1); + expect(allKeys[0]).toBe('wrapper'); + }); +});