// A VISITOR'S OWN COPY ON THE TRANSCRIPT'S CLOCK (lane missing-source, slice 2). // // The export site can play a local file the visitor picks — from an object URL, // in the page, never uploaded — with the transcript following it. Their copy // may start earlier or later than the cues' clock, so everything the page does // (follow, click-to-seek, clip marks, Share's t=) stays on the CUE clock and the // file is reached through one offset: // // fileTime = cueTime + offset cueTime = fileTime - offset // // A positive offset: the transcript's 0:00 is that far into the file (the copy // has a lead-in). The offset is the ONLY thing ever stored, per video, in // localStorage — never the file's name, size or anything else about it. // // Pure: client-safe, no node: imports, no I/O but the Storage handed in. export const OFFSET_LIMIT_SECONDS = 24 * 3600; export const OFFSET_KEY_PREFIX = "archilyzer:local-offset:"; const round = (v: number) => Math.round(v * 100) / 100; const clampOffset = (v: number) => Math.max(-OFFSET_LIMIT_SECONDS, Math.min(OFFSET_LIMIT_SECONDS, round(v))); export function toFileTime(cueTime: number, offset: number): number { return Math.max(0, cueTime + offset); } export function toCueTime(fileTime: number, offset: number): number { return Math.max(0, fileTime - offset); } export function nudgeOffset(offset: number, delta: number): number { return clampOffset(offset + delta); } // "This line is playing now": the line that starts at `cueStart` is what the // file plays at `fileTime`. export function offsetFromSync(fileTime: number, cueStart: number): number { return clampOffset(fileTime - cueStart); } export function formatOffset(offset: number): string { const v = round(offset); if (v === 0) return "0.00 s"; return `${v > 0 ? "+" : "−"}${Math.abs(v).toFixed(2)} s`; } export function offsetKey(slug: string): string { return `${OFFSET_KEY_PREFIX}${slug}`; } type OffsetStore = Pick; // A stored offset, or 0 when there is none, it is not a finite number in range, // or storage is unavailable (a private window, blocked site data). export function readOffset(store: OffsetStore | null | undefined, slug: string): number { try { const raw = store?.getItem(offsetKey(slug)); if (raw == null) return 0; const v = Number(raw); if (!Number.isFinite(v) || Math.abs(v) > OFFSET_LIMIT_SECONDS) return 0; return round(v); } catch { return 0; } } // 0 removes the key: nothing is kept for a copy that needs no offset. export function writeOffset(store: OffsetStore | null | undefined, slug: string, offset: number): void { try { const v = clampOffset(offset); if (v === 0) store?.removeItem(offsetKey(slug)); else store?.setItem(offsetKey(slug), String(v)); } catch { /* storage refused: the offset lives for this page only */ } }