@folijs/view 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +28 -0
- package/README.md +4 -3
- package/dist/clipboard.d.ts +28 -9
- package/dist/clipboard.d.ts.map +1 -1
- package/dist/clipboard.js +53 -25
- package/dist/clipboard.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/layout/flow.d.ts +18 -1
- package/dist/layout/flow.d.ts.map +1 -1
- package/dist/layout/flow.js +20 -11
- package/dist/layout/flow.js.map +1 -1
- package/dist/render.d.ts +0 -20
- package/dist/render.d.ts.map +1 -1
- package/dist/render.js +0 -163
- package/dist/render.js.map +1 -1
- package/dist/view.d.ts +37 -16
- package/dist/view.d.ts.map +1 -1
- package/dist/view.js +348 -273
- package/dist/view.js.map +1 -1
- package/dist/word/blocks.d.ts +72 -0
- package/dist/word/blocks.d.ts.map +1 -0
- package/dist/word/blocks.js +718 -0
- package/dist/word/blocks.js.map +1 -0
- package/dist/word/edit.d.ts +61 -0
- package/dist/word/edit.d.ts.map +1 -0
- package/dist/word/edit.js +296 -0
- package/dist/word/edit.js.map +1 -0
- package/dist/word/pages.d.ts +45 -0
- package/dist/word/pages.d.ts.map +1 -0
- package/dist/word/pages.js +335 -0
- package/dist/word/pages.js.map +1 -0
- package/package.json +2 -2
- package/src/clipboard.ts +75 -25
- package/src/index.ts +6 -0
- package/src/layout/flow.ts +36 -13
- package/src/render.ts +1 -171
- package/src/view.ts +394 -281
- package/src/word/blocks.ts +777 -0
- package/src/word/edit.ts +360 -0
- package/src/word/pages.ts +372 -0
package/dist/view.js
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
|
-
import { AllSelection, arrowAcrossLeaf, autolinkHref, closeHistory, deleteBackward, deleteForward, deleteSelection, insertHardBreak, insertLink, insertText, marksAt, NodeSelection, redo, Selection, Slice, setLink, splitBlock, TextSelection, TransformError, toggleMark, undo, } from "@folijs/core";
|
|
1
|
+
import { AllSelection, arrowAcrossLeaf, autolinkHref, closeHistory, deleteBackward, deleteForward, deleteSelection, fitPaste, fitPastedV2, insertHardBreak, insertLink, insertText, isSafeHref, marksAt, NodeSelection, parseContentV2, redo, Selection, Slice, sectionSettings, setLink, splitBlock, TextSelection, TransformError, toggleMark, typedMarks, undo, withEnvelope, wordContext, } from "@folijs/core";
|
|
2
2
|
import { docFromClipboard, FOLI_MIME, serializeRange } from "./clipboard.js";
|
|
3
3
|
import { contentHeight, DEFAULT_RULES, flowFrom, PX_PER_MM, } from "./layout/flow.js";
|
|
4
|
-
import {
|
|
4
|
+
import { PAGE_BREAK_ATTR } from "./render.js";
|
|
5
|
+
import { spacing, WordRenderer } from "./word/edit.js";
|
|
6
|
+
import { measureLineRatio } from "./word/pages.js";
|
|
7
|
+
/** Word's paragraph spacing for a schema v3 document (`flow`'s `word` spacing). */
|
|
8
|
+
const WORD_RULES = { ...DEFAULT_RULES, spacing: "word" };
|
|
5
9
|
/** Class of the element of a node-selected block (a rule). */
|
|
6
10
|
const SELECTED_CLASS = "foli-selected-node";
|
|
7
11
|
const PLACEHOLDER_ATTR = "data-foli-placeholder";
|
|
@@ -30,18 +34,97 @@ const VIEW_ATTR = "data-foli-view";
|
|
|
30
34
|
let viewCount = 0;
|
|
31
35
|
/**
|
|
32
36
|
* Print rules for the page settings of the view whose host has `VIEW_ATTR` = `id`: one sheet of
|
|
33
|
-
*
|
|
34
|
-
* exactly as tall as a sheet (the flow keeps content inside it; the exact height absorbs its
|
|
37
|
+
* its section's size per page (the last section's where named pages are not supported), without
|
|
38
|
+
* margins (the page's padding holds them). A page is exactly as tall as a sheet (the flow keeps content inside it; the exact height absorbs its
|
|
35
39
|
* half-pixel slack), with no gap after it. Content taller than a page (a line taller than a
|
|
36
40
|
* page) is not cut: it prints over the next sheet.
|
|
37
41
|
* shortcut: `@page` is per document: with several editors of different page sizes on one web page, the last one's sheet size wins (each keeps its own page rules); print one editor per document until that is needed.
|
|
38
42
|
*/
|
|
39
|
-
function printStyles(
|
|
40
|
-
const { width, height } = pageSize;
|
|
43
|
+
function printStyles(settings, sheets, id) {
|
|
44
|
+
const { width, height } = settings.pageSize;
|
|
45
|
+
const last = sheetOf(settings, 0).css;
|
|
41
46
|
const pages = `[${VIEW_ATTR}="${id}"]>.foli-page`;
|
|
47
|
+
// Sections with other page setups: a named page each (engines without named pages print
|
|
48
|
+
// every sheet at the last section's size).
|
|
49
|
+
const named = sheets.every((sheet) => sheet.css === last)
|
|
50
|
+
? ""
|
|
51
|
+
: sheets
|
|
52
|
+
.map(({ settings: { pageSize: size }, index }) => {
|
|
53
|
+
const name = `foli-${id}-${index}`;
|
|
54
|
+
return (`@page ${name}{size:${size.width}mm ${size.height}mm;margin:0}` +
|
|
55
|
+
`${pages}[${SHEET_ATTR}="${index}"]{page:${name};height:${size.height}mm!important}`);
|
|
56
|
+
})
|
|
57
|
+
.join("");
|
|
42
58
|
return (`@media print{@page{size:${width}mm ${height}mm;margin:0}` +
|
|
43
59
|
`${pages}{height:${height}mm!important;margin-block:0!important;` +
|
|
44
|
-
`break-after:page;break-inside:avoid}${pages}:last-child{break-after:auto}}`);
|
|
60
|
+
`break-after:page;break-inside:avoid}${pages}:last-child{break-after:auto}${named}}`);
|
|
61
|
+
}
|
|
62
|
+
/** The attribute naming a page's page setup (its index among the document's) for print. */
|
|
63
|
+
const SHEET_ATTR = "data-foli-sheet";
|
|
64
|
+
function sheetOf(settings, index) {
|
|
65
|
+
const { pageSize, margins } = settings;
|
|
66
|
+
const css = `box-sizing: border-box; width: ${pageSize.width}mm; min-height: ${pageSize.height}mm; ` +
|
|
67
|
+
`padding: ${margins.top}mm ${margins.right}mm ${margins.bottom}mm ${margins.left}mm;`;
|
|
68
|
+
return { settings, css, index };
|
|
69
|
+
}
|
|
70
|
+
/** The style each page element was last given (its `cssText` reads back normalized). */
|
|
71
|
+
const pageSheets = new WeakMap();
|
|
72
|
+
/**
|
|
73
|
+
* The section of each top-level block (the section a paragraph's `section` ends; the
|
|
74
|
+
* envelope's for the blocks after the last such paragraph). A section starts a new page,
|
|
75
|
+
* unless it is continuous with the same page setup as the one before (shortcut: a continuous
|
|
76
|
+
* section with another page setup starts a new page; Word's even and odd page sections do not
|
|
77
|
+
* add a blank page), as `renderDocument` lays sections out.
|
|
78
|
+
*/
|
|
79
|
+
function sectionLayouts(doc, last) {
|
|
80
|
+
const sheets = new Map();
|
|
81
|
+
const bySection = new Map();
|
|
82
|
+
const sheet = (section) => {
|
|
83
|
+
let found = bySection.get(section);
|
|
84
|
+
if (!found) {
|
|
85
|
+
const settings = sectionSettings(section);
|
|
86
|
+
const css = sheetOf(settings, 0).css;
|
|
87
|
+
found = sheets.get(css) ?? sheetOf(settings, sheets.size);
|
|
88
|
+
sheets.set(css, found);
|
|
89
|
+
bySection.set(section, found);
|
|
90
|
+
}
|
|
91
|
+
return found;
|
|
92
|
+
};
|
|
93
|
+
const sections = [];
|
|
94
|
+
let section = last;
|
|
95
|
+
for (let i = doc.childCount - 1; i >= 0; i--) {
|
|
96
|
+
const own = doc.child(i).attrs.section;
|
|
97
|
+
if (own)
|
|
98
|
+
section = own;
|
|
99
|
+
sections[i] = section;
|
|
100
|
+
}
|
|
101
|
+
// Indexes in document order: the first page setup is 0.
|
|
102
|
+
sections.forEach(sheet);
|
|
103
|
+
return sections.map((own, i) => {
|
|
104
|
+
const next = sections[i + 1];
|
|
105
|
+
// By position, as `renderDocument`: two paragraphs may end sections with one object.
|
|
106
|
+
const ends = !!next && !!doc.child(i).attrs.section;
|
|
107
|
+
const breakAfter = ends && !(next.type === "continuous" && sheet(next).css === sheet(own).css);
|
|
108
|
+
return { sheet: sheet(own), breakAfter };
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Initial pages (before a flow), moved where needed so that each page holds blocks of one
|
|
113
|
+
* page setup: a block is measured in a page of its own section's width.
|
|
114
|
+
*/
|
|
115
|
+
function separateSheets(pages, layout) {
|
|
116
|
+
const css = [];
|
|
117
|
+
let floor = 0;
|
|
118
|
+
return pages.map((list, i) => list.map((p) => {
|
|
119
|
+
const want = layout[i].sheet.css;
|
|
120
|
+
let page = Math.max(p, floor);
|
|
121
|
+
// Only the last page used so far has a setup: the next one is free.
|
|
122
|
+
if (css[page] !== undefined && css[page] !== want)
|
|
123
|
+
page++;
|
|
124
|
+
css[page] ??= want;
|
|
125
|
+
floor = page;
|
|
126
|
+
return page;
|
|
127
|
+
}));
|
|
45
128
|
}
|
|
46
129
|
/**
|
|
47
130
|
* The editable view of an `Editor` from `@folijs/core`: renders its state into one
|
|
@@ -52,9 +135,8 @@ function printStyles({ pageSize }, id) {
|
|
|
52
135
|
* view cannot prevent (Android keyboards, extensions). A MutationObserver records those; the view
|
|
53
136
|
* then reads the changed text back into a transaction (`readBack`) and re-renders.
|
|
54
137
|
*
|
|
55
|
-
* Blocks are the
|
|
56
|
-
* containers
|
|
57
|
-
* (ADR-0004).
|
|
138
|
+
* Blocks are the document's paragraphs and opaque blocks (a schema v3 document has no
|
|
139
|
+
* containers: a list is numbered paragraphs).
|
|
58
140
|
*/
|
|
59
141
|
export class EditorView {
|
|
60
142
|
host;
|
|
@@ -63,12 +145,8 @@ export class EditorView {
|
|
|
63
145
|
blocks = [];
|
|
64
146
|
/** Position before each block, parallel to `blocks`. */
|
|
65
147
|
starts = [];
|
|
66
|
-
/** The containers around each block, outermost first, parallel to `blocks`. */
|
|
67
|
-
chains = [];
|
|
68
148
|
/** The index of each block in `blocks`. */
|
|
69
149
|
indexOf = new Map();
|
|
70
|
-
/** The container types around each block at its last render (a change means re-measuring). */
|
|
71
|
-
chainKeys = new WeakMap();
|
|
72
150
|
/** The element that shows a node selection, if any. */
|
|
73
151
|
selectedDom = null;
|
|
74
152
|
/** The page of each fragment of each block. */
|
|
@@ -77,7 +155,16 @@ export class EditorView {
|
|
|
77
155
|
owner = new Map();
|
|
78
156
|
/** The page elements, in order; the host's only children. */
|
|
79
157
|
pages = [];
|
|
80
|
-
|
|
158
|
+
/** The page setup of each page (its section's), parallel to `pages`. */
|
|
159
|
+
sheets = [];
|
|
160
|
+
/** The page setups the print rules were written for. */
|
|
161
|
+
printKey = "";
|
|
162
|
+
/**
|
|
163
|
+
* Each block's page setup and whether a section break follows it, as last rendered: a block
|
|
164
|
+
* whose layout changed (a section break inserted or removed around it) is measured and
|
|
165
|
+
* flowed again.
|
|
166
|
+
*/
|
|
167
|
+
layoutKeys = new WeakMap();
|
|
81
168
|
/** Marks the host for this view's print rules (`printStyles`). */
|
|
82
169
|
id = String(++viewCount);
|
|
83
170
|
/**
|
|
@@ -92,7 +179,7 @@ export class EditorView {
|
|
|
92
179
|
* Measurements by model node: a node's rendered height, margins and lines only depend on
|
|
93
180
|
* its content (unchanged node, same identity), the page width and the CSS. Cleared when the
|
|
94
181
|
* page settings or the fonts change; single entries go stale when a block's size changes
|
|
95
|
-
* without an edit (`ResizeObserver`: CSS, fonts
|
|
182
|
+
* without an edit (`ResizeObserver`: CSS, fonts).
|
|
96
183
|
*/
|
|
97
184
|
measures = new WeakMap();
|
|
98
185
|
stale = new Set();
|
|
@@ -117,6 +204,8 @@ export class EditorView {
|
|
|
117
204
|
observer;
|
|
118
205
|
unsubscribe;
|
|
119
206
|
editable = true;
|
|
207
|
+
/** Escape was the last key: Tab now leaves the editor. */
|
|
208
|
+
tabLeaves = false;
|
|
120
209
|
placeholder = "";
|
|
121
210
|
/** The block that shows the placeholder, if any. */
|
|
122
211
|
placeholderDom = null;
|
|
@@ -124,6 +213,12 @@ export class EditorView {
|
|
|
124
213
|
pointerFocus = false;
|
|
125
214
|
/** Whether the view added the host's tabindex (a read-only host must stay focusable). */
|
|
126
215
|
addedTabIndex;
|
|
216
|
+
/** Renders the document's blocks; made by the first render. */
|
|
217
|
+
word = null;
|
|
218
|
+
/** Fonts' normal line height ratios, measured once per family (Word's single spacing). */
|
|
219
|
+
ratios = new Map();
|
|
220
|
+
/** Web fonts loaded since the v3 blocks were drawn: the next render draws them again. */
|
|
221
|
+
fontsChanged = false;
|
|
127
222
|
constructor(host, editor, options = {}) {
|
|
128
223
|
this.host = host;
|
|
129
224
|
this.editor = editor;
|
|
@@ -300,6 +395,29 @@ export class EditorView {
|
|
|
300
395
|
render() {
|
|
301
396
|
const { doc } = this.state;
|
|
302
397
|
const ownerDocument = this.host.ownerDocument;
|
|
398
|
+
// A new document (envelope) gets its renderer; nothing rendered before is reused then. An
|
|
399
|
+
// editor's state always has one (`createEditor`).
|
|
400
|
+
const context = wordContext(this.state);
|
|
401
|
+
// Fonts loaded: a new renderer (new line heights), but not while a composition runs in a
|
|
402
|
+
// block it would redraw.
|
|
403
|
+
const renewed = context !== (this.word?.context ?? null) || (this.fontsChanged && !this.composing);
|
|
404
|
+
if (renewed)
|
|
405
|
+
this.fontsChanged = false;
|
|
406
|
+
let word = this.word;
|
|
407
|
+
if (renewed) {
|
|
408
|
+
word = this.word = new WordRenderer(context, ownerDocument, this.lineRatio);
|
|
409
|
+
this.invalidateLayout();
|
|
410
|
+
}
|
|
411
|
+
word.prepare(doc);
|
|
412
|
+
const layout = sectionLayouts(doc, context.envelope.section);
|
|
413
|
+
doc.forEach((node, _, i) => {
|
|
414
|
+
const { sheet, breakAfter } = layout[i];
|
|
415
|
+
const key = `${sheet.css}${breakAfter ? "|break" : ""}`;
|
|
416
|
+
const before = this.layoutKeys.get(node);
|
|
417
|
+
if (before !== undefined && before !== key)
|
|
418
|
+
this.stale.add(node);
|
|
419
|
+
this.layoutKeys.set(node, key);
|
|
420
|
+
});
|
|
303
421
|
const previous = new Map(this.blocks.filter((desc) => !this.dirty.has(desc)).map((desc) => [desc.node, desc]));
|
|
304
422
|
const previousPages = this.fragmentPages;
|
|
305
423
|
this.dirty.clear();
|
|
@@ -310,40 +428,13 @@ export class EditorView {
|
|
|
310
428
|
}
|
|
311
429
|
let blocks = [];
|
|
312
430
|
const starts = [];
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
let chain = [];
|
|
319
|
-
let chainKey = "";
|
|
320
|
-
const changed = () => {
|
|
321
|
-
chain = open.slice();
|
|
322
|
-
chainKey = open.map((container) => container.node.type.name).join(">");
|
|
323
|
-
};
|
|
324
|
-
doc.descendants((node, pos, _parent, index) => {
|
|
325
|
-
let popped = false;
|
|
326
|
-
while (open.length) {
|
|
327
|
-
const last = open[open.length - 1];
|
|
328
|
-
if (pos < last.pos + last.node.nodeSize)
|
|
329
|
-
break;
|
|
330
|
-
open.pop();
|
|
331
|
-
popped = true;
|
|
332
|
-
}
|
|
333
|
-
if (!node.isTextblock && !node.isLeaf) {
|
|
334
|
-
open.push({ node, pos, index, firstBlock: blocks.length });
|
|
335
|
-
changed();
|
|
336
|
-
return true;
|
|
337
|
-
}
|
|
338
|
-
if (popped)
|
|
339
|
-
changed();
|
|
340
|
-
// A block moved into or out of a container (wrapped in a list, indented, lifted) keeps its
|
|
341
|
-
// node but not its line width or margins: measure it again, and re-flow from it.
|
|
342
|
-
const before = this.chainKeys.get(node);
|
|
343
|
-
if (before !== undefined && before !== chainKey)
|
|
431
|
+
doc.forEach((node, pos) => {
|
|
432
|
+
let reused = renewed ? undefined : previous.get(node);
|
|
433
|
+
// A block whose list number or comment shading changed (an edit before it) is redrawn.
|
|
434
|
+
if (reused && reused.key !== word.key(blocks.length)) {
|
|
435
|
+
reused = undefined;
|
|
344
436
|
this.stale.add(node);
|
|
345
|
-
|
|
346
|
-
let reused = previous.get(node);
|
|
437
|
+
}
|
|
347
438
|
// A split block about to be measured again (fonts, CSS) is rendered whole first: each
|
|
348
439
|
// fragment's end breaks its line, so its lines would depend on where it was split before.
|
|
349
440
|
const remeasure = dropLines || !this.measures.has(node) || this.stale.has(node);
|
|
@@ -351,26 +442,25 @@ export class EditorView {
|
|
|
351
442
|
reused = undefined;
|
|
352
443
|
if (reused)
|
|
353
444
|
previous.delete(node);
|
|
354
|
-
blocks.push(reused ??
|
|
445
|
+
blocks.push(reused ?? word.render(node, blocks.length));
|
|
355
446
|
starts.push(pos);
|
|
356
|
-
chains.push(chain);
|
|
357
|
-
return false;
|
|
358
447
|
});
|
|
359
448
|
for (const stale of previous.values())
|
|
360
449
|
for (const fragment of stale.fragments)
|
|
361
450
|
fragment.remove();
|
|
362
|
-
this.
|
|
451
|
+
this.applyPrintStyles(layout);
|
|
363
452
|
let last = 0;
|
|
364
453
|
let pages = blocks.map((desc) => {
|
|
365
454
|
const before = previousPages.get(desc);
|
|
366
455
|
return desc.fragments.map((_, f) => (last = Math.max(last, before?.[f] ?? last)));
|
|
367
456
|
});
|
|
368
|
-
|
|
457
|
+
pages = separateSheets(pages, layout);
|
|
458
|
+
this.place(blocks, pages, layout);
|
|
369
459
|
if (!this.composing) {
|
|
370
460
|
// DOM reads only for blocks without a valid measurement (new, changed or stale ones).
|
|
371
461
|
let scale;
|
|
372
462
|
const layoutPx = () => {
|
|
373
|
-
scale ??= layoutScale(this.pages[0], this.
|
|
463
|
+
scale ??= layoutScale(this.pages[0], this.sheets[0].settings);
|
|
374
464
|
return scale;
|
|
375
465
|
};
|
|
376
466
|
if (dropLines)
|
|
@@ -380,7 +470,8 @@ export class EditorView {
|
|
|
380
470
|
delete measure.lines;
|
|
381
471
|
}
|
|
382
472
|
const nodes = blocks.map((desc) => desc.node);
|
|
383
|
-
|
|
473
|
+
// A block's spacing depends on its neighbours' styles: reuse stops a block earlier.
|
|
474
|
+
const reuse = this.flowed ? reuseOf(this.flowed, nodes, this.stale, 1) : undefined;
|
|
384
475
|
const measureOf = (desc) => {
|
|
385
476
|
let found = this.measures.get(desc.node);
|
|
386
477
|
// A stale block is measured again once per render.
|
|
@@ -395,8 +486,8 @@ export class EditorView {
|
|
|
395
486
|
found.lines ??= measureLines(desc, layoutPx());
|
|
396
487
|
return found.lines;
|
|
397
488
|
};
|
|
398
|
-
const boxes = blocks.map((desc) =>
|
|
399
|
-
const result = flowFrom(boxes, contentHeight(this.editor.settings),
|
|
489
|
+
const boxes = blocks.map((desc, i) => wordBox(blocks, i, measureOf(desc), () => lines(desc), layout[i]));
|
|
490
|
+
const result = flowFrom(boxes, contentHeight(this.editor.settings), WORD_RULES, reuse);
|
|
400
491
|
this.flowed = { nodes, result };
|
|
401
492
|
this.stale.clear();
|
|
402
493
|
const flowed = result.fragments;
|
|
@@ -409,14 +500,14 @@ export class EditorView {
|
|
|
409
500
|
return clusterBoundary(doc, contentStart + offset, false) - contentStart;
|
|
410
501
|
});
|
|
411
502
|
const same = splits.length === desc.splits.length && splits.every((at, k) => at === desc.splits[k]);
|
|
412
|
-
return same ? desc :
|
|
503
|
+
return same ? desc : word.render(desc.node, i, splits);
|
|
413
504
|
});
|
|
414
505
|
pages = flowed.map((fragments) => fragments.map((fragment) => fragment.page));
|
|
415
|
-
|
|
506
|
+
applyGaps(blocks, pages, boxes);
|
|
507
|
+
this.place(blocks, pages, layout);
|
|
416
508
|
}
|
|
417
509
|
this.blocks = blocks;
|
|
418
510
|
this.starts = starts;
|
|
419
|
-
this.chains = chains;
|
|
420
511
|
this.indexOf = new Map(blocks.map((desc, i) => [desc, i]));
|
|
421
512
|
this.fragmentPages = new Map(blocks.map((desc, i) => [desc, pages[i] ?? []]));
|
|
422
513
|
this.owner = new Map(blocks.flatMap((desc) => desc.fragments.map((fragment) => [fragment, desc])));
|
|
@@ -430,19 +521,47 @@ export class EditorView {
|
|
|
430
521
|
if (this.composing)
|
|
431
522
|
this.pending.push(...records);
|
|
432
523
|
}
|
|
524
|
+
/** A font's normal line height ratio, measured outside the host (no input to read back). */
|
|
525
|
+
lineRatio = (family) => {
|
|
526
|
+
let ratio = this.ratios.get(family);
|
|
527
|
+
if (ratio === undefined) {
|
|
528
|
+
const doc = this.host.ownerDocument;
|
|
529
|
+
ratio = measureLineRatio(doc.body ?? doc.documentElement)(family);
|
|
530
|
+
this.ratios.set(family, ratio);
|
|
531
|
+
}
|
|
532
|
+
return ratio;
|
|
533
|
+
};
|
|
433
534
|
/**
|
|
434
535
|
* Puts each fragment of each block into its page (pages are non-decreasing in document
|
|
435
|
-
* order),
|
|
436
|
-
*
|
|
437
|
-
*
|
|
438
|
-
* page or a container (stray DOM) is removed.
|
|
536
|
+
* order), creating and removing pages as needed, and gives each page the page setup of its
|
|
537
|
+
* blocks' section (one per page: `separateSheets`, section breaks). Anything else in the host
|
|
538
|
+
* or a page (stray DOM) is removed.
|
|
439
539
|
*/
|
|
440
|
-
place(blocks, pages,
|
|
540
|
+
place(blocks, pages, layout) {
|
|
441
541
|
const { host } = this;
|
|
442
|
-
const items = blocks.flatMap((desc, i) => desc.fragments.map((dom, f) => ({ dom, page: pages[i]?.[f] ?? 0
|
|
542
|
+
const items = blocks.flatMap((desc, i) => desc.fragments.map((dom, f) => ({ dom, page: pages[i]?.[f] ?? 0 })));
|
|
443
543
|
const count = (items[items.length - 1]?.page ?? 0) + 1;
|
|
544
|
+
const sheets = [];
|
|
545
|
+
pages.forEach((list, i) => {
|
|
546
|
+
for (const p of list)
|
|
547
|
+
sheets[p] ??= layout[i].sheet;
|
|
548
|
+
});
|
|
549
|
+
// A page no block starts on (none yet) takes the setup of the page before it.
|
|
550
|
+
for (let p = 0; p < count; p++)
|
|
551
|
+
sheets[p] ??=
|
|
552
|
+
sheets[p - 1] ?? layout[0]?.sheet ?? this.lastSheet();
|
|
553
|
+
this.sheets = sheets.slice(0, count);
|
|
444
554
|
while (this.pages.length < count)
|
|
445
555
|
this.pages.push(this.createPage());
|
|
556
|
+
this.sheets.forEach((sheet, p) => {
|
|
557
|
+
const page = this.pages[p];
|
|
558
|
+
const key = `${sheet.index} ${sheet.css}`;
|
|
559
|
+
if (pageSheets.get(page) === key)
|
|
560
|
+
return;
|
|
561
|
+
pageSheets.set(page, key);
|
|
562
|
+
page.style.cssText = sheet.css;
|
|
563
|
+
page.setAttribute(SHEET_ATTR, String(sheet.index));
|
|
564
|
+
});
|
|
446
565
|
// Pages first, in order; anything else in the host ends up after them.
|
|
447
566
|
let cursor = host.firstChild;
|
|
448
567
|
for (const page of this.pages.slice(0, count)) {
|
|
@@ -451,90 +570,23 @@ export class EditorView {
|
|
|
451
570
|
else
|
|
452
571
|
host.insertBefore(page, cursor);
|
|
453
572
|
}
|
|
454
|
-
const ownerDocument = host.ownerDocument;
|
|
455
|
-
/** Removes what is left after the last placed child (strays, and blocks of later pages). */
|
|
456
|
-
const closeLevel = (level) => {
|
|
457
|
-
let at = level.at;
|
|
458
|
-
while (at) {
|
|
459
|
-
const next = at.nextSibling;
|
|
460
|
-
at.remove();
|
|
461
|
-
at = next;
|
|
462
|
-
}
|
|
463
|
-
};
|
|
464
|
-
// Block fragments not placed yet, and their blocks. A container element whose first block is
|
|
465
|
-
// still to be placed, in another container at that level, belongs to that later container: a
|
|
466
|
-
// block before it gets a new element instead of taking it (which would move every later block
|
|
467
|
-
// into the next element along, e.g. when one list item moves to the next page).
|
|
468
|
-
const pending = new Map(items.map((item) => [item.dom, item.block]));
|
|
469
|
-
/** Container elements placed in this pass (never taken twice). */
|
|
470
|
-
const claimed = new Set();
|
|
471
573
|
let i = 0;
|
|
472
574
|
for (let p = 0; p < count; p++) {
|
|
473
575
|
const page = this.pages[p];
|
|
474
|
-
|
|
475
|
-
{ el: page, at: page.firstChild, pos: -1 },
|
|
476
|
-
];
|
|
576
|
+
let at = page.firstChild;
|
|
477
577
|
for (; i < items.length && items[i]?.page === p; i++) {
|
|
478
|
-
const
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
const container = chain[k];
|
|
490
|
-
const parent = stack[stack.length - 1];
|
|
491
|
-
let element;
|
|
492
|
-
const candidate = parent.at;
|
|
493
|
-
const later = isWrapperFor(candidate, container.node)
|
|
494
|
-
? pending.get(firstBlockIn(candidate))
|
|
495
|
-
: undefined;
|
|
496
|
-
// The element that holds this block now, at the innermost level: a list item whose
|
|
497
|
-
// block moves to another page takes its element along (as a paragraph does), instead
|
|
498
|
-
// of a new one replacing it on every page an edit shifts.
|
|
499
|
-
const own = k === chain.length - 1 ? item.dom.parentNode : null;
|
|
500
|
-
if (isWrapperFor(candidate, container.node) &&
|
|
501
|
-
!claimed.has(candidate) &&
|
|
502
|
-
(later === undefined || chains[later]?.[k]?.pos === container.pos)) {
|
|
503
|
-
element = candidate;
|
|
504
|
-
parent.at = element.nextSibling;
|
|
505
|
-
}
|
|
506
|
-
else if (isWrapperFor(own, container.node) &&
|
|
507
|
-
!claimed.has(own) &&
|
|
508
|
-
!own.contains(parent.el)) {
|
|
509
|
-
element = own;
|
|
510
|
-
for (const level of stack)
|
|
511
|
-
if (level.at === element)
|
|
512
|
-
level.at = element.nextSibling;
|
|
513
|
-
parent.el.insertBefore(element, parent.at);
|
|
514
|
-
}
|
|
515
|
-
else {
|
|
516
|
-
element = createWrapper(container.node, ownerDocument);
|
|
517
|
-
parent.el.insertBefore(element, parent.at);
|
|
518
|
-
}
|
|
519
|
-
claimed.add(element);
|
|
520
|
-
styleContainer(element, container, chain[k - 1], item);
|
|
521
|
-
stack.push({ el: element, at: element.firstChild, pos: container.pos });
|
|
522
|
-
}
|
|
523
|
-
const top = stack[stack.length - 1];
|
|
524
|
-
if (item.dom === top.at)
|
|
525
|
-
top.at = top.at.nextSibling;
|
|
526
|
-
else {
|
|
527
|
-
// A cursor of an outer level may point at this block (it moves into a new container):
|
|
528
|
-
// step it past, or it would point into the container.
|
|
529
|
-
for (const level of stack)
|
|
530
|
-
if (level.at === item.dom)
|
|
531
|
-
level.at = item.dom.nextSibling;
|
|
532
|
-
top.el.insertBefore(item.dom, top.at);
|
|
533
|
-
}
|
|
534
|
-
pending.delete(item.dom);
|
|
578
|
+
const { dom } = items[i];
|
|
579
|
+
if (dom === at)
|
|
580
|
+
at = at.nextSibling;
|
|
581
|
+
else
|
|
582
|
+
page.insertBefore(dom, at);
|
|
583
|
+
}
|
|
584
|
+
// What is left after the last placed block: strays, and blocks of later pages.
|
|
585
|
+
while (at) {
|
|
586
|
+
const next = at.nextSibling;
|
|
587
|
+
at.remove();
|
|
588
|
+
at = next;
|
|
535
589
|
}
|
|
536
|
-
while (stack.length)
|
|
537
|
-
closeLevel(stack.pop());
|
|
538
590
|
}
|
|
539
591
|
// Pages no longer needed and stray DOM in the host: all after the last page by now.
|
|
540
592
|
let rest = this.pages[count - 1].nextSibling;
|
|
@@ -548,19 +600,19 @@ export class EditorView {
|
|
|
548
600
|
createPage() {
|
|
549
601
|
const page = this.host.ownerDocument.createElement("section");
|
|
550
602
|
page.className = "foli-page";
|
|
551
|
-
page.style.cssText = this.pageStyle;
|
|
552
603
|
return page;
|
|
553
604
|
}
|
|
554
|
-
/**
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
605
|
+
/** The last section's page setup (the envelope's), for an empty document. */
|
|
606
|
+
lastSheet() {
|
|
607
|
+
return sheetOf(this.editor.settings, 0);
|
|
608
|
+
}
|
|
609
|
+
/** The print rules for the document's page setups, written when they change. */
|
|
610
|
+
applyPrintStyles(layout) {
|
|
611
|
+
const sheets = [...new Set(layout.map((block) => block.sheet))];
|
|
612
|
+
const key = [this.lastSheet(), ...sheets].map((sheet) => sheet.css).join("\n");
|
|
613
|
+
if (key === this.printKey)
|
|
560
614
|
return;
|
|
561
|
-
this.
|
|
562
|
-
for (const page of this.pages)
|
|
563
|
-
page.style.cssText = style;
|
|
615
|
+
this.printKey = key;
|
|
564
616
|
// shortcut: written when the page settings change; a host moved to another document or shadow root keeps them where they were until then.
|
|
565
617
|
for (const style of this.printStyles)
|
|
566
618
|
style.remove();
|
|
@@ -572,11 +624,10 @@ export class EditorView {
|
|
|
572
624
|
this.printStyles = parents.map((parent) => {
|
|
573
625
|
const style = doc.createElement("style");
|
|
574
626
|
style.setAttribute("data-foli-print", "");
|
|
575
|
-
style.textContent = printStyles(this.editor.settings, this.id);
|
|
627
|
+
style.textContent = printStyles(this.editor.settings, sheets, this.id);
|
|
576
628
|
parent.append(style);
|
|
577
629
|
return style;
|
|
578
630
|
});
|
|
579
|
-
this.invalidateLayout();
|
|
580
631
|
}
|
|
581
632
|
/** Forgets every measurement: the next flow measures the whole document again. */
|
|
582
633
|
invalidateLayout() {
|
|
@@ -585,6 +636,9 @@ export class EditorView {
|
|
|
585
636
|
}
|
|
586
637
|
/** Re-flows the pages once web fonts have loaded: line heights and wrapping change. */
|
|
587
638
|
onFontsLoaded = () => {
|
|
639
|
+
// v3 line heights come from the fonts' measured ratios: render every block again.
|
|
640
|
+
this.ratios.clear();
|
|
641
|
+
this.fontsChanged = true;
|
|
588
642
|
this.invalidateLayout();
|
|
589
643
|
this.scheduleRelayout();
|
|
590
644
|
};
|
|
@@ -619,7 +673,7 @@ export class EditorView {
|
|
|
619
673
|
// A hidden host (display: none) reports every block as 0x0: nothing to re-flow.
|
|
620
674
|
if (!entry.contentRect.width)
|
|
621
675
|
continue;
|
|
622
|
-
scale ??= layoutScale(this.pages[0], this.
|
|
676
|
+
scale ??= layoutScale(this.pages[0], this.sheets[0].settings);
|
|
623
677
|
let height = 0;
|
|
624
678
|
for (const fragment of desc.fragments)
|
|
625
679
|
height += fragment.getBoundingClientRect().height;
|
|
@@ -666,9 +720,9 @@ export class EditorView {
|
|
|
666
720
|
}
|
|
667
721
|
return null;
|
|
668
722
|
}
|
|
669
|
-
/** Whether `node` holds blocks rather than content: the host
|
|
723
|
+
/** Whether `node` holds blocks rather than content: the host or a page. */
|
|
670
724
|
holdsBlocks(node) {
|
|
671
|
-
return node === this.host || this.pages.includes(node)
|
|
725
|
+
return node === this.host || this.pages.includes(node);
|
|
672
726
|
}
|
|
673
727
|
/** Index of the first block that starts at or after `pos` (the block count if none). */
|
|
674
728
|
blockFrom(pos) {
|
|
@@ -761,7 +815,10 @@ export class EditorView {
|
|
|
761
815
|
const point = before(last.dom);
|
|
762
816
|
return { node: point.node, offset: point.offset + 1 };
|
|
763
817
|
}
|
|
764
|
-
|
|
818
|
+
// An empty block: before its <br> (after a list number shown in front of it).
|
|
819
|
+
const tail = desc.dom.lastChild;
|
|
820
|
+
const end = tail?.nodeName === "BR" ? desc.dom.childNodes.length - 1 : 0;
|
|
821
|
+
return { node: desc.dom, offset: end };
|
|
765
822
|
}
|
|
766
823
|
// ---------- selection ----------
|
|
767
824
|
/** The live DOM selection as a model selection, or null if it is not inside the host. */
|
|
@@ -1034,19 +1091,27 @@ export class EditorView {
|
|
|
1034
1091
|
const $pos = this.state.doc.resolve(pos);
|
|
1035
1092
|
const node = $pos.parent.maybeChild($pos.index());
|
|
1036
1093
|
const href = node && type.isInSet(node.marks)?.attrs.href;
|
|
1037
|
-
return
|
|
1094
|
+
return isSafeHref(href) ? href : null;
|
|
1038
1095
|
}
|
|
1039
1096
|
onKeyDown = (event) => {
|
|
1040
1097
|
if (event.isComposing || this.composing)
|
|
1041
1098
|
return;
|
|
1042
1099
|
this.syncSelection();
|
|
1043
|
-
if (this.crossFragmentEdge(event)) {
|
|
1100
|
+
if (this.crossFragmentEdge(event) || this.crossListNumber(event)) {
|
|
1044
1101
|
event.preventDefault();
|
|
1045
1102
|
return;
|
|
1046
1103
|
}
|
|
1047
1104
|
const name = keyName(event);
|
|
1048
1105
|
// Ctrl/Cmd+Shift+V: the paste event that follows uses only the plain text.
|
|
1049
1106
|
this.plainPaste = name === "Mod-Shift-v";
|
|
1107
|
+
// Tab and Shift+Tab move the focus on in read-only mode, and right after Escape where Tab
|
|
1108
|
+
// would type a tab (no keyboard trap).
|
|
1109
|
+
const tab = name === "Tab" || name === "Shift-Tab";
|
|
1110
|
+
const leave = tab && (this.tabLeaves || !this.editable);
|
|
1111
|
+
if (event.key !== "Shift")
|
|
1112
|
+
this.tabLeaves = name === "Escape";
|
|
1113
|
+
if (leave)
|
|
1114
|
+
return;
|
|
1050
1115
|
// Like `editor.handleKey`, but through the view's dispatch, so read-only mode drops edits.
|
|
1051
1116
|
// A key no command takes keeps its default (Tab outside a list moves the focus on).
|
|
1052
1117
|
if (this.runKey(name)) {
|
|
@@ -1147,6 +1212,32 @@ export class EditorView {
|
|
|
1147
1212
|
this.syncSelection();
|
|
1148
1213
|
return true;
|
|
1149
1214
|
}
|
|
1215
|
+
/**
|
|
1216
|
+
* A v3 list number is not text, but browsers stop the caret before it as well as after: Right
|
|
1217
|
+
* at the end of the paragraph before, or Left at a numbered paragraph's start, would seem to do
|
|
1218
|
+
* nothing. Moves over the paragraph boundary in one step (word moves are left to the browser).
|
|
1219
|
+
*/
|
|
1220
|
+
crossListNumber(event) {
|
|
1221
|
+
const forward = event.key === "ArrowRight";
|
|
1222
|
+
if (!this.word || (!forward && event.key !== "ArrowLeft"))
|
|
1223
|
+
return false;
|
|
1224
|
+
if (event.metaKey || event.ctrlKey || event.altKey)
|
|
1225
|
+
return false;
|
|
1226
|
+
const { selection, doc } = this.state;
|
|
1227
|
+
if (!(selection instanceof TextSelection) || (!event.shiftKey && !selection.empty))
|
|
1228
|
+
return false;
|
|
1229
|
+
const { $head } = selection;
|
|
1230
|
+
if ($head.depth !== 1 || $head.parentOffset !== (forward ? $head.parent.content.size : 0))
|
|
1231
|
+
return false;
|
|
1232
|
+
const numbered = $head.index(0) + (forward ? 1 : 0);
|
|
1233
|
+
const other = doc.maybeChild($head.index(0) + (forward ? 1 : -1));
|
|
1234
|
+
if (!other?.isTextblock || !this.word.hasMarker(numbered))
|
|
1235
|
+
return false;
|
|
1236
|
+
const head = forward ? $head.after() + 1 : $head.before() - 1;
|
|
1237
|
+
const anchor = event.shiftKey ? selection.anchor : head;
|
|
1238
|
+
this.dispatch(this.state.tr.setSelection(TextSelection.create(doc, anchor, head)));
|
|
1239
|
+
return true;
|
|
1240
|
+
}
|
|
1150
1241
|
/** Safety net: a DOM change that was not prevented is read back into the model. */
|
|
1151
1242
|
onInput = () => {
|
|
1152
1243
|
if (!this.composing)
|
|
@@ -1234,6 +1325,9 @@ export class EditorView {
|
|
|
1234
1325
|
}
|
|
1235
1326
|
else if (hardBreaks.has(node))
|
|
1236
1327
|
domText += HARD_BREAK;
|
|
1328
|
+
// Shown but not document text (a list number, opaque content): never read back.
|
|
1329
|
+
else if (node.getAttribute?.("contenteditable") === "false")
|
|
1330
|
+
return;
|
|
1237
1331
|
else
|
|
1238
1332
|
node.childNodes.forEach(walk);
|
|
1239
1333
|
};
|
|
@@ -1269,7 +1363,14 @@ export class EditorView {
|
|
|
1269
1363
|
if (i > 0)
|
|
1270
1364
|
modelText += BLOCK_SEPARATOR;
|
|
1271
1365
|
lineStarts.push(modelText.length);
|
|
1272
|
-
|
|
1366
|
+
block.forEach((child) => {
|
|
1367
|
+
// A tab is text in the DOM (a "\t" text node).
|
|
1368
|
+
modelText += child.isText
|
|
1369
|
+
? (child.text ?? "")
|
|
1370
|
+
: child.type.name === "tab"
|
|
1371
|
+
? "\t"
|
|
1372
|
+
: HARD_BREAK;
|
|
1373
|
+
});
|
|
1273
1374
|
});
|
|
1274
1375
|
/** Model position of an offset in `modelText`. */
|
|
1275
1376
|
const posAt = (offset) => {
|
|
@@ -1344,7 +1445,8 @@ export class EditorView {
|
|
|
1344
1445
|
if (selection.empty || !event.clipboardData)
|
|
1345
1446
|
return;
|
|
1346
1447
|
event.preventDefault();
|
|
1347
|
-
const
|
|
1448
|
+
const context = wordContext(this.state);
|
|
1449
|
+
const { text, html, json } = serializeRange(doc, selection.from, selection.to, this.host.ownerDocument, { context, ratio: this.lineRatio });
|
|
1348
1450
|
event.clipboardData.setData("text/plain", text);
|
|
1349
1451
|
event.clipboardData.setData("text/html", html);
|
|
1350
1452
|
event.clipboardData.setData(FOLI_MIME, json);
|
|
@@ -1395,15 +1497,19 @@ export class EditorView {
|
|
|
1395
1497
|
if (command(state, dispatch))
|
|
1396
1498
|
return applied;
|
|
1397
1499
|
}
|
|
1398
|
-
const marks = (state.storedMarks ?? marksAt(state.selection.$from)).map((m) => m.toJSON());
|
|
1399
|
-
|
|
1400
|
-
//
|
|
1500
|
+
const marks = typedMarks(state.storedMarks ?? marksAt(state.selection.$from)).map((m) => m.toJSON());
|
|
1501
|
+
// The document takes the styles, lists and comments the content needs (HTML is read as
|
|
1502
|
+
// schema v2 content, then migrated).
|
|
1503
|
+
const pasted = docFromClipboard(data, marks, (found) => found.html
|
|
1504
|
+
? fitPastedV2(state, parseContentV2(found.content))
|
|
1505
|
+
: fitPaste(state, found.content, found.word));
|
|
1506
|
+
// One empty paragraph: nothing to paste (a lone opaque block is something).
|
|
1401
1507
|
const only = pasted?.doc.childCount === 1 ? pasted.doc.firstChild : null;
|
|
1402
1508
|
if (!pasted || (only?.isTextblock && !only.content.size))
|
|
1403
1509
|
return false;
|
|
1404
|
-
const { doc, open } = pasted;
|
|
1405
|
-
// As a slice (text joins the text around the caret); where
|
|
1406
|
-
//
|
|
1510
|
+
const { doc, open, envelope } = pasted;
|
|
1511
|
+
// As a slice (text joins the text around the caret); where that cannot be (an opaque block
|
|
1512
|
+
// at an end), as whole blocks at the caret.
|
|
1407
1513
|
const asSlice = (tr) => {
|
|
1408
1514
|
const $pos = tr.selection.$from;
|
|
1409
1515
|
if (!open && $pos.depth === 1 && $pos.parent.isTextblock && !$pos.parent.content.size) {
|
|
@@ -1412,9 +1518,9 @@ export class EditorView {
|
|
|
1412
1518
|
tr.setSelection(Selection.near(tr.doc.resolve(from + doc.content.size), -1));
|
|
1413
1519
|
}
|
|
1414
1520
|
else {
|
|
1415
|
-
// A slice opens into its first and last blocks:
|
|
1521
|
+
// A slice opens into its first and last blocks: an opaque block there has no inside.
|
|
1416
1522
|
if (doc.firstChild?.isLeaf || doc.lastChild?.isLeaf) {
|
|
1417
|
-
throw new TransformError("
|
|
1523
|
+
throw new TransformError("An opaque block at an end is pasted as a block");
|
|
1418
1524
|
}
|
|
1419
1525
|
tr.replaceSelection(doc.slice(1, doc.content.size - 1));
|
|
1420
1526
|
}
|
|
@@ -1430,6 +1536,8 @@ export class EditorView {
|
|
|
1430
1536
|
try {
|
|
1431
1537
|
tr.deleteSelection();
|
|
1432
1538
|
strategy(tr);
|
|
1539
|
+
if (envelope && envelope !== wordContext(state)?.envelope)
|
|
1540
|
+
withEnvelope(tr, envelope);
|
|
1433
1541
|
}
|
|
1434
1542
|
catch (error) {
|
|
1435
1543
|
if (error instanceof TransformError)
|
|
@@ -1446,11 +1554,8 @@ export class EditorView {
|
|
|
1446
1554
|
};
|
|
1447
1555
|
}
|
|
1448
1556
|
/**
|
|
1449
|
-
* Pastes whole blocks at the caret (after deleting the selection)
|
|
1450
|
-
*
|
|
1451
|
-
* empty item, go before or after the item, or between its two halves); other blocks go before
|
|
1452
|
-
* or after the caret's textblock (replacing it when empty) where its parent takes them, else
|
|
1453
|
-
* between its two halves. Throws TransformError where the schema does not allow it.
|
|
1557
|
+
* Pastes whole blocks at the caret (after deleting the selection): before or after the caret's
|
|
1558
|
+
* paragraph, else between its two halves (an empty one is replaced as a slice first).
|
|
1454
1559
|
*/
|
|
1455
1560
|
function insertBlocks(tr, doc) {
|
|
1456
1561
|
const $pos = tr.selection.$from;
|
|
@@ -1458,89 +1563,18 @@ function insertBlocks(tr, doc) {
|
|
|
1458
1563
|
throw new TransformError("No textblock at the caret");
|
|
1459
1564
|
const atStart = $pos.parentOffset === 0;
|
|
1460
1565
|
const atEnd = $pos.parentOffset === $pos.parent.content.size;
|
|
1461
|
-
const only = doc.childCount === 1 ? doc.firstChild : null;
|
|
1462
|
-
const itemType = tr.doc.type.schema.nodes.listItem;
|
|
1463
|
-
const inItem = $pos.depth >= 2 && $pos.node(-1).type === itemType;
|
|
1464
1566
|
let at;
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
const item = $pos.node(depth);
|
|
1470
|
-
if (atStart && atEnd && item.childCount === 1) {
|
|
1471
|
-
at = $pos.before(depth);
|
|
1472
|
-
tr.delete(at, $pos.after(depth));
|
|
1473
|
-
}
|
|
1474
|
-
else if (atEnd) {
|
|
1475
|
-
// After the item (and its nested list, if it has one): no item left without its text.
|
|
1476
|
-
at = $pos.after(depth);
|
|
1477
|
-
}
|
|
1478
|
-
else if (atStart && $pos.index(depth) === 0)
|
|
1479
|
-
at = $pos.before(depth);
|
|
1480
|
-
else if (atStart) {
|
|
1481
|
-
// At a later block's start: split the item between blocks, not inside the text.
|
|
1482
|
-
const boundary = $pos.before();
|
|
1483
|
-
tr.split(boundary, 1);
|
|
1484
|
-
at = boundary + 1;
|
|
1485
|
-
}
|
|
1486
|
-
else {
|
|
1487
|
-
tr.split($pos.pos, 2);
|
|
1488
|
-
at = $pos.pos + 2;
|
|
1489
|
-
}
|
|
1490
|
-
}
|
|
1567
|
+
if (atStart)
|
|
1568
|
+
at = $pos.before();
|
|
1569
|
+
else if (atEnd)
|
|
1570
|
+
at = $pos.after();
|
|
1491
1571
|
else {
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
const parent = $pos.node(-1);
|
|
1495
|
-
const index = $pos.index(-1);
|
|
1496
|
-
if (atStart && atEnd && parent.canReplace(index, index + 1, content)) {
|
|
1497
|
-
at = $pos.before();
|
|
1498
|
-
tr.delete(at, $pos.after());
|
|
1499
|
-
}
|
|
1500
|
-
else if (atStart && parent.canReplace(index, index, content))
|
|
1501
|
-
at = $pos.before();
|
|
1502
|
-
else if (atEnd && parent.canReplace(index + 1, index + 1, content))
|
|
1503
|
-
at = $pos.after();
|
|
1504
|
-
else {
|
|
1505
|
-
tr.split($pos.pos);
|
|
1506
|
-
at = $pos.pos + 1;
|
|
1507
|
-
}
|
|
1572
|
+
tr.split($pos.pos);
|
|
1573
|
+
at = $pos.pos + 1;
|
|
1508
1574
|
}
|
|
1509
|
-
tr.insert(at, content);
|
|
1510
|
-
tr.setSelection(Selection.near(tr.doc.resolve(at + content.size), -1));
|
|
1511
|
-
}
|
|
1512
|
-
/** The first block element inside a container element (through nested containers), or null. */
|
|
1513
|
-
function firstBlockIn(element) {
|
|
1514
|
-
let child = element.firstElementChild;
|
|
1515
|
-
while (isWrapper(child))
|
|
1516
|
-
child = child.firstElementChild;
|
|
1517
|
-
return child;
|
|
1575
|
+
tr.insert(at, doc.content);
|
|
1576
|
+
tr.setSelection(Selection.near(tr.doc.resolve(at + doc.content.size), -1));
|
|
1518
1577
|
}
|
|
1519
|
-
/**
|
|
1520
|
-
* Sets what a container element shows on a page: an ordered list item's number (so numbering
|
|
1521
|
-
* continues across pages), and no marker on an item continued from the previous page (its
|
|
1522
|
-
* first block is on an earlier page, or this is a continued fragment of it).
|
|
1523
|
-
*/
|
|
1524
|
-
function styleContainer(element, container, parent, item) {
|
|
1525
|
-
if (element.localName !== "li")
|
|
1526
|
-
return;
|
|
1527
|
-
const continued = item.block !== container.firstBlock || item.fragment > 0;
|
|
1528
|
-
const start = parent?.node.type.name === "orderedList" ? parent.node.attrs.start : undefined;
|
|
1529
|
-
const value = typeof start === "number" ? String(start + container.index) : null;
|
|
1530
|
-
// Written only when they change (checked against what was written last, without reading the
|
|
1531
|
-
// DOM): this runs for every list item on every render, and every write invalidates style.
|
|
1532
|
-
const state = `${continued}|${value}`;
|
|
1533
|
-
if (containerStates.get(element) === state)
|
|
1534
|
-
return;
|
|
1535
|
-
containerStates.set(element, state);
|
|
1536
|
-
element.style.listStyleType = continued ? "none" : "";
|
|
1537
|
-
if (value === null)
|
|
1538
|
-
element.removeAttribute("value");
|
|
1539
|
-
else
|
|
1540
|
-
element.setAttribute("value", value);
|
|
1541
|
-
}
|
|
1542
|
-
/** What `styleContainer` last wrote on each list item element. */
|
|
1543
|
-
const containerStates = new WeakMap();
|
|
1544
1578
|
/**
|
|
1545
1579
|
* Client rects are scaled by CSS `zoom` or a `transform` on an ancestor, while margins and the
|
|
1546
1580
|
* page geometry are in layout px: the ratio between the two for a page. From the page's width
|
|
@@ -1570,37 +1604,75 @@ function measureBox(desc, scale) {
|
|
|
1570
1604
|
};
|
|
1571
1605
|
}
|
|
1572
1606
|
/**
|
|
1573
|
-
* What the flow needs of a block: its measurement, its lines (on demand),
|
|
1574
|
-
*
|
|
1607
|
+
* What the flow needs of a block: its measurement, its lines (on demand), Word's spacing before
|
|
1608
|
+
* and after (next to its neighbours), keep with next, keep lines together (not for a block
|
|
1609
|
+
* taller than a page, or one with a page break in it), page breaks ending their line's page,
|
|
1610
|
+
* and a page break before the next block ending this one's page.
|
|
1575
1611
|
*/
|
|
1576
|
-
function
|
|
1612
|
+
function wordBox(blocks, i, measure, lines, layout) {
|
|
1613
|
+
const pageHeight = contentHeight(layout.sheet.settings);
|
|
1614
|
+
const desc = blocks[i];
|
|
1615
|
+
const next = blocks[i + 1];
|
|
1616
|
+
const { before, after } = spacing(blocks[i - 1]?.word, desc.word, next?.word);
|
|
1617
|
+
const pageBreaks = desc.leaves.filter((leaf) => leaf.dom.nodeType !== Node.TEXT_NODE && leaf.dom.hasAttribute(PAGE_BREAK_ATTR));
|
|
1618
|
+
const hasBreak = pageBreaks.length > 0;
|
|
1619
|
+
const kept = desc.word.keepLines && !hasBreak && measure.height <= pageHeight;
|
|
1620
|
+
const bottoms = () => (kept ? [measure.height] : lines().bottoms);
|
|
1577
1621
|
return {
|
|
1578
1622
|
height: measure.height,
|
|
1579
|
-
marginTop:
|
|
1580
|
-
marginBottom:
|
|
1581
|
-
|
|
1582
|
-
|
|
1623
|
+
marginTop: before,
|
|
1624
|
+
marginBottom: after,
|
|
1625
|
+
keepWithNext: desc.word.keepNext,
|
|
1626
|
+
pageHeight,
|
|
1627
|
+
lines: bottoms,
|
|
1583
1628
|
breaksAfter: () => {
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
return [];
|
|
1588
|
-
const { starts } = lines();
|
|
1589
|
-
// The line holding the break: the last one starting at or before it.
|
|
1590
|
-
return breaks.map((leaf) => {
|
|
1629
|
+
// The line holding each break: the last one starting at or before it.
|
|
1630
|
+
const breaks = pageBreaks.map((leaf) => {
|
|
1631
|
+
const { starts } = lines();
|
|
1591
1632
|
let line = starts.length - 1;
|
|
1592
1633
|
while (line > 0 && starts[line] > leaf.offset)
|
|
1593
1634
|
line--;
|
|
1594
1635
|
return line;
|
|
1595
1636
|
});
|
|
1637
|
+
if (next?.word.pageBreakBefore || layout.breakAfter)
|
|
1638
|
+
breaks.push(bottoms().length - 1);
|
|
1639
|
+
return breaks;
|
|
1596
1640
|
},
|
|
1597
1641
|
};
|
|
1598
1642
|
}
|
|
1643
|
+
/**
|
|
1644
|
+
* Writes Word's space above each v3 block as its top margin: the space after the block above
|
|
1645
|
+
* and the space before this one, on the same page; at the top of a page only its space before,
|
|
1646
|
+
* and only on the first page or after an explicit break (a page the text flowed onto drops it).
|
|
1647
|
+
* Written only when it changes.
|
|
1648
|
+
*/
|
|
1649
|
+
function applyGaps(blocks, pages, boxes) {
|
|
1650
|
+
blocks.forEach((desc, i) => {
|
|
1651
|
+
const page = pages[i]?.[0] ?? 0;
|
|
1652
|
+
const above = pages[i - 1];
|
|
1653
|
+
const box = boxes[i];
|
|
1654
|
+
let gap;
|
|
1655
|
+
if (above && above[above.length - 1] === page)
|
|
1656
|
+
gap = boxes[i - 1].marginBottom + box.marginTop;
|
|
1657
|
+
else {
|
|
1658
|
+
const prior = boxes[i - 1];
|
|
1659
|
+
const breaks = prior?.breaksAfter?.() ?? [];
|
|
1660
|
+
const forced = page === 0 || (!!prior && breaks.length > 0 && breaks.includes(prior.lines().length - 1));
|
|
1661
|
+
gap = forced ? box.marginTop : 0;
|
|
1662
|
+
}
|
|
1663
|
+
if (gaps.get(desc.dom) === gap)
|
|
1664
|
+
return;
|
|
1665
|
+
gaps.set(desc.dom, gap);
|
|
1666
|
+
desc.dom.style.marginTop = `${gap}px`;
|
|
1667
|
+
});
|
|
1668
|
+
}
|
|
1669
|
+
/** The gap `applyGaps` last wrote on each block element (the style serializes it rounded). */
|
|
1670
|
+
const gaps = new WeakMap();
|
|
1599
1671
|
/**
|
|
1600
1672
|
* What the next flow can reuse of the last one: the unchanged blocks before the first changed
|
|
1601
1673
|
* (or stale) one, and the unchanged tail after the last.
|
|
1602
1674
|
*/
|
|
1603
|
-
function reuseOf(last, nodes, stale) {
|
|
1675
|
+
function reuseOf(last, nodes, stale, margin = 0) {
|
|
1604
1676
|
const before = last.nodes;
|
|
1605
1677
|
const same = (a, b) => a !== undefined && a === b && !stale.has(a);
|
|
1606
1678
|
let from = 0;
|
|
@@ -1611,6 +1683,9 @@ function reuseOf(last, nodes, stale) {
|
|
|
1611
1683
|
tail < before.length - from &&
|
|
1612
1684
|
same(nodes[nodes.length - 1 - tail], before[before.length - 1 - tail]))
|
|
1613
1685
|
tail++;
|
|
1686
|
+
// Blocks whose box depends on a changed neighbour are not reused either.
|
|
1687
|
+
from = Math.max(0, from - margin);
|
|
1688
|
+
tail = Math.max(0, tail - margin);
|
|
1614
1689
|
const delta = nodes.length - before.length;
|
|
1615
1690
|
return {
|
|
1616
1691
|
previous: last.result,
|