Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion web/src/ui/locator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ export interface Locator {
}

export const LOCATOR_PREFIX = 'r1';
const OFFSET_DECIMALS = 4;
export const LOCATOR_OFFSET_ROUNDING_ERROR = 0.5 * 10 ** -OFFSET_DECIMALS;

export function parseLocator(value: string, doc: BookDoc): Locator | null {
if (!value) return null;
Expand Down Expand Up @@ -95,7 +97,7 @@ function resolve(sectionId: string, offset: number, doc: BookDoc): Locator | nul
*/
export function formatLocator(sectionId: string, offset: number): string {
const clamped = Math.min(1, Math.max(0, Number.isFinite(offset) ? offset : 0));
return `${LOCATOR_PREFIX}:${clamped.toFixed(4)}:${sectionId}`;
return `${LOCATOR_PREFIX}:${clamped.toFixed(OFFSET_DECIMALS)}:${sectionId}`;
}

/**
Expand Down
31 changes: 21 additions & 10 deletions web/src/ui/reader-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { NativePageHost } from './native-page.ts';
import { ResourceResolver, hydrateResources } from './resources.ts';
import { createBookHost, extractBody, extractInlineStyles, type BookShadowHost } from './shadow.ts';
import { escapeHtml, textToChapterHtml } from '../formats/segments.ts';
import { bookPercentage, formatLocator, parseLocator } from './locator.ts';
import { bookPercentage, formatLocator, LOCATOR_OFFSET_ROUNDING_ERROR, parseLocator } from './locator.ts';
import { collectSpokenChunks, type SpokenChunk } from '../render/tts-text.ts';
import { adoptWindow } from '../formats/windowed.ts';
import { BOOK_RESOURCE_MARKER, chapterRefFromLink } from '../formats/book-resource.ts';
Expand Down Expand Up @@ -153,6 +153,7 @@ export interface ViewSettings {

/** A stand-in for the section index being out of range, so a caller need not guard. */
const EMPTY_SECTION: Section = { id: '', label: '', depth: 0 };
const SCROLL_PIXEL_TOLERANCE = 1;

const DEFAULT_SETTINGS: ViewSettings = {
mode: 'scroll',
Expand Down Expand Up @@ -1477,8 +1478,17 @@ export class ReaderView {
this.syncPageMask();
return;
}
const limit = Math.max(0, scroller.scrollHeight - scroller.clientHeight);
scroller.scrollTop = limit * this.sectionOffset;
const limit = this.scrollRange();
const target = limit * this.sectionOffset;
let offset = target;
if (target > 0 && target < limit) {
// A saved page start can land just before its boundary after locator and
// browser rounding. Restore the exact boundary without snapping free scroll.
const tolerance = SCROLL_PIXEL_TOLERANCE + limit * LOCATOR_OFFSET_ROUNDING_ERROR;
const boundary = this.screenBoundaryAt(target, tolerance);
if (Math.abs(boundary.offset - target) <= tolerance) offset = boundary.offset;
}
scroller.scrollTop = offset;
this.syncPageMask();
}

Expand Down Expand Up @@ -1570,15 +1580,16 @@ export class ReaderView {
* is: see `screenOffset`.
*/
private scrollPage(): number {
return this.screenBoundaryAt(this.scroller.scrollTop, SCROLL_PIXEL_TOLERANCE).page;
}

private screenBoundaryAt(top: number, tolerance: number): { page: number; offset: number } {
const total = this.screenCount();
const top = this.scroller.scrollTop;
for (let page = total - 1; page >= 0; page -= 1) {
// A tolerance of one pixel, because the browser rounds `scrollTop` on read
// (`722.99` is reported as `723`) and an exact comparison would then put the
// reader on the page *before* the one they are looking at.
if (top >= this.screenOffset(page) - 1) return page;
for (let page = total - 1; page > 0; page -= 1) {
const offset = this.screenOffset(page);
if (top >= offset - tolerance) return { page, offset };
}
return 0;
return { page: 0, offset: 0 };
}

/**
Expand Down
34 changes: 33 additions & 1 deletion web/test/reader-paging.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,15 @@ beforeAll(() => {
function scrollable(
element: HTMLElement,
geometry: { clientHeight: number; scrollHeight: number; clientWidth?: number; scrollWidth?: number },
quantizeScrollTop = Math.round,
): void {
const state = { top: 0, left: 0 };
Object.defineProperty(element, 'clientHeight', { get: () => geometry.clientHeight, configurable: true });
Object.defineProperty(element, 'scrollHeight', { get: () => geometry.scrollHeight, configurable: true });
Object.defineProperty(element, 'clientWidth', { get: () => geometry.clientWidth ?? 400, configurable: true });
Object.defineProperty(element, 'scrollWidth', { get: () => geometry.scrollWidth ?? 400, configurable: true });
Object.defineProperty(element, 'scrollTop', {
get: () => Math.round(state.top),
get: () => quantizeScrollTop(state.top),
set: (value: number) => {
state.top = Math.min(Math.max(0, value), Math.max(0, geometry.scrollHeight - geometry.clientHeight));
},
Expand Down Expand Up @@ -158,6 +159,37 @@ describe('page turns in scroll mode', () => {
}
});

it('reopens a rounded page-three locator at the same screen boundary', async () => {
const pages: number[] = [];
const view = new ReaderView({
container,
doc: doc(1),
onPositionChange: (position) => pages.push(position.pageInChapter),
});
const host = view.elementHost;
scrollable(host, { clientHeight: 650, scrollHeight: 4254 }, Math.floor);
try {
await view.open(0);
await view.next();
await view.next();
const saved = view.position();
expect([saved.pageInChapter, saved.chapterPages]).toEqual([3, 7]);
const pageStart = host.scrollTop;

await view.open(0);
await view.openLocator(saved.locator);
expect(host.scrollTop).toBe(pageStart);
expect(pages.at(-1)).toBe(3);
await view.next();
expect(view.position().pageInChapter).toBe(4);

await view.openLocator('r1:0.4000:ch0.xhtml');
expect(host.scrollTop).toBe(1441);
} finally {
view.dispose();
}
});

it('walks back page by page and reports the page it is on', async () => {
// The regression: `previous()` decided whether it had room by comparing
// `scrollTop` against 8, then *subtracted* 0.9 of a screen. The two numbers do
Expand Down
Loading