/** * "Resume where you left off": the last page read per PDF, kept in this * browser only (local storage), and a small prompt to jump back to it. */ export type ResumeMode = 'ask' | 'auto' | 'off'; interface Entry { page: number; total: number; /** Unix ms */ at: number; } const KEY = 'pdfrack-resume'; const MAX_ENTRIES = 50; const MAX_AGE = 180 * 24 * 60 * 60 * 1000; export function resolveResumeMode(value: unknown): ResumeMode { return value === 'auto' || value === 'off' ? value : 'ask'; } /** Stable key for a document: its attachment ID, else its URL without query. */ export function resumeKey(pdfId: number, pdfUrl?: string): string | null { if (pdfId) return `id:${pdfId}`; if (!pdfUrl) return null; try { const url = new URL(pdfUrl, window.location.href); return `url:${url.host}${url.pathname}`; } catch { return null; } } function load(): Record { try { const data = JSON.parse(window.localStorage.getItem(KEY) || '{}'); return data && typeof data === 'object' ? data : {}; } catch { return {}; } } function save(data: Record): void { try { window.localStorage.setItem(KEY, JSON.stringify(data)); } catch { /* storage full or blocked: resuming is a nicety */ } } export function savedPage(key: string): number { const entry = load()[key]; if (!entry || Date.now() - entry.at > MAX_AGE) return 0; return entry.page > 1 && (!entry.total || entry.page <= entry.total) ? entry.page : 0; } /** Throttled writer for page changes; page 1 clears the entry. */ export function createResumeWriter(key: string): (page: number, total: number) => void { let timer = 0; let pending: [number, number] | null = null; const flush = () => { timer = 0; if (!pending) return; const [page, total] = pending; pending = null; const data = load(); if (page <= 1) { delete data[key]; } else { data[key] = { page, total, at: Date.now() }; const keys = Object.keys(data); if (keys.length > MAX_ENTRIES) { keys.sort((a, b) => data[a].at - data[b].at) .slice(0, keys.length - MAX_ENTRIES) .forEach((k) => delete data[k]); } } save(data); }; window.addEventListener('pagehide', flush); return (page, total) => { if (!total) return; pending = [page, total]; if (!timer) timer = window.setTimeout(flush, 800); }; } /** * "Continue from page N?" chip at the top of the viewer. * Disappears on its own after a while; returns a function that removes it. */ export function showResumePrompt(container: HTMLElement, page: number, onResume: () => void): () => void { container.querySelector('.pdfrack-resume')?.remove(); const root = document.createElement('div'); root.className = 'pdfrack-resume'; root.setAttribute('role', 'status'); root.innerHTML = ` Continue from page ${page}? `; let timer = 0; const close = () => { window.clearTimeout(timer); root.classList.add('pdfrack-resume-out'); window.setTimeout(() => root.remove(), 200); }; root.querySelector('.pdfrack-resume-go')?.addEventListener('click', (e) => { e.stopPropagation(); onResume(); close(); }); root.querySelector('.pdfrack-resume-close')?.addEventListener('click', (e) => { e.stopPropagation(); close(); }); // Keep the viewer's drag-to-pan from seeing presses on the prompt. ['pointerdown', 'mousedown', 'touchstart'].forEach((type) => root.addEventListener(type, (e) => e.stopPropagation())); // Stay while the pointer is on it. root.addEventListener('pointerenter', () => window.clearTimeout(timer)); root.addEventListener('pointerleave', () => { timer = window.setTimeout(close, 4000); }); container.appendChild(root); timer = window.setTimeout(close, 20000); return close; }