Run scheduled save before add

This commit is contained in:
Abdo 2025-10-18 03:20:47 +03:00
parent 7dc2d29136
commit 71dfb6350d
2 changed files with 15 additions and 14 deletions

View file

@ -9,7 +9,7 @@ export class ChangeTimer {
this.fireImmediately = this.fireImmediately.bind(this); this.fireImmediately = this.fireImmediately.bind(this);
} }
schedule(action: () => void, delay: number): void { schedule(action: () => Promise<void>, delay: number): void {
this.clear(); this.clear();
this.action = action; this.action = action;
this.value = setTimeout(this.fireImmediately, delay) as any; this.value = setTimeout(this.fireImmediately, delay) as any;
@ -22,9 +22,9 @@ export class ChangeTimer {
} }
} }
fireImmediately(): void { async fireImmediately(): Promise<void> {
if (this.action) { if (this.action) {
this.action(); await this.action();
this.action = null; this.action = null;
} }

View file

@ -433,15 +433,15 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
}, 600); }, 600);
} }
function saveFieldNow(): void { async function saveFieldNow() {
/* this will always be a key save */ /* this will always be a key save */
fieldSave.fireImmediately(); await fieldSave.fireImmediately();
} }
function saveNow(): void { async function saveNow() {
closeMathjaxEditor?.(); closeMathjaxEditor?.();
$commitTagEdits(); $commitTagEdits();
saveFieldNow(); await saveFieldNow();
} }
// Used for detecting changed sticky fields on close // Used for detecting changed sticky fields on close
@ -467,12 +467,12 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
} }
async function closeAddCards() { async function closeAddCards() {
saveNow(); await saveNow();
await closeAddCardsBackend({ val: await shouldPromptBeforeClosing() }); await closeAddCardsBackend({ val: await shouldPromptBeforeClosing() });
} }
async function closeEditCurrent() { async function closeEditCurrent() {
saveNow(); await saveNow();
await closeEditCurrentBackend({}); await closeEditCurrentBackend({});
} }
@ -514,10 +514,10 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
historyModal.show(); historyModal.show();
} }
export function saveOnPageHide() { export async function saveOnPageHide() {
if (document.visibilityState === "hidden") { if (document.visibilityState === "hidden") {
// will fire on session close and minimize // will fire on session close and minimize
saveFieldNow(); await saveNow();
} }
} }
@ -581,6 +581,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
} }
async function addCurrentNoteInner(deckId: bigint) { async function addCurrentNoteInner(deckId: bigint) {
await saveNow();
if (!(await noteCanBeAdded())) { if (!(await noteCanBeAdded())) {
return; return;
} }
@ -830,7 +831,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
fieldStores[ioFields.image].set(options.html); fieldStores[ioFields.image].set(options.html);
// the image field is set programmatically and does not need debouncing // the image field is set programmatically and does not need debouncing
// commit immediately to avoid a race condition with the occlusions field // commit immediately to avoid a race condition with the occlusions field
saveFieldNow(); await saveFieldNow();
// new image is being added // new image is being added
if (isIOImageLoaded) { if (isIOImageLoaded) {
@ -909,13 +910,13 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
} }
// reset for new occlusion in add mode // reset for new occlusion in add mode
function resetIOImageLoaded() { async function resetIOImageLoaded() {
isIOImageLoaded = false; isIOImageLoaded = false;
globalThis.canvas.clear(); globalThis.canvas.clear();
globalThis.canvas = undefined; globalThis.canvas = undefined;
if (imageOcclusionMode?.kind === "add") { if (imageOcclusionMode?.kind === "add") {
// canvas.clear indirectly calls saveOcclusions // canvas.clear indirectly calls saveOcclusions
saveFieldNow(); await saveFieldNow();
fieldStores[ioFields.image].set(""); fieldStores[ioFields.image].set("");
} }
const page = document.querySelector(".image-occlusion"); const page = document.querySelector(".image-occlusion");