@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
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
import { clamp, glide } from "./dom.js";
|
|
2
|
+
const DRAG_THRESHOLD_PX = 4;
|
|
3
|
+
/**
|
|
4
|
+
* NOTE: a finger that comes down and lifts again within this distance and this long has tapped; further or longer, it
|
|
5
|
+
* scrolled or rested. Space let go within this long, having moved nothing, was tapped too.
|
|
6
|
+
*/
|
|
7
|
+
const TAP_MOVE_PX = 10;
|
|
8
|
+
const TAP_MS = 500;
|
|
9
|
+
/** NOTE: how much of the pane a tap of Space scrolls, as far as Chromium scrolls for it. */
|
|
10
|
+
const SCREEN_STEP = 0.875;
|
|
11
|
+
const NO_MARKS = () => [];
|
|
12
|
+
export function createPointer(view, options) {
|
|
13
|
+
const scroll = view.pane;
|
|
14
|
+
const marksAt = options.marksAt ?? NO_MARKS;
|
|
15
|
+
let frame = 0;
|
|
16
|
+
let last = null;
|
|
17
|
+
let press = null;
|
|
18
|
+
/** NOTE: a drag that started on a word is the browser's own selection until the button lifts; nothing is reported under it. */
|
|
19
|
+
let isSelectingText = false;
|
|
20
|
+
/** NOTE: a finger that is down and has not yet moved or stayed too long to be a tap. One finger drags nothing: it scrolls, which the browser does. */
|
|
21
|
+
let finger = null;
|
|
22
|
+
let isLastFromFinger = false;
|
|
23
|
+
let marksUnder = "";
|
|
24
|
+
/** NOTE: Space held down over the pane, since when, and whether a hand has pressed under it, which makes it no tap. */
|
|
25
|
+
let space = null;
|
|
26
|
+
let hand = null;
|
|
27
|
+
const showHand = () => {
|
|
28
|
+
if (hand)
|
|
29
|
+
scroll.dataset.hand = "moving";
|
|
30
|
+
else if (space)
|
|
31
|
+
scroll.dataset.hand = "ready";
|
|
32
|
+
else
|
|
33
|
+
delete scroll.dataset.hand;
|
|
34
|
+
};
|
|
35
|
+
const report = () => {
|
|
36
|
+
frame = 0;
|
|
37
|
+
if (press?.isDragging || hand)
|
|
38
|
+
return;
|
|
39
|
+
const point = last ? view.pointAt(last.clientX, last.clientY) : null;
|
|
40
|
+
options.onPointer?.(point);
|
|
41
|
+
const under = point ? marksAt(point) : [];
|
|
42
|
+
const key = under.map((mark) => mark.id).join("\n");
|
|
43
|
+
if (key === marksUnder)
|
|
44
|
+
return;
|
|
45
|
+
marksUnder = key;
|
|
46
|
+
options.onPointerMarks?.(under, point);
|
|
47
|
+
};
|
|
48
|
+
const reportSoon = () => {
|
|
49
|
+
if (!frame)
|
|
50
|
+
frame = requestAnimationFrame(report);
|
|
51
|
+
};
|
|
52
|
+
const dragTo = (start, clientX, clientY) => {
|
|
53
|
+
const size = view.pages[start.page]?.size ?? { width: 0, height: 0 };
|
|
54
|
+
const point = view.pointOn(start.page, clientX, clientY) ?? start;
|
|
55
|
+
return { x: clamp(point.x, 0, size.width), y: clamp(point.y, 0, size.height) };
|
|
56
|
+
};
|
|
57
|
+
const onDown = (event) => {
|
|
58
|
+
if (event.target instanceof Element && event.target.closest(".pm-float, .pm-attached, .pm-link"))
|
|
59
|
+
return;
|
|
60
|
+
if (event.pointerType === "touch") {
|
|
61
|
+
finger = event.isPrimary ? { pointerId: event.pointerId, clientX: event.clientX, clientY: event.clientY, downAt: event.timeStamp } : null;
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
if (event.button !== 0)
|
|
65
|
+
return;
|
|
66
|
+
const start = view.pointAt(event.clientX, event.clientY);
|
|
67
|
+
if (!start)
|
|
68
|
+
return;
|
|
69
|
+
const isOnText = event.target instanceof Element && event.target.closest(".pm-word") !== null;
|
|
70
|
+
press = { pointerId: event.pointerId, clientX: event.clientX, clientY: event.clientY, start, isDragging: false, isOnText };
|
|
71
|
+
/** CONSTRAINT: capturing the pointer would take the drag from the browser, which is the one selecting the text. */
|
|
72
|
+
if (!isOnText)
|
|
73
|
+
scroll.setPointerCapture(event.pointerId);
|
|
74
|
+
};
|
|
75
|
+
/**
|
|
76
|
+
* NOTE: heard before any element under the pointer, so a press with Space held reaches no word, link or attachment.
|
|
77
|
+
* The pane then holds the pointer, so the click that ends the press is the pane's and follows nothing under it.
|
|
78
|
+
*/
|
|
79
|
+
const onHandDown = (event) => {
|
|
80
|
+
if (!space || event.pointerType === "touch" || event.button !== 0)
|
|
81
|
+
return;
|
|
82
|
+
event.preventDefault();
|
|
83
|
+
event.stopImmediatePropagation();
|
|
84
|
+
space.isUsed = true;
|
|
85
|
+
hand = { pointerId: event.pointerId, clientX: event.clientX, clientY: event.clientY };
|
|
86
|
+
scroll.setPointerCapture(event.pointerId);
|
|
87
|
+
options.onPointer?.(null);
|
|
88
|
+
showHand();
|
|
89
|
+
};
|
|
90
|
+
const onHandUp = (event) => {
|
|
91
|
+
hand = null;
|
|
92
|
+
if (scroll.hasPointerCapture(event.pointerId))
|
|
93
|
+
scroll.releasePointerCapture(event.pointerId);
|
|
94
|
+
showHand();
|
|
95
|
+
reportSoon();
|
|
96
|
+
};
|
|
97
|
+
/** NOTE: with the pointer away from the pane, as for a reader on the keyboard alone, Space scrolls as it always has. */
|
|
98
|
+
const onKeyDown = (event) => {
|
|
99
|
+
if (event.key !== " " || event.target !== scroll || event.ctrlKey || event.metaKey || event.altKey)
|
|
100
|
+
return;
|
|
101
|
+
if (!space && (!last || isLastFromFinger))
|
|
102
|
+
return;
|
|
103
|
+
event.preventDefault();
|
|
104
|
+
if (space)
|
|
105
|
+
return;
|
|
106
|
+
space = { downAt: event.timeStamp, isUsed: false };
|
|
107
|
+
showHand();
|
|
108
|
+
};
|
|
109
|
+
const onKeyUp = (event) => {
|
|
110
|
+
if (event.key !== " " || !space)
|
|
111
|
+
return;
|
|
112
|
+
const held = space;
|
|
113
|
+
space = null;
|
|
114
|
+
showHand();
|
|
115
|
+
if (held.isUsed || event.timeStamp - held.downAt > TAP_MS)
|
|
116
|
+
return;
|
|
117
|
+
scroll.scrollBy({ top: (event.shiftKey ? -1 : 1) * scroll.clientHeight * SCREEN_STEP, behavior: glide() });
|
|
118
|
+
};
|
|
119
|
+
/** NOTE: Space let go while the pane had lost the keyboard is never heard, so losing it lets Space go. */
|
|
120
|
+
const onBlur = () => {
|
|
121
|
+
space = null;
|
|
122
|
+
showHand();
|
|
123
|
+
};
|
|
124
|
+
const onMove = (event) => {
|
|
125
|
+
if (event.pointerType === "touch")
|
|
126
|
+
return;
|
|
127
|
+
isLastFromFinger = false;
|
|
128
|
+
if (hand?.pointerId === event.pointerId) {
|
|
129
|
+
scroll.scrollBy({ left: hand.clientX - event.clientX, top: hand.clientY - event.clientY, behavior: "instant" });
|
|
130
|
+
hand.clientX = event.clientX;
|
|
131
|
+
hand.clientY = event.clientY;
|
|
132
|
+
}
|
|
133
|
+
last = { clientX: event.clientX, clientY: event.clientY };
|
|
134
|
+
if (isSelectingText || hand)
|
|
135
|
+
return;
|
|
136
|
+
if (!press || press.pointerId !== event.pointerId) {
|
|
137
|
+
reportSoon();
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
const from = { x: press.start.x, y: press.start.y };
|
|
141
|
+
if (!press.isDragging) {
|
|
142
|
+
if (Math.hypot(event.clientX - press.clientX, event.clientY - press.clientY) < DRAG_THRESHOLD_PX)
|
|
143
|
+
return;
|
|
144
|
+
if (press.isOnText) {
|
|
145
|
+
press = null;
|
|
146
|
+
isSelectingText = true;
|
|
147
|
+
options.onPointer?.(null);
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
press.isDragging = true;
|
|
151
|
+
options.onPointer?.(null);
|
|
152
|
+
options.onDrag?.({ phase: "start", page: press.start.page, from, to: from });
|
|
153
|
+
}
|
|
154
|
+
options.onDrag?.({ phase: "move", page: press.start.page, from, to: dragTo(press.start, event.clientX, event.clientY) });
|
|
155
|
+
};
|
|
156
|
+
const onTap = (event) => {
|
|
157
|
+
const tapped = finger;
|
|
158
|
+
finger = null;
|
|
159
|
+
if (event.type !== "pointerup" || !tapped || tapped.pointerId !== event.pointerId)
|
|
160
|
+
return;
|
|
161
|
+
if (event.timeStamp - tapped.downAt > TAP_MS || Math.hypot(event.clientX - tapped.clientX, event.clientY - tapped.clientY) > TAP_MOVE_PX)
|
|
162
|
+
return;
|
|
163
|
+
const point = view.pointAt(event.clientX, event.clientY);
|
|
164
|
+
last = point ? { clientX: event.clientX, clientY: event.clientY } : null;
|
|
165
|
+
isLastFromFinger = true;
|
|
166
|
+
if (point)
|
|
167
|
+
options.onPress?.(point, marksAt(point));
|
|
168
|
+
reportSoon();
|
|
169
|
+
};
|
|
170
|
+
const onUp = (event) => {
|
|
171
|
+
if (event.pointerType === "touch") {
|
|
172
|
+
onTap(event);
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
if (hand?.pointerId === event.pointerId) {
|
|
176
|
+
onHandUp(event);
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
if (!press || press.pointerId !== event.pointerId)
|
|
180
|
+
return;
|
|
181
|
+
const ended = press;
|
|
182
|
+
press = null;
|
|
183
|
+
if (scroll.hasPointerCapture(event.pointerId))
|
|
184
|
+
scroll.releasePointerCapture(event.pointerId);
|
|
185
|
+
if (ended.isDragging) {
|
|
186
|
+
const to = dragTo(ended.start, event.clientX, event.clientY);
|
|
187
|
+
options.onDrag?.({ phase: "end", page: ended.start.page, from: { x: ended.start.x, y: ended.start.y }, to });
|
|
188
|
+
}
|
|
189
|
+
else {
|
|
190
|
+
options.onPress?.(ended.start, marksAt(ended.start));
|
|
191
|
+
}
|
|
192
|
+
reportSoon();
|
|
193
|
+
};
|
|
194
|
+
/** NOTE: a selection may end with the pointer anywhere, so the lift is heard on the window and not on the pane alone. */
|
|
195
|
+
const onLift = () => {
|
|
196
|
+
if (!isSelectingText)
|
|
197
|
+
return;
|
|
198
|
+
isSelectingText = false;
|
|
199
|
+
reportSoon();
|
|
200
|
+
};
|
|
201
|
+
const onLeave = (event) => {
|
|
202
|
+
if (event.pointerType === "touch")
|
|
203
|
+
return;
|
|
204
|
+
last = null;
|
|
205
|
+
reportSoon();
|
|
206
|
+
};
|
|
207
|
+
scroll.addEventListener("pointerdown", onHandDown, { capture: true });
|
|
208
|
+
scroll.addEventListener("pointerdown", onDown);
|
|
209
|
+
scroll.addEventListener("pointermove", onMove);
|
|
210
|
+
scroll.addEventListener("pointerup", onUp);
|
|
211
|
+
scroll.addEventListener("pointercancel", onUp);
|
|
212
|
+
scroll.addEventListener("pointerleave", onLeave);
|
|
213
|
+
scroll.addEventListener("keydown", onKeyDown);
|
|
214
|
+
scroll.addEventListener("keyup", onKeyUp);
|
|
215
|
+
scroll.addEventListener("blur", onBlur);
|
|
216
|
+
window.addEventListener("pointerup", onLift);
|
|
217
|
+
window.addEventListener("pointercancel", onLift);
|
|
218
|
+
/** NOTE: when the pages move under a pointer that did not, where it is is reported again. A finger is gone once it has tapped, so its place is forgotten. */
|
|
219
|
+
const unwatch = view.onChange((reason) => {
|
|
220
|
+
if (reason !== "scroll")
|
|
221
|
+
return;
|
|
222
|
+
if (isLastFromFinger)
|
|
223
|
+
last = null;
|
|
224
|
+
report();
|
|
225
|
+
});
|
|
226
|
+
return {
|
|
227
|
+
destroy() {
|
|
228
|
+
unwatch();
|
|
229
|
+
cancelAnimationFrame(frame);
|
|
230
|
+
delete scroll.dataset.hand;
|
|
231
|
+
scroll.removeEventListener("pointerdown", onHandDown, { capture: true });
|
|
232
|
+
scroll.removeEventListener("pointerdown", onDown);
|
|
233
|
+
scroll.removeEventListener("pointermove", onMove);
|
|
234
|
+
scroll.removeEventListener("pointerup", onUp);
|
|
235
|
+
scroll.removeEventListener("pointercancel", onUp);
|
|
236
|
+
scroll.removeEventListener("pointerleave", onLeave);
|
|
237
|
+
scroll.removeEventListener("keydown", onKeyDown);
|
|
238
|
+
scroll.removeEventListener("keyup", onKeyUp);
|
|
239
|
+
scroll.removeEventListener("blur", onBlur);
|
|
240
|
+
window.removeEventListener("pointerup", onLift);
|
|
241
|
+
window.removeEventListener("pointercancel", onLift);
|
|
242
|
+
},
|
|
243
|
+
};
|
|
244
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { ViewerPage } from "./types.js";
|
|
2
|
+
import type { ViewPage } from "./view.js";
|
|
3
|
+
/**
|
|
4
|
+
* NOTE: the frame of pagemark's own viewer: the scrolling pane, one element per page in a column, and a layer for
|
|
5
|
+
* what floats beside a place. It holds no behaviour, and the parts lay their own layers over its pages.
|
|
6
|
+
*/
|
|
7
|
+
export type Stage = {
|
|
8
|
+
scroll: HTMLDivElement;
|
|
9
|
+
floats: HTMLDivElement;
|
|
10
|
+
pages: ReadonlyArray<StagePage>;
|
|
11
|
+
destroy(): void;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* NOTE: a page of the stage is two elements. `box` is what the column lays out, as large as the page is on the
|
|
15
|
+
* screen. `node` is the sheet inside it, as large as the page itself, which the parts draw in and the stylesheet
|
|
16
|
+
* turns. Unturned, the two are the same size.
|
|
17
|
+
*/
|
|
18
|
+
export type StagePage = ViewPage & {
|
|
19
|
+
readonly box: HTMLElement;
|
|
20
|
+
};
|
|
21
|
+
export declare function createStage(container: HTMLElement, pages: ReadonlyArray<ViewerPage>, label?: string): Stage;
|
package/viewer/stage.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { element } from "./dom.js";
|
|
2
|
+
export function createStage(container, pages, label) {
|
|
3
|
+
container.classList.add("pm-viewer");
|
|
4
|
+
const scroll = element("pm-scroll");
|
|
5
|
+
scroll.tabIndex = 0;
|
|
6
|
+
if (label !== undefined) {
|
|
7
|
+
scroll.setAttribute("role", "group");
|
|
8
|
+
scroll.setAttribute("aria-label", label);
|
|
9
|
+
}
|
|
10
|
+
const column = element("pm-column");
|
|
11
|
+
const floats = element("pm-floats");
|
|
12
|
+
scroll.append(column, floats);
|
|
13
|
+
const staged = pages.map((size, index) => {
|
|
14
|
+
const box = element("pm-page");
|
|
15
|
+
box.dataset.page = String(index);
|
|
16
|
+
const node = element("pm-sheet");
|
|
17
|
+
box.append(node);
|
|
18
|
+
column.append(box);
|
|
19
|
+
return { index, size, node, box };
|
|
20
|
+
});
|
|
21
|
+
container.replaceChildren(scroll);
|
|
22
|
+
return {
|
|
23
|
+
scroll,
|
|
24
|
+
floats,
|
|
25
|
+
pages: staged,
|
|
26
|
+
destroy: () => {
|
|
27
|
+
container.classList.remove("pm-viewer");
|
|
28
|
+
container.replaceChildren();
|
|
29
|
+
},
|
|
30
|
+
};
|
|
31
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { ViewerTextLine } from "./types.js";
|
|
2
|
+
/** NOTE: a place in a document's text: a word of a line of a page, and how many of its characters come before the place. */
|
|
3
|
+
export type TextPoint = {
|
|
4
|
+
page: number;
|
|
5
|
+
line: number;
|
|
6
|
+
word: number;
|
|
7
|
+
offset: number;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* NOTE: the text from one place to another, as a reader copies it: words apart by a space, lines and pages by a line
|
|
11
|
+
* break. It reads the lines the viewer was given, so it is whole even where a page between the two places is not drawn.
|
|
12
|
+
*/
|
|
13
|
+
export declare function textBetween(text: ReadonlyArray<ReadonlyArray<ViewerTextLine> | undefined>, from: TextPoint, to: TextPoint): string;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* NOTE: the text from one place to another, as a reader copies it: words apart by a space, lines and pages by a line
|
|
3
|
+
* break. It reads the lines the viewer was given, so it is whole even where a page between the two places is not drawn.
|
|
4
|
+
*/
|
|
5
|
+
export function textBetween(text, from, to) {
|
|
6
|
+
const lines = [];
|
|
7
|
+
for (let page = from.page; page <= to.page; page += 1) {
|
|
8
|
+
const pageLines = text[page] ?? [];
|
|
9
|
+
const firstLine = page === from.page ? from.line : 0;
|
|
10
|
+
const lastLine = page === to.page ? to.line : pageLines.length - 1;
|
|
11
|
+
for (let line = firstLine; line <= lastLine; line += 1) {
|
|
12
|
+
const words = pageLines[line]?.words ?? [];
|
|
13
|
+
const isFirst = page === from.page && line === from.line;
|
|
14
|
+
const isLast = page === to.page && line === to.line;
|
|
15
|
+
const firstWord = isFirst ? from.word : 0;
|
|
16
|
+
const lastWord = isLast ? to.word : words.length - 1;
|
|
17
|
+
const parts = [];
|
|
18
|
+
for (let word = firstWord; word <= lastWord; word += 1) {
|
|
19
|
+
const whole = words[word]?.text ?? "";
|
|
20
|
+
const start = isFirst && word === from.word ? from.offset : 0;
|
|
21
|
+
const end = isLast && word === to.word ? to.offset : whole.length;
|
|
22
|
+
parts.push(whole.slice(start, end));
|
|
23
|
+
}
|
|
24
|
+
lines.push(parts.join(" "));
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return lines.join("\n");
|
|
28
|
+
}
|
package/viewer/text.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { ViewerTextLine } from "./types.js";
|
|
2
|
+
import type { Part, View } from "./view.js";
|
|
3
|
+
type PagesText = ReadonlyArray<ReadonlyArray<ViewerTextLine> | undefined>;
|
|
4
|
+
/**
|
|
5
|
+
* NOTE: owns the text a reader can select: each word of a page as text of its own, laid exactly over the word in the
|
|
6
|
+
* page's picture and left unseen, so the browser's own selection, copy and screen reader work on a page that is an image.
|
|
7
|
+
* NOTE: a word's element holds the word alone, so what the browser lights is the word's own box. The spaces and line
|
|
8
|
+
* breaks a copy needs stand between the words, at no size.
|
|
9
|
+
* CONSTRAINT: only words take the pointer. Were the layer to take it too, a drag through a gap between lines or a
|
|
10
|
+
* margin would jump the selection to the page's first or last word.
|
|
11
|
+
*/
|
|
12
|
+
export type SelectableText = Part & {
|
|
13
|
+
/**
|
|
14
|
+
* NOTE: gives the pages text a reader can select and copy as on any web page, and a screen reader can read: one
|
|
15
|
+
* list of lines for each page, in page order. A page left out, or `null` for all of them, has none.
|
|
16
|
+
*/
|
|
17
|
+
setText(text: PagesText | null): void;
|
|
18
|
+
};
|
|
19
|
+
export declare function createText(view: View): SelectableText;
|
|
20
|
+
export {};
|
package/viewer/text.js
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import { pageFinder, pageLayer } from "./page-layers.js";
|
|
2
|
+
import { textBetween } from "./text-between.js";
|
|
3
|
+
/** NOTE: how far past the pane a page keeps its words, so a selection can run on while the pages scroll under it. */
|
|
4
|
+
const KEPT_AROUND = "100% 0px";
|
|
5
|
+
const MEASURED_AT_PX = 100;
|
|
6
|
+
const FONT = "sans-serif";
|
|
7
|
+
export function createText(view) {
|
|
8
|
+
let text = [];
|
|
9
|
+
const drawn = new Map();
|
|
10
|
+
const layers = new Map();
|
|
11
|
+
const pageIn = pageFinder(view.pages);
|
|
12
|
+
const near = new Set();
|
|
13
|
+
const widths = new Map();
|
|
14
|
+
const context = document.createElement("canvas").getContext("2d");
|
|
15
|
+
if (context)
|
|
16
|
+
context.font = `${MEASURED_AT_PX}px ${FONT}`;
|
|
17
|
+
/** NOTE: how wide a word is for each unit of its font size, in the font the layer is set in. */
|
|
18
|
+
const widthOf = (word) => {
|
|
19
|
+
let width = widths.get(word);
|
|
20
|
+
if (width === undefined) {
|
|
21
|
+
width = context ? context.measureText(word).width / MEASURED_AT_PX : word.length / 2;
|
|
22
|
+
widths.set(word, width);
|
|
23
|
+
}
|
|
24
|
+
return width;
|
|
25
|
+
};
|
|
26
|
+
const layerOf = (page) => {
|
|
27
|
+
let layer = layers.get(page.index);
|
|
28
|
+
if (!layer) {
|
|
29
|
+
layer = pageLayer(page, "text");
|
|
30
|
+
layers.set(page.index, layer);
|
|
31
|
+
}
|
|
32
|
+
return layer;
|
|
33
|
+
};
|
|
34
|
+
const clear = (index) => {
|
|
35
|
+
if (!drawn.has(index))
|
|
36
|
+
return;
|
|
37
|
+
layers.get(index)?.replaceChildren();
|
|
38
|
+
drawn.delete(index);
|
|
39
|
+
};
|
|
40
|
+
const draw = (index) => {
|
|
41
|
+
const page = view.pages[index];
|
|
42
|
+
const lines = text[index];
|
|
43
|
+
if (!page || !lines || lines.length === 0) {
|
|
44
|
+
clear(index);
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
if (drawn.get(index) === lines)
|
|
48
|
+
return;
|
|
49
|
+
const nodes = [];
|
|
50
|
+
lines.forEach((line, lineIndex) => {
|
|
51
|
+
line.words.forEach((word, wordIndex) => {
|
|
52
|
+
const natural = widthOf(word.text) * line.box.height;
|
|
53
|
+
const node = document.createElement("span");
|
|
54
|
+
node.className = "pm-word";
|
|
55
|
+
node.textContent = word.text;
|
|
56
|
+
node.dataset.line = String(lineIndex);
|
|
57
|
+
node.dataset.word = String(wordIndex);
|
|
58
|
+
node.style.left = `${(word.box.x / page.size.width) * 100}%`;
|
|
59
|
+
node.style.top = `${(line.box.y / page.size.height) * 100}%`;
|
|
60
|
+
node.style.fontSize = `calc(var(--pm-zoom) * ${line.box.height}px)`;
|
|
61
|
+
if (natural > 0)
|
|
62
|
+
node.style.transform = `scaleX(${word.box.width / natural})`;
|
|
63
|
+
nodes.push(node);
|
|
64
|
+
if (wordIndex < line.words.length - 1)
|
|
65
|
+
nodes.push(document.createTextNode(" "));
|
|
66
|
+
});
|
|
67
|
+
nodes.push(document.createElement("br"));
|
|
68
|
+
});
|
|
69
|
+
layerOf(page).replaceChildren(...nodes);
|
|
70
|
+
drawn.set(index, lines);
|
|
71
|
+
};
|
|
72
|
+
/** NOTE: the page whose text holds a node, or null for a node anywhere else, in a page's own content included. */
|
|
73
|
+
const pageOf = (node) => {
|
|
74
|
+
const layer = (node instanceof Element ? node : node?.parentElement)?.closest(".pm-text");
|
|
75
|
+
const page = layer ? pageIn(layer) : null;
|
|
76
|
+
return page && layers.get(page.index) === layer ? page : null;
|
|
77
|
+
};
|
|
78
|
+
/**
|
|
79
|
+
* NOTE: a page a selection starts or ends on keeps its words however far it scrolls, or the selection would go with
|
|
80
|
+
* them. That holds for a selection of nothing too: it is the place a Shift and a press further on extend from.
|
|
81
|
+
*/
|
|
82
|
+
const holdsSelectionEnd = (index) => {
|
|
83
|
+
const selection = document.getSelection();
|
|
84
|
+
const layer = layers.get(index);
|
|
85
|
+
if (!selection || !layer)
|
|
86
|
+
return false;
|
|
87
|
+
return layer.contains(selection.anchorNode) || layer.contains(selection.focusNode);
|
|
88
|
+
};
|
|
89
|
+
const sync = (index) => {
|
|
90
|
+
if (near.has(index))
|
|
91
|
+
draw(index);
|
|
92
|
+
else if (!holdsSelectionEnd(index))
|
|
93
|
+
clear(index);
|
|
94
|
+
};
|
|
95
|
+
const observer = new IntersectionObserver((entries) => {
|
|
96
|
+
for (const entry of entries) {
|
|
97
|
+
const page = pageIn(entry.target);
|
|
98
|
+
if (!page)
|
|
99
|
+
continue;
|
|
100
|
+
if (entry.isIntersecting)
|
|
101
|
+
near.add(page.index);
|
|
102
|
+
else
|
|
103
|
+
near.delete(page.index);
|
|
104
|
+
sync(page.index);
|
|
105
|
+
}
|
|
106
|
+
}, { root: view.pane, rootMargin: KEPT_AROUND });
|
|
107
|
+
for (const page of view.pages)
|
|
108
|
+
observer.observe(page.node);
|
|
109
|
+
const onSelectionChange = () => {
|
|
110
|
+
for (const index of [...drawn.keys()])
|
|
111
|
+
if (!near.has(index))
|
|
112
|
+
sync(index);
|
|
113
|
+
};
|
|
114
|
+
/** NOTE: a press anywhere but on a word puts the selection down, as a click beside a paragraph does. */
|
|
115
|
+
const onDown = (event) => {
|
|
116
|
+
if (event.target instanceof Element && event.target.closest(".pm-word, .pm-float, .pm-attached, .pm-link"))
|
|
117
|
+
return;
|
|
118
|
+
const selection = document.getSelection();
|
|
119
|
+
if (selection && !selection.isCollapsed && pageOf(selection.anchorNode) && pageOf(selection.focusNode))
|
|
120
|
+
selection.removeAllRanges();
|
|
121
|
+
};
|
|
122
|
+
const wordsOf = (page) => [...(layers.get(page.index)?.querySelectorAll(".pm-word") ?? [])];
|
|
123
|
+
const pointAt = (page, node, offset) => ({
|
|
124
|
+
page: page.index,
|
|
125
|
+
line: Number(node.dataset.line),
|
|
126
|
+
word: Number(node.dataset.word),
|
|
127
|
+
offset,
|
|
128
|
+
});
|
|
129
|
+
/** NOTE: how many of a word's characters come before an end of the selection; `otherwise` when that end lies outside the word. */
|
|
130
|
+
const charsBefore = (word, container, offset, otherwise) => {
|
|
131
|
+
if (container === word)
|
|
132
|
+
return offset === 0 ? 0 : (word.textContent ?? "").length;
|
|
133
|
+
return word.contains(container) ? offset : otherwise;
|
|
134
|
+
};
|
|
135
|
+
/**
|
|
136
|
+
* NOTE: what a copy takes is read from the lines the viewer was given, not from the page: the pages between a
|
|
137
|
+
* selection's two ends give their words back as they scroll away, so the page alone would copy a selection with a hole.
|
|
138
|
+
*/
|
|
139
|
+
const onCopy = (event) => {
|
|
140
|
+
const selection = document.getSelection();
|
|
141
|
+
if (!selection || selection.rangeCount === 0 || selection.isCollapsed || !event.clipboardData)
|
|
142
|
+
return;
|
|
143
|
+
const range = selection.getRangeAt(0);
|
|
144
|
+
const firstPage = pageOf(range.startContainer);
|
|
145
|
+
const lastPage = pageOf(range.endContainer);
|
|
146
|
+
if (!firstPage || !lastPage)
|
|
147
|
+
return;
|
|
148
|
+
const first = wordsOf(firstPage).find((node) => range.intersectsNode(node));
|
|
149
|
+
const last = wordsOf(lastPage).findLast((node) => range.intersectsNode(node));
|
|
150
|
+
if (!first || !last)
|
|
151
|
+
return;
|
|
152
|
+
const from = pointAt(firstPage, first, charsBefore(first, range.startContainer, range.startOffset, 0));
|
|
153
|
+
const to = pointAt(lastPage, last, charsBefore(last, range.endContainer, range.endOffset, (last.textContent ?? "").length));
|
|
154
|
+
event.clipboardData.setData("text/plain", textBetween(text, from, to));
|
|
155
|
+
event.preventDefault();
|
|
156
|
+
};
|
|
157
|
+
document.addEventListener("selectionchange", onSelectionChange);
|
|
158
|
+
view.pane.addEventListener("pointerdown", onDown);
|
|
159
|
+
view.pane.addEventListener("copy", onCopy);
|
|
160
|
+
return {
|
|
161
|
+
setText(next) {
|
|
162
|
+
text = next ?? [];
|
|
163
|
+
for (const index of [...drawn.keys()])
|
|
164
|
+
if (text[index] !== drawn.get(index))
|
|
165
|
+
clear(index);
|
|
166
|
+
for (const index of near)
|
|
167
|
+
draw(index);
|
|
168
|
+
},
|
|
169
|
+
destroy() {
|
|
170
|
+
observer.disconnect();
|
|
171
|
+
document.removeEventListener("selectionchange", onSelectionChange);
|
|
172
|
+
view.pane.removeEventListener("pointerdown", onDown);
|
|
173
|
+
view.pane.removeEventListener("copy", onCopy);
|
|
174
|
+
for (const layer of layers.values())
|
|
175
|
+
layer.remove();
|
|
176
|
+
layers.clear();
|
|
177
|
+
drawn.clear();
|
|
178
|
+
},
|
|
179
|
+
};
|
|
180
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { Box } from "../core/index.js";
|
|
2
|
+
import type { ViewerPage } from "./types.js";
|
|
3
|
+
/** NOTE: the side of a tile in the pixels it is drawn with; its edge tiles are smaller, ending where the page ends. */
|
|
4
|
+
export declare const TILE_PIXELS = 512;
|
|
5
|
+
/** NOTE: one cell of the grid laid over a page at a scale: where it is in the grid, and the part of the page it shows, in page units. */
|
|
6
|
+
export type Tile = {
|
|
7
|
+
column: number;
|
|
8
|
+
row: number;
|
|
9
|
+
box: Box;
|
|
10
|
+
};
|
|
11
|
+
/** NOTE: the scale tiles are drawn at to have `needed` pixels for each page unit, and no sharper than `sharpest`, past which a page has no more to show. */
|
|
12
|
+
export declare function tileScale(needed: number, sharpest?: number): number;
|
|
13
|
+
/** NOTE: the sharpest of `scales`, up to `wanted`, at which a whole page is within `maxPixels`; the lowest when none is. */
|
|
14
|
+
export declare function heldScale(scales: ReadonlyArray<number>, wanted: number, page: ViewerPage, maxPixels: number): number;
|
|
15
|
+
/**
|
|
16
|
+
* NOTE: the tiles of a page drawn at `scale` that show some of `view`, a box in page units that may hang off the
|
|
17
|
+
* page, row by row. A tile's box depends on the page and the scale alone, so the same tile can be asked for again and
|
|
18
|
+
* found in a cache.
|
|
19
|
+
*/
|
|
20
|
+
export declare function tilesOver(page: ViewerPage, scale: number, view: Box): Tile[];
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/** NOTE: the side of a tile in the pixels it is drawn with; its edge tiles are smaller, ending where the page ends. */
|
|
2
|
+
export const TILE_PIXELS = 512;
|
|
3
|
+
/** NOTE: each step is at most half as sharp again as the one before, so a tile is never drawn with much more than twice the pixels the screen can show. */
|
|
4
|
+
const TILE_SCALES = [0.5, 0.75, 1, 1.5, 2, 3, 4, 6, 8, 12, 16, 24, 32, 48, 64];
|
|
5
|
+
const SAME = 0.01;
|
|
6
|
+
const HALF_A_PIXEL = 0.5;
|
|
7
|
+
/** NOTE: the scale tiles are drawn at to have `needed` pixels for each page unit, and no sharper than `sharpest`, past which a page has no more to show. */
|
|
8
|
+
export function tileScale(needed, sharpest = Number.POSITIVE_INFINITY) {
|
|
9
|
+
const lowest = TILE_SCALES[0] ?? 1;
|
|
10
|
+
const allowed = TILE_SCALES.filter((scale) => scale <= sharpest + SAME);
|
|
11
|
+
return allowed.find((scale) => scale >= needed - SAME) ?? allowed[allowed.length - 1] ?? lowest;
|
|
12
|
+
}
|
|
13
|
+
/** NOTE: the sharpest of `scales`, up to `wanted`, at which a whole page is within `maxPixels`; the lowest when none is. */
|
|
14
|
+
export function heldScale(scales, wanted, page, maxPixels) {
|
|
15
|
+
const fitting = scales.filter((scale) => scale <= wanted + SAME && page.width * scale * page.height * scale <= maxPixels);
|
|
16
|
+
return fitting[fitting.length - 1] ?? scales[0] ?? 1;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* NOTE: the tiles of a page drawn at `scale` that show some of `view`, a box in page units that may hang off the
|
|
20
|
+
* page, row by row. A tile's box depends on the page and the scale alone, so the same tile can be asked for again and
|
|
21
|
+
* found in a cache.
|
|
22
|
+
*/
|
|
23
|
+
export function tilesOver(page, scale, view) {
|
|
24
|
+
const side = TILE_PIXELS / scale;
|
|
25
|
+
const isWorthDrawing = (start, most) => (most - start) * scale >= HALF_A_PIXEL;
|
|
26
|
+
const first = (from) => Math.max(0, Math.floor(from / side));
|
|
27
|
+
const tiles = [];
|
|
28
|
+
for (let row = first(view.y); row * side < Math.min(page.height, view.y + view.height) && isWorthDrawing(row * side, page.height); row += 1) {
|
|
29
|
+
for (let column = first(view.x); column * side < Math.min(page.width, view.x + view.width) && isWorthDrawing(column * side, page.width); column += 1) {
|
|
30
|
+
const x = column * side;
|
|
31
|
+
const y = row * side;
|
|
32
|
+
tiles.push({ column, row, box: { x, y, width: Math.min(side, page.width - x), height: Math.min(side, page.height - y) } });
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return tiles;
|
|
36
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { ViewerOptions } from "./types.js";
|
|
2
|
+
import type { View } from "./view.js";
|
|
3
|
+
/**
|
|
4
|
+
* NOTE: keeps the part of a page on screen sharp when a whole picture of the page no longer can be: it lays tiles
|
|
5
|
+
* over the page's picture, only where the pane is, and gives them back as the pane moves on. What it holds is bounded
|
|
6
|
+
* by the pane, however far in the zoom is and however large the page.
|
|
7
|
+
*/
|
|
8
|
+
export type Tiles = {
|
|
9
|
+
/** NOTE: `needed` is the pixels for each page unit the screen can show; `held` is the scale a page's whole picture is kept at. */
|
|
10
|
+
cover(pages: Iterable<number>, needed: number, held: (page: number) => number): void;
|
|
11
|
+
destroy(): void;
|
|
12
|
+
};
|
|
13
|
+
type TileOptions = Required<Pick<ViewerOptions, "tileUrl">> & Pick<ViewerOptions, "releaseImage">;
|
|
14
|
+
export declare function createTiles(view: View, options: TileOptions): Tiles;
|
|
15
|
+
export {};
|