Be able to select a single node by clicking

This commit is contained in:
Jos de Jong 2020-07-12 14:49:12 +02:00
parent 7ad262a7b1
commit c773e5bfa9
3 changed files with 40 additions and 11 deletions

View File

@ -19,6 +19,7 @@
import { createHistory } from './history.js' import { createHistory } from './history.js'
import Node from './JSONNode.svelte' import Node from './JSONNode.svelte'
import { expandSelection } from './selection.js' import { expandSelection } from './selection.js'
import { isContentEditableDiv } from './utils/domUtils.js'
import { import {
existsIn, existsIn,
getIn, getIn,
@ -410,10 +411,7 @@
function handleKeyDown (event) { function handleKeyDown (event) {
const combo = keyComboFromEvent(event) const combo = keyComboFromEvent(event)
const targetIsContentEditableDiv = ( if (!isContentEditableDiv(event.target)) {
event.target.nodeName === 'DIV' && event.target.contentEditable === 'true')
if (!targetIsContentEditableDiv) {
if (combo === 'Ctrl+X' || combo === 'Command+X') { if (combo === 'Ctrl+X' || combo === 'Command+X') {
event.preventDefault() event.preventDefault()
handleCut() handleCut()

View File

@ -12,7 +12,11 @@
INDENTATION_WIDTH INDENTATION_WIDTH
} from './constants.js' } from './constants.js'
import { singleton } from './singleton.js' import { singleton } from './singleton.js'
import { getPlainText, setPlainText } from './utils/domUtils.js' import {
getPlainText,
isChildOfButton, isContentEditableDiv,
setPlainText
} from './utils/domUtils.js'
import Icon from 'svelte-awesome' import Icon from 'svelte-awesome'
import { faCaretDown, faCaretRight } from '@fortawesome/free-solid-svg-icons' import { faCaretDown, faCaretRight } from '@fortawesome/free-solid-svg-icons'
import classnames from 'classnames' import classnames from 'classnames'
@ -96,10 +100,18 @@
} }
function toggleExpand (event) { function toggleExpand (event) {
event.stopPropagation()
const recursive = event.ctrlKey const recursive = event.ctrlKey
onExpand(path, !expanded, recursive) onExpand(path, !expanded, recursive)
} }
function handleExpand (event) {
event.stopPropagation()
onExpand(path, true)
}
function updateKey () { function updateKey () {
const newKey = getPlainText(domKey) const newKey = getPlainText(domKey)
@ -205,14 +217,17 @@
onSelect(null) onSelect(null)
} }
// check if the mouse down is not happening in the key or value input fields // check if the mouse down is not happening in the key or value input fields or on a button
if (event.target.contentEditable !== 'true') { if (!isContentEditableDiv(event.target) && !isChildOfButton(event.target)) {
// initialize dragging a selection // initialize dragging a selection
singleton.mousedown = true singleton.mousedown = true
singleton.selectionAnchor = path singleton.selectionAnchor = path
singleton.selectionFocus = null singleton.selectionFocus = path
// TODO: select the clicked node directly onSelect({
anchorPath: singleton.selectionAnchor,
focusPath: singleton.selectionFocus
})
event.stopPropagation() event.stopPropagation()
} }
@ -335,7 +350,7 @@
<div class="delimiter">[</div> <div class="delimiter">[</div>
{:else} {:else}
<div class="delimiter">[</div> <div class="delimiter">[</div>
<button class="tag" on:click={() => onExpand(path, true)}>{value.length} items</button> <button class="tag" on:click={handleExpand}>{value.length} items</button>
<div class="delimiter">]</div> <div class="delimiter">]</div>
{/if} {/if}
</div> </div>
@ -403,7 +418,7 @@
<span class="delimiter">&#123;</span> <span class="delimiter">&#123;</span>
{:else} {:else}
<span class="delimiter"> &#123;</span> <span class="delimiter"> &#123;</span>
<button class="tag" on:click={() => onExpand(path, true)}>{Object.keys(value).length} props</button> <button class="tag" on:click={handleExpand}>{Object.keys(value).length} props</button>
<span class="delimiter">}</span> <span class="delimiter">}</span>
{/if} {/if}
</div> </div>

View File

@ -178,3 +178,19 @@ export function traverseInnerText (element, buffer) {
// br or unknown // br or unknown
return '' return ''
} }
// test whether a DOM element is a child of a button
export function isChildOfButton (element) {
let e = element
while (e && e.nodeName !== 'BUTTON') {
e = e.parentNode
}
return e && e.nodeName === 'BUTTON'
}
// test whether a DOM element is a content editable div
export function isContentEditableDiv (element) {
return (element.nodeName === 'DIV' && element.contentEditable === 'true')
}