openreader/src/lib/client/epub/tts-epub-preload.ts
Richard R 08d9218ce4 refactor(client,tts,html): migrate EPUB TTS utilities to client and unify HTML progress logic
Move `epub-word-highlight`, `tts-epub-handoff`, and `tts-epub-preload` from shared to client/epub to clarify their usage and improve bundling. Update all imports to use new client paths. Extend TTS and sidebar logic to support HTML document progress tracking and restoration, introducing a new location format for HTML and consistent handling for non-EPUB readers.

BREAKING CHANGE: EPUB TTS utilities relocated to `lib/client/epub`; update import paths. HTML progress persistence now uses a new format incompatible with previous versions.
2026-05-12 17:51:17 -06:00

38 lines
1.3 KiB
TypeScript

import type { CanonicalTtsSourceUnit } from '@/lib/shared/tts-segment-plan';
import { normalizeEpubLocationToken } from '@/lib/shared/tts-locator';
export interface EpubWalkerLocationItem {
cfi: string;
}
/**
* Keep the EPUB depth contract explicit:
* - `maxDepth` counts the current location as depth 1
* - upcoming preload targets are therefore `maxDepth - 1`
*/
export function selectUpcomingWalkerItems<T extends EpubWalkerLocationItem>(
locationItems: readonly T[],
currentCfi: string,
maxDepth: number,
): T[] {
const currentToken = normalizeEpubLocationToken(String(currentCfi || ''));
const filtered = locationItems.filter((item) =>
normalizeEpubLocationToken(item.cfi) !== currentToken,
);
const targetDepth = Math.max(0, maxDepth - 1);
return filtered.slice(0, targetDepth);
}
/**
* Build the canonical source-unit list used for walker-based EPUB planning.
* When smart splitting is enabled we seed with live context units
* (previous/current) so walker boundary behavior aligns with setText.
*/
export function buildWalkerPlanningSourceUnits(
smartSentenceSplitting: boolean,
contextUnits: readonly CanonicalTtsSourceUnit[],
upcomingUnits: readonly CanonicalTtsSourceUnit[],
): CanonicalTtsSourceUnit[] {
if (!smartSentenceSplitting) return [...upcomingUnits];
return [...contextUnits, ...upcomingUnits];
}