Files
SnowLuma/.pnpm-store/v11/files/a3/02ad68aeb06829709501ae92570d311cb3a3a8dd8f8e7e648e20da03066fcb4b42a4617ff6ec61802df877803d4a55bbfc76a191a4028702ce8a745e737a78
TanTan 2a69b23b47
Dev Build / cleanup (push) Canceled after 0s
Dev Build / typecheck (push) Canceled after 0s
Dev Build / build (linux-arm64, ubuntu-24.04-arm, linux-arm64) (push) Canceled after 0s
Dev Build / build (linux-x64, ubuntu-latest, linux-x64) (push) Canceled after 0s
Dev Build / build (win-x64, windows-latest, win32-x64) (push) Canceled after 0s
Dev Build / dispatch-dev-docker (push) Canceled after 0s
docs: 仓库链接指向本镜像仓库
2026-11-13 09:27:07 +08:00

82 lines
1.8 KiB
Plaintext

export default function (options: {
storageKey: string
key?: string
behavior?: ScrollToOptions['behavior']
shouldScrollRestoration?: boolean
}) {
let byKey
try {
byKey = JSON.parse(sessionStorage.getItem(options.storageKey) || '{}')
} catch (error) {
console.error(error)
return
}
const resolvedKey = options.key || window.history.state?.__TSR_key
const elementEntries = resolvedKey ? byKey[resolvedKey] : undefined
if (
options.shouldScrollRestoration &&
elementEntries &&
typeof elementEntries === 'object' &&
Object.keys(elementEntries).length > 0
) {
for (const elementSelector in elementEntries) {
const entry = elementEntries[elementSelector]
if (!entry || typeof entry !== 'object') {
continue
}
const scrollX = entry.scrollX
const scrollY = entry.scrollY
if (!Number.isFinite(scrollX) || !Number.isFinite(scrollY)) {
continue
}
if (elementSelector === 'window') {
window.scrollTo({
top: scrollY,
left: scrollX,
behavior: options.behavior,
})
} else if (elementSelector) {
let element
try {
element = document.querySelector(elementSelector)
} catch {
continue
}
if (element) {
element.scrollLeft = scrollX
element.scrollTop = scrollY
}
}
}
return
}
const hash = window.location.hash.split('#', 2)[1]
if (hash) {
const hashScrollIntoViewOptions =
window.history.state?.__hashScrollIntoViewOptions ?? true
if (hashScrollIntoViewOptions) {
const el = document.getElementById(hash)
if (el) {
el.scrollIntoView(hashScrollIntoViewOptions)
}
}
return
}
window.scrollTo({ top: 0, left: 0, behavior: options.behavior })
}