wolfy-reader 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +277 -0
- package/dist/core/book.d.ts +75 -0
- package/dist/core/book.d.ts.map +1 -0
- package/dist/core/book.js +2 -0
- package/dist/core/book.js.map +1 -0
- package/dist/core/errors.d.ts +17 -0
- package/dist/core/errors.d.ts.map +1 -0
- package/dist/core/errors.js +29 -0
- package/dist/core/errors.js.map +1 -0
- package/dist/core/format.d.ts +13 -0
- package/dist/core/format.d.ts.map +1 -0
- package/dist/core/format.js +2 -0
- package/dist/core/format.js.map +1 -0
- package/dist/core/graphemes.d.ts +36 -0
- package/dist/core/graphemes.d.ts.map +1 -0
- package/dist/core/graphemes.js +62 -0
- package/dist/core/graphemes.js.map +1 -0
- package/dist/core/index.d.ts +11 -0
- package/dist/core/index.d.ts.map +1 -0
- package/dist/core/index.js +6 -0
- package/dist/core/index.js.map +1 -0
- package/dist/core/lookup.d.ts +10 -0
- package/dist/core/lookup.d.ts.map +1 -0
- package/dist/core/lookup.js +16 -0
- package/dist/core/lookup.js.map +1 -0
- package/dist/core/open.d.ts +12 -0
- package/dist/core/open.d.ts.map +1 -0
- package/dist/core/open.js +23 -0
- package/dist/core/open.js.map +1 -0
- package/dist/core/position.d.ts +121 -0
- package/dist/core/position.d.ts.map +1 -0
- package/dist/core/position.js +327 -0
- package/dist/core/position.js.map +1 -0
- package/dist/core/reading-text.d.ts +77 -0
- package/dist/core/reading-text.d.ts.map +1 -0
- package/dist/core/reading-text.js +144 -0
- package/dist/core/reading-text.js.map +1 -0
- package/dist/core/source.d.ts +17 -0
- package/dist/core/source.d.ts.map +1 -0
- package/dist/core/source.js +39 -0
- package/dist/core/source.js.map +1 -0
- package/dist/core/storage.d.ts +7 -0
- package/dist/core/storage.d.ts.map +1 -0
- package/dist/core/storage.js +2 -0
- package/dist/core/storage.js.map +1 -0
- package/dist/core/text.d.ts +18 -0
- package/dist/core/text.d.ts.map +1 -0
- package/dist/core/text.js +32 -0
- package/dist/core/text.js.map +1 -0
- package/dist/formats/epub/href.d.ts +14 -0
- package/dist/formats/epub/href.d.ts.map +1 -0
- package/dist/formats/epub/href.js +52 -0
- package/dist/formats/epub/href.js.map +1 -0
- package/dist/formats/epub/index.d.ts +3 -0
- package/dist/formats/epub/index.d.ts.map +1 -0
- package/dist/formats/epub/index.js +223 -0
- package/dist/formats/epub/index.js.map +1 -0
- package/dist/formats/epub/opf.d.ts +27 -0
- package/dist/formats/epub/opf.d.ts.map +1 -0
- package/dist/formats/epub/opf.js +110 -0
- package/dist/formats/epub/opf.js.map +1 -0
- package/dist/formats/epub/toc.d.ts +6 -0
- package/dist/formats/epub/toc.d.ts.map +1 -0
- package/dist/formats/epub/toc.js +88 -0
- package/dist/formats/epub/toc.js.map +1 -0
- package/dist/formats/fb2/index.d.ts +13 -0
- package/dist/formats/fb2/index.d.ts.map +1 -0
- package/dist/formats/fb2/index.js +317 -0
- package/dist/formats/fb2/index.js.map +1 -0
- package/dist/formats/index.d.ts +4 -0
- package/dist/formats/index.d.ts.map +1 -0
- package/dist/formats/index.js +4 -0
- package/dist/formats/index.js.map +1 -0
- package/dist/formats/text/index.d.ts +11 -0
- package/dist/formats/text/index.d.ts.map +1 -0
- package/dist/formats/text/index.js +201 -0
- package/dist/formats/text/index.js.map +1 -0
- package/dist/formats/xml.d.ts +33 -0
- package/dist/formats/xml.d.ts.map +1 -0
- package/dist/formats/xml.js +321 -0
- package/dist/formats/xml.js.map +1 -0
- package/dist/index.d.ts +27 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +28 -0
- package/dist/index.js.map +1 -0
- package/dist/layout/chunk.d.ts +51 -0
- package/dist/layout/chunk.d.ts.map +1 -0
- package/dist/layout/chunk.js +131 -0
- package/dist/layout/chunk.js.map +1 -0
- package/dist/layout/index.d.ts +238 -0
- package/dist/layout/index.d.ts.map +1 -0
- package/dist/layout/index.js +476 -0
- package/dist/layout/index.js.map +1 -0
- package/dist/reader/index.d.ts +231 -0
- package/dist/reader/index.d.ts.map +1 -0
- package/dist/reader/index.js +958 -0
- package/dist/reader/index.js.map +1 -0
- package/dist/reader/input.d.ts +69 -0
- package/dist/reader/input.d.ts.map +1 -0
- package/dist/reader/input.js +75 -0
- package/dist/reader/input.js.map +1 -0
- package/dist/search/extract.d.ts +51 -0
- package/dist/search/extract.d.ts.map +1 -0
- package/dist/search/extract.js +401 -0
- package/dist/search/extract.js.map +1 -0
- package/dist/search/index.d.ts +11 -0
- package/dist/search/index.d.ts.map +1 -0
- package/dist/search/index.js +11 -0
- package/dist/search/index.js.map +1 -0
- package/dist/search/matcher.d.ts +56 -0
- package/dist/search/matcher.d.ts.map +1 -0
- package/dist/search/matcher.js +128 -0
- package/dist/search/matcher.js.map +1 -0
- package/dist/search/normalize.d.ts +48 -0
- package/dist/search/normalize.d.ts.map +1 -0
- package/dist/search/normalize.js +103 -0
- package/dist/search/normalize.js.map +1 -0
- package/dist/view/allowlist.d.ts +59 -0
- package/dist/view/allowlist.d.ts.map +1 -0
- package/dist/view/allowlist.js +198 -0
- package/dist/view/allowlist.js.map +1 -0
- package/dist/view/appearance.d.ts +137 -0
- package/dist/view/appearance.d.ts.map +1 -0
- package/dist/view/appearance.js +274 -0
- package/dist/view/appearance.js.map +1 -0
- package/dist/view/css.d.ts +24 -0
- package/dist/view/css.d.ts.map +1 -0
- package/dist/view/css.js +153 -0
- package/dist/view/css.js.map +1 -0
- package/dist/view/frame.d.ts +54 -0
- package/dist/view/frame.d.ts.map +1 -0
- package/dist/view/frame.js +980 -0
- package/dist/view/frame.js.map +1 -0
- package/dist/view/host.d.ts +160 -0
- package/dist/view/host.d.ts.map +1 -0
- package/dist/view/host.js +385 -0
- package/dist/view/host.js.map +1 -0
- package/dist/view/index.d.ts +12 -0
- package/dist/view/index.d.ts.map +1 -0
- package/dist/view/index.js +7 -0
- package/dist/view/index.js.map +1 -0
- package/dist/view/protocol.d.ts +220 -0
- package/dist/view/protocol.d.ts.map +1 -0
- package/dist/view/protocol.js +246 -0
- package/dist/view/protocol.js.map +1 -0
- package/dist/view/reference.d.ts +18 -0
- package/dist/view/reference.d.ts.map +1 -0
- package/dist/view/reference.js +25 -0
- package/dist/view/reference.js.map +1 -0
- package/dist/view/resources.d.ts +51 -0
- package/dist/view/resources.d.ts.map +1 -0
- package/dist/view/resources.js +242 -0
- package/dist/view/resources.js.map +1 -0
- package/dist/view/sanitize.d.ts +32 -0
- package/dist/view/sanitize.d.ts.map +1 -0
- package/dist/view/sanitize.js +226 -0
- package/dist/view/sanitize.js.map +1 -0
- package/dist/zip/central.d.ts +12 -0
- package/dist/zip/central.d.ts.map +1 -0
- package/dist/zip/central.js +72 -0
- package/dist/zip/central.js.map +1 -0
- package/dist/zip/crc32.d.ts +3 -0
- package/dist/zip/crc32.d.ts.map +1 -0
- package/dist/zip/crc32.js +28 -0
- package/dist/zip/crc32.js.map +1 -0
- package/dist/zip/eocd.d.ts +9 -0
- package/dist/zip/eocd.d.ts.map +1 -0
- package/dist/zip/eocd.js +84 -0
- package/dist/zip/eocd.js.map +1 -0
- package/dist/zip/errors.d.ts +19 -0
- package/dist/zip/errors.d.ts.map +1 -0
- package/dist/zip/errors.js +37 -0
- package/dist/zip/errors.js.map +1 -0
- package/dist/zip/index.d.ts +18 -0
- package/dist/zip/index.d.ts.map +1 -0
- package/dist/zip/index.js +78 -0
- package/dist/zip/index.js.map +1 -0
- package/dist/zip/inflate.d.ts +2 -0
- package/dist/zip/inflate.d.ts.map +1 -0
- package/dist/zip/inflate.js +43 -0
- package/dist/zip/inflate.js.map +1 -0
- package/dist/zip/source.d.ts +15 -0
- package/dist/zip/source.d.ts.map +1 -0
- package/dist/zip/source.js +34 -0
- package/dist/zip/source.js.map +1 -0
- package/package.json +75 -0
- package/src/core/book.ts +80 -0
- package/src/core/errors.ts +31 -0
- package/src/core/format.ts +14 -0
- package/src/core/graphemes.ts +73 -0
- package/src/core/index.ts +27 -0
- package/src/core/lookup.ts +16 -0
- package/src/core/open.ts +31 -0
- package/src/core/position.ts +485 -0
- package/src/core/reading-text.ts +200 -0
- package/src/core/source.ts +61 -0
- package/src/core/storage.ts +6 -0
- package/src/core/text.ts +35 -0
- package/src/formats/epub/href.ts +54 -0
- package/src/formats/epub/index.ts +235 -0
- package/src/formats/epub/opf.ts +143 -0
- package/src/formats/epub/toc.ts +120 -0
- package/src/formats/fb2/index.ts +348 -0
- package/src/formats/index.ts +3 -0
- package/src/formats/text/index.ts +208 -0
- package/src/formats/xml.ts +326 -0
- package/src/index.ts +26 -0
- package/src/layout/chunk.ts +169 -0
- package/src/layout/index.ts +573 -0
- package/src/reader/index.ts +1206 -0
- package/src/reader/input.ts +122 -0
- package/src/search/extract.ts +403 -0
- package/src/search/index.ts +10 -0
- package/src/search/matcher.ts +187 -0
- package/src/search/normalize.ts +122 -0
- package/src/view/allowlist.ts +273 -0
- package/src/view/appearance.ts +380 -0
- package/src/view/css.ts +172 -0
- package/src/view/frame.ts +1018 -0
- package/src/view/host.ts +527 -0
- package/src/view/index.ts +24 -0
- package/src/view/protocol.ts +412 -0
- package/src/view/reference.ts +30 -0
- package/src/view/resources.ts +265 -0
- package/src/view/sanitize.ts +266 -0
- package/src/zip/central.ts +85 -0
- package/src/zip/crc32.ts +30 -0
- package/src/zip/eocd.ts +97 -0
- package/src/zip/errors.ts +41 -0
- package/src/zip/index.ts +114 -0
- package/src/zip/inflate.ts +41 -0
- package/src/zip/source.ts +51 -0
|
@@ -0,0 +1,958 @@
|
|
|
1
|
+
import { parsePosition, segmentSentences } from '../core/index.js';
|
|
2
|
+
import { Paginator } from '../layout/index.js';
|
|
3
|
+
import { searchBook } from '../search/index.js';
|
|
4
|
+
import { isReflowingUpdate, mergeAppearance, themeStyleSheet, } from '../view/appearance.js';
|
|
5
|
+
import { keyIntent, swipeIntent, tapIntent, } from './input.js';
|
|
6
|
+
/** Something went wrong driving the reader; never a book decoding failure. */
|
|
7
|
+
export class ReaderError extends Error {
|
|
8
|
+
constructor(message, options) {
|
|
9
|
+
super(message, options);
|
|
10
|
+
this.name = 'ReaderError';
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Renders `book` into `element` and returns a live {@link Reader}. The reader is
|
|
15
|
+
* usable immediately; the first section paints asynchronously and a `ready` event
|
|
16
|
+
* fires once it does. Options are applied where they map today and otherwise
|
|
17
|
+
* retained for M3. Pass `options.start` to open somewhere other than the book's
|
|
18
|
+
* beginning.
|
|
19
|
+
*/
|
|
20
|
+
export function render(book, element, options = {}) {
|
|
21
|
+
return new ReaderImpl(book, element, options);
|
|
22
|
+
}
|
|
23
|
+
/** Split a raw href into its path (fragment removed) and optional fragment. */
|
|
24
|
+
function splitFragment(href) {
|
|
25
|
+
const hash = href.indexOf('#');
|
|
26
|
+
if (hash === -1)
|
|
27
|
+
return { path: href };
|
|
28
|
+
return { path: href.slice(0, hash), fragment: decodeFragment(href.slice(hash + 1)) };
|
|
29
|
+
}
|
|
30
|
+
function decodeFragment(fragment) {
|
|
31
|
+
if (!fragment.includes('%'))
|
|
32
|
+
return fragment;
|
|
33
|
+
try {
|
|
34
|
+
return decodeURIComponent(fragment);
|
|
35
|
+
}
|
|
36
|
+
catch {
|
|
37
|
+
return fragment;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/** The last path segment of a reference, e.g. '../text/c3.xhtml' → 'c3.xhtml'. */
|
|
41
|
+
function lastSegment(path) {
|
|
42
|
+
const clean = path.replace(/[?].*$/, '');
|
|
43
|
+
const slash = clean.lastIndexOf('/');
|
|
44
|
+
return slash === -1 ? clean : clean.slice(slash + 1);
|
|
45
|
+
}
|
|
46
|
+
/** Strip a single trailing file extension: 'c3.xhtml' → 'c3'. */
|
|
47
|
+
function stripExtension(name) {
|
|
48
|
+
const dot = name.lastIndexOf('.');
|
|
49
|
+
return dot <= 0 ? name : name.slice(0, dot);
|
|
50
|
+
}
|
|
51
|
+
class ReaderImpl {
|
|
52
|
+
#book;
|
|
53
|
+
#paginator;
|
|
54
|
+
#element;
|
|
55
|
+
#sections;
|
|
56
|
+
#options;
|
|
57
|
+
#listeners = {
|
|
58
|
+
ready: new Set(),
|
|
59
|
+
positionchange: new Set(),
|
|
60
|
+
sectionchange: new Set(),
|
|
61
|
+
linkclick: new Set(),
|
|
62
|
+
selection: new Set(),
|
|
63
|
+
error: new Set(),
|
|
64
|
+
};
|
|
65
|
+
/** Internal-link back-stack: positions to return to via {@link back}. */
|
|
66
|
+
#backStack = [];
|
|
67
|
+
#sectionIndex = 0;
|
|
68
|
+
#mode;
|
|
69
|
+
#destroyed = false;
|
|
70
|
+
#ready = false;
|
|
71
|
+
/** Live appearance state (theme half). Merged by {@link setAppearance}. */
|
|
72
|
+
#appearance;
|
|
73
|
+
/** Reading direction; drives the RTL flip in every input mode. */
|
|
74
|
+
#direction;
|
|
75
|
+
/** Resolved input config: which modes are on and the tap-zone geometry. */
|
|
76
|
+
#keyboard;
|
|
77
|
+
#swipe;
|
|
78
|
+
#tapZones;
|
|
79
|
+
/** Serializes navigation so overlapping calls settle in order. */
|
|
80
|
+
#queue = Promise.resolve();
|
|
81
|
+
/** The host-side image-zoom overlay, mounted lazily on the first image tap. */
|
|
82
|
+
#zoom = null;
|
|
83
|
+
/**
|
|
84
|
+
* Observes the container so a resize re-paginates: page geometry is captured at
|
|
85
|
+
* render time, so a container that later shrinks leaves the frame scrolling and
|
|
86
|
+
* one that grows leaves a gap. Null where `ResizeObserver` is absent.
|
|
87
|
+
*/
|
|
88
|
+
#resizeObserver = null;
|
|
89
|
+
/** Pending rAF handle coalescing a burst of resize notifications into one re-flow. */
|
|
90
|
+
#resizeFrame = null;
|
|
91
|
+
constructor(book, element, options) {
|
|
92
|
+
this.#book = book;
|
|
93
|
+
this.#element = element;
|
|
94
|
+
this.#sections = book.sections;
|
|
95
|
+
this.#options = options;
|
|
96
|
+
this.#mode = options.mode ?? 'paginated';
|
|
97
|
+
this.#direction = book.direction ?? 'ltr';
|
|
98
|
+
const input = options.input ?? {};
|
|
99
|
+
this.#keyboard = input.keyboard ?? true;
|
|
100
|
+
this.#swipe = input.swipe ?? true;
|
|
101
|
+
this.#tapZones = input.tapZones === false ? null : (input.tapZones ?? {});
|
|
102
|
+
// Seed the live appearance from the options. Only keys that were supplied are
|
|
103
|
+
// carried — `exactOptionalPropertyTypes` keeps "follow the OS" (theme absent)
|
|
104
|
+
// distinct from a forced theme, and an unset typography knob from a forced one.
|
|
105
|
+
// The typography half is applied at the first render (below), not merely
|
|
106
|
+
// retained: the seeded stylesheet rides the opening srcdoc and `columns` drives
|
|
107
|
+
// the opening paginate.
|
|
108
|
+
this.#appearance = {};
|
|
109
|
+
if (options.theme !== undefined)
|
|
110
|
+
this.#appearance = { ...this.#appearance, theme: options.theme };
|
|
111
|
+
if (options.customProperties !== undefined) {
|
|
112
|
+
this.#appearance = { ...this.#appearance, customProperties: options.customProperties };
|
|
113
|
+
}
|
|
114
|
+
if (options.fontFamily !== undefined)
|
|
115
|
+
this.#appearance = { ...this.#appearance, fontFamily: options.fontFamily };
|
|
116
|
+
if (options.fontSize !== undefined)
|
|
117
|
+
this.#appearance = { ...this.#appearance, fontSize: options.fontSize };
|
|
118
|
+
if (options.lineHeight !== undefined)
|
|
119
|
+
this.#appearance = { ...this.#appearance, lineHeight: options.lineHeight };
|
|
120
|
+
if (options.margin !== undefined)
|
|
121
|
+
this.#appearance = { ...this.#appearance, margin: options.margin };
|
|
122
|
+
if (options.textAlign !== undefined)
|
|
123
|
+
this.#appearance = { ...this.#appearance, textAlign: options.textAlign };
|
|
124
|
+
if (options.justify !== undefined)
|
|
125
|
+
this.#appearance = { ...this.#appearance, justify: options.justify };
|
|
126
|
+
if (options.hyphenate !== undefined)
|
|
127
|
+
this.#appearance = { ...this.#appearance, hyphenate: options.hyphenate };
|
|
128
|
+
if (options.columns !== undefined)
|
|
129
|
+
this.#appearance = { ...this.#appearance, columns: options.columns };
|
|
130
|
+
this.#paginator = new Paginator(element, {
|
|
131
|
+
themeCss: themeStyleSheet(this.#appearance),
|
|
132
|
+
// Baked into the frame's coordination script: with keyboard input off the
|
|
133
|
+
// frame stops preventDefault'ing nav keys it would forward to a deaf host.
|
|
134
|
+
keyboardNav: this.#keyboard,
|
|
135
|
+
onLinkClick: (href) => {
|
|
136
|
+
void this.#onLinkClick(href);
|
|
137
|
+
},
|
|
138
|
+
onError: (message) => {
|
|
139
|
+
this.#emit('error', new ReaderError(message));
|
|
140
|
+
},
|
|
141
|
+
onKey: (key) => {
|
|
142
|
+
if (this.#keyboard)
|
|
143
|
+
this.#dispatchIntent(keyIntent(key, this.#direction));
|
|
144
|
+
},
|
|
145
|
+
onSwipe: (dx, dy) => {
|
|
146
|
+
if (this.#swipe)
|
|
147
|
+
this.#dispatchIntent(swipeIntent(dx, dy, this.#direction));
|
|
148
|
+
},
|
|
149
|
+
onTap: (tap) => {
|
|
150
|
+
if (this.#tapZones !== null) {
|
|
151
|
+
this.#dispatchIntent(tapIntent(tap, this.#tapZones, this.#direction));
|
|
152
|
+
}
|
|
153
|
+
},
|
|
154
|
+
onImageTap: (image) => {
|
|
155
|
+
this.#openZoom(image);
|
|
156
|
+
},
|
|
157
|
+
onSelection: (selection) => {
|
|
158
|
+
void this.#onSelection(selection);
|
|
159
|
+
},
|
|
160
|
+
});
|
|
161
|
+
// Kick off the initial render; `ready` fires when it settles.
|
|
162
|
+
void this.#run(() => this.#open());
|
|
163
|
+
this.#observeResize();
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Watches the container for size changes and re-paginates on each, so the frame
|
|
167
|
+
* never scrolls (shrunk container) or shows a gap (grown one). Notifications are
|
|
168
|
+
* coalesced onto the next animation frame, then the re-flow is enqueued behind any
|
|
169
|
+
* in-flight navigation so it settles in order and holds the reading place. The
|
|
170
|
+
* `ResizeObserver` fires once on `observe()` at the current size — a no-op, since
|
|
171
|
+
* `Paginator.resize()` short-circuits when the size has not actually changed.
|
|
172
|
+
*/
|
|
173
|
+
#observeResize() {
|
|
174
|
+
if (typeof ResizeObserver === 'undefined')
|
|
175
|
+
return;
|
|
176
|
+
this.#resizeObserver = new ResizeObserver(() => this.#onResize());
|
|
177
|
+
this.#resizeObserver.observe(this.#element);
|
|
178
|
+
}
|
|
179
|
+
#onResize() {
|
|
180
|
+
if (this.#destroyed || this.#resizeFrame !== null)
|
|
181
|
+
return;
|
|
182
|
+
this.#resizeFrame = requestAnimationFrame(() => {
|
|
183
|
+
this.#resizeFrame = null;
|
|
184
|
+
if (this.#destroyed || this.#paginator.section === null)
|
|
185
|
+
return;
|
|
186
|
+
void this.#run(() => this.#reflowToContainer());
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
async #reflowToContainer() {
|
|
190
|
+
if (this.#destroyed || this.#paginator.section === null)
|
|
191
|
+
return;
|
|
192
|
+
const state = await this.#paginator.resize();
|
|
193
|
+
// `null` means the size did not actually change (or the container is hidden):
|
|
194
|
+
// nothing re-laid out, so nothing to announce.
|
|
195
|
+
if (this.#destroyed || state === null)
|
|
196
|
+
return;
|
|
197
|
+
this.#emit('positionchange', this.#snapshot());
|
|
198
|
+
}
|
|
199
|
+
get position() {
|
|
200
|
+
return this.#snapshot();
|
|
201
|
+
}
|
|
202
|
+
get mode() {
|
|
203
|
+
return this.#mode;
|
|
204
|
+
}
|
|
205
|
+
on(event, handler) {
|
|
206
|
+
const set = this.#listeners[event];
|
|
207
|
+
set.add(handler);
|
|
208
|
+
return () => {
|
|
209
|
+
set.delete(handler);
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
next() {
|
|
213
|
+
return this.#run(() => this.#next());
|
|
214
|
+
}
|
|
215
|
+
prev() {
|
|
216
|
+
return this.#run(() => this.#prev());
|
|
217
|
+
}
|
|
218
|
+
nextSection() {
|
|
219
|
+
return this.#run(() => this.#gotoSection(this.#sectionIndex + 1, 'first'));
|
|
220
|
+
}
|
|
221
|
+
prevSection() {
|
|
222
|
+
return this.#run(() => this.#gotoSection(this.#sectionIndex - 1, 'first'));
|
|
223
|
+
}
|
|
224
|
+
goTo(target) {
|
|
225
|
+
return this.#run(() => this.#goTo(target));
|
|
226
|
+
}
|
|
227
|
+
back() {
|
|
228
|
+
return this.#run(() => this.#back());
|
|
229
|
+
}
|
|
230
|
+
setMode(mode) {
|
|
231
|
+
return this.#run(() => this.#setMode(mode));
|
|
232
|
+
}
|
|
233
|
+
setAppearance(appearance) {
|
|
234
|
+
return this.#run(() => this.#setAppearance(appearance));
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* Streams whole-book search hits. Not enqueued behind navigation — it reads the
|
|
238
|
+
* book's section bytes headlessly (decode → strip markup → match) and never drives
|
|
239
|
+
* the paginator or the frame, so it composes with reading rather than blocking it.
|
|
240
|
+
* Delegates to the headless `searchBook` generator; `goTo(hit.position)` jumps.
|
|
241
|
+
*/
|
|
242
|
+
search(query, options = {}) {
|
|
243
|
+
return searchBook(this.#book, query, options);
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Draws (or replaces) a decoration over the range `position` resolves to. Enqueued
|
|
247
|
+
* behind navigation so it settles in issue order against a concurrent re-layout;
|
|
248
|
+
* the paginator holds the intent and re-anchors it across every later re-layout, so
|
|
249
|
+
* a highlight survives a font-size or mode change. Draw-only — no annotation data is
|
|
250
|
+
* ever stored, serialized, or fetched. A soft-miss draws nothing (no throw).
|
|
251
|
+
*/
|
|
252
|
+
decorate(id, position, opts) {
|
|
253
|
+
return this.#run(() => this.#decorate(id, position, opts.className));
|
|
254
|
+
}
|
|
255
|
+
/** Removes the decoration for `id`. Enqueued so it orders against navigation. */
|
|
256
|
+
undecorate(id) {
|
|
257
|
+
return this.#run(() => this.#undecorate(id));
|
|
258
|
+
}
|
|
259
|
+
sentences() {
|
|
260
|
+
return this.#runResult([], async () => {
|
|
261
|
+
const section = this.#paginator.section;
|
|
262
|
+
if (section === null)
|
|
263
|
+
return [];
|
|
264
|
+
// Segment the same frame-measured section text decorations resolve against, so
|
|
265
|
+
// every sentence Position both jumps (goTo) and highlights (decorate) cleanly.
|
|
266
|
+
const text = await this.#paginator.sectionText();
|
|
267
|
+
return segmentSentences(text, section.id);
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
async #decorate(id, position, className) {
|
|
271
|
+
if (this.#destroyed || this.#paginator.section === null)
|
|
272
|
+
return;
|
|
273
|
+
await this.#paginator.decorate(id, position, className);
|
|
274
|
+
}
|
|
275
|
+
async #undecorate(id) {
|
|
276
|
+
if (this.#destroyed)
|
|
277
|
+
return;
|
|
278
|
+
await this.#paginator.undecorate(id);
|
|
279
|
+
}
|
|
280
|
+
destroy() {
|
|
281
|
+
if (this.#destroyed)
|
|
282
|
+
return;
|
|
283
|
+
this.#destroyed = true;
|
|
284
|
+
this.#resizeObserver?.disconnect();
|
|
285
|
+
this.#resizeObserver = null;
|
|
286
|
+
if (this.#resizeFrame !== null) {
|
|
287
|
+
cancelAnimationFrame(this.#resizeFrame);
|
|
288
|
+
this.#resizeFrame = null;
|
|
289
|
+
}
|
|
290
|
+
this.#zoom?.destroy();
|
|
291
|
+
this.#zoom = null;
|
|
292
|
+
this.#paginator.destroy();
|
|
293
|
+
for (const event of Object.keys(this.#listeners)) {
|
|
294
|
+
this.#listeners[event].clear();
|
|
295
|
+
}
|
|
296
|
+
this.#backStack = [];
|
|
297
|
+
}
|
|
298
|
+
// --- Input dispatch -------------------------------------------------------
|
|
299
|
+
/**
|
|
300
|
+
* Route a resolved {@link NavIntent} (from keyboard/swipe/tap, already made
|
|
301
|
+
* direction-aware in `input.ts`) to the matching public navigation method. Each
|
|
302
|
+
* goes through the same serialized queue as programmatic navigation, so hand
|
|
303
|
+
* input interleaves in issue order. `none` is a no-op. Input is never gated on
|
|
304
|
+
* `prefers-reduced-motion`: pages must always turn. The only motion in the reader
|
|
305
|
+
* is the zoom overlay's open/close transition, which the overlay gates itself.
|
|
306
|
+
*/
|
|
307
|
+
#dispatchIntent(intent) {
|
|
308
|
+
switch (intent) {
|
|
309
|
+
case 'next':
|
|
310
|
+
void this.next();
|
|
311
|
+
return;
|
|
312
|
+
case 'prev':
|
|
313
|
+
void this.prev();
|
|
314
|
+
return;
|
|
315
|
+
case 'nextSection':
|
|
316
|
+
void this.nextSection();
|
|
317
|
+
return;
|
|
318
|
+
case 'prevSection':
|
|
319
|
+
void this.prevSection();
|
|
320
|
+
return;
|
|
321
|
+
case 'start':
|
|
322
|
+
void this.goTo('start');
|
|
323
|
+
return;
|
|
324
|
+
case 'end':
|
|
325
|
+
void this.goTo('end');
|
|
326
|
+
return;
|
|
327
|
+
case 'none':
|
|
328
|
+
return;
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
// --- Image zoom overlay ---------------------------------------------------
|
|
332
|
+
/**
|
|
333
|
+
* Opens the host-side zoom overlay over the tapped image's already-served
|
|
334
|
+
* `data:` bytes. The overlay is a DOM element in the *host* document, outside the
|
|
335
|
+
* sandboxed frame — it mints no new frame resource and never a `blob:` URL. A
|
|
336
|
+
* non-`data:` src (a remote image the CSP already refuses in-frame) is ignored.
|
|
337
|
+
*/
|
|
338
|
+
#openZoom(image) {
|
|
339
|
+
if (this.#destroyed || !image.src.startsWith('data:'))
|
|
340
|
+
return;
|
|
341
|
+
if (this.#zoom === null)
|
|
342
|
+
this.#zoom = new ImageZoom(this.#element);
|
|
343
|
+
this.#zoom.open(image.src, image.alt);
|
|
344
|
+
}
|
|
345
|
+
// --- Navigation queue -----------------------------------------------------
|
|
346
|
+
/**
|
|
347
|
+
* Runs `task` after all previously-queued navigation, so overlapping calls
|
|
348
|
+
* settle in the order they were issued. A task failure is reported through the
|
|
349
|
+
* `error` event and swallowed so it never breaks the chain; `destroy` short-
|
|
350
|
+
* circuits everything.
|
|
351
|
+
*/
|
|
352
|
+
#run(task) {
|
|
353
|
+
const next = this.#queue.then(async () => {
|
|
354
|
+
if (this.#destroyed)
|
|
355
|
+
return;
|
|
356
|
+
try {
|
|
357
|
+
await task();
|
|
358
|
+
}
|
|
359
|
+
catch (error) {
|
|
360
|
+
if (!this.#destroyed) {
|
|
361
|
+
this.#emit('error', error instanceof Error ? error : new ReaderError(String(error)));
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
});
|
|
365
|
+
this.#queue = next;
|
|
366
|
+
return next;
|
|
367
|
+
}
|
|
368
|
+
/**
|
|
369
|
+
* Like {@link #run} but returns the task's value, resolving to `fallback` when the
|
|
370
|
+
* reader is destroyed or the task throws (the error is reported through `error`).
|
|
371
|
+
* For read-only queries that must settle in order with navigation.
|
|
372
|
+
*/
|
|
373
|
+
#runResult(fallback, task) {
|
|
374
|
+
const run = this.#queue.then(async () => {
|
|
375
|
+
if (this.#destroyed)
|
|
376
|
+
return fallback;
|
|
377
|
+
try {
|
|
378
|
+
return await task();
|
|
379
|
+
}
|
|
380
|
+
catch (error) {
|
|
381
|
+
if (!this.#destroyed) {
|
|
382
|
+
this.#emit('error', error instanceof Error ? error : new ReaderError(String(error)));
|
|
383
|
+
}
|
|
384
|
+
return fallback;
|
|
385
|
+
}
|
|
386
|
+
});
|
|
387
|
+
this.#queue = run.then(() => undefined, () => undefined);
|
|
388
|
+
return run;
|
|
389
|
+
}
|
|
390
|
+
// --- Lifecycle ------------------------------------------------------------
|
|
391
|
+
async #open() {
|
|
392
|
+
const start = this.#options.start;
|
|
393
|
+
if (start === undefined) {
|
|
394
|
+
await this.#gotoSection(0, 'first');
|
|
395
|
+
}
|
|
396
|
+
else {
|
|
397
|
+
await this.#goTo(start);
|
|
398
|
+
}
|
|
399
|
+
if (this.#destroyed)
|
|
400
|
+
return;
|
|
401
|
+
this.#ready = true;
|
|
402
|
+
this.#emit('ready', this.#snapshot());
|
|
403
|
+
}
|
|
404
|
+
// --- Section / page paint -------------------------------------------------
|
|
405
|
+
/**
|
|
406
|
+
* Paginates the section at `index` and lands on its first or last page. Fires
|
|
407
|
+
* `sectionchange` when the active section actually changes, then always fires
|
|
408
|
+
* `positionchange` once the page settles. Out-of-range indices are no-ops.
|
|
409
|
+
*/
|
|
410
|
+
async #gotoSection(index, land) {
|
|
411
|
+
if (index < 0 || index >= this.#sections.length)
|
|
412
|
+
return;
|
|
413
|
+
const section = this.#sections[index];
|
|
414
|
+
const changed = this.#paginator.section === null || this.#sectionIndex !== index;
|
|
415
|
+
// Re-paginating is the expensive step (sanitize + chunk + measure). Skip it
|
|
416
|
+
// when we are already on this section — back(), a same-section fragment jump,
|
|
417
|
+
// or a Position within the current chapter — and just seek within the layout
|
|
418
|
+
// already in place.
|
|
419
|
+
if (changed) {
|
|
420
|
+
await this.#paginator.paginate(section, { mode: this.#mode, ...this.#requestExtras() });
|
|
421
|
+
this.#sectionIndex = index;
|
|
422
|
+
}
|
|
423
|
+
// Always establish the landing page explicitly. A multi-chunk section paints
|
|
424
|
+
// its chunks stacked until a page is selected, so entering on 'first' must
|
|
425
|
+
// seek page 0, not assume paginate left it there.
|
|
426
|
+
if (land === 'last') {
|
|
427
|
+
await this.#paginator.refine();
|
|
428
|
+
const state = this.#paginator.state;
|
|
429
|
+
await this.#paginator.goToPage(state === null ? 0 : Math.max(0, state.pageCount - 1));
|
|
430
|
+
}
|
|
431
|
+
else {
|
|
432
|
+
await this.#paginator.goToPage(0);
|
|
433
|
+
}
|
|
434
|
+
if (changed)
|
|
435
|
+
this.#emit('sectionchange', { index, sectionId: section.id });
|
|
436
|
+
this.#emit('positionchange', this.#snapshot());
|
|
437
|
+
}
|
|
438
|
+
/**
|
|
439
|
+
* The layout knobs the facade threads into every `paginate`: the page margin
|
|
440
|
+
* (`columnGap`) and the column count. Both are appearance-owned, so a section
|
|
441
|
+
* re-paginated after a mode switch or roll keeps the live typography rather than
|
|
442
|
+
* snapping back to the paginator defaults.
|
|
443
|
+
*/
|
|
444
|
+
#requestExtras() {
|
|
445
|
+
const extras = {};
|
|
446
|
+
if (this.#appearance.margin !== undefined)
|
|
447
|
+
extras.columnGap = this.#appearance.margin;
|
|
448
|
+
if (this.#appearance.columns !== undefined)
|
|
449
|
+
extras.columnCount = this.#appearance.columns;
|
|
450
|
+
return extras;
|
|
451
|
+
}
|
|
452
|
+
// --- next / prev ----------------------------------------------------------
|
|
453
|
+
async #next() {
|
|
454
|
+
if (this.#paginator.section === null)
|
|
455
|
+
return;
|
|
456
|
+
const before = this.#paginator.page;
|
|
457
|
+
const after = await this.#paginator.nextPage();
|
|
458
|
+
if (after !== before) {
|
|
459
|
+
this.#emit('positionchange', this.#snapshot());
|
|
460
|
+
return;
|
|
461
|
+
}
|
|
462
|
+
// At the section's last page: roll to the next section's first page.
|
|
463
|
+
await this.#gotoSection(this.#sectionIndex + 1, 'first');
|
|
464
|
+
}
|
|
465
|
+
async #prev() {
|
|
466
|
+
if (this.#paginator.section === null)
|
|
467
|
+
return;
|
|
468
|
+
const before = this.#paginator.page;
|
|
469
|
+
const after = await this.#paginator.previousPage();
|
|
470
|
+
if (after !== before) {
|
|
471
|
+
this.#emit('positionchange', this.#snapshot());
|
|
472
|
+
return;
|
|
473
|
+
}
|
|
474
|
+
// At the section's first page: roll to the previous section's last page.
|
|
475
|
+
await this.#gotoSection(this.#sectionIndex - 1, 'last');
|
|
476
|
+
}
|
|
477
|
+
// --- goTo -----------------------------------------------------------------
|
|
478
|
+
async #goTo(target) {
|
|
479
|
+
if (target === 'start') {
|
|
480
|
+
await this.#gotoSection(0, 'first');
|
|
481
|
+
return;
|
|
482
|
+
}
|
|
483
|
+
if (target === 'end') {
|
|
484
|
+
await this.#gotoSection(this.#sections.length - 1, 'last');
|
|
485
|
+
return;
|
|
486
|
+
}
|
|
487
|
+
if (typeof target === 'number') {
|
|
488
|
+
await this.#gotoFraction(target);
|
|
489
|
+
return;
|
|
490
|
+
}
|
|
491
|
+
if (typeof target === 'string') {
|
|
492
|
+
await this.#gotoHref(target);
|
|
493
|
+
return;
|
|
494
|
+
}
|
|
495
|
+
if (isPosition(target)) {
|
|
496
|
+
await this.#gotoPosition(target);
|
|
497
|
+
return;
|
|
498
|
+
}
|
|
499
|
+
// TocItem.
|
|
500
|
+
await this.#gotoTocItem(target);
|
|
501
|
+
}
|
|
502
|
+
async #gotoFraction(fraction) {
|
|
503
|
+
const count = this.#sections.length;
|
|
504
|
+
if (count === 0)
|
|
505
|
+
return;
|
|
506
|
+
const clamped = Math.min(1, Math.max(0, fraction));
|
|
507
|
+
// Even-weight the book across sections (matching BookProgress.bookFraction),
|
|
508
|
+
// then land on the page nearest the residual fraction within that section.
|
|
509
|
+
const scaled = clamped * count;
|
|
510
|
+
const index = Math.min(count - 1, Math.floor(scaled));
|
|
511
|
+
const within = scaled - index;
|
|
512
|
+
await this.#gotoSection(index, 'first');
|
|
513
|
+
if (this.#destroyed)
|
|
514
|
+
return;
|
|
515
|
+
await this.#paginator.refine();
|
|
516
|
+
const state = this.#paginator.state;
|
|
517
|
+
if (state !== null && state.pageCount > 1) {
|
|
518
|
+
const page = Math.round(within * (state.pageCount - 1));
|
|
519
|
+
await this.#paginator.goToPage(page);
|
|
520
|
+
}
|
|
521
|
+
this.#emit('positionchange', this.#snapshot());
|
|
522
|
+
}
|
|
523
|
+
async #gotoPosition(position) {
|
|
524
|
+
const index = this.#indexOfSection(position.sectionId);
|
|
525
|
+
if (index === -1)
|
|
526
|
+
return; // soft miss: unknown section
|
|
527
|
+
await this.#gotoSection(index, 'first');
|
|
528
|
+
if (this.#destroyed)
|
|
529
|
+
return;
|
|
530
|
+
const page = await this.#paginator.pageOfPosition(position);
|
|
531
|
+
if (page >= 0)
|
|
532
|
+
await this.#paginator.goToPage(page);
|
|
533
|
+
this.#emit('positionchange', this.#snapshot());
|
|
534
|
+
}
|
|
535
|
+
async #gotoTocItem(item) {
|
|
536
|
+
const index = this.#indexOfSection(item.sectionId);
|
|
537
|
+
if (index === -1)
|
|
538
|
+
return; // soft miss
|
|
539
|
+
await this.#gotoSection(index, 'first');
|
|
540
|
+
if (this.#destroyed)
|
|
541
|
+
return;
|
|
542
|
+
if (item.fragment !== undefined)
|
|
543
|
+
await this.#seekFragment(item.fragment);
|
|
544
|
+
this.#emit('positionchange', this.#snapshot());
|
|
545
|
+
}
|
|
546
|
+
/**
|
|
547
|
+
* Resolves a content-internal href to a section (+ fragment) using ONLY public
|
|
548
|
+
* Book data, then navigates. The core exposes no href→section seam (sections are
|
|
549
|
+
* id-addressed; `Section.resolve` yields a `Resource`, not a section target), so
|
|
550
|
+
* this is a best-effort, format-neutral heuristic that degrades quietly on a
|
|
551
|
+
* miss — consistent with the Position soft-miss philosophy. In order:
|
|
552
|
+
* 1. the path (or its last segment) equals a `section.id`;
|
|
553
|
+
* 2. the last segment, or that segment minus its extension, matches a
|
|
554
|
+
* `section.id` or a section id's own last segment / extension-less form;
|
|
555
|
+
* 3. the href carries a fragment that a `TocItem` also carries — borrow that
|
|
556
|
+
* item's `sectionId`.
|
|
557
|
+
* A pure-fragment href (`#note`) seeks within the current section.
|
|
558
|
+
*
|
|
559
|
+
* Returns whether the href resolved and navigation happened — `false` on a
|
|
560
|
+
* soft miss — so the link-click path can decide whether a back-stack entry is
|
|
561
|
+
* warranted. `goTo` ignores the value.
|
|
562
|
+
*/
|
|
563
|
+
async #gotoHref(href) {
|
|
564
|
+
const { path, fragment } = splitFragment(href);
|
|
565
|
+
if (path === '') {
|
|
566
|
+
// Pure fragment: within the current section.
|
|
567
|
+
if (fragment !== undefined)
|
|
568
|
+
await this.#seekFragment(fragment);
|
|
569
|
+
this.#emit('positionchange', this.#snapshot());
|
|
570
|
+
return fragment !== undefined;
|
|
571
|
+
}
|
|
572
|
+
// Prefer the format's own section-relative resolver: it maps the raw href to a
|
|
573
|
+
// section by the same authority the TOC uses (EPUB's resolved-path → id table),
|
|
574
|
+
// which the id/filename heuristic below cannot match against opaque manifest ids.
|
|
575
|
+
// Fall back to the heuristic for formats that expose no resolver.
|
|
576
|
+
const viaFormat = this.#paginator.section?.resolveHref?.(href);
|
|
577
|
+
let index = viaFormat === undefined ? -1 : this.#indexOfSection(viaFormat.sectionId);
|
|
578
|
+
const targetFragment = (viaFormat?.fragment ?? fragment);
|
|
579
|
+
if (index === -1)
|
|
580
|
+
index = this.#resolveHrefToSection(path, fragment);
|
|
581
|
+
if (index === -1)
|
|
582
|
+
return false; // soft miss
|
|
583
|
+
await this.#gotoSection(index, 'first');
|
|
584
|
+
if (this.#destroyed)
|
|
585
|
+
return false;
|
|
586
|
+
if (targetFragment !== undefined)
|
|
587
|
+
await this.#seekFragment(targetFragment);
|
|
588
|
+
this.#emit('positionchange', this.#snapshot());
|
|
589
|
+
return true;
|
|
590
|
+
}
|
|
591
|
+
/** Heuristic href-path → section index; -1 when nothing matches (soft miss). */
|
|
592
|
+
#resolveHrefToSection(path, fragment) {
|
|
593
|
+
// 1. Direct id match on the full path or its last segment.
|
|
594
|
+
const segment = lastSegment(path);
|
|
595
|
+
for (const candidate of [path, segment]) {
|
|
596
|
+
const direct = this.#indexOfSection(candidate);
|
|
597
|
+
if (direct !== -1)
|
|
598
|
+
return direct;
|
|
599
|
+
}
|
|
600
|
+
// 2. Filename / extension-less match against section ids and their tails.
|
|
601
|
+
const stem = stripExtension(segment);
|
|
602
|
+
for (let index = 0; index < this.#sections.length; index += 1) {
|
|
603
|
+
const id = this.#sections[index].id;
|
|
604
|
+
const idSegment = lastSegment(id);
|
|
605
|
+
if (id === segment ||
|
|
606
|
+
id === stem ||
|
|
607
|
+
idSegment === segment ||
|
|
608
|
+
idSegment === stem ||
|
|
609
|
+
stripExtension(idSegment) === stem) {
|
|
610
|
+
return index;
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
// 3. TOC fragment fallback: a toc item carrying this fragment names a section.
|
|
614
|
+
if (fragment !== undefined) {
|
|
615
|
+
const byFragment = this.#findTocByFragment(this.#book.toc, fragment);
|
|
616
|
+
if (byFragment !== undefined)
|
|
617
|
+
return this.#indexOfSection(byFragment);
|
|
618
|
+
}
|
|
619
|
+
return -1;
|
|
620
|
+
}
|
|
621
|
+
#findTocByFragment(items, fragment) {
|
|
622
|
+
for (const item of items) {
|
|
623
|
+
if (item.fragment === fragment)
|
|
624
|
+
return item.sectionId;
|
|
625
|
+
const nested = this.#findTocByFragment(item.children, fragment);
|
|
626
|
+
if (nested !== undefined)
|
|
627
|
+
return nested;
|
|
628
|
+
}
|
|
629
|
+
return undefined;
|
|
630
|
+
}
|
|
631
|
+
/**
|
|
632
|
+
* Best-effort seek to a fragment within the already-paginated section. The frame
|
|
633
|
+
* maps an element id to its character offset (`offsetOfElementId`), which the
|
|
634
|
+
* paginator turns into a page via `pageOfElementId`. A resolved id lands on that
|
|
635
|
+
* page; an unresolvable id (no such element) is a soft miss that lands on the
|
|
636
|
+
* section's first page — never a throw. Kept as a seam so callers need not
|
|
637
|
+
* special-case fragment presence.
|
|
638
|
+
*/
|
|
639
|
+
async #seekFragment(fragment) {
|
|
640
|
+
const page = await this.#paginator.pageOfElementId(fragment);
|
|
641
|
+
// Soft miss (-1): the id is absent, so stay at the section's first page.
|
|
642
|
+
await this.#paginator.goToPage(page >= 0 ? page : 0);
|
|
643
|
+
}
|
|
644
|
+
// --- selection ------------------------------------------------------------
|
|
645
|
+
/**
|
|
646
|
+
* Turns a frame-forwarded selection (UTF-16 offset range + text over the section
|
|
647
|
+
* text) into a `Position` anchored at the range start and emits `selection`. The
|
|
648
|
+
* frame already dropped collapsed and empty-rect selections, so a callback here
|
|
649
|
+
* always carries real text. Position capture reads the section text but moves
|
|
650
|
+
* nothing, so it is not enqueued behind navigation.
|
|
651
|
+
*/
|
|
652
|
+
async #onSelection(selection) {
|
|
653
|
+
if (this.#destroyed || this.#paginator.section === null)
|
|
654
|
+
return;
|
|
655
|
+
const position = await this.#paginator.positionOfOffsetRange(selection.start, selection.end);
|
|
656
|
+
if (this.#destroyed)
|
|
657
|
+
return;
|
|
658
|
+
this.#emit('selection', { text: selection.text, position });
|
|
659
|
+
}
|
|
660
|
+
// --- back-stack -----------------------------------------------------------
|
|
661
|
+
async #onLinkClick(href) {
|
|
662
|
+
if (this.#destroyed)
|
|
663
|
+
return;
|
|
664
|
+
this.#emit('linkclick', { href });
|
|
665
|
+
// Capture the pre-jump position, follow the link, and push only if the href
|
|
666
|
+
// actually resolved and navigated. An external (https:) or otherwise
|
|
667
|
+
// unresolvable href is a soft miss that moves nothing — pushing it would
|
|
668
|
+
// make the next back() a no-op "return" to where the user already is
|
|
669
|
+
// instead of the genuinely prior position. Queued so it settles in order
|
|
670
|
+
// with any concurrent navigation.
|
|
671
|
+
await this.#run(async () => {
|
|
672
|
+
const here = await this.#currentPosition();
|
|
673
|
+
const navigated = await this.#gotoHref(href);
|
|
674
|
+
if (navigated && here !== null)
|
|
675
|
+
this.#backStack.push(here);
|
|
676
|
+
});
|
|
677
|
+
}
|
|
678
|
+
async #back() {
|
|
679
|
+
const position = this.#backStack.pop();
|
|
680
|
+
if (position === undefined)
|
|
681
|
+
return;
|
|
682
|
+
await this.#gotoPosition(position);
|
|
683
|
+
}
|
|
684
|
+
// --- mode switch ----------------------------------------------------------
|
|
685
|
+
async #setMode(mode) {
|
|
686
|
+
if (mode === this.#mode)
|
|
687
|
+
return;
|
|
688
|
+
if (this.#paginator.section === null) {
|
|
689
|
+
this.#mode = mode;
|
|
690
|
+
return;
|
|
691
|
+
}
|
|
692
|
+
// The paginator itself captures a Position, re-paginates, and seeks back,
|
|
693
|
+
// so position is preserved across the switch (M1-2 machinery).
|
|
694
|
+
await this.#paginator.switchMode(mode);
|
|
695
|
+
this.#mode = mode;
|
|
696
|
+
this.#emit('positionchange', this.#snapshot());
|
|
697
|
+
}
|
|
698
|
+
// --- appearance -----------------------------------------------------------
|
|
699
|
+
/**
|
|
700
|
+
* Merges `appearance` over the live state, rebuilds the appearance stylesheet,
|
|
701
|
+
* and applies it to the current section preserving the reading place. Two paths,
|
|
702
|
+
* chosen by whether the *update* touches a reflowing typography knob:
|
|
703
|
+
*
|
|
704
|
+
* - **Non-reflowing** (theme colours / background only): `setThemeCss` re-assembles
|
|
705
|
+
* the srcdoc, captures a `Position`, and seeks back. Geometry is invariant, so
|
|
706
|
+
* the exact page is restored and the text does not reflow.
|
|
707
|
+
* - **Reflowing** (font family/size, line height, alignment, hyphenation, columns):
|
|
708
|
+
* `applyAppearance` reuses the same capture → re-layout → resolve → restore
|
|
709
|
+
* machinery as `setMode`, but geometry changes, so the anchor's *nearest* page is
|
|
710
|
+
* restored (the reading place survives the reflow, page number may shift).
|
|
711
|
+
*
|
|
712
|
+
* A no-op change short-circuits inside the paginator. Before the first paint it
|
|
713
|
+
* records the appearance for the opening render.
|
|
714
|
+
*/
|
|
715
|
+
async #setAppearance(appearance) {
|
|
716
|
+
const reflows = isReflowingUpdate(appearance);
|
|
717
|
+
this.#appearance = mergeAppearance(this.#appearance, appearance);
|
|
718
|
+
const css = themeStyleSheet(this.#appearance);
|
|
719
|
+
if (reflows) {
|
|
720
|
+
const geometry = {};
|
|
721
|
+
if (this.#appearance.columns !== undefined)
|
|
722
|
+
geometry.columnCount = this.#appearance.columns;
|
|
723
|
+
if (this.#appearance.margin !== undefined)
|
|
724
|
+
geometry.columnGap = this.#appearance.margin;
|
|
725
|
+
await this.#paginator.applyAppearance(css, geometry);
|
|
726
|
+
}
|
|
727
|
+
else {
|
|
728
|
+
await this.#paginator.setThemeCss(css);
|
|
729
|
+
}
|
|
730
|
+
if (this.#destroyed)
|
|
731
|
+
return;
|
|
732
|
+
// Emitted after the change settles so a host can refresh anything keyed on the
|
|
733
|
+
// settled state (parity with setMode). A reflowing knob may have moved the page
|
|
734
|
+
// number even though the reading place is held.
|
|
735
|
+
this.#emit('positionchange', this.#snapshot());
|
|
736
|
+
}
|
|
737
|
+
// --- helpers --------------------------------------------------------------
|
|
738
|
+
#indexOfSection(sectionId) {
|
|
739
|
+
return this.#sections.findIndex((section) => section.id === sectionId);
|
|
740
|
+
}
|
|
741
|
+
/** The current reading position as a `Position`, or null before the first paint. */
|
|
742
|
+
async #currentPosition() {
|
|
743
|
+
if (this.#paginator.section === null)
|
|
744
|
+
return null;
|
|
745
|
+
return this.#paginator.positionOfPage(this.#paginator.page);
|
|
746
|
+
}
|
|
747
|
+
#snapshot() {
|
|
748
|
+
if (this.#paginator.section === null || this.#paginator.state === null) {
|
|
749
|
+
return { section: this.#sectionIndex, progress: 0, chapterProgress: 0, page: 0, totalPages: 0 };
|
|
750
|
+
}
|
|
751
|
+
const progress = this.#paginator.bookProgress(this.#sectionIndex, this.#sections.length);
|
|
752
|
+
return {
|
|
753
|
+
section: this.#sectionIndex,
|
|
754
|
+
progress: progress.bookFraction,
|
|
755
|
+
chapterProgress: progress.fraction,
|
|
756
|
+
page: progress.page,
|
|
757
|
+
totalPages: progress.totalPages,
|
|
758
|
+
};
|
|
759
|
+
}
|
|
760
|
+
#emit(event, payload) {
|
|
761
|
+
// Copy so a handler unsubscribing mid-dispatch cannot skip a sibling.
|
|
762
|
+
for (const handler of [...this.#listeners[event]]) {
|
|
763
|
+
try {
|
|
764
|
+
handler(payload);
|
|
765
|
+
}
|
|
766
|
+
catch {
|
|
767
|
+
// A listener throwing must not derail the emit or the navigation.
|
|
768
|
+
}
|
|
769
|
+
}
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
/**
|
|
773
|
+
* The host-side image-zoom overlay. It lives entirely in the *host* document,
|
|
774
|
+
* outside the sandboxed frame: it renders the image's already-served `data:` bytes
|
|
775
|
+
* in a host DOM element, so it mints no new frame resource and — crucially — never
|
|
776
|
+
* a `blob:` URL (an opaque-origin frame cannot load a host blob URL; `data:` is the
|
|
777
|
+
* boundary-crossing form the whole view stack already relies on).
|
|
778
|
+
*
|
|
779
|
+
* Accessibility: it is a modal dialog with a focus TRAP (Tab cycles the close
|
|
780
|
+
* control and image), closes on Escape or a backdrop click, and restores focus to
|
|
781
|
+
* whatever the reader element held before it opened. Its open/close transition —
|
|
782
|
+
* the reader's only animation — is gated on `prefers-reduced-motion`; input and
|
|
783
|
+
* page-turning are never gated on the preference.
|
|
784
|
+
*/
|
|
785
|
+
class ImageZoom {
|
|
786
|
+
#anchor;
|
|
787
|
+
#doc;
|
|
788
|
+
#root = null;
|
|
789
|
+
#image = null;
|
|
790
|
+
#closeButton = null;
|
|
791
|
+
#previouslyFocused = null;
|
|
792
|
+
#onKeyDown = (event) => {
|
|
793
|
+
if (event.key === 'Escape') {
|
|
794
|
+
event.preventDefault();
|
|
795
|
+
this.close();
|
|
796
|
+
return;
|
|
797
|
+
}
|
|
798
|
+
if (event.key === 'Tab')
|
|
799
|
+
this.#trapTab(event);
|
|
800
|
+
};
|
|
801
|
+
constructor(anchor) {
|
|
802
|
+
this.#anchor = anchor;
|
|
803
|
+
const doc = anchor.ownerDocument;
|
|
804
|
+
if (doc === null)
|
|
805
|
+
throw new ReaderError('the reader element is not in a document');
|
|
806
|
+
this.#doc = doc;
|
|
807
|
+
}
|
|
808
|
+
open(src, alt) {
|
|
809
|
+
if (this.#root !== null) {
|
|
810
|
+
// Re-target an already-open overlay rather than stacking a second one.
|
|
811
|
+
this.#image.src = src;
|
|
812
|
+
this.#image.alt = alt;
|
|
813
|
+
return;
|
|
814
|
+
}
|
|
815
|
+
const active = this.#doc.activeElement;
|
|
816
|
+
this.#previouslyFocused = active instanceof HTMLElement ? active : this.#anchor;
|
|
817
|
+
const root = this.#doc.createElement('div');
|
|
818
|
+
root.setAttribute('role', 'dialog');
|
|
819
|
+
root.setAttribute('aria-modal', 'true');
|
|
820
|
+
root.setAttribute('aria-label', alt !== '' ? alt : 'Zoomed image');
|
|
821
|
+
Object.assign(root.style, {
|
|
822
|
+
position: 'fixed',
|
|
823
|
+
inset: '0',
|
|
824
|
+
display: 'flex',
|
|
825
|
+
alignItems: 'center',
|
|
826
|
+
justifyContent: 'center',
|
|
827
|
+
background: 'rgba(0, 0, 0, 0.85)',
|
|
828
|
+
zIndex: '2147483647',
|
|
829
|
+
padding: '4vmin',
|
|
830
|
+
boxSizing: 'border-box',
|
|
831
|
+
opacity: this.#reducedMotion() ? '1' : '0',
|
|
832
|
+
transition: this.#reducedMotion() ? 'none' : 'opacity 150ms ease',
|
|
833
|
+
});
|
|
834
|
+
root.addEventListener('pointerdown', (event) => {
|
|
835
|
+
if (event.target === root)
|
|
836
|
+
this.close();
|
|
837
|
+
});
|
|
838
|
+
const image = this.#doc.createElement('img');
|
|
839
|
+
image.src = src;
|
|
840
|
+
image.alt = alt;
|
|
841
|
+
image.tabIndex = 0;
|
|
842
|
+
Object.assign(image.style, {
|
|
843
|
+
maxWidth: '100%',
|
|
844
|
+
maxHeight: '100%',
|
|
845
|
+
objectFit: 'contain',
|
|
846
|
+
boxShadow: '0 4px 32px rgba(0, 0, 0, 0.5)',
|
|
847
|
+
});
|
|
848
|
+
const close = this.#doc.createElement('button');
|
|
849
|
+
close.type = 'button';
|
|
850
|
+
close.setAttribute('aria-label', 'Close');
|
|
851
|
+
close.textContent = '×';
|
|
852
|
+
Object.assign(close.style, {
|
|
853
|
+
position: 'absolute',
|
|
854
|
+
top: '2vmin',
|
|
855
|
+
right: '2vmin',
|
|
856
|
+
width: '2.5rem',
|
|
857
|
+
height: '2.5rem',
|
|
858
|
+
fontSize: '1.5rem',
|
|
859
|
+
lineHeight: '1',
|
|
860
|
+
cursor: 'pointer',
|
|
861
|
+
border: '0',
|
|
862
|
+
borderRadius: '50%',
|
|
863
|
+
background: 'rgba(255, 255, 255, 0.9)',
|
|
864
|
+
color: '#000',
|
|
865
|
+
});
|
|
866
|
+
close.addEventListener('click', () => {
|
|
867
|
+
this.close();
|
|
868
|
+
});
|
|
869
|
+
root.append(image, close);
|
|
870
|
+
this.#doc.body.append(root);
|
|
871
|
+
this.#root = root;
|
|
872
|
+
this.#image = image;
|
|
873
|
+
this.#closeButton = close;
|
|
874
|
+
this.#doc.addEventListener('keydown', this.#onKeyDown, true);
|
|
875
|
+
// Fade in on the next frame so the transition has a start value to animate from.
|
|
876
|
+
if (!this.#reducedMotion()) {
|
|
877
|
+
const view = this.#doc.defaultView;
|
|
878
|
+
if (view !== null)
|
|
879
|
+
view.requestAnimationFrame(() => (root.style.opacity = '1'));
|
|
880
|
+
}
|
|
881
|
+
close.focus();
|
|
882
|
+
}
|
|
883
|
+
close() {
|
|
884
|
+
const root = this.#root;
|
|
885
|
+
if (root === null)
|
|
886
|
+
return;
|
|
887
|
+
this.#doc.removeEventListener('keydown', this.#onKeyDown, true);
|
|
888
|
+
this.#root = null;
|
|
889
|
+
this.#image = null;
|
|
890
|
+
this.#closeButton = null;
|
|
891
|
+
const restore = this.#previouslyFocused;
|
|
892
|
+
this.#previouslyFocused = null;
|
|
893
|
+
root.remove();
|
|
894
|
+
this.#restoreFocus(restore);
|
|
895
|
+
}
|
|
896
|
+
/**
|
|
897
|
+
* Return focus to the reader region. The tap that opened the overlay came from
|
|
898
|
+
* inside the sandboxed frame, so the host's active element is usually the frame's
|
|
899
|
+
* body (not focusable from here) — fall back to the reader mount, made
|
|
900
|
+
* programmatically focusable with a temporary `tabindex` so focus lands on the
|
|
901
|
+
* reader rather than the document body.
|
|
902
|
+
*/
|
|
903
|
+
#restoreFocus(saved) {
|
|
904
|
+
if (saved !== null && saved.isConnected && saved !== this.#doc.body) {
|
|
905
|
+
saved.focus();
|
|
906
|
+
if (this.#doc.activeElement === saved)
|
|
907
|
+
return;
|
|
908
|
+
}
|
|
909
|
+
const anchor = this.#anchor;
|
|
910
|
+
// A bare div is not focusable; a -1 tabindex makes it programmatically
|
|
911
|
+
// focusable without adding it to the tab order. Idempotent and benign.
|
|
912
|
+
if (!anchor.hasAttribute('tabindex'))
|
|
913
|
+
anchor.tabIndex = -1;
|
|
914
|
+
anchor.focus();
|
|
915
|
+
}
|
|
916
|
+
destroy() {
|
|
917
|
+
this.close();
|
|
918
|
+
}
|
|
919
|
+
/**
|
|
920
|
+
* Keep Tab focus inside the overlay: the close control and image are the stops.
|
|
921
|
+
* Tab is intercepted unconditionally and focus is moved explicitly so it can
|
|
922
|
+
* never escape the overlay — the reader region behind it is inert while open.
|
|
923
|
+
*/
|
|
924
|
+
#trapTab(event) {
|
|
925
|
+
event.preventDefault();
|
|
926
|
+
const stops = this.#focusStops();
|
|
927
|
+
if (stops.length === 0)
|
|
928
|
+
return;
|
|
929
|
+
const active = this.#doc.activeElement;
|
|
930
|
+
const current = active === null ? -1 : stops.indexOf(active);
|
|
931
|
+
const step = event.shiftKey ? -1 : 1;
|
|
932
|
+
const nextIndex = current === -1 ? 0 : (current + step + stops.length) % stops.length;
|
|
933
|
+
stops[nextIndex].focus();
|
|
934
|
+
}
|
|
935
|
+
#focusStops() {
|
|
936
|
+
const stops = [];
|
|
937
|
+
if (this.#closeButton !== null)
|
|
938
|
+
stops.push(this.#closeButton);
|
|
939
|
+
if (this.#image !== null)
|
|
940
|
+
stops.push(this.#image);
|
|
941
|
+
return stops;
|
|
942
|
+
}
|
|
943
|
+
#reducedMotion() {
|
|
944
|
+
const view = this.#doc.defaultView;
|
|
945
|
+
return view !== null && view.matchMedia('(prefers-reduced-motion: reduce)').matches;
|
|
946
|
+
}
|
|
947
|
+
}
|
|
948
|
+
/** Structural check for a `Position` (vs a TocItem, both objects). */
|
|
949
|
+
function isPosition(value) {
|
|
950
|
+
return (typeof value.serialized === 'string' &&
|
|
951
|
+
typeof value.sectionId === 'string' &&
|
|
952
|
+
value.anchor !== undefined &&
|
|
953
|
+
value.children === undefined);
|
|
954
|
+
}
|
|
955
|
+
// Re-exported so a caller can rebuild a Position from its serialized form before
|
|
956
|
+
// handing it to goTo — the facade never persists, but goTo(Position) is a target.
|
|
957
|
+
export { parsePosition };
|
|
958
|
+
//# sourceMappingURL=index.js.map
|