diff --git a/ts/editor/MathjaxHandleEditor.svelte b/ts/editor/MathjaxHandleEditor.svelte index 6dccbfe71..35568b8f8 100644 --- a/ts/editor/MathjaxHandleEditor.svelte +++ b/ts/editor/MathjaxHandleEditor.svelte @@ -16,6 +16,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html let codeMirror: CodeMirror.EditorFromTextArea; const changeTimer = new ChangeTimer(); + const dispatch = createEventDispatcher(); function onInput() { changeTimer.schedule( @@ -24,12 +25,16 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html ); } + function onBlur() { + changeTimer.fireImmediately(); + } + function openCodemirror(textarea: HTMLTextAreaElement): void { codeMirror = CodeMirror.fromTextArea(textarea, codeMirrorOptions); codeMirror.on("change", onInput); + codeMirror.on("blur", onBlur); } - const dispatch = createEventDispatcher(); let textarea: HTMLTextAreaElement; onMount(() => { diff --git a/ts/editor/change-timer.ts b/ts/editor/change-timer.ts index 1849c33f0..9c48ee718 100644 --- a/ts/editor/change-timer.ts +++ b/ts/editor/change-timer.ts @@ -3,10 +3,16 @@ export class ChangeTimer { private value: number | null = null; + private action: (() => void) | null = null; + + constructor() { + this.fireImmediately = this.fireImmediately.bind(this); + } schedule(action: () => void, delay: number): void { this.clear(); - this.value = setTimeout(action, delay); + this.action = action; + this.value = setTimeout(this.fireImmediately, delay); } clear(): void { @@ -15,4 +21,13 @@ export class ChangeTimer { this.value = null; } } + + fireImmediately(): void { + if (this.action) { + this.action(); + this.action = null; + } + + this.clear(); + } } diff --git a/ts/editor/saving.ts b/ts/editor/saving.ts index ebb70e7bd..e00c6e839 100644 --- a/ts/editor/saving.ts +++ b/ts/editor/saving.ts @@ -29,12 +29,11 @@ export function saveNow(keepFocus: boolean): void { return; } - saveFieldTimer.clear(); - if (keepFocus) { - saveField(currentField, "key"); + saveFieldTimer.fireImmediately(); } else { // triggers onBlur, which saves + saveFieldTimer.clear(); currentField.blur(); } }