feat(cbz): pure chapter-window helpers (chapterIndexAt/chapterWin) with table tests

This commit is contained in:
2026-09-08 14:56:46 +08:00
parent bba82e5529
commit cd251b940c
2 changed files with 63 additions and 1 deletions
+30
View File
@@ -76,3 +76,33 @@ export class PageHeights {
return [a, b];
}
}
/** 页号 → 所在章下标;无章(扁平包或单组)恒 0;首章 start 之前的散页归首章。 */
export function chapterIndexAt(chs: { start: number }[] | null, page: number): number {
if (!chs || chs.length < 2) return 0;
return chs.reduce((acc, c, i) => (c.start <= page ? i : acc), 0);
}
export interface ChapterWindow {
start: number; // 窗口首页(含)——滚动/挂载下界
end: number; // 窗口末页(不含)——锁章时的滚动边界
mountEnd: number; // 挂载上界(含预读章),恒 ≤ count 且 ≥ end
}
/** 阅读窗口:连读或扁平包 = 全书;锁章 = 本章滚动、本章 + prefetch 章挂载。 */
export function chapterWin(
chs: { start: number }[] | null,
count: number,
ci: number,
continuous: boolean,
prefetch: number,
): ChapterWindow {
if (!chs || chs.length < 2 || continuous) return { start: 0, end: count, mountEnd: count };
const c = Math.max(0, Math.min(ci, chs.length - 1));
const end = chs[c + 1]?.start ?? count;
return {
start: c === 0 ? 0 : chs[c].start,
end,
mountEnd: chs[c + 1 + Math.max(0, prefetch)]?.start ?? count,
};
}