Browse Source

Init refactor

pull/2732/head
Artur Arseniev 6 years ago
parent
commit
89087fffc6
  1. 11
      src/commands/view/SelectComponent.js
  2. 4
      src/dom_components/index.js
  3. 4
      src/editor/index.js
  4. 18
      src/editor/model/Editor.js
  5. 27
      test/specs/editor/index.js

11
src/commands/view/SelectComponent.js

@ -95,7 +95,7 @@ export default {
.getFrames() .getFrames()
.forEach(frame => { .forEach(frame => {
const { view } = 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.currentDoc = null;
this.em.setHovered(0); this.em.setHovered(0);
this.canvas.getFrames().forEach(frame => { this.canvas.getFrames().forEach(frame => {
const el = frame.view.getToolsEl(); const { view } = frame;
this.toggleToolsEl(0, 0, { el }); const el = view && view.getToolsEl();
el && this.toggleToolsEl(0, 0, { el });
}); });
}, },
toggleToolsEl(on, view, opts = {}) { toggleToolsEl(on, view, opts = {}) {
const el = opts.el || this.canvas.getToolsEl(view); const el = opts.el || this.canvas.getToolsEl(view);
el.style.opacity = on ? 1 : 0; el && (el.style.opacity = on ? 1 : 0);
return el; return el || {};
}, },
/** /**

4
src/dom_components/index.js

@ -727,6 +727,10 @@ export default () => {
}); });
model && isEmpty(model.get('status')) && model.set('status', state); model && isEmpty(model.get('status')) && model.set('status', state);
},
allById() {
return componentsById;
} }
}; };
}; };

4
src/editor/index.js

@ -278,8 +278,8 @@ export default (config = {}) => {
* @return {this} * @return {this}
* @private * @private
*/ */
init() { init(opts = {}) {
em.init(this); em.init(this, { ...c, ...opts });
// Do post render stuff after the iframe is loaded otherwise it'll // Do post render stuff after the iframe is loaded otherwise it'll
// be empty during tests // be empty during tests

18
src/editor/model/Editor.js

@ -77,6 +77,7 @@ export default Backbone.Model.extend({
this.set('modules', []); this.set('modules', []);
this.set('toLoad', []); this.set('toLoad', []);
this.set('storables', []); this.set('storables', []);
this.set('selected', new Collection());
this.set('dmode', c.dragMode); this.set('dmode', c.dragMode);
const el = c.el; const el = c.el;
const log = c.log; const log = c.log;
@ -224,7 +225,11 @@ export default Backbone.Model.extend({
* @return {this} * @return {this}
* @private * @private
*/ */
init(editor) { init(editor, opts = {}) {
if (this.destroyed) {
this.initialize(opts);
this.destroyed = 0;
}
this.set('Editor', editor); this.set('Editor', editor);
}, },
@ -592,7 +597,8 @@ export default Backbone.Model.extend({
* @private * @private
*/ */
stopDefault(opts = {}) { 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; if (!command) return;
command.stop(this, this, opts); command.stop(this, this, opts);
this.defaultRunning = 0; this.defaultRunning = 0;
@ -688,8 +694,9 @@ export default Backbone.Model.extend({
* Destroy editor * Destroy editor
*/ */
destroyAll() { destroyAll() {
const { config } = this;
const editor = this.getEditor(); const editor = this.getEditor();
const { editors } = this.config.grapesjs; const { editors = [] } = config.grapesjs || {};
const { const {
DomComponents, DomComponents,
CssComposer, CssComposer,
@ -710,10 +717,9 @@ export default Backbone.Model.extend({
this.view.remove(); this.view.remove();
this.stopListening(); this.stopListening();
this.clear({ silent: true }); this.clear({ silent: true });
this._previousAttributes = {}; this.destroyed = 1;
this.attributes = {};
editors.splice(editors.indexOf(editor), 1); editors.splice(editors.indexOf(editor), 1);
$(this.config.el) $(config.el)
.empty() .empty()
.attr(this.attrsOrig); .attr(this.attrsOrig);
}, },

27
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');
});
});
Loading…
Cancel
Save