Add ChangeTimer.prototype.fireImmediately

so Mathjax is saved when exiting editor prematurely
This commit is contained in:
Henrik Giesel 2021-09-16 14:47:05 +02:00
parent a1dde7c966
commit c30ba6a3f6
3 changed files with 24 additions and 5 deletions

View File

@ -16,6 +16,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
let codeMirror: CodeMirror.EditorFromTextArea; let codeMirror: CodeMirror.EditorFromTextArea;
const changeTimer = new ChangeTimer(); const changeTimer = new ChangeTimer();
const dispatch = createEventDispatcher();
function onInput() { function onInput() {
changeTimer.schedule( 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 { function openCodemirror(textarea: HTMLTextAreaElement): void {
codeMirror = CodeMirror.fromTextArea(textarea, codeMirrorOptions); codeMirror = CodeMirror.fromTextArea(textarea, codeMirrorOptions);
codeMirror.on("change", onInput); codeMirror.on("change", onInput);
codeMirror.on("blur", onBlur);
} }
const dispatch = createEventDispatcher();
let textarea: HTMLTextAreaElement; let textarea: HTMLTextAreaElement;
onMount(() => { onMount(() => {

View File

@ -3,10 +3,16 @@
export class ChangeTimer { export class ChangeTimer {
private value: number | null = null; private value: number | null = null;
private action: (() => void) | null = null;
constructor() {
this.fireImmediately = this.fireImmediately.bind(this);
}
schedule(action: () => void, delay: number): void { schedule(action: () => void, delay: number): void {
this.clear(); this.clear();
this.value = setTimeout(action, delay); this.action = action;
this.value = setTimeout(this.fireImmediately, delay);
} }
clear(): void { clear(): void {
@ -15,4 +21,13 @@ export class ChangeTimer {
this.value = null; this.value = null;
} }
} }
fireImmediately(): void {
if (this.action) {
this.action();
this.action = null;
}
this.clear();
}
} }

View File

@ -29,12 +29,11 @@ export function saveNow(keepFocus: boolean): void {
return; return;
} }
saveFieldTimer.clear();
if (keepFocus) { if (keepFocus) {
saveField(currentField, "key"); saveFieldTimer.fireImmediately();
} else { } else {
// triggers onBlur, which saves // triggers onBlur, which saves
saveFieldTimer.clear();
currentField.blur(); currentField.blur();
} }
} }