From 2f393e594895696398d5eeb1ee6421dac6810b3b Mon Sep 17 00:00:00 2001 From: Jos de Jong Date: Wed, 8 Jul 2020 12:49:12 +0200 Subject: [PATCH] Fix pasting properties inline in an object instead of at the bottom --- src/JSONEditor.svelte | 28 +++------ src/JSONNode.svelte | 23 +++++--- src/actions.js | 32 +++++++++++ src/utils/updateProps.js | 104 +++++++++++++++++++++++++++------- src/utils/updateProps.test.js | 1 + 5 files changed, 140 insertions(+), 48 deletions(-) diff --git a/src/JSONEditor.svelte b/src/JSONEditor.svelte index f6f768d..430b1f4 100644 --- a/src/JSONEditor.svelte +++ b/src/JSONEditor.svelte @@ -19,9 +19,7 @@ import { createHistory } from './history.js' import Node from './JSONNode.svelte' import { expandSelection } from './selection.js' - import { singleton } from './singleton.js' import { - deleteIn, existsIn, getIn, setIn, @@ -35,6 +33,7 @@ import jump from './assets/jump.js/src/jump.js' import { syncState } from './utils/syncState.js' import { isObject } from './utils/typeUtils.js' + import { patchProps } from './utils/updateProps.js' let divContents @@ -99,25 +98,7 @@ // if a property is renamed (move operation), rename it in the object's props // so it maintains its identity and hence its index - operations - .filter(operation => { - return operation.op === 'move' && isEqual( - initial(parseJSONPointer(operation.from)), - initial(parseJSONPointer(operation.path)) - ) - }) - .forEach(operation => { - const pathFrom = parseJSONPointer(operation.from) - const to = parseJSONPointer(operation.path) - const parentPath = initial(pathFrom) - const oldKey = last(pathFrom) - const newKey = last(to) - const props = getIn(state, parentPath.concat(STATE_PROPS)) - const index = props.findIndex(item => item.key === oldKey) - if (index !== -1) { - state = setIn(state, parentPath.concat([STATE_PROPS, index, 'key']), newKey) - } - }) + state = patchProps(state, operations) history.add({ undo: documentPatchResult.revert, @@ -303,6 +284,10 @@ emitOnChange() } + function handleUpdateKey (oldKey, newKey) { + // should never be called on the root + } + function handleToggleSearch() { showSearch = !showSearch } @@ -526,6 +511,7 @@ state={state} searchResult={searchResultWithActive} onPatch={handlePatch} + onUpdateKey={handleUpdateKey} onExpand={handleExpand} onLimit={handleLimit} onSelect={handleSelect} diff --git a/src/JSONNode.svelte b/src/JSONNode.svelte index d5de3a1..cb901fe 100644 --- a/src/JSONNode.svelte +++ b/src/JSONNode.svelte @@ -1,5 +1,6 @@