diff --git a/CHANGELOG.md b/CHANGELOG.md index 9c1d32b6..7f9f4f3e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ - widgets: css: @media queries parsing - prevent some errors when synchronizing clients - use command key instead of ctrl for zooming on mac + - clone: memory leak / freeze - default client - mac: disabled unsupported pinch zoom - themes diff --git a/src/browser/js/app/managers/widgets.js b/src/browser/js/app/managers/widgets.js index 65027e44..0d2274ee 100644 --- a/src/browser/js/app/managers/widgets.js +++ b/src/browser/js/app/managers/widgets.js @@ -139,11 +139,15 @@ var WidgetManager = class WidgetManager extends EventEmitter { if (this.widgets[hash]) { if (this.widgets[hash].onRemove) this.widgets[hash].onRemove() + delete this.widgets[hash] ipc.send('removeWidget', { hash:hash }) + + this.trigger(/^widget-removed(\..*)?/, [{widget: widget}]) + } if (id && this.idRoute[id].indexOf(hash) != -1) this.idRoute[id].splice(this.idRoute[id].indexOf(hash), 1) if (address && this.addressRoute[address].indexOf(hash) != -1) this.addressRoute[address].splice(this.addressRoute[address].indexOf(hash), 1) diff --git a/src/browser/js/app/widgets/common/widget.js b/src/browser/js/app/widgets/common/widget.js index cee02e19..d1455e94 100644 --- a/src/browser/js/app/widgets/common/widget.js +++ b/src/browser/js/app/widgets/common/widget.js @@ -141,6 +141,19 @@ class Widget extends EventEmitter { } + contains(widget) { + + if (this.childrenHashes.includes(widget.hash)) { + return true + } + + var parent = widget.parent + while (parent && parent !== widgetManager) { + if (parent === this) return true + parent = parent.parent + } + } + created() { this.trigger(/^widget-created(\..*)?/, [{ diff --git a/src/browser/js/app/widgets/containers/clone.js b/src/browser/js/app/widgets/containers/clone.js index 80c1e2cd..5e32322e 100644 --- a/src/browser/js/app/widgets/containers/clone.js +++ b/src/browser/js/app/widgets/containers/clone.js @@ -41,70 +41,100 @@ module.exports = class Clone extends Widget { super({...options, html: '
'}) this.cloneLock = false - this.cloneClass = '' - this.createClone() + this.cloneTarget = null + this.cloneClass = '_' + + this.getCloneTarget() + if (this.cloneTarget) this.createClone() widgetManager.on(`widget-created.${this.hash}`, (e)=>{ var {id, widget} = e if ( - id == this.getProp('widgetId') && - !(widget.parent && widget.parent.getProp('type') == 'clone') + this.cloneTarget === null && + id === this.getProp('widgetId') && + this.isValidCloneTarget(widget) ) { - this.createClone(widget) + this.cloneTarget = widget + this.createClone() resize.check(this.container) } }) - this.on('widget-created', (e)=>{ - if (e.widget == this) return - e.widget.reCreateWidget = ()=>{} + widgetManager.on(`widget-removed.${this.hash}`, (e)=>{ + if (this.cloneTarget === e.widget) { + this.cloneTarget = null + this.cleanClone() + } }) } - createClone(widget) { + getCloneTarget() { - if (this.cloneLock) return - - this.cloneLock = true - - this.widget.innerHTML = '' - if (this.cloneClass) this.container.classList.remove(this.cloneClass) - - widgetManager.removeWidgets(this.childrenHashes) - - var widgets = widget ? [widget] : widgetManager.getWidgetById(this.getProp('widgetId')) - - if (widgets.length) { - - for (var i = widgets.length - 1; i>=0; i--) { - - if (!(widgets[i].parent && widgets[i].parent.getProp('type') == 'clone')) { - - this.cloneClass = widgets[i].container.getAttribute('class').match(/[^\s]*-container/)[0] - this.container.classList.add(this.cloneClass) - - parser.parse([{...Widget.deepCopy(widgets[i].props), ...this.getProp('props')}], this.widget, this) - - DOM.each(this.widget, '.widget', (el)=>{el.classList.add('not-editable')}) - - widgets[i].once(`widget-created.${this.hash}`, (e)=>{ - this.createClone(widgets[i]) - resize.check(this.container) - }) - - break - - } + var widgets = widgetManager.getWidgetById(this.getProp('widgetId')) + for (var i in widgets) { + if ( + this.isValidCloneTarget(widgets[i]) + ) { + this.cloneTarget = widgets[i] + break } - } + } + + isValidCloneTarget(widget) { + + if (widget.contains(this) || this.contains(widget)) return false + + var parent = widget.parent + while (parent && parent !== widgetManager) { + if (parent.getProp('type') === 'clone') return false + parent = parent.parent + } + + return true + } + + cleanClone() { + + widgetManager.removeWidgets(this.childrenHashes) + this.widget.innerHTML = '' + this.childrenHashes = [] + this.container.classList.remove(this.cloneClass) + + } + + createClone() { + + if (this.cloneLock) return + this.cloneLock = true + + this.cleanClone() + + this.cloneClass = this.cloneTarget.container.getAttribute('class').match(/[^\s]*-container/)[0] + this.container.classList.add(this.cloneClass) + + parser.parse([{...Widget.deepCopy(this.cloneTarget.props), ...this.getProp('props')}], this.widget, this) + + DOM.each(this.widget, '.widget', (el)=>{el.classList.add('not-editable')}) + + this.cloneTarget.once(`widget-created.${this.hash}`, (e)=>{ + this.createClone(this.cloneTarget) + resize.check(this.container) + }) this.cloneLock = false } + onRemove(){ + + if (this.cloneTarget) this.cloneTarget.off(`widget-created.${this.hash}`) + widgetManager.off(`widget-removed.${this.hash}`) + super.onRemove() + + } }