From 61d91c2f9ce2dd641199eb47143ef76ff8fad894 Mon Sep 17 00:00:00 2001 From: josdejong Date: Tue, 2 Jun 2020 21:41:59 +0200 Subject: [PATCH] Implement methods `expandAll`, `collapseAll`, and Ctrl+Click on expand button --- src/JSONEditor.svelte | 27 +++++++++++++++++++++++---- src/JSONNode.svelte | 9 +++++---- src/utils/syncState.js | 14 +++++++------- 3 files changed, 35 insertions(+), 15 deletions(-) diff --git a/src/JSONEditor.svelte b/src/JSONEditor.svelte index 319fd5e..97e3af5 100644 --- a/src/JSONEditor.svelte +++ b/src/JSONEditor.svelte @@ -11,7 +11,12 @@ import { faSearch, faUndo, faRedo } from '@fortawesome/free-solid-svg-icons' import { createHistory } from './history.js' import Node from './JSONNode.svelte' - import { existsIn, getIn, setIn } from './utils/immutabilityHelpers.js' + import { + existsIn, + getIn, + setIn, + updateIn + } from './utils/immutabilityHelpers.js' import { compileJSONPointer, parseJSONPointer } from './utils/jsonPointer.js' import { keyComboFromEvent } from './utils/keyBindings.js' import { flattenSearch, search } from './utils/search.js' @@ -54,6 +59,14 @@ }) let historyState = history.getState() + export function expandAll () { + state = syncState(doc, state, [], () => true, true) + } + + export function collapseAll () { + state = syncState(doc, state, [], () => false, true) + } + export function get() { return doc } @@ -227,10 +240,16 @@ * Toggle expanded state of a node * @param {Path} path * @param {boolean} expanded + * @param {boolean} [recursive=false] */ - function handleExpand (path, expanded) { - console.log('handleExpand', path, expanded) - state = setIn(state, path.concat(STATE_EXPANDED), expanded) + function handleExpand (path, expanded, recursive = false) { + if (recursive) { + state = updateIn(state, path, (childState) => { + return syncState(getIn(doc, path), childState, [], () => expanded, true) + }) + } else { + state = setIn(state, path.concat(STATE_EXPANDED), expanded) + } } /** diff --git a/src/JSONNode.svelte b/src/JSONNode.svelte index a32f09a..4b64946 100644 --- a/src/JSONNode.svelte +++ b/src/JSONNode.svelte @@ -82,8 +82,9 @@ }) } - function toggle () { - onExpand(path, !expanded) + function toggleExpand (event) { + const recursive = event.ctrlKey + onExpand(path, !expanded, recursive) } function updateKey () { @@ -182,7 +183,7 @@
{#if type === 'array'}
-