Replace unload handlers with visibilitychange (#36387)

Co-authored-by: Mathias Jakobsen <mathias.jakobsen@overleaf.com>
GitOrigin-RevId: 3b52b09445597d93eb5fdfa5a343fbc2fac14d6c
This commit is contained in:
Alf Eaton
2026-09-17 11:57:24 +00:00
committed by Copybot
co-authored by Mathias Jakobsen
parent aac9e669a3
commit 8c869db734
2 changed files with 23 additions and 11 deletions
@@ -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)
},
}
}),
@@ -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)
}
},
}
},