Something went wrong. Try again.
An experimental Wallabag client
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970// We store reading position as a 0-1 fraction of scrollable distance, not raw// pixels. This survives reader-pref changes (font size, width) and viewport// resizes between sessions — the user lands at roughly the same paragraph even// if the document height has shifted.
export function computeScrollPercent( scrollY: number, scrollHeight: number, innerHeight: number): number { const max = scrollHeight - innerHeight; if (max <= 0) return 0; const raw = scrollY / max; if (!Number.isFinite(raw)) return 0; return Math.min(1, Math.max(0, raw));}
export function scrollTargetFromPercent( percent: number, scrollHeight: number, innerHeight: number): number { const max = scrollHeight - innerHeight; if (max <= 0) return 0; const clamped = Math.min(1, Math.max(0, percent)); return Math.round(max * clamped);}
// Trivial trailing-edge debounce. Returns a wrapper and a flush() helper that// fires any pending call immediately — used when the user is about to leave// the page and we don't want to lose the last scroll position.export function debounce<Args extends unknown[]>( fn: (...args: Args) => void, delayMs: number): { call: (...args: Args) => void; flush: () => void; cancel: () => void } { let timer: ReturnType<typeof setTimeout> | null = null; let pendingArgs: Args | null = null;
function call(...args: Args) { pendingArgs = args; if (timer) clearTimeout(timer); timer = setTimeout(() => { timer = null; const a = pendingArgs; pendingArgs = null; if (a) fn(...a); }, delayMs); }
function flush() { if (timer) { clearTimeout(timer); timer = null; } const a = pendingArgs; pendingArgs = null; if (a) fn(...a); }
function cancel() { if (timer) { clearTimeout(timer); timer = null; } pendingArgs = null; }
return { call, flush, cancel };}