editor: reuse children in almost every case

This commit is contained in:
jean-emmanuel 2018-10-17 13:14:24 +02:00
parent 4742303418
commit 171544a3d2
9 changed files with 128 additions and 71 deletions

View file

@ -165,8 +165,10 @@ var handleClick = function(event) {
if (parent === widgetManager) {
actions['<i class="fa fa-plus"></i> ' + locales('editor_addtab')] = ()=>{
data[0].tabs.push({})
updateWidget(widget)
editor.pushHistory()
var indexes = {addedIndexes: [data[0].tabs.length -1]}
updateWidget(widget, indexes)
editor.pushHistory(indexes)
}
contextMenu.open(eventData, actions)
@ -214,8 +216,8 @@ var handleClick = function(event) {
parent.props.widgets = parent.props.widgets.slice(0, i).concat(wrap, parent.props.widgets.slice(i, parent.props.widgets.length))
editor.select(updateWidget(parent, {preventSelect: true}))
editor.pushHistory()
editor.select(updateWidget(parent, {preventSelect: true, removedIndexes: index, addedIndexes: [i]}))
editor.pushHistory({removedIndexes: index, addedIndexes: [i]})
}
}
@ -266,8 +268,10 @@ var handleClick = function(event) {
data[0].widgets = data[0].widgets || []
data[0].widgets.push(newData)
updateWidget(editor.selectedWidgets[0])
editor.pushHistory()
var indexes = {addedIndexes: [data[0].widgets.length -1]}
updateWidget(editor.selectedWidgets[0], indexes)
editor.pushHistory(indexes)
}
@ -282,8 +286,10 @@ var handleClick = function(event) {
actions['<i class="fa fa-plus"></i> ' + locales('editor_addtab')] = ()=>{
data[0].tabs = data[0].tabs || []
data[0].tabs.push({})
updateWidget(editor.selectedWidgets[0])
editor.pushHistory()
var indexes = {addedIndexes: [data[0].tabs.length -1]}
updateWidget(editor.selectedWidgets[0], indexes)
editor.pushHistory(indexes)
}

View file

@ -12,6 +12,7 @@ DOM.ready(()=>{
function updateWidget(widget, options={}) {
// save state
stateManager.incrementQueue()
var toSave = [widget],
@ -34,28 +35,56 @@ function updateWidget(widget, options={}) {
}
}
var reuseChildren = options.reuseChildren !== false && widget instanceof Panel,
children = undefined,
removedChildren = []
if (reuseChildren) {
children = widget.children
if (options.removedIndexes) {
options.removedIndexes = options.removedIndexes.sort((a, b) => b - a)
for (let i of options.removedIndexes) {
removedChildren.push(children.splice(i, 1)[0])
}
}
if (options.addedIndexes) {
options.addedIndexes = options.addedIndexes.sort((a, b) => a - b)
for (let i of options.addedIndexes) {
children.splice(i, 0, null)
}
}
}
// get widget's index
var index = widget.parent.children.indexOf(widget)
// remove old widgets
var reuseChildren = options.reuseChildren !== false && widget instanceof Panel
var removedWidgets = reuseChildren ?
(options.removedChildren || []).map(x => x.getAllChildren()).concat(widget) :
removedChildren.map(x => x.getAllChildren()).concat(widget) :
widget.getAllChildren().concat(widget)
widgetManager.removeWidgets(removedWidgets)
// retreive widget's data from it's parent if possible
var parentProps = widget.parent.props[widget.getProp('type') == 'tab' ? 'tabs' : 'widgets'],
data = parentProps ? parentProps[index] : widget.props
// create new widget
var newWidget = parser.parse({
data: widget.props,
data: data,
parent: widget.parent,
parentNode: widget.parentNode,
reCreateOptions: options.reCreateOptions,
children: reuseChildren ? widget.children : undefined
children: children,
index: index
})
widget.container.parentNode.replaceChild(newWidget.container, widget.container)
if (newWidget.getProp('type') == 'tab') newWidget.parent.trigger('tab-created', [{widget: widget}])
if (newWidget.getProp('id') == 'root') DOM.get('.editor-root')[0].setAttribute('data-widget', newWidget.hash)
newWidget.container.parentNode.replaceChild(newWidget.container, widget.container)
if (newWidget.getProp('type') === 'tab') newWidget.parent.trigger('tab-created', [{widget: widget}])
if (newWidget.getProp('id') === 'root') DOM.get('.editor-root')[0].setAttribute('data-widget', newWidget.hash)
if (reuseChildren && !(newWidget instanceof Panel)) {

View file

@ -519,7 +519,7 @@ var Editor = class Editor {
parent = this.selectedWidgets[0].parent,
index = this.selectedWidgets.map(w => parent.children.indexOf(w)).sort((a,b)=>{return b-a}),
data = this.selectedWidgets.map((w)=>w.props),
removedChildren = []
removedIndexes = []
if (type !== 'widget') return
@ -532,15 +532,15 @@ var Editor = class Editor {
}
for (var i of index) {
removedChildren.push(parent.children.splice(i,1)[0])
removedIndexes.push(i)
parent.props.widgets.splice(i,1)
}
this.select(updateWidget(parent, {
preventSelect: true,
removedChildren
removedIndexes
}))
this.pushHistory()
this.pushHistory({removedIndexes})
}
@ -574,8 +574,9 @@ var Editor = class Editor {
data[0].widgets = data[0].widgets || []
data[0].widgets = data[0].widgets.concat(pastedData)
updateWidget(this.selectedWidgets[0])
this.pushHistory()
var indexes = {addedIndexes: [data[0].widgets.length -1]}
updateWidget(this.selectedWidgets[0], indexes)
this.pushHistory(indexes)
}
@ -601,8 +602,9 @@ var Editor = class Editor {
data[0].widgets = data[0].widgets || []
data[0].widgets.push(clone)
updateWidget(this.selectedWidgets[0])
this.pushHistory()
var indexes = {addedIndexes: [data[0].widgets.length -1]}
updateWidget(this.selectedWidgets[0], indexes)
this.pushHistory(indexes)
}
@ -614,27 +616,27 @@ var Editor = class Editor {
var type = this.selectedWidgets[0].props.type == 'tab' ? 'tab' : 'widget',
parent = this.selectedWidgets[0].parent,
index = this.selectedWidgets.map(w => parent.children.indexOf(w)).sort((a,b)=>{return b-a}),
removedChildren = []
removedIndexes = []
if (this.selectedWidgets[0].getProp('id') === 'root') return
if (type === 'widget') {
for (let i of index) {
removedChildren.push(parent.children.splice(i,1)[0])
removedIndexes.push(i)
parent.props.widgets.splice(i,1)
}
} else {
for (let i of index) {
removedChildren.push(parent.children.splice(i,1)[0])
removedIndexes.push(i)
parent.props.tabs.splice(i,1)
}
}
this.select(updateWidget(parent, {
preventSelect: true,
removedChildren
removedIndexes
}))
this.pushHistory()
this.pushHistory({removedIndexes})
}
@ -719,7 +721,7 @@ var Editor = class Editor {
pushHistory(infos) {
pushHistory(indexes) {
if (this.historyState > -1) {
this.history.splice(0, this.historyState + 1)
@ -732,7 +734,7 @@ var Editor = class Editor {
for (var c of deepCopy(d)) {
diff.applyChange(this.historySession, null, c)
}
this.history.unshift([deepCopy(d), infos])
this.history.unshift([deepCopy(d), indexes])
if (this.history.length > HISTORY_SIZE) this.history.pop()
}
@ -752,7 +754,7 @@ var Editor = class Editor {
this.historyState += 1
var [patch, infos] = this.history[this.historyState],
var [patch, indexes] = this.history[this.historyState],
d1 = deepCopy(patch),
d2 = deepCopy(patch),
path
@ -779,8 +781,10 @@ var Editor = class Editor {
path.splice(n, path.length - n)
}
this.updateWidgetByPath(path)
this.updateWidgetByPath(path, indexes ? {
addedIndexes: deepCopy(indexes.removedIndexes),
removedIndexes: deepCopy(indexes.addedIndexes),
} : {})
}
@ -788,7 +792,7 @@ var Editor = class Editor {
if (this.historyState === -1) return
var [patch, infos] = this.history[this.historyState],
var [patch, indexes] = this.history[this.historyState],
d1 = deepCopy(patch),
d2 = deepCopy(patch),
path
@ -816,13 +820,16 @@ var Editor = class Editor {
}
this.updateWidgetByPath(path)
this.updateWidgetByPath(path, indexes ? {
addedIndexes: deepCopy(indexes.addedIndexes),
removedIndexes: deepCopy(indexes.removedIndexes),
} : {})
this.historyState -= 1
}
updateWidgetByPath(path) {
updateWidgetByPath(path, indexes) {
//TODO traverse widget.children instead of querying the dom
@ -850,9 +857,7 @@ var Editor = class Editor {
w = widgetManager.getWidgetById('root')[0]
}
updateWidget(w, {
reuseChildren: false
})
updateWidget(w, indexes)
}

View file

@ -8,6 +8,7 @@ var WidgetManager = class WidgetManager extends EventEmitter {
super()
this.widgets = {}
this.children = []
this.addressRoute = {}
this.idRoute = {}

View file

@ -26,7 +26,15 @@ var Parser = class Parser {
parse(options) {
var {data, parentNode, parent, tab, reCreateOptions, children} = options
var {
data,
parentNode,
parent,
tab,
reCreateOptions,
children,
index
} = options
if (!Array.isArray(data)) data = [data]
@ -80,7 +88,7 @@ var Parser = class Parser {
widgetManager.addWidget(widget)
widget.created()
widget.created(index)
// set widget's initial state
var defaultValue = widget.getProp('default'),
@ -96,8 +104,9 @@ var Parser = class Parser {
}
// Append the widget to its parent
parentNode.appendChild(widget.container)
}
// Editor needs to get the container object

View file

@ -11,7 +11,17 @@ class Container extends Widget {
this.on('widget-created', (e)=>{
if (e.widget.parent === this) {
this.children.push(e.widget)
var index = e.index
if (index !== undefined) {
if (!this.children[index]) {
this.children[index] = e.widget
} else {
this.children.splice(index, 0, e.widget)
}
} else {
this.children.push(e.widget)
}
}
})

View file

@ -250,6 +250,7 @@ class Widget extends EventEmitter {
var children = []
for (var i = 0; i < this.children.length; i++) {
if (!this.children[i]) continue
children.push(this.children[i])
children = children.concat(this.children[i].getAllChildren())
}
@ -258,12 +259,13 @@ class Widget extends EventEmitter {
}
created() {
created(index) {
this.trigger('widget-created', [{
id: this.getProp('id'),
widget: this,
options: this.reCreateOptions
options: this.reCreateOptions,
index: index
}])
}

View file

@ -158,7 +158,6 @@ class Clone extends Container {
}])
}
for (var w of this.getAllChildren()) {
w.container.classList.add('not-editable')
}

View file

@ -50,24 +50,22 @@ class Panel extends Container {
this.wrapper = this.widget.appendChild(DOM.create('<div class="tabs-wrapper"></div>'))
if (options.children && options.children.length) {
// reuse old children
this.children = options.children
for (let i = 0; i < this.children.length; i++) {
this.children = options.children || new Array(this.getProp('tabs').length)
for (let i = 0; i < this.children.length; i++) {
if (this.children[i]) {
this.wrapper.appendChild(this.children[i].container)
this.children[i].parent = this
this.children[i].parentNode = this.wrapper
} else {
parser.parse({
data: this.getProp('tabs')[i],
parentNode: this.wrapper,
parent: this,
tab: true,
index: i
})
}
}
if (this.getProp('tabs').length > this.children.length) {
// parse new children
parser.parse({
data: this.getProp('tabs').slice(this.children.length),
parentNode: this.wrapper,
parent: this,
tab: true
})
}
this.createNavigation()
@ -86,23 +84,21 @@ class Panel extends Container {
} else if (this.getProp('widgets') && this.getProp('widgets').length) {
if (options.children && options.children.length) {
// reuse old children
this.children = options.children
for (let i = 0; i < this.children.length; i++) {
this.children = options.children || new Array(this.getProp('widgets').length)
for (let i = 0; i < this.children.length; i++) {
if (this.children[i]) {
this.widget.appendChild(this.children[i].container)
this.children[i].parent = this
this.children[i].parentNode = this.widget
} else {
parser.parse({
data: this.getProp('widgets')[i],
parentNode: this.widget,
parent: this,
index: i
})
}
}
if (this.getProp('widgets').length > this.children.length) {
// parse new children
parser.parse({
data: this.getProp('widgets').slice(this.children.length),
parentNode: this.widget,
parent: this
})
}
}