From 52f8bdf2e894de536d44c1699c1e18bbaf52488f Mon Sep 17 00:00:00 2001 From: Artur Arseniev Date: Sat, 9 Jun 2018 00:38:49 +0200 Subject: [PATCH] Add destroy method --- src/editor/index.js | 7 +++++++ src/editor/model/Editor.js | 27 +++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/src/editor/index.js b/src/editor/index.js index b2c0d2719..c1f94a21f 100644 --- a/src/editor/index.js +++ b/src/editor/index.js @@ -618,6 +618,13 @@ module.exports = config => { return em.trigger.apply(em, arguments); }, + /** + * Destroy the editor + */ + destroy() { + return em.destroyAll(); + }, + /** * Returns editor element * @return {HTMLElement} diff --git a/src/editor/model/Editor.js b/src/editor/model/Editor.js index c69a9f454..f2f35b31b 100644 --- a/src/editor/model/Editor.js +++ b/src/editor/model/Editor.js @@ -60,6 +60,10 @@ module.exports = Backbone.Model.extend({ this.set('storables', []); if (c.el && c.fromElement) this.config.components = c.el.innerHTML; + this.attrsOrig = [...c.el.attributes].reduce((res, next) => { + res[next.nodeName] = next.nodeValue; + return res; + }, {}); // Load modules deps.forEach(name => this.loadModule(name)); @@ -571,6 +575,29 @@ module.exports = Backbone.Model.extend({ return this.get('changesCount'); }, + /** + * Destroy editor + */ + destroyAll() { + const { + DomComponents, + CssComposer, + UndoManager, + Panels, + Canvas + } = this.attributes; + DomComponents.clear(); + CssComposer.clear(); + UndoManager.clear().removeAll(); + Panels.getPanels().reset(); + Canvas.getCanvasView().remove(); + this.view.remove(); + this.stopListening(); + $(this.config.el) + .empty() + .attr(this.attrsOrig); + }, + /** * Set/get data from the HTMLElement * @param {HTMLElement} el