editor: change diffing algo for one that does unpatch well

This commit is contained in:
jean-emmanuel 2018-10-17 18:18:27 +02:00
parent 16a05e156c
commit 5acd6bf8cf
2 changed files with 30 additions and 78 deletions

View file

@ -2,7 +2,7 @@ var {widgets} = require('../widgets/'),
editField = require('./edit-field'),
{updateWidget, incrementWidget} = require('./data-workers'),
keyboardJS = require('keyboardjs'),
diff = require('deep-diff'),
diff = require('jsondiffpatch'),
widgetManager = require('../managers/widgets'),
{deepCopy} = require('../utils'),
macOs = (navigator.platform || '').match('Mac'),
@ -737,9 +737,7 @@ var Editor = class Editor {
var d = diff.diff(this.historySession, sessionManager.session)
if (d) {
for (var c of deepCopy(d)) {
diff.applyChange(this.historySession, null, c)
}
diff.patch(this.historySession, deepCopy(d))
this.history.unshift([deepCopy(d), indexes])
if (this.history.length > HISTORY_SIZE) this.history.pop()
}
@ -762,32 +760,12 @@ var Editor = class Editor {
var [patch, indexes] = this.history[this.historyState],
d1 = deepCopy(patch),
d2 = deepCopy(patch),
path
d2 = deepCopy(patch)
for (var i = d1.length - 1; i > -1; i--) {
diff.revertChange(this.historySession, true, d1[i])
diff.revertChange(sessionManager.session, true, d2[i])
if (!path || path.length > d1[i].path.length) path = d1[i].path
}
diff.unpatch(this.historySession, d1)
diff.unpatch(sessionManager.session, d2)
if (d1.length > 1) {
// path intersection
var stop, j, k, n
for (j = 0; j < path.length; j++) {
for (k = 0; k < d1.length; k++) {
if (d1[k].path[j] !== path[j]) {
stop = true
break
}
}
if (stop) break
}
n = stop ? j : j - 1
path.splice(n, path.length - n)
}
this.updateWidgetByPath(path, indexes ? {
this.updateWidgetFromPatch(patch, indexes ? {
addedIndexes: deepCopy(indexes.removedIndexes),
removedIndexes: deepCopy(indexes.addedIndexes),
} : {})
@ -800,33 +778,12 @@ var Editor = class Editor {
var [patch, indexes] = this.history[this.historyState],
d1 = deepCopy(patch),
d2 = deepCopy(patch),
path
d2 = deepCopy(patch)
for (var i = 0; i < d1.length; i++) {
diff.applyChange(this.historySession, true, d1[i])
diff.applyChange(sessionManager.session, true, d2[i])
if (!path || path.length > d1[i].path.length) path = d1[i].path
}
diff.patch(this.historySession, d1)
diff.patch(sessionManager.session, d2)
if (d1.length > 1) {
// path intersection
var stop, j, k, n
for (j = 0; j < path.length; j++) {
for (k = 0; k < d1.length; k++) {
if (d1[k].path[j] !== path[j]) {
stop = true
break
}
}
if (stop) break
}
n = stop ? j : j - 1
path.splice(n, path.length - n)
}
this.updateWidgetByPath(path, indexes ? {
this.updateWidgetFromPatch(patch, indexes ? {
addedIndexes: deepCopy(indexes.addedIndexes),
removedIndexes: deepCopy(indexes.removedIndexes),
} : {})
@ -835,36 +792,31 @@ var Editor = class Editor {
}
updateWidgetByPath(path, indexes) {
updateWidgetFromPatch(patch, indexes) {
//TODO traverse widget.children instead of querying the dom
var widget = widgetManager.getWidgetById('root')[0],
pointer = patch[0],
nextKey
var req = '.root-container'
for (var j = 1; j < path.length; j++) {
var item = path[j]
if (item === 'widgets' && j + 1 < path.length) {
req += ' > .panel'
} else if (item === 'tabs' && j + 1 < path.length) {
req += ' > .panel > .tabs-wrapper'
} else if (!isNaN(item)) {
req += ' > .widget:nth-child(' + (item + 1) + ')'
} else {
while(true) {
var keys = Object.keys(pointer),
potentialPointers = keys.filter(x => !isNaN(x) || x === 'tabs' || x === 'widgets')
if (
keys.filter(x => typeof x === 'string' && x.match(/_[0-9]+/)).length ||
!potentialPointers.length
) {
break
}
nextKey = potentialPointers[0]
if (!isNaN(nextKey)) widget = widget.children[parseInt(nextKey)]
pointer = pointer[nextKey]
}
var e = DOM.get(req),
w
if (e.length) {
w = widgetManager.getWidgetByElement(e[0])
updateWidget(w, indexes)
} else {
// in case the elements are in a hidden tab (detached dom)
w = widgetManager.getWidgetById('root')[0]
updateWidget(w, {reuseChildren: false})
}
updateWidget(widget, indexes)
}