@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.
Files changed (150) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +25 -2
  3. package/behaviours/behaviour.d.ts +43 -0
  4. package/behaviours/behaviour.js +84 -0
  5. package/behaviours/comparison.d.ts +62 -0
  6. package/behaviours/comparison.js +129 -0
  7. package/behaviours/document-viewer.d.ts +9 -0
  8. package/behaviours/document-viewer.js +9 -0
  9. package/behaviours/hidden.d.ts +24 -0
  10. package/behaviours/hidden.js +38 -0
  11. package/behaviours/hover.d.ts +34 -0
  12. package/behaviours/hover.js +49 -0
  13. package/behaviours/index.d.ts +24 -0
  14. package/behaviours/index.js +14 -0
  15. package/behaviours/linked-viewers.d.ts +14 -0
  16. package/behaviours/linked-viewers.js +25 -0
  17. package/behaviours/links.d.ts +4 -0
  18. package/behaviours/links.js +4 -0
  19. package/behaviours/marks.d.ts +64 -0
  20. package/behaviours/marks.js +33 -0
  21. package/behaviours/quotes.d.ts +7 -0
  22. package/behaviours/quotes.js +9 -0
  23. package/behaviours/region.d.ts +27 -0
  24. package/behaviours/region.js +40 -0
  25. package/behaviours/search.d.ts +38 -0
  26. package/behaviours/search.js +63 -0
  27. package/behaviours/selection.d.ts +30 -0
  28. package/behaviours/selection.js +41 -0
  29. package/behaviours/text.d.ts +8 -0
  30. package/behaviours/text.js +24 -0
  31. package/behaviours/viewer-state.d.ts +36 -0
  32. package/behaviours/viewer-state.js +58 -0
  33. package/browser/blob-pieces.d.ts +9 -0
  34. package/browser/blob-pieces.js +50 -0
  35. package/browser/browser-source.d.ts +25 -0
  36. package/browser/browser-source.js +62 -0
  37. package/browser/index.d.ts +2 -0
  38. package/browser/index.js +1 -0
  39. package/browser/local-pdf.d.ts +27 -0
  40. package/browser/local-pdf.js +61 -0
  41. package/browser/worker-engine.d.ts +21 -0
  42. package/browser/worker-engine.js +1 -0
  43. package/browser/worker.d.ts +1 -0
  44. package/browser/worker.js +51 -0
  45. package/core/compare.d.ts +55 -0
  46. package/core/compare.js +350 -0
  47. package/core/find.d.ts +12 -0
  48. package/core/find.js +177 -0
  49. package/core/hidden.d.ts +12 -0
  50. package/core/hidden.js +26 -0
  51. package/core/index.d.ts +11 -0
  52. package/core/index.js +6 -0
  53. package/core/links.d.ts +7 -0
  54. package/core/links.js +19 -0
  55. package/core/locate.d.ts +21 -0
  56. package/core/locate.js +140 -0
  57. package/core/paragraphs.d.ts +37 -0
  58. package/core/paragraphs.js +141 -0
  59. package/core/types.d.ts +89 -0
  60. package/core/types.js +1 -0
  61. package/document/document.d.ts +83 -0
  62. package/document/document.js +215 -0
  63. package/document/handover-source.d.ts +18 -0
  64. package/document/handover-source.js +39 -0
  65. package/document/index.d.ts +10 -0
  66. package/document/index.js +5 -0
  67. package/document/reading-source.d.ts +7 -0
  68. package/document/reading-source.js +16 -0
  69. package/document/server-source.d.ts +10 -0
  70. package/document/server-source.js +23 -0
  71. package/document/source.d.ts +21 -0
  72. package/document/source.js +1 -0
  73. package/document/split-source.d.ts +9 -0
  74. package/document/split-source.js +25 -0
  75. package/document/store.d.ts +12 -0
  76. package/document/store.js +21 -0
  77. package/engine/engine-thread.d.ts +10 -0
  78. package/engine/engine-thread.js +1 -0
  79. package/engine/index.d.ts +2 -0
  80. package/engine/index.js +1 -0
  81. package/engine/pdfium.d.ts +16 -0
  82. package/engine/pdfium.js +222 -0
  83. package/engine/remote.d.ts +51 -0
  84. package/engine/remote.js +109 -0
  85. package/package.json +65 -4
  86. package/react/hooks.d.ts +128 -0
  87. package/react/hooks.js +129 -0
  88. package/react/index.d.ts +3 -0
  89. package/react/index.js +2 -0
  90. package/react/viewer.d.ts +48 -0
  91. package/react/viewer.js +127 -0
  92. package/reader/assemble.d.ts +4 -0
  93. package/reader/assemble.js +29 -0
  94. package/reader/index.d.ts +7 -0
  95. package/reader/index.js +5 -0
  96. package/reader/layer-page.d.ts +30 -0
  97. package/reader/layer-page.js +40 -0
  98. package/reader/layer-words.d.ts +4 -0
  99. package/reader/layer-words.js +102 -0
  100. package/reader/letter-edges.d.ts +13 -0
  101. package/reader/letter-edges.js +60 -0
  102. package/reader/links.d.ts +7 -0
  103. package/reader/links.js +22 -0
  104. package/reader/seams.d.ts +92 -0
  105. package/reader/seams.js +3 -0
  106. package/reader/seen.d.ts +16 -0
  107. package/reader/seen.js +67 -0
  108. package/reader/trust.d.ts +9 -0
  109. package/reader/trust.js +34 -0
  110. package/viewer/attachments.d.ts +11 -0
  111. package/viewer/attachments.js +104 -0
  112. package/viewer/dom.d.ts +14 -0
  113. package/viewer/dom.js +38 -0
  114. package/viewer/events.d.ts +17 -0
  115. package/viewer/events.js +20 -0
  116. package/viewer/index.d.ts +17 -0
  117. package/viewer/index.js +8 -0
  118. package/viewer/links.d.ts +16 -0
  119. package/viewer/links.js +59 -0
  120. package/viewer/marks.d.ts +24 -0
  121. package/viewer/marks.js +171 -0
  122. package/viewer/page-layers.d.ts +13 -0
  123. package/viewer/page-layers.js +33 -0
  124. package/viewer/pictures.d.ts +24 -0
  125. package/viewer/pictures.js +293 -0
  126. package/viewer/pointer.d.ts +21 -0
  127. package/viewer/pointer.js +244 -0
  128. package/viewer/stage.d.ts +21 -0
  129. package/viewer/stage.js +31 -0
  130. package/viewer/text-between.d.ts +13 -0
  131. package/viewer/text-between.js +28 -0
  132. package/viewer/text.d.ts +20 -0
  133. package/viewer/text.js +180 -0
  134. package/viewer/tile-grid.d.ts +20 -0
  135. package/viewer/tile-grid.js +36 -0
  136. package/viewer/tiles.d.ts +15 -0
  137. package/viewer/tiles.js +190 -0
  138. package/viewer/turn.d.ts +18 -0
  139. package/viewer/turn.js +40 -0
  140. package/viewer/types.d.ts +238 -0
  141. package/viewer/types.js +1 -0
  142. package/viewer/view-over.d.ts +21 -0
  143. package/viewer/view-over.js +191 -0
  144. package/viewer/view.d.ts +59 -0
  145. package/viewer/view.js +1 -0
  146. package/viewer/viewer.css +301 -0
  147. package/viewer/viewer.d.ts +11 -0
  148. package/viewer/viewer.js +104 -0
  149. package/viewer/viewport.d.ts +37 -0
  150. package/viewer/viewport.js +429 -0
@@ -0,0 +1,37 @@
1
+ import type { Box } from "../core/index.js";
2
+ import type { Stage } from "./stage.js";
3
+ import type { ScreenBox, ViewerFit, ViewerRotation, ViewerSpread } from "./types.js";
4
+ import type { View } from "./view.js";
5
+ export declare const MIN_ZOOM = 0.15;
6
+ export declare const MAX_ZOOM = 8;
7
+ /**
8
+ * NOTE: the view of pagemark's own viewer. It owns where the pages are: their layout, the zoom, the scroll position,
9
+ * and every conversion between a place on a page and a position on the screen. The parts ask it, as they would ask
10
+ * any view, and none of them reads page offsets itself.
11
+ */
12
+ export type Viewport = View & {
13
+ toScreen(page: number, box: Box): ScreenBox | null;
14
+ zoomIn(): void;
15
+ zoomOut(): void;
16
+ zoomTo(zoom: number, anchor?: {
17
+ x: number;
18
+ y: number;
19
+ }): void;
20
+ fitWidth(): void;
21
+ fitPage(): void;
22
+ readonly fit: ViewerFit | null;
23
+ rotateTo(rotation: ViewerRotation): void;
24
+ readonly rotation: ViewerRotation;
25
+ setSpread(spread: ViewerSpread): void;
26
+ readonly spread: ViewerSpread;
27
+ /** NOTE: moves the pages under the pane by a distance in page units, at once. */
28
+ moveBy(x: number, y: number): void;
29
+ /** NOTE: the page at the middle of the pane, or -1 before it was first worked out. */
30
+ readonly pageInView: number;
31
+ reportPageInView(): void;
32
+ destroy(): void;
33
+ };
34
+ export declare function createViewport(stage: Stage, options: {
35
+ onPageInView?: (page: number) => void;
36
+ fit?: ViewerFit;
37
+ }): Viewport;
@@ -0,0 +1,429 @@
1
+ import { clamp, glide } from "./dom.js";
2
+ import { turnedBox, turnedSize, unturnedBox, unturnedPoint } from "./turn.js";
3
+ export const MIN_ZOOM = 0.15;
4
+ export const MAX_ZOOM = 8;
5
+ const ZOOM_STEP = 1.25;
6
+ const COLUMN_PADDING = 24;
7
+ /** CONSTRAINT: the space the stylesheet leaves between two pages, which does not grow with the zoom. */
8
+ const PAGE_GAP = 16;
9
+ /** NOTE: a move the viewer makes itself is over once no scroll has been heard for this long. */
10
+ const OWN_MOVE_REST_MS = 200;
11
+ /**
12
+ * NOTE: a move of the viewer's own further than this many panes lands at once. Gliding past dozens of pages shows
13
+ * the reader nothing, and the page they asked for is not drawn sharp until the glide ends.
14
+ */
15
+ const GLIDE_WITHIN_PANES = 2;
16
+ export function createViewport(stage, options) {
17
+ const { scroll, pages } = stage;
18
+ const listeners = new Set();
19
+ const STILL = { x: 0, y: 0, byReader: true };
20
+ let zoom = 1;
21
+ /** INVARIANT: every change of this is followed at once by a zoom, which is how a listener hears of it. */
22
+ let fit = options.fit ?? "width";
23
+ /** INVARIANT: like `fit`, every change of this is followed at once by a zoom being told. */
24
+ let rotation = 0;
25
+ /** INVARIANT: like `fit`, every change of this is followed at once by a zoom being told. */
26
+ let spread = "single";
27
+ let scrollFrame = 0;
28
+ let pageInView = -1;
29
+ let lastScrollAt = 0;
30
+ /** NOTE: where the pane was when a scroll was last told of, so the next one can say how far it went. */
31
+ let told = { left: 0, top: 0 };
32
+ let isOwnMove = false;
33
+ let ownMoveTimer;
34
+ /** NOTE: true once the pane has been laid out at a size; until then the pages have no place to scroll to. */
35
+ let isLaidOut = false;
36
+ /**
37
+ * NOTE: a move asked for before the pane was laid out, made at once when it is. A scroll asked of a pane with no
38
+ * size goes nowhere, and one asked before the first layout is undone by the zoom fitted to the pane, so neither is
39
+ * tried until then.
40
+ */
41
+ let sentTo = null;
42
+ const notify = (reason, shift = STILL) => {
43
+ for (const listener of [...listeners])
44
+ listener(reason, shift);
45
+ };
46
+ /** NOTE: a scroll the viewer starts itself, at once or gliding, is its own until the pane has come to rest. */
47
+ const ownMove = () => {
48
+ isOwnMove = true;
49
+ clearTimeout(ownMoveTimer);
50
+ ownMoveTimer = setTimeout(() => {
51
+ isOwnMove = false;
52
+ }, OWN_MOVE_REST_MS);
53
+ };
54
+ /** NOTE: a page as it stands on the screen: as wide as it is high once turned a quarter. */
55
+ const shown = (page) => turnedSize(page.size, rotation);
56
+ const across = () => (spread === "double" ? 2 : 1);
57
+ const rowCount = () => Math.ceil(pages.length / across());
58
+ /** NOTE: the pages that stand side by side, by the row's place from the top: one page, or a pair of an open book. */
59
+ const row = (order) => pages.slice(order * across(), (order + 1) * across());
60
+ const topOf = (order) => Math.min(...row(order).map((page) => page.box.offsetTop));
61
+ /** NOTE: where the pane scrolls to for a page: its row a little below the top, so the page's edge shows. */
62
+ const scrollTopFor = (index) => topOf(Math.floor(index / across())) - COLUMN_PADDING / 2;
63
+ const hasSize = () => scroll.clientWidth > 0 && scroll.clientHeight > 0;
64
+ /** NOTE: the viewer's own move to a place in the content: a glide when it is near, at once when it is far. */
65
+ const moveTo = (place) => {
66
+ ownMove();
67
+ const isFar = Math.abs(place.top - scroll.scrollTop) > scroll.clientHeight * GLIDE_WITHIN_PANES;
68
+ scroll.scrollTo({ ...place, behavior: isFar ? "instant" : glide() });
69
+ };
70
+ /**
71
+ * NOTE: the widest page of each side and the tallest page of all decide, so every row fits at the one zoom the
72
+ * pages share. Two to a row, the pages of a pair meet at one line down the middle, so the room the pages need is
73
+ * the widest left page beside the widest right page, though no row may hold both.
74
+ * NOTE: the space between the two pages of a pair stays as it is at every zoom, so it is taken from the pane
75
+ * before the pages share it.
76
+ */
77
+ const fittedZoom = (held) => {
78
+ const sides = Array.from({ length: Math.min(across(), pages.length) }, (_, side) => Math.max(1, ...pages.filter((page) => page.index % across() === side).map((page) => shown(page).width)));
79
+ const width = scroll.clientWidth - COLUMN_PADDING * 2 - Math.max(0, sides.length - 1) * PAGE_GAP;
80
+ const height = scroll.clientHeight - COLUMN_PADDING * 2;
81
+ const byWidth = width > 0 ? width / Math.max(1, sides.reduce((sum, side) => sum + side, 0)) : 1;
82
+ return held === "page" && height > 0 ? Math.min(byWidth, height / Math.max(1, ...pages.map((page) => shown(page).height))) : byWidth;
83
+ };
84
+ const layout = () => {
85
+ scroll.style.setProperty("--pm-zoom", String(zoom));
86
+ scroll.dataset.rotation = String(rotation);
87
+ scroll.dataset.spread = spread;
88
+ for (const page of pages) {
89
+ const size = shown(page);
90
+ page.box.style.width = `${size.width * zoom}px`;
91
+ page.box.style.height = `${size.height * zoom}px`;
92
+ page.node.style.width = `${page.size.width * zoom}px`;
93
+ page.node.style.height = `${page.size.height * zoom}px`;
94
+ }
95
+ };
96
+ /**
97
+ * NOTE: the page at a place in the content: of the last row that starts at or above it, the page it is over or
98
+ * nearer to. Rows start further down one after another, so the row is found by halving.
99
+ */
100
+ const pageAt = (contentX, contentY) => {
101
+ let [low, high] = [0, rowCount() - 1];
102
+ while (low < high) {
103
+ const middle = Math.ceil((low + high) / 2);
104
+ if (topOf(middle) <= contentY)
105
+ low = middle;
106
+ else
107
+ high = middle - 1;
108
+ }
109
+ const [left, right] = row(low);
110
+ if (!left)
111
+ return 0;
112
+ return right && contentX >= (left.box.offsetLeft + left.box.offsetWidth + right.box.offsetLeft) / 2 ? right.index : left.index;
113
+ };
114
+ const pointOn = (index, clientX, clientY) => {
115
+ const page = pages[index];
116
+ if (!page)
117
+ return null;
118
+ const bounds = scroll.getBoundingClientRect();
119
+ const across = (clientX - bounds.left + scroll.scrollLeft - page.box.offsetLeft) / zoom;
120
+ const down = (clientY - bounds.top + scroll.scrollTop - page.box.offsetTop) / zoom;
121
+ return { page: index, ...unturnedPoint(page.size, rotation, { x: across, y: down }) };
122
+ };
123
+ const pointAt = (clientX, clientY) => {
124
+ const bounds = scroll.getBoundingClientRect();
125
+ const point = pointOn(pageAt(clientX - bounds.left + scroll.scrollLeft, clientY - bounds.top + scroll.scrollTop), clientX, clientY);
126
+ const size = point ? pages[point.page]?.size : undefined;
127
+ if (!point || !size)
128
+ return null;
129
+ return point.x >= 0 && point.y >= 0 && point.x <= size.width && point.y <= size.height ? point : null;
130
+ };
131
+ const inContent = (index, box) => {
132
+ const page = pages[index];
133
+ if (!page)
134
+ return null;
135
+ const turned = turnedBox(page.size, rotation, box);
136
+ return { x: page.box.offsetLeft + turned.x * zoom, y: page.box.offsetTop + turned.y * zoom, width: turned.width * zoom, height: turned.height * zoom };
137
+ };
138
+ const inPage = (index, content) => {
139
+ const page = pages[index];
140
+ if (!page)
141
+ return null;
142
+ const turned = { x: (content.x - page.box.offsetLeft) / zoom, y: (content.y - page.box.offsetTop) / zoom, width: content.width / zoom, height: content.height / zoom };
143
+ return unturnedBox(page.size, rotation, turned);
144
+ };
145
+ /**
146
+ * NOTE: of a pair, the page in view is the left one, whichever of the two the middle of the pane is over.
147
+ * CONSTRAINT: a pane with no size has no middle, and its pages all stand at the top, so it has no page in view.
148
+ */
149
+ const reportPageInView = () => {
150
+ if (!hasSize())
151
+ return;
152
+ const index = pageAt(0, scroll.scrollTop + scroll.clientHeight / 2);
153
+ if (index === pageInView)
154
+ return;
155
+ pageInView = index;
156
+ options.onPageInView?.(index);
157
+ };
158
+ /**
159
+ * NOTE: what follows every new layout. Keeping a place through it may have scrolled the pane, which is not the
160
+ * pages moving, and the page at the middle of the pane may be another though the pane did not scroll at all.
161
+ */
162
+ const settle = () => {
163
+ told = { left: scroll.scrollLeft, top: scroll.scrollTop };
164
+ reportPageInView();
165
+ notify("zoom");
166
+ };
167
+ /** NOTE: the page point under `anchor` stays under it, so a zoom never moves what the reader is looking at. */
168
+ const applyZoom = (next, anchor) => {
169
+ const at = anchor ?? { x: scroll.clientWidth / 2, y: scroll.clientHeight / 2 };
170
+ const held = pages[pageAt(scroll.scrollLeft + at.x, scroll.scrollTop + at.y)];
171
+ const unitX = held ? (scroll.scrollLeft + at.x - held.box.offsetLeft) / zoom : 0;
172
+ const unitY = held ? (scroll.scrollTop + at.y - held.box.offsetTop) / zoom : 0;
173
+ zoom = clamp(next, MIN_ZOOM, MAX_ZOOM);
174
+ layout();
175
+ if (held) {
176
+ scroll.scrollLeft = held.box.offsetLeft + unitX * zoom - at.x;
177
+ scroll.scrollTop = held.box.offsetTop + unitY * zoom - at.y;
178
+ }
179
+ settle();
180
+ };
181
+ /** NOTE: fitting a page is for seeing one whole, so the page in view is put in the middle of the pane as well as made to fit it. */
182
+ const fitToPage = () => {
183
+ const held = pages[pageInView] ?? pages[pageAt(0, scroll.scrollTop + scroll.clientHeight / 2)];
184
+ applyZoom(fittedZoom("page"));
185
+ if (!held)
186
+ return;
187
+ ownMove();
188
+ scroll.scrollTop = held.box.offsetTop - (scroll.clientHeight - shown(held).height * zoom) / 2;
189
+ };
190
+ /**
191
+ * NOTE: lays the pages out another way: turned, or two to a row. The point of a page at the middle of the pane is
192
+ * there again afterwards, as a zoom keeps the point under its anchor. A zoom held to the pane is fitted again,
193
+ * since the pages now take another shape.
194
+ */
195
+ const layOutAgain = (change) => {
196
+ const middle = { x: scroll.clientWidth / 2, y: scroll.clientHeight / 2 };
197
+ const bounds = scroll.getBoundingClientRect();
198
+ const kept = pointOn(pageAt(scroll.scrollLeft + middle.x, scroll.scrollTop + middle.y), bounds.left + middle.x, bounds.top + middle.y);
199
+ change();
200
+ if (fit === "page") {
201
+ fitToPage();
202
+ }
203
+ else {
204
+ if (fit)
205
+ zoom = clamp(fittedZoom(fit), MIN_ZOOM, MAX_ZOOM);
206
+ layout();
207
+ const place = kept ? inContent(kept.page, { x: kept.x, y: kept.y, width: 0, height: 0 }) : null;
208
+ if (place) {
209
+ scroll.scrollLeft = place.x - middle.x;
210
+ scroll.scrollTop = place.y - middle.y;
211
+ }
212
+ settle();
213
+ }
214
+ };
215
+ const onWheel = (event) => {
216
+ if (!event.ctrlKey && !event.metaKey)
217
+ return;
218
+ event.preventDefault();
219
+ const bounds = scroll.getBoundingClientRect();
220
+ const delta = clamp(event.deltaY, -40, 40);
221
+ fit = null;
222
+ applyZoom(zoom * Math.exp(-delta * 0.01), { x: event.clientX - bounds.left, y: event.clientY - bounds.top });
223
+ };
224
+ /** NOTE: two fingers down: how far apart they were and the zoom at that moment, and where the middle between them was last. */
225
+ let pinch = null;
226
+ const between = (event) => {
227
+ const [a, b] = [event.touches[0], event.touches[1]];
228
+ if (!a || !b)
229
+ return null;
230
+ const bounds = scroll.getBoundingClientRect();
231
+ return { distance: Math.hypot(a.clientX - b.clientX, a.clientY - b.clientY), x: (a.clientX + b.clientX) / 2 - bounds.left, y: (a.clientY + b.clientY) / 2 - bounds.top };
232
+ };
233
+ const onTouchStart = (event) => {
234
+ const span = event.touches.length === 2 ? between(event) : null;
235
+ pinch = span && span.distance > 0 ? { ...span, zoom } : null;
236
+ };
237
+ /**
238
+ * NOTE: two fingers zoom about the place between them, and carry the pages along when they move together. One
239
+ * finger is left to the browser, which scrolls.
240
+ * CONSTRAINT: the browser's own two-finger zoom is turned off with `touch-action` in the stylesheet, and its
241
+ * two-finger scroll by cancelling the move; a move it will not let be cancelled is one it is already scrolling.
242
+ */
243
+ const onTouchMove = (event) => {
244
+ const span = pinch && event.touches.length === 2 ? between(event) : null;
245
+ if (!pinch || !span)
246
+ return;
247
+ if (event.cancelable) {
248
+ event.preventDefault();
249
+ scroll.scrollLeft -= span.x - pinch.x;
250
+ scroll.scrollTop -= span.y - pinch.y;
251
+ }
252
+ pinch.x = span.x;
253
+ pinch.y = span.y;
254
+ fit = null;
255
+ applyZoom(pinch.zoom * (span.distance / pinch.distance), { x: span.x, y: span.y });
256
+ };
257
+ const onTouchEnd = (event) => {
258
+ if (event.touches.length < 2)
259
+ pinch = null;
260
+ };
261
+ const onScroll = () => {
262
+ lastScrollAt = performance.now();
263
+ if (isOwnMove)
264
+ ownMove();
265
+ if (scrollFrame)
266
+ return;
267
+ scrollFrame = requestAnimationFrame(() => {
268
+ scrollFrame = 0;
269
+ reportPageInView();
270
+ const shift = { x: (scroll.scrollLeft - told.left) / zoom, y: (scroll.scrollTop - told.top) / zoom, byReader: !isOwnMove };
271
+ told = { left: scroll.scrollLeft, top: scroll.scrollTop };
272
+ notify("scroll", shift);
273
+ });
274
+ };
275
+ /** NOTE: the move kept for the first layout, made at once, since a reader opening a document there should not watch it glide. */
276
+ const makeSentMove = (sent) => {
277
+ if (fit)
278
+ zoom = clamp(fittedZoom(fit), MIN_ZOOM, MAX_ZOOM);
279
+ layout();
280
+ ownMove();
281
+ const place = sent.rect ? inContent(sent.page, sent.rect) : null;
282
+ if (place) {
283
+ scroll.scrollLeft = place.x + place.width / 2 - scroll.clientWidth / 2;
284
+ scroll.scrollTop = place.y + place.height / 2 - scroll.clientHeight / 2;
285
+ }
286
+ else {
287
+ scroll.scrollTop = scrollTopFor(sent.page);
288
+ }
289
+ settle();
290
+ };
291
+ const resizeObserver = new ResizeObserver(() => {
292
+ if (!hasSize())
293
+ return;
294
+ isLaidOut = true;
295
+ const sent = sentTo;
296
+ sentTo = null;
297
+ if (sent)
298
+ makeSentMove(sent);
299
+ else if (fit === "page")
300
+ fitToPage();
301
+ else if (fit)
302
+ applyZoom(fittedZoom(fit), { x: 0, y: 0 });
303
+ else
304
+ notify("resize");
305
+ });
306
+ resizeObserver.observe(scroll);
307
+ scroll.addEventListener("wheel", onWheel, { passive: false });
308
+ scroll.addEventListener("scroll", onScroll, { passive: true });
309
+ scroll.addEventListener("touchstart", onTouchStart, { passive: true });
310
+ scroll.addEventListener("touchmove", onTouchMove, { passive: false });
311
+ scroll.addEventListener("touchend", onTouchEnd, { passive: true });
312
+ scroll.addEventListener("touchcancel", onTouchEnd, { passive: true });
313
+ zoom = fittedZoom(fit);
314
+ layout();
315
+ return {
316
+ pane: scroll,
317
+ floats: stage.floats,
318
+ pages,
319
+ get zoom() {
320
+ return zoom;
321
+ },
322
+ get lastScrollAt() {
323
+ return lastScrollAt;
324
+ },
325
+ get pageInView() {
326
+ return pageInView;
327
+ },
328
+ get fit() {
329
+ return fit;
330
+ },
331
+ get rotation() {
332
+ return rotation;
333
+ },
334
+ get spread() {
335
+ return spread;
336
+ },
337
+ pointOn,
338
+ pointAt,
339
+ inContent,
340
+ inPage,
341
+ reportPageInView,
342
+ toScreen(index, box) {
343
+ const place = inContent(index, box);
344
+ if (!place)
345
+ return null;
346
+ const bounds = scroll.getBoundingClientRect();
347
+ return { left: bounds.left - scroll.scrollLeft + place.x, top: bounds.top - scroll.scrollTop + place.y, width: place.width, height: place.height };
348
+ },
349
+ paneBox: () => ({ x: scroll.scrollLeft, y: scroll.scrollTop, width: scroll.clientWidth, height: scroll.clientHeight }),
350
+ revealRect(index, rect) {
351
+ if (!pages[index])
352
+ return;
353
+ if (!isLaidOut || !hasSize()) {
354
+ sentTo = { page: index, rect };
355
+ return;
356
+ }
357
+ const place = inContent(index, rect);
358
+ if (!place)
359
+ return;
360
+ moveTo({ left: place.x + place.width / 2 - scroll.clientWidth / 2, top: place.y + place.height / 2 - scroll.clientHeight / 2 });
361
+ },
362
+ goToPage(index) {
363
+ if (!pages[index])
364
+ return;
365
+ if (!isLaidOut || !hasSize()) {
366
+ sentTo = { page: index };
367
+ return;
368
+ }
369
+ moveTo({ top: scrollTopFor(index) });
370
+ },
371
+ moveBy(x, y) {
372
+ ownMove();
373
+ scroll.scrollLeft += x * zoom;
374
+ scroll.scrollTop += y * zoom;
375
+ },
376
+ zoomIn() {
377
+ fit = null;
378
+ applyZoom(zoom * ZOOM_STEP);
379
+ },
380
+ zoomOut() {
381
+ fit = null;
382
+ applyZoom(zoom / ZOOM_STEP);
383
+ },
384
+ zoomTo(next, anchor) {
385
+ fit = null;
386
+ applyZoom(next, anchor);
387
+ },
388
+ fitWidth() {
389
+ fit = "width";
390
+ applyZoom(fittedZoom("width"));
391
+ },
392
+ fitPage() {
393
+ fit = "page";
394
+ fitToPage();
395
+ },
396
+ rotateTo(next) {
397
+ if (next === rotation)
398
+ return;
399
+ layOutAgain(() => {
400
+ rotation = next;
401
+ });
402
+ },
403
+ setSpread(next) {
404
+ if (next === spread)
405
+ return;
406
+ layOutAgain(() => {
407
+ spread = next;
408
+ });
409
+ },
410
+ onChange(listener) {
411
+ listeners.add(listener);
412
+ return () => {
413
+ listeners.delete(listener);
414
+ };
415
+ },
416
+ destroy() {
417
+ listeners.clear();
418
+ cancelAnimationFrame(scrollFrame);
419
+ clearTimeout(ownMoveTimer);
420
+ resizeObserver.disconnect();
421
+ scroll.removeEventListener("wheel", onWheel);
422
+ scroll.removeEventListener("touchstart", onTouchStart);
423
+ scroll.removeEventListener("touchmove", onTouchMove);
424
+ scroll.removeEventListener("touchend", onTouchEnd);
425
+ scroll.removeEventListener("touchcancel", onTouchEnd);
426
+ scroll.removeEventListener("scroll", onScroll);
427
+ },
428
+ };
429
+ }