From 23067b4638dbce189bfa354f5d89216de7b5a26a Mon Sep 17 00:00:00 2001 From: Jos de Jong Date: Sun, 26 Jul 2020 10:56:04 +0200 Subject: [PATCH] Fix expanding nested search results --- src/JSONEditor.svelte | 5 +++-- src/JSONNode.svelte | 4 ++-- src/selection.js | 2 +- src/utils/search.js | 6 +++--- src/utils/stateUtils.js | 6 +++--- src/utils/updateProps.js | 8 ++++---- 6 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/JSONEditor.svelte b/src/JSONEditor.svelte index 8d0b239..62334bc 100644 --- a/src/JSONEditor.svelte +++ b/src/JSONEditor.svelte @@ -253,6 +253,7 @@ function changeSearchText (text) { searchText = text searchResult = search(doc, searchText, searchResult) + focusActiveSearchResult(searchResult && searchResult.activeItem) } function nextSearchResult () { @@ -331,7 +332,7 @@ return stateUtils(getIn(doc, path), childState, [], () => expanded, true) }) } else { - state = setIn(state, path.concat(STATE_EXPANDED), expanded) + state = setIn(state, path.concat(STATE_EXPANDED), expanded, true) } } @@ -341,7 +342,7 @@ * @param {boolean} limit */ function handleLimit (path, limit) { - state = setIn(state, path.concat(STATE_LIMIT), limit) + state = setIn(state, path.concat(STATE_LIMIT), limit, true) } /** diff --git a/src/JSONNode.svelte b/src/JSONNode.svelte index 45d4f8c..804b427 100644 --- a/src/JSONNode.svelte +++ b/src/JSONNode.svelte @@ -442,7 +442,7 @@ {:else} { - } + } {/if} {#if expanded} @@ -472,7 +472,7 @@ {/if} {:else} diff --git a/src/selection.js b/src/selection.js index d940d4a..4c7b212 100644 --- a/src/selection.js +++ b/src/selection.js @@ -1,4 +1,4 @@ -import { first, initial, isEmpty, isEqual, last } from 'lodash-es' +import { isEqual } from 'lodash-es' import { STATE_PROPS } from './constants.js' import { getIn } from './utils/immutabilityHelpers.js' import { compileJSONPointer, parseJSONPointer } from './utils/jsonPointer.js' diff --git a/src/utils/search.js b/src/utils/search.js index b988d49..14372cb 100644 --- a/src/utils/search.js +++ b/src/utils/search.js @@ -41,7 +41,7 @@ export function search (doc, searchText, previousResult) { const activeIndex = flatItems.findIndex(item => isEqual(item, activeItem)) const itemsWithActive = (items && activeItem) - ? setIn(items, activeItem.path.concat(activeItem.what), 'search active') + ? setIn(items, activeItem.path.concat(activeItem.what), 'search active', true) : items return { @@ -66,7 +66,7 @@ export function searchNext (searchResult) { const nextActiveItem = searchResult.flatItems[nextActiveIndex] const itemsWithActive = nextActiveItem - ? setIn(searchResult.items, nextActiveItem.path.concat(nextActiveItem.what), 'search active') + ? setIn(searchResult.items, nextActiveItem.path.concat(nextActiveItem.what), 'search active', true) : searchResult.items return { @@ -89,7 +89,7 @@ export function searchPrevious (searchResult) { const previousActiveItem = searchResult.flatItems[previousActiveIndex] const itemsWithActive = previousActiveItem - ? setIn(searchResult.items, previousActiveItem.path.concat(previousActiveItem.what), 'search active') + ? setIn(searchResult.items, previousActiveItem.path.concat(previousActiveItem.what), 'search active', true) : searchResult.items return { diff --git a/src/utils/stateUtils.js b/src/utils/stateUtils.js index a45f5a7..69dc035 100644 --- a/src/utils/stateUtils.js +++ b/src/utils/stateUtils.js @@ -5,7 +5,7 @@ import { STATE_LIMIT, STATE_PROPS } from '../constants.js' -import { setIn } from './immutabilityHelpers.js' +import { getIn, setIn } from './immutabilityHelpers.js' import { isObject, isObjectOrArray } from './typeUtils.js' import { updateProps } from './updateProps.js' @@ -87,7 +87,7 @@ export function expandPath (state, path) { for (let i = 1; i < path.length; i++) { const partialPath = path.slice(0, i) // FIXME: setIn has to create object first - updatedState = setIn(updatedState, partialPath.concat(STATE_EXPANDED), true) + updatedState = setIn(updatedState, partialPath.concat(STATE_EXPANDED), true, true) // if needed, enlarge the limit such that the search result becomes visible const key = path[i] @@ -95,7 +95,7 @@ export function expandPath (state, path) { const limit = getIn(updatedState, partialPath.concat(STATE_LIMIT)) || DEFAULT_LIMIT if (key > limit) { const newLimit = Math.ceil(key / DEFAULT_LIMIT) * DEFAULT_LIMIT - updatedState = setIn(updatedState, partialPath.concat(STATE_LIMIT), newLimit) + updatedState = setIn(updatedState, partialPath.concat(STATE_LIMIT), newLimit, true) } } } diff --git a/src/utils/updateProps.js b/src/utils/updateProps.js index 2cf8bd4..b3a2a41 100644 --- a/src/utils/updateProps.js +++ b/src/utils/updateProps.js @@ -58,18 +58,18 @@ export function patchProps (state, operations) { const newIndex = props.findIndex(item => item.key === newKey) if (newIndex !== -1) { const updatedProps = deleteIn(props, [newIndex]) - updatedState = setIn(updatedState, parentPath.concat([STATE_PROPS]), updatedProps) + updatedState = setIn(updatedState, parentPath.concat([STATE_PROPS]), updatedProps, true) } // Rename the key in the object's props so it maintains its identity and hence its index - updatedState = setIn(updatedState, parentPath.concat([STATE_PROPS, oldIndex, 'key']), newKey) + updatedState = setIn(updatedState, parentPath.concat([STATE_PROPS, oldIndex, 'key']), newKey, true) } else { // operation.from and operation.path are the same: // property is moved but stays the same -> move it to the end of the props const oldProp = props[oldIndex] const updatedProps = insertAt(deleteIn(props, [oldIndex]), [props.length - 1], oldProp) - updatedState = setIn(updatedState, parentPath.concat([STATE_PROPS]), updatedProps) + updatedState = setIn(updatedState, parentPath.concat([STATE_PROPS]), updatedProps, true) } } } @@ -90,7 +90,7 @@ export function patchProps (state, operations) { } const updatedProps = insertAt(props, [props.length], newProp) - updatedState = setIn(updatedState, parentPath.concat([STATE_PROPS]), updatedProps) + updatedState = setIn(updatedState, parentPath.concat([STATE_PROPS]), updatedProps, true) } } }