Browse Source

Fix editor.destroy with selected components. Closes #2681

pull/4072/head
Artur Arseniev 4 years ago
parent
commit
b2d0ac5565
  1. 78
      src/editor/model/Editor.js

78
src/editor/model/Editor.js

@ -5,7 +5,7 @@ import {
contains, contains,
toArray, toArray,
keys, keys,
bindAll bindAll,
} from 'underscore'; } from 'underscore';
import Backbone from 'backbone'; import Backbone from 'backbone';
import $ from 'utils/cash-dom'; import $ from 'utils/cash-dom';
@ -37,7 +37,7 @@ const deps = [
require('navigator'), require('navigator'),
require('canvas'), require('canvas'),
require('commands'), require('commands'),
require('block_manager') require('block_manager'),
]; ];
let timedInterval; let timedInterval;
@ -45,14 +45,14 @@ let updateItr;
Extender({ Extender({
Backbone: Backbone, Backbone: Backbone,
$: Backbone.$ $: Backbone.$,
}); });
const logs = { const logs = {
debug: console.log, debug: console.log,
info: console.info, info: console.info,
warning: console.warn, warning: console.warn,
error: console.error error: console.error,
}; };
export default Backbone.Model.extend({ export default Backbone.Model.extend({
@ -69,7 +69,7 @@ export default Backbone.Model.extend({
modules: [], modules: [],
toLoad: [], toLoad: [],
opened: {}, opened: {},
device: '' device: '',
}; };
}, },
@ -96,15 +96,15 @@ export default Backbone.Model.extend({
: ''; : '';
// Load modules // Load modules
deps.forEach(name => this.loadModule(name)); deps.forEach((name) => this.loadModule(name));
this.on('change:componentHovered', this.componentHovered, this); this.on('change:componentHovered', this.componentHovered, this);
this.on('change:changesCount', this.updateChanges, this); this.on('change:changesCount', this.updateChanges, this);
this.on('change:readyLoad change:readyCanvas', this._checkReady, this); this.on('change:readyLoad change:readyCanvas', this._checkReady, this);
toLog.forEach(e => this.listenLog(e)); toLog.forEach((e) => this.listenLog(e));
// Deprecations // Deprecations
[{ from: 'change:selectedComponent', to: 'component:toggled' }].forEach( [{ from: 'change:selectedComponent', to: 'component:toggled' }].forEach(
event => { (event) => {
const eventFrom = event.from; const eventFrom = event.from;
const eventTo = event.to; const eventTo = event.to;
this.listenTo(this, eventFrom, (...args) => { this.listenTo(this, eventFrom, (...args) => {
@ -156,14 +156,14 @@ export default Backbone.Model.extend({
// Generally, with `onLoad`, the module will try to load the data from // Generally, with `onLoad`, the module will try to load the data from
// its configurations // its configurations
this.get('toLoad').forEach(module => { this.get('toLoad').forEach((module) => {
module.onLoad(); module.onLoad();
}); });
// Stuff to do post load // Stuff to do post load
const postLoad = () => { const postLoad = () => {
const modules = this.get('modules'); const modules = this.get('modules');
modules.forEach(module => module.postLoad && module.postLoad(this)); modules.forEach((module) => module.postLoad && module.postLoad(this));
this.set('readyLoad', 1); this.set('readyLoad', 1);
clb && clb(); clb && clb();
}; };
@ -187,7 +187,7 @@ export default Backbone.Model.extend({
updateItr = setTimeout(() => this.trigger('update')); updateItr = setTimeout(() => this.trigger('update'));
if (this.config.noticeOnUnload) { if (this.config.noticeOnUnload) {
window.onbeforeunload = changes ? e => 1 : null; window.onbeforeunload = changes ? (e) => 1 : null;
} }
if (stm.isAutosave() && changes >= stm.getStepsBeforeSave()) { if (stm.isAutosave() && changes >= stm.getStepsBeforeSave()) {
@ -320,16 +320,16 @@ export default Backbone.Model.extend({
const ctrlKey = event && (event.ctrlKey || event.metaKey); const ctrlKey = event && (event.ctrlKey || event.metaKey);
const { shiftKey } = event || {}; const { shiftKey } = event || {};
const multiple = isArray(el); const multiple = isArray(el);
const els = (multiple ? el : [el]).map(el => getModel(el, $)); const els = (multiple ? el : [el]).map((el) => getModel(el, $));
const selected = this.getSelectedAll(); const selected = this.getSelectedAll();
const mltSel = this.getConfig('multipleSelection'); const mltSel = this.getConfig('multipleSelection');
let added; let added;
// If an array is passed remove all selected // If an array is passed remove all selected
// expect those yet to be selected // expect those yet to be selected
multiple && this.removeSelected(selected.filter(s => !contains(els, s))); multiple && this.removeSelected(selected.filter((s) => !contains(els, s)));
els.forEach(el => { els.forEach((el) => {
const model = getModel(el, $); const model = getModel(el, $);
if (model && !model.get('selectable')) return; if (model && !model.get('selectable')) return;
@ -343,7 +343,7 @@ export default Backbone.Model.extend({
let min, max; let min, max;
// Fin min and max siblings // Fin min and max siblings
this.getSelectedAll().forEach(sel => { this.getSelectedAll().forEach((sel) => {
const selColl = sel.collection; const selColl = sel.collection;
const selIndex = sel.index(); const selIndex = sel.index();
if (selColl === coll) { if (selColl === coll) {
@ -374,7 +374,7 @@ export default Backbone.Model.extend({
return this.addSelected(model); return this.addSelected(model);
} }
!multiple && this.removeSelected(selected.filter(s => s !== model)); !multiple && this.removeSelected(selected.filter((s) => s !== model));
this.addSelected(model, opts); this.addSelected(model, opts);
added = model; added = model;
}); });
@ -390,7 +390,7 @@ export default Backbone.Model.extend({
const model = getModel(el, $); const model = getModel(el, $);
const models = isArray(model) ? model : [model]; const models = isArray(model) ? model : [model];
models.forEach(model => { models.forEach((model) => {
if (model && !model.get('selectable')) return; if (model && !model.get('selectable')) return;
const selected = this.get('selected'); const selected = this.get('selected');
opts.forceChange && this.removeSelected(model, opts); opts.forceChange && this.removeSelected(model, opts);
@ -418,7 +418,7 @@ export default Backbone.Model.extend({
const model = getModel(el, $); const model = getModel(el, $);
const models = isArray(model) ? model : [model]; const models = isArray(model) ? model : [model];
models.forEach(model => { models.forEach((model) => {
if (this.get('selected').hasComponent(model)) { if (this.get('selected').hasComponent(model)) {
this.removeSelected(model, opts); this.removeSelected(model, opts);
} else { } else {
@ -538,7 +538,7 @@ export default Backbone.Model.extend({
exportWrapper, exportWrapper,
wrapperIsBody, wrapperIsBody,
...optsHtml, ...optsHtml,
...opts ...opts,
}) })
: ''; : '';
html += js ? `<script>${js}</script>` : ''; html += js ? `<script>${js}</script>` : '';
@ -568,7 +568,7 @@ export default Backbone.Model.extend({
wrapperIsBody, wrapperIsBody,
keepUnusedStyles, keepUnusedStyles,
...optsCss, ...optsCss,
...opts ...opts,
}); });
return wrp ? (opts.json ? css : protCss + css) : ''; return wrp ? (opts.json ? css : protCss + css) : '';
}, },
@ -580,11 +580,7 @@ export default Backbone.Model.extend({
*/ */
getJs(opts = {}) { getJs(opts = {}) {
var wrp = opts.component || this.get('DomComponents').getWrapper(); var wrp = opts.component || this.get('DomComponents').getWrapper();
return wrp return wrp ? this.get('CodeManager').getCode(wrp, 'js').trim() : '';
? this.get('CodeManager')
.getCode(wrp, 'js')
.trim()
: '';
}, },
/** /**
@ -598,7 +594,7 @@ export default Backbone.Model.extend({
if (!sm) return; if (!sm) return;
const store = this.storeData(); const store = this.storeData();
sm.store(store, res => { sm.store(store, (res) => {
clb && clb(res, store); clb && clb(res, store);
this.set('changesCount', 0); this.set('changesCount', 0);
this.trigger('storage:store', store); this.trigger('storage:store', store);
@ -613,7 +609,7 @@ export default Backbone.Model.extend({
const editingCmp = this.getEditing(); const editingCmp = this.getEditing();
editingCmp && editingCmp.trigger('sync:content', { noCount: true }); editingCmp && editingCmp.trigger('sync:content', { noCount: true });
this.get('storables').forEach(m => { this.get('storables').forEach((m) => {
result = { ...result, ...m.store(1) }; result = { ...result, ...m.store(1) };
}); });
return result; return result;
@ -625,7 +621,7 @@ export default Backbone.Model.extend({
* @private * @private
*/ */
load(clb = null) { load(clb = null) {
this.getCacheLoad(1, res => { this.getCacheLoad(1, (res) => {
this.loadData(res); this.loadData(res);
clb && clb(res); clb && clb(res);
}); });
@ -635,7 +631,7 @@ export default Backbone.Model.extend({
const sm = this.get('StorageManager'); const sm = this.get('StorageManager');
const result = sm.__clearKeys(data); const result = sm.__clearKeys(data);
this.get('storables').forEach(module => { this.get('storables').forEach((module) => {
module.load(result); module.load(result);
module.postLoad && module.postLoad(this); module.postLoad && module.postLoad(this);
}); });
@ -657,14 +653,14 @@ export default Backbone.Model.extend({
if (!sm) return {}; if (!sm) return {};
this.get('storables').forEach(m => { this.get('storables').forEach((m) => {
let key = m.storageKey; let key = m.storageKey;
key = isFunction(key) ? key() : key; key = isFunction(key) ? key() : key;
const keys = isArray(key) ? key : [key]; const keys = isArray(key) ? key : [key];
keys.forEach(k => load.push(k)); keys.forEach((k) => load.push(k));
}); });
sm.load(load, res => { sm.load(load, (res) => {
this.cacheLoad = res; this.cacheLoad = res;
clb && clb(res); clb && clb(res);
setTimeout(() => this.trigger('storage:load', res)); setTimeout(() => this.trigger('storage:load', res));
@ -783,7 +779,8 @@ export default Backbone.Model.extend({
}, },
t(...args) { t(...args) {
return this.get('I18n').t(...args); const i18n = this.get('I18n');
return i18n?.t(...args);
}, },
/** /**
@ -801,23 +798,20 @@ export default Backbone.Model.extend({
const { config, view } = this; const { config, view } = this;
const editor = this.getEditor(); const editor = this.getEditor();
const { editors = [] } = config.grapesjs || {}; const { editors = [] } = config.grapesjs || {};
this.stopListening();
this.stopDefault(); this.stopDefault();
this.get('modules') this.get('modules')
.slice() .slice()
.reverse() .reverse()
.forEach(mod => mod.destroy()); .forEach((mod) => mod.destroy());
view && view.remove(); view && view.remove();
this.stopListening();
this.clear({ silent: true }); this.clear({ silent: true });
this.destroyed = 1; this.destroyed = 1;
['config', 'view', '_previousAttributes', '_events', '_listeners'].forEach( ['config', 'view', '_previousAttributes', '_events', '_listeners'].forEach(
i => (this[i] = {}) (i) => (this[i] = {})
); );
editors.splice(editors.indexOf(editor), 1); editors.splice(editors.indexOf(editor), 1);
hasWin() && hasWin() && $(config.el).empty().attr(this.attrsOrig);
$(config.el)
.empty()
.attr(this.attrsOrig);
}, },
getEditing() { getEditing() {
@ -874,7 +868,7 @@ export default Backbone.Model.extend({
chooseText: 'Ok', chooseText: 'Ok',
cancelText: '⨯', cancelText: '⨯',
...opts, ...opts,
...colorPicker ...colorPicker,
}); });
}, },
@ -898,5 +892,5 @@ export default Backbone.Model.extend({
} else { } else {
el[varName][name] = value; el[varName][name] = value;
} }
} },
}); });

Loading…
Cancel
Save