diff --git a/src/components/read-mode-flow.jsx b/src/components/read-mode-flow.jsx index bd157a2..d7e5d0a 100644 --- a/src/components/read-mode-flow.jsx +++ b/src/components/read-mode-flow.jsx @@ -8,6 +8,7 @@ import { metadataVariableTranslation } from "../data/metadataTranslator.js"; export default function ReadModeFlow({ cookiePermission, savedPageIndex, setSavedPageIndex, setReadmode, pefObject }) { const [bookView, setBookView] = useState(FormatModeEnum.BRAILLE_VIEW) + const [hasScrolled, setHasScrolled] = useState(false) const [autoSave, setAutoSave] = useState(true) let maxPageIndex let startPageIndex @@ -15,20 +16,26 @@ export default function ReadModeFlow({ cookiePermission, savedPageIndex, setSave updateBrowserTabText(pefObject.metaData.title) useEffect(() => { - if (savedPageIndex !== null && savedPageIndex !== undefined) { - const pageId = `page-${savedPageIndex}`; - const element = document.getElementById(pageId); - - if (element) { - element.scrollIntoView({ behavior: "smooth" }); - element.focus(); + // Check if the scroll action has not been performed yet + if (!hasScrolled) { + if (savedPageIndex) { + const pageId = `page-${savedPageIndex}`; + const element = document.getElementById(pageId); + + if (element) { + // Scroll the element into view smoothly and set focus on it + element.scrollIntoView({ behavior: 'smooth' }); + element.focus(); + // Mark the scroll action as performed + setHasScrolled(true); + } else { + console.error(`Error: Unable to find the specified element with id ${pageId}.`); + } } else { - console.error(`Error: Unable to find the specified element with id ${pageId}.`); + console.error('Error: There is no saved page index or cookie.'); } - } else { - console.error('Error: There is no saved page index or cookie.'); } - }, [pefObject, bookView]); // remove savedPageIndex and use callback + }, [savedPageIndex, hasScrolled]); useEffect(() => { if (autoSave) { @@ -215,9 +222,11 @@ export default function ReadModeFlow({ cookiePermission, savedPageIndex, setSave } -
- debug savedPageIndex: {savedPageIndex} -
+ {/* only for debug */} + {/* savedPageIndex && +
+ debug savedPageIndex: {savedPageIndex} +
*/} {cookiePermission === CookieEnum.DENIED &&
@@ -234,7 +243,6 @@ export default function ReadModeFlow({ cookiePermission, savedPageIndex, setSave { /* navigator buttons */}
-
-
- -