From 9a23799d5f14c1c9992fbb1272521929349e47db Mon Sep 17 00:00:00 2001 From: josdejong Date: Wed, 24 Jun 2020 14:20:25 +0200 Subject: [PATCH] Implement selecting one or multiple nodes by dragging --- src/JSONEditor.scss | 5 +- src/JSONEditor.svelte | 25 ++++++++- src/JSONNode.scss | 19 ++++--- src/JSONNode.svelte | 101 +++++++++++++++++++++++++++++++----- src/constants.js | 2 + src/selection.js | 83 +++++++++++++++++++++++++++++ src/selection.test.js | 68 ++++++++++++++++++++++++ src/singleton.js | 5 ++ src/styles.scss | 4 +- src/utils/syncState.test.js | 12 ++--- 10 files changed, 294 insertions(+), 30 deletions(-) create mode 100644 src/selection.js create mode 100644 src/selection.test.js create mode 100644 src/singleton.js diff --git a/src/JSONEditor.scss b/src/JSONEditor.scss index 7248daf..491494e 100644 --- a/src/JSONEditor.scss +++ b/src/JSONEditor.scss @@ -64,8 +64,7 @@ flex: 1; overflow: auto; - .bottom { - height: $input-padding; - } + //display: flex; + //flex-direction: column; } } \ No newline at end of file diff --git a/src/JSONEditor.svelte b/src/JSONEditor.svelte index 0ad937a..a6ce91f 100644 --- a/src/JSONEditor.svelte +++ b/src/JSONEditor.svelte @@ -11,6 +11,7 @@ import { faSearch, faUndo, faRedo } from '@fortawesome/free-solid-svg-icons' import { createHistory } from './history.js' import Node from './JSONNode.svelte' + import { expandSelection } from './selection.js' import { existsIn, getIn, @@ -36,6 +37,19 @@ export let doc = {} let state = undefined + let selection = null + let selectionMap = {} + + $: { + selectionMap = {} + if (selection != null) { + selection.forEach(path => { + selectionMap[compileJSONPointer(path)] = true + }) + } + } + + $: console.log('selectionMap', selectionMap) export let onChangeJson = () => {} @@ -257,6 +271,14 @@ state = setIn(state, path.concat(STATE_LIMIT), limit) } + function handleSelect (anchorPath, focusPath) { + if (anchorPath && focusPath) { + selection = expandSelection(doc, state, anchorPath, focusPath) + } else { + selection = null + } + } + /** * Expand all nodes on given path * @param {Path} path @@ -373,8 +395,9 @@ onPatch={handlePatch} onExpand={handleExpand} onLimit={handleLimit} + onSelect={handleSelect} + selectionMap={selectionMap} /> -
diff --git a/src/JSONNode.scss b/src/JSONNode.scss index c2dd531..835cdd7 100644 --- a/src/JSONNode.scss +++ b/src/JSONNode.scss @@ -5,6 +5,18 @@ font-size: $font-size; color: $black; + &.root { + min-height: 100%; + padding-bottom: $input-padding; + box-sizing: border-box; + } + + &.selected .header, + &.selected .contents, + &.selected .footer { + background-color: $selection-background; + } + .header { position: relative; } @@ -27,6 +39,7 @@ } .footer { + display: inline-block; padding-left: $line-height + $input-padding; // must be the same as the width of the expand button } } @@ -86,10 +99,6 @@ } } -.items, -.props { - padding-left: $indentation-width; -} .value { &.string { @@ -125,8 +134,6 @@ } div.limit { - margin-left: $indentation-width; - button { background: none; border: none; diff --git a/src/JSONNode.svelte b/src/JSONNode.svelte index 6ddab14..ef4293c 100644 --- a/src/JSONNode.svelte +++ b/src/JSONNode.svelte @@ -1,18 +1,21 @@ -
+
{#if type === 'array'} -
+
)
{/if}
-