mirror of
https://github.com/jackyzha0/quartz
synced 2026-11-27 22:14:40 +01:00
fix(spa): keep a scroll position still pending when Back/Forward leaves the entry
This commit is contained in:
2 files changed
+45
-4
No files matched your search
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@jackyzha0/quartz": patch
|
||||
---
|
||||
|
||||
A scroll made just before Back (less than 150 ms) is no longer lost: Forward returns to where the reader scrolled.
|
||||
@@ -70,13 +70,43 @@ history.scrollRestoration = "manual"
|
||||
let renderedPath = window.location.pathname
|
||||
let saveTimer: ReturnType<typeof setTimeout> | undefined
|
||||
|
||||
// Each entry the router creates gets a key, so a position that was still waiting to be saved
|
||||
// when Back or Forward left its entry (history.state is the new entry's by then) is kept here
|
||||
// and written to the entry when it is shown again.
|
||||
type EntryState = { key?: string; scrollY?: unknown } | null
|
||||
const newKey = () => Date.now().toString(36) + Math.random().toString(36).slice(2, 8)
|
||||
const unsavedScroll = new Map<string, number>()
|
||||
let shownKey: string | undefined
|
||||
|
||||
function entryKey(): string {
|
||||
let key = (history.state as EntryState)?.key
|
||||
if (typeof key !== "string") {
|
||||
key = newKey()
|
||||
history.replaceState({ ...(history.state ?? {}), key }, "")
|
||||
}
|
||||
return key
|
||||
}
|
||||
|
||||
function pushEntry(url: URL) {
|
||||
history.pushState({ key: newKey() }, "", url)
|
||||
shownKey = entryKey()
|
||||
}
|
||||
|
||||
function saveScroll() {
|
||||
clearTimeout(saveTimer)
|
||||
saveTimer = undefined
|
||||
history.replaceState({ ...(history.state ?? {}), scrollY: window.scrollY }, "")
|
||||
}
|
||||
|
||||
function savedScroll(): number | undefined {
|
||||
const y = (history.state as { scrollY?: unknown } | null)?.scrollY
|
||||
const key = entryKey()
|
||||
const unsaved = unsavedScroll.get(key)
|
||||
if (unsaved !== undefined) {
|
||||
unsavedScroll.delete(key)
|
||||
history.replaceState({ ...(history.state ?? {}), scrollY: unsaved }, "")
|
||||
return unsaved
|
||||
}
|
||||
const y = (history.state as EntryState)?.scrollY
|
||||
return typeof y === "number" ? y : undefined
|
||||
}
|
||||
|
||||
@@ -185,7 +215,7 @@ async function _navigate(url: URL, isBack: boolean = false) {
|
||||
// delay setting the url until now
|
||||
// at this point everything is loaded so changing the url should resolve to the correct addresses
|
||||
if (!isBack) {
|
||||
history.pushState({}, "", finalUrl)
|
||||
pushEntry(finalUrl)
|
||||
}
|
||||
renderedPath = window.location.pathname
|
||||
|
||||
@@ -220,7 +250,7 @@ function createRouter() {
|
||||
if (isSamePage(url) && url.hash) {
|
||||
// Save where the reader was, then add the entry, then jump: Back returns here.
|
||||
saveScroll()
|
||||
history.pushState({}, "", url)
|
||||
pushEntry(url)
|
||||
const el = document.getElementById(decodeURIComponent(url.hash.substring(1)))
|
||||
el?.scrollIntoView()
|
||||
return
|
||||
@@ -230,8 +260,13 @@ function createRouter() {
|
||||
})
|
||||
|
||||
window.addEventListener("popstate", () => {
|
||||
// Whatever is pending belongs to the entry just left.
|
||||
// Whatever is pending belongs to the entry just left: keep it for when it's shown again.
|
||||
if (saveTimer !== undefined && shownKey !== undefined) {
|
||||
unsavedScroll.set(shownKey, window.scrollY)
|
||||
}
|
||||
clearTimeout(saveTimer)
|
||||
saveTimer = undefined
|
||||
shownKey = entryKey()
|
||||
// Back or Forward between headings of the page on screen: only scroll.
|
||||
if (window.location.pathname === renderedPath) {
|
||||
restoreScroll()
|
||||
@@ -257,6 +292,7 @@ function createRouter() {
|
||||
window.addEventListener("pagehide", () => {
|
||||
if (!isNavigating) saveScroll()
|
||||
})
|
||||
shownKey = entryKey()
|
||||
if (savedScroll() !== undefined) restoreScroll()
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user