From 91f88aa328825338436dfb3168e6009e0f247c2b Mon Sep 17 00:00:00 2001 From: Raphael Forment Date: Fri, 10 Nov 2023 19:55:57 +0100 Subject: [PATCH] Try new saving technique for PWA --- src/WindowBehavior.ts | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/src/WindowBehavior.ts b/src/WindowBehavior.ts index 1c8f8f9..f85c6b6 100644 --- a/src/WindowBehavior.ts +++ b/src/WindowBehavior.ts @@ -16,14 +16,23 @@ const handleResize = (canvas: HTMLCanvasElement) => { } }; +const saveBeforeExit = (app: Editor): void => { + // @ts-ignore + event.preventDefault(); + // Iterate over all local files and set the candidate to the committed + app.currentFile().candidate = app.view.state.doc.toString(); + app.currentFile().committed = app.view.state.doc.toString(); + app.settings.saveApplicationToLocalStorage(app.universes, app.settings); + app.clock.stop(); + return null; +}; + export const installWindowBehaviors = ( app: Editor, window: Window, preventMultipleTabs: boolean = false ) => { - - window.addEventListener("resize", () => handleResize(app.interface.scope as HTMLCanvasElement) ); @@ -31,14 +40,10 @@ export const installWindowBehaviors = ( handleResize(app.interface.feedback as HTMLCanvasElement) ); window.addEventListener("beforeunload", () => { - // @ts-ignore - event.preventDefault(); - // Iterate over all local files and set the candidate to the committed - app.currentFile().candidate = app.view.state.doc.toString(); - app.currentFile().committed = app.view.state.doc.toString(); - app.settings.saveApplicationToLocalStorage(app.universes, app.settings); - app.clock.stop(); - return null; + saveBeforeExit(app) + }); + window.addEventListener("visibilitychange", () => { + saveBeforeExit(app) }); if (preventMultipleTabs) {