diff --git a/src/browser/js/app/editor/diff.js b/src/browser/js/app/editor/diff.js new file mode 100644 index 00000000..13160e27 --- /dev/null +++ b/src/browser/js/app/editor/diff.js @@ -0,0 +1,30 @@ +module.exports = { + + diff: require('jsondiffpatch'), + + diffToWidget: function(widget, diff) { + + var props = Object.keys(diff), + children = diff.tabs ||diff.widgets + + if (children) { + var childrenKeys = Object.keys(children), + deletedChildren = childrenKeys.filter(x => typeof x === 'string' && x.match(/_[0-9]+/)), + changedChildren = childrenKeys.filter(x => !isNaN(x)) + } + + if ( + !children || + Array.isArray(children) || + deletedChildren.length || + changedChildren.length !== 1 + + ) { + return [widget, props] + } + + return module.exports.diffToWidget(widget.children[changedChildren[0]], children[changedChildren[0]]) + + } + +} diff --git a/src/browser/js/app/editor/index.js b/src/browser/js/app/editor/index.js index 1e49fb62..a0e8a6bc 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('jsondiffpatch'), + {diff, diffToWidget} = require('./diff'), widgetManager = require('../managers/widgets'), {deepCopy} = require('../utils'), macOs = (navigator.platform || '').match('Mac'), @@ -812,41 +812,17 @@ var Editor = class Editor { updateWidgetFromPatch(patch, indexes) { - // get object path from patch - var pointer = patch[0], - path = [] - - 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 !== 1 - ) { - break - } - - path.push(potentialPointers[0]) - pointer = pointer[potentialPointers[0]] + var [widget, props] = diffToWidget(widgetManager.getWidgetById('root')[0], patch[0]), + options = {} + if (indexes) { + options = {...indexes} + } else { + options.changedProps = props + options.reuseChildren = false } - if (!isNaN(path[path.length - 1]) && !isNaN(path[path.length - 2])) { - path.pop() - path.pop() - } - - // get widget from path - var widget = widgetManager.getWidgetById('root')[0] - pointer = patch[0] - for (var i in path) { - if (!isNaN(path[i])) widget = widget.children[parseInt(path[i])] - pointer = pointer[path[i]] - } - - updateWidget(widget, indexes || {reuseChildren: false}) + updateWidget(widget, options) }