@folijs/view 0.1.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 +54 -0
- package/README.md +4 -3
- package/dist/clipboard.d.ts +37 -11
- package/dist/clipboard.d.ts.map +1 -1
- package/dist/clipboard.js +413 -66
- 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 -7
- package/dist/render.d.ts.map +1 -1
- package/dist/render.js +0 -67
- package/dist/render.js.map +1 -1
- package/dist/view.d.ts +67 -10
- package/dist/view.d.ts.map +1 -1
- package/dist/view.js +605 -114
- 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 +465 -76
- package/src/index.ts +6 -0
- package/src/layout/flow.ts +36 -13
- package/src/render.ts +1 -72
- package/src/view.ts +648 -113
- package/src/word/blocks.ts +777 -0
- package/src/word/edit.ts +360 -0
- package/src/word/pages.ts +372 -0
package/src/view.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
2
|
AllSelection,
|
|
3
|
+
arrowAcrossLeaf,
|
|
4
|
+
autolinkHref,
|
|
3
5
|
type Command,
|
|
4
6
|
closeHistory,
|
|
5
7
|
type DocumentSettings,
|
|
@@ -8,19 +10,32 @@ import {
|
|
|
8
10
|
deleteSelection,
|
|
9
11
|
type Editor,
|
|
10
12
|
type EditorState,
|
|
13
|
+
fitPaste,
|
|
14
|
+
fitPastedV2,
|
|
11
15
|
insertHardBreak,
|
|
16
|
+
insertLink,
|
|
12
17
|
insertText,
|
|
18
|
+
isSafeHref,
|
|
13
19
|
type Node as ModelNode,
|
|
14
20
|
marksAt,
|
|
21
|
+
NodeSelection,
|
|
22
|
+
parseContentV2,
|
|
15
23
|
redo,
|
|
24
|
+
type SectionJSON,
|
|
16
25
|
Selection,
|
|
17
26
|
Slice,
|
|
27
|
+
sectionSettings,
|
|
28
|
+
setLink,
|
|
18
29
|
splitBlock,
|
|
19
30
|
TextSelection,
|
|
20
31
|
type Transaction,
|
|
21
32
|
TransformError,
|
|
22
33
|
toggleMark,
|
|
34
|
+
typedMarks,
|
|
23
35
|
undo,
|
|
36
|
+
type WordContext,
|
|
37
|
+
withEnvelope,
|
|
38
|
+
wordContext,
|
|
24
39
|
} from "@folijs/core";
|
|
25
40
|
import { docFromClipboard, FOLI_MIME, serializeRange } from "./clipboard.js";
|
|
26
41
|
import {
|
|
@@ -28,11 +43,20 @@ import {
|
|
|
28
43
|
contentHeight,
|
|
29
44
|
DEFAULT_RULES,
|
|
30
45
|
type FlowResult,
|
|
46
|
+
type FlowRules,
|
|
31
47
|
flowFrom,
|
|
32
48
|
PX_PER_MM,
|
|
33
49
|
type Reuse,
|
|
34
50
|
} from "./layout/flow.js";
|
|
35
|
-
import { type BlockDesc, PAGE_BREAK_ATTR
|
|
51
|
+
import { type BlockDesc, PAGE_BREAK_ATTR } from "./render.js";
|
|
52
|
+
import { spacing, type WordBlockDesc, WordRenderer } from "./word/edit.js";
|
|
53
|
+
import { measureLineRatio } from "./word/pages.js";
|
|
54
|
+
|
|
55
|
+
/** Word's paragraph spacing for a schema v3 document (`flow`'s `word` spacing). */
|
|
56
|
+
const WORD_RULES: FlowRules = { ...DEFAULT_RULES, spacing: "word" };
|
|
57
|
+
|
|
58
|
+
/** Class of the element of a node-selected block (a rule). */
|
|
59
|
+
const SELECTED_CLASS = "foli-selected-node";
|
|
36
60
|
|
|
37
61
|
interface DomPoint {
|
|
38
62
|
node: Node;
|
|
@@ -75,19 +99,122 @@ let viewCount = 0;
|
|
|
75
99
|
|
|
76
100
|
/**
|
|
77
101
|
* Print rules for the page settings of the view whose host has `VIEW_ATTR` = `id`: one sheet of
|
|
78
|
-
*
|
|
79
|
-
* exactly as tall as a sheet (the flow keeps content inside it; the exact height absorbs its
|
|
102
|
+
* its section's size per page (the last section's where named pages are not supported), without
|
|
103
|
+
* 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
|
|
80
104
|
* half-pixel slack), with no gap after it. Content taller than a page (a line taller than a
|
|
81
105
|
* page) is not cut: it prints over the next sheet.
|
|
82
106
|
* 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.
|
|
83
107
|
*/
|
|
84
|
-
function printStyles(
|
|
85
|
-
const { width, height } = pageSize;
|
|
108
|
+
function printStyles(settings: DocumentSettings, sheets: readonly Sheet[], id: string): string {
|
|
109
|
+
const { width, height } = settings.pageSize;
|
|
110
|
+
const last = sheetOf(settings, 0).css;
|
|
86
111
|
const pages = `[${VIEW_ATTR}="${id}"]>.foli-page`;
|
|
112
|
+
// Sections with other page setups: a named page each (engines without named pages print
|
|
113
|
+
// every sheet at the last section's size).
|
|
114
|
+
const named = sheets.every((sheet) => sheet.css === last)
|
|
115
|
+
? ""
|
|
116
|
+
: sheets
|
|
117
|
+
.map(({ settings: { pageSize: size }, index }) => {
|
|
118
|
+
const name = `foli-${id}-${index}`;
|
|
119
|
+
return (
|
|
120
|
+
`@page ${name}{size:${size.width}mm ${size.height}mm;margin:0}` +
|
|
121
|
+
`${pages}[${SHEET_ATTR}="${index}"]{page:${name};height:${size.height}mm!important}`
|
|
122
|
+
);
|
|
123
|
+
})
|
|
124
|
+
.join("");
|
|
87
125
|
return (
|
|
88
126
|
`@media print{@page{size:${width}mm ${height}mm;margin:0}` +
|
|
89
127
|
`${pages}{height:${height}mm!important;margin-block:0!important;` +
|
|
90
|
-
`break-after:page;break-inside:avoid}${pages}:last-child{break-after:auto}}`
|
|
128
|
+
`break-after:page;break-inside:avoid}${pages}:last-child{break-after:auto}${named}}`
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** The attribute naming a page's page setup (its index among the document's) for print. */
|
|
133
|
+
const SHEET_ATTR = "data-foli-sheet";
|
|
134
|
+
|
|
135
|
+
/** A page setup: its settings (mm), the page style it gives, its index among the document's. */
|
|
136
|
+
interface Sheet {
|
|
137
|
+
readonly settings: DocumentSettings;
|
|
138
|
+
readonly css: string;
|
|
139
|
+
readonly index: number;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function sheetOf(settings: DocumentSettings, index: number): Sheet {
|
|
143
|
+
const { pageSize, margins } = settings;
|
|
144
|
+
const css =
|
|
145
|
+
`box-sizing: border-box; width: ${pageSize.width}mm; min-height: ${pageSize.height}mm; ` +
|
|
146
|
+
`padding: ${margins.top}mm ${margins.right}mm ${margins.bottom}mm ${margins.left}mm;`;
|
|
147
|
+
return { settings, css, index };
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** The style each page element was last given (its `cssText` reads back normalized). */
|
|
151
|
+
const pageSheets = new WeakMap<HTMLElement, string>();
|
|
152
|
+
|
|
153
|
+
/** A block's section: its page setup, and whether the next block starts a new page. */
|
|
154
|
+
interface BlockLayout {
|
|
155
|
+
readonly sheet: Sheet;
|
|
156
|
+
readonly breakAfter: boolean;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* The section of each top-level block (the section a paragraph's `section` ends; the
|
|
161
|
+
* envelope's for the blocks after the last such paragraph). A section starts a new page,
|
|
162
|
+
* unless it is continuous with the same page setup as the one before (shortcut: a continuous
|
|
163
|
+
* section with another page setup starts a new page; Word's even and odd page sections do not
|
|
164
|
+
* add a blank page), as `renderDocument` lays sections out.
|
|
165
|
+
*/
|
|
166
|
+
function sectionLayouts(doc: ModelNode, last: SectionJSON): BlockLayout[] {
|
|
167
|
+
const sheets = new Map<string, Sheet>();
|
|
168
|
+
const bySection = new Map<SectionJSON, Sheet>();
|
|
169
|
+
const sheet = (section: SectionJSON) => {
|
|
170
|
+
let found = bySection.get(section);
|
|
171
|
+
if (!found) {
|
|
172
|
+
const settings = sectionSettings(section);
|
|
173
|
+
const css = sheetOf(settings, 0).css;
|
|
174
|
+
found = sheets.get(css) ?? sheetOf(settings, sheets.size);
|
|
175
|
+
sheets.set(css, found);
|
|
176
|
+
bySection.set(section, found);
|
|
177
|
+
}
|
|
178
|
+
return found;
|
|
179
|
+
};
|
|
180
|
+
const sections: SectionJSON[] = [];
|
|
181
|
+
let section = last;
|
|
182
|
+
for (let i = doc.childCount - 1; i >= 0; i--) {
|
|
183
|
+
const own = doc.child(i).attrs.section as SectionJSON | null | undefined;
|
|
184
|
+
if (own) section = own;
|
|
185
|
+
sections[i] = section;
|
|
186
|
+
}
|
|
187
|
+
// Indexes in document order: the first page setup is 0.
|
|
188
|
+
sections.forEach(sheet);
|
|
189
|
+
return sections.map((own, i) => {
|
|
190
|
+
const next = sections[i + 1];
|
|
191
|
+
// By position, as `renderDocument`: two paragraphs may end sections with one object.
|
|
192
|
+
const ends = !!next && !!doc.child(i).attrs.section;
|
|
193
|
+
const breakAfter = ends && !(next.type === "continuous" && sheet(next).css === sheet(own).css);
|
|
194
|
+
return { sheet: sheet(own), breakAfter };
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Initial pages (before a flow), moved where needed so that each page holds blocks of one
|
|
200
|
+
* page setup: a block is measured in a page of its own section's width.
|
|
201
|
+
*/
|
|
202
|
+
function separateSheets(
|
|
203
|
+
pages: readonly (readonly number[])[],
|
|
204
|
+
layout: readonly BlockLayout[],
|
|
205
|
+
): number[][] {
|
|
206
|
+
const css: string[] = [];
|
|
207
|
+
let floor = 0;
|
|
208
|
+
return pages.map((list, i) =>
|
|
209
|
+
list.map((p) => {
|
|
210
|
+
const want = (layout[i] as BlockLayout).sheet.css;
|
|
211
|
+
let page = Math.max(p, floor);
|
|
212
|
+
// Only the last page used so far has a setup: the next one is free.
|
|
213
|
+
if (css[page] !== undefined && css[page] !== want) page++;
|
|
214
|
+
css[page] ??= want;
|
|
215
|
+
floor = page;
|
|
216
|
+
return page;
|
|
217
|
+
}),
|
|
91
218
|
);
|
|
92
219
|
}
|
|
93
220
|
|
|
@@ -100,20 +227,34 @@ function printStyles({ pageSize }: DocumentSettings, id: string): string {
|
|
|
100
227
|
* view cannot prevent (Android keyboards, extensions). A MutationObserver records those; the view
|
|
101
228
|
* then reads the changed text back into a transaction (`readBack`) and re-renders.
|
|
102
229
|
*
|
|
103
|
-
*
|
|
230
|
+
* Blocks are the document's paragraphs and opaque blocks (a schema v3 document has no
|
|
231
|
+
* containers: a list is numbered paragraphs).
|
|
104
232
|
*/
|
|
105
233
|
export class EditorView {
|
|
106
234
|
private state: EditorState;
|
|
107
|
-
private blocks:
|
|
235
|
+
private blocks: WordBlockDesc[] = [];
|
|
108
236
|
/** Position before each block, parallel to `blocks`. */
|
|
109
237
|
private starts: number[] = [];
|
|
238
|
+
/** The index of each block in `blocks`. */
|
|
239
|
+
private indexOf = new Map<BlockDesc, number>();
|
|
240
|
+
/** The element that shows a node selection, if any. */
|
|
241
|
+
private selectedDom: HTMLElement | null = null;
|
|
110
242
|
/** The page of each fragment of each block. */
|
|
111
243
|
private fragmentPages = new Map<BlockDesc, readonly number[]>();
|
|
112
244
|
/** The block each fragment element belongs to. */
|
|
113
245
|
private owner = new Map<Node, BlockDesc>();
|
|
114
246
|
/** The page elements, in order; the host's only children. */
|
|
115
247
|
private pages: HTMLElement[] = [];
|
|
116
|
-
|
|
248
|
+
/** The page setup of each page (its section's), parallel to `pages`. */
|
|
249
|
+
private sheets: Sheet[] = [];
|
|
250
|
+
/** The page setups the print rules were written for. */
|
|
251
|
+
private printKey = "";
|
|
252
|
+
/**
|
|
253
|
+
* Each block's page setup and whether a section break follows it, as last rendered: a block
|
|
254
|
+
* whose layout changed (a section break inserted or removed around it) is measured and
|
|
255
|
+
* flowed again.
|
|
256
|
+
*/
|
|
257
|
+
private layoutKeys = new WeakMap<ModelNode, string>();
|
|
117
258
|
/** Marks the host for this view's print rules (`printStyles`). */
|
|
118
259
|
private readonly id = String(++viewCount);
|
|
119
260
|
/**
|
|
@@ -128,7 +269,7 @@ export class EditorView {
|
|
|
128
269
|
* Measurements by model node: a node's rendered height, margins and lines only depend on
|
|
129
270
|
* its content (unchanged node, same identity), the page width and the CSS. Cleared when the
|
|
130
271
|
* page settings or the fonts change; single entries go stale when a block's size changes
|
|
131
|
-
* without an edit (`ResizeObserver`: CSS, fonts
|
|
272
|
+
* without an edit (`ResizeObserver`: CSS, fonts).
|
|
132
273
|
*/
|
|
133
274
|
private measures = new WeakMap<ModelNode, Measure>();
|
|
134
275
|
private readonly stale = new Set<ModelNode>();
|
|
@@ -153,6 +294,8 @@ export class EditorView {
|
|
|
153
294
|
private readonly observer: MutationObserver;
|
|
154
295
|
private readonly unsubscribe: () => void;
|
|
155
296
|
private editable = true;
|
|
297
|
+
/** Escape was the last key: Tab now leaves the editor. */
|
|
298
|
+
private tabLeaves = false;
|
|
156
299
|
private placeholder = "";
|
|
157
300
|
/** The block that shows the placeholder, if any. */
|
|
158
301
|
private placeholderDom: HTMLElement | null = null;
|
|
@@ -160,6 +303,12 @@ export class EditorView {
|
|
|
160
303
|
private pointerFocus = false;
|
|
161
304
|
/** Whether the view added the host's tabindex (a read-only host must stay focusable). */
|
|
162
305
|
private readonly addedTabIndex: boolean;
|
|
306
|
+
/** Renders the document's blocks; made by the first render. */
|
|
307
|
+
private word: WordRenderer | null = null;
|
|
308
|
+
/** Fonts' normal line height ratios, measured once per family (Word's single spacing). */
|
|
309
|
+
private readonly ratios = new Map<string, number>();
|
|
310
|
+
/** Web fonts loaded since the v3 blocks were drawn: the next render draws them again. */
|
|
311
|
+
private fontsChanged = false;
|
|
163
312
|
|
|
164
313
|
constructor(
|
|
165
314
|
readonly host: HTMLElement,
|
|
@@ -194,6 +343,8 @@ export class EditorView {
|
|
|
194
343
|
host.addEventListener("copy", this.onCopy);
|
|
195
344
|
host.addEventListener("cut", this.onCut);
|
|
196
345
|
host.addEventListener("paste", this.onPaste);
|
|
346
|
+
host.addEventListener("click", this.onClick);
|
|
347
|
+
host.addEventListener("auxclick", this.onClick);
|
|
197
348
|
host.ownerDocument.addEventListener("selectionchange", this.onSelectionChange);
|
|
198
349
|
host.ownerDocument.fonts?.addEventListener("loadingdone", this.onFontsLoaded);
|
|
199
350
|
this.unsubscribe = editor.subscribe((state) => this.update(state));
|
|
@@ -218,6 +369,8 @@ export class EditorView {
|
|
|
218
369
|
host.removeEventListener("copy", this.onCopy);
|
|
219
370
|
host.removeEventListener("cut", this.onCut);
|
|
220
371
|
host.removeEventListener("paste", this.onPaste);
|
|
372
|
+
host.removeEventListener("click", this.onClick);
|
|
373
|
+
host.removeEventListener("auxclick", this.onClick);
|
|
221
374
|
host.ownerDocument.removeEventListener("selectionchange", this.onSelectionChange);
|
|
222
375
|
const owned = [
|
|
223
376
|
"contenteditable",
|
|
@@ -287,6 +440,7 @@ export class EditorView {
|
|
|
287
440
|
const selectionChanged = !state.selection.eq(this.state.selection);
|
|
288
441
|
this.state = state;
|
|
289
442
|
if (docChanged) this.render();
|
|
443
|
+
this.showSelectedNode();
|
|
290
444
|
this.writeSelection(docChanged);
|
|
291
445
|
// shortcut: scrolls for every change while focused; once remote or plugin changes exist, scroll only for local input (a transaction flag).
|
|
292
446
|
if (docChanged || selectionChanged) this.scrollToSelection();
|
|
@@ -329,6 +483,28 @@ export class EditorView {
|
|
|
329
483
|
private render(): void {
|
|
330
484
|
const { doc } = this.state;
|
|
331
485
|
const ownerDocument = this.host.ownerDocument;
|
|
486
|
+
// A new document (envelope) gets its renderer; nothing rendered before is reused then. An
|
|
487
|
+
// editor's state always has one (`createEditor`).
|
|
488
|
+
const context = wordContext(this.state) as WordContext;
|
|
489
|
+
// Fonts loaded: a new renderer (new line heights), but not while a composition runs in a
|
|
490
|
+
// block it would redraw.
|
|
491
|
+
const renewed =
|
|
492
|
+
context !== (this.word?.context ?? null) || (this.fontsChanged && !this.composing);
|
|
493
|
+
if (renewed) this.fontsChanged = false;
|
|
494
|
+
let word = this.word as WordRenderer;
|
|
495
|
+
if (renewed) {
|
|
496
|
+
word = this.word = new WordRenderer(context, ownerDocument, this.lineRatio);
|
|
497
|
+
this.invalidateLayout();
|
|
498
|
+
}
|
|
499
|
+
word.prepare(doc);
|
|
500
|
+
const layout = sectionLayouts(doc, context.envelope.section);
|
|
501
|
+
doc.forEach((node, _, i) => {
|
|
502
|
+
const { sheet, breakAfter } = layout[i] as BlockLayout;
|
|
503
|
+
const key = `${sheet.css}${breakAfter ? "|break" : ""}`;
|
|
504
|
+
const before = this.layoutKeys.get(node);
|
|
505
|
+
if (before !== undefined && before !== key) this.stale.add(node);
|
|
506
|
+
this.layoutKeys.set(node, key);
|
|
507
|
+
});
|
|
332
508
|
const previous = new Map(
|
|
333
509
|
this.blocks.filter((desc) => !this.dirty.has(desc)).map((desc) => [desc.node, desc]),
|
|
334
510
|
);
|
|
@@ -339,32 +515,38 @@ export class EditorView {
|
|
|
339
515
|
this.linesStale = false;
|
|
340
516
|
this.flowed = null;
|
|
341
517
|
}
|
|
342
|
-
let blocks:
|
|
518
|
+
let blocks: WordBlockDesc[] = [];
|
|
343
519
|
const starts: number[] = [];
|
|
344
|
-
doc.forEach((node,
|
|
345
|
-
let reused = previous.get(node);
|
|
520
|
+
doc.forEach((node, pos) => {
|
|
521
|
+
let reused = renewed ? undefined : previous.get(node);
|
|
522
|
+
// A block whose list number or comment shading changed (an edit before it) is redrawn.
|
|
523
|
+
if (reused && reused.key !== word.key(blocks.length)) {
|
|
524
|
+
reused = undefined;
|
|
525
|
+
this.stale.add(node);
|
|
526
|
+
}
|
|
346
527
|
// A split block about to be measured again (fonts, CSS) is rendered whole first: each
|
|
347
528
|
// fragment's end breaks its line, so its lines would depend on where it was split before.
|
|
348
529
|
const remeasure = dropLines || !this.measures.has(node) || this.stale.has(node);
|
|
349
530
|
if (reused && reused.fragments.length > 1 && remeasure && !this.composing) reused = undefined;
|
|
350
531
|
if (reused) previous.delete(node);
|
|
351
|
-
blocks.push(reused ??
|
|
352
|
-
starts.push(
|
|
532
|
+
blocks.push(reused ?? word.render(node, blocks.length));
|
|
533
|
+
starts.push(pos);
|
|
353
534
|
});
|
|
354
535
|
for (const stale of previous.values())
|
|
355
536
|
for (const fragment of stale.fragments) fragment.remove();
|
|
356
|
-
this.
|
|
537
|
+
this.applyPrintStyles(layout);
|
|
357
538
|
let last = 0;
|
|
358
539
|
let pages = blocks.map((desc) => {
|
|
359
540
|
const before = previousPages.get(desc);
|
|
360
541
|
return desc.fragments.map((_, f) => (last = Math.max(last, before?.[f] ?? last)));
|
|
361
542
|
});
|
|
362
|
-
|
|
543
|
+
pages = separateSheets(pages, layout);
|
|
544
|
+
this.place(blocks, pages, layout);
|
|
363
545
|
if (!this.composing) {
|
|
364
546
|
// DOM reads only for blocks without a valid measurement (new, changed or stale ones).
|
|
365
547
|
let scale: number | undefined;
|
|
366
548
|
const layoutPx = () => {
|
|
367
|
-
scale ??= layoutScale(this.pages[0] as HTMLElement, this.
|
|
549
|
+
scale ??= layoutScale(this.pages[0] as HTMLElement, (this.sheets[0] as Sheet).settings);
|
|
368
550
|
return scale;
|
|
369
551
|
};
|
|
370
552
|
if (dropLines)
|
|
@@ -373,7 +555,8 @@ export class EditorView {
|
|
|
373
555
|
if (measure) delete measure.lines;
|
|
374
556
|
}
|
|
375
557
|
const nodes = blocks.map((desc) => desc.node);
|
|
376
|
-
|
|
558
|
+
// A block's spacing depends on its neighbours' styles: reuse stops a block earlier.
|
|
559
|
+
const reuse = this.flowed ? reuseOf(this.flowed, nodes, this.stale, 1) : undefined;
|
|
377
560
|
const measureOf = (desc: BlockDesc) => {
|
|
378
561
|
let found = this.measures.get(desc.node);
|
|
379
562
|
// A stale block is measured again once per render.
|
|
@@ -388,8 +571,10 @@ export class EditorView {
|
|
|
388
571
|
found.lines ??= measureLines(desc, layoutPx());
|
|
389
572
|
return found.lines;
|
|
390
573
|
};
|
|
391
|
-
const boxes = blocks.map((desc
|
|
392
|
-
|
|
574
|
+
const boxes = blocks.map((desc, i) =>
|
|
575
|
+
wordBox(blocks, i, measureOf(desc), () => lines(desc), layout[i] as BlockLayout),
|
|
576
|
+
);
|
|
577
|
+
const result = flowFrom(boxes, contentHeight(this.editor.settings), WORD_RULES, reuse);
|
|
393
578
|
this.flowed = { nodes, result };
|
|
394
579
|
this.stale.clear();
|
|
395
580
|
const flowed = result.fragments;
|
|
@@ -403,13 +588,15 @@ export class EditorView {
|
|
|
403
588
|
});
|
|
404
589
|
const same =
|
|
405
590
|
splits.length === desc.splits.length && splits.every((at, k) => at === desc.splits[k]);
|
|
406
|
-
return same ? desc :
|
|
591
|
+
return same ? desc : word.render(desc.node, i, splits);
|
|
407
592
|
});
|
|
408
593
|
pages = flowed.map((fragments) => fragments.map((fragment) => fragment.page));
|
|
409
|
-
|
|
594
|
+
applyGaps(blocks, pages, boxes);
|
|
595
|
+
this.place(blocks, pages, layout);
|
|
410
596
|
}
|
|
411
597
|
this.blocks = blocks;
|
|
412
598
|
this.starts = starts;
|
|
599
|
+
this.indexOf = new Map(blocks.map((desc, i) => [desc, i]));
|
|
413
600
|
this.fragmentPages = new Map(blocks.map((desc, i) => [desc, pages[i] ?? []]));
|
|
414
601
|
this.owner = new Map(
|
|
415
602
|
blocks.flatMap((desc) => desc.fragments.map((fragment) => [fragment, desc] as const)),
|
|
@@ -417,49 +604,81 @@ export class EditorView {
|
|
|
417
604
|
this.unflowed = this.composing;
|
|
418
605
|
this.observeSizes();
|
|
419
606
|
this.showPlaceholder();
|
|
607
|
+
this.showSelectedNode();
|
|
420
608
|
// The view's own DOM changes are not input. During a composition, keep what the browser
|
|
421
609
|
// changed before this render: the touched blocks are read back at compositionend.
|
|
422
610
|
const records = this.observer.takeRecords();
|
|
423
611
|
if (this.composing) this.pending.push(...records);
|
|
424
612
|
}
|
|
425
613
|
|
|
614
|
+
/** A font's normal line height ratio, measured outside the host (no input to read back). */
|
|
615
|
+
private lineRatio = (family: string): number => {
|
|
616
|
+
let ratio = this.ratios.get(family);
|
|
617
|
+
if (ratio === undefined) {
|
|
618
|
+
const doc = this.host.ownerDocument;
|
|
619
|
+
ratio = measureLineRatio(doc.body ?? doc.documentElement)(family);
|
|
620
|
+
this.ratios.set(family, ratio);
|
|
621
|
+
}
|
|
622
|
+
return ratio;
|
|
623
|
+
};
|
|
624
|
+
|
|
426
625
|
/**
|
|
427
626
|
* Puts each fragment of each block into its page (pages are non-decreasing in document
|
|
428
|
-
* order), creating and removing pages as needed
|
|
429
|
-
*
|
|
627
|
+
* order), creating and removing pages as needed, and gives each page the page setup of its
|
|
628
|
+
* blocks' section (one per page: `separateSheets`, section breaks). Anything else in the host
|
|
629
|
+
* or a page (stray DOM) is removed.
|
|
430
630
|
*/
|
|
431
|
-
private place(
|
|
631
|
+
private place(
|
|
632
|
+
blocks: readonly BlockDesc[],
|
|
633
|
+
pages: readonly (readonly number[])[],
|
|
634
|
+
layout: readonly BlockLayout[],
|
|
635
|
+
): void {
|
|
432
636
|
const { host } = this;
|
|
433
637
|
const items = blocks.flatMap((desc, i) =>
|
|
434
638
|
desc.fragments.map((dom, f) => ({ dom, page: pages[i]?.[f] ?? 0 })),
|
|
435
639
|
);
|
|
436
640
|
const count = (items[items.length - 1]?.page ?? 0) + 1;
|
|
641
|
+
const sheets: Sheet[] = [];
|
|
642
|
+
pages.forEach((list, i) => {
|
|
643
|
+
for (const p of list) sheets[p] ??= (layout[i] as BlockLayout).sheet;
|
|
644
|
+
});
|
|
645
|
+
// A page no block starts on (none yet) takes the setup of the page before it.
|
|
646
|
+
for (let p = 0; p < count; p++)
|
|
647
|
+
sheets[p] ??=
|
|
648
|
+
sheets[p - 1] ?? (layout[0] as BlockLayout | undefined)?.sheet ?? this.lastSheet();
|
|
649
|
+
this.sheets = sheets.slice(0, count);
|
|
437
650
|
while (this.pages.length < count) this.pages.push(this.createPage());
|
|
651
|
+
this.sheets.forEach((sheet, p) => {
|
|
652
|
+
const page = this.pages[p] as HTMLElement;
|
|
653
|
+
const key = `${sheet.index} ${sheet.css}`;
|
|
654
|
+
if (pageSheets.get(page) === key) return;
|
|
655
|
+
pageSheets.set(page, key);
|
|
656
|
+
page.style.cssText = sheet.css;
|
|
657
|
+
page.setAttribute(SHEET_ATTR, String(sheet.index));
|
|
658
|
+
});
|
|
438
659
|
// Pages first, in order; anything else in the host ends up after them.
|
|
439
660
|
let cursor = host.firstChild;
|
|
440
661
|
for (const page of this.pages.slice(0, count)) {
|
|
441
662
|
if (page === cursor) cursor = cursor.nextSibling;
|
|
442
663
|
else host.insertBefore(page, cursor);
|
|
443
664
|
}
|
|
444
|
-
const blockDoms = new Set<Node>(items.map((item) => item.dom));
|
|
445
665
|
let i = 0;
|
|
446
666
|
for (let p = 0; p < count; p++) {
|
|
447
667
|
const page = this.pages[p] as HTMLElement;
|
|
448
668
|
let at = page.firstChild;
|
|
449
669
|
for (; i < items.length && items[i]?.page === p; i++) {
|
|
450
|
-
const dom =
|
|
670
|
+
const { dom } = items[i] as (typeof items)[number];
|
|
451
671
|
if (dom === at) at = at.nextSibling;
|
|
452
672
|
else page.insertBefore(dom, at);
|
|
453
673
|
}
|
|
454
|
-
// What is left
|
|
674
|
+
// What is left after the last placed block: strays, and blocks of later pages.
|
|
455
675
|
while (at) {
|
|
456
676
|
const next: ChildNode | null = at.nextSibling;
|
|
457
|
-
|
|
677
|
+
at.remove();
|
|
458
678
|
at = next;
|
|
459
679
|
}
|
|
460
680
|
}
|
|
461
|
-
// Pages no longer needed and stray DOM in the host: all after the last page by now.
|
|
462
|
-
// after the block loop, since a block found loose in the host has just moved into a page.
|
|
681
|
+
// Pages no longer needed and stray DOM in the host: all after the last page by now.
|
|
463
682
|
let rest = (this.pages[count - 1] as HTMLElement).nextSibling;
|
|
464
683
|
while (rest) {
|
|
465
684
|
const next: ChildNode | null = rest.nextSibling;
|
|
@@ -472,19 +691,20 @@ export class EditorView {
|
|
|
472
691
|
private createPage(): HTMLElement {
|
|
473
692
|
const page = this.host.ownerDocument.createElement("section");
|
|
474
693
|
page.className = "foli-page";
|
|
475
|
-
page.style.cssText = this.pageStyle;
|
|
476
694
|
return page;
|
|
477
695
|
}
|
|
478
696
|
|
|
479
|
-
/**
|
|
480
|
-
private
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
697
|
+
/** The last section's page setup (the envelope's), for an empty document. */
|
|
698
|
+
private lastSheet(): Sheet {
|
|
699
|
+
return sheetOf(this.editor.settings, 0);
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
/** The print rules for the document's page setups, written when they change. */
|
|
703
|
+
private applyPrintStyles(layout: readonly BlockLayout[]): void {
|
|
704
|
+
const sheets = [...new Set(layout.map((block) => block.sheet))];
|
|
705
|
+
const key = [this.lastSheet(), ...sheets].map((sheet) => sheet.css).join("\n");
|
|
706
|
+
if (key === this.printKey) return;
|
|
707
|
+
this.printKey = key;
|
|
488
708
|
// shortcut: written when the page settings change; a host moved to another document or shadow root keeps them where they were until then.
|
|
489
709
|
for (const style of this.printStyles) style.remove();
|
|
490
710
|
const doc = this.host.ownerDocument;
|
|
@@ -494,11 +714,10 @@ export class EditorView {
|
|
|
494
714
|
this.printStyles = parents.map((parent) => {
|
|
495
715
|
const style = doc.createElement("style");
|
|
496
716
|
style.setAttribute("data-foli-print", "");
|
|
497
|
-
style.textContent = printStyles(this.editor.settings, this.id);
|
|
717
|
+
style.textContent = printStyles(this.editor.settings, sheets, this.id);
|
|
498
718
|
parent.append(style);
|
|
499
719
|
return style;
|
|
500
720
|
});
|
|
501
|
-
this.invalidateLayout();
|
|
502
721
|
}
|
|
503
722
|
|
|
504
723
|
/** Forgets every measurement: the next flow measures the whole document again. */
|
|
@@ -509,6 +728,9 @@ export class EditorView {
|
|
|
509
728
|
|
|
510
729
|
/** Re-flows the pages once web fonts have loaded: line heights and wrapping change. */
|
|
511
730
|
private onFontsLoaded = (): void => {
|
|
731
|
+
// v3 line heights come from the fonts' measured ratios: render every block again.
|
|
732
|
+
this.ratios.clear();
|
|
733
|
+
this.fontsChanged = true;
|
|
512
734
|
this.invalidateLayout();
|
|
513
735
|
this.scheduleRelayout();
|
|
514
736
|
};
|
|
@@ -540,7 +762,7 @@ export class EditorView {
|
|
|
540
762
|
if (!desc || !measured || this.stale.has(desc.node)) continue;
|
|
541
763
|
// A hidden host (display: none) reports every block as 0x0: nothing to re-flow.
|
|
542
764
|
if (!entry.contentRect.width) continue;
|
|
543
|
-
scale ??= layoutScale(this.pages[0] as HTMLElement, this.
|
|
765
|
+
scale ??= layoutScale(this.pages[0] as HTMLElement, (this.sheets[0] as Sheet).settings);
|
|
544
766
|
let height = 0;
|
|
545
767
|
for (const fragment of desc.fragments) height += fragment.getBoundingClientRect().height;
|
|
546
768
|
// A block is measured whole; each fragment of a split one keeps its padding and border,
|
|
@@ -579,20 +801,35 @@ export class EditorView {
|
|
|
579
801
|
|
|
580
802
|
// ---------- position mapping ----------
|
|
581
803
|
|
|
582
|
-
/** The block element
|
|
804
|
+
/** The block element (a fragment of a rendered block) that contains `node`, or null. */
|
|
583
805
|
private blockElement(node: Node | null): Node | null {
|
|
584
|
-
let element = node;
|
|
585
|
-
|
|
586
|
-
if (element.parentNode === this.host) return null;
|
|
587
|
-
element = element.parentNode;
|
|
806
|
+
for (let element = node; element && element !== this.host; element = element.parentNode) {
|
|
807
|
+
if (this.owner.has(element)) return element;
|
|
588
808
|
}
|
|
589
|
-
return
|
|
809
|
+
return null;
|
|
810
|
+
}
|
|
811
|
+
|
|
812
|
+
/** Whether `node` holds blocks rather than content: the host or a page. */
|
|
813
|
+
private holdsBlocks(node: Node): boolean {
|
|
814
|
+
return node === this.host || this.pages.includes(node as HTMLElement);
|
|
815
|
+
}
|
|
816
|
+
|
|
817
|
+
/** Index of the first block that starts at or after `pos` (the block count if none). */
|
|
818
|
+
private blockFrom(pos: number): number {
|
|
819
|
+
let lo = 0;
|
|
820
|
+
let hi = this.starts.length;
|
|
821
|
+
while (lo < hi) {
|
|
822
|
+
const mid = (lo + hi) >> 1;
|
|
823
|
+
if ((this.starts[mid] as number) < pos) lo = mid + 1;
|
|
824
|
+
else hi = mid;
|
|
825
|
+
}
|
|
826
|
+
return lo;
|
|
590
827
|
}
|
|
591
828
|
|
|
592
829
|
/** Model position of a DOM point inside the host, or null if it is outside. */
|
|
593
830
|
posFromDOM(node: Node, offset: number): number | null {
|
|
594
831
|
const { host } = this;
|
|
595
|
-
if (
|
|
832
|
+
if (this.holdsBlocks(node)) {
|
|
596
833
|
// Between fragments: the start of the first one after the point (a block start, or
|
|
597
834
|
// where a continuation starts), else the document end.
|
|
598
835
|
const range = host.ownerDocument.createRange();
|
|
@@ -606,9 +843,14 @@ export class EditorView {
|
|
|
606
843
|
}
|
|
607
844
|
const element = this.blockElement(node);
|
|
608
845
|
const owner = element ? this.owner.get(element) : undefined;
|
|
609
|
-
const index = owner ? this.
|
|
846
|
+
const index = owner ? (this.indexOf.get(owner) ?? -1) : -1;
|
|
610
847
|
const desc = this.blocks[index];
|
|
611
848
|
if (!desc) return null;
|
|
849
|
+
// A point in or at a block leaf (a rule): before it, or after it at its end.
|
|
850
|
+
if (!desc.node.isTextblock) {
|
|
851
|
+
const after = node === desc.dom && offset > 0;
|
|
852
|
+
return (this.starts[index] as number) + (after ? desc.node.nodeSize : 0);
|
|
853
|
+
}
|
|
612
854
|
const contentStart = (this.starts[index] as number) + 1;
|
|
613
855
|
const leaf = desc.leaves.find((l) => l.dom === node);
|
|
614
856
|
if (leaf && node.nodeType === Node.TEXT_NODE)
|
|
@@ -625,10 +867,19 @@ export class EditorView {
|
|
|
625
867
|
/** DOM point for a model position. At a mark boundary the caret stays in the text before it. */
|
|
626
868
|
domFromPos(pos: number): DomPoint {
|
|
627
869
|
const { doc } = this.state;
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
870
|
+
const $pos = doc.resolve(pos);
|
|
871
|
+
if (!$pos.parent.isTextblock) {
|
|
872
|
+
// Between blocks: the start of the next block (before a rule), else the document end.
|
|
873
|
+
const next = this.blocks[this.blockFrom(pos)];
|
|
874
|
+
if (next) return next.node.isTextblock ? fragmentStart(next, 0) : before(next.dom);
|
|
875
|
+
const last = this.blocks[this.blocks.length - 1] as BlockDesc;
|
|
876
|
+
if (!last.node.isTextblock) {
|
|
877
|
+
const point = before(last.dom);
|
|
878
|
+
return { node: point.node, offset: point.offset + 1 };
|
|
879
|
+
}
|
|
880
|
+
return fragmentEnd(last, last.fragments.length - 1);
|
|
881
|
+
}
|
|
882
|
+
const desc = this.blocks[this.blockFrom($pos.before())] as BlockDesc;
|
|
632
883
|
const offset = $pos.parentOffset;
|
|
633
884
|
// Where a fragment starts, show the caret at the start of the next page's fragment, as a
|
|
634
885
|
// browser shows it at the start of the next line at a soft wrap.
|
|
@@ -649,7 +900,10 @@ export class EditorView {
|
|
|
649
900
|
const point = before(last.dom);
|
|
650
901
|
return { node: point.node, offset: point.offset + 1 };
|
|
651
902
|
}
|
|
652
|
-
|
|
903
|
+
// An empty block: before its <br> (after a list number shown in front of it).
|
|
904
|
+
const tail = desc.dom.lastChild;
|
|
905
|
+
const end = tail?.nodeName === "BR" ? desc.dom.childNodes.length - 1 : 0;
|
|
906
|
+
return { node: desc.dom, offset: end };
|
|
653
907
|
}
|
|
654
908
|
|
|
655
909
|
// ---------- selection ----------
|
|
@@ -658,13 +912,50 @@ export class EditorView {
|
|
|
658
912
|
private readSelection(): Selection | null {
|
|
659
913
|
const sel = this.host.ownerDocument.getSelection();
|
|
660
914
|
if (!sel?.anchorNode || !sel.focusNode) return null;
|
|
915
|
+
// A range around exactly one block leaf element (a rule): the node selection that wrote it.
|
|
916
|
+
// (Its end, mapped alone, would be the start of the next block, not the end of the rule.)
|
|
917
|
+
if (sel.anchorNode === sel.focusNode && Math.abs(sel.anchorOffset - sel.focusOffset) === 1) {
|
|
918
|
+
const child = sel.anchorNode.childNodes[Math.min(sel.anchorOffset, sel.focusOffset)];
|
|
919
|
+
const owner = child && this.owner.get(child);
|
|
920
|
+
const index = owner ? (this.indexOf.get(owner) ?? -1) : -1;
|
|
921
|
+
if (owner && !owner.node.isTextblock && NodeSelection.isSelectable(owner.node))
|
|
922
|
+
return NodeSelection.create(this.state.doc, this.starts[index] as number);
|
|
923
|
+
}
|
|
661
924
|
const anchor = this.posFromDOM(sel.anchorNode, sel.anchorOffset);
|
|
662
925
|
const head = this.posFromDOM(sel.focusNode, sel.focusOffset);
|
|
663
926
|
if (anchor === null || head === null) return null;
|
|
664
927
|
const { doc } = this.state;
|
|
928
|
+
// Exactly around a selectable block leaf (a rule): the node selection that wrote it.
|
|
929
|
+
const from = Math.min(anchor, head);
|
|
930
|
+
const node = doc.nodeAt(from);
|
|
931
|
+
if (
|
|
932
|
+
node &&
|
|
933
|
+
node.isBlock &&
|
|
934
|
+
node.isAtom &&
|
|
935
|
+
Math.max(anchor, head) === from + node.nodeSize &&
|
|
936
|
+
NodeSelection.isSelectable(node)
|
|
937
|
+
)
|
|
938
|
+
return NodeSelection.create(doc, from);
|
|
665
939
|
return TextSelection.between(doc.resolve(anchor), doc.resolve(head));
|
|
666
940
|
}
|
|
667
941
|
|
|
942
|
+
/** The rendered block a node selection selects, if it selects one. */
|
|
943
|
+
private selectedBlock(): BlockDesc | null {
|
|
944
|
+
const { selection } = this.state;
|
|
945
|
+
if (!(selection instanceof NodeSelection)) return null;
|
|
946
|
+
const desc = this.blocks[this.blockFrom(selection.from)];
|
|
947
|
+
return desc && this.starts[this.indexOf.get(desc) ?? -1] === selection.from ? desc : null;
|
|
948
|
+
}
|
|
949
|
+
|
|
950
|
+
/** Marks the element of a node-selected block, for styling (the DOM selection is around it). */
|
|
951
|
+
private showSelectedNode(): void {
|
|
952
|
+
const dom = this.selectedBlock()?.dom ?? null;
|
|
953
|
+
if (dom === this.selectedDom) return;
|
|
954
|
+
this.selectedDom?.classList.remove(SELECTED_CLASS);
|
|
955
|
+
dom?.classList.add(SELECTED_CLASS);
|
|
956
|
+
this.selectedDom = dom;
|
|
957
|
+
}
|
|
958
|
+
|
|
668
959
|
/**
|
|
669
960
|
* Brings the model selection up to date with the DOM. `selectionchange` is asynchronous, so
|
|
670
961
|
* every input handler calls this first instead of trusting the last event.
|
|
@@ -704,6 +995,12 @@ export class EditorView {
|
|
|
704
995
|
const current = this.readSelection();
|
|
705
996
|
if (current && this.shows(current)) return;
|
|
706
997
|
}
|
|
998
|
+
const selected = this.selectedBlock();
|
|
999
|
+
if (selected) {
|
|
1000
|
+
const point = before(selected.dom);
|
|
1001
|
+
sel.setBaseAndExtent(point.node, point.offset, point.node, point.offset + 1);
|
|
1002
|
+
return;
|
|
1003
|
+
}
|
|
707
1004
|
// Never show a caret inside a grapheme cluster (the core snaps only out of surrogate pairs).
|
|
708
1005
|
// The model takes the snapped selection back on its next read of the DOM.
|
|
709
1006
|
const from = clusterBoundary(doc, selection.from, false);
|
|
@@ -761,7 +1058,8 @@ export class EditorView {
|
|
|
761
1058
|
this.run(insertText(text));
|
|
762
1059
|
return;
|
|
763
1060
|
case "insertParagraph":
|
|
764
|
-
|
|
1061
|
+
// The same as Enter (a list item splits or ends), for keyboards that send no keydown.
|
|
1062
|
+
if (!this.runKey("Enter")) this.run(splitBlock);
|
|
765
1063
|
return;
|
|
766
1064
|
case "insertLineBreak":
|
|
767
1065
|
this.run(insertHardBreak);
|
|
@@ -802,9 +1100,20 @@ export class EditorView {
|
|
|
802
1100
|
return;
|
|
803
1101
|
}
|
|
804
1102
|
const unit = type.startsWith("deleteWord") ? "word" : "char";
|
|
1103
|
+
// A one-character delete goes through the key bindings first (Backspace at a list item's
|
|
1104
|
+
// start lifts it), as the key itself would.
|
|
1105
|
+
if (unit === "char" && this.runKey(type.endsWith("Forward") ? "Delete" : "Backspace")) return;
|
|
805
1106
|
this.run(type.endsWith("Forward") ? deleteForward(unit) : deleteBackward(unit));
|
|
806
1107
|
};
|
|
807
1108
|
|
|
1109
|
+
/** Runs the first command bound to `name` that applies, through the view's dispatch. */
|
|
1110
|
+
private runKey(name: string): boolean {
|
|
1111
|
+
return this.state.plugins.some((plugin) => {
|
|
1112
|
+
const command = plugin.spec.keymap?.[name];
|
|
1113
|
+
return command ? this.run(command) : false;
|
|
1114
|
+
});
|
|
1115
|
+
}
|
|
1116
|
+
|
|
808
1117
|
/**
|
|
809
1118
|
* Triple-click selects the clicked block's content. Chromium and WebKit natively extend the
|
|
810
1119
|
* selection to the start of the next block, so typing over it would merge the two blocks.
|
|
@@ -814,42 +1123,108 @@ export class EditorView {
|
|
|
814
1123
|
// Focus from a mousedown happens in its default action, before this timer. A mousedown that
|
|
815
1124
|
// focuses nothing (prevented, middle button) must not skip the next keyboard focus.
|
|
816
1125
|
if (this.pointerFocus) setTimeout(() => (this.pointerFocus = false));
|
|
817
|
-
if (event.
|
|
1126
|
+
if (event.button !== 0) return;
|
|
818
1127
|
const element = this.blockElement(event.target as Node | null);
|
|
819
1128
|
const owner = element ? this.owner.get(element) : undefined;
|
|
820
|
-
const index = owner ? this.
|
|
1129
|
+
const index = owner ? (this.indexOf.get(owner) ?? -1) : -1;
|
|
821
1130
|
const desc = this.blocks[index];
|
|
822
1131
|
if (!desc) return;
|
|
823
|
-
event.preventDefault();
|
|
824
|
-
const from = (this.starts[index] as number) + 1;
|
|
825
1132
|
const { doc } = this.state;
|
|
1133
|
+
const start = this.starts[index] as number;
|
|
1134
|
+
let selection: Selection;
|
|
1135
|
+
if (!desc.node.isTextblock) {
|
|
1136
|
+
// A click on a rule selects it (the browser would put the caret beside it); Shift+click
|
|
1137
|
+
// extends the selection natively.
|
|
1138
|
+
if (event.shiftKey || !NodeSelection.isSelectable(desc.node)) return;
|
|
1139
|
+
selection = NodeSelection.create(doc, start);
|
|
1140
|
+
} else if (event.detail === 3) {
|
|
1141
|
+
selection = TextSelection.create(doc, start + 1, start + 1 + desc.node.content.size);
|
|
1142
|
+
} else return;
|
|
1143
|
+
event.preventDefault();
|
|
826
1144
|
this.host.focus();
|
|
827
|
-
this.dispatch(
|
|
828
|
-
this.state.tr
|
|
829
|
-
.setSelection(TextSelection.create(doc, from, from + desc.node.content.size))
|
|
830
|
-
.setStoredMarks(null),
|
|
831
|
-
);
|
|
1145
|
+
this.dispatch(this.state.tr.setSelection(selection).setStoredMarks(null));
|
|
832
1146
|
this.writeSelection(true);
|
|
833
1147
|
};
|
|
834
1148
|
|
|
1149
|
+
/**
|
|
1150
|
+
* A click on a link in the document does not navigate (it places the caret, also in read-only
|
|
1151
|
+
* mode); Ctrl+click (Cmd+click on Apple platforms) opens it in a new tab, without `opener`.
|
|
1152
|
+
*/
|
|
1153
|
+
private onClick = (event: MouseEvent): void => {
|
|
1154
|
+
const target = event.target;
|
|
1155
|
+
const element = target instanceof Element ? target.closest("a") : null;
|
|
1156
|
+
if (!element || !this.host.contains(element)) return;
|
|
1157
|
+
event.preventDefault();
|
|
1158
|
+
if (event.type !== "click" || !(IS_MAC ? event.metaKey : event.ctrlKey)) return;
|
|
1159
|
+
const href = this.linkHref(element);
|
|
1160
|
+
if (href) this.host.ownerDocument.defaultView?.open(href, "_blank", "noopener,noreferrer");
|
|
1161
|
+
};
|
|
1162
|
+
|
|
1163
|
+
/** The href of the link an `<a>` shows, read from the model (the DOM is never the truth). */
|
|
1164
|
+
private linkHref(element: Element): string | null {
|
|
1165
|
+
const pos = this.posFromDOM(element, 0);
|
|
1166
|
+
const type = this.state.schema.marks.link;
|
|
1167
|
+
if (pos === null || !type) return null;
|
|
1168
|
+
const $pos = this.state.doc.resolve(pos);
|
|
1169
|
+
const node = $pos.parent.maybeChild($pos.index());
|
|
1170
|
+
const href = node && type.isInSet(node.marks)?.attrs.href;
|
|
1171
|
+
return isSafeHref(href) ? href : null;
|
|
1172
|
+
}
|
|
1173
|
+
|
|
835
1174
|
private onKeyDown = (event: KeyboardEvent): void => {
|
|
836
1175
|
if (event.isComposing || this.composing) return;
|
|
837
1176
|
this.syncSelection();
|
|
838
|
-
if (this.crossFragmentEdge(event)) {
|
|
1177
|
+
if (this.crossFragmentEdge(event) || this.crossListNumber(event)) {
|
|
839
1178
|
event.preventDefault();
|
|
840
1179
|
return;
|
|
841
1180
|
}
|
|
842
1181
|
const name = keyName(event);
|
|
843
1182
|
// Ctrl/Cmd+Shift+V: the paste event that follows uses only the plain text.
|
|
844
1183
|
this.plainPaste = name === "Mod-Shift-v";
|
|
1184
|
+
// Tab and Shift+Tab move the focus on in read-only mode, and right after Escape where Tab
|
|
1185
|
+
// would type a tab (no keyboard trap).
|
|
1186
|
+
const tab = name === "Tab" || name === "Shift-Tab";
|
|
1187
|
+
const leave = tab && (this.tabLeaves || !this.editable);
|
|
1188
|
+
if (event.key !== "Shift") this.tabLeaves = name === "Escape";
|
|
1189
|
+
if (leave) return;
|
|
845
1190
|
// Like `editor.handleKey`, but through the view's dispatch, so read-only mode drops edits.
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
1191
|
+
// A key no command takes keeps its default (Tab outside a list moves the focus on).
|
|
1192
|
+
if (this.runKey(name)) {
|
|
1193
|
+
event.preventDefault();
|
|
1194
|
+
return;
|
|
1195
|
+
}
|
|
1196
|
+
// Up/Down on a textblock's first/last line next to a rule select it (only the view knows
|
|
1197
|
+
// lines; measured only when a rule is beside the block); else the browser moves the caret.
|
|
1198
|
+
const vertical = name === "ArrowUp" ? -1 : name === "ArrowDown" ? 1 : 0;
|
|
1199
|
+
const across = vertical ? arrowAcrossLeaf(vertical, "line") : null;
|
|
1200
|
+
if (across?.(this.state) && this.caretOnEdgeLine(vertical as -1 | 1) && this.run(across)) {
|
|
1201
|
+
event.preventDefault();
|
|
1202
|
+
}
|
|
851
1203
|
};
|
|
852
1204
|
|
|
1205
|
+
/**
|
|
1206
|
+
* Whether the caret is on the first (`dir` -1) or last (1) line of its textblock, in the
|
|
1207
|
+
* block's first or last fragment (a block split across pages has several).
|
|
1208
|
+
*/
|
|
1209
|
+
private caretOnEdgeLine(dir: -1 | 1): boolean {
|
|
1210
|
+
const sel = this.host.ownerDocument.getSelection();
|
|
1211
|
+
if (!sel?.rangeCount || !sel.isCollapsed || !sel.focusNode) return false;
|
|
1212
|
+
const element = this.blockElement(sel.focusNode);
|
|
1213
|
+
const desc = element ? this.owner.get(element) : undefined;
|
|
1214
|
+
if (!desc?.node.isTextblock) return false;
|
|
1215
|
+
const fragment = desc.fragments[dir < 0 ? 0 : desc.fragments.length - 1];
|
|
1216
|
+
if (!fragment || element !== fragment) return false;
|
|
1217
|
+
if (!desc.node.content.size) return true;
|
|
1218
|
+
const caret = sel.getRangeAt(0).getClientRects()[0];
|
|
1219
|
+
const lines = this.host.ownerDocument.createRange();
|
|
1220
|
+
lines.selectNodeContents(fragment);
|
|
1221
|
+
const rects = lines.getClientRects();
|
|
1222
|
+
const edge = rects[dir < 0 ? 0 : rects.length - 1];
|
|
1223
|
+
// No caret box (some positions next to a line break): leave the move to the browser.
|
|
1224
|
+
if (!caret || !edge) return false;
|
|
1225
|
+
return dir < 0 ? caret.top + 1 < edge.bottom : caret.bottom - 1 > edge.top;
|
|
1226
|
+
}
|
|
1227
|
+
|
|
853
1228
|
/**
|
|
854
1229
|
* Caret affinity at a page break. The end of one fragment and the start of the next are two
|
|
855
1230
|
* DOM caret stops for one model position, so a native Left/Right (or word move) there would
|
|
@@ -873,7 +1248,7 @@ export class EditorView {
|
|
|
873
1248
|
const f = desc.fragments.indexOf(element as HTMLElement);
|
|
874
1249
|
const head = this.posFromDOM(sel.focusNode, sel.focusOffset);
|
|
875
1250
|
if (head === null) return false;
|
|
876
|
-
const offset = head - (this.starts[this.
|
|
1251
|
+
const offset = head - (this.starts[this.indexOf.get(desc) ?? -1] as number) - 1;
|
|
877
1252
|
let target: DomPoint | null = null;
|
|
878
1253
|
// Stepping back over an edge after a hard break lands before the <br>: the point after it
|
|
879
1254
|
// is still the split, and browsers would show it before the <br> anyway. That step is
|
|
@@ -904,6 +1279,30 @@ export class EditorView {
|
|
|
904
1279
|
return true;
|
|
905
1280
|
}
|
|
906
1281
|
|
|
1282
|
+
/**
|
|
1283
|
+
* A v3 list number is not text, but browsers stop the caret before it as well as after: Right
|
|
1284
|
+
* at the end of the paragraph before, or Left at a numbered paragraph's start, would seem to do
|
|
1285
|
+
* nothing. Moves over the paragraph boundary in one step (word moves are left to the browser).
|
|
1286
|
+
*/
|
|
1287
|
+
private crossListNumber(event: KeyboardEvent): boolean {
|
|
1288
|
+
const forward = event.key === "ArrowRight";
|
|
1289
|
+
if (!this.word || (!forward && event.key !== "ArrowLeft")) return false;
|
|
1290
|
+
if (event.metaKey || event.ctrlKey || event.altKey) return false;
|
|
1291
|
+
const { selection, doc } = this.state;
|
|
1292
|
+
if (!(selection instanceof TextSelection) || (!event.shiftKey && !selection.empty))
|
|
1293
|
+
return false;
|
|
1294
|
+
const { $head } = selection;
|
|
1295
|
+
if ($head.depth !== 1 || $head.parentOffset !== (forward ? $head.parent.content.size : 0))
|
|
1296
|
+
return false;
|
|
1297
|
+
const numbered = $head.index(0) + (forward ? 1 : 0);
|
|
1298
|
+
const other = doc.maybeChild($head.index(0) + (forward ? 1 : -1));
|
|
1299
|
+
if (!other?.isTextblock || !this.word.hasMarker(numbered)) return false;
|
|
1300
|
+
const head = forward ? $head.after() + 1 : $head.before() - 1;
|
|
1301
|
+
const anchor = event.shiftKey ? selection.anchor : head;
|
|
1302
|
+
this.dispatch(this.state.tr.setSelection(TextSelection.create(doc, anchor, head)));
|
|
1303
|
+
return true;
|
|
1304
|
+
}
|
|
1305
|
+
|
|
907
1306
|
/** Safety net: a DOM change that was not prevented is read back into the model. */
|
|
908
1307
|
private onInput = (): void => {
|
|
909
1308
|
if (!this.composing) this.flush();
|
|
@@ -988,13 +1387,20 @@ export class EditorView {
|
|
|
988
1387
|
if (node === sel?.focusNode) caret = domText.length + sel.focusOffset;
|
|
989
1388
|
domText += (node as Text).data;
|
|
990
1389
|
} else if (hardBreaks.has(node)) domText += HARD_BREAK;
|
|
1390
|
+
// Shown but not document text (a list number, opaque content): never read back.
|
|
1391
|
+
else if ((node as Element).getAttribute?.("contenteditable") === "false") return;
|
|
991
1392
|
else node.childNodes.forEach(walk);
|
|
992
1393
|
};
|
|
993
1394
|
let first = true;
|
|
994
|
-
// Block-level nodes in order:
|
|
995
|
-
const blockLevel = [
|
|
996
|
-
|
|
997
|
-
|
|
1395
|
+
// Block-level nodes in order: inside pages and containers, and anything else in the host.
|
|
1396
|
+
const blockLevel: Node[] = [];
|
|
1397
|
+
const collect = (parent: Node) => {
|
|
1398
|
+
for (const child of parent.childNodes) {
|
|
1399
|
+
if (this.holdsBlocks(child)) collect(child);
|
|
1400
|
+
else blockLevel.push(child);
|
|
1401
|
+
}
|
|
1402
|
+
};
|
|
1403
|
+
collect(this.host);
|
|
998
1404
|
let previousOwner: BlockDesc | undefined;
|
|
999
1405
|
for (const child of blockLevel) {
|
|
1000
1406
|
const owner = this.owner.get(child);
|
|
@@ -1010,10 +1416,17 @@ export class EditorView {
|
|
|
1010
1416
|
const { doc } = this.state;
|
|
1011
1417
|
const lineStarts: number[] = [];
|
|
1012
1418
|
let modelText = "";
|
|
1013
|
-
|
|
1419
|
+
this.blocks.forEach(({ node: block }, i) => {
|
|
1014
1420
|
if (i > 0) modelText += BLOCK_SEPARATOR;
|
|
1015
1421
|
lineStarts.push(modelText.length);
|
|
1016
|
-
|
|
1422
|
+
block.forEach((child) => {
|
|
1423
|
+
// A tab is text in the DOM (a "\t" text node).
|
|
1424
|
+
modelText += child.isText
|
|
1425
|
+
? (child.text ?? "")
|
|
1426
|
+
: child.type.name === "tab"
|
|
1427
|
+
? "\t"
|
|
1428
|
+
: HARD_BREAK;
|
|
1429
|
+
});
|
|
1017
1430
|
});
|
|
1018
1431
|
/** Model position of an offset in `modelText`. */
|
|
1019
1432
|
const posAt = (offset: number): number => {
|
|
@@ -1089,11 +1502,13 @@ export class EditorView {
|
|
|
1089
1502
|
const { selection, doc } = this.state;
|
|
1090
1503
|
if (selection.empty || !event.clipboardData) return;
|
|
1091
1504
|
event.preventDefault();
|
|
1505
|
+
const context = wordContext(this.state) as WordContext;
|
|
1092
1506
|
const { text, html, json } = serializeRange(
|
|
1093
1507
|
doc,
|
|
1094
1508
|
selection.from,
|
|
1095
1509
|
selection.to,
|
|
1096
1510
|
this.host.ownerDocument,
|
|
1511
|
+
{ context, ratio: this.lineRatio },
|
|
1097
1512
|
);
|
|
1098
1513
|
event.clipboardData.setData("text/plain", text);
|
|
1099
1514
|
event.clipboardData.setData("text/html", html);
|
|
@@ -1118,6 +1533,7 @@ export class EditorView {
|
|
|
1118
1533
|
const data = event.clipboardData;
|
|
1119
1534
|
this.paste(
|
|
1120
1535
|
plain ? { getData: (type) => (type === "text/plain" ? data.getData(type) : "") } : data,
|
|
1536
|
+
!plain,
|
|
1121
1537
|
);
|
|
1122
1538
|
};
|
|
1123
1539
|
|
|
@@ -1126,31 +1542,75 @@ export class EditorView {
|
|
|
1126
1542
|
* paste starts a new undo step (typing right after it joins that step). Closed content (whole
|
|
1127
1543
|
* blocks) goes into an empty textblock whole, so a pasted heading stays a heading; open
|
|
1128
1544
|
* content, and anything pasted into a non-empty block, joins the text around the selection,
|
|
1129
|
-
* so the target block keeps its type.
|
|
1545
|
+
* so the target block keeps its type. Plain text that is one URL becomes a link (`autolink`,
|
|
1546
|
+
* not for paste as plain text): over selected text inside one block, that text is linked;
|
|
1547
|
+
* otherwise the URL is inserted as linked text. Returns whether anything was inserted.
|
|
1130
1548
|
*/
|
|
1131
|
-
paste(data: Pick<DataTransfer, "getData"
|
|
1549
|
+
paste(data: Pick<DataTransfer, "getData">, autolink = true): boolean {
|
|
1132
1550
|
const { state } = this;
|
|
1133
|
-
const
|
|
1134
|
-
const
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1551
|
+
const text = autolink && !data.getData(FOLI_MIME) ? data.getData("text/plain") : "";
|
|
1552
|
+
const href = text ? autolinkHref(text) : null;
|
|
1553
|
+
if (href) {
|
|
1554
|
+
const { selection } = state;
|
|
1555
|
+
const inside =
|
|
1556
|
+
!selection.empty &&
|
|
1557
|
+
selection.$from.sameParent(selection.$to) &&
|
|
1558
|
+
selection.$from.parent.inlineContent;
|
|
1559
|
+
let applied = false;
|
|
1560
|
+
const command = inside ? setLink(href) : insertLink(href, text.trim());
|
|
1561
|
+
const dispatch = (tr: Transaction) => {
|
|
1562
|
+
applied = this.dispatch(tr.setMeta(closeHistory, true));
|
|
1563
|
+
};
|
|
1564
|
+
if (command(state, dispatch)) return applied;
|
|
1565
|
+
}
|
|
1566
|
+
const marks = typedMarks(state.storedMarks ?? marksAt(state.selection.$from)).map((m) =>
|
|
1567
|
+
m.toJSON(),
|
|
1568
|
+
);
|
|
1569
|
+
// The document takes the styles, lists and comments the content needs (HTML is read as
|
|
1570
|
+
// schema v2 content, then migrated).
|
|
1571
|
+
const pasted = docFromClipboard(data, marks, (found) =>
|
|
1572
|
+
found.html
|
|
1573
|
+
? fitPastedV2(state, parseContentV2(found.content))
|
|
1574
|
+
: fitPaste(state, found.content, found.word),
|
|
1575
|
+
);
|
|
1576
|
+
// One empty paragraph: nothing to paste (a lone opaque block is something).
|
|
1577
|
+
const only = pasted?.doc.childCount === 1 ? pasted.doc.firstChild : null;
|
|
1578
|
+
if (!pasted || (only?.isTextblock && !only.content.size)) return false;
|
|
1579
|
+
const { doc, open, envelope } = pasted;
|
|
1580
|
+
// As a slice (text joins the text around the caret); where that cannot be (an opaque block
|
|
1581
|
+
// at an end), as whole blocks at the caret.
|
|
1582
|
+
const asSlice = (tr: Transaction) => {
|
|
1140
1583
|
const $pos = tr.selection.$from;
|
|
1141
1584
|
if (!open && $pos.depth === 1 && $pos.parent.isTextblock && !$pos.parent.content.size) {
|
|
1142
1585
|
const from = $pos.before();
|
|
1143
1586
|
tr.replace(from, $pos.after(), new Slice(doc.content, 0, 0));
|
|
1144
1587
|
tr.setSelection(Selection.near(tr.doc.resolve(from + doc.content.size), -1));
|
|
1145
1588
|
} else {
|
|
1589
|
+
// A slice opens into its first and last blocks: an opaque block there has no inside.
|
|
1590
|
+
if (doc.firstChild?.isLeaf || doc.lastChild?.isLeaf) {
|
|
1591
|
+
throw new TransformError("An opaque block at an end is pasted as a block");
|
|
1592
|
+
}
|
|
1146
1593
|
tr.replaceSelection(doc.slice(1, doc.content.size - 1));
|
|
1147
1594
|
}
|
|
1595
|
+
};
|
|
1596
|
+
const asBlocks = (tr: Transaction) => {
|
|
1597
|
+
if (open) throw new TransformError("Open content is pasted as a slice");
|
|
1598
|
+
insertBlocks(tr, doc);
|
|
1599
|
+
};
|
|
1600
|
+
const strategies = [asSlice, asBlocks];
|
|
1601
|
+
for (const strategy of strategies) {
|
|
1602
|
+
const tr = state.tr;
|
|
1603
|
+
try {
|
|
1604
|
+
tr.deleteSelection();
|
|
1605
|
+
strategy(tr);
|
|
1606
|
+
if (envelope && envelope !== wordContext(state)?.envelope) withEnvelope(tr, envelope);
|
|
1607
|
+
} catch (error) {
|
|
1608
|
+
if (error instanceof TransformError) continue;
|
|
1609
|
+
throw error;
|
|
1610
|
+
}
|
|
1148
1611
|
return this.dispatch(tr.setMeta(closeHistory, true));
|
|
1149
|
-
} catch (error) {
|
|
1150
|
-
// shortcut: content the schema cannot fit at the selection is not pasted; general fitting comes with nested blocks (Phase 7).
|
|
1151
|
-
if (error instanceof TransformError) return false;
|
|
1152
|
-
throw error;
|
|
1153
1612
|
}
|
|
1613
|
+
return false;
|
|
1154
1614
|
}
|
|
1155
1615
|
|
|
1156
1616
|
private onSelectionChange = (): void => {
|
|
@@ -1158,6 +1618,26 @@ export class EditorView {
|
|
|
1158
1618
|
};
|
|
1159
1619
|
}
|
|
1160
1620
|
|
|
1621
|
+
/**
|
|
1622
|
+
* Pastes whole blocks at the caret (after deleting the selection): before or after the caret's
|
|
1623
|
+
* paragraph, else between its two halves (an empty one is replaced as a slice first).
|
|
1624
|
+
*/
|
|
1625
|
+
function insertBlocks(tr: Transaction, doc: ModelNode): void {
|
|
1626
|
+
const $pos = tr.selection.$from;
|
|
1627
|
+
if (!$pos.parent.isTextblock) throw new TransformError("No textblock at the caret");
|
|
1628
|
+
const atStart = $pos.parentOffset === 0;
|
|
1629
|
+
const atEnd = $pos.parentOffset === $pos.parent.content.size;
|
|
1630
|
+
let at: number;
|
|
1631
|
+
if (atStart) at = $pos.before();
|
|
1632
|
+
else if (atEnd) at = $pos.after();
|
|
1633
|
+
else {
|
|
1634
|
+
tr.split($pos.pos);
|
|
1635
|
+
at = $pos.pos + 1;
|
|
1636
|
+
}
|
|
1637
|
+
tr.insert(at, doc.content);
|
|
1638
|
+
tr.setSelection(Selection.near(tr.doc.resolve(at + doc.content.size), -1));
|
|
1639
|
+
}
|
|
1640
|
+
|
|
1161
1641
|
/**
|
|
1162
1642
|
* Client rects are scaled by CSS `zoom` or a `transform` on an ancestor, while margins and the
|
|
1163
1643
|
* page geometry are in layout px: the ratio between the two for a page. From the page's width
|
|
@@ -1199,34 +1679,84 @@ function measureBox(desc: BlockDesc, scale: number): Measure {
|
|
|
1199
1679
|
}
|
|
1200
1680
|
|
|
1201
1681
|
/**
|
|
1202
|
-
* What the flow needs of a block: its measurement, its lines (on demand),
|
|
1203
|
-
*
|
|
1682
|
+
* What the flow needs of a block: its measurement, its lines (on demand), Word's spacing before
|
|
1683
|
+
* and after (next to its neighbours), keep with next, keep lines together (not for a block
|
|
1684
|
+
* taller than a page, or one with a page break in it), page breaks ending their line's page,
|
|
1685
|
+
* and a page break before the next block ending this one's page.
|
|
1204
1686
|
*/
|
|
1205
|
-
function
|
|
1687
|
+
function wordBox(
|
|
1688
|
+
blocks: readonly WordBlockDesc[],
|
|
1689
|
+
i: number,
|
|
1690
|
+
measure: Measure,
|
|
1691
|
+
lines: () => Lines,
|
|
1692
|
+
layout: BlockLayout,
|
|
1693
|
+
): BlockBox {
|
|
1694
|
+
const pageHeight = contentHeight(layout.sheet.settings);
|
|
1695
|
+
const desc = blocks[i] as WordBlockDesc;
|
|
1696
|
+
const next = blocks[i + 1];
|
|
1697
|
+
const { before, after } = spacing(blocks[i - 1]?.word, desc.word, next?.word);
|
|
1698
|
+
const pageBreaks = desc.leaves.filter(
|
|
1699
|
+
(leaf) =>
|
|
1700
|
+
leaf.dom.nodeType !== Node.TEXT_NODE && (leaf.dom as Element).hasAttribute(PAGE_BREAK_ATTR),
|
|
1701
|
+
);
|
|
1702
|
+
const hasBreak = pageBreaks.length > 0;
|
|
1703
|
+
const kept = desc.word.keepLines && !hasBreak && measure.height <= pageHeight;
|
|
1704
|
+
const bottoms = () => (kept ? [measure.height] : lines().bottoms);
|
|
1206
1705
|
return {
|
|
1207
1706
|
height: measure.height,
|
|
1208
|
-
marginTop:
|
|
1209
|
-
marginBottom:
|
|
1210
|
-
|
|
1211
|
-
|
|
1707
|
+
marginTop: before,
|
|
1708
|
+
marginBottom: after,
|
|
1709
|
+
keepWithNext: desc.word.keepNext,
|
|
1710
|
+
pageHeight,
|
|
1711
|
+
lines: bottoms,
|
|
1212
1712
|
breaksAfter: () => {
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
(leaf.dom as Element).hasAttribute(PAGE_BREAK_ATTR),
|
|
1217
|
-
);
|
|
1218
|
-
if (!breaks.length) return [];
|
|
1219
|
-
const { starts } = lines();
|
|
1220
|
-
// The line holding the break: the last one starting at or before it.
|
|
1221
|
-
return breaks.map((leaf) => {
|
|
1713
|
+
// The line holding each break: the last one starting at or before it.
|
|
1714
|
+
const breaks = pageBreaks.map((leaf) => {
|
|
1715
|
+
const { starts } = lines();
|
|
1222
1716
|
let line = starts.length - 1;
|
|
1223
1717
|
while (line > 0 && (starts[line] as number) > leaf.offset) line--;
|
|
1224
1718
|
return line;
|
|
1225
1719
|
});
|
|
1720
|
+
if (next?.word.pageBreakBefore || layout.breakAfter) breaks.push(bottoms().length - 1);
|
|
1721
|
+
return breaks;
|
|
1226
1722
|
},
|
|
1227
1723
|
};
|
|
1228
1724
|
}
|
|
1229
1725
|
|
|
1726
|
+
/**
|
|
1727
|
+
* Writes Word's space above each v3 block as its top margin: the space after the block above
|
|
1728
|
+
* and the space before this one, on the same page; at the top of a page only its space before,
|
|
1729
|
+
* and only on the first page or after an explicit break (a page the text flowed onto drops it).
|
|
1730
|
+
* Written only when it changes.
|
|
1731
|
+
*/
|
|
1732
|
+
function applyGaps(
|
|
1733
|
+
blocks: readonly BlockDesc[],
|
|
1734
|
+
pages: readonly (readonly number[])[],
|
|
1735
|
+
boxes: readonly BlockBox[],
|
|
1736
|
+
): void {
|
|
1737
|
+
blocks.forEach((desc, i) => {
|
|
1738
|
+
const page = pages[i]?.[0] ?? 0;
|
|
1739
|
+
const above = pages[i - 1];
|
|
1740
|
+
const box = boxes[i] as BlockBox;
|
|
1741
|
+
let gap: number;
|
|
1742
|
+
if (above && above[above.length - 1] === page)
|
|
1743
|
+
gap = (boxes[i - 1] as BlockBox).marginBottom + box.marginTop;
|
|
1744
|
+
else {
|
|
1745
|
+
const prior = boxes[i - 1];
|
|
1746
|
+
const breaks = prior?.breaksAfter?.() ?? [];
|
|
1747
|
+
const forced =
|
|
1748
|
+
page === 0 || (!!prior && breaks.length > 0 && breaks.includes(prior.lines().length - 1));
|
|
1749
|
+
gap = forced ? box.marginTop : 0;
|
|
1750
|
+
}
|
|
1751
|
+
if (gaps.get(desc.dom) === gap) return;
|
|
1752
|
+
gaps.set(desc.dom, gap);
|
|
1753
|
+
desc.dom.style.marginTop = `${gap}px`;
|
|
1754
|
+
});
|
|
1755
|
+
}
|
|
1756
|
+
|
|
1757
|
+
/** The gap `applyGaps` last wrote on each block element (the style serializes it rounded). */
|
|
1758
|
+
const gaps = new WeakMap<HTMLElement, number>();
|
|
1759
|
+
|
|
1230
1760
|
/**
|
|
1231
1761
|
* What the next flow can reuse of the last one: the unchanged blocks before the first changed
|
|
1232
1762
|
* (or stale) one, and the unchanged tail after the last.
|
|
@@ -1235,6 +1765,7 @@ function reuseOf(
|
|
|
1235
1765
|
last: { nodes: readonly ModelNode[]; result: FlowResult },
|
|
1236
1766
|
nodes: readonly ModelNode[],
|
|
1237
1767
|
stale: ReadonlySet<ModelNode>,
|
|
1768
|
+
margin = 0,
|
|
1238
1769
|
): Reuse {
|
|
1239
1770
|
const before = last.nodes;
|
|
1240
1771
|
const same = (a: ModelNode | undefined, b: ModelNode | undefined) =>
|
|
@@ -1248,6 +1779,9 @@ function reuseOf(
|
|
|
1248
1779
|
same(nodes[nodes.length - 1 - tail], before[before.length - 1 - tail])
|
|
1249
1780
|
)
|
|
1250
1781
|
tail++;
|
|
1782
|
+
// Blocks whose box depends on a changed neighbour are not reused either.
|
|
1783
|
+
from = Math.max(0, from - margin);
|
|
1784
|
+
tail = Math.max(0, tail - margin);
|
|
1251
1785
|
const delta = nodes.length - before.length;
|
|
1252
1786
|
return {
|
|
1253
1787
|
previous: last.result,
|
|
@@ -1451,9 +1985,10 @@ const IS_MAC = typeof navigator !== "undefined" && /Mac|iP(hone|[oa]d)/.test(nav
|
|
|
1451
1985
|
export function keyName(event: KeyboardEvent): string {
|
|
1452
1986
|
let key = event.key.length === 1 ? event.key.toLowerCase() : event.key;
|
|
1453
1987
|
const mod = IS_MAC ? event.metaKey : event.ctrlKey;
|
|
1454
|
-
// Non-Latin layouts: Ctrl+B still means "b" (use the physical key when the character is not a-z)
|
|
1455
|
-
|
|
1456
|
-
|
|
1988
|
+
// Non-Latin layouts: Ctrl+B still means "b" (use the physical key when the character is not a-z);
|
|
1989
|
+
// with Shift, Ctrl+Shift+7 still means "7" (the character is "&" or "/" depending on layout).
|
|
1990
|
+
if (mod && !/^[a-z0-9]$/.test(key) && /^(Key[A-Z]|Digit[0-9])$/.test(event.code))
|
|
1991
|
+
key = event.code.slice(-1).toLowerCase();
|
|
1457
1992
|
let name = key;
|
|
1458
1993
|
if (event.shiftKey && key !== "Shift") name = `Shift-${name}`;
|
|
1459
1994
|
if (event.altKey) name = `Alt-${name}`;
|