From aaabdd7e31b2076d18e0ee5b7f3165c8fc2b9613 Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Wed, 17 Oct 2018 18:18:27 +0200 Subject: [PATCH] editor: change diffing algo for one that does unpatch well --- package.json | 2 +- src/browser/js/app/editor/index.js | 106 ++++++++--------------------- 2 files changed, 30 insertions(+), 78 deletions(-) diff --git a/package.json b/package.json index 61800e15..8445108c 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,6 @@ "babelify": "8.0.0", "browserify": "16.2.3", "cardinal-spline-js": "2.3.9", - "deep-diff": "^1.0.2", "deep-extend": "^0.6.0", "electron": "1.8.8", "electron-packager": "12.2.0", @@ -35,6 +34,7 @@ "gyronorm": "^2.0.6", "jquery": "3.3.1", "json5": "2.1.0", + "jsondiffpatch": "^0.3.11", "keyboardjs": "github:jean-emmanuel/KeyboardJS", "licensify": "3.1.3", "mathjs": "^5.2.0", diff --git a/src/browser/js/app/editor/index.js b/src/browser/js/app/editor/index.js index af542c0e..a781bee8 100644 --- a/src/browser/js/app/editor/index.js +++ b/src/browser/js/app/editor/index.js @@ -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) }