mirror of
https://github.com/yu-i-i/overleaf-cep.git
synced 2026-11-14 14:13:43 +01:00
Store the scroll position of the PDF per root doc (#20376)
GitOrigin-RevId: 561c38b7258df40f0e738fdc2575e23d9d15102a
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
import { Dispatch, SetStateAction, useEffect, useRef, useState } from 'react'
|
||||
import customLocalStorage from '@/infrastructure/local-storage'
|
||||
import { debugConsole } from '@/utils/debugging'
|
||||
|
||||
export type PdfScrollPosition = Record<string, any> | undefined
|
||||
|
||||
export const usePdfScrollPosition = (
|
||||
lastCompileRootDocId: string | null | undefined
|
||||
): [PdfScrollPosition, Dispatch<SetStateAction<PdfScrollPosition>>] => {
|
||||
// scroll position of the PDF
|
||||
const [position, setPosition] = useState<PdfScrollPosition>()
|
||||
|
||||
const lastCompileRootDocIdRef = useRef<string | null | undefined>(
|
||||
lastCompileRootDocId
|
||||
)
|
||||
useEffect(() => {
|
||||
lastCompileRootDocIdRef.current = lastCompileRootDocId
|
||||
}, [lastCompileRootDocId])
|
||||
|
||||
const initialScrollPositionRef = useRef<PdfScrollPosition | null>(null)
|
||||
|
||||
// load the stored PDF scroll position when the compiled root doc changes
|
||||
useEffect(() => {
|
||||
if (lastCompileRootDocId) {
|
||||
const position = customLocalStorage.getItem(
|
||||
`pdf.position.${lastCompileRootDocId}`
|
||||
)
|
||||
if (position) {
|
||||
debugConsole.log('loaded position for', lastCompileRootDocId, position)
|
||||
initialScrollPositionRef.current = position
|
||||
setPosition(position)
|
||||
}
|
||||
}
|
||||
}, [lastCompileRootDocId])
|
||||
|
||||
// store the current root doc's PDF scroll position when it changes
|
||||
useEffect(() => {
|
||||
if (
|
||||
lastCompileRootDocIdRef.current &&
|
||||
position &&
|
||||
position !== initialScrollPositionRef.current
|
||||
) {
|
||||
debugConsole.log(
|
||||
'storing position for',
|
||||
lastCompileRootDocIdRef.current,
|
||||
position
|
||||
)
|
||||
customLocalStorage.setItem(
|
||||
`pdf.position.${lastCompileRootDocIdRef.current}`,
|
||||
position
|
||||
)
|
||||
}
|
||||
}, [position])
|
||||
|
||||
return [position, setPosition]
|
||||
}
|
||||
Reference in New Issue
Block a user