plitejs 0.0.1 → 54.0.0-beta.3
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 +4 -1
- package/README.md +83 -0
- package/dist/annotation-store-metrics-Ccjkef45.js +12 -0
- package/dist/annotations/index.d.ts +2 -0
- package/dist/annotations/index.js +2 -0
- package/dist/authored/index.d.ts +596 -0
- package/dist/authored/index.js +18558 -0
- package/dist/authored-veto-DSnQcHGR.js +6 -0
- package/dist/browser-protocol-pvt4HFF7.js +5 -0
- package/dist/comparison-plan-CEQL0ooW.js +1338 -0
- package/dist/comparison-types-Dt-unJZY.d.ts +179 -0
- package/dist/create-editor-CkHtPWXV.js +1107 -0
- package/dist/diff/index.d.ts +32 -0
- package/dist/diff/index.js +1748 -0
- package/dist/dom/index.d.ts +2 -0
- package/dist/dom/index.js +2 -0
- package/dist/dom-Cgfscvnm.js +9220 -0
- package/dist/editor-owner-IUmkTeXz.js +171 -0
- package/dist/editor-view-lifetime-GJKa_QGh.js +23 -0
- package/dist/history/index.d.ts +170 -0
- package/dist/history/index.js +6718 -0
- package/dist/hyperscript/index.d.ts +2 -0
- package/dist/hyperscript/index.js +2 -0
- package/dist/hyperscript-FPuoArEi.js +299 -0
- package/dist/index-BpQx2UG4.d.ts +1093 -0
- package/dist/index-Cw-oC2ps.d.ts +190 -0
- package/dist/index-DPWyk3lG.d.ts +65 -0
- package/dist/index-DpltOCot.d.ts +9753 -0
- package/dist/index-HPYawTae.d.ts +119 -0
- package/dist/index-K0Za9YXc.d.ts +1246 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +5 -0
- package/dist/internal/index.d.ts +2 -0
- package/dist/internal/index.js +156 -0
- package/dist/mapped-view-store-ld-6uxEU.js +120 -0
- package/dist/native-grouping-input-DMNcsIaW.js +8 -0
- package/dist/pagination/index.d.ts +169 -0
- package/dist/pagination/index.js +2 -0
- package/dist/pagination/react.d.ts +33 -0
- package/dist/pagination/react.js +1509 -0
- package/dist/pagination-D_A8iPJP.js +1021 -0
- package/dist/plugin-DWzkJbnJ.js +50999 -0
- package/dist/range-geometry-BLAHRE11.d.ts +38 -0
- package/dist/range-geometry-BjeLHOnl.js +486 -0
- package/dist/react/index.d.ts +5 -0
- package/dist/react/index.js +7 -0
- package/dist/react/virtualized.d.ts +13 -0
- package/dist/react/virtualized.js +209 -0
- package/dist/react-DZ15gmz2.js +1018 -0
- package/dist/retraction-BMem8Rfk.js +109 -0
- package/dist/src-cT1OTKP_.js +1386 -0
- package/dist/store-Cz2mXyDe.js +704 -0
- package/dist/testing/index.d.ts +183 -0
- package/dist/testing/index.js +96 -0
- package/dist/use-editor-dom-scope-BbMDSCpW.js +28354 -0
- package/dist/yjs/index.d.ts +2 -0
- package/dist/yjs/index.js +2 -0
- package/dist/yjs/react/index.d.ts +26 -0
- package/dist/yjs/react/index.js +85 -0
- package/dist/yjs-DOK-m7kl.js +13413 -0
- package/package.json +191 -43
|
@@ -0,0 +1,1509 @@
|
|
|
1
|
+
import { B as NodeApi, al as reportEditorLifecycleError, hu as PointApi, lu as TextApi, mu as RangeApi, xu as PathApi } from "../plugin-DWzkJbnJ.js";
|
|
2
|
+
import { Bt as findDOMRootRuntime, Dt as SOFT_LINE_END_SELECTOR, H as subscribeEditorDOMScope, Ot as SOFT_LINE_FLAG, xn as getPliteStringDirectPlacement } from "../dom-Cgfscvnm.js";
|
|
3
|
+
import { a as invalidatePageLayoutEngines, i as pageSettingsCodec, n as createPretextPageLayoutEngine, r as measurePages, t as createEstimatedPageLayoutEngine } from "../pagination-D_A8iPJP.js";
|
|
4
|
+
import { H as DecorationContext, K as useHydrated, L as useRootNodeKeys, St as toReactRuntimeEditor, Ut as useEditorContext, Y as useEditorState, _ as defaultScrollSelectionIntoView, b as createContainerDecorationSlice, ct as landOnViewLine, f as TextRenderScopeContext, g as pathKey, h as getElementPlacementBox, lt as nearestViewText, s as EditableViewportSurface, st as landInViewBlock, u as SurfaceRoot, ut as placeOnViewText, v as getElementTransformScale, x as createEditableViewportPlan, y as ImperativeTextFlowContext, zt as useEditorReadOnly } from "../use-editor-dom-scope-BbMDSCpW.js";
|
|
5
|
+
import { p as useElementPath } from "../react-DZ15gmz2.js";
|
|
6
|
+
import React, { createContext, useCallback, useContext, useEffect, useLayoutEffect, useMemo, useRef, useState, useSyncExternalStore } from "react";
|
|
7
|
+
import { flushSync } from "react-dom";
|
|
8
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
9
|
+
//#region src/react/components/windowed-editable.internal.tsx
|
|
10
|
+
const WindowedEditableInner = ({ decorationStore, elementLayouts, enabled = true, mountedTopLevelIndexes, onRequestMount, scrollToPath, totalSize, ...props }) => {
|
|
11
|
+
const topLevelNodeKeys = useRootNodeKeys();
|
|
12
|
+
const viewportPlan = React.useMemo(() => enabled ? createEditableViewportPlan({
|
|
13
|
+
coordinateSpace: "canvas",
|
|
14
|
+
indexes: mountedTopLevelIndexes,
|
|
15
|
+
requestMount: onRequestMount,
|
|
16
|
+
scrollToPath,
|
|
17
|
+
sizeAtIndex: () => 0,
|
|
18
|
+
startAtIndex: () => 0,
|
|
19
|
+
topLevelNodeKeys,
|
|
20
|
+
totalSize
|
|
21
|
+
}) : null, [
|
|
22
|
+
enabled,
|
|
23
|
+
mountedTopLevelIndexes,
|
|
24
|
+
onRequestMount,
|
|
25
|
+
scrollToPath,
|
|
26
|
+
topLevelNodeKeys,
|
|
27
|
+
totalSize
|
|
28
|
+
]);
|
|
29
|
+
return /* @__PURE__ */ jsx(EditableViewportSurface, {
|
|
30
|
+
...props,
|
|
31
|
+
decorationStore,
|
|
32
|
+
elementLayouts,
|
|
33
|
+
viewportPlan
|
|
34
|
+
});
|
|
35
|
+
};
|
|
36
|
+
const WindowedEditable = ({ root, ...props }) => {
|
|
37
|
+
if (root === "main") throw new Error("Omit root to render the primary editable.");
|
|
38
|
+
const inheritedReadOnly = useEditorReadOnly();
|
|
39
|
+
const editor = useEditorContext();
|
|
40
|
+
const readOnly = props.readOnly || inheritedReadOnly;
|
|
41
|
+
const editable = /* @__PURE__ */ jsx(WindowedEditableInner, { ...props });
|
|
42
|
+
return root === void 0 ? editable : /* @__PURE__ */ jsx(SurfaceRoot, {
|
|
43
|
+
editor,
|
|
44
|
+
readOnly,
|
|
45
|
+
root,
|
|
46
|
+
children: editable
|
|
47
|
+
});
|
|
48
|
+
};
|
|
49
|
+
//#endregion
|
|
50
|
+
//#region src/react/hooks/use-editable-layout-pass.ts
|
|
51
|
+
/**
|
|
52
|
+
* Runs `read` in a DOM read phase after mount, after each DOM scope change and
|
|
53
|
+
* after each DOM change the root's integrity observer reports, then runs the
|
|
54
|
+
* write it returns in the same flush, before paint.
|
|
55
|
+
*/
|
|
56
|
+
const useEditableLayoutPass = (editor, root, label, read) => {
|
|
57
|
+
useLayoutEffect(() => {
|
|
58
|
+
if (!root) return void 0;
|
|
59
|
+
let cancelRead = null;
|
|
60
|
+
let runtime = null;
|
|
61
|
+
let stopLayout = null;
|
|
62
|
+
const schedule = () => {
|
|
63
|
+
const current = runtime;
|
|
64
|
+
if (!current || cancelRead) return;
|
|
65
|
+
cancelRead = current.domPhaseScheduler.schedule("dom-read", label, () => {
|
|
66
|
+
cancelRead = null;
|
|
67
|
+
const write = read(root);
|
|
68
|
+
if (write) current.domPhaseScheduler.schedule("dom-write", label, write, { timing: "immediate" });
|
|
69
|
+
}, { timing: "microtask" });
|
|
70
|
+
};
|
|
71
|
+
const bind = () => {
|
|
72
|
+
const next = findDOMRootRuntime(root);
|
|
73
|
+
if (next === runtime) return;
|
|
74
|
+
cancelRead?.();
|
|
75
|
+
cancelRead = null;
|
|
76
|
+
stopLayout?.();
|
|
77
|
+
runtime = next;
|
|
78
|
+
stopLayout = next?.subscribeLayout(schedule) ?? null;
|
|
79
|
+
};
|
|
80
|
+
const stopBinding = subscribeEditorDOMScope(editor, () => {
|
|
81
|
+
bind();
|
|
82
|
+
schedule();
|
|
83
|
+
});
|
|
84
|
+
bind();
|
|
85
|
+
schedule();
|
|
86
|
+
return () => {
|
|
87
|
+
stopBinding();
|
|
88
|
+
stopLayout?.();
|
|
89
|
+
cancelRead?.();
|
|
90
|
+
};
|
|
91
|
+
}, [
|
|
92
|
+
editor,
|
|
93
|
+
label,
|
|
94
|
+
read,
|
|
95
|
+
root
|
|
96
|
+
]);
|
|
97
|
+
};
|
|
98
|
+
//#endregion
|
|
99
|
+
//#region src/pagination/page-geometry.internal.ts
|
|
100
|
+
const createPageLayoutGeometry = ({ fragments, gap = 24, mode = "single", pages }) => {
|
|
101
|
+
const extents = /* @__PURE__ */ new Map();
|
|
102
|
+
for (const { pageIndex, rect } of fragments) {
|
|
103
|
+
const extent = extents.get(pageIndex);
|
|
104
|
+
const bottom = rect.top + rect.height;
|
|
105
|
+
const right = rect.left + rect.width;
|
|
106
|
+
if (extent) {
|
|
107
|
+
extent.bottom = Math.max(extent.bottom, bottom);
|
|
108
|
+
extent.right = Math.max(extent.right, right);
|
|
109
|
+
} else extents.set(pageIndex, {
|
|
110
|
+
bottom,
|
|
111
|
+
right
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
const occupiedSizes = pages.map((page) => {
|
|
115
|
+
const extent = extents.get(page.index);
|
|
116
|
+
return {
|
|
117
|
+
height: Math.max(page.height, extent?.bottom ?? page.height),
|
|
118
|
+
width: Math.max(page.width, extent?.right ?? page.width)
|
|
119
|
+
};
|
|
120
|
+
});
|
|
121
|
+
if (mode === "spread") {
|
|
122
|
+
const placements = [];
|
|
123
|
+
let top = 0;
|
|
124
|
+
let width = 0;
|
|
125
|
+
for (let index = 0; index < pages.length; index += 2) {
|
|
126
|
+
const left = occupiedSizes[index] ?? {
|
|
127
|
+
height: 0,
|
|
128
|
+
width: 0
|
|
129
|
+
};
|
|
130
|
+
const right = occupiedSizes[index + 1];
|
|
131
|
+
placements[index] = {
|
|
132
|
+
left: 0,
|
|
133
|
+
top
|
|
134
|
+
};
|
|
135
|
+
if (right) placements[index + 1] = {
|
|
136
|
+
left: left.width + gap,
|
|
137
|
+
top
|
|
138
|
+
};
|
|
139
|
+
width = Math.max(width, left.width + (right ? gap + right.width : 0));
|
|
140
|
+
top += Math.max(left.height, right?.height ?? 0);
|
|
141
|
+
if (index + 2 < pages.length) top += gap;
|
|
142
|
+
}
|
|
143
|
+
return {
|
|
144
|
+
height: top,
|
|
145
|
+
occupiedSizes,
|
|
146
|
+
pagePlacements: placements,
|
|
147
|
+
width
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
const pagePlacements = [];
|
|
151
|
+
let top = 0;
|
|
152
|
+
let width = 0;
|
|
153
|
+
occupiedSizes.forEach((size, index) => {
|
|
154
|
+
pagePlacements[index] = {
|
|
155
|
+
left: 0,
|
|
156
|
+
top
|
|
157
|
+
};
|
|
158
|
+
width = Math.max(width, size.width);
|
|
159
|
+
top += size.height + (index < occupiedSizes.length - 1 ? gap : 0);
|
|
160
|
+
});
|
|
161
|
+
return {
|
|
162
|
+
height: top,
|
|
163
|
+
occupiedSizes,
|
|
164
|
+
pagePlacements,
|
|
165
|
+
width
|
|
166
|
+
};
|
|
167
|
+
};
|
|
168
|
+
const toCanvasRect = (rect, pageIndex, geometry) => {
|
|
169
|
+
const placement = geometry.pagePlacements[pageIndex] ?? {
|
|
170
|
+
left: 0,
|
|
171
|
+
top: 0
|
|
172
|
+
};
|
|
173
|
+
return {
|
|
174
|
+
...rect,
|
|
175
|
+
left: placement.left + rect.left,
|
|
176
|
+
top: placement.top + rect.top
|
|
177
|
+
};
|
|
178
|
+
};
|
|
179
|
+
const bounds = (rects) => {
|
|
180
|
+
if (rects.length === 0) return {
|
|
181
|
+
height: 0,
|
|
182
|
+
left: 0,
|
|
183
|
+
top: 0,
|
|
184
|
+
width: 0
|
|
185
|
+
};
|
|
186
|
+
const left = Math.min(...rects.map((rect) => rect.left));
|
|
187
|
+
const top = Math.min(...rects.map((rect) => rect.top));
|
|
188
|
+
const right = Math.max(...rects.map((rect) => rect.left + rect.width));
|
|
189
|
+
return {
|
|
190
|
+
height: Math.max(...rects.map((rect) => rect.top + rect.height)) - top,
|
|
191
|
+
left,
|
|
192
|
+
top,
|
|
193
|
+
width: right - left
|
|
194
|
+
};
|
|
195
|
+
};
|
|
196
|
+
//#endregion
|
|
197
|
+
//#region src/pagination/page-mount-plan.ts
|
|
198
|
+
const getPageMountGroups = (pages, mode) => {
|
|
199
|
+
if (mode === "spread") {
|
|
200
|
+
const groups = [];
|
|
201
|
+
for (let index = 0; index < pages.length; index += 2) {
|
|
202
|
+
const pageIndexes = [pages[index]?.index, pages[index + 1]?.index].filter((pageIndex) => typeof pageIndex === "number");
|
|
203
|
+
if (pageIndexes.length > 0) groups.push(pageIndexes);
|
|
204
|
+
}
|
|
205
|
+
return groups;
|
|
206
|
+
}
|
|
207
|
+
return pages.map((page) => [page.index]);
|
|
208
|
+
};
|
|
209
|
+
const createPagedEditablePageMountPlan = ({ fragments, geometry, mode, pages }) => {
|
|
210
|
+
const itemIndexesByPath = /* @__PURE__ */ new Map();
|
|
211
|
+
const itemIndexesByTopLevelIndex = /* @__PURE__ */ new Map();
|
|
212
|
+
const fragmentsByPageIndex = /* @__PURE__ */ new Map();
|
|
213
|
+
const pageArrayIndexByPageIndex = new Map(pages.map((page, index) => [page.index, index]));
|
|
214
|
+
for (const fragment of fragments) {
|
|
215
|
+
const pageFragments = fragmentsByPageIndex.get(fragment.pageIndex) ?? [];
|
|
216
|
+
pageFragments.push(fragment);
|
|
217
|
+
fragmentsByPageIndex.set(fragment.pageIndex, pageFragments);
|
|
218
|
+
}
|
|
219
|
+
return {
|
|
220
|
+
itemIndexesByPath,
|
|
221
|
+
itemIndexesByTopLevelIndex,
|
|
222
|
+
items: getPageMountGroups(pages, mode).map((pageIndexes, index) => {
|
|
223
|
+
const itemFragments = pageIndexes.flatMap((pageIndex) => fragmentsByPageIndex.get(pageIndex) ?? []);
|
|
224
|
+
const topLevelIndexes = [...new Set(itemFragments.flatMap((fragment) => [fragment.path[0], ...fragment.type === "direct-children" ? (fragment.children ?? []).map((child) => child.path[0]) : []]))].filter((topLevelIndex) => typeof topLevelIndex === "number").sort((left, right) => left - right);
|
|
225
|
+
const fragmentPaths = itemFragments.map((fragment) => fragment.path);
|
|
226
|
+
const childPaths = itemFragments.flatMap((fragment) => fragment.type === "direct-children" ? (fragment.children ?? []).map((child) => child.path) : []);
|
|
227
|
+
const placements = pageIndexes.map((pageIndex) => {
|
|
228
|
+
const pageArrayIndex = pageArrayIndexByPageIndex.get(pageIndex);
|
|
229
|
+
return pageArrayIndex == null ? {
|
|
230
|
+
left: 0,
|
|
231
|
+
top: 0
|
|
232
|
+
} : geometry.pagePlacements[pageArrayIndex] ?? {
|
|
233
|
+
left: 0,
|
|
234
|
+
top: 0
|
|
235
|
+
};
|
|
236
|
+
});
|
|
237
|
+
const start = Math.min(...placements.map((placement) => placement.top));
|
|
238
|
+
const size = pageIndexes.reduce((height, pageIndex) => {
|
|
239
|
+
const pageArrayIndex = pageArrayIndexByPageIndex.get(pageIndex);
|
|
240
|
+
const placement = pageArrayIndex == null ? null : geometry.pagePlacements[pageArrayIndex];
|
|
241
|
+
const occupied = pageArrayIndex == null ? null : geometry.occupiedSizes[pageArrayIndex];
|
|
242
|
+
if (!occupied || !placement) return height;
|
|
243
|
+
return Math.max(height, placement.top + occupied.height - start);
|
|
244
|
+
}, 0);
|
|
245
|
+
for (const topLevelIndex of topLevelIndexes) {
|
|
246
|
+
const current = itemIndexesByTopLevelIndex.get(topLevelIndex) ?? [];
|
|
247
|
+
current.push(index);
|
|
248
|
+
itemIndexesByTopLevelIndex.set(topLevelIndex, current);
|
|
249
|
+
}
|
|
250
|
+
for (const path of [...fragmentPaths, ...childPaths]) {
|
|
251
|
+
const key = path.join(".");
|
|
252
|
+
const current = itemIndexesByPath.get(key) ?? [];
|
|
253
|
+
if (!current.includes(index)) current.push(index);
|
|
254
|
+
itemIndexesByPath.set(key, current);
|
|
255
|
+
}
|
|
256
|
+
return {
|
|
257
|
+
childPaths,
|
|
258
|
+
fragmentPaths,
|
|
259
|
+
index,
|
|
260
|
+
key: `page-mount:${pageIndexes.join("-")}`,
|
|
261
|
+
pageIndexes,
|
|
262
|
+
size,
|
|
263
|
+
start,
|
|
264
|
+
topLevelIndexes
|
|
265
|
+
};
|
|
266
|
+
})
|
|
267
|
+
};
|
|
268
|
+
};
|
|
269
|
+
const getPagedEditableVisiblePageMountItems = (plan, { gap, overscan, pages, virtualizes, viewport }) => {
|
|
270
|
+
if (!virtualizes) return plan.items;
|
|
271
|
+
const firstPageHeight = pages[0]?.height ?? 1024;
|
|
272
|
+
const overscanSize = overscan * (firstPageHeight + gap);
|
|
273
|
+
const effectiveViewport = viewport ?? {
|
|
274
|
+
bottom: firstPageHeight,
|
|
275
|
+
top: 0
|
|
276
|
+
};
|
|
277
|
+
return plan.items.filter((item) => item.start + item.size >= effectiveViewport.top - overscanSize && item.start <= effectiveViewport.bottom + overscanSize);
|
|
278
|
+
};
|
|
279
|
+
//#endregion
|
|
280
|
+
//#region src/pagination/paged-view-lines.ts
|
|
281
|
+
const isNativeFlowBlock = (path, drawnTextFragments, selectedPaths) => drawnTextFragments === 1 && selectedPaths.some((selected) => PathApi.isCommon(path, selected));
|
|
282
|
+
const FRAGMENTS_BY_PATH = /* @__PURE__ */ new WeakMap();
|
|
283
|
+
const readFragmentsByPath = (snapshot) => {
|
|
284
|
+
const cached = FRAGMENTS_BY_PATH.get(snapshot);
|
|
285
|
+
if (cached) return cached;
|
|
286
|
+
const index = /* @__PURE__ */ new Map();
|
|
287
|
+
const add = (path, fragmentIndex) => {
|
|
288
|
+
const key = path.join(".");
|
|
289
|
+
const indexes = index.get(key) ?? [];
|
|
290
|
+
if (indexes.at(-1) !== fragmentIndex) indexes.push(fragmentIndex);
|
|
291
|
+
index.set(key, indexes);
|
|
292
|
+
};
|
|
293
|
+
snapshot.fragments.forEach((fragment, fragmentIndex) => {
|
|
294
|
+
add(fragment.path, fragmentIndex);
|
|
295
|
+
if (fragment.type === "direct-children") fragment.children.forEach((child) => add(child.path, fragmentIndex));
|
|
296
|
+
});
|
|
297
|
+
FRAGMENTS_BY_PATH.set(snapshot, index);
|
|
298
|
+
return index;
|
|
299
|
+
};
|
|
300
|
+
const lineStart = (line) => RangeApi.start(line.runs[0]?.source ?? line.source);
|
|
301
|
+
const lineEnd = (line) => RangeApi.end(line.runs.at(-1)?.source ?? line.source);
|
|
302
|
+
const caretLineIndex = (lines, point, upper) => {
|
|
303
|
+
const index = lines.findLastIndex(({ line }) => PointApi.compare(lineStart(line), point) <= 0);
|
|
304
|
+
const caret = lines[index];
|
|
305
|
+
if (!caret) return 0;
|
|
306
|
+
return upper && index > 0 && PointApi.equals(lineStart(caret.line), point) ? index - 1 : index;
|
|
307
|
+
};
|
|
308
|
+
const pagedLineTarget = ({ direction, from, isSelectable, mountedPageIndexes, selectedPaths, snapshot }) => {
|
|
309
|
+
const isDrawn = (fragment) => mountedPageIndexes === "all" || mountedPageIndexes.has(fragment.pageIndex);
|
|
310
|
+
const toLine = ({ fragment, line }) => ({
|
|
311
|
+
drawn: isDrawn(fragment),
|
|
312
|
+
end: lineEnd(line),
|
|
313
|
+
kind: "line",
|
|
314
|
+
path: fragment.path,
|
|
315
|
+
start: lineStart(line)
|
|
316
|
+
});
|
|
317
|
+
const { fragments } = snapshot;
|
|
318
|
+
const byPath = readFragmentsByPath(snapshot);
|
|
319
|
+
let blockIndexes;
|
|
320
|
+
for (let depth = from.point.path.length; depth > 0 && !blockIndexes; depth--) blockIndexes = byPath.get(from.point.path.slice(0, depth).join("."));
|
|
321
|
+
if (!blockIndexes) return null;
|
|
322
|
+
const blockFragments = blockIndexes.flatMap((index) => fragments[index] ?? []);
|
|
323
|
+
const blockPath = blockFragments[0]?.path;
|
|
324
|
+
if (!blockPath) return null;
|
|
325
|
+
const textFragments = blockFragments.filter((fragment) => fragment.type === "text" && PathApi.equals(fragment.path, blockPath));
|
|
326
|
+
if (textFragments.length !== blockFragments.length) return null;
|
|
327
|
+
const lines = textFragments.flatMap((fragment) => fragment.lines.map((line) => ({
|
|
328
|
+
fragment,
|
|
329
|
+
line
|
|
330
|
+
})));
|
|
331
|
+
if (lines.length === 0) return null;
|
|
332
|
+
const step = direction === "down" ? 1 : -1;
|
|
333
|
+
const caretIndex = caretLineIndex(lines, from.point, from.upper);
|
|
334
|
+
const target = lines[caretIndex + step];
|
|
335
|
+
if (target) return target.fragment === lines[caretIndex]?.fragment && isNativeFlowBlock(blockPath, textFragments.filter(isDrawn).length, selectedPaths) ? null : toLine(target);
|
|
336
|
+
const start = direction === "down" ? blockIndexes.at(-1) : blockIndexes[0];
|
|
337
|
+
if (start === void 0) return null;
|
|
338
|
+
for (let index = start + step; fragments[index]; index += step) {
|
|
339
|
+
const neighbor = fragments[index];
|
|
340
|
+
if (neighbor.type === "direct-children") return null;
|
|
341
|
+
if (PathApi.equals(neighbor.path, blockPath) || !isSelectable(neighbor.path)) continue;
|
|
342
|
+
const block = {
|
|
343
|
+
drawn: isDrawn(neighbor),
|
|
344
|
+
kind: "block",
|
|
345
|
+
path: neighbor.path
|
|
346
|
+
};
|
|
347
|
+
if (neighbor.type !== "text") return block;
|
|
348
|
+
const neighborLine = direction === "down" ? neighbor.lines[0] : neighbor.lines.at(-1);
|
|
349
|
+
return neighborLine ? toLine({
|
|
350
|
+
fragment: neighbor,
|
|
351
|
+
line: neighborLine
|
|
352
|
+
}) : block;
|
|
353
|
+
}
|
|
354
|
+
return null;
|
|
355
|
+
};
|
|
356
|
+
const BOXES_BY_PAGE = /* @__PURE__ */ new WeakMap();
|
|
357
|
+
const readBoxesByPage = (snapshot) => {
|
|
358
|
+
const cached = BOXES_BY_PAGE.get(snapshot);
|
|
359
|
+
if (cached) return cached;
|
|
360
|
+
const index = /* @__PURE__ */ new Map();
|
|
361
|
+
const add = (pageIndex, box) => {
|
|
362
|
+
const boxes = index.get(pageIndex);
|
|
363
|
+
if (boxes) boxes.push(box);
|
|
364
|
+
else index.set(pageIndex, [box]);
|
|
365
|
+
};
|
|
366
|
+
for (const fragment of snapshot.fragments) if (fragment.type === "text") fragment.lines.forEach((line, lineIndex) => add(fragment.pageIndex, {
|
|
367
|
+
fragment,
|
|
368
|
+
kind: "line",
|
|
369
|
+
line,
|
|
370
|
+
lineIndex,
|
|
371
|
+
rect: line.rect
|
|
372
|
+
}));
|
|
373
|
+
else if (fragment.type === "atomic") add(fragment.pageIndex, {
|
|
374
|
+
kind: "block",
|
|
375
|
+
path: fragment.path,
|
|
376
|
+
rect: fragment.rect
|
|
377
|
+
});
|
|
378
|
+
else fragment.children.forEach((child) => add(fragment.pageIndex, {
|
|
379
|
+
kind: "block",
|
|
380
|
+
path: child.path,
|
|
381
|
+
rect: child.rect
|
|
382
|
+
}));
|
|
383
|
+
BOXES_BY_PAGE.set(snapshot, index);
|
|
384
|
+
return index;
|
|
385
|
+
};
|
|
386
|
+
const axisDistance = (value, start, size) => value < start ? start - value : value > start + size ? value - start - size : 0;
|
|
387
|
+
const distanceTo = (rect, x, y) => [axisDistance(y, rect.top, rect.height), axisDistance(x, rect.left, rect.width)];
|
|
388
|
+
const isNearer = (left, right) => left[0] < right[0] || left[0] === right[0] && left[1] < right[1];
|
|
389
|
+
const placedTextAt = ({ measureBlock, mountedPageIndexes, occupiedSizes, pagePlacements, snapshot, x, y }) => {
|
|
390
|
+
let page = null;
|
|
391
|
+
for (const layoutPage of snapshot.pages) {
|
|
392
|
+
if (mountedPageIndexes !== "all" && !mountedPageIndexes.has(layoutPage.index)) continue;
|
|
393
|
+
const placement = pagePlacements[layoutPage.index];
|
|
394
|
+
if (!placement) continue;
|
|
395
|
+
const distance = Math.hypot(...distanceTo({
|
|
396
|
+
...occupiedSizes[layoutPage.index] ?? layoutPage,
|
|
397
|
+
...placement
|
|
398
|
+
}, x, y));
|
|
399
|
+
if (!page || distance < page.distance) page = {
|
|
400
|
+
distance,
|
|
401
|
+
index: layoutPage.index,
|
|
402
|
+
origin: placement
|
|
403
|
+
};
|
|
404
|
+
}
|
|
405
|
+
if (!page) return null;
|
|
406
|
+
const local = {
|
|
407
|
+
x: x - page.origin.left,
|
|
408
|
+
y: y - page.origin.top
|
|
409
|
+
};
|
|
410
|
+
const ranked = (readBoxesByPage(snapshot).get(page.index) ?? []).map((box) => ({
|
|
411
|
+
box,
|
|
412
|
+
distance: distanceTo(box.rect, local.x, local.y)
|
|
413
|
+
})).sort((left, right) => isNearer(left.distance, right.distance) ? -1 : isNearer(right.distance, left.distance) ? 1 : 0);
|
|
414
|
+
let best = null;
|
|
415
|
+
for (const { box, distance } of ranked) {
|
|
416
|
+
if (best && !isNearer(distance, best.distance)) break;
|
|
417
|
+
if (box.kind === "line") {
|
|
418
|
+
best = {
|
|
419
|
+
distance,
|
|
420
|
+
text: {
|
|
421
|
+
box,
|
|
422
|
+
kind: "line"
|
|
423
|
+
}
|
|
424
|
+
};
|
|
425
|
+
continue;
|
|
426
|
+
}
|
|
427
|
+
const measured = measureBlock(box);
|
|
428
|
+
if (measured && (!best || isNearer(measured.distance, best.distance))) best = {
|
|
429
|
+
distance: measured.distance,
|
|
430
|
+
text: {
|
|
431
|
+
kind: "text",
|
|
432
|
+
text: measured.text
|
|
433
|
+
}
|
|
434
|
+
};
|
|
435
|
+
}
|
|
436
|
+
return best?.text ?? null;
|
|
437
|
+
};
|
|
438
|
+
//#endregion
|
|
439
|
+
//#region src/pagination/react.tsx
|
|
440
|
+
const lineStartAfterTrailingBreak = (editor, point) => editor.read((state) => {
|
|
441
|
+
const node = state.nodes.get(point.path)?.[0];
|
|
442
|
+
return TextApi.isText(node) && point.offset === node.text.length && node.text.endsWith("\n") ? state.points.after(point, { unit: "offset" }) ?? point : point;
|
|
443
|
+
});
|
|
444
|
+
const RUN_ATTRIBUTE = "data-pagination-run";
|
|
445
|
+
const createPagedViewLines = ({ editor, readLayout, readVersion, readViewport }) => ({
|
|
446
|
+
lineAfter: ({ direction, from, readX }) => {
|
|
447
|
+
const layout = readLayout();
|
|
448
|
+
if (!layout || layout.measuredVersion !== readVersion()) return null;
|
|
449
|
+
const upper = from.side === "upper";
|
|
450
|
+
const target = pagedLineTarget({
|
|
451
|
+
direction,
|
|
452
|
+
from: {
|
|
453
|
+
point: upper ? from.point : lineStartAfterTrailingBreak(editor, from.point),
|
|
454
|
+
upper
|
|
455
|
+
},
|
|
456
|
+
isSelectable: (path) => editor.read((state) => {
|
|
457
|
+
const start = state.points.start(path);
|
|
458
|
+
return !!start && !state.nodes.above({
|
|
459
|
+
at: start,
|
|
460
|
+
match: (node) => NodeApi.isElement(node) && !state.nodes.isSelectable(node),
|
|
461
|
+
mode: "highest",
|
|
462
|
+
voids: true
|
|
463
|
+
});
|
|
464
|
+
}),
|
|
465
|
+
mountedPageIndexes: layout.mountedPageIndexes,
|
|
466
|
+
selectedPaths: layout.selectedPaths,
|
|
467
|
+
snapshot: layout.snapshot
|
|
468
|
+
});
|
|
469
|
+
if (!target) return null;
|
|
470
|
+
const root = target.drawn ? layout.root : null;
|
|
471
|
+
const { path } = target;
|
|
472
|
+
return target.kind === "line" ? landOnViewLine({
|
|
473
|
+
editor,
|
|
474
|
+
end: target.end,
|
|
475
|
+
path,
|
|
476
|
+
readX,
|
|
477
|
+
root,
|
|
478
|
+
start: target.start
|
|
479
|
+
}) : landInViewBlock({
|
|
480
|
+
direction,
|
|
481
|
+
editor,
|
|
482
|
+
path,
|
|
483
|
+
readX,
|
|
484
|
+
root
|
|
485
|
+
});
|
|
486
|
+
},
|
|
487
|
+
placementAt: ({ reach, target, x, y }) => {
|
|
488
|
+
const layout = readLayout();
|
|
489
|
+
if (!layout?.root || !layout.canvas || !layout.geometry || layout.measuredVersion !== readVersion()) return null;
|
|
490
|
+
const { canvas, geometry, root } = layout;
|
|
491
|
+
const direct = getPliteStringDirectPlacement({
|
|
492
|
+
root,
|
|
493
|
+
target,
|
|
494
|
+
x,
|
|
495
|
+
y
|
|
496
|
+
});
|
|
497
|
+
if (direct) return direct;
|
|
498
|
+
const canvasRect = canvas.getBoundingClientRect();
|
|
499
|
+
const scale = geometry.height > 0 && canvasRect.height > 0 ? canvasRect.height / geometry.height : 1;
|
|
500
|
+
const viewport = readViewport();
|
|
501
|
+
const canvasY = (y - canvasRect.top) / scale;
|
|
502
|
+
const visibleY = viewport ? Math.min(Math.max(canvasY, viewport.top), viewport.bottom) : canvasY;
|
|
503
|
+
const point = {
|
|
504
|
+
x,
|
|
505
|
+
y: canvasRect.top + visibleY * scale
|
|
506
|
+
};
|
|
507
|
+
const placed = placedTextAt({
|
|
508
|
+
measureBlock: ({ path }) => {
|
|
509
|
+
const hit = nearestViewText({
|
|
510
|
+
editor,
|
|
511
|
+
root,
|
|
512
|
+
scopes: [{ path }],
|
|
513
|
+
target,
|
|
514
|
+
...point
|
|
515
|
+
});
|
|
516
|
+
return hit ? {
|
|
517
|
+
distance: [hit.vertical / scale, hit.horizontal / scale],
|
|
518
|
+
text: hit
|
|
519
|
+
} : null;
|
|
520
|
+
},
|
|
521
|
+
mountedPageIndexes: layout.mountedPageIndexes,
|
|
522
|
+
occupiedSizes: geometry.occupiedSizes,
|
|
523
|
+
pagePlacements: geometry.pagePlacements,
|
|
524
|
+
snapshot: layout.snapshot,
|
|
525
|
+
x: (x - canvasRect.left) / scale,
|
|
526
|
+
y: visibleY
|
|
527
|
+
});
|
|
528
|
+
const hit = placed?.kind === "line" ? nearestViewText({
|
|
529
|
+
editor,
|
|
530
|
+
root,
|
|
531
|
+
scopes: placed.box.line.runs.map((run, runIndex) => ({
|
|
532
|
+
path: run.source.anchor.path,
|
|
533
|
+
selector: `[${RUN_ATTRIBUTE}="${placed.box.fragment.pageIndex}:${placed.box.lineIndex}:${runIndex}"]`
|
|
534
|
+
})),
|
|
535
|
+
target: null,
|
|
536
|
+
...point
|
|
537
|
+
}) : placed?.text ?? null;
|
|
538
|
+
return hit ? placeOnViewText(hit, {
|
|
539
|
+
reach,
|
|
540
|
+
x
|
|
541
|
+
}) : null;
|
|
542
|
+
}
|
|
543
|
+
});
|
|
544
|
+
const createPublishedLayoutStore = () => {
|
|
545
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
546
|
+
let snapshot = null;
|
|
547
|
+
return {
|
|
548
|
+
getSnapshot: () => snapshot,
|
|
549
|
+
setSnapshot(next) {
|
|
550
|
+
if (snapshot === next) return;
|
|
551
|
+
snapshot = next;
|
|
552
|
+
listeners.forEach((listener) => listener());
|
|
553
|
+
},
|
|
554
|
+
subscribe(listener) {
|
|
555
|
+
listeners.add(listener);
|
|
556
|
+
return () => listeners.delete(listener);
|
|
557
|
+
}
|
|
558
|
+
};
|
|
559
|
+
};
|
|
560
|
+
const LAYOUT_STORE_BY_HOST = /* @__PURE__ */ new WeakMap();
|
|
561
|
+
const layoutHostListeners = /* @__PURE__ */ new Set();
|
|
562
|
+
const bindLayoutHost = (host, store) => {
|
|
563
|
+
LAYOUT_STORE_BY_HOST.set(host, store);
|
|
564
|
+
layoutHostListeners.forEach((listener) => listener());
|
|
565
|
+
return () => {
|
|
566
|
+
if (LAYOUT_STORE_BY_HOST.get(host) === store) {
|
|
567
|
+
LAYOUT_STORE_BY_HOST.delete(host);
|
|
568
|
+
layoutHostListeners.forEach((listener) => listener());
|
|
569
|
+
}
|
|
570
|
+
};
|
|
571
|
+
};
|
|
572
|
+
const usePageLayout = (editableRef) => {
|
|
573
|
+
return useSyncExternalStore(useCallback((listener) => {
|
|
574
|
+
let unsubscribeStore = () => {};
|
|
575
|
+
let currentStore;
|
|
576
|
+
const bind = () => {
|
|
577
|
+
const nextStore = editableRef.current ? LAYOUT_STORE_BY_HOST.get(editableRef.current) : void 0;
|
|
578
|
+
if (nextStore === currentStore) return;
|
|
579
|
+
unsubscribeStore();
|
|
580
|
+
currentStore = nextStore;
|
|
581
|
+
unsubscribeStore = nextStore?.subscribe(listener) ?? (() => {});
|
|
582
|
+
listener();
|
|
583
|
+
};
|
|
584
|
+
layoutHostListeners.add(bind);
|
|
585
|
+
bind();
|
|
586
|
+
return () => {
|
|
587
|
+
layoutHostListeners.delete(bind);
|
|
588
|
+
unsubscribeStore();
|
|
589
|
+
};
|
|
590
|
+
}, [editableRef]), useCallback(() => editableRef.current ? LAYOUT_STORE_BY_HOST.get(editableRef.current)?.getSnapshot() ?? null : null, [editableRef]), () => null);
|
|
591
|
+
};
|
|
592
|
+
const FragmentContext = createContext(null);
|
|
593
|
+
const usePageLayoutFragments = () => {
|
|
594
|
+
const context = useContext(FragmentContext);
|
|
595
|
+
const path = useElementPath();
|
|
596
|
+
return useMemo(() => context && path ? context.placementsByPath.get(pathKey(path)) ?? [] : [], [context, path]);
|
|
597
|
+
};
|
|
598
|
+
const EMPTY_DECORATIONS = Object.freeze([]);
|
|
599
|
+
const sameSlices = (left, right) => left === right || left.length === right.length && left.every((slice, index) => slice.key === right[index]?.key && slice.start === right[index]?.start && slice.end === right[index]?.end && JSON.stringify(slice.attributes) === JSON.stringify(right[index]?.attributes));
|
|
600
|
+
const createSurfaceDecorationStore = (inherited) => {
|
|
601
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
602
|
+
const listenersByNodeKey = /* @__PURE__ */ new Map();
|
|
603
|
+
const merged = /* @__PURE__ */ new Map();
|
|
604
|
+
let local = /* @__PURE__ */ new Map();
|
|
605
|
+
let version = 0;
|
|
606
|
+
let unsubscribeInherited = () => {};
|
|
607
|
+
const publish = (keys) => {
|
|
608
|
+
if (keys.length === 0) return;
|
|
609
|
+
version += 1;
|
|
610
|
+
keys.forEach((key) => {
|
|
611
|
+
merged.delete(key);
|
|
612
|
+
listenersByNodeKey.get(key)?.forEach((listener) => listener());
|
|
613
|
+
});
|
|
614
|
+
listeners.forEach((listener) => listener(keys));
|
|
615
|
+
};
|
|
616
|
+
return {
|
|
617
|
+
destroy() {
|
|
618
|
+
unsubscribeInherited();
|
|
619
|
+
listeners.clear();
|
|
620
|
+
listenersByNodeKey.clear();
|
|
621
|
+
merged.clear();
|
|
622
|
+
local.clear();
|
|
623
|
+
},
|
|
624
|
+
getNodeSnapshot(nodeKey) {
|
|
625
|
+
const cached = merged.get(nodeKey);
|
|
626
|
+
if (cached) return cached;
|
|
627
|
+
const inheritedSlices = inherited?.getNodeSnapshot(nodeKey) ?? EMPTY_DECORATIONS;
|
|
628
|
+
const localSlices = local.get(nodeKey) ?? EMPTY_DECORATIONS;
|
|
629
|
+
const next = inheritedSlices.length === 0 ? localSlices : localSlices.length === 0 ? inheritedSlices : Object.freeze([...inheritedSlices, ...localSlices]);
|
|
630
|
+
merged.set(nodeKey, next);
|
|
631
|
+
return next;
|
|
632
|
+
},
|
|
633
|
+
getVersion: () => version,
|
|
634
|
+
hasSources: () => Boolean(inherited?.hasSources() || local.size > 0),
|
|
635
|
+
mount() {
|
|
636
|
+
unsubscribeInherited = inherited?.subscribe((keys) => publish(keys)) ?? (() => {});
|
|
637
|
+
return () => {
|
|
638
|
+
unsubscribeInherited();
|
|
639
|
+
unsubscribeInherited = () => {};
|
|
640
|
+
};
|
|
641
|
+
},
|
|
642
|
+
setLocalBuckets(nextBuckets) {
|
|
643
|
+
const changed = /* @__PURE__ */ new Set([...local.keys(), ...nextBuckets.keys()]);
|
|
644
|
+
const next = /* @__PURE__ */ new Map();
|
|
645
|
+
changed.forEach((key) => {
|
|
646
|
+
const previousBucket = local.get(key) ?? EMPTY_DECORATIONS;
|
|
647
|
+
const candidate = nextBuckets.get(key) ?? EMPTY_DECORATIONS;
|
|
648
|
+
if (candidate.length > 0) next.set(key, sameSlices(previousBucket, candidate) ? previousBucket : candidate);
|
|
649
|
+
if (sameSlices(previousBucket, candidate)) changed.delete(key);
|
|
650
|
+
});
|
|
651
|
+
local = next;
|
|
652
|
+
publish([...changed]);
|
|
653
|
+
},
|
|
654
|
+
subscribe(listener) {
|
|
655
|
+
listeners.add(listener);
|
|
656
|
+
return () => listeners.delete(listener);
|
|
657
|
+
},
|
|
658
|
+
subscribeNodeKey(nodeKey, listener) {
|
|
659
|
+
const nodeListeners = listenersByNodeKey.get(nodeKey) ?? /* @__PURE__ */ new Set();
|
|
660
|
+
nodeListeners.add(listener);
|
|
661
|
+
listenersByNodeKey.set(nodeKey, nodeListeners);
|
|
662
|
+
return () => {
|
|
663
|
+
nodeListeners.delete(listener);
|
|
664
|
+
if (nodeListeners.size === 0) listenersByNodeKey.delete(nodeKey);
|
|
665
|
+
};
|
|
666
|
+
}
|
|
667
|
+
};
|
|
668
|
+
};
|
|
669
|
+
const FONT_OWNERS = /* @__PURE__ */ new WeakMap();
|
|
670
|
+
const attachFontView = (fonts, view) => {
|
|
671
|
+
let owner = FONT_OWNERS.get(fonts);
|
|
672
|
+
if (!owner) {
|
|
673
|
+
const views = /* @__PURE__ */ new Set();
|
|
674
|
+
const invalidate = () => {
|
|
675
|
+
if (views.size === 0) return;
|
|
676
|
+
invalidatePageLayoutEngines(new Set([...views].map((entry) => entry.engine)));
|
|
677
|
+
views.forEach((entry) => entry.invalidate());
|
|
678
|
+
};
|
|
679
|
+
fonts.addEventListener("loadingdone", invalidate);
|
|
680
|
+
fonts.addEventListener("loadingerror", invalidate);
|
|
681
|
+
fonts.ready.then(invalidate);
|
|
682
|
+
owner = {
|
|
683
|
+
cleanup: () => {
|
|
684
|
+
fonts.removeEventListener("loadingdone", invalidate);
|
|
685
|
+
fonts.removeEventListener("loadingerror", invalidate);
|
|
686
|
+
},
|
|
687
|
+
views
|
|
688
|
+
};
|
|
689
|
+
FONT_OWNERS.set(fonts, owner);
|
|
690
|
+
}
|
|
691
|
+
owner.views.add(view);
|
|
692
|
+
return () => {
|
|
693
|
+
owner?.views.delete(view);
|
|
694
|
+
if (owner?.views.size === 0) {
|
|
695
|
+
owner.cleanup();
|
|
696
|
+
FONT_OWNERS.delete(fonts);
|
|
697
|
+
}
|
|
698
|
+
};
|
|
699
|
+
};
|
|
700
|
+
const SCROLLABLE_OVERFLOW_PATTERN = /(auto|scroll|overlay)/;
|
|
701
|
+
const parseCSSPixels = (value) => {
|
|
702
|
+
if (!value || value === "auto" || value === "none") return 0;
|
|
703
|
+
const parsed = Number.parseFloat(value);
|
|
704
|
+
return Number.isFinite(parsed) ? parsed : 0;
|
|
705
|
+
};
|
|
706
|
+
const canScroll = (element) => {
|
|
707
|
+
if (!element) return false;
|
|
708
|
+
const style = element.ownerDocument.defaultView?.getComputedStyle(element);
|
|
709
|
+
return SCROLLABLE_OVERFLOW_PATTERN.test(`${style?.overflow ?? ""} ${style?.overflowY ?? ""}`) && (element.clientHeight > 0 || parseCSSPixels(style?.height) > 0 || parseCSSPixels(style?.maxHeight) > 0);
|
|
710
|
+
};
|
|
711
|
+
const getScrollRoot = (element) => {
|
|
712
|
+
const editable = element?.querySelector("[data-editor=\"true\"]") ?? null;
|
|
713
|
+
if (canScroll(editable)) return editable;
|
|
714
|
+
let current = element;
|
|
715
|
+
while (current) {
|
|
716
|
+
if (canScroll(current)) return current;
|
|
717
|
+
current = current.parentElement;
|
|
718
|
+
}
|
|
719
|
+
return null;
|
|
720
|
+
};
|
|
721
|
+
const INACTIVE_VIEWPORT = {
|
|
722
|
+
canTrackContentViewport: false,
|
|
723
|
+
viewport: null
|
|
724
|
+
};
|
|
725
|
+
const EMPTY_PAGE_MOUNT_PLAN = {
|
|
726
|
+
itemIndexesByPath: /* @__PURE__ */ new Map(),
|
|
727
|
+
itemIndexesByTopLevelIndex: /* @__PURE__ */ new Map(),
|
|
728
|
+
items: []
|
|
729
|
+
};
|
|
730
|
+
const createViewportStore = () => {
|
|
731
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
732
|
+
let root = null;
|
|
733
|
+
let geometryHeight = 0;
|
|
734
|
+
let cleanup = () => {};
|
|
735
|
+
let lastScrollAt = Number.NEGATIVE_INFINITY;
|
|
736
|
+
let snapshot = INACTIVE_VIEWPORT;
|
|
737
|
+
let measured = null;
|
|
738
|
+
const publish = (next, sync = false) => {
|
|
739
|
+
const commit = () => {
|
|
740
|
+
snapshot = next;
|
|
741
|
+
listeners.forEach((listener) => listener());
|
|
742
|
+
};
|
|
743
|
+
if (sync) flushSync(commit);
|
|
744
|
+
else commit();
|
|
745
|
+
};
|
|
746
|
+
const restart = () => {
|
|
747
|
+
cleanup();
|
|
748
|
+
if (listeners.size === 0 || !root) return;
|
|
749
|
+
const scrollRoot = getScrollRoot(root);
|
|
750
|
+
if (!scrollRoot) {
|
|
751
|
+
publish(INACTIVE_VIEWPORT);
|
|
752
|
+
return;
|
|
753
|
+
}
|
|
754
|
+
const update = (sync = false) => {
|
|
755
|
+
if (!root) return;
|
|
756
|
+
const rootRect = root.getBoundingClientRect();
|
|
757
|
+
const scrollRect = scrollRoot.getBoundingClientRect();
|
|
758
|
+
const offset = root.contains(scrollRoot) ? scrollRoot.scrollTop : 0;
|
|
759
|
+
const scale = geometryHeight > 0 && rootRect.height > 0 ? rootRect.height / geometryHeight : 1;
|
|
760
|
+
const top = Math.max(0, (scrollRect.top - rootRect.top + offset) / scale);
|
|
761
|
+
measured = {
|
|
762
|
+
scale,
|
|
763
|
+
scrollRoot,
|
|
764
|
+
scrollTop: scrollRoot.scrollTop
|
|
765
|
+
};
|
|
766
|
+
publish({
|
|
767
|
+
canTrackContentViewport: true,
|
|
768
|
+
viewport: {
|
|
769
|
+
bottom: Math.max(top, (scrollRect.bottom - rootRect.top + offset) / scale),
|
|
770
|
+
top
|
|
771
|
+
}
|
|
772
|
+
}, sync);
|
|
773
|
+
};
|
|
774
|
+
const onScroll = () => {
|
|
775
|
+
lastScrollAt = performance.now();
|
|
776
|
+
update(true);
|
|
777
|
+
};
|
|
778
|
+
const onResize = () => update();
|
|
779
|
+
update();
|
|
780
|
+
scrollRoot.addEventListener("scroll", onScroll, { passive: true });
|
|
781
|
+
root.ownerDocument.defaultView?.addEventListener("resize", onResize);
|
|
782
|
+
const observer = typeof ResizeObserver === "undefined" ? null : new ResizeObserver(onResize);
|
|
783
|
+
observer?.observe(root);
|
|
784
|
+
observer?.observe(scrollRoot);
|
|
785
|
+
cleanup = () => {
|
|
786
|
+
scrollRoot.removeEventListener("scroll", onScroll);
|
|
787
|
+
root?.ownerDocument.defaultView?.removeEventListener("resize", onResize);
|
|
788
|
+
observer?.disconnect();
|
|
789
|
+
};
|
|
790
|
+
};
|
|
791
|
+
const getSnapshot = () => snapshot;
|
|
792
|
+
const readViewport = () => {
|
|
793
|
+
const { canTrackContentViewport, viewport } = snapshot;
|
|
794
|
+
if (!canTrackContentViewport || !viewport) return null;
|
|
795
|
+
const shift = measured ? (measured.scrollRoot.scrollTop - measured.scrollTop) / measured.scale : 0;
|
|
796
|
+
return {
|
|
797
|
+
bottom: viewport.bottom + shift,
|
|
798
|
+
top: viewport.top + shift
|
|
799
|
+
};
|
|
800
|
+
};
|
|
801
|
+
const subscribe = (listener) => {
|
|
802
|
+
listeners.add(listener);
|
|
803
|
+
if (listeners.size === 1) restart();
|
|
804
|
+
return () => {
|
|
805
|
+
listeners.delete(listener);
|
|
806
|
+
if (listeners.size === 0) {
|
|
807
|
+
cleanup();
|
|
808
|
+
cleanup = () => {};
|
|
809
|
+
}
|
|
810
|
+
};
|
|
811
|
+
};
|
|
812
|
+
return {
|
|
813
|
+
configure(nextRoot, nextHeight) {
|
|
814
|
+
root = nextRoot;
|
|
815
|
+
geometryHeight = nextHeight;
|
|
816
|
+
restart();
|
|
817
|
+
},
|
|
818
|
+
getLastScrollAt: () => lastScrollAt,
|
|
819
|
+
getSnapshot,
|
|
820
|
+
readViewport,
|
|
821
|
+
subscribe
|
|
822
|
+
};
|
|
823
|
+
};
|
|
824
|
+
const sameSelectedPaths = (left, right) => left === right || left !== null && left.length === right.length && left.every((path, index) => PathApi.equals(path, right[index]));
|
|
825
|
+
const selectedPathsFor = (selection) => selection ? [selection.anchor.path, selection.focus.path] : [];
|
|
826
|
+
const setRef = (ref, value) => {
|
|
827
|
+
if (typeof ref === "function") ref(value);
|
|
828
|
+
else if (ref) ref.current = value;
|
|
829
|
+
};
|
|
830
|
+
const createPlacementsByPath = (fragments, geometry) => {
|
|
831
|
+
const placements = /* @__PURE__ */ new Map();
|
|
832
|
+
const add = (path, placement) => {
|
|
833
|
+
const key = pathKey(path);
|
|
834
|
+
const values = placements.get(key) ?? [];
|
|
835
|
+
values.push(placement);
|
|
836
|
+
placements.set(key, values);
|
|
837
|
+
};
|
|
838
|
+
fragments.forEach((fragment) => {
|
|
839
|
+
add(fragment.path, {
|
|
840
|
+
pageIndex: fragment.pageIndex,
|
|
841
|
+
rect: toCanvasRect(fragment.rect, fragment.pageIndex, geometry)
|
|
842
|
+
});
|
|
843
|
+
if (fragment.type === "direct-children") fragment.children.forEach((child) => add(child.path, {
|
|
844
|
+
pageIndex: fragment.pageIndex,
|
|
845
|
+
rect: toCanvasRect(child.rect, fragment.pageIndex, geometry)
|
|
846
|
+
}));
|
|
847
|
+
});
|
|
848
|
+
return placements;
|
|
849
|
+
};
|
|
850
|
+
const boxRect = (rects) => {
|
|
851
|
+
const [first] = rects;
|
|
852
|
+
const sharesLeftEdge = rects.every((rect) => rect.left === first.left);
|
|
853
|
+
return {
|
|
854
|
+
rect: sharesLeftEdge ? bounds(rects) : first,
|
|
855
|
+
spillsPastBox: !sharesLeftEdge
|
|
856
|
+
};
|
|
857
|
+
};
|
|
858
|
+
const createElementLayouts = (fragments, placementsByPath, previous) => {
|
|
859
|
+
const paths = /* @__PURE__ */ new Map();
|
|
860
|
+
const textPaths = /* @__PURE__ */ new Set();
|
|
861
|
+
fragments.forEach((fragment) => {
|
|
862
|
+
paths.set(pathKey(fragment.path), fragment.path);
|
|
863
|
+
if (fragment.type === "text") textPaths.add(pathKey(fragment.path));
|
|
864
|
+
if (fragment.type === "direct-children") fragment.children.forEach((child) => paths.set(pathKey(child.path), child.path));
|
|
865
|
+
});
|
|
866
|
+
const boxes = /* @__PURE__ */ new Map();
|
|
867
|
+
placementsByPath.forEach((placements, key) => {
|
|
868
|
+
boxes.set(key, boxRect(placements.map((placement) => placement.rect)));
|
|
869
|
+
});
|
|
870
|
+
const layouts = /* @__PURE__ */ new Map();
|
|
871
|
+
paths.forEach((path, key) => {
|
|
872
|
+
const box = boxes.get(key);
|
|
873
|
+
if (!box) return;
|
|
874
|
+
let parent = path.slice(0, -1);
|
|
875
|
+
let parentRect;
|
|
876
|
+
while (parent.length > 0 && !parentRect) {
|
|
877
|
+
parentRect = boxes.get(pathKey(parent))?.rect;
|
|
878
|
+
parent = parent.slice(0, -1);
|
|
879
|
+
}
|
|
880
|
+
const layout = {
|
|
881
|
+
hasTextLines: textPaths.has(key),
|
|
882
|
+
height: box.rect.height,
|
|
883
|
+
left: box.rect.left - (parentRect?.left ?? 0),
|
|
884
|
+
spillsPastBox: box.spillsPastBox,
|
|
885
|
+
top: box.rect.top - (parentRect?.top ?? 0),
|
|
886
|
+
width: box.rect.width
|
|
887
|
+
};
|
|
888
|
+
const kept = previous.get(key);
|
|
889
|
+
layouts.set(key, kept && kept.hasTextLines === layout.hasTextLines && kept.height === layout.height && kept.left === layout.left && kept.spillsPastBox === layout.spillsPastBox && kept.top === layout.top && kept.width === layout.width ? kept : layout);
|
|
890
|
+
});
|
|
891
|
+
return layouts;
|
|
892
|
+
};
|
|
893
|
+
const EMPTY_ADMISSION = {
|
|
894
|
+
parked: [],
|
|
895
|
+
projected: []
|
|
896
|
+
};
|
|
897
|
+
const admitFragments = (fragments, mountedPageIndexes, mountedTopLevelIndexes) => {
|
|
898
|
+
const projected = [];
|
|
899
|
+
const parked = [];
|
|
900
|
+
fragments.forEach((fragment) => {
|
|
901
|
+
if (mountedPageIndexes.has(fragment.pageIndex)) projected.push(fragment);
|
|
902
|
+
else if (fragment.type === "text" && mountedTopLevelIndexes.has(fragment.path[0])) parked.push(fragment);
|
|
903
|
+
});
|
|
904
|
+
return {
|
|
905
|
+
parked,
|
|
906
|
+
projected
|
|
907
|
+
};
|
|
908
|
+
};
|
|
909
|
+
const sameItems = (left, right) => left.length === right.length && left.every((item, index) => item === right[index]);
|
|
910
|
+
const reuseUnchangedAdmission = (previous, next) => {
|
|
911
|
+
const sameProjected = sameItems(previous.projected, next.projected);
|
|
912
|
+
const sameParked = sameItems(previous.parked, next.parked);
|
|
913
|
+
if (sameProjected && sameParked) return previous;
|
|
914
|
+
return {
|
|
915
|
+
parked: sameParked ? previous.parked : next.parked,
|
|
916
|
+
projected: sameProjected ? previous.projected : next.projected
|
|
917
|
+
};
|
|
918
|
+
};
|
|
919
|
+
const PARK_INTERVALS = /* @__PURE__ */ new WeakMap();
|
|
920
|
+
const readParkIntervals = (fragment) => {
|
|
921
|
+
const cached = PARK_INTERVALS.get(fragment);
|
|
922
|
+
if (cached) return cached;
|
|
923
|
+
const intervals = /* @__PURE__ */ new Map();
|
|
924
|
+
fragment.lines.forEach((line) => line.runs.forEach(({ source }) => {
|
|
925
|
+
const key = pathKey(source.anchor.path);
|
|
926
|
+
const start = Math.min(source.anchor.offset, source.focus.offset);
|
|
927
|
+
const end = Math.max(source.anchor.offset, source.focus.offset);
|
|
928
|
+
const current = intervals.get(key);
|
|
929
|
+
intervals.set(key, {
|
|
930
|
+
end: Math.max(end, current?.end ?? end),
|
|
931
|
+
path: source.anchor.path,
|
|
932
|
+
start: Math.min(start, current?.start ?? start)
|
|
933
|
+
});
|
|
934
|
+
}));
|
|
935
|
+
const result = [...intervals.values()];
|
|
936
|
+
PARK_INTERVALS.set(fragment, result);
|
|
937
|
+
return result;
|
|
938
|
+
};
|
|
939
|
+
const NATIVE_FLOW_BREAK_RULE = `[${SOFT_LINE_FLAG}='true']::after{content:'\\A';white-space:pre}`;
|
|
940
|
+
const SOFT_LINE_END_RULE = `${SOFT_LINE_END_SELECTOR}::after{content:'\\A';white-space:pre}`;
|
|
941
|
+
const PARK_RULE = "[data-pagination-unmounted-page]{opacity:0;pointer-events:none}[data-pagination-unmounted-page],[data-pagination-unmounted-page] *{font-size:0!important;line-height:0!important;margin:0!important;padding:0!important;border-width:0!important}";
|
|
942
|
+
/**
|
|
943
|
+
* A line whose last character is a hard `\n` already breaks under the run's
|
|
944
|
+
* `white-space: pre`, so no run carries the native-flow break. A line that
|
|
945
|
+
* ends at an inline void's empty text breaks after the run before the void,
|
|
946
|
+
* because the void's renderer can wrap its text in an `inline-block`, as the
|
|
947
|
+
* registry mention does, and a forced break inside an inline-block ends that
|
|
948
|
+
* box's line, not the paragraph's.
|
|
949
|
+
*/
|
|
950
|
+
const nativeFlowBreakRunIndex = (line, readNode, isVoid) => {
|
|
951
|
+
const end = RangeApi.end(line.source);
|
|
952
|
+
const endIndex = line.runs.findIndex((run) => PointApi.equals(RangeApi.end(run.source), end));
|
|
953
|
+
const lastCharacter = line.runs.findLast((run, index) => index <= endIndex && RangeApi.isExpanded(run.source));
|
|
954
|
+
if (lastCharacter) {
|
|
955
|
+
const { offset, path } = RangeApi.end(lastCharacter.source);
|
|
956
|
+
const node = readNode(path);
|
|
957
|
+
if (TextApi.isText(node) && node.text[offset - 1] === "\n") return -1;
|
|
958
|
+
}
|
|
959
|
+
return line.runs.findLastIndex((run, index) => index <= endIndex && (RangeApi.isExpanded(run.source) || !isVoid(readNode(PathApi.parent(run.source.anchor.path)))));
|
|
960
|
+
};
|
|
961
|
+
const createPaginationBuckets = (editor, { parked, projected: fragments }, geometry, selectedPaths) => {
|
|
962
|
+
const buckets = /* @__PURE__ */ new Map();
|
|
963
|
+
const blockRects = /* @__PURE__ */ new Map();
|
|
964
|
+
const push = (nodeKey, slice) => {
|
|
965
|
+
const current = buckets.get(nodeKey) ?? [];
|
|
966
|
+
current.push(slice);
|
|
967
|
+
buckets.set(nodeKey, current);
|
|
968
|
+
};
|
|
969
|
+
fragments.forEach((fragment) => {
|
|
970
|
+
if (fragment.type !== "text") return;
|
|
971
|
+
const key = pathKey(fragment.path);
|
|
972
|
+
const rects = blockRects.get(key) ?? [];
|
|
973
|
+
rects.push(toCanvasRect(fragment.rect, fragment.pageIndex, geometry));
|
|
974
|
+
blockRects.set(key, rects);
|
|
975
|
+
});
|
|
976
|
+
const blockBoxes = /* @__PURE__ */ new Map();
|
|
977
|
+
blockRects.forEach((rects, key) => blockBoxes.set(key, boxRect(rects).rect));
|
|
978
|
+
editor.read((state) => {
|
|
979
|
+
const { index } = state.runtime.snapshot();
|
|
980
|
+
fragments.forEach((fragment) => {
|
|
981
|
+
if (fragment.type !== "text") return;
|
|
982
|
+
const blockKey = pathKey(fragment.path);
|
|
983
|
+
const nativeFlow = isNativeFlowBlock(fragment.path, blockRects.get(blockKey)?.length ?? 0, selectedPaths);
|
|
984
|
+
const blockRect = blockBoxes.get(blockKey) ?? toCanvasRect(fragment.rect, fragment.pageIndex, geometry);
|
|
985
|
+
fragment.lines.forEach((line, lineIndex) => {
|
|
986
|
+
const breakRunIndex = nativeFlow && lineIndex < fragment.lines.length - 1 ? nativeFlowBreakRunIndex(line, (path) => state.nodes.get(path)?.[0], (node) => NodeApi.isElement(node) && editor.read.schema.isVoid(node)) : -1;
|
|
987
|
+
line.runs.forEach((run, runIndex) => {
|
|
988
|
+
const nodeKey = index.keyAt(run.source.anchor.path);
|
|
989
|
+
if (!nodeKey) return;
|
|
990
|
+
const rangeStart = Math.min(run.source.anchor.offset, run.source.focus.offset);
|
|
991
|
+
const rangeEnd = Math.max(run.source.anchor.offset, run.source.focus.offset);
|
|
992
|
+
const rect = toCanvasRect(run.rect, fragment.pageIndex, geometry);
|
|
993
|
+
const runKey = `${fragment.pageIndex}:${lineIndex}:${runIndex}`;
|
|
994
|
+
const slice = createContainerDecorationSlice({
|
|
995
|
+
attributes: Object.freeze(nativeFlow ? {
|
|
996
|
+
"data-pagination-line": true,
|
|
997
|
+
[RUN_ATTRIBUTE]: runKey,
|
|
998
|
+
[SOFT_LINE_FLAG]: runIndex === breakRunIndex || void 0,
|
|
999
|
+
style: Object.freeze({ whiteSpace: "pre" })
|
|
1000
|
+
} : {
|
|
1001
|
+
"data-pagination-line": true,
|
|
1002
|
+
[RUN_ATTRIBUTE]: runKey,
|
|
1003
|
+
style: Object.freeze({
|
|
1004
|
+
display: "inline-block",
|
|
1005
|
+
height: rect.height,
|
|
1006
|
+
left: `calc(${rect.left - blockRect.left}px - var(${RUN_ORIGIN_LEFT}, 0px))`,
|
|
1007
|
+
lineHeight: `${rect.height}px`,
|
|
1008
|
+
minWidth: rect.width === 0 ? 1 : void 0,
|
|
1009
|
+
pointerEvents: "auto",
|
|
1010
|
+
position: "absolute",
|
|
1011
|
+
top: `calc(${rect.top - blockRect.top}px - var(${RUN_ORIGIN_TOP}, 0px))`,
|
|
1012
|
+
whiteSpace: "pre",
|
|
1013
|
+
width: rect.width
|
|
1014
|
+
})
|
|
1015
|
+
}),
|
|
1016
|
+
end: rangeEnd,
|
|
1017
|
+
key: `pagination:${fragment.pageIndex}:${blockKey}:${lineIndex}:${runIndex}`,
|
|
1018
|
+
start: rangeStart
|
|
1019
|
+
});
|
|
1020
|
+
push(nodeKey, slice);
|
|
1021
|
+
});
|
|
1022
|
+
});
|
|
1023
|
+
});
|
|
1024
|
+
parked.forEach((fragment) => {
|
|
1025
|
+
const blockKey = pathKey(fragment.path);
|
|
1026
|
+
if (!blockBoxes.has(blockKey)) return;
|
|
1027
|
+
readParkIntervals(fragment).forEach(({ end, path, start }) => {
|
|
1028
|
+
const nodeKey = index.keyAt(path);
|
|
1029
|
+
if (!nodeKey) return;
|
|
1030
|
+
push(nodeKey, createContainerDecorationSlice({
|
|
1031
|
+
attributes: Object.freeze({ "data-pagination-unmounted-page": fragment.pageIndex }),
|
|
1032
|
+
end,
|
|
1033
|
+
key: `pagination-park:${fragment.pageIndex}:${blockKey}:${pathKey(path)}`,
|
|
1034
|
+
start
|
|
1035
|
+
}));
|
|
1036
|
+
});
|
|
1037
|
+
});
|
|
1038
|
+
});
|
|
1039
|
+
return buckets;
|
|
1040
|
+
};
|
|
1041
|
+
const defaultRenderPage = ({ attributes }) => /* @__PURE__ */ jsx("div", { ...attributes });
|
|
1042
|
+
const pageDependency = (page) => "margins" in page && "preset" in page ? `${page.preset}:${JSON.stringify(page.margins)}` : page;
|
|
1043
|
+
const readEditorVersion = (editor) => editor.read((state) => state.lastCommit()?.version ?? state.runtime.snapshot().version);
|
|
1044
|
+
const samePageLayoutSnapshot = (left, right) => left.version === right.version && JSON.stringify(left) === JSON.stringify(right);
|
|
1045
|
+
const measurePagedEditable = (editor, options) => measurePages(editor, options);
|
|
1046
|
+
const createPagedCanvasStyles = (geometry, style) => ({
|
|
1047
|
+
editor: {
|
|
1048
|
+
inset: 0,
|
|
1049
|
+
pointerEvents: "auto",
|
|
1050
|
+
position: "absolute"
|
|
1051
|
+
},
|
|
1052
|
+
host: {
|
|
1053
|
+
...style,
|
|
1054
|
+
minHeight: geometry.height,
|
|
1055
|
+
position: "relative",
|
|
1056
|
+
width: geometry.width,
|
|
1057
|
+
zIndex: 0
|
|
1058
|
+
},
|
|
1059
|
+
overlay: {
|
|
1060
|
+
height: geometry.height,
|
|
1061
|
+
inset: 0,
|
|
1062
|
+
pointerEvents: "none",
|
|
1063
|
+
position: "absolute",
|
|
1064
|
+
width: geometry.width,
|
|
1065
|
+
zIndex: 1
|
|
1066
|
+
},
|
|
1067
|
+
root: {
|
|
1068
|
+
minHeight: geometry.height,
|
|
1069
|
+
position: "relative",
|
|
1070
|
+
width: geometry.width
|
|
1071
|
+
}
|
|
1072
|
+
});
|
|
1073
|
+
const RUN_ORIGIN_LEFT = "--editor-run-origin-left";
|
|
1074
|
+
const RUN_ORIGIN_TOP = "--editor-run-origin-top";
|
|
1075
|
+
const RUN_ORIGIN_TOLERANCE = .05;
|
|
1076
|
+
const readRunOriginChanges = (root) => {
|
|
1077
|
+
const changes = [];
|
|
1078
|
+
const boxes = /* @__PURE__ */ new Map();
|
|
1079
|
+
const parentRects = /* @__PURE__ */ new Map();
|
|
1080
|
+
const runParents = /* @__PURE__ */ new Set();
|
|
1081
|
+
root.querySelectorAll("[data-pagination-line]").forEach((run) => {
|
|
1082
|
+
const runParent = run.parentElement;
|
|
1083
|
+
if (run.style.position !== "absolute" || !runParent || runParents.has(runParent)) return;
|
|
1084
|
+
runParents.add(runParent);
|
|
1085
|
+
const { offsetParent } = run;
|
|
1086
|
+
const block = run.closest("[data-editor-node=\"element\"]:not([data-editor-inline=\"true\"])");
|
|
1087
|
+
if (!offsetParent || !block) return;
|
|
1088
|
+
let box = boxes.get(block);
|
|
1089
|
+
if (!box) {
|
|
1090
|
+
const placed = getElementPlacementBox(block);
|
|
1091
|
+
const rect = placed.getBoundingClientRect();
|
|
1092
|
+
box = {
|
|
1093
|
+
rect,
|
|
1094
|
+
scale: getElementTransformScale(placed, rect)
|
|
1095
|
+
};
|
|
1096
|
+
boxes.set(block, box);
|
|
1097
|
+
}
|
|
1098
|
+
let parentRect = parentRects.get(offsetParent);
|
|
1099
|
+
if (!parentRect) {
|
|
1100
|
+
parentRect = offsetParent.getBoundingClientRect();
|
|
1101
|
+
parentRects.set(offsetParent, parentRect);
|
|
1102
|
+
}
|
|
1103
|
+
const origin = {
|
|
1104
|
+
[RUN_ORIGIN_LEFT]: (parentRect.left - box.rect.left) / box.scale.x + offsetParent.clientLeft,
|
|
1105
|
+
[RUN_ORIGIN_TOP]: (parentRect.top - box.rect.top) / box.scale.y + offsetParent.clientTop
|
|
1106
|
+
};
|
|
1107
|
+
for (const property of [RUN_ORIGIN_LEFT, RUN_ORIGIN_TOP]) {
|
|
1108
|
+
const current = Number.parseFloat(runParent.style.getPropertyValue(property));
|
|
1109
|
+
if (Number.isNaN(current) || Math.abs(origin[property] - current) >= RUN_ORIGIN_TOLERANCE) changes.push([
|
|
1110
|
+
runParent,
|
|
1111
|
+
property,
|
|
1112
|
+
`${Math.round(origin[property] * 100) / 100}px`
|
|
1113
|
+
]);
|
|
1114
|
+
}
|
|
1115
|
+
});
|
|
1116
|
+
return changes;
|
|
1117
|
+
};
|
|
1118
|
+
const readRunOriginWrite = (root) => {
|
|
1119
|
+
const changes = readRunOriginChanges(root);
|
|
1120
|
+
if (changes.length === 0) return null;
|
|
1121
|
+
return () => {
|
|
1122
|
+
changes.forEach(([runParent, property, value]) => runParent.style.setProperty(property, value));
|
|
1123
|
+
};
|
|
1124
|
+
};
|
|
1125
|
+
const useReportEachNewMeasureFailure = (editor, error) => {
|
|
1126
|
+
const lastReportedFailure = useRef(null);
|
|
1127
|
+
useEffect(() => {
|
|
1128
|
+
if (!error) {
|
|
1129
|
+
lastReportedFailure.current = null;
|
|
1130
|
+
return;
|
|
1131
|
+
}
|
|
1132
|
+
const failure = error instanceof Error ? error.message : error;
|
|
1133
|
+
if (Object.is(failure, lastReportedFailure.current)) return;
|
|
1134
|
+
lastReportedFailure.current = failure;
|
|
1135
|
+
reportEditorLifecycleError({
|
|
1136
|
+
cause: error,
|
|
1137
|
+
editor,
|
|
1138
|
+
phase: "measure",
|
|
1139
|
+
source: "pagination"
|
|
1140
|
+
});
|
|
1141
|
+
}, [editor, error]);
|
|
1142
|
+
};
|
|
1143
|
+
const PagedEditableInner = ({ engine: suppliedEngine, fragmentation, ignoreBlankEditableRootClicks = true, page, pageView, ref: forwardedRef, renderPage = defaultRenderPage, style, typography, virtualize = false, ...editableProps }) => {
|
|
1144
|
+
const editor = useEditorContext();
|
|
1145
|
+
const inheritedDecorations = useContext(DecorationContext);
|
|
1146
|
+
const [fallbackEngine] = useState(() => createPretextPageLayoutEngine());
|
|
1147
|
+
const engine = suppliedEngine ?? fallbackEngine;
|
|
1148
|
+
const [fontEpoch, setFontEpoch] = useState(0);
|
|
1149
|
+
const [editableHost, setEditableHost] = useState(null);
|
|
1150
|
+
const rootRef = useRef(null);
|
|
1151
|
+
const publishedStore = useMemo(createPublishedLayoutStore, []);
|
|
1152
|
+
const decorationStore = useMemo(() => createSurfaceDecorationStore(inheritedDecorations), [inheritedDecorations]);
|
|
1153
|
+
const [viewportStore] = useState(createViewportStore);
|
|
1154
|
+
const selectedPaths = useEditorState((state) => selectedPathsFor(state.selection()), {
|
|
1155
|
+
equalityFn: sameSelectedPaths,
|
|
1156
|
+
shouldUpdate: (change) => !change || change.selectionChanged || change.changed.hasAny("structure")
|
|
1157
|
+
});
|
|
1158
|
+
const pageFieldKey = "margins" in page && "preset" in page ? null : page.key;
|
|
1159
|
+
const [documentVersion, setDocumentVersion] = useState(() => readEditorVersion(editor));
|
|
1160
|
+
const invalidationVersion = useRef(documentVersion);
|
|
1161
|
+
useLayoutEffect(() => {
|
|
1162
|
+
invalidationVersion.current = readEditorVersion(editor);
|
|
1163
|
+
setDocumentVersion(invalidationVersion.current);
|
|
1164
|
+
return editor.subscribeCommit((change) => {
|
|
1165
|
+
if (change.changed.hasAny("document") || pageFieldKey !== null && change.dirtyStateKeys.includes(pageFieldKey)) {
|
|
1166
|
+
invalidationVersion.current = change.version;
|
|
1167
|
+
setDocumentVersion(change.version);
|
|
1168
|
+
}
|
|
1169
|
+
});
|
|
1170
|
+
}, [editor, pageFieldKey]);
|
|
1171
|
+
const dependency = pageDependency(page);
|
|
1172
|
+
const canMeasure = useHydrated();
|
|
1173
|
+
const measuredSnapshot = useRef(null);
|
|
1174
|
+
const measurement = useMemo(() => {
|
|
1175
|
+
if (!canMeasure) return {
|
|
1176
|
+
error: null,
|
|
1177
|
+
snapshot: null
|
|
1178
|
+
};
|
|
1179
|
+
try {
|
|
1180
|
+
const snapshot = measurePagedEditable(editor, {
|
|
1181
|
+
engine,
|
|
1182
|
+
fragmentation,
|
|
1183
|
+
page,
|
|
1184
|
+
typography
|
|
1185
|
+
});
|
|
1186
|
+
if (readEditorVersion(editor) !== snapshot.version) return {
|
|
1187
|
+
error: null,
|
|
1188
|
+
snapshot: null
|
|
1189
|
+
};
|
|
1190
|
+
const previous = measuredSnapshot.current;
|
|
1191
|
+
const stable = previous && samePageLayoutSnapshot(previous, snapshot) ? previous : snapshot;
|
|
1192
|
+
measuredSnapshot.current = stable;
|
|
1193
|
+
return {
|
|
1194
|
+
error: null,
|
|
1195
|
+
snapshot: stable
|
|
1196
|
+
};
|
|
1197
|
+
} catch (error) {
|
|
1198
|
+
return {
|
|
1199
|
+
error,
|
|
1200
|
+
snapshot: null
|
|
1201
|
+
};
|
|
1202
|
+
}
|
|
1203
|
+
}, [
|
|
1204
|
+
canMeasure,
|
|
1205
|
+
dependency,
|
|
1206
|
+
documentVersion,
|
|
1207
|
+
editor,
|
|
1208
|
+
engine,
|
|
1209
|
+
fontEpoch,
|
|
1210
|
+
fragmentation,
|
|
1211
|
+
typography
|
|
1212
|
+
]);
|
|
1213
|
+
const { snapshot } = measurement;
|
|
1214
|
+
const gap = pageView?.gap ?? 24;
|
|
1215
|
+
const mode = pageView?.mode ?? "single";
|
|
1216
|
+
const geometry = useMemo(() => snapshot ? createPageLayoutGeometry({
|
|
1217
|
+
fragments: snapshot.fragments,
|
|
1218
|
+
gap,
|
|
1219
|
+
mode,
|
|
1220
|
+
pages: snapshot.pages
|
|
1221
|
+
}) : null, [
|
|
1222
|
+
gap,
|
|
1223
|
+
mode,
|
|
1224
|
+
snapshot
|
|
1225
|
+
]);
|
|
1226
|
+
useReportEachNewMeasureFailure(editor, measurement.error);
|
|
1227
|
+
useLayoutEffect(() => decorationStore.mount(), [decorationStore]);
|
|
1228
|
+
useEffect(() => () => decorationStore.destroy(), [decorationStore]);
|
|
1229
|
+
useLayoutEffect(() => {
|
|
1230
|
+
if (!editableHost) return void 0;
|
|
1231
|
+
return bindLayoutHost(editableHost, publishedStore);
|
|
1232
|
+
}, [editableHost, publishedStore]);
|
|
1233
|
+
useLayoutEffect(() => {
|
|
1234
|
+
publishedStore.setSnapshot(snapshot);
|
|
1235
|
+
}, [publishedStore, snapshot]);
|
|
1236
|
+
const viewLayout = useRef(null);
|
|
1237
|
+
const viewLines = useMemo(() => createPagedViewLines({
|
|
1238
|
+
editor: toReactRuntimeEditor(editor),
|
|
1239
|
+
readLayout: () => viewLayout.current,
|
|
1240
|
+
readVersion: () => invalidationVersion.current,
|
|
1241
|
+
readViewport: viewportStore.readViewport
|
|
1242
|
+
}), [editor, viewportStore]);
|
|
1243
|
+
useEffect(() => {
|
|
1244
|
+
const fonts = editableHost?.ownerDocument.fonts;
|
|
1245
|
+
if (!fonts) return void 0;
|
|
1246
|
+
return attachFontView(fonts, {
|
|
1247
|
+
engine,
|
|
1248
|
+
invalidate: () => setFontEpoch((value) => value + 1)
|
|
1249
|
+
});
|
|
1250
|
+
}, [editableHost, engine]);
|
|
1251
|
+
const editableRef = useCallback((host) => {
|
|
1252
|
+
setEditableHost(host);
|
|
1253
|
+
setRef(forwardedRef, host);
|
|
1254
|
+
}, [forwardedRef]);
|
|
1255
|
+
const pageMountPlan = useMemo(() => snapshot && geometry ? createPagedEditablePageMountPlan({
|
|
1256
|
+
fragments: snapshot.fragments,
|
|
1257
|
+
geometry,
|
|
1258
|
+
mode,
|
|
1259
|
+
pages: snapshot.pages
|
|
1260
|
+
}) : EMPTY_PAGE_MOUNT_PLAN, [
|
|
1261
|
+
geometry,
|
|
1262
|
+
mode,
|
|
1263
|
+
snapshot
|
|
1264
|
+
]);
|
|
1265
|
+
useLayoutEffect(() => {
|
|
1266
|
+
viewportStore.configure(rootRef.current, geometry?.height ?? 0);
|
|
1267
|
+
}, [geometry?.height, viewportStore]);
|
|
1268
|
+
const viewportState = useSyncExternalStore(viewportStore.subscribe, viewportStore.getSnapshot, () => INACTIVE_VIEWPORT);
|
|
1269
|
+
const visibleItems = useMemo(() => virtualize ? getPagedEditableVisiblePageMountItems(pageMountPlan, {
|
|
1270
|
+
gap,
|
|
1271
|
+
overscan: 0,
|
|
1272
|
+
pages: snapshot?.pages ?? [],
|
|
1273
|
+
virtualizes: true,
|
|
1274
|
+
viewport: viewportState.canTrackContentViewport ? viewportState.viewport : null
|
|
1275
|
+
}) : pageMountPlan.items, [
|
|
1276
|
+
gap,
|
|
1277
|
+
pageMountPlan,
|
|
1278
|
+
snapshot?.pages,
|
|
1279
|
+
viewportState,
|
|
1280
|
+
virtualize
|
|
1281
|
+
]);
|
|
1282
|
+
const [promotedPath, setPromotedPath] = useState(null);
|
|
1283
|
+
const windowedItems = useMemo(() => {
|
|
1284
|
+
const requiredItemIndexes = new Set(visibleItems.map((item) => item.index));
|
|
1285
|
+
[...selectedPaths, promotedPath].forEach((path) => {
|
|
1286
|
+
if (!path) return;
|
|
1287
|
+
let ownerPath = path;
|
|
1288
|
+
let itemIndexes;
|
|
1289
|
+
while (ownerPath.length > 0 && !itemIndexes) {
|
|
1290
|
+
itemIndexes = pageMountPlan.itemIndexesByPath.get(pathKey(ownerPath));
|
|
1291
|
+
ownerPath = ownerPath.slice(0, -1);
|
|
1292
|
+
}
|
|
1293
|
+
itemIndexes?.forEach((itemIndex) => requiredItemIndexes.add(itemIndex));
|
|
1294
|
+
});
|
|
1295
|
+
return pageMountPlan.items.filter((item) => requiredItemIndexes.has(item.index));
|
|
1296
|
+
}, [
|
|
1297
|
+
pageMountPlan,
|
|
1298
|
+
promotedPath,
|
|
1299
|
+
selectedPaths,
|
|
1300
|
+
visibleItems
|
|
1301
|
+
]);
|
|
1302
|
+
const visiblePageIndexes = useMemo(() => new Set(windowedItems.flatMap((item) => item.pageIndexes)), [windowedItems]);
|
|
1303
|
+
const previousMountedTopLevelIndexes = useRef([]);
|
|
1304
|
+
const mountedTopLevelIndexes = useMemo(() => {
|
|
1305
|
+
const next = [...new Set(windowedItems.flatMap((item) => item.topLevelIndexes))].sort((left, right) => left - right);
|
|
1306
|
+
const previous = previousMountedTopLevelIndexes.current;
|
|
1307
|
+
if (sameItems(previous, next)) return previous;
|
|
1308
|
+
previousMountedTopLevelIndexes.current = next;
|
|
1309
|
+
return next;
|
|
1310
|
+
}, [windowedItems]);
|
|
1311
|
+
const previousAdmission = useRef(EMPTY_ADMISSION);
|
|
1312
|
+
const admission = useMemo(() => {
|
|
1313
|
+
const fragments = snapshot?.fragments ?? [];
|
|
1314
|
+
const next = virtualize ? admitFragments(fragments, visiblePageIndexes, new Set(mountedTopLevelIndexes)) : {
|
|
1315
|
+
parked: EMPTY_ADMISSION.parked,
|
|
1316
|
+
projected: fragments
|
|
1317
|
+
};
|
|
1318
|
+
const kept = reuseUnchangedAdmission(previousAdmission.current, next);
|
|
1319
|
+
previousAdmission.current = kept;
|
|
1320
|
+
return kept;
|
|
1321
|
+
}, [
|
|
1322
|
+
mountedTopLevelIndexes,
|
|
1323
|
+
snapshot?.fragments,
|
|
1324
|
+
virtualize,
|
|
1325
|
+
visiblePageIndexes
|
|
1326
|
+
]);
|
|
1327
|
+
const projectedFragments = admission.projected;
|
|
1328
|
+
const placementsByPath = useMemo(() => geometry ? createPlacementsByPath(projectedFragments, geometry) : /* @__PURE__ */ new Map(), [geometry, projectedFragments]);
|
|
1329
|
+
const previousElementLayouts = useRef(/* @__PURE__ */ new Map());
|
|
1330
|
+
const elementLayouts = useMemo(() => {
|
|
1331
|
+
if (!geometry) return void 0;
|
|
1332
|
+
const layouts = createElementLayouts(snapshot?.fragments ?? [], placementsByPath, previousElementLayouts.current);
|
|
1333
|
+
previousElementLayouts.current = layouts;
|
|
1334
|
+
return layouts;
|
|
1335
|
+
}, [
|
|
1336
|
+
geometry,
|
|
1337
|
+
placementsByPath,
|
|
1338
|
+
snapshot?.fragments
|
|
1339
|
+
]);
|
|
1340
|
+
useLayoutEffect(() => {
|
|
1341
|
+
viewLayout.current = snapshot ? {
|
|
1342
|
+
canvas: rootRef.current,
|
|
1343
|
+
geometry,
|
|
1344
|
+
measuredVersion: documentVersion,
|
|
1345
|
+
mountedPageIndexes: virtualize ? visiblePageIndexes : "all",
|
|
1346
|
+
root: editableHost,
|
|
1347
|
+
selectedPaths,
|
|
1348
|
+
snapshot
|
|
1349
|
+
} : null;
|
|
1350
|
+
}, [
|
|
1351
|
+
documentVersion,
|
|
1352
|
+
editableHost,
|
|
1353
|
+
geometry,
|
|
1354
|
+
selectedPaths,
|
|
1355
|
+
snapshot,
|
|
1356
|
+
virtualize,
|
|
1357
|
+
visiblePageIndexes
|
|
1358
|
+
]);
|
|
1359
|
+
const localBuckets = useMemo(() => geometry ? createPaginationBuckets(editor, admission, geometry, selectedPaths) : /* @__PURE__ */ new Map(), [
|
|
1360
|
+
admission,
|
|
1361
|
+
editor,
|
|
1362
|
+
geometry,
|
|
1363
|
+
selectedPaths
|
|
1364
|
+
]);
|
|
1365
|
+
useEditableLayoutPass(editor, editableHost, "Pagination run origins", readRunOriginWrite);
|
|
1366
|
+
const fragmentContext = useMemo(() => ({ placementsByPath }), [placementsByPath]);
|
|
1367
|
+
useLayoutEffect(() => {
|
|
1368
|
+
decorationStore.setLocalBuckets(localBuckets);
|
|
1369
|
+
}, [decorationStore, localBuckets]);
|
|
1370
|
+
const scrollSelectionIntoView = useMemo(() => {
|
|
1371
|
+
const custom = editableProps.scrollSelectionIntoView;
|
|
1372
|
+
return ((innerEditor, domRange) => {
|
|
1373
|
+
if (virtualize && performance.now() - viewportStore.getLastScrollAt() < 500) return;
|
|
1374
|
+
if (custom) custom(innerEditor, domRange);
|
|
1375
|
+
else defaultScrollSelectionIntoView(editor, domRange);
|
|
1376
|
+
});
|
|
1377
|
+
}, [
|
|
1378
|
+
editableProps.scrollSelectionIntoView,
|
|
1379
|
+
editor,
|
|
1380
|
+
viewportStore,
|
|
1381
|
+
virtualize
|
|
1382
|
+
]);
|
|
1383
|
+
const scrollToPath = useCallback((path, align = "auto") => {
|
|
1384
|
+
const index = path[0];
|
|
1385
|
+
const itemIndex = typeof index === "number" ? pageMountPlan.itemIndexesByTopLevelIndex.get(index)?.[0] : void 0;
|
|
1386
|
+
const item = itemIndex === void 0 ? void 0 : pageMountPlan.items[itemIndex];
|
|
1387
|
+
const scrollRoot = getScrollRoot(rootRef.current);
|
|
1388
|
+
if (!item || !scrollRoot) return false;
|
|
1389
|
+
const viewportHeight = Math.max(1, scrollRoot.clientHeight || scrollRoot.getBoundingClientRect().height);
|
|
1390
|
+
const top = align === "center" ? item.start - (viewportHeight - item.size) / 2 : align === "end" ? item.start - viewportHeight + item.size : item.start;
|
|
1391
|
+
scrollRoot.scrollTo({ top: Math.max(0, top) });
|
|
1392
|
+
return true;
|
|
1393
|
+
}, [pageMountPlan]);
|
|
1394
|
+
const { pageArrayIndex, pageByIndex } = useMemo(() => {
|
|
1395
|
+
const pages = snapshot?.pages ?? [];
|
|
1396
|
+
return {
|
|
1397
|
+
pageArrayIndex: new Map(pages.map((value, index) => [value.index, index])),
|
|
1398
|
+
pageByIndex: new Map(pages.map((value) => [value.index, value]))
|
|
1399
|
+
};
|
|
1400
|
+
}, [snapshot?.pages]);
|
|
1401
|
+
const canvas = geometry && createPagedCanvasStyles(geometry, style);
|
|
1402
|
+
return /* @__PURE__ */ jsx(FragmentContext, {
|
|
1403
|
+
value: fragmentContext,
|
|
1404
|
+
children: /* @__PURE__ */ jsxs("div", {
|
|
1405
|
+
"data-editor-paged-editable": true,
|
|
1406
|
+
"data-editor-paged-editable-page-virtualization": canvas && virtualize ? "true" : void 0,
|
|
1407
|
+
ref: rootRef,
|
|
1408
|
+
style: canvas?.root,
|
|
1409
|
+
children: [
|
|
1410
|
+
/* @__PURE__ */ jsx("style", {
|
|
1411
|
+
href: "editor-pagination-native-flow-break",
|
|
1412
|
+
precedence: "editor",
|
|
1413
|
+
children: NATIVE_FLOW_BREAK_RULE
|
|
1414
|
+
}),
|
|
1415
|
+
/* @__PURE__ */ jsx("style", {
|
|
1416
|
+
href: "editor-pagination-park",
|
|
1417
|
+
precedence: "editor",
|
|
1418
|
+
children: PARK_RULE
|
|
1419
|
+
}),
|
|
1420
|
+
/* @__PURE__ */ jsx("style", {
|
|
1421
|
+
href: "editor-pagination-soft-line-end",
|
|
1422
|
+
precedence: "editor",
|
|
1423
|
+
children: SOFT_LINE_END_RULE
|
|
1424
|
+
}),
|
|
1425
|
+
geometry && (virtualize ? windowedItems : pageMountPlan.items).flatMap((item) => item.pageIndexes.map((pageIndex) => {
|
|
1426
|
+
const layoutPage = pageByIndex.get(pageIndex);
|
|
1427
|
+
const arrayIndex = pageArrayIndex.get(pageIndex);
|
|
1428
|
+
if (!layoutPage || arrayIndex === void 0) return null;
|
|
1429
|
+
const placement = geometry.pagePlacements[arrayIndex] ?? {
|
|
1430
|
+
left: 0,
|
|
1431
|
+
top: 0
|
|
1432
|
+
};
|
|
1433
|
+
const occupied = geometry.occupiedSizes[arrayIndex] ?? layoutPage;
|
|
1434
|
+
const attributes = {
|
|
1435
|
+
"data-editor-page": true,
|
|
1436
|
+
"data-editor-page-index": layoutPage.index,
|
|
1437
|
+
style: {
|
|
1438
|
+
boxSizing: "border-box",
|
|
1439
|
+
height: layoutPage.height,
|
|
1440
|
+
overflow: "hidden",
|
|
1441
|
+
pointerEvents: "none",
|
|
1442
|
+
position: "relative",
|
|
1443
|
+
width: layoutPage.width
|
|
1444
|
+
}
|
|
1445
|
+
};
|
|
1446
|
+
return /* @__PURE__ */ jsx("div", {
|
|
1447
|
+
"data-editor-page-mount-item-index": item.index,
|
|
1448
|
+
"data-editor-page-surface": true,
|
|
1449
|
+
style: {
|
|
1450
|
+
height: occupied.height,
|
|
1451
|
+
left: placement.left,
|
|
1452
|
+
pointerEvents: "none",
|
|
1453
|
+
position: "absolute",
|
|
1454
|
+
top: placement.top,
|
|
1455
|
+
width: occupied.width
|
|
1456
|
+
},
|
|
1457
|
+
children: renderPage({
|
|
1458
|
+
attributes,
|
|
1459
|
+
page: layoutPage
|
|
1460
|
+
})
|
|
1461
|
+
}, layoutPage.index);
|
|
1462
|
+
})),
|
|
1463
|
+
/* @__PURE__ */ jsx("div", {
|
|
1464
|
+
"data-editor-paged-editable-editor-overlay": true,
|
|
1465
|
+
style: canvas?.overlay,
|
|
1466
|
+
children: /* @__PURE__ */ jsx("div", {
|
|
1467
|
+
"data-editor-paged-editable-editor": true,
|
|
1468
|
+
style: canvas?.editor,
|
|
1469
|
+
children: /* @__PURE__ */ jsx(ImperativeTextFlowContext, {
|
|
1470
|
+
value: false,
|
|
1471
|
+
children: /* @__PURE__ */ jsx(TextRenderScopeContext, {
|
|
1472
|
+
value: "paged",
|
|
1473
|
+
children: /* @__PURE__ */ jsx(WindowedEditable, {
|
|
1474
|
+
...editableProps,
|
|
1475
|
+
decorationStore,
|
|
1476
|
+
elementLayouts,
|
|
1477
|
+
enabled: Boolean(canvas) && virtualize,
|
|
1478
|
+
ignoreBlankEditableRootClicks,
|
|
1479
|
+
mountedTopLevelIndexes,
|
|
1480
|
+
onRequestMount: (index, path) => setPromotedPath(path ?? [index]),
|
|
1481
|
+
ref: editableRef,
|
|
1482
|
+
scrollSelectionIntoView,
|
|
1483
|
+
scrollToPath,
|
|
1484
|
+
style: canvas ? canvas.host : style,
|
|
1485
|
+
totalSize: geometry?.height ?? 0,
|
|
1486
|
+
viewLines
|
|
1487
|
+
})
|
|
1488
|
+
})
|
|
1489
|
+
})
|
|
1490
|
+
})
|
|
1491
|
+
})
|
|
1492
|
+
]
|
|
1493
|
+
})
|
|
1494
|
+
});
|
|
1495
|
+
};
|
|
1496
|
+
const PagedEditable = ({ root, ...props }) => {
|
|
1497
|
+
if (root === "main") throw new Error("Omit root to render the primary document.");
|
|
1498
|
+
const editor = useEditorContext();
|
|
1499
|
+
const inheritedReadOnly = useEditorReadOnly();
|
|
1500
|
+
const editable = /* @__PURE__ */ jsx(PagedEditableInner, { ...props });
|
|
1501
|
+
return root === void 0 ? editable : /* @__PURE__ */ jsx(SurfaceRoot, {
|
|
1502
|
+
editor,
|
|
1503
|
+
readOnly: props.readOnly || inheritedReadOnly,
|
|
1504
|
+
root,
|
|
1505
|
+
children: editable
|
|
1506
|
+
});
|
|
1507
|
+
};
|
|
1508
|
+
//#endregion
|
|
1509
|
+
export { PagedEditable, createEstimatedPageLayoutEngine, createPretextPageLayoutEngine, measurePages, pageSettingsCodec, usePageLayout, usePageLayoutFragments };
|