diff --git a/services/web/frontend/js/features/source-editor/extensions/cursor-position.ts b/services/web/frontend/js/features/source-editor/extensions/cursor-position.ts index 0397f26123..7e384cc797 100644 --- a/services/web/frontend/js/features/source-editor/extensions/cursor-position.ts +++ b/services/web/frontend/js/features/source-editor/extensions/cursor-position.ts @@ -24,16 +24,21 @@ export const cursorPosition = ({ return [ // store cursor position ViewPlugin.define(view => { - const unloadListener = () => { - storeCursorPosition(view, docId) + const handleVisibilityChange = () => { + if (document.visibilityState === 'hidden') { + storeCursorPosition(view, docId) + } } - window.addEventListener('unload', unloadListener) + document.addEventListener('visibilitychange', handleVisibilityChange) return { destroy: () => { - window.removeEventListener('unload', unloadListener) - unloadListener() + document.removeEventListener( + 'visibilitychange', + handleVisibilityChange + ) + storeCursorPosition(view, docId) }, } }), diff --git a/services/web/frontend/js/features/source-editor/extensions/scroll-position.ts b/services/web/frontend/js/features/source-editor/extensions/scroll-position.ts index 0133603309..4c123f0c88 100644 --- a/services/web/frontend/js/features/source-editor/extensions/scroll-position.ts +++ b/services/web/frontend/js/features/source-editor/extensions/scroll-position.ts @@ -51,13 +51,15 @@ export const scrollPosition = ( // store/dispatch scroll position ViewPlugin.define( view => { - const unloadListener = () => { - if (lineInfo) { - storeScrollPosition(lineInfo, view, docId) + const handleVisibilityChange = () => { + if (document.visibilityState === 'hidden') { + if (lineInfo) { + storeScrollPosition(lineInfo, view, docId) + } } } - window.addEventListener('unload', unloadListener) + document.addEventListener('visibilitychange', handleVisibilityChange) return { update: (update: ViewUpdate) => { @@ -82,8 +84,13 @@ export const scrollPosition = ( }, destroy: () => { scrollHandler.cancel() - window.removeEventListener('unload', unloadListener) - unloadListener() + document.removeEventListener( + 'visibilitychange', + handleVisibilityChange + ) + if (lineInfo) { + storeScrollPosition(lineInfo, view, docId) + } }, } },