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()
.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 || {};
},
/**

4
src/dom_components/index.js

@ -727,6 +727,10 @@ export default () => {
});
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}
* @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

18
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);
},

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