From c30ba6a3f650b1041b80cec68e2e6c394b7593b8 Mon Sep 17 00:00:00 2001 From: Henrik Giesel Date: Thu, 16 Sep 2021 14:47:05 +0200 Subject: [PATCH] Add ChangeTimer.prototype.fireImmediately so Mathjax is saved when exiting editor prematurely --- ts/editor/MathjaxHandleEditor.svelte | 7 ++++++- ts/editor/change-timer.ts | 17 ++++++++++++++++- ts/editor/saving.ts | 5 ++--- 3 files changed, 24 insertions(+), 5 deletions(-) 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(); } }