@seamless-co/pagemark 0.0.0-stage → 0.5.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 +25 -2
- package/behaviours/behaviour.d.ts +43 -0
- package/behaviours/behaviour.js +84 -0
- package/behaviours/comparison.d.ts +62 -0
- package/behaviours/comparison.js +129 -0
- package/behaviours/document-viewer.d.ts +9 -0
- package/behaviours/document-viewer.js +9 -0
- package/behaviours/hidden.d.ts +24 -0
- package/behaviours/hidden.js +38 -0
- package/behaviours/hover.d.ts +34 -0
- package/behaviours/hover.js +49 -0
- package/behaviours/index.d.ts +24 -0
- package/behaviours/index.js +14 -0
- package/behaviours/linked-viewers.d.ts +14 -0
- package/behaviours/linked-viewers.js +25 -0
- package/behaviours/links.d.ts +4 -0
- package/behaviours/links.js +4 -0
- package/behaviours/marks.d.ts +64 -0
- package/behaviours/marks.js +33 -0
- package/behaviours/quotes.d.ts +7 -0
- package/behaviours/quotes.js +9 -0
- package/behaviours/region.d.ts +27 -0
- package/behaviours/region.js +40 -0
- package/behaviours/search.d.ts +38 -0
- package/behaviours/search.js +63 -0
- package/behaviours/selection.d.ts +30 -0
- package/behaviours/selection.js +41 -0
- package/behaviours/text.d.ts +8 -0
- package/behaviours/text.js +24 -0
- package/behaviours/viewer-state.d.ts +36 -0
- package/behaviours/viewer-state.js +58 -0
- package/browser/blob-pieces.d.ts +9 -0
- package/browser/blob-pieces.js +50 -0
- package/browser/browser-source.d.ts +25 -0
- package/browser/browser-source.js +62 -0
- package/browser/index.d.ts +2 -0
- package/browser/index.js +1 -0
- package/browser/local-pdf.d.ts +27 -0
- package/browser/local-pdf.js +61 -0
- package/browser/worker-engine.d.ts +21 -0
- package/browser/worker-engine.js +1 -0
- package/browser/worker.d.ts +1 -0
- package/browser/worker.js +51 -0
- package/core/compare.d.ts +55 -0
- package/core/compare.js +350 -0
- package/core/find.d.ts +12 -0
- package/core/find.js +177 -0
- package/core/hidden.d.ts +12 -0
- package/core/hidden.js +26 -0
- package/core/index.d.ts +11 -0
- package/core/index.js +6 -0
- package/core/links.d.ts +7 -0
- package/core/links.js +19 -0
- package/core/locate.d.ts +21 -0
- package/core/locate.js +140 -0
- package/core/paragraphs.d.ts +37 -0
- package/core/paragraphs.js +141 -0
- package/core/types.d.ts +89 -0
- package/core/types.js +1 -0
- package/document/document.d.ts +83 -0
- package/document/document.js +215 -0
- package/document/handover-source.d.ts +18 -0
- package/document/handover-source.js +39 -0
- package/document/index.d.ts +10 -0
- package/document/index.js +5 -0
- package/document/reading-source.d.ts +7 -0
- package/document/reading-source.js +16 -0
- package/document/server-source.d.ts +10 -0
- package/document/server-source.js +23 -0
- package/document/source.d.ts +21 -0
- package/document/source.js +1 -0
- package/document/split-source.d.ts +9 -0
- package/document/split-source.js +25 -0
- package/document/store.d.ts +12 -0
- package/document/store.js +21 -0
- package/engine/engine-thread.d.ts +10 -0
- package/engine/engine-thread.js +1 -0
- package/engine/index.d.ts +2 -0
- package/engine/index.js +1 -0
- package/engine/pdfium.d.ts +16 -0
- package/engine/pdfium.js +222 -0
- package/engine/remote.d.ts +51 -0
- package/engine/remote.js +109 -0
- package/package.json +65 -4
- package/react/hooks.d.ts +128 -0
- package/react/hooks.js +129 -0
- package/react/index.d.ts +3 -0
- package/react/index.js +2 -0
- package/react/viewer.d.ts +48 -0
- package/react/viewer.js +127 -0
- package/reader/assemble.d.ts +4 -0
- package/reader/assemble.js +29 -0
- package/reader/index.d.ts +7 -0
- package/reader/index.js +5 -0
- package/reader/layer-page.d.ts +30 -0
- package/reader/layer-page.js +40 -0
- package/reader/layer-words.d.ts +4 -0
- package/reader/layer-words.js +102 -0
- package/reader/letter-edges.d.ts +13 -0
- package/reader/letter-edges.js +60 -0
- package/reader/links.d.ts +7 -0
- package/reader/links.js +22 -0
- package/reader/seams.d.ts +92 -0
- package/reader/seams.js +3 -0
- package/reader/seen.d.ts +16 -0
- package/reader/seen.js +67 -0
- package/reader/trust.d.ts +9 -0
- package/reader/trust.js +34 -0
- package/viewer/attachments.d.ts +11 -0
- package/viewer/attachments.js +104 -0
- package/viewer/dom.d.ts +14 -0
- package/viewer/dom.js +38 -0
- package/viewer/events.d.ts +17 -0
- package/viewer/events.js +20 -0
- package/viewer/index.d.ts +17 -0
- package/viewer/index.js +8 -0
- package/viewer/links.d.ts +16 -0
- package/viewer/links.js +59 -0
- package/viewer/marks.d.ts +24 -0
- package/viewer/marks.js +171 -0
- package/viewer/page-layers.d.ts +13 -0
- package/viewer/page-layers.js +33 -0
- package/viewer/pictures.d.ts +24 -0
- package/viewer/pictures.js +293 -0
- package/viewer/pointer.d.ts +21 -0
- package/viewer/pointer.js +244 -0
- package/viewer/stage.d.ts +21 -0
- package/viewer/stage.js +31 -0
- package/viewer/text-between.d.ts +13 -0
- package/viewer/text-between.js +28 -0
- package/viewer/text.d.ts +20 -0
- package/viewer/text.js +180 -0
- package/viewer/tile-grid.d.ts +20 -0
- package/viewer/tile-grid.js +36 -0
- package/viewer/tiles.d.ts +15 -0
- package/viewer/tiles.js +190 -0
- package/viewer/turn.d.ts +18 -0
- package/viewer/turn.js +40 -0
- package/viewer/types.d.ts +238 -0
- package/viewer/types.js +1 -0
- package/viewer/view-over.d.ts +21 -0
- package/viewer/view-over.js +191 -0
- package/viewer/view.d.ts +59 -0
- package/viewer/view.js +1 -0
- package/viewer/viewer.css +301 -0
- package/viewer/viewer.d.ts +11 -0
- package/viewer/viewer.js +104 -0
- package/viewer/viewport.d.ts +37 -0
- package/viewer/viewport.js +429 -0
package/viewer/marks.js
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import { clamp, element, placeOver, unionOf } from "./dom.js";
|
|
2
|
+
import { pageLayer } from "./page-layers.js";
|
|
3
|
+
/** NOTE: the keys that step from one mark to another, and how far each goes. */
|
|
4
|
+
const STEPS = { ArrowDown: 1, ArrowRight: 1, ArrowUp: -1, ArrowLeft: -1, Home: Number.NEGATIVE_INFINITY, End: Number.POSITIVE_INFINITY };
|
|
5
|
+
/**
|
|
6
|
+
* NOTE: a mark with a label is a button a keyboard reaches. All of them are one stop for the Tab key, however many
|
|
7
|
+
* there are: Tab comes to one mark, the arrow keys and Home and End step between them in reading order, and Enter
|
|
8
|
+
* or the space bar presses the one in hand. Stepping brings a mark into view and changes nothing else, so which
|
|
9
|
+
* mark the caller has in focus stays the caller's to say, as it is under a pointer.
|
|
10
|
+
*/
|
|
11
|
+
export function createMarks(view, options = {}) {
|
|
12
|
+
const layers = new Map();
|
|
13
|
+
const hosts = view.pages.map(() => new Map());
|
|
14
|
+
const own = new Set();
|
|
15
|
+
/** NOTE: what the caller asked to have in focus, kept whether or not each mark is drawn yet, so a mark whose layer arrives later is lit when it does. */
|
|
16
|
+
let focusedIds = [];
|
|
17
|
+
/** NOTE: the mark Tab comes to: the one a keyboard last reached, or the caller's mark in focus if that changed since. */
|
|
18
|
+
let stopId = null;
|
|
19
|
+
const findMark = (id) => {
|
|
20
|
+
for (const drawn of layers.values()) {
|
|
21
|
+
const found = drawn.get(id);
|
|
22
|
+
if (found)
|
|
23
|
+
return found;
|
|
24
|
+
}
|
|
25
|
+
return undefined;
|
|
26
|
+
};
|
|
27
|
+
/** NOTE: the marks a keyboard reaches, in reading order: by page, then down it, then across. */
|
|
28
|
+
const reachable = () => [...layers.values()]
|
|
29
|
+
.flatMap((drawn) => [...drawn.values()])
|
|
30
|
+
.filter(({ mark, nodes }) => mark.label !== undefined && nodes.length > 0)
|
|
31
|
+
.sort((first, second) => {
|
|
32
|
+
const [a, b] = [first.mark.rects[0], second.mark.rects[0]];
|
|
33
|
+
return first.mark.page - second.mark.page || (a?.y ?? 0) - (b?.y ?? 0) || (a?.x ?? 0) - (b?.x ?? 0);
|
|
34
|
+
});
|
|
35
|
+
/** INVARIANT: exactly one reachable mark takes the Tab key, so the marks are one stop among everything else on the screen. */
|
|
36
|
+
const setStop = () => {
|
|
37
|
+
const all = reachable();
|
|
38
|
+
const stop = all.find(({ mark }) => mark.id === stopId) ?? all.find(({ mark }) => focusedIds.includes(mark.id)) ?? all[0];
|
|
39
|
+
for (const drawn of all) {
|
|
40
|
+
const node = drawn.nodes[0];
|
|
41
|
+
if (node)
|
|
42
|
+
node.tabIndex = drawn === stop ? 0 : -1;
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
const light = () => {
|
|
46
|
+
const targets = focusedIds.flatMap((focused) => findMark(focused) ?? []);
|
|
47
|
+
for (const target of targets)
|
|
48
|
+
for (const node of target.nodes)
|
|
49
|
+
node.classList.add("pm-mark-focused");
|
|
50
|
+
return targets;
|
|
51
|
+
};
|
|
52
|
+
const focusMark = (id, asked) => {
|
|
53
|
+
for (const focused of focusedIds)
|
|
54
|
+
for (const node of findMark(focused)?.nodes ?? [])
|
|
55
|
+
node.classList.remove("pm-mark-focused");
|
|
56
|
+
focusedIds = id === null ? [] : typeof id === "string" ? [id] : [...id];
|
|
57
|
+
const targets = light();
|
|
58
|
+
stopId = targets.find((target) => target.mark.label !== undefined)?.mark.id ?? stopId;
|
|
59
|
+
setStop();
|
|
60
|
+
const first = targets[0];
|
|
61
|
+
const area = first ? unionOf(first.mark.rects) : null;
|
|
62
|
+
if (asked?.reveal && first && area)
|
|
63
|
+
view.revealRect(first.mark.page, area);
|
|
64
|
+
};
|
|
65
|
+
/** NOTE: the mark an element is the button of, or undefined for any other element. */
|
|
66
|
+
const markOf = (node) => {
|
|
67
|
+
const id = node instanceof HTMLElement ? node.dataset.markId : undefined;
|
|
68
|
+
const drawn = id === undefined ? undefined : findMark(id);
|
|
69
|
+
return drawn && drawn.nodes[0] === node && drawn.mark.label !== undefined ? drawn : undefined;
|
|
70
|
+
};
|
|
71
|
+
const onKey = (event) => {
|
|
72
|
+
const drawn = markOf(event.target);
|
|
73
|
+
if (!drawn)
|
|
74
|
+
return;
|
|
75
|
+
const step = STEPS[event.key];
|
|
76
|
+
if (step !== undefined) {
|
|
77
|
+
event.preventDefault();
|
|
78
|
+
const all = reachable();
|
|
79
|
+
const next = all[clamp(all.indexOf(drawn) + step, 0, all.length - 1)];
|
|
80
|
+
const area = next ? unionOf(next.mark.rects) : null;
|
|
81
|
+
if (!next || !area)
|
|
82
|
+
return;
|
|
83
|
+
next.nodes[0]?.focus({ preventScroll: true });
|
|
84
|
+
view.revealRect(next.mark.page, area);
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
const rect = drawn.mark.rects[0];
|
|
88
|
+
if ((event.key !== "Enter" && event.key !== " ") || !rect)
|
|
89
|
+
return;
|
|
90
|
+
event.preventDefault();
|
|
91
|
+
const point = { page: drawn.mark.page, x: rect.x + rect.width / 2, y: rect.y + rect.height / 2 };
|
|
92
|
+
options.onPress?.(point, marksAt(point));
|
|
93
|
+
};
|
|
94
|
+
/** NOTE: however a mark came to hold the keyboard, by Tab or by an arrow, it is the one Tab comes back to. */
|
|
95
|
+
const onFocus = (event) => {
|
|
96
|
+
const drawn = markOf(event.target);
|
|
97
|
+
if (!drawn)
|
|
98
|
+
return;
|
|
99
|
+
stopId = drawn.mark.id;
|
|
100
|
+
setStop();
|
|
101
|
+
};
|
|
102
|
+
const marksAt = (point) => {
|
|
103
|
+
const under = [];
|
|
104
|
+
for (const drawn of layers.values()) {
|
|
105
|
+
for (const { mark } of drawn.values()) {
|
|
106
|
+
if (mark.page !== point.page)
|
|
107
|
+
continue;
|
|
108
|
+
const isUnder = mark.rects.some((rect) => point.x >= rect.x && point.x <= rect.x + rect.width && point.y >= rect.y && point.y <= rect.y + rect.height);
|
|
109
|
+
if (isUnder)
|
|
110
|
+
under.push(mark);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return under;
|
|
114
|
+
};
|
|
115
|
+
view.pane.addEventListener("keydown", onKey);
|
|
116
|
+
view.pane.addEventListener("focusin", onFocus);
|
|
117
|
+
return {
|
|
118
|
+
focusMark,
|
|
119
|
+
setLayer(layer, marks) {
|
|
120
|
+
const drawn = new Map();
|
|
121
|
+
/** NOTE: a layer is drawn afresh, which would drop the keyboard from a mark in it, so the mark of the same id is given it back. */
|
|
122
|
+
const held = markOf(view.pane.ownerDocument.activeElement);
|
|
123
|
+
const heldId = held && layers.get(layer)?.has(held.mark.id) ? held.mark.id : null;
|
|
124
|
+
for (const pageHosts of hosts)
|
|
125
|
+
pageHosts.get(layer)?.replaceChildren();
|
|
126
|
+
for (const mark of marks) {
|
|
127
|
+
const page = view.pages[mark.page];
|
|
128
|
+
const pageHosts = hosts[mark.page];
|
|
129
|
+
if (!page || !pageHosts)
|
|
130
|
+
continue;
|
|
131
|
+
let host = pageHosts.get(layer);
|
|
132
|
+
if (!host) {
|
|
133
|
+
host = element("pm-layer");
|
|
134
|
+
host.dataset.layer = layer;
|
|
135
|
+
pageHosts.set(layer, host);
|
|
136
|
+
const over = pageLayer(page, "marks");
|
|
137
|
+
own.add(over);
|
|
138
|
+
over.append(host);
|
|
139
|
+
}
|
|
140
|
+
const nodes = [];
|
|
141
|
+
for (const rect of mark.rects) {
|
|
142
|
+
const node = element(mark.tone ? `pm-mark pm-mark-${mark.tone}` : "pm-mark");
|
|
143
|
+
node.dataset.markId = mark.id;
|
|
144
|
+
placeOver(node, rect, page.size);
|
|
145
|
+
host.append(node);
|
|
146
|
+
nodes.push(node);
|
|
147
|
+
}
|
|
148
|
+
const button = nodes[0];
|
|
149
|
+
if (button && mark.label !== undefined) {
|
|
150
|
+
button.setAttribute("role", "button");
|
|
151
|
+
button.setAttribute("aria-label", mark.label);
|
|
152
|
+
}
|
|
153
|
+
drawn.set(mark.id, { mark, nodes });
|
|
154
|
+
}
|
|
155
|
+
layers.set(layer, drawn);
|
|
156
|
+
light();
|
|
157
|
+
setStop();
|
|
158
|
+
if (heldId !== null)
|
|
159
|
+
drawn.get(heldId)?.nodes[0]?.focus({ preventScroll: true });
|
|
160
|
+
},
|
|
161
|
+
marksAt,
|
|
162
|
+
destroy() {
|
|
163
|
+
view.pane.removeEventListener("keydown", onKey);
|
|
164
|
+
view.pane.removeEventListener("focusin", onFocus);
|
|
165
|
+
for (const over of own)
|
|
166
|
+
over.remove();
|
|
167
|
+
own.clear();
|
|
168
|
+
layers.clear();
|
|
169
|
+
},
|
|
170
|
+
};
|
|
171
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { ViewPage } from "./view.js";
|
|
2
|
+
/**
|
|
3
|
+
* INVARIANT: a page's layers stand in this order, bottom to top, whichever part asks for its own first: the tiles
|
|
4
|
+
* over the page's picture, then the text, the links, the marks and the caller's attachments. What draws the page
|
|
5
|
+
* lies under the text a reader selects. A link, which a reader presses, lies under the marks, which take no pointer.
|
|
6
|
+
*/
|
|
7
|
+
declare const ORDER: readonly ["tiles", "text", "links", "marks", "attached"];
|
|
8
|
+
export type PageLayer = (typeof ORDER)[number];
|
|
9
|
+
/** NOTE: one part's layer over one page, made when first asked for. A part keeps what this gives it. */
|
|
10
|
+
export declare function pageLayer(page: ViewPage, layer: PageLayer): HTMLDivElement;
|
|
11
|
+
/** NOTE: which page an element is, or lies inside. Parts ask this and never read an attribute a frame may not have set. */
|
|
12
|
+
export declare function pageFinder(pages: ReadonlyArray<ViewPage>): (node: Element | null) => ViewPage | null;
|
|
13
|
+
export {};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { element } from "./dom.js";
|
|
2
|
+
/**
|
|
3
|
+
* INVARIANT: a page's layers stand in this order, bottom to top, whichever part asks for its own first: the tiles
|
|
4
|
+
* over the page's picture, then the text, the links, the marks and the caller's attachments. What draws the page
|
|
5
|
+
* lies under the text a reader selects. A link, which a reader presses, lies under the marks, which take no pointer.
|
|
6
|
+
*/
|
|
7
|
+
const ORDER = ["tiles", "text", "links", "marks", "attached"];
|
|
8
|
+
const CLASS = { tiles: "pm-tiles", text: "pm-text", links: "pm-links", marks: "pm-marks", attached: "pm-attached-layer" };
|
|
9
|
+
const childOf = (page, layer) => page.node.querySelector(`:scope > .${CLASS[layer]}`);
|
|
10
|
+
/** NOTE: one part's layer over one page, made when first asked for. A part keeps what this gives it. */
|
|
11
|
+
export function pageLayer(page, layer) {
|
|
12
|
+
const known = childOf(page, layer);
|
|
13
|
+
if (known)
|
|
14
|
+
return known;
|
|
15
|
+
const node = element(CLASS[layer]);
|
|
16
|
+
const above = ORDER.slice(ORDER.indexOf(layer) + 1)
|
|
17
|
+
.map((later) => childOf(page, later))
|
|
18
|
+
.find((found) => found !== null);
|
|
19
|
+
page.node.insertBefore(node, above ?? null);
|
|
20
|
+
return node;
|
|
21
|
+
}
|
|
22
|
+
/** NOTE: which page an element is, or lies inside. Parts ask this and never read an attribute a frame may not have set. */
|
|
23
|
+
export function pageFinder(pages) {
|
|
24
|
+
const known = new Map(pages.map((page) => [page.node, page]));
|
|
25
|
+
return (node) => {
|
|
26
|
+
for (let at = node; at; at = at.parentElement) {
|
|
27
|
+
const page = known.get(at);
|
|
28
|
+
if (page)
|
|
29
|
+
return page;
|
|
30
|
+
}
|
|
31
|
+
return null;
|
|
32
|
+
};
|
|
33
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { ViewerOptions } from "./types.js";
|
|
2
|
+
import type { Part, View } from "./view.js";
|
|
3
|
+
/**
|
|
4
|
+
* NOTE: owns which picture each page shows: a small one at once, a sharp one for pages on screen and then their
|
|
5
|
+
* neighbours, tiles over the part on screen once a whole picture cannot be sharp enough, and none for pages far away.
|
|
6
|
+
* It asks the caller's `imageUrl` and `tileUrl` for every picture and never draws one itself.
|
|
7
|
+
* NOTE: a page left with no picture after its tries failed is a failed page: its element carries `data-failed`, and
|
|
8
|
+
* it stays so until a retry, a picture that does arrive, or the page going far from view and coming back.
|
|
9
|
+
*/
|
|
10
|
+
export type Pictures = Part & {
|
|
11
|
+
/** NOTE: the failed pages, counted from 0, in page order. */
|
|
12
|
+
readonly failedPages: ReadonlyArray<number>;
|
|
13
|
+
/** NOTE: tries a failed page again, or every failed page when none is named. */
|
|
14
|
+
retry(page?: number): void;
|
|
15
|
+
};
|
|
16
|
+
export type PictureOptions = Pick<ViewerOptions, "imageUrl" | "tileUrl" | "releaseImage" | "scales"> & {
|
|
17
|
+
/** NOTE: called each time every page on screen has become as sharp as it needs to be. */
|
|
18
|
+
onIdle?: () => void;
|
|
19
|
+
/** NOTE: called with every failed page each time a page fails or stops being failed. */
|
|
20
|
+
onFailures?: (pages: ReadonlyArray<number>) => void;
|
|
21
|
+
/** NOTE: called with the pages on screen and within a pane's height of it, in page order, each time they change. */
|
|
22
|
+
onNear?: (pages: ReadonlyArray<number>) => void;
|
|
23
|
+
};
|
|
24
|
+
export declare function createPictures(view: View, options: PictureOptions): Pictures;
|
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
import { pageFinder } from "./page-layers.js";
|
|
2
|
+
import { heldScale } from "./tile-grid.js";
|
|
3
|
+
import { createTiles } from "./tiles.js";
|
|
4
|
+
const DEFAULT_SCALES = [0.5, 1, 1.5, 2, 2.5, 3, 4, 6];
|
|
5
|
+
/** NOTE: an image this share of the screen's own sharpness is drawn rather than the next size up, which costs far more bytes for a difference few can see. */
|
|
6
|
+
const SHARPNESS_TOLERANCE = 0.9;
|
|
7
|
+
/** NOTE: a page next to the ones on screen is fetched no sharper than this until it comes into view. */
|
|
8
|
+
const NEIGHBOUR_MAX_SCALE = 2;
|
|
9
|
+
/**
|
|
10
|
+
* NOTE: where tiles can be had, a whole page's picture is held no larger than this. Past it the page costs more to
|
|
11
|
+
* hold than the tiles that cover the pane, which is all a reader sees of a page that large.
|
|
12
|
+
*/
|
|
13
|
+
const MAX_WHOLE_PIXELS = 6_000_000;
|
|
14
|
+
const MAX_SHARP_LOADS = 2;
|
|
15
|
+
const MAX_LOAD_FAILURES = 2;
|
|
16
|
+
/** NOTE: sharp images wait until scrolling has rested this long, so a page that only flies past costs one small preview. */
|
|
17
|
+
const SCROLL_REST_MS = 160;
|
|
18
|
+
const SHARPEN_DELAY_MS = 140;
|
|
19
|
+
export function createPictures(view, options) {
|
|
20
|
+
const { imageUrl, tileUrl } = options;
|
|
21
|
+
const tiles = tileUrl ? createTiles(view, { tileUrl, releaseImage: options.releaseImage }) : null;
|
|
22
|
+
const scales = [...(options.scales ?? DEFAULT_SCALES)].sort((a, b) => a - b);
|
|
23
|
+
const pageIn = pageFinder(view.pages);
|
|
24
|
+
const pictures = view.pages.map((page) => {
|
|
25
|
+
const image = document.createElement("img");
|
|
26
|
+
image.className = "pm-image";
|
|
27
|
+
image.alt = `Page ${page.index + 1}`;
|
|
28
|
+
image.draggable = false;
|
|
29
|
+
page.node.prepend(image);
|
|
30
|
+
return { image, loadedScale: 0, requestedScale: 0, hasAskedPreview: false, failures: 0, loads: new AbortController(), sharpLoad: null, url: null };
|
|
31
|
+
});
|
|
32
|
+
const near = new Set();
|
|
33
|
+
const visible = new Set();
|
|
34
|
+
const kept = new Set();
|
|
35
|
+
const failed = new Set();
|
|
36
|
+
let failedPages = [];
|
|
37
|
+
const tellFailures = () => {
|
|
38
|
+
failedPages = [...failed].sort((first, second) => first - second);
|
|
39
|
+
options.onFailures?.(failedPages);
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* NOTE: a page is failed once it has nothing to show and no try is left. It stays so through a retry, until a
|
|
43
|
+
* picture arrives or the page goes far from view, so a retry that fails again is no change to tell.
|
|
44
|
+
*/
|
|
45
|
+
const judge = (index) => {
|
|
46
|
+
const picture = pictures[index];
|
|
47
|
+
const page = view.pages[index];
|
|
48
|
+
if (!picture || !page)
|
|
49
|
+
return;
|
|
50
|
+
let isFailed = failed.has(index);
|
|
51
|
+
if (picture.loadedScale > 0 || !isHeld(index))
|
|
52
|
+
isFailed = false;
|
|
53
|
+
else if (picture.failures >= MAX_LOAD_FAILURES)
|
|
54
|
+
isFailed = true;
|
|
55
|
+
if (isFailed === failed.has(index))
|
|
56
|
+
return;
|
|
57
|
+
if (isFailed) {
|
|
58
|
+
failed.add(index);
|
|
59
|
+
page.node.dataset.failed = "";
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
failed.delete(index);
|
|
63
|
+
delete page.node.dataset.failed;
|
|
64
|
+
}
|
|
65
|
+
tellFailures();
|
|
66
|
+
};
|
|
67
|
+
let sharpenTimer;
|
|
68
|
+
let sharpLoads = 0;
|
|
69
|
+
let isBusy = true;
|
|
70
|
+
let knowsWhatIsOnScreen = false;
|
|
71
|
+
/** NOTE: the pixels for each page unit the screen can show at this zoom. */
|
|
72
|
+
const neededScale = () => view.zoom * (window.devicePixelRatio || 1) * SHARPNESS_TOLERANCE;
|
|
73
|
+
const stepUpTo = (needed) => scales.find((scale) => scale >= needed - 0.01) ?? scales[scales.length - 1] ?? 1;
|
|
74
|
+
/** NOTE: a page is asked for no sharper than it has detail to show, and, where tiles can be had, no larger than a whole picture is worth holding. */
|
|
75
|
+
const targetScale = (index) => {
|
|
76
|
+
const size = view.pages[index]?.size;
|
|
77
|
+
const wanted = stepUpTo(Math.min(neededScale(), size?.sharpest ?? Number.POSITIVE_INFINITY));
|
|
78
|
+
const asked = visible.has(index) || wanted <= NEIGHBOUR_MAX_SCALE ? wanted : ([...scales].reverse().find((scale) => scale <= NEIGHBOUR_MAX_SCALE) ?? wanted);
|
|
79
|
+
return tiles && size ? heldScale(scales, asked, size, MAX_WHOLE_PIXELS) : asked;
|
|
80
|
+
};
|
|
81
|
+
const isHeld = (index) => near.has(index) || kept.has(index);
|
|
82
|
+
/**
|
|
83
|
+
* NOTE: the fetched image itself takes the page's place, so nothing is downloaded a second time even where the
|
|
84
|
+
* browser's cache is off, and it is decoded before it is shown, so swapping it in never stalls the page.
|
|
85
|
+
* TRADEOFF: drawing into a canvas the viewer owns was tried, to give memory back explicitly on unload; in Chromium it
|
|
86
|
+
* held about twice the memory of image elements after reading through a long document, so images stay.
|
|
87
|
+
*/
|
|
88
|
+
const fetchImage = async (index, scale, signal) => {
|
|
89
|
+
const url = await imageUrl(index, scale, signal);
|
|
90
|
+
const image = new Image();
|
|
91
|
+
image.src = url;
|
|
92
|
+
try {
|
|
93
|
+
await image.decode();
|
|
94
|
+
}
|
|
95
|
+
catch (error) {
|
|
96
|
+
options.releaseImage?.(url);
|
|
97
|
+
throw error;
|
|
98
|
+
}
|
|
99
|
+
return { image, url };
|
|
100
|
+
};
|
|
101
|
+
const show = (index, picture, fetched, scale) => {
|
|
102
|
+
fetched.image.className = "pm-image";
|
|
103
|
+
fetched.image.alt = picture.image.alt;
|
|
104
|
+
fetched.image.draggable = false;
|
|
105
|
+
fetched.image.dataset.scale = String(scale);
|
|
106
|
+
picture.image.replaceWith(fetched.image);
|
|
107
|
+
picture.image = fetched.image;
|
|
108
|
+
if (picture.url !== null)
|
|
109
|
+
options.releaseImage?.(picture.url);
|
|
110
|
+
picture.url = fetched.url;
|
|
111
|
+
picture.loadedScale = scale;
|
|
112
|
+
judge(index);
|
|
113
|
+
};
|
|
114
|
+
/** NOTE: a small image shown at once, so a page is never blank while its sharp image travels. */
|
|
115
|
+
const askPreview = (index) => {
|
|
116
|
+
const picture = pictures[index];
|
|
117
|
+
const scale = scales[0];
|
|
118
|
+
if (!picture || scale === undefined || picture.loadedScale > 0 || picture.hasAskedPreview)
|
|
119
|
+
return;
|
|
120
|
+
picture.hasAskedPreview = true;
|
|
121
|
+
const loads = picture.loads;
|
|
122
|
+
fetchImage(index, scale, loads.signal).then((fetched) => {
|
|
123
|
+
if (picture.loadedScale === 0 && isHeld(index))
|
|
124
|
+
show(index, picture, fetched, scale);
|
|
125
|
+
else
|
|
126
|
+
options.releaseImage?.(fetched.url);
|
|
127
|
+
}, () => {
|
|
128
|
+
if (loads.signal.aborted)
|
|
129
|
+
return;
|
|
130
|
+
picture.failures += 1;
|
|
131
|
+
judge(index);
|
|
132
|
+
});
|
|
133
|
+
};
|
|
134
|
+
const isSharp = (index) => {
|
|
135
|
+
const picture = pictures[index];
|
|
136
|
+
return !picture || picture.failures >= MAX_LOAD_FAILURES || picture.loadedScale >= targetScale(index);
|
|
137
|
+
};
|
|
138
|
+
/** NOTE: a neighbour waits until every page on screen is sharp, so it never shares the connection with one being read. */
|
|
139
|
+
const nextToSharpen = () => {
|
|
140
|
+
const needs = (index) => {
|
|
141
|
+
const picture = pictures[index];
|
|
142
|
+
return picture !== undefined && !isSharp(index) && picture.requestedScale < targetScale(index);
|
|
143
|
+
};
|
|
144
|
+
const inOrder = (pages) => [...pages].sort((first, second) => first - second);
|
|
145
|
+
if (!knowsWhatIsOnScreen)
|
|
146
|
+
return null;
|
|
147
|
+
const onScreen = inOrder(visible).find(needs);
|
|
148
|
+
if (onScreen !== undefined)
|
|
149
|
+
return onScreen;
|
|
150
|
+
if (![...visible].every(isSharp))
|
|
151
|
+
return null;
|
|
152
|
+
return inOrder(near).find(needs) ?? null;
|
|
153
|
+
};
|
|
154
|
+
const reportIdle = () => {
|
|
155
|
+
const isIdle = visible.size > 0 && [...visible].every(isSharp);
|
|
156
|
+
if (isIdle && isBusy)
|
|
157
|
+
options.onIdle?.();
|
|
158
|
+
isBusy = !isIdle;
|
|
159
|
+
};
|
|
160
|
+
/**
|
|
161
|
+
* NOTE: pages on screen are sharpened before their neighbours, a few at a time, so a slow connection serves what is
|
|
162
|
+
* being read first. Asking a page for a sharper picture drops its earlier request, so zooming through several steps
|
|
163
|
+
* draws the last one only.
|
|
164
|
+
*/
|
|
165
|
+
const sharpen = () => {
|
|
166
|
+
for (const index of visible)
|
|
167
|
+
askPreview(index);
|
|
168
|
+
if (performance.now() - view.lastScrollAt < SCROLL_REST_MS) {
|
|
169
|
+
clearTimeout(sharpenTimer);
|
|
170
|
+
sharpenTimer = setTimeout(sharpen, SCROLL_REST_MS);
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
if (knowsWhatIsOnScreen)
|
|
174
|
+
tiles?.cover(visible, neededScale(), targetScale);
|
|
175
|
+
if ([...visible].every(isSharp))
|
|
176
|
+
for (const index of near)
|
|
177
|
+
askPreview(index);
|
|
178
|
+
while (sharpLoads < MAX_SHARP_LOADS) {
|
|
179
|
+
const index = nextToSharpen();
|
|
180
|
+
const picture = index === null ? undefined : pictures[index];
|
|
181
|
+
if (index === null || !picture)
|
|
182
|
+
break;
|
|
183
|
+
const scale = targetScale(index);
|
|
184
|
+
picture.requestedScale = scale;
|
|
185
|
+
sharpLoads += 1;
|
|
186
|
+
picture.sharpLoad?.abort();
|
|
187
|
+
const load = new AbortController();
|
|
188
|
+
picture.sharpLoad = load;
|
|
189
|
+
fetchImage(index, scale, load.signal).then((fetched) => {
|
|
190
|
+
if (isHeld(index) && picture.loadedScale < scale)
|
|
191
|
+
show(index, picture, fetched, scale);
|
|
192
|
+
else
|
|
193
|
+
options.releaseImage?.(fetched.url);
|
|
194
|
+
sharpLoads -= 1;
|
|
195
|
+
sharpen();
|
|
196
|
+
}, () => {
|
|
197
|
+
if (picture.sharpLoad === load)
|
|
198
|
+
picture.requestedScale = picture.loadedScale;
|
|
199
|
+
if (!load.signal.aborted)
|
|
200
|
+
picture.failures += 1;
|
|
201
|
+
sharpLoads -= 1;
|
|
202
|
+
judge(index);
|
|
203
|
+
sharpen();
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
reportIdle();
|
|
207
|
+
};
|
|
208
|
+
/** NOTE: a page far from view gives its image back, so memory stays bounded however long the document is. */
|
|
209
|
+
const unload = (index) => {
|
|
210
|
+
const picture = pictures[index];
|
|
211
|
+
if (!picture)
|
|
212
|
+
return;
|
|
213
|
+
picture.loads.abort();
|
|
214
|
+
picture.loads = new AbortController();
|
|
215
|
+
picture.sharpLoad?.abort();
|
|
216
|
+
picture.sharpLoad = null;
|
|
217
|
+
if (picture.url !== null)
|
|
218
|
+
options.releaseImage?.(picture.url);
|
|
219
|
+
picture.url = null;
|
|
220
|
+
picture.image.removeAttribute("src");
|
|
221
|
+
delete picture.image.dataset.scale;
|
|
222
|
+
picture.loadedScale = 0;
|
|
223
|
+
picture.requestedScale = 0;
|
|
224
|
+
picture.hasAskedPreview = false;
|
|
225
|
+
picture.failures = 0;
|
|
226
|
+
judge(index);
|
|
227
|
+
};
|
|
228
|
+
const watch = (pages, rootMargin, told = {}) => {
|
|
229
|
+
const observer = new IntersectionObserver((entries) => {
|
|
230
|
+
if (pages === visible)
|
|
231
|
+
knowsWhatIsOnScreen = true;
|
|
232
|
+
const before = pages.size;
|
|
233
|
+
let isChanged = false;
|
|
234
|
+
for (const entry of entries) {
|
|
235
|
+
const page = pageIn(entry.target);
|
|
236
|
+
if (!page)
|
|
237
|
+
continue;
|
|
238
|
+
if (entry.isIntersecting)
|
|
239
|
+
pages.add(page.index);
|
|
240
|
+
else if (pages.delete(page.index)) {
|
|
241
|
+
isChanged = true;
|
|
242
|
+
told.onLeave?.(page.index);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
if (isChanged || pages.size !== before)
|
|
246
|
+
told.onChange?.();
|
|
247
|
+
sharpen();
|
|
248
|
+
}, { root: view.pane, rootMargin });
|
|
249
|
+
for (const page of view.pages)
|
|
250
|
+
observer.observe(page.node);
|
|
251
|
+
return observer;
|
|
252
|
+
};
|
|
253
|
+
const tellNear = () => options.onNear?.([...near].sort((first, second) => first - second));
|
|
254
|
+
const observers = [watch(visible, "0px"), watch(near, "100% 0px", { onChange: tellNear }), watch(kept, "300% 0px", { onLeave: unload })];
|
|
255
|
+
/** NOTE: a scroll sharpens at once, since `sharpen` itself waits for the pane to rest. A zoom or a resize waits a moment, so several steps draw the last one only. */
|
|
256
|
+
const unwatch = view.onChange((reason) => {
|
|
257
|
+
if (reason === "scroll") {
|
|
258
|
+
sharpen();
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
clearTimeout(sharpenTimer);
|
|
262
|
+
sharpenTimer = setTimeout(sharpen, SHARPEN_DELAY_MS);
|
|
263
|
+
});
|
|
264
|
+
return {
|
|
265
|
+
get failedPages() {
|
|
266
|
+
return failedPages;
|
|
267
|
+
},
|
|
268
|
+
retry(page) {
|
|
269
|
+
for (const index of page === undefined ? [...failed] : [page]) {
|
|
270
|
+
const picture = pictures[index];
|
|
271
|
+
if (!picture || !failed.has(index))
|
|
272
|
+
continue;
|
|
273
|
+
picture.failures = 0;
|
|
274
|
+
picture.hasAskedPreview = false;
|
|
275
|
+
}
|
|
276
|
+
sharpen();
|
|
277
|
+
},
|
|
278
|
+
destroy() {
|
|
279
|
+
unwatch();
|
|
280
|
+
clearTimeout(sharpenTimer);
|
|
281
|
+
tiles?.destroy();
|
|
282
|
+
for (const observer of observers)
|
|
283
|
+
observer.disconnect();
|
|
284
|
+
for (const picture of pictures) {
|
|
285
|
+
picture.loads.abort();
|
|
286
|
+
picture.sharpLoad?.abort();
|
|
287
|
+
if (picture.url !== null)
|
|
288
|
+
options.releaseImage?.(picture.url);
|
|
289
|
+
picture.image.remove();
|
|
290
|
+
}
|
|
291
|
+
},
|
|
292
|
+
};
|
|
293
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { PagePoint, ViewerDrag, ViewerMark } from "./types.js";
|
|
2
|
+
import type { Part, View } from "./view.js";
|
|
3
|
+
/**
|
|
4
|
+
* NOTE: owns what the pointer does over the pages and reports it in page units: where it is, which marks it is over, a
|
|
5
|
+
* press, and a drag held inside the page it started on. It decides nothing about what those mean.
|
|
6
|
+
* NOTE: while Space is held with the pointer over the pane, a press is a hand instead: it moves the pages and is not
|
|
7
|
+
* reported, as in most PDF readers.
|
|
8
|
+
*/
|
|
9
|
+
export type Pointer = Part;
|
|
10
|
+
export type PointerOptions = {
|
|
11
|
+
/** NOTE: the marks a point lies on, as a marks part's `marksAt` gives them. Left out, a press and the pointer are over none. */
|
|
12
|
+
marksAt?: (point: PagePoint) => ViewerMark[];
|
|
13
|
+
/** NOTE: the point under the pointer while no button is held, or null once it leaves the pages. */
|
|
14
|
+
onPointer?: (point: PagePoint | null) => void;
|
|
15
|
+
/** NOTE: called whenever the set of marks under the pointer changes; empty once it is over none. */
|
|
16
|
+
onPointerMarks?: (marks: ReadonlyArray<ViewerMark>, point: PagePoint | null) => void;
|
|
17
|
+
onDrag?: (drag: ViewerDrag) => void;
|
|
18
|
+
/** NOTE: a press that did not become a drag, with the marks under it. */
|
|
19
|
+
onPress?: (point: PagePoint, marks: ReadonlyArray<ViewerMark>) => void;
|
|
20
|
+
};
|
|
21
|
+
export declare function createPointer(view: View, options: PointerOptions): Pointer;
|