Skip to content

Commit 9f6ea35

Browse files
committedSep 19, 2023
fix: when inserting a new value by typing somewhere, it doesn't initially have the right color
1 parent d8f065a commit 9f6ea35

File tree

3 files changed

+36
-9
lines changed

3 files changed

+36
-9
lines changed
 

‎src/lib/components/controls/EditableDiv.svelte

+7-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
export let onValueClass: (value: string) => string = () => ''
2222
2323
let domValue: HTMLDivElement | undefined
24-
let valueClass = onValueClass(value)
24+
let valueClass: string
25+
$: valueClass = onValueClass(value)
2526
let closed = false
2627
2728
onMount(() => {
@@ -32,6 +33,11 @@
3233
setTimeout(() => {
3334
if (domValue) {
3435
setCursorToEnd(domValue)
36+
37+
// The refresh method can be used to update the classnames for example
38+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
39+
// @ts-ignore
40+
domValue.refresh = handleValueInput
3541
}
3642
})
3743
})

‎src/lib/logic/actions.ts

+23-7
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ import type {
2525
DocumentState,
2626
InsertType,
2727
JSONParser,
28+
JSONSelection,
2829
OnChange,
2930
OnChangeText,
30-
OnPatch,
3131
OnJSONSelect,
32-
JSONSelection
32+
OnPatch
3333
} from '$lib/types'
3434
import { createDebug } from '$lib/utils/debug.js'
3535
import {
@@ -562,8 +562,8 @@ export function onInsert({
562562

563563
if (operation) {
564564
if (newValue === '') {
565-
// open the newly inserted value in edit mode
566-
tick2(() => insertActiveElementContents(refJsonEditor, '', true))
565+
// open the newly inserted value in edit mode (can be cancelled via ESC this way)
566+
tick2(() => insertActiveElementContents(refJsonEditor, '', true, refreshEditableDiv))
567567
}
568568
}
569569
} else {
@@ -620,7 +620,10 @@ export async function onInsertCharacter({
620620
const replaceContents = !selection.edit
621621

622622
onSelect({ ...selection, edit: true })
623-
tick2(() => insertActiveElementContents(refJsonEditor, char, replaceContents))
623+
tick2(() =>
624+
// We use this way via insertActiveElementContents, so we can cancel via ESC
625+
insertActiveElementContents(refJsonEditor, char, replaceContents, refreshEditableDiv)
626+
)
624627
return
625628
}
626629

@@ -656,7 +659,10 @@ export async function onInsertCharacter({
656659
const replaceContents = !selection.edit
657660

658661
onSelect({ ...selection, edit: true })
659-
tick2(() => insertActiveElementContents(refJsonEditor, char, replaceContents))
662+
tick2(() =>
663+
// We use this way via insertActiveElementContents, so we can cancel via ESC
664+
insertActiveElementContents(refJsonEditor, char, replaceContents, refreshEditableDiv)
665+
)
660666
} else {
661667
// TODO: replace the object/array with editing a text in edit mode?
662668
// (Ideally this this should not create an entry in history though,
@@ -721,7 +727,7 @@ async function onInsertValueWithCharacter({
721727
// multiple characters very quickly after each other due to the async handling)
722728
const replaceContents = !isEditingSelection(selection)
723729

724-
tick2(() => insertActiveElementContents(refJsonEditor, char, replaceContents))
730+
tick2(() => insertActiveElementContents(refJsonEditor, char, replaceContents, refreshEditableDiv))
725731
}
726732

727733
/**
@@ -732,3 +738,13 @@ async function onInsertValueWithCharacter({
732738
function tick2(callback: () => void) {
733739
setTimeout(() => setTimeout(callback))
734740
}
741+
742+
function refreshEditableDiv(element: HTMLElement) {
743+
// We force a refresh because when changing the text of the editable div programmatically,
744+
// the DIV doesn't get a trigger to update it's class
745+
// TODO: come up with a better solution
746+
747+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
748+
// @ts-ignore
749+
element?.refresh()
750+
}

‎src/lib/utils/domUtils.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,8 @@ export function setCursorToEnd(element: HTMLElement) {
229229
export function insertActiveElementContents(
230230
container: HTMLElement,
231231
text: string,
232-
replaceContents: boolean
232+
replaceContents: boolean,
233+
onActiveElement?: (activeElement: HTMLElement) => void
233234
) {
234235
const window = getWindow(container)
235236
if (!window) {
@@ -242,7 +243,11 @@ export function insertActiveElementContents(
242243

243244
if (activeElement && activeElement.isContentEditable) {
244245
activeElement.textContent = replaceContents ? text : activeElement.textContent + text
246+
console.log('UPDATED TEXT', replaceContents ? text : activeElement.textContent + text)
245247
setCursorToEnd(activeElement)
248+
if (onActiveElement) {
249+
onActiveElement(activeElement)
250+
}
246251
}
247252
}
248253

0 commit comments

Comments
 (0)
Please sign in to comment.