Skip to content

Commit

Permalink
fix: only expand JSON after sort, transform and expand when isn't exp…
Browse files Browse the repository at this point in the history
…anded already
  • Loading branch information
josdejong committed Nov 5, 2024
1 parent 3708998 commit 5d84af0
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 12 deletions.
11 changes: 6 additions & 5 deletions src/lib/components/modes/treemode/TreeMode.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
expandSection,
expandSelf,
expandSmart,
expandSmartIfCollapsed,
getEnforceString,
setInDocumentState,
syncDocumentState
Expand Down Expand Up @@ -813,7 +814,7 @@
// expand extracted object/array
const path: JSONPath = []
return {
state: expandSmart(patchedJson, patchedState, path)
state: expandSmartIfCollapsed(patchedJson, patchedState, path)
}
}
Expand Down Expand Up @@ -1042,8 +1043,8 @@
debug('onSort', rootPath, operations)
handlePatch(operations, (patchedJson, patchedState) => ({
// expand the newly replaced array and select it
state: expandSmart(patchedJson, patchedState, rootPath),
// expand the newly replaced array if needed, and select it
state: expandSmartIfCollapsed(patchedJson, patchedState, rootPath),
selection: createValueSelection(rootPath)
}))
},
Expand Down Expand Up @@ -1096,8 +1097,8 @@
debug('onTransform', rootPath, operations)
handlePatch(operations, (patchedJson, patchedState) => ({
// expand the newly replaced array and select it
state: expandSmart(patchedJson, patchedState, rootPath),
// expand the newly replaced array if needed and select it
state: expandSmartIfCollapsed(patchedJson, patchedState, rootPath),
selection: createValueSelection(rootPath)
}))
}
Expand Down
39 changes: 38 additions & 1 deletion src/lib/logic/documentState.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
expandSection,
expandSelf,
expandSmart,
expandSmartIfCollapsed,
forEachVisibleIndex,
getEnforceString,
getInRecursiveState,
Expand All @@ -38,7 +39,14 @@ import {
type ValueDocumentState,
type VisibleSection
} from '$lib/types.js'
import { deleteIn, getIn, type JSONPatchDocument, setIn, updateIn } from 'immutable-json-patch'
import {
deleteIn,
getIn,
type JSONPatchDocument,
type JSONPath,
setIn,
updateIn
} from 'immutable-json-patch'
import { isArrayRecursiveState } from 'svelte-jsoneditor'

const json3 = [{ id: 0 }, { id: 1 }, { id: 2 }]
Expand Down Expand Up @@ -1939,6 +1947,35 @@ describe('documentState', () => {
expect(deleteInDocumentState(json, documentState, ['foo'])).toEqual(documentState)
})
})

describe('expandSmartIfCollapsed', () => {
test('should only trigger expandSmart when the state is collapsed', () => {
const json = { nested: {} }
const stateCollapsed = createDocumentState({ json, expand: () => false })
const stateExpanded = createDocumentState({ json, expand: (path) => path.length === 0 })
const path: JSONPath = []

// will trigger smart expand, expanding all
expect(expandSmartIfCollapsed(json, stateCollapsed, path)).toEqual({
expanded: true,
properties: {
nested: {
expanded: true,
properties: {},
type: 'object'
}
},
type: 'object'
})

// will not trigger smart expand, leaving the nested object collapsed as it was
expect(expandSmartIfCollapsed(json, stateExpanded, path)).toEqual({
expanded: true,
properties: {},
type: 'object'
})
})
})
})

/**
Expand Down
19 changes: 13 additions & 6 deletions src/lib/logic/documentState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,12 @@ export function createDocumentState({
json,
expand
}: CreateDocumentStateProps): DocumentState | undefined {
let documentState: DocumentState | undefined = createRecursiveState({
const documentState: DocumentState | undefined = createRecursiveState({
json,
factory: documentStateFactory
}) as DocumentState

if (expand && documentState) {
documentState = expandPath(json, documentState, [], expand)
}

return documentState
return expand && documentState ? expandPath(json, documentState, [], expand) : documentState
}

export function createArrayDocumentState({ expanded } = { expanded: false }): ArrayDocumentState {
Expand Down Expand Up @@ -879,6 +875,17 @@ export function expandSmart(
return expandPath(json, documentState, path, callback)
}

export function expandSmartIfCollapsed(
json: unknown | undefined,
documentState: DocumentState | undefined,
path: JSONPath
) {
const nestedState = getInRecursiveState(json, documentState, path)
const isExpanded = isExpandableState(nestedState) ? nestedState.expanded : false

return isExpanded ? documentState : expandSmart(json, documentState, path)
}

/**
* Expand the root array or object, and in case of an array, expand the first array item
*/
Expand Down

0 comments on commit 5d84af0

Please sign in to comment.