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,1021 @@
|
|
|
1
|
+
import { B as NodeApi, Yc as toInternalRoot, hu as PointApi } from "./plugin-DWzkJbnJ.js";
|
|
2
|
+
import { clearCache, layoutWithLines, measureNaturalWidth, prepareWithSegments } from "@chenglou/pretext";
|
|
3
|
+
import { materializeRichInlineLineRange, prepareRichInline, walkRichInlineLineRanges } from "@chenglou/pretext/rich-inline";
|
|
4
|
+
//#region src/pagination/page-composition.internal.ts
|
|
5
|
+
const PAGE_PRESETS = {
|
|
6
|
+
a4: {
|
|
7
|
+
height: 1123,
|
|
8
|
+
width: 794
|
|
9
|
+
},
|
|
10
|
+
letter: {
|
|
11
|
+
height: 1056,
|
|
12
|
+
width: 816
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
const normalizeMargins = (margins) => typeof margins === "number" ? {
|
|
16
|
+
bottom: margins,
|
|
17
|
+
left: margins,
|
|
18
|
+
right: margins,
|
|
19
|
+
top: margins
|
|
20
|
+
} : margins;
|
|
21
|
+
const createPage = (settings, index = 0) => {
|
|
22
|
+
const size = PAGE_PRESETS[settings.preset];
|
|
23
|
+
const margins = normalizeMargins(settings.margins);
|
|
24
|
+
return {
|
|
25
|
+
content: {
|
|
26
|
+
height: Math.max(0, size.height - margins.top - margins.bottom),
|
|
27
|
+
left: margins.left,
|
|
28
|
+
top: margins.top,
|
|
29
|
+
width: Math.max(0, size.width - margins.left - margins.right)
|
|
30
|
+
},
|
|
31
|
+
height: size.height,
|
|
32
|
+
index,
|
|
33
|
+
width: size.width
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
const createFragmentRect = (rects) => {
|
|
37
|
+
if (rects.length === 0) return {
|
|
38
|
+
height: 0,
|
|
39
|
+
left: 0,
|
|
40
|
+
top: 0,
|
|
41
|
+
width: 0
|
|
42
|
+
};
|
|
43
|
+
const left = Math.min(...rects.map((rect) => rect.left));
|
|
44
|
+
const top = Math.min(...rects.map((rect) => rect.top));
|
|
45
|
+
const right = Math.max(...rects.map((rect) => rect.left + rect.width));
|
|
46
|
+
return {
|
|
47
|
+
height: Math.max(...rects.map((rect) => rect.top + rect.height)) - top,
|
|
48
|
+
left,
|
|
49
|
+
top,
|
|
50
|
+
width: right - left
|
|
51
|
+
};
|
|
52
|
+
};
|
|
53
|
+
const freezeOwnedRange = (range) => {
|
|
54
|
+
if (Object.isFrozen(range)) return range;
|
|
55
|
+
if (!Object.isFrozen(range.anchor.path)) Object.freeze(range.anchor.path);
|
|
56
|
+
if (!Object.isFrozen(range.anchor)) Object.freeze(range.anchor);
|
|
57
|
+
if (!Object.isFrozen(range.focus.path)) Object.freeze(range.focus.path);
|
|
58
|
+
if (!Object.isFrozen(range.focus)) Object.freeze(range.focus);
|
|
59
|
+
return Object.freeze(range);
|
|
60
|
+
};
|
|
61
|
+
const freezeOwnedFragment = (fragment) => {
|
|
62
|
+
Object.freeze(fragment.path);
|
|
63
|
+
Object.freeze(fragment.rect);
|
|
64
|
+
if (fragment.type === "direct-children") {
|
|
65
|
+
fragment.children.forEach((child) => {
|
|
66
|
+
Object.freeze(child.path);
|
|
67
|
+
Object.freeze(child.rect);
|
|
68
|
+
Object.freeze(child);
|
|
69
|
+
});
|
|
70
|
+
Object.freeze(fragment.children);
|
|
71
|
+
} else if (fragment.type === "text") {
|
|
72
|
+
fragment.lines.forEach((line) => {
|
|
73
|
+
Object.freeze(line.rect);
|
|
74
|
+
line.runs.forEach((run) => {
|
|
75
|
+
Object.freeze(run.rect);
|
|
76
|
+
freezeOwnedRange(run.source);
|
|
77
|
+
Object.freeze(run);
|
|
78
|
+
});
|
|
79
|
+
Object.freeze(line.runs);
|
|
80
|
+
freezeOwnedRange(line.source);
|
|
81
|
+
Object.freeze(line);
|
|
82
|
+
});
|
|
83
|
+
Object.freeze(fragment.lines);
|
|
84
|
+
}
|
|
85
|
+
Object.freeze(fragment);
|
|
86
|
+
};
|
|
87
|
+
const placeBlock = (block, start, frame) => {
|
|
88
|
+
const contentAt = (index) => (index === 0 ? frame.first : frame.later).content;
|
|
89
|
+
const fragments = [];
|
|
90
|
+
let { pageIndex } = start;
|
|
91
|
+
let content = contentAt(pageIndex);
|
|
92
|
+
let cursorTop = start.top;
|
|
93
|
+
const advancePage = () => {
|
|
94
|
+
pageIndex += 1;
|
|
95
|
+
content = contentAt(pageIndex);
|
|
96
|
+
cursorTop = content.top;
|
|
97
|
+
};
|
|
98
|
+
const ensureFits = (height) => {
|
|
99
|
+
const remaining = content.top + content.height - cursorTop;
|
|
100
|
+
if (cursorTop !== content.top && height > remaining) advancePage();
|
|
101
|
+
};
|
|
102
|
+
if (block.type === "atomic") {
|
|
103
|
+
ensureFits(block.size.height);
|
|
104
|
+
fragments.push({
|
|
105
|
+
pageIndex,
|
|
106
|
+
path: [...block.path],
|
|
107
|
+
rect: {
|
|
108
|
+
height: block.size.height,
|
|
109
|
+
left: content.left,
|
|
110
|
+
top: cursorTop,
|
|
111
|
+
width: block.size.width
|
|
112
|
+
},
|
|
113
|
+
type: "atomic"
|
|
114
|
+
});
|
|
115
|
+
cursorTop += block.size.height + block.spacingAfter;
|
|
116
|
+
return {
|
|
117
|
+
end: {
|
|
118
|
+
pageIndex,
|
|
119
|
+
top: cursorTop
|
|
120
|
+
},
|
|
121
|
+
fragments
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
if (block.type === "direct-children") {
|
|
125
|
+
let consumed = 0;
|
|
126
|
+
while (consumed < block.children.length) {
|
|
127
|
+
const fragmentChildren = [];
|
|
128
|
+
while (consumed < block.children.length) {
|
|
129
|
+
const child = block.children[consumed];
|
|
130
|
+
const remaining = content.top + content.height - cursorTop;
|
|
131
|
+
const pageIsEmpty = cursorTop === content.top;
|
|
132
|
+
if (child.size.height > remaining && !pageIsEmpty) break;
|
|
133
|
+
fragmentChildren.push({
|
|
134
|
+
path: [...child.path],
|
|
135
|
+
rect: {
|
|
136
|
+
height: child.size.height,
|
|
137
|
+
left: content.left,
|
|
138
|
+
top: cursorTop,
|
|
139
|
+
width: child.size.width
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
cursorTop += child.size.height;
|
|
143
|
+
consumed += 1;
|
|
144
|
+
if (child.size.height > remaining && pageIsEmpty) break;
|
|
145
|
+
}
|
|
146
|
+
if (fragmentChildren.length === 0) {
|
|
147
|
+
advancePage();
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
fragments.push({
|
|
151
|
+
children: fragmentChildren,
|
|
152
|
+
pageIndex,
|
|
153
|
+
path: [...block.path],
|
|
154
|
+
rect: createFragmentRect(fragmentChildren.map((child) => child.rect)),
|
|
155
|
+
type: "direct-children"
|
|
156
|
+
});
|
|
157
|
+
if (consumed < block.children.length) advancePage();
|
|
158
|
+
}
|
|
159
|
+
cursorTop += block.spacingAfter;
|
|
160
|
+
return {
|
|
161
|
+
end: {
|
|
162
|
+
pageIndex,
|
|
163
|
+
top: cursorTop
|
|
164
|
+
},
|
|
165
|
+
fragments
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
const totalHeight = block.lines.reduce((sum, line) => sum + line.height, 0) + block.spacingAfter;
|
|
169
|
+
if (block.keepTogether && totalHeight <= frame.first.content.height) ensureFits(totalHeight);
|
|
170
|
+
let consumed = 0;
|
|
171
|
+
while (consumed < block.lines.length) {
|
|
172
|
+
const fragmentLines = [];
|
|
173
|
+
while (consumed < block.lines.length) {
|
|
174
|
+
const line = block.lines[consumed];
|
|
175
|
+
const remaining = content.top + content.height - cursorTop;
|
|
176
|
+
const pageIsEmpty = cursorTop === content.top;
|
|
177
|
+
if (line.height > remaining && !pageIsEmpty) break;
|
|
178
|
+
const measuredWidth = Math.max(line.width, ...line.runs.map((run) => run.left + run.width));
|
|
179
|
+
fragmentLines.push({
|
|
180
|
+
rect: {
|
|
181
|
+
height: line.height,
|
|
182
|
+
left: content.left,
|
|
183
|
+
top: cursorTop,
|
|
184
|
+
width: measuredWidth
|
|
185
|
+
},
|
|
186
|
+
runs: line.runs.map((run) => ({
|
|
187
|
+
rect: {
|
|
188
|
+
height: line.height,
|
|
189
|
+
left: content.left + run.left,
|
|
190
|
+
top: cursorTop,
|
|
191
|
+
width: run.width
|
|
192
|
+
},
|
|
193
|
+
source: run.source
|
|
194
|
+
})),
|
|
195
|
+
source: line.source
|
|
196
|
+
});
|
|
197
|
+
cursorTop += line.height;
|
|
198
|
+
consumed += 1;
|
|
199
|
+
if (line.height > remaining && pageIsEmpty) break;
|
|
200
|
+
}
|
|
201
|
+
if (fragmentLines.length === 0) {
|
|
202
|
+
advancePage();
|
|
203
|
+
continue;
|
|
204
|
+
}
|
|
205
|
+
fragments.push({
|
|
206
|
+
lines: fragmentLines,
|
|
207
|
+
pageIndex,
|
|
208
|
+
path: [...block.path],
|
|
209
|
+
rect: createFragmentRect(fragmentLines.map((line) => line.rect)),
|
|
210
|
+
type: "text"
|
|
211
|
+
});
|
|
212
|
+
if (consumed < block.lines.length) advancePage();
|
|
213
|
+
}
|
|
214
|
+
cursorTop += block.spacingAfter;
|
|
215
|
+
return {
|
|
216
|
+
end: {
|
|
217
|
+
pageIndex,
|
|
218
|
+
top: cursorTop
|
|
219
|
+
},
|
|
220
|
+
fragments
|
|
221
|
+
};
|
|
222
|
+
};
|
|
223
|
+
const samePageFrame = (left, right) => left.height === right.height && left.width === right.width && left.content.height === right.content.height && left.content.left === right.content.left && left.content.top === right.content.top && left.content.width === right.content.width;
|
|
224
|
+
const createFlowPage = (frame, index, frozen) => {
|
|
225
|
+
if (index === 0) return frame.first;
|
|
226
|
+
const next = {
|
|
227
|
+
...frame.later,
|
|
228
|
+
content: { ...frame.later.content },
|
|
229
|
+
index
|
|
230
|
+
};
|
|
231
|
+
if (!frozen) return next;
|
|
232
|
+
Object.freeze(next.content);
|
|
233
|
+
return Object.freeze(next);
|
|
234
|
+
};
|
|
235
|
+
const createPageComposer = (source) => {
|
|
236
|
+
let last = null;
|
|
237
|
+
let generation = 0;
|
|
238
|
+
const measure = (block, blockIndex, input, previous, started) => {
|
|
239
|
+
if (block.type !== "text") return previous?.block === block ? previous : {
|
|
240
|
+
measured: block,
|
|
241
|
+
mode: null
|
|
242
|
+
};
|
|
243
|
+
const mode = source.mode(block, blockIndex, input);
|
|
244
|
+
if (previous?.block === block && previous.mode === mode && generation === started) return previous;
|
|
245
|
+
return {
|
|
246
|
+
measured: {
|
|
247
|
+
...block,
|
|
248
|
+
lines: source.lines(block, mode, input.page)
|
|
249
|
+
},
|
|
250
|
+
mode
|
|
251
|
+
};
|
|
252
|
+
};
|
|
253
|
+
const composeFrom = (input, previous, frozen) => {
|
|
254
|
+
const started = generation;
|
|
255
|
+
const frame = {
|
|
256
|
+
first: input.page,
|
|
257
|
+
later: createPage(input.settings, 1)
|
|
258
|
+
};
|
|
259
|
+
const reusable = previous && samePageFrame(previous.page, input.page) ? previous : null;
|
|
260
|
+
const placements = [];
|
|
261
|
+
let start = {
|
|
262
|
+
pageIndex: 0,
|
|
263
|
+
top: input.page.content.top
|
|
264
|
+
};
|
|
265
|
+
let unchanged = reusable !== null && reusable.placements.length === input.blocks.length;
|
|
266
|
+
input.blocks.forEach((block, blockIndex) => {
|
|
267
|
+
const old = reusable?.placements[blockIndex];
|
|
268
|
+
const measurement = measure(block, blockIndex, input, old, started);
|
|
269
|
+
if (measurement === old && old.start.pageIndex === start.pageIndex && old.start.top === start.top) {
|
|
270
|
+
placements.push(old);
|
|
271
|
+
start = old.end;
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
const placed = placeBlock(measurement.measured, start, frame);
|
|
275
|
+
if (frozen) placed.fragments.forEach(freezeOwnedFragment);
|
|
276
|
+
placements.push({
|
|
277
|
+
...measurement,
|
|
278
|
+
block,
|
|
279
|
+
end: placed.end,
|
|
280
|
+
fragments: placed.fragments,
|
|
281
|
+
start
|
|
282
|
+
});
|
|
283
|
+
start = placed.end;
|
|
284
|
+
unchanged = false;
|
|
285
|
+
});
|
|
286
|
+
if (unchanged && reusable) return reusable;
|
|
287
|
+
const fragments = placements.flatMap((placement) => placement.fragments);
|
|
288
|
+
const pages = Array.from({ length: start.pageIndex + 1 }, (_, index) => reusable?.output.pages[index] ?? createFlowPage(frame, index, frozen));
|
|
289
|
+
return {
|
|
290
|
+
output: frozen ? {
|
|
291
|
+
fragments: Object.freeze(fragments),
|
|
292
|
+
pages: Object.freeze(pages)
|
|
293
|
+
} : {
|
|
294
|
+
fragments,
|
|
295
|
+
pages
|
|
296
|
+
},
|
|
297
|
+
page: input.page,
|
|
298
|
+
placements
|
|
299
|
+
};
|
|
300
|
+
};
|
|
301
|
+
return Object.freeze({
|
|
302
|
+
compose: (input) => composeFrom(input, null, false).output,
|
|
303
|
+
composeReusing: (input) => {
|
|
304
|
+
const started = generation;
|
|
305
|
+
const composition = composeFrom(input, last, true);
|
|
306
|
+
last = generation === started ? composition : null;
|
|
307
|
+
return composition.output;
|
|
308
|
+
},
|
|
309
|
+
reset: () => {
|
|
310
|
+
last = null;
|
|
311
|
+
generation += 1;
|
|
312
|
+
}
|
|
313
|
+
});
|
|
314
|
+
};
|
|
315
|
+
//#endregion
|
|
316
|
+
//#region src/pagination/page-layout-engine-invalidation.internal.ts
|
|
317
|
+
const REGISTERED_INVALIDATORS = /* @__PURE__ */ new WeakMap();
|
|
318
|
+
const registerPageLayoutEngineInvalidator = (engine, invalidator) => {
|
|
319
|
+
REGISTERED_INVALIDATORS.set(engine, invalidator);
|
|
320
|
+
};
|
|
321
|
+
const invalidatePageLayoutEngines = (engines) => {
|
|
322
|
+
const sharedInvalidators = /* @__PURE__ */ new Set();
|
|
323
|
+
engines.forEach((engine) => {
|
|
324
|
+
const registered = REGISTERED_INVALIDATORS.get(engine);
|
|
325
|
+
if (!registered) {
|
|
326
|
+
engine.invalidate?.();
|
|
327
|
+
return;
|
|
328
|
+
}
|
|
329
|
+
registered.local();
|
|
330
|
+
if (registered.shared) sharedInvalidators.add(registered.shared);
|
|
331
|
+
});
|
|
332
|
+
sharedInvalidators.forEach((invalidate) => invalidate());
|
|
333
|
+
};
|
|
334
|
+
//#endregion
|
|
335
|
+
//#region src/pagination/index.ts
|
|
336
|
+
const MAIN_ROOT_KEY = "main";
|
|
337
|
+
const DEFAULT_TEXT_STYLE = {
|
|
338
|
+
font: "400 16px Arial, sans-serif",
|
|
339
|
+
letterSpacing: 0
|
|
340
|
+
};
|
|
341
|
+
const DEFAULT_BLOCK_STYLE = {
|
|
342
|
+
blockSpacing: 12,
|
|
343
|
+
lineHeight: 24
|
|
344
|
+
};
|
|
345
|
+
const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
346
|
+
const decodePageMargins = (value) => {
|
|
347
|
+
if (typeof value === "number" && Number.isFinite(value)) return value;
|
|
348
|
+
if (!isRecord(value)) throw new Error("Invalid page margins.");
|
|
349
|
+
const { bottom, left, right, top } = value;
|
|
350
|
+
if (![
|
|
351
|
+
bottom,
|
|
352
|
+
left,
|
|
353
|
+
right,
|
|
354
|
+
top
|
|
355
|
+
].every((margin) => typeof margin === "number" && Number.isFinite(margin))) throw new Error("Invalid page margins.");
|
|
356
|
+
return {
|
|
357
|
+
bottom,
|
|
358
|
+
left,
|
|
359
|
+
right,
|
|
360
|
+
top
|
|
361
|
+
};
|
|
362
|
+
};
|
|
363
|
+
const decodePageSettings = (value) => {
|
|
364
|
+
if (!isRecord(value) || value.preset !== "a4" && value.preset !== "letter") throw new Error("Invalid page settings.");
|
|
365
|
+
return {
|
|
366
|
+
margins: decodePageMargins(value.margins),
|
|
367
|
+
preset: value.preset
|
|
368
|
+
};
|
|
369
|
+
};
|
|
370
|
+
const pageSettingsCodec = Object.freeze({
|
|
371
|
+
decode: decodePageSettings,
|
|
372
|
+
encode: (value) => decodePageSettings(value)
|
|
373
|
+
});
|
|
374
|
+
const isFiniteSize = (size) => Number.isFinite(size.height) && Number.isFinite(size.width) && size.height >= 0 && size.width >= 0;
|
|
375
|
+
const samePath = (left, right) => left.length === right.length && left.every((value, index) => value === right[index]);
|
|
376
|
+
const rangeRoot = (root) => root === void 0 ? {} : { root };
|
|
377
|
+
const createRange = (path, start, end, root) => {
|
|
378
|
+
const sourcePath = Object.isFrozen(path) ? path : Object.freeze([...path]);
|
|
379
|
+
return {
|
|
380
|
+
anchor: {
|
|
381
|
+
offset: start,
|
|
382
|
+
path: sourcePath,
|
|
383
|
+
...rangeRoot(root)
|
|
384
|
+
},
|
|
385
|
+
focus: {
|
|
386
|
+
offset: end,
|
|
387
|
+
path: sourcePath,
|
|
388
|
+
...rangeRoot(root)
|
|
389
|
+
}
|
|
390
|
+
};
|
|
391
|
+
};
|
|
392
|
+
const getRangeEdges = (range) => {
|
|
393
|
+
const { anchor, focus } = range;
|
|
394
|
+
return anchor.offset <= focus.offset ? {
|
|
395
|
+
end: focus.offset,
|
|
396
|
+
start: anchor.offset
|
|
397
|
+
} : {
|
|
398
|
+
end: anchor.offset,
|
|
399
|
+
start: focus.offset
|
|
400
|
+
};
|
|
401
|
+
};
|
|
402
|
+
const getBlockStyle = (typography, element, path) => ({
|
|
403
|
+
...DEFAULT_BLOCK_STYLE,
|
|
404
|
+
...typography?.block?.({
|
|
405
|
+
element,
|
|
406
|
+
path
|
|
407
|
+
})
|
|
408
|
+
});
|
|
409
|
+
const getTextStyle = (typography, element, leaf, path) => ({
|
|
410
|
+
...DEFAULT_TEXT_STYLE,
|
|
411
|
+
...typography?.text?.({
|
|
412
|
+
element,
|
|
413
|
+
leaf,
|
|
414
|
+
path
|
|
415
|
+
})
|
|
416
|
+
});
|
|
417
|
+
const isSupportedInlineFlow = (editor, element) => element.children.every((child) => {
|
|
418
|
+
if (NodeApi.isText(child)) return true;
|
|
419
|
+
if (!editor.read.schema.isInline(child)) return false;
|
|
420
|
+
return isSupportedInlineFlow(editor, child);
|
|
421
|
+
});
|
|
422
|
+
const extractTextRuns = (element, path, root, typography) => {
|
|
423
|
+
const runs = [...NodeApi.texts(element)].map(([leaf, leafPath]) => {
|
|
424
|
+
const sourcePath = [...path, ...leafPath];
|
|
425
|
+
return {
|
|
426
|
+
source: createRange(sourcePath, 0, leaf.text.length, root),
|
|
427
|
+
text: leaf.text,
|
|
428
|
+
textStyle: getTextStyle(typography, element, leaf, sourcePath)
|
|
429
|
+
};
|
|
430
|
+
});
|
|
431
|
+
return runs.length > 0 ? runs : [{
|
|
432
|
+
source: createRange([...path, 0], 0, 0, root),
|
|
433
|
+
text: "",
|
|
434
|
+
textStyle: getTextStyle(typography, element, { text: "" }, [...path, 0])
|
|
435
|
+
}];
|
|
436
|
+
};
|
|
437
|
+
const extractBlock = ({ editor, fragmentation, page, root, typography }, element, index) => {
|
|
438
|
+
const path = [index];
|
|
439
|
+
const style = getBlockStyle(typography, element, path);
|
|
440
|
+
const plan = fragmentation?.({
|
|
441
|
+
content: {
|
|
442
|
+
height: page.content.height,
|
|
443
|
+
width: page.content.width
|
|
444
|
+
},
|
|
445
|
+
element,
|
|
446
|
+
path
|
|
447
|
+
});
|
|
448
|
+
const supportedInlineFlow = isSupportedInlineFlow(editor, element);
|
|
449
|
+
const resolved = plan ?? (supportedInlineFlow ? { type: "text" } : void 0);
|
|
450
|
+
if (!resolved) throw new Error(`Pagination requires an explicit fragmentation plan for ${String(element.type ?? "this element")} at [${path.join(",")}].`);
|
|
451
|
+
const spacingAfter = style.blockSpacing ?? 0;
|
|
452
|
+
if (resolved.type === "text") {
|
|
453
|
+
if (!supportedInlineFlow) throw new Error(`Pagination text fragmentation requires one supported inline flow at [${path.join(",")}].`);
|
|
454
|
+
return {
|
|
455
|
+
keepTogether: resolved.keepTogether ?? false,
|
|
456
|
+
lineHeight: style.lineHeight,
|
|
457
|
+
path,
|
|
458
|
+
runs: extractTextRuns(element, path, root, typography),
|
|
459
|
+
spacingAfter,
|
|
460
|
+
type: "text"
|
|
461
|
+
};
|
|
462
|
+
}
|
|
463
|
+
if (resolved.type === "atomic") {
|
|
464
|
+
if (!isFiniteSize(resolved.size)) throw new Error(`Pagination atomic size must be finite and nonnegative at [${path.join(",")}].`);
|
|
465
|
+
return {
|
|
466
|
+
path,
|
|
467
|
+
size: { ...resolved.size },
|
|
468
|
+
spacingAfter,
|
|
469
|
+
type: "atomic"
|
|
470
|
+
};
|
|
471
|
+
}
|
|
472
|
+
if (element.children.length === 0 || element.children.some((child) => !NodeApi.isElement(child)) || resolved.sizes.length !== element.children.length) throw new Error(`Pagination direct-children fragmentation requires one size for every element child at [${path.join(",")}].`);
|
|
473
|
+
if (resolved.sizes.some((size) => !isFiniteSize(size))) throw new Error(`Pagination direct-child sizes must be finite and nonnegative at [${path.join(",")}].`);
|
|
474
|
+
return {
|
|
475
|
+
children: resolved.sizes.map((size, childIndex) => ({
|
|
476
|
+
path: [...path, childIndex],
|
|
477
|
+
size: { ...size }
|
|
478
|
+
})),
|
|
479
|
+
path,
|
|
480
|
+
spacingAfter,
|
|
481
|
+
type: "direct-children"
|
|
482
|
+
};
|
|
483
|
+
};
|
|
484
|
+
const sameSize = (left, right) => left.height === right.height && left.width === right.width;
|
|
485
|
+
const sameTextStyle = (left, right) => {
|
|
486
|
+
const keys = Object.keys(left);
|
|
487
|
+
return keys.length === Object.keys(right).length && keys.every((key) => Object.hasOwn(right, key) && left[key] === right[key]);
|
|
488
|
+
};
|
|
489
|
+
const sameRun = (left, right) => left.text === right.text && PointApi.equals(left.source.anchor, right.source.anchor) && PointApi.equals(left.source.focus, right.source.focus) && sameTextStyle(left.textStyle, right.textStyle);
|
|
490
|
+
const sameBlock = (left, right) => {
|
|
491
|
+
if (left.spacingAfter !== right.spacingAfter) return false;
|
|
492
|
+
if (left.type === "text") return right.type === "text" && left.keepTogether === right.keepTogether && left.lineHeight === right.lineHeight && left.runs.length === right.runs.length && left.runs.every((run, index) => sameRun(run, right.runs[index]));
|
|
493
|
+
if (left.type === "atomic") return right.type === "atomic" && sameSize(left.size, right.size);
|
|
494
|
+
return right.type === "direct-children" && left.children.length === right.children.length && left.children.every((child, index) => sameSize(child.size, right.children[index].size));
|
|
495
|
+
};
|
|
496
|
+
const lastBlocks = /* @__PURE__ */ new WeakMap();
|
|
497
|
+
const extractPageBlocks = (previous, children, context, frozen) => children.map((node, index) => {
|
|
498
|
+
if (!NodeApi.isElement(node)) return void 0;
|
|
499
|
+
const block = extractBlock(context, node, index);
|
|
500
|
+
const earlier = previous?.[index];
|
|
501
|
+
if (earlier && sameBlock(earlier, block)) return earlier;
|
|
502
|
+
return frozen ? freezeBlock(block) : block;
|
|
503
|
+
});
|
|
504
|
+
const flattenRuns = (block) => {
|
|
505
|
+
let offset = 0;
|
|
506
|
+
return block.runs.map((run) => {
|
|
507
|
+
const blockStart = offset;
|
|
508
|
+
const blockEnd = blockStart + run.text.length;
|
|
509
|
+
offset = blockEnd;
|
|
510
|
+
return {
|
|
511
|
+
...run,
|
|
512
|
+
blockEnd,
|
|
513
|
+
blockStart
|
|
514
|
+
};
|
|
515
|
+
});
|
|
516
|
+
};
|
|
517
|
+
const getBlockText = (block) => block.runs.map((run) => run.text).join("");
|
|
518
|
+
const createSourceRange = (runs, start, end) => {
|
|
519
|
+
const first = runs.find((run) => run.blockEnd > start || run.text.length === 0 && run.blockEnd === start) ?? runs.at(-1);
|
|
520
|
+
if (!first) throw new Error("Pagination text block has no source run.");
|
|
521
|
+
const last = [...runs].reverse().find((run) => run.blockStart < end || run.text.length === 0 && run.blockStart === end) ?? first;
|
|
522
|
+
const firstEdges = getRangeEdges(first.source);
|
|
523
|
+
const lastEdges = getRangeEdges(last.source);
|
|
524
|
+
return {
|
|
525
|
+
anchor: {
|
|
526
|
+
...first.source.anchor,
|
|
527
|
+
offset: firstEdges.start + Math.max(0, Math.min(first.text.length, start - first.blockStart))
|
|
528
|
+
},
|
|
529
|
+
focus: {
|
|
530
|
+
...last.source.focus,
|
|
531
|
+
offset: lastEdges.start + Math.max(0, Math.min(last.text.length, end - last.blockStart))
|
|
532
|
+
}
|
|
533
|
+
};
|
|
534
|
+
};
|
|
535
|
+
const createMeasuredRuns = (block, start, end, measure, flatRuns = flattenRuns(block)) => {
|
|
536
|
+
const measured = [];
|
|
537
|
+
let left = 0;
|
|
538
|
+
for (const run of flatRuns) {
|
|
539
|
+
const blockStart = Math.max(start, run.blockStart);
|
|
540
|
+
const blockEnd = Math.min(end, run.blockEnd);
|
|
541
|
+
if (blockEnd < blockStart || blockEnd === blockStart && run.text.length > 0) continue;
|
|
542
|
+
const textStart = blockStart - run.blockStart;
|
|
543
|
+
const textEnd = blockEnd - run.blockStart;
|
|
544
|
+
const width = measure(run.text.slice(textStart, textEnd).replaceAll("\n", ""), run.textStyle);
|
|
545
|
+
const edges = getRangeEdges(run.source);
|
|
546
|
+
measured.push({
|
|
547
|
+
blockEnd,
|
|
548
|
+
blockStart,
|
|
549
|
+
left,
|
|
550
|
+
source: createRange(run.source.anchor.path, edges.start + textStart, edges.start + textEnd, run.source.anchor.root),
|
|
551
|
+
width
|
|
552
|
+
});
|
|
553
|
+
left += width;
|
|
554
|
+
}
|
|
555
|
+
return measured;
|
|
556
|
+
};
|
|
557
|
+
const createMeasuredLine = (block, start, end, width, measure, flatRuns = flattenRuns(block)) => {
|
|
558
|
+
const runs = createMeasuredRuns(block, start, end, measure, flatRuns);
|
|
559
|
+
const firstRun = runs[0];
|
|
560
|
+
const lastRun = runs.at(-1);
|
|
561
|
+
return {
|
|
562
|
+
end,
|
|
563
|
+
height: block.lineHeight,
|
|
564
|
+
runs,
|
|
565
|
+
source: firstRun && lastRun ? {
|
|
566
|
+
anchor: firstRun.source.anchor,
|
|
567
|
+
focus: lastRun.source.focus
|
|
568
|
+
} : createSourceRange(flatRuns, start, end),
|
|
569
|
+
start,
|
|
570
|
+
width
|
|
571
|
+
};
|
|
572
|
+
};
|
|
573
|
+
const estimateTextWidth = (text) => text.length * 8;
|
|
574
|
+
const createEstimatedLines = (block, page) => {
|
|
575
|
+
const text = getBlockText(block);
|
|
576
|
+
const charactersPerLine = Math.max(1, Math.floor(page.content.width / 8));
|
|
577
|
+
const lines = [];
|
|
578
|
+
const flatRuns = flattenRuns(block);
|
|
579
|
+
let hardLineStart = 0;
|
|
580
|
+
const hardLines = text.split("\n");
|
|
581
|
+
hardLines.forEach((hardLine, hardLineIndex) => {
|
|
582
|
+
const hasHardBreak = hardLineIndex < hardLines.length - 1;
|
|
583
|
+
if (hardLine.length === 0) lines.push(createMeasuredLine(block, hardLineStart, hardLineStart + (hasHardBreak ? 1 : 0), 0, estimateTextWidth, flatRuns));
|
|
584
|
+
else for (let start = 0; start < hardLine.length; start += charactersPerLine) {
|
|
585
|
+
const end = Math.min(hardLine.length, start + charactersPerLine);
|
|
586
|
+
const blockStart = hardLineStart + start;
|
|
587
|
+
const blockEnd = hardLineStart + end + (hasHardBreak && end === hardLine.length ? 1 : 0);
|
|
588
|
+
lines.push(createMeasuredLine(block, blockStart, blockEnd, estimateTextWidth(text.slice(blockStart, blockEnd).replaceAll("\n", "")), (value) => estimateTextWidth(value), flatRuns));
|
|
589
|
+
}
|
|
590
|
+
hardLineStart += hardLine.length + 1;
|
|
591
|
+
});
|
|
592
|
+
return lines.length > 0 ? lines : [createMeasuredLine(block, 0, 0, 0, () => 0, flatRuns)];
|
|
593
|
+
};
|
|
594
|
+
const validateRect = (rect) => {
|
|
595
|
+
if (!Number.isFinite(rect.height) || !Number.isFinite(rect.left) || !Number.isFinite(rect.top) || !Number.isFinite(rect.width) || rect.height < 0 || rect.width < 0) throw new Error("Pagination engine returned invalid geometry.");
|
|
596
|
+
};
|
|
597
|
+
const sameRect = (left, right) => left.height === right.height && left.left === right.left && left.top === right.top && left.width === right.width;
|
|
598
|
+
const sameRangeOwner = (left, right) => samePath(left.anchor.path, right.anchor.path) && left.anchor.root === right.anchor.root && samePath(left.focus.path, right.focus.path) && left.focus.root === right.focus.root;
|
|
599
|
+
const validateEngineOutput = (blocks, output, settings) => {
|
|
600
|
+
if (output.pages.length === 0) throw new Error("Pagination engine must return at least one page.");
|
|
601
|
+
output.pages.forEach((page, index) => {
|
|
602
|
+
const expected = createPage(settings, index);
|
|
603
|
+
validateRect(page.content);
|
|
604
|
+
if (page.index !== index || page.height !== expected.height || page.width !== expected.width || !sameRect(page.content, expected.content)) throw new Error("Pagination engine returned an invalid page sequence.");
|
|
605
|
+
});
|
|
606
|
+
const blockByPath = new Map(blocks.map((block, index) => [block.path.join("."), {
|
|
607
|
+
block,
|
|
608
|
+
index
|
|
609
|
+
}]));
|
|
610
|
+
const fragmentsByPath = /* @__PURE__ */ new Map();
|
|
611
|
+
let previousBlockIndex = -1;
|
|
612
|
+
let previousPageIndex = -1;
|
|
613
|
+
output.fragments.forEach((fragment) => {
|
|
614
|
+
const pathKey = fragment.path.join(".");
|
|
615
|
+
const owner = blockByPath.get(pathKey);
|
|
616
|
+
if (!owner || owner.block.type !== fragment.type) throw new Error("Pagination engine returned a foreign fragment.");
|
|
617
|
+
if (fragment.pageIndex < previousPageIndex || owner.index < previousBlockIndex) throw new Error("Pagination engine returned fragments out of source order.");
|
|
618
|
+
previousPageIndex = fragment.pageIndex;
|
|
619
|
+
previousBlockIndex = owner.index;
|
|
620
|
+
const siblings = fragmentsByPath.get(pathKey);
|
|
621
|
+
if (siblings) siblings.push(fragment);
|
|
622
|
+
else fragmentsByPath.set(pathKey, [fragment]);
|
|
623
|
+
if (output.pages[fragment.pageIndex]?.index !== fragment.pageIndex) throw new Error("Pagination engine returned a fragment for no page.");
|
|
624
|
+
validateRect(fragment.rect);
|
|
625
|
+
if (fragment.type === "text") {
|
|
626
|
+
if (fragment.lines.length === 0) throw new Error("Pagination engine returned an empty text fragment.");
|
|
627
|
+
fragment.lines.forEach((line) => {
|
|
628
|
+
validateRect(line.rect);
|
|
629
|
+
if (line.runs.length === 0) throw new Error("Pagination engine returned a text line without source.");
|
|
630
|
+
line.runs.forEach((run) => {
|
|
631
|
+
validateRect(run.rect);
|
|
632
|
+
if (run.rect.top !== line.rect.top || run.rect.height !== line.rect.height) throw new Error("Pagination engine returned inconsistent line geometry.");
|
|
633
|
+
});
|
|
634
|
+
const firstRun = line.runs[0];
|
|
635
|
+
const lastRun = line.runs.at(-1);
|
|
636
|
+
if (!firstRun || !lastRun || !samePath(line.source.anchor.path, firstRun.source.anchor.path) || line.source.anchor.root !== firstRun.source.anchor.root || line.source.anchor.offset !== firstRun.source.anchor.offset || !samePath(line.source.focus.path, lastRun.source.focus.path) || line.source.focus.root !== lastRun.source.focus.root || line.source.focus.offset !== lastRun.source.focus.offset) throw new Error("Pagination engine returned inconsistent line source.");
|
|
637
|
+
});
|
|
638
|
+
if (!sameRect(fragment.rect, createFragmentRect(fragment.lines.map((line) => line.rect)))) throw new Error("Pagination engine returned inconsistent fragment geometry.");
|
|
639
|
+
} else if (fragment.type === "direct-children") {
|
|
640
|
+
fragment.children.forEach((child) => validateRect(child.rect));
|
|
641
|
+
if (!sameRect(fragment.rect, createFragmentRect(fragment.children.map((child) => child.rect)))) throw new Error("Pagination engine returned inconsistent fragment geometry.");
|
|
642
|
+
}
|
|
643
|
+
});
|
|
644
|
+
for (const block of blocks) {
|
|
645
|
+
const fragments = fragmentsByPath.get(block.path.join(".")) ?? [];
|
|
646
|
+
if (block.type === "atomic") {
|
|
647
|
+
if (fragments.length !== 1 || fragments[0]?.type !== "atomic") throw new Error("Pagination engine must emit each atomic block once.");
|
|
648
|
+
if (fragments[0].rect.height !== block.size.height || fragments[0].rect.width !== block.size.width) throw new Error("Pagination engine changed an atomic block size.");
|
|
649
|
+
continue;
|
|
650
|
+
}
|
|
651
|
+
if (block.type === "direct-children") {
|
|
652
|
+
const children = fragments.flatMap((fragment) => fragment.type === "direct-children" ? fragment.children : []);
|
|
653
|
+
if (children.length !== block.children.length || children.some((child, index) => !samePath(child.path, block.children[index].path) || child.rect.height !== block.children[index].size.height || child.rect.width !== block.children[index].size.width)) throw new Error("Pagination engine must conserve direct children in source order.");
|
|
654
|
+
continue;
|
|
655
|
+
}
|
|
656
|
+
if (fragments.length === 0 || fragments.some((fragment) => fragment.type !== "text")) throw new Error("Pagination engine must emit every text block.");
|
|
657
|
+
const expectedRuns = new Map(block.runs.map((run) => [run.source.anchor.path.join("."), run]));
|
|
658
|
+
const actualRuns = /* @__PURE__ */ new Map();
|
|
659
|
+
for (const fragment of fragments) {
|
|
660
|
+
if (fragment.type !== "text") continue;
|
|
661
|
+
for (const line of fragment.lines) for (const candidate of line.runs) {
|
|
662
|
+
const pathKey = candidate.source.anchor.path.join(".");
|
|
663
|
+
const expectedRun = expectedRuns.get(pathKey);
|
|
664
|
+
if (!expectedRun || !sameRangeOwner(candidate.source, expectedRun.source)) throw new Error("Pagination engine returned a foreign text range.");
|
|
665
|
+
const ranges = actualRuns.get(pathKey);
|
|
666
|
+
const edges = getRangeEdges(candidate.source);
|
|
667
|
+
if (ranges) ranges.push(edges);
|
|
668
|
+
else actualRuns.set(pathKey, [edges]);
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
for (const run of block.runs) {
|
|
672
|
+
const expected = getRangeEdges(run.source);
|
|
673
|
+
const actual = actualRuns.get(run.source.anchor.path.join(".")) ?? [];
|
|
674
|
+
if (run.text.length === 0) {
|
|
675
|
+
if (actual.length !== 1 || actual[0].start !== expected.start || actual[0].end !== expected.end) throw new Error("Pagination engine lost an empty text position.");
|
|
676
|
+
continue;
|
|
677
|
+
}
|
|
678
|
+
let cursor = expected.start;
|
|
679
|
+
for (const range of actual) {
|
|
680
|
+
if (range.start !== cursor || range.end < range.start) throw new Error("Pagination engine returned overlapping text ranges.");
|
|
681
|
+
cursor = range.end;
|
|
682
|
+
}
|
|
683
|
+
if (cursor !== expected.end) throw new Error("Pagination engine did not conserve text content.");
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
};
|
|
687
|
+
const freezeRange = (range) => Object.freeze({
|
|
688
|
+
anchor: Object.freeze({
|
|
689
|
+
...range.anchor,
|
|
690
|
+
path: Object.freeze([...range.anchor.path])
|
|
691
|
+
}),
|
|
692
|
+
focus: Object.freeze({
|
|
693
|
+
...range.focus,
|
|
694
|
+
path: Object.freeze([...range.focus.path])
|
|
695
|
+
})
|
|
696
|
+
});
|
|
697
|
+
const freezeRect = (rect) => Object.freeze({ ...rect });
|
|
698
|
+
const freezePage = (page) => Object.freeze({
|
|
699
|
+
...page,
|
|
700
|
+
content: freezeRect(page.content)
|
|
701
|
+
});
|
|
702
|
+
const freezeSettings = (settings) => Object.freeze({
|
|
703
|
+
...settings,
|
|
704
|
+
margins: typeof settings.margins === "number" ? settings.margins : Object.freeze({ ...settings.margins })
|
|
705
|
+
});
|
|
706
|
+
const freezeBlock = (block) => {
|
|
707
|
+
Object.freeze(block.path);
|
|
708
|
+
if (block.type === "text") {
|
|
709
|
+
block.runs.forEach((run) => {
|
|
710
|
+
freezeOwnedRange(run.source);
|
|
711
|
+
Object.freeze(run.textStyle);
|
|
712
|
+
Object.freeze(run);
|
|
713
|
+
});
|
|
714
|
+
Object.freeze(block.runs);
|
|
715
|
+
return Object.freeze(block);
|
|
716
|
+
}
|
|
717
|
+
if (block.type === "atomic") {
|
|
718
|
+
Object.freeze(block.size);
|
|
719
|
+
return Object.freeze(block);
|
|
720
|
+
}
|
|
721
|
+
block.children.forEach((child) => {
|
|
722
|
+
Object.freeze(child.path);
|
|
723
|
+
Object.freeze(child.size);
|
|
724
|
+
Object.freeze(child);
|
|
725
|
+
});
|
|
726
|
+
Object.freeze(block.children);
|
|
727
|
+
return Object.freeze(block);
|
|
728
|
+
};
|
|
729
|
+
const freezeFragment = (fragment) => {
|
|
730
|
+
const common = {
|
|
731
|
+
pageIndex: fragment.pageIndex,
|
|
732
|
+
path: Object.freeze([...fragment.path]),
|
|
733
|
+
rect: freezeRect(fragment.rect)
|
|
734
|
+
};
|
|
735
|
+
if (fragment.type === "atomic") return Object.freeze({
|
|
736
|
+
...common,
|
|
737
|
+
type: "atomic"
|
|
738
|
+
});
|
|
739
|
+
if (fragment.type === "direct-children") return Object.freeze({
|
|
740
|
+
...common,
|
|
741
|
+
children: Object.freeze(fragment.children.map((child) => Object.freeze({
|
|
742
|
+
path: Object.freeze([...child.path]),
|
|
743
|
+
rect: freezeRect(child.rect)
|
|
744
|
+
}))),
|
|
745
|
+
type: "direct-children"
|
|
746
|
+
});
|
|
747
|
+
return Object.freeze({
|
|
748
|
+
...common,
|
|
749
|
+
lines: Object.freeze(fragment.lines.map((line) => Object.freeze({
|
|
750
|
+
rect: freezeRect(line.rect),
|
|
751
|
+
runs: Object.freeze(line.runs.map((run) => Object.freeze({
|
|
752
|
+
rect: freezeRect(run.rect),
|
|
753
|
+
source: freezeRange(run.source)
|
|
754
|
+
}))),
|
|
755
|
+
source: freezeRange(line.source)
|
|
756
|
+
}))),
|
|
757
|
+
type: "text"
|
|
758
|
+
});
|
|
759
|
+
};
|
|
760
|
+
const ownedComposers = /* @__PURE__ */ new WeakMap();
|
|
761
|
+
const createEstimatedPageLayoutEngine = () => {
|
|
762
|
+
const composer = createPageComposer({
|
|
763
|
+
lines: (block, _mode, page) => createEstimatedLines(block, page),
|
|
764
|
+
mode: () => "estimated"
|
|
765
|
+
});
|
|
766
|
+
const engine = Object.freeze({ compose: composer.compose });
|
|
767
|
+
ownedComposers.set(engine, composer.composeReusing);
|
|
768
|
+
return engine;
|
|
769
|
+
};
|
|
770
|
+
const getPreparedTextOffset = (prepared, cursor) => {
|
|
771
|
+
let offset = 0;
|
|
772
|
+
for (let index = 0; index < cursor.segmentIndex; index++) offset += prepared.segments[index]?.length ?? 0;
|
|
773
|
+
return offset + Array.from(prepared.segments[cursor.segmentIndex] ?? "").slice(0, cursor.graphemeIndex).join("").length;
|
|
774
|
+
};
|
|
775
|
+
const stableKey = (value) => JSON.stringify(value);
|
|
776
|
+
const LEADING_COLLAPSIBLE_RE = /^[ \t\n\f\r]+/;
|
|
777
|
+
const TRAILING_COLLAPSIBLE_RE = /[ \t\n\f\r]+$/;
|
|
778
|
+
const getLeadingCollapsibleLength = (text) => text.match(LEADING_COLLAPSIBLE_RE)?.[0].length ?? 0;
|
|
779
|
+
const getTrailingCollapsibleLength = (text) => text.match(TRAILING_COLLAPSIBLE_RE)?.[0].length ?? 0;
|
|
780
|
+
const createPretextPageLayoutEngine = ({ estimateBlock, maxPreparedEntries = 5e3, whiteSpace = "pre-wrap", wordBreak = "normal" } = {}) => {
|
|
781
|
+
const preparedCache = /* @__PURE__ */ new Map();
|
|
782
|
+
const measuredCache = /* @__PURE__ */ new Map();
|
|
783
|
+
const getPrepared = (text, style) => {
|
|
784
|
+
const key = stableKey([
|
|
785
|
+
text,
|
|
786
|
+
style.font,
|
|
787
|
+
style.letterSpacing ?? 0,
|
|
788
|
+
whiteSpace,
|
|
789
|
+
wordBreak
|
|
790
|
+
]);
|
|
791
|
+
const cached = preparedCache.get(key);
|
|
792
|
+
if (cached) return cached;
|
|
793
|
+
const prepared = prepareWithSegments(text, style.font, {
|
|
794
|
+
letterSpacing: style.letterSpacing,
|
|
795
|
+
whiteSpace,
|
|
796
|
+
wordBreak
|
|
797
|
+
});
|
|
798
|
+
preparedCache.set(key, prepared);
|
|
799
|
+
if (preparedCache.size > maxPreparedEntries) {
|
|
800
|
+
const oldest = preparedCache.keys().next().value;
|
|
801
|
+
if (oldest) preparedCache.delete(oldest);
|
|
802
|
+
}
|
|
803
|
+
return prepared;
|
|
804
|
+
};
|
|
805
|
+
const measure = (text, style) => measureNaturalWidth(getPrepared(text, style));
|
|
806
|
+
const createRichInlineLines = (block, maxWidth) => {
|
|
807
|
+
if (whiteSpace !== "normal" || wordBreak !== "normal" || block.runs.length <= 1) return null;
|
|
808
|
+
const flatRuns = flattenRuns(block);
|
|
809
|
+
const sources = flatRuns.map((run) => {
|
|
810
|
+
const sourceStart = getLeadingCollapsibleLength(run.text);
|
|
811
|
+
const sourceEnd = run.text.length - getTrailingCollapsibleLength(run.text);
|
|
812
|
+
const text = sourceEnd <= sourceStart ? "" : run.text.slice(sourceStart, sourceEnd);
|
|
813
|
+
return {
|
|
814
|
+
flat: run,
|
|
815
|
+
prepared: getPrepared(text, run.textStyle),
|
|
816
|
+
sourceEnd,
|
|
817
|
+
sourceStart
|
|
818
|
+
};
|
|
819
|
+
});
|
|
820
|
+
const prepared = prepareRichInline(block.runs.map((run) => ({
|
|
821
|
+
font: run.textStyle.font,
|
|
822
|
+
letterSpacing: run.textStyle.letterSpacing,
|
|
823
|
+
text: run.text
|
|
824
|
+
})));
|
|
825
|
+
const rawLines = [];
|
|
826
|
+
walkRichInlineLineRanges(prepared, maxWidth, (range) => {
|
|
827
|
+
const line = materializeRichInlineLineRange(prepared, range);
|
|
828
|
+
const runs = [];
|
|
829
|
+
let left = 0;
|
|
830
|
+
line.fragments.forEach((fragment) => {
|
|
831
|
+
const source = sources[fragment.itemIndex];
|
|
832
|
+
if (!source) return;
|
|
833
|
+
if (fragment.gapBefore > 0) {
|
|
834
|
+
const previous = sources[fragment.itemIndex - 1];
|
|
835
|
+
const gap = previous && previous.sourceEnd < previous.flat.text.length ? {
|
|
836
|
+
end: previous.flat.text.length,
|
|
837
|
+
source: previous,
|
|
838
|
+
start: previous.sourceEnd
|
|
839
|
+
} : source.sourceStart > 0 ? {
|
|
840
|
+
end: source.sourceStart,
|
|
841
|
+
source,
|
|
842
|
+
start: 0
|
|
843
|
+
} : null;
|
|
844
|
+
if (gap && gap.end > gap.start) {
|
|
845
|
+
const edges = getRangeEdges(gap.source.flat.source);
|
|
846
|
+
runs.push({
|
|
847
|
+
blockEnd: gap.source.flat.blockStart + gap.end,
|
|
848
|
+
blockStart: gap.source.flat.blockStart + gap.start,
|
|
849
|
+
left,
|
|
850
|
+
source: createRange(gap.source.flat.source.anchor.path, edges.start + gap.start, edges.start + gap.end, gap.source.flat.source.anchor.root),
|
|
851
|
+
width: fragment.gapBefore
|
|
852
|
+
});
|
|
853
|
+
}
|
|
854
|
+
left += fragment.gapBefore;
|
|
855
|
+
}
|
|
856
|
+
const textStart = source.sourceStart + getPreparedTextOffset(source.prepared, fragment.start);
|
|
857
|
+
const textEnd = source.sourceStart + getPreparedTextOffset(source.prepared, fragment.end);
|
|
858
|
+
if (textEnd < textStart || textEnd === textStart && source.flat.text.length > 0) {
|
|
859
|
+
left += fragment.occupiedWidth;
|
|
860
|
+
return;
|
|
861
|
+
}
|
|
862
|
+
const edges = getRangeEdges(source.flat.source);
|
|
863
|
+
runs.push({
|
|
864
|
+
blockEnd: source.flat.blockStart + textEnd,
|
|
865
|
+
blockStart: source.flat.blockStart + textStart,
|
|
866
|
+
left,
|
|
867
|
+
source: createRange(source.flat.source.anchor.path, edges.start + textStart, edges.start + textEnd, source.flat.source.anchor.root),
|
|
868
|
+
width: fragment.occupiedWidth
|
|
869
|
+
});
|
|
870
|
+
left += fragment.occupiedWidth;
|
|
871
|
+
});
|
|
872
|
+
rawLines.push({
|
|
873
|
+
runs,
|
|
874
|
+
width: line.width
|
|
875
|
+
});
|
|
876
|
+
});
|
|
877
|
+
const textLength = getBlockText(block).length;
|
|
878
|
+
if (rawLines.length === 0) return [createMeasuredLine(block, 0, textLength, 0, () => 0)];
|
|
879
|
+
let assignedStart = 0;
|
|
880
|
+
return rawLines.map((line, lineIndex) => {
|
|
881
|
+
const nextStart = rawLines[lineIndex + 1]?.runs[0]?.blockStart;
|
|
882
|
+
const rawEnd = line.runs.at(-1)?.blockEnd ?? assignedStart;
|
|
883
|
+
const assignedEnd = lineIndex === rawLines.length - 1 ? textLength : Math.max(assignedStart, rawEnd, nextStart ?? rawEnd);
|
|
884
|
+
const completeRuns = [];
|
|
885
|
+
let cursor = assignedStart;
|
|
886
|
+
let left = 0;
|
|
887
|
+
line.runs.forEach((run) => {
|
|
888
|
+
if (run.blockStart > cursor) createMeasuredRuns(block, cursor, run.blockStart, () => 0).forEach((gap) => completeRuns.push({
|
|
889
|
+
...gap,
|
|
890
|
+
left
|
|
891
|
+
}));
|
|
892
|
+
completeRuns.push({
|
|
893
|
+
...run,
|
|
894
|
+
left
|
|
895
|
+
});
|
|
896
|
+
left += run.width;
|
|
897
|
+
cursor = Math.max(cursor, run.blockEnd);
|
|
898
|
+
});
|
|
899
|
+
createMeasuredRuns(block, cursor, assignedEnd, () => 0).forEach((gap) => completeRuns.push({
|
|
900
|
+
...gap,
|
|
901
|
+
left
|
|
902
|
+
}));
|
|
903
|
+
const measuredLine = {
|
|
904
|
+
end: assignedEnd,
|
|
905
|
+
height: block.lineHeight,
|
|
906
|
+
runs: completeRuns,
|
|
907
|
+
source: createSourceRange(flatRuns, assignedStart, assignedEnd),
|
|
908
|
+
start: assignedStart,
|
|
909
|
+
width: Math.max(line.width, left)
|
|
910
|
+
};
|
|
911
|
+
assignedStart = assignedEnd;
|
|
912
|
+
return measuredLine;
|
|
913
|
+
});
|
|
914
|
+
};
|
|
915
|
+
const measureBlock = (block, page) => {
|
|
916
|
+
const richLines = createRichInlineLines(block, page.content.width);
|
|
917
|
+
if (richLines) return richLines;
|
|
918
|
+
const text = getBlockText(block);
|
|
919
|
+
const cacheKey = stableKey({
|
|
920
|
+
lineHeight: block.lineHeight,
|
|
921
|
+
pageWidth: page.content.width,
|
|
922
|
+
runs: block.runs.map((run) => ({
|
|
923
|
+
text: run.text,
|
|
924
|
+
textStyle: run.textStyle
|
|
925
|
+
})),
|
|
926
|
+
whiteSpace,
|
|
927
|
+
wordBreak
|
|
928
|
+
});
|
|
929
|
+
const cached = measuredCache.get(cacheKey);
|
|
930
|
+
if (cached) return cached.map((line) => createMeasuredLine(block, line.start, line.end, line.width, measure));
|
|
931
|
+
const style = block.runs[0]?.textStyle ?? DEFAULT_TEXT_STYLE;
|
|
932
|
+
const prepared = getPrepared(text, style);
|
|
933
|
+
const result = layoutWithLines(prepared, page.content.width, block.lineHeight);
|
|
934
|
+
const lines = result.lines.length === 0 ? [createMeasuredLine(block, 0, 0, 0, measure)] : result.lines.map((line) => {
|
|
935
|
+
const start = getPreparedTextOffset(prepared, line.start);
|
|
936
|
+
const end = getPreparedTextOffset(prepared, line.end);
|
|
937
|
+
return createMeasuredLine(block, start, end, line.width, measure);
|
|
938
|
+
});
|
|
939
|
+
measuredCache.set(cacheKey, lines);
|
|
940
|
+
if (measuredCache.size > maxPreparedEntries) {
|
|
941
|
+
const oldest = measuredCache.keys().next().value;
|
|
942
|
+
if (oldest) measuredCache.delete(oldest);
|
|
943
|
+
}
|
|
944
|
+
return lines;
|
|
945
|
+
};
|
|
946
|
+
const composer = createPageComposer({
|
|
947
|
+
lines: (block, mode, page) => mode === "estimated" ? createEstimatedLines(block, page) : measureBlock(block, page),
|
|
948
|
+
mode: (block, blockIndex, input) => estimateBlock?.({
|
|
949
|
+
block,
|
|
950
|
+
blockIndex,
|
|
951
|
+
page: input.page,
|
|
952
|
+
settings: input.settings
|
|
953
|
+
}) ? "estimated" : "measured"
|
|
954
|
+
});
|
|
955
|
+
const clearLocal = () => {
|
|
956
|
+
preparedCache.clear();
|
|
957
|
+
measuredCache.clear();
|
|
958
|
+
composer.reset();
|
|
959
|
+
};
|
|
960
|
+
const engine = Object.freeze({
|
|
961
|
+
compose: composer.compose,
|
|
962
|
+
invalidate() {
|
|
963
|
+
clearLocal();
|
|
964
|
+
clearCache();
|
|
965
|
+
}
|
|
966
|
+
});
|
|
967
|
+
ownedComposers.set(engine, composer.composeReusing);
|
|
968
|
+
registerPageLayoutEngineInvalidator(engine, {
|
|
969
|
+
local: clearLocal,
|
|
970
|
+
shared: clearCache
|
|
971
|
+
});
|
|
972
|
+
return engine;
|
|
973
|
+
};
|
|
974
|
+
const composeCustom = (engine, input) => {
|
|
975
|
+
const output = engine.compose(input);
|
|
976
|
+
validateEngineOutput(input.blocks, output, input.settings);
|
|
977
|
+
return {
|
|
978
|
+
fragments: Object.freeze(output.fragments.map(freezeFragment)),
|
|
979
|
+
pages: Object.freeze(output.pages.map(freezePage))
|
|
980
|
+
};
|
|
981
|
+
};
|
|
982
|
+
const measurePages = (editor, options) => {
|
|
983
|
+
if (options.root === "main") throw new Error("Omit root to target the primary document.");
|
|
984
|
+
const source = editor.read((state) => {
|
|
985
|
+
const root = toInternalRoot(options.root ?? state.view.root());
|
|
986
|
+
const settings = "margins" in options.page && "preset" in options.page ? decodePageSettings(options.page) : decodePageSettings(state.getField(options.page));
|
|
987
|
+
return {
|
|
988
|
+
children: root === MAIN_ROOT_KEY ? state.children() : state.root(root),
|
|
989
|
+
publicRoot: root === MAIN_ROOT_KEY ? void 0 : root,
|
|
990
|
+
settings,
|
|
991
|
+
version: state.lastCommit()?.version ?? state.runtime.snapshot().version
|
|
992
|
+
};
|
|
993
|
+
});
|
|
994
|
+
const settings = freezeSettings(source.settings);
|
|
995
|
+
const page = freezePage(createPage(settings));
|
|
996
|
+
const composeOwned = ownedComposers.get(options.engine);
|
|
997
|
+
const blockAt = extractPageBlocks(lastBlocks.get(options.engine), source.children, {
|
|
998
|
+
editor,
|
|
999
|
+
fragmentation: options.fragmentation,
|
|
1000
|
+
page,
|
|
1001
|
+
root: source.publicRoot,
|
|
1002
|
+
typography: options.typography
|
|
1003
|
+
}, !composeOwned);
|
|
1004
|
+
const input = {
|
|
1005
|
+
blocks: Object.freeze(blockAt.flatMap((block) => block ? [block] : [])),
|
|
1006
|
+
page,
|
|
1007
|
+
settings,
|
|
1008
|
+
version: source.version
|
|
1009
|
+
};
|
|
1010
|
+
const { fragments, pages } = composeOwned ? composeOwned(input) : composeCustom(options.engine, input);
|
|
1011
|
+
lastBlocks.set(options.engine, blockAt);
|
|
1012
|
+
return Object.freeze({
|
|
1013
|
+
fragments,
|
|
1014
|
+
pages,
|
|
1015
|
+
...source.publicRoot === void 0 ? {} : { root: source.publicRoot },
|
|
1016
|
+
settings,
|
|
1017
|
+
version: source.version
|
|
1018
|
+
});
|
|
1019
|
+
};
|
|
1020
|
+
//#endregion
|
|
1021
|
+
export { invalidatePageLayoutEngines as a, pageSettingsCodec as i, createPretextPageLayoutEngine as n, measurePages as r, createEstimatedPageLayoutEngine as t };
|