diff --git a/src/block_manager/view/BlocksView.js b/src/block_manager/view/BlocksView.js index 75cfdf25c..e14663450 100644 --- a/src/block_manager/view/BlocksView.js +++ b/src/block_manager/view/BlocksView.js @@ -84,8 +84,6 @@ module.exports = Backbone.View.extend({ model.set('activeOnRender', 0); } - // Register all its components (eg. for the Undo Manager) - em.initChildrenComp(model); em.trigger('block:drag:stop', model); } }, diff --git a/src/commands/view/InsertCustom.js b/src/commands/view/InsertCustom.js index 939578a8b..02bd7fa06 100644 --- a/src/commands/view/InsertCustom.js +++ b/src/commands/view/InsertCustom.js @@ -47,9 +47,6 @@ module.exports = _.extend({}, CreateComponent, { if(!model) return; - if(this.em) - this.em.editor.initChildrenComp(model); - this.afterInsert(model, this); }, diff --git a/src/dom_components/index.js b/src/dom_components/index.js index 40001520e..b61b8a19d 100644 --- a/src/dom_components/index.js +++ b/src/dom_components/index.js @@ -231,8 +231,8 @@ module.exports = () => { onLoad() { const comps = this.getComponents(); comps.reset(c.components); - const um = em && em.get('UndoManager'); - um && um.add(comps); + //const um = em && em.get('UndoManager'); + //um && um.add(comps); }, /** @@ -241,9 +241,44 @@ module.exports = () => { * @private */ postLoad(em) { - em.initChildrenComp(this.getWrapper()); + this.handleChanges(this.getWrapper(), null, { avoidStore: 1 }); }, + + /** + * Handle component changes + * @private + */ + handleChanges(model, value, opts = {}) { + const comps = model.get('components'); + const um = em.get('UndoManager'); + const handleUpdates = em.handleUpdates.bind(em); + um && um.add(model) && comps && um.add(comps); + const evn = 'change:style change:content change:attributes change:src'; + + [ [comps, 'add', this.handleChanges], + [comps, 'remove', this.handleRemoves], + [model, evn, handleUpdates], + [model.get('classes'), 'add remove', handleUpdates], + ].forEach(els => { + em.stopListening(els[0], els[1], els[2]); + em.listenTo(els[0], els[1], els[2]); + }); + + !opts.avoidStore && handleUpdates('', '', opts); + comps.each(model => this.handleChanges(model)); + }, + + + /** + * Triggered when some component is removed + * @private + * */ + handleRemoves(model, value, opts = {}) { + !opts.avoidStore && em.handleUpdates(model, value, opts); + }, + + /** * Load components from the passed object, if the object is empty will try to fetch them * autonomously from the selected storage diff --git a/src/editor/model/Editor.js b/src/editor/model/Editor.js index 610d8040a..db4a4d824 100644 --- a/src/editor/model/Editor.js +++ b/src/editor/model/Editor.js @@ -358,16 +358,15 @@ module.exports = Backbone.Model.extend({ * Init stuff like storage for already existing elements * @param {Object} model * @private - */ + * initChildrenComp(model) { var comps = model.get('components'); this.updateComponents(model, null, { avoidStore : 1 }); - comps.each(function(md) { + comps.each(md => { this.initChildrenComp(md); - if(this.um) - this.um.register(md); - }, this); - }, + this.um && this.um.register(md); + }); + },*/ /** * Triggered when some component is removed updated