Skip to content

Commit

Permalink
fix: partial fix for #40, clicking outside the editor should stop edi…
Browse files Browse the repository at this point in the history
…ting a key/value
  • Loading branch information
josdejong committed Dec 15, 2021
1 parent 880795f commit 70eab0c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
25 changes: 23 additions & 2 deletions src/lib/components/controls/EditableDiv.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<script>
import { onDestroy, onMount } from 'svelte'
import { getPlainText, setCursorToEnd, setPlainText } from '$lib/utils/domUtils'
import { getPlainText, isChildOf, setCursorToEnd, setPlainText } from '$lib/utils/domUtils'
import { keyComboFromEvent } from '$lib/utils/keyBindings'
import { createDebug } from '$lib/utils/debug'
import { noop } from 'lodash-es'
Expand Down Expand Up @@ -33,7 +33,8 @@
debug('onDestroy', { closed, value, newValue })
if (!closed && newValue !== value) {
onChange(newValue)
const passiveExit = true
onChange(newValue, passiveExit)
}
})
Expand Down Expand Up @@ -86,8 +87,28 @@
const clipboardText = event.clipboardData.getData('text/plain')
onPaste(clipboardText)
}
function handleWindowMouseDown(event) {
const outsideEditableDiv = !isChildOf(event.target, (element) => element === domValue)
if (outsideEditableDiv) {
// apply changes
closed = true
const newValue = getDomValue()
if (newValue !== value) {
const passiveExit = true
onChange(newValue, passiveExit)
}
// FIXME: after applying the change, when the mouse click was outside of
// the editor, it should loose focus directly (right now the hidden input
// always gets focus after any selection change)
}
}
</script>

<svelte:window on:mousedown|capture={handleWindowMouseDown} />

<div
class={'jse-editable-div ' + valueClass}
contenteditable="true"
Expand Down
8 changes: 6 additions & 2 deletions src/lib/components/modes/treemode/JSONKey.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,15 @@
})
}
function handleChangeValue(newKey) {
function handleChangeValue(newKey, passiveExit = false) {
const updatedKey = onUpdateKey(key, newKey)
const updatedPath = initial(path).concat(updatedKey)
onSelect({ type: SELECTION_TYPE.KEY, path: updatedPath, next: true })
onSelect({
type: SELECTION_TYPE.KEY,
path: updatedPath,
next: !passiveExit
})
}
function handleCancelChange() {
Expand Down
8 changes: 6 additions & 2 deletions src/lib/plugins/value/components/EditableValue.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
export let onPasteJson
export let onSelect
function handleChangeValue(newValue) {
function handleChangeValue(newValue, passiveExit = false) {
onPatch([
{
op: 'replace',
Expand All @@ -22,7 +22,11 @@
}
])
onSelect({ type: SELECTION_TYPE.VALUE, path, nextInside: true })
onSelect({
type: SELECTION_TYPE.VALUE,
path,
nextInside: !passiveExit
})
}
function handleCancelChange() {
Expand Down

0 comments on commit 70eab0c

Please sign in to comment.