diff options
Diffstat (limited to 'FrontEnd/src/app/utilities/useReverseScrollPositionRemember.ts')
-rw-r--r-- | FrontEnd/src/app/utilities/useReverseScrollPositionRemember.ts | 62 |
1 files changed, 35 insertions, 27 deletions
diff --git a/FrontEnd/src/app/utilities/useReverseScrollPositionRemember.ts b/FrontEnd/src/app/utilities/useReverseScrollPositionRemember.ts index 67c7c458..742751eb 100644 --- a/FrontEnd/src/app/utilities/useReverseScrollPositionRemember.ts +++ b/FrontEnd/src/app/utilities/useReverseScrollPositionRemember.ts @@ -1,39 +1,47 @@ import React from "react"; let on = false; +let recordDisabled = false; + +function getScrollPosition(): number { + if (document.documentElement.scrollHeight <= window.innerHeight) { + return 0; + } else { + return ( + document.documentElement.scrollHeight - + document.documentElement.scrollTop - + window.innerHeight + ); + } +} + +let scrollPosition = getScrollPosition(); + +function scrollToRecordPosition(): void { + if (document.documentElement.scrollHeight <= window.innerHeight) return; + document.documentElement.scrollTop = + document.documentElement.scrollHeight - window.innerHeight - scrollPosition; +} + +const scrollListener = (): void => { + if (recordDisabled) return; + scrollPosition = getScrollPosition(); +}; + +const resizeObserver = new ResizeObserver(() => { + scrollToRecordPosition(); +}); + +export function setRecordDisabled(disabled: boolean): void { + recordDisabled = disabled; + if (!disabled) scrollToRecordPosition(); +} export default function useReverseScrollPositionRemember(): void { React.useEffect(() => { if (on) return; on = true; - - function getScrollPosition(): number { - if (document.documentElement.scrollHeight <= window.innerHeight) { - return 0; - } else { - return ( - document.documentElement.scrollHeight - - document.documentElement.scrollTop - - window.innerHeight - ); - } - } - - let scrollPosition = getScrollPosition(); - const scrollListener = (): void => { - scrollPosition = getScrollPosition(); - }; - window.addEventListener("scroll", scrollListener); - - const resizeObserver = new ResizeObserver(() => { - if (document.documentElement.scrollHeight <= window.innerHeight) return; - document.documentElement.scrollTop = - document.documentElement.scrollHeight - - window.innerHeight - - scrollPosition; - }); - resizeObserver.observe(document.documentElement); return () => { |