Move expandPath to stateUtils.js

This commit is contained in:
Jos de Jong 2020-07-22 17:28:33 +02:00
parent da2f912d6d
commit 2961e0d910
2 changed files with 36 additions and 27 deletions

View File

@ -1,13 +1,13 @@
<script> <script>
import { tick } from 'svelte' import { tick } from 'svelte'
import { import {
append, duplicate, append,
duplicate,
insertBefore, insertBefore,
removeAll, removeAll,
replace replace
} from './operations.js' } from './operations.js'
import { import {
DEFAULT_LIMIT,
STATE_EXPANDED, STATE_EXPANDED,
STATE_LIMIT, STATE_LIMIT,
SCROLL_DURATION, SCROLL_DURATION,
@ -25,7 +25,6 @@
} from './selection.js' } from './selection.js'
import { isContentEditableDiv } from './utils/domUtils.js' import { isContentEditableDiv } from './utils/domUtils.js'
import { import {
existsIn,
getIn, getIn,
setIn, setIn,
updateIn updateIn
@ -34,9 +33,9 @@
import { keyComboFromEvent } from './utils/keyBindings.js' import { keyComboFromEvent } from './utils/keyBindings.js'
import { search, searchNext, searchPrevious } from './utils/search.js' import { search, searchNext, searchPrevious } from './utils/search.js'
import { immutableJSONPatch } from './utils/immutableJSONPatch' import { immutableJSONPatch } from './utils/immutableJSONPatch'
import { isNumber, initial, last, cloneDeep } from 'lodash-es' import { initial, last, cloneDeep } from 'lodash-es'
import jump from './assets/jump.js/src/jump.js' import jump from './assets/jump.js/src/jump.js'
import { stateUtils } from './utils/stateUtils.js' import { expandPath, stateUtils } from './utils/stateUtils.js'
import { getNextKeys, patchProps } from './utils/updateProps.js' import { getNextKeys, patchProps } from './utils/updateProps.js'
let divContents let divContents
@ -268,7 +267,7 @@
async function focusActiveSearchResult (activeItem) { async function focusActiveSearchResult (activeItem) {
if (activeItem) { if (activeItem) {
expandPath(activeItem.path) state = expandPath(state, activeItem.path)
await tick() await tick()
@ -374,27 +373,6 @@
} }
} }
/**
* Expand all nodes on given path
* @param {Path} path
*/
function expandPath (path) {
for (let i = 1; i < path.length; i++) {
const partialPath = path.slice(0, i)
state = setIn(state, partialPath.concat(STATE_EXPANDED), true)
// if needed, enlarge the limit such that the search result becomes visible
const key = path[i]
if (isNumber(key)) {
const limit = getIn(state, partialPath.concat(STATE_LIMIT)) || DEFAULT_LIMIT
if (key > limit) {
const newLimit = Math.ceil(key / DEFAULT_LIMIT) * DEFAULT_LIMIT
state = setIn(state, partialPath.concat(STATE_LIMIT), newLimit)
}
}
}
}
function handleKeyDown (event) { function handleKeyDown (event) {
const combo = keyComboFromEvent(event) const combo = keyComboFromEvent(event)

View File

@ -1,9 +1,11 @@
import { isNumber } from 'lodash-es'
import { import {
DEFAULT_LIMIT, DEFAULT_LIMIT,
STATE_EXPANDED, STATE_EXPANDED,
STATE_LIMIT, STATE_LIMIT,
STATE_PROPS STATE_PROPS
} from '../constants.js' } from '../constants.js'
import { setIn } from './immutabilityHelpers.js'
import { isObject, isObjectOrArray } from './typeUtils.js' import { isObject, isObjectOrArray } from './typeUtils.js'
import { updateProps } from './updateProps.js' import { updateProps } from './updateProps.js'
@ -71,3 +73,32 @@ export function stateUtils (doc, state = undefined, path, expand, forceRefresh =
// primitive values have no state // primitive values have no state
return undefined return undefined
} }
/**
* Expand all nodes on given path
* @param {JSON} state
* @param {Path} path
* @return {JSON} returns the updated state
*/
// TODO: write unit tests for expandPath
export function expandPath (state, path) {
let updatedState = state
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)
// if needed, enlarge the limit such that the search result becomes visible
const key = path[i]
if (isNumber(key)) {
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)
}
}
}
return updatedState
}