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;
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(() => {

View File

@ -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();
}
}

View File

@ -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();
}
}