@folijs/view 0.2.0 → 0.4.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 +55 -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 +39 -16
- package/dist/view.d.ts.map +1 -1
- package/dist/view.js +380 -278
- package/dist/view.js.map +1 -1
- package/dist/word/blocks.d.ts +84 -0
- package/dist/word/blocks.d.ts.map +1 -0
- package/dist/word/blocks.js +756 -0
- package/dist/word/blocks.js.map +1 -0
- package/dist/word/edit.d.ts +63 -0
- package/dist/word/edit.d.ts.map +1 -0
- package/dist/word/edit.js +318 -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 +438 -286
- package/src/word/blocks.ts +826 -0
- package/src/word/edit.ts +387 -0
- package/src/word/pages.ts +372 -0
package/src/view.ts
CHANGED
|
@@ -10,22 +10,35 @@ import {
|
|
|
10
10
|
deleteSelection,
|
|
11
11
|
type Editor,
|
|
12
12
|
type EditorState,
|
|
13
|
+
fitPaste,
|
|
14
|
+
fitPastedV2,
|
|
13
15
|
insertHardBreak,
|
|
14
16
|
insertLink,
|
|
15
17
|
insertText,
|
|
18
|
+
isSafeHref,
|
|
16
19
|
type Node as ModelNode,
|
|
17
20
|
marksAt,
|
|
18
21
|
NodeSelection,
|
|
22
|
+
parseContentV2,
|
|
23
|
+
type ReviewView,
|
|
19
24
|
redo,
|
|
25
|
+
type SectionJSON,
|
|
20
26
|
Selection,
|
|
21
27
|
Slice,
|
|
28
|
+
sectionSettings,
|
|
29
|
+
setComments,
|
|
22
30
|
setLink,
|
|
23
31
|
splitBlock,
|
|
24
32
|
TextSelection,
|
|
25
33
|
type Transaction,
|
|
26
34
|
TransformError,
|
|
27
35
|
toggleMark,
|
|
36
|
+
trackState,
|
|
37
|
+
typedMarks,
|
|
28
38
|
undo,
|
|
39
|
+
type WordContext,
|
|
40
|
+
withEnvelope,
|
|
41
|
+
wordContext,
|
|
29
42
|
} from "@folijs/core";
|
|
30
43
|
import { docFromClipboard, FOLI_MIME, serializeRange } from "./clipboard.js";
|
|
31
44
|
import {
|
|
@@ -33,29 +46,18 @@ import {
|
|
|
33
46
|
contentHeight,
|
|
34
47
|
DEFAULT_RULES,
|
|
35
48
|
type FlowResult,
|
|
49
|
+
type FlowRules,
|
|
36
50
|
flowFrom,
|
|
37
51
|
PX_PER_MM,
|
|
38
52
|
type Reuse,
|
|
39
53
|
} from "./layout/flow.js";
|
|
40
|
-
import {
|
|
41
|
-
type BlockDesc,
|
|
42
|
-
createWrapper,
|
|
43
|
-
isWrapper,
|
|
44
|
-
isWrapperFor,
|
|
45
|
-
PAGE_BREAK_ATTR,
|
|
46
|
-
renderBlock,
|
|
47
|
-
} from "./render.js";
|
|
54
|
+
import { type BlockDesc, PAGE_BREAK_ATTR } from "./render.js";
|
|
48
55
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
*/
|
|
53
|
-
|
|
54
|
-
readonly node: ModelNode;
|
|
55
|
-
readonly pos: number;
|
|
56
|
-
readonly index: number;
|
|
57
|
-
readonly firstBlock: number;
|
|
58
|
-
}
|
|
56
|
+
import { spacing, type WordBlockDesc, WordRenderer } from "./word/edit.js";
|
|
57
|
+
import { measureLineRatio } from "./word/pages.js";
|
|
58
|
+
|
|
59
|
+
/** Word's paragraph spacing for a schema v3 document (`flow`'s `word` spacing). */
|
|
60
|
+
const WORD_RULES: FlowRules = { ...DEFAULT_RULES, spacing: "word" };
|
|
59
61
|
|
|
60
62
|
/** Class of the element of a node-selected block (a rule). */
|
|
61
63
|
const SELECTED_CLASS = "foli-selected-node";
|
|
@@ -101,19 +103,129 @@ let viewCount = 0;
|
|
|
101
103
|
|
|
102
104
|
/**
|
|
103
105
|
* Print rules for the page settings of the view whose host has `VIEW_ATTR` = `id`: one sheet of
|
|
104
|
-
*
|
|
105
|
-
* exactly as tall as a sheet (the flow keeps content inside it; the exact height absorbs its
|
|
106
|
+
* its section's size per page (the last section's where named pages are not supported), without
|
|
107
|
+
* 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
|
|
106
108
|
* half-pixel slack), with no gap after it. Content taller than a page (a line taller than a
|
|
107
109
|
* page) is not cut: it prints over the next sheet.
|
|
108
110
|
* 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.
|
|
109
111
|
*/
|
|
110
|
-
function printStyles(
|
|
111
|
-
const { width, height } = pageSize;
|
|
112
|
+
function printStyles(settings: DocumentSettings, sheets: readonly Sheet[], id: string): string {
|
|
113
|
+
const { width, height } = settings.pageSize;
|
|
114
|
+
const last = sheetOf(settings, 0).css;
|
|
112
115
|
const pages = `[${VIEW_ATTR}="${id}"]>.foli-page`;
|
|
116
|
+
// Sections with other page setups: a named page each (engines without named pages print
|
|
117
|
+
// every sheet at the last section's size).
|
|
118
|
+
const named = sheets.every((sheet) => sheet.css === last)
|
|
119
|
+
? ""
|
|
120
|
+
: sheets
|
|
121
|
+
.map(({ settings: { pageSize: size }, index }) => {
|
|
122
|
+
const name = `foli-${id}-${index}`;
|
|
123
|
+
return (
|
|
124
|
+
`@page ${name}{size:${size.width}mm ${size.height}mm;margin:0}` +
|
|
125
|
+
`${pages}[${SHEET_ATTR}="${index}"]{page:${name};height:${size.height}mm!important}`
|
|
126
|
+
);
|
|
127
|
+
})
|
|
128
|
+
.join("");
|
|
113
129
|
return (
|
|
114
130
|
`@media print{@page{size:${width}mm ${height}mm;margin:0}` +
|
|
115
131
|
`${pages}{height:${height}mm!important;margin-block:0!important;` +
|
|
116
|
-
`break-after:page;break-inside:avoid}${pages}:last-child{break-after:auto}}`
|
|
132
|
+
`break-after:page;break-inside:avoid}${pages}:last-child{break-after:auto}${named}}`
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** Whether `node` is inside an element of `stop` that is not displayed (hidden deleted text). */
|
|
137
|
+
function hiddenIn(node: Node, stop: Node): boolean {
|
|
138
|
+
for (let el = node.parentElement; el && el !== stop; el = el.parentElement)
|
|
139
|
+
if (el.style.display === "none") return true;
|
|
140
|
+
return false;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** The attribute naming a page's page setup (its index among the document's) for print. */
|
|
144
|
+
const SHEET_ATTR = "data-foli-sheet";
|
|
145
|
+
|
|
146
|
+
/** A page setup: its settings (mm), the page style it gives, its index among the document's. */
|
|
147
|
+
interface Sheet {
|
|
148
|
+
readonly settings: DocumentSettings;
|
|
149
|
+
readonly css: string;
|
|
150
|
+
readonly index: number;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function sheetOf(settings: DocumentSettings, index: number): Sheet {
|
|
154
|
+
const { pageSize, margins } = settings;
|
|
155
|
+
const css =
|
|
156
|
+
`box-sizing: border-box; width: ${pageSize.width}mm; min-height: ${pageSize.height}mm; ` +
|
|
157
|
+
`padding: ${margins.top}mm ${margins.right}mm ${margins.bottom}mm ${margins.left}mm;`;
|
|
158
|
+
return { settings, css, index };
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** The style each page element was last given (its `cssText` reads back normalized). */
|
|
162
|
+
const pageSheets = new WeakMap<HTMLElement, string>();
|
|
163
|
+
|
|
164
|
+
/** A block's section: its page setup, and whether the next block starts a new page. */
|
|
165
|
+
interface BlockLayout {
|
|
166
|
+
readonly sheet: Sheet;
|
|
167
|
+
readonly breakAfter: boolean;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* The section of each top-level block (the section a paragraph's `section` ends; the
|
|
172
|
+
* envelope's for the blocks after the last such paragraph). A section starts a new page,
|
|
173
|
+
* unless it is continuous with the same page setup as the one before (shortcut: a continuous
|
|
174
|
+
* section with another page setup starts a new page; Word's even and odd page sections do not
|
|
175
|
+
* add a blank page), as `renderDocument` lays sections out.
|
|
176
|
+
*/
|
|
177
|
+
function sectionLayouts(doc: ModelNode, last: SectionJSON): BlockLayout[] {
|
|
178
|
+
const sheets = new Map<string, Sheet>();
|
|
179
|
+
const bySection = new Map<SectionJSON, Sheet>();
|
|
180
|
+
const sheet = (section: SectionJSON) => {
|
|
181
|
+
let found = bySection.get(section);
|
|
182
|
+
if (!found) {
|
|
183
|
+
const settings = sectionSettings(section);
|
|
184
|
+
const css = sheetOf(settings, 0).css;
|
|
185
|
+
found = sheets.get(css) ?? sheetOf(settings, sheets.size);
|
|
186
|
+
sheets.set(css, found);
|
|
187
|
+
bySection.set(section, found);
|
|
188
|
+
}
|
|
189
|
+
return found;
|
|
190
|
+
};
|
|
191
|
+
const sections: SectionJSON[] = [];
|
|
192
|
+
let section = last;
|
|
193
|
+
for (let i = doc.childCount - 1; i >= 0; i--) {
|
|
194
|
+
const own = doc.child(i).attrs.section as SectionJSON | null | undefined;
|
|
195
|
+
if (own) section = own;
|
|
196
|
+
sections[i] = section;
|
|
197
|
+
}
|
|
198
|
+
// Indexes in document order: the first page setup is 0.
|
|
199
|
+
sections.forEach(sheet);
|
|
200
|
+
return sections.map((own, i) => {
|
|
201
|
+
const next = sections[i + 1];
|
|
202
|
+
// By position, as `renderDocument`: two paragraphs may end sections with one object.
|
|
203
|
+
const ends = !!next && !!doc.child(i).attrs.section;
|
|
204
|
+
const breakAfter = ends && !(next.type === "continuous" && sheet(next).css === sheet(own).css);
|
|
205
|
+
return { sheet: sheet(own), breakAfter };
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Initial pages (before a flow), moved where needed so that each page holds blocks of one
|
|
211
|
+
* page setup: a block is measured in a page of its own section's width.
|
|
212
|
+
*/
|
|
213
|
+
function separateSheets(
|
|
214
|
+
pages: readonly (readonly number[])[],
|
|
215
|
+
layout: readonly BlockLayout[],
|
|
216
|
+
): number[][] {
|
|
217
|
+
const css: string[] = [];
|
|
218
|
+
let floor = 0;
|
|
219
|
+
return pages.map((list, i) =>
|
|
220
|
+
list.map((p) => {
|
|
221
|
+
const want = (layout[i] as BlockLayout).sheet.css;
|
|
222
|
+
let page = Math.max(p, floor);
|
|
223
|
+
// Only the last page used so far has a setup: the next one is free.
|
|
224
|
+
if (css[page] !== undefined && css[page] !== want) page++;
|
|
225
|
+
css[page] ??= want;
|
|
226
|
+
floor = page;
|
|
227
|
+
return page;
|
|
228
|
+
}),
|
|
117
229
|
);
|
|
118
230
|
}
|
|
119
231
|
|
|
@@ -126,21 +238,16 @@ function printStyles({ pageSize }: DocumentSettings, id: string): string {
|
|
|
126
238
|
* view cannot prevent (Android keyboards, extensions). A MutationObserver records those; the view
|
|
127
239
|
* then reads the changed text back into a transaction (`readBack`) and re-renders.
|
|
128
240
|
*
|
|
129
|
-
* Blocks are the
|
|
130
|
-
* containers
|
|
131
|
-
* (ADR-0004).
|
|
241
|
+
* Blocks are the document's paragraphs and opaque blocks (a schema v3 document has no
|
|
242
|
+
* containers: a list is numbered paragraphs).
|
|
132
243
|
*/
|
|
133
244
|
export class EditorView {
|
|
134
245
|
private state: EditorState;
|
|
135
|
-
private blocks:
|
|
246
|
+
private blocks: WordBlockDesc[] = [];
|
|
136
247
|
/** Position before each block, parallel to `blocks`. */
|
|
137
248
|
private starts: number[] = [];
|
|
138
|
-
/** The containers around each block, outermost first, parallel to `blocks`. */
|
|
139
|
-
private chains: (readonly Container[])[] = [];
|
|
140
249
|
/** The index of each block in `blocks`. */
|
|
141
250
|
private indexOf = new Map<BlockDesc, number>();
|
|
142
|
-
/** The container types around each block at its last render (a change means re-measuring). */
|
|
143
|
-
private chainKeys = new WeakMap<ModelNode, string>();
|
|
144
251
|
/** The element that shows a node selection, if any. */
|
|
145
252
|
private selectedDom: HTMLElement | null = null;
|
|
146
253
|
/** The page of each fragment of each block. */
|
|
@@ -149,7 +256,16 @@ export class EditorView {
|
|
|
149
256
|
private owner = new Map<Node, BlockDesc>();
|
|
150
257
|
/** The page elements, in order; the host's only children. */
|
|
151
258
|
private pages: HTMLElement[] = [];
|
|
152
|
-
|
|
259
|
+
/** The page setup of each page (its section's), parallel to `pages`. */
|
|
260
|
+
private sheets: Sheet[] = [];
|
|
261
|
+
/** The page setups the print rules were written for. */
|
|
262
|
+
private printKey = "";
|
|
263
|
+
/**
|
|
264
|
+
* Each block's page setup and whether a section break follows it, as last rendered: a block
|
|
265
|
+
* whose layout changed (a section break inserted or removed around it) is measured and
|
|
266
|
+
* flowed again.
|
|
267
|
+
*/
|
|
268
|
+
private layoutKeys = new WeakMap<ModelNode, string>();
|
|
153
269
|
/** Marks the host for this view's print rules (`printStyles`). */
|
|
154
270
|
private readonly id = String(++viewCount);
|
|
155
271
|
/**
|
|
@@ -164,7 +280,7 @@ export class EditorView {
|
|
|
164
280
|
* Measurements by model node: a node's rendered height, margins and lines only depend on
|
|
165
281
|
* its content (unchanged node, same identity), the page width and the CSS. Cleared when the
|
|
166
282
|
* page settings or the fonts change; single entries go stale when a block's size changes
|
|
167
|
-
* without an edit (`ResizeObserver`: CSS, fonts
|
|
283
|
+
* without an edit (`ResizeObserver`: CSS, fonts).
|
|
168
284
|
*/
|
|
169
285
|
private measures = new WeakMap<ModelNode, Measure>();
|
|
170
286
|
private readonly stale = new Set<ModelNode>();
|
|
@@ -189,6 +305,8 @@ export class EditorView {
|
|
|
189
305
|
private readonly observer: MutationObserver;
|
|
190
306
|
private readonly unsubscribe: () => void;
|
|
191
307
|
private editable = true;
|
|
308
|
+
/** Escape was the last key: Tab now leaves the editor. */
|
|
309
|
+
private tabLeaves = false;
|
|
192
310
|
private placeholder = "";
|
|
193
311
|
/** The block that shows the placeholder, if any. */
|
|
194
312
|
private placeholderDom: HTMLElement | null = null;
|
|
@@ -196,6 +314,14 @@ export class EditorView {
|
|
|
196
314
|
private pointerFocus = false;
|
|
197
315
|
/** Whether the view added the host's tabindex (a read-only host must stay focusable). */
|
|
198
316
|
private readonly addedTabIndex: boolean;
|
|
317
|
+
/** Renders the document's blocks; made by the first render. */
|
|
318
|
+
private word: WordRenderer | null = null;
|
|
319
|
+
/** How tracked changes show. */
|
|
320
|
+
private review: ReviewView = "markup";
|
|
321
|
+
/** Fonts' normal line height ratios, measured once per family (Word's single spacing). */
|
|
322
|
+
private readonly ratios = new Map<string, number>();
|
|
323
|
+
/** Web fonts loaded since the v3 blocks were drawn: the next render draws them again. */
|
|
324
|
+
private fontsChanged = false;
|
|
199
325
|
|
|
200
326
|
constructor(
|
|
201
327
|
readonly host: HTMLElement,
|
|
@@ -212,6 +338,7 @@ export class EditorView {
|
|
|
212
338
|
host.style.whiteSpace = "pre-wrap";
|
|
213
339
|
host.style.overflowWrap = "break-word";
|
|
214
340
|
host.replaceChildren();
|
|
341
|
+
this.review = trackState(this.state)?.view ?? "markup";
|
|
215
342
|
this.resizeObserver =
|
|
216
343
|
typeof ResizeObserver === "undefined" ? null : new ResizeObserver(this.onResize);
|
|
217
344
|
this.observer = new MutationObserver(this.onMutations);
|
|
@@ -293,6 +420,7 @@ export class EditorView {
|
|
|
293
420
|
this.editable = options.editable;
|
|
294
421
|
}
|
|
295
422
|
if (options.placeholder !== undefined) this.placeholder = options.placeholder;
|
|
423
|
+
|
|
296
424
|
// Read-only: no caret and no browser editing; the text stays selectable and copyable.
|
|
297
425
|
host.setAttribute("contenteditable", String(this.editable));
|
|
298
426
|
if (this.editable) host.removeAttribute("aria-readonly");
|
|
@@ -323,7 +451,14 @@ export class EditorView {
|
|
|
323
451
|
// ---------- rendering ----------
|
|
324
452
|
|
|
325
453
|
private update(state: EditorState): void {
|
|
326
|
-
const
|
|
454
|
+
const review = trackState(state)?.view ?? "markup";
|
|
455
|
+
// Another review view (`setReviewView`): every block drawn again, by a new renderer.
|
|
456
|
+
const reviewChanged = review !== this.review;
|
|
457
|
+
if (reviewChanged) {
|
|
458
|
+
this.review = review;
|
|
459
|
+
this.word = null;
|
|
460
|
+
}
|
|
461
|
+
const docChanged = state.doc !== this.state.doc || reviewChanged;
|
|
327
462
|
const selectionChanged = !state.selection.eq(this.state.selection);
|
|
328
463
|
this.state = state;
|
|
329
464
|
if (docChanged) this.render();
|
|
@@ -370,6 +505,34 @@ export class EditorView {
|
|
|
370
505
|
private render(): void {
|
|
371
506
|
const { doc } = this.state;
|
|
372
507
|
const ownerDocument = this.host.ownerDocument;
|
|
508
|
+
// A new document (envelope) gets its renderer; nothing rendered before is reused then. An
|
|
509
|
+
// editor's state always has one (`createEditor`).
|
|
510
|
+
const context = wordContext(this.state) as WordContext;
|
|
511
|
+
// Fonts loaded: a new renderer (new line heights), but not while a composition runs in a
|
|
512
|
+
// block it would redraw.
|
|
513
|
+
const renewed =
|
|
514
|
+
context !== (this.word?.context ?? null) || (this.fontsChanged && !this.composing);
|
|
515
|
+
if (renewed) this.fontsChanged = false;
|
|
516
|
+
let word = this.word as WordRenderer;
|
|
517
|
+
if (renewed) {
|
|
518
|
+
word = this.word = new WordRenderer(
|
|
519
|
+
context,
|
|
520
|
+
ownerDocument,
|
|
521
|
+
this.lineRatio,
|
|
522
|
+
false,
|
|
523
|
+
this.review,
|
|
524
|
+
);
|
|
525
|
+
this.invalidateLayout();
|
|
526
|
+
}
|
|
527
|
+
word.prepare(doc);
|
|
528
|
+
const layout = sectionLayouts(doc, context.envelope.section);
|
|
529
|
+
doc.forEach((node, _, i) => {
|
|
530
|
+
const { sheet, breakAfter } = layout[i] as BlockLayout;
|
|
531
|
+
const key = `${sheet.css}${breakAfter ? "|break" : ""}`;
|
|
532
|
+
const before = this.layoutKeys.get(node);
|
|
533
|
+
if (before !== undefined && before !== key) this.stale.add(node);
|
|
534
|
+
this.layoutKeys.set(node, key);
|
|
535
|
+
});
|
|
373
536
|
const previous = new Map(
|
|
374
537
|
this.blocks.filter((desc) => !this.dirty.has(desc)).map((desc) => [desc.node, desc]),
|
|
375
538
|
);
|
|
@@ -380,63 +543,38 @@ export class EditorView {
|
|
|
380
543
|
this.linesStale = false;
|
|
381
544
|
this.flowed = null;
|
|
382
545
|
}
|
|
383
|
-
let blocks:
|
|
546
|
+
let blocks: WordBlockDesc[] = [];
|
|
384
547
|
const starts: number[] = [];
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
let chainKey = "";
|
|
392
|
-
const changed = () => {
|
|
393
|
-
chain = open.slice();
|
|
394
|
-
chainKey = open.map((container) => container.node.type.name).join(">");
|
|
395
|
-
};
|
|
396
|
-
doc.descendants((node, pos, _parent, index) => {
|
|
397
|
-
let popped = false;
|
|
398
|
-
while (open.length) {
|
|
399
|
-
const last = open[open.length - 1] as Container;
|
|
400
|
-
if (pos < last.pos + last.node.nodeSize) break;
|
|
401
|
-
open.pop();
|
|
402
|
-
popped = true;
|
|
548
|
+
doc.forEach((node, pos) => {
|
|
549
|
+
let reused = renewed ? undefined : previous.get(node);
|
|
550
|
+
// A block whose list number or comment shading changed (an edit before it) is redrawn.
|
|
551
|
+
if (reused && reused.key !== word.key(blocks.length)) {
|
|
552
|
+
reused = undefined;
|
|
553
|
+
this.stale.add(node);
|
|
403
554
|
}
|
|
404
|
-
if (!node.isTextblock && !node.isLeaf) {
|
|
405
|
-
open.push({ node, pos, index, firstBlock: blocks.length });
|
|
406
|
-
changed();
|
|
407
|
-
return true;
|
|
408
|
-
}
|
|
409
|
-
if (popped) changed();
|
|
410
|
-
// A block moved into or out of a container (wrapped in a list, indented, lifted) keeps its
|
|
411
|
-
// node but not its line width or margins: measure it again, and re-flow from it.
|
|
412
|
-
const before = this.chainKeys.get(node);
|
|
413
|
-
if (before !== undefined && before !== chainKey) this.stale.add(node);
|
|
414
|
-
this.chainKeys.set(node, chainKey);
|
|
415
|
-
let reused = previous.get(node);
|
|
416
555
|
// A split block about to be measured again (fonts, CSS) is rendered whole first: each
|
|
417
556
|
// fragment's end breaks its line, so its lines would depend on where it was split before.
|
|
418
557
|
const remeasure = dropLines || !this.measures.has(node) || this.stale.has(node);
|
|
419
558
|
if (reused && reused.fragments.length > 1 && remeasure && !this.composing) reused = undefined;
|
|
420
559
|
if (reused) previous.delete(node);
|
|
421
|
-
blocks.push(reused ??
|
|
560
|
+
blocks.push(reused ?? word.render(node, blocks.length));
|
|
422
561
|
starts.push(pos);
|
|
423
|
-
chains.push(chain);
|
|
424
|
-
return false;
|
|
425
562
|
});
|
|
426
563
|
for (const stale of previous.values())
|
|
427
564
|
for (const fragment of stale.fragments) fragment.remove();
|
|
428
|
-
this.
|
|
565
|
+
this.applyPrintStyles(layout);
|
|
429
566
|
let last = 0;
|
|
430
567
|
let pages = blocks.map((desc) => {
|
|
431
568
|
const before = previousPages.get(desc);
|
|
432
569
|
return desc.fragments.map((_, f) => (last = Math.max(last, before?.[f] ?? last)));
|
|
433
570
|
});
|
|
434
|
-
|
|
571
|
+
pages = separateSheets(pages, layout);
|
|
572
|
+
this.place(blocks, pages, layout);
|
|
435
573
|
if (!this.composing) {
|
|
436
574
|
// DOM reads only for blocks without a valid measurement (new, changed or stale ones).
|
|
437
575
|
let scale: number | undefined;
|
|
438
576
|
const layoutPx = () => {
|
|
439
|
-
scale ??= layoutScale(this.pages[0] as HTMLElement, this.
|
|
577
|
+
scale ??= layoutScale(this.pages[0] as HTMLElement, (this.sheets[0] as Sheet).settings);
|
|
440
578
|
return scale;
|
|
441
579
|
};
|
|
442
580
|
if (dropLines)
|
|
@@ -445,7 +583,8 @@ export class EditorView {
|
|
|
445
583
|
if (measure) delete measure.lines;
|
|
446
584
|
}
|
|
447
585
|
const nodes = blocks.map((desc) => desc.node);
|
|
448
|
-
|
|
586
|
+
// A block's spacing depends on its neighbours' styles: reuse stops a block earlier.
|
|
587
|
+
const reuse = this.flowed ? reuseOf(this.flowed, nodes, this.stale, 1) : undefined;
|
|
449
588
|
const measureOf = (desc: BlockDesc) => {
|
|
450
589
|
let found = this.measures.get(desc.node);
|
|
451
590
|
// A stale block is measured again once per render.
|
|
@@ -460,8 +599,10 @@ export class EditorView {
|
|
|
460
599
|
found.lines ??= measureLines(desc, layoutPx());
|
|
461
600
|
return found.lines;
|
|
462
601
|
};
|
|
463
|
-
const boxes = blocks.map((desc
|
|
464
|
-
|
|
602
|
+
const boxes = blocks.map((desc, i) =>
|
|
603
|
+
wordBox(blocks, i, measureOf(desc), () => lines(desc), layout[i] as BlockLayout),
|
|
604
|
+
);
|
|
605
|
+
const result = flowFrom(boxes, contentHeight(this.editor.settings), WORD_RULES, reuse);
|
|
465
606
|
this.flowed = { nodes, result };
|
|
466
607
|
this.stale.clear();
|
|
467
608
|
const flowed = result.fragments;
|
|
@@ -475,14 +616,14 @@ export class EditorView {
|
|
|
475
616
|
});
|
|
476
617
|
const same =
|
|
477
618
|
splits.length === desc.splits.length && splits.every((at, k) => at === desc.splits[k]);
|
|
478
|
-
return same ? desc :
|
|
619
|
+
return same ? desc : word.render(desc.node, i, splits);
|
|
479
620
|
});
|
|
480
621
|
pages = flowed.map((fragments) => fragments.map((fragment) => fragment.page));
|
|
481
|
-
|
|
622
|
+
applyGaps(blocks, pages, boxes);
|
|
623
|
+
this.place(blocks, pages, layout);
|
|
482
624
|
}
|
|
483
625
|
this.blocks = blocks;
|
|
484
626
|
this.starts = starts;
|
|
485
|
-
this.chains = chains;
|
|
486
627
|
this.indexOf = new Map(blocks.map((desc, i) => [desc, i]));
|
|
487
628
|
this.fragmentPages = new Map(blocks.map((desc, i) => [desc, pages[i] ?? []]));
|
|
488
629
|
this.owner = new Map(
|
|
@@ -498,111 +639,72 @@ export class EditorView {
|
|
|
498
639
|
if (this.composing) this.pending.push(...records);
|
|
499
640
|
}
|
|
500
641
|
|
|
642
|
+
/** A font's normal line height ratio, measured outside the host (no input to read back). */
|
|
643
|
+
private lineRatio = (family: string): number => {
|
|
644
|
+
let ratio = this.ratios.get(family);
|
|
645
|
+
if (ratio === undefined) {
|
|
646
|
+
const doc = this.host.ownerDocument;
|
|
647
|
+
ratio = measureLineRatio(doc.body ?? doc.documentElement)(family);
|
|
648
|
+
this.ratios.set(family, ratio);
|
|
649
|
+
}
|
|
650
|
+
return ratio;
|
|
651
|
+
};
|
|
652
|
+
|
|
501
653
|
/**
|
|
502
654
|
* Puts each fragment of each block into its page (pages are non-decreasing in document
|
|
503
|
-
* order),
|
|
504
|
-
*
|
|
505
|
-
*
|
|
506
|
-
* page or a container (stray DOM) is removed.
|
|
655
|
+
* order), creating and removing pages as needed, and gives each page the page setup of its
|
|
656
|
+
* blocks' section (one per page: `separateSheets`, section breaks). Anything else in the host
|
|
657
|
+
* or a page (stray DOM) is removed.
|
|
507
658
|
*/
|
|
508
659
|
private place(
|
|
509
660
|
blocks: readonly BlockDesc[],
|
|
510
661
|
pages: readonly (readonly number[])[],
|
|
511
|
-
|
|
662
|
+
layout: readonly BlockLayout[],
|
|
512
663
|
): void {
|
|
513
664
|
const { host } = this;
|
|
514
665
|
const items = blocks.flatMap((desc, i) =>
|
|
515
|
-
desc.fragments.map((dom, f) => ({ dom, page: pages[i]?.[f] ?? 0
|
|
666
|
+
desc.fragments.map((dom, f) => ({ dom, page: pages[i]?.[f] ?? 0 })),
|
|
516
667
|
);
|
|
517
668
|
const count = (items[items.length - 1]?.page ?? 0) + 1;
|
|
669
|
+
const sheets: Sheet[] = [];
|
|
670
|
+
pages.forEach((list, i) => {
|
|
671
|
+
for (const p of list) sheets[p] ??= (layout[i] as BlockLayout).sheet;
|
|
672
|
+
});
|
|
673
|
+
// A page no block starts on (none yet) takes the setup of the page before it.
|
|
674
|
+
for (let p = 0; p < count; p++)
|
|
675
|
+
sheets[p] ??=
|
|
676
|
+
sheets[p - 1] ?? (layout[0] as BlockLayout | undefined)?.sheet ?? this.lastSheet();
|
|
677
|
+
this.sheets = sheets.slice(0, count);
|
|
518
678
|
while (this.pages.length < count) this.pages.push(this.createPage());
|
|
679
|
+
this.sheets.forEach((sheet, p) => {
|
|
680
|
+
const page = this.pages[p] as HTMLElement;
|
|
681
|
+
const key = `${sheet.index} ${sheet.css}`;
|
|
682
|
+
if (pageSheets.get(page) === key) return;
|
|
683
|
+
pageSheets.set(page, key);
|
|
684
|
+
page.style.cssText = sheet.css;
|
|
685
|
+
page.setAttribute(SHEET_ATTR, String(sheet.index));
|
|
686
|
+
});
|
|
519
687
|
// Pages first, in order; anything else in the host ends up after them.
|
|
520
688
|
let cursor = host.firstChild;
|
|
521
689
|
for (const page of this.pages.slice(0, count)) {
|
|
522
690
|
if (page === cursor) cursor = cursor.nextSibling;
|
|
523
691
|
else host.insertBefore(page, cursor);
|
|
524
692
|
}
|
|
525
|
-
const ownerDocument = host.ownerDocument;
|
|
526
|
-
/** Removes what is left after the last placed child (strays, and blocks of later pages). */
|
|
527
|
-
const closeLevel = (level: { at: ChildNode | null }) => {
|
|
528
|
-
let at = level.at;
|
|
529
|
-
while (at) {
|
|
530
|
-
const next: ChildNode | null = at.nextSibling;
|
|
531
|
-
at.remove();
|
|
532
|
-
at = next;
|
|
533
|
-
}
|
|
534
|
-
};
|
|
535
|
-
// Block fragments not placed yet, and their blocks. A container element whose first block is
|
|
536
|
-
// still to be placed, in another container at that level, belongs to that later container: a
|
|
537
|
-
// block before it gets a new element instead of taking it (which would move every later block
|
|
538
|
-
// into the next element along, e.g. when one list item moves to the next page).
|
|
539
|
-
const pending = new Map<Node, number>(items.map((item) => [item.dom, item.block]));
|
|
540
|
-
/** Container elements placed in this pass (never taken twice). */
|
|
541
|
-
const claimed = new Set<Node>();
|
|
542
693
|
let i = 0;
|
|
543
694
|
for (let p = 0; p < count; p++) {
|
|
544
695
|
const page = this.pages[p] as HTMLElement;
|
|
545
|
-
|
|
546
|
-
{ el: page, at: page.firstChild, pos: -1 },
|
|
547
|
-
];
|
|
696
|
+
let at = page.firstChild;
|
|
548
697
|
for (; i < items.length && items[i]?.page === p; i++) {
|
|
549
|
-
const
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
common++;
|
|
559
|
-
while (stack.length > common + 1) closeLevel(stack.pop() as (typeof stack)[number]);
|
|
560
|
-
for (let k = common; k < chain.length; k++) {
|
|
561
|
-
const container = chain[k] as Container;
|
|
562
|
-
const parent = stack[stack.length - 1] as (typeof stack)[number];
|
|
563
|
-
let element: HTMLElement;
|
|
564
|
-
const candidate = parent.at;
|
|
565
|
-
const later = isWrapperFor(candidate, container.node)
|
|
566
|
-
? pending.get(firstBlockIn(candidate) as Node)
|
|
567
|
-
: undefined;
|
|
568
|
-
// The element that holds this block now, at the innermost level: a list item whose
|
|
569
|
-
// block moves to another page takes its element along (as a paragraph does), instead
|
|
570
|
-
// of a new one replacing it on every page an edit shifts.
|
|
571
|
-
const own = k === chain.length - 1 ? item.dom.parentNode : null;
|
|
572
|
-
if (
|
|
573
|
-
isWrapperFor(candidate, container.node) &&
|
|
574
|
-
!claimed.has(candidate) &&
|
|
575
|
-
(later === undefined || chains[later]?.[k]?.pos === container.pos)
|
|
576
|
-
) {
|
|
577
|
-
element = candidate;
|
|
578
|
-
parent.at = element.nextSibling;
|
|
579
|
-
} else if (
|
|
580
|
-
isWrapperFor(own, container.node) &&
|
|
581
|
-
!claimed.has(own) &&
|
|
582
|
-
!own.contains(parent.el)
|
|
583
|
-
) {
|
|
584
|
-
element = own;
|
|
585
|
-
for (const level of stack) if (level.at === element) level.at = element.nextSibling;
|
|
586
|
-
parent.el.insertBefore(element, parent.at);
|
|
587
|
-
} else {
|
|
588
|
-
element = createWrapper(container.node, ownerDocument);
|
|
589
|
-
parent.el.insertBefore(element, parent.at);
|
|
590
|
-
}
|
|
591
|
-
claimed.add(element);
|
|
592
|
-
styleContainer(element, container, chain[k - 1], item);
|
|
593
|
-
stack.push({ el: element, at: element.firstChild, pos: container.pos });
|
|
594
|
-
}
|
|
595
|
-
const top = stack[stack.length - 1] as (typeof stack)[number];
|
|
596
|
-
if (item.dom === top.at) top.at = top.at.nextSibling;
|
|
597
|
-
else {
|
|
598
|
-
// A cursor of an outer level may point at this block (it moves into a new container):
|
|
599
|
-
// step it past, or it would point into the container.
|
|
600
|
-
for (const level of stack) if (level.at === item.dom) level.at = item.dom.nextSibling;
|
|
601
|
-
top.el.insertBefore(item.dom, top.at);
|
|
602
|
-
}
|
|
603
|
-
pending.delete(item.dom);
|
|
698
|
+
const { dom } = items[i] as (typeof items)[number];
|
|
699
|
+
if (dom === at) at = at.nextSibling;
|
|
700
|
+
else page.insertBefore(dom, at);
|
|
701
|
+
}
|
|
702
|
+
// What is left after the last placed block: strays, and blocks of later pages.
|
|
703
|
+
while (at) {
|
|
704
|
+
const next: ChildNode | null = at.nextSibling;
|
|
705
|
+
at.remove();
|
|
706
|
+
at = next;
|
|
604
707
|
}
|
|
605
|
-
while (stack.length) closeLevel(stack.pop() as (typeof stack)[number]);
|
|
606
708
|
}
|
|
607
709
|
// Pages no longer needed and stray DOM in the host: all after the last page by now.
|
|
608
710
|
let rest = (this.pages[count - 1] as HTMLElement).nextSibling;
|
|
@@ -617,19 +719,20 @@ export class EditorView {
|
|
|
617
719
|
private createPage(): HTMLElement {
|
|
618
720
|
const page = this.host.ownerDocument.createElement("section");
|
|
619
721
|
page.className = "foli-page";
|
|
620
|
-
page.style.cssText = this.pageStyle;
|
|
621
722
|
return page;
|
|
622
723
|
}
|
|
623
724
|
|
|
624
|
-
/**
|
|
625
|
-
private
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
725
|
+
/** The last section's page setup (the envelope's), for an empty document. */
|
|
726
|
+
private lastSheet(): Sheet {
|
|
727
|
+
return sheetOf(this.editor.settings, 0);
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
/** The print rules for the document's page setups, written when they change. */
|
|
731
|
+
private applyPrintStyles(layout: readonly BlockLayout[]): void {
|
|
732
|
+
const sheets = [...new Set(layout.map((block) => block.sheet))];
|
|
733
|
+
const key = [this.lastSheet(), ...sheets].map((sheet) => sheet.css).join("\n");
|
|
734
|
+
if (key === this.printKey) return;
|
|
735
|
+
this.printKey = key;
|
|
633
736
|
// shortcut: written when the page settings change; a host moved to another document or shadow root keeps them where they were until then.
|
|
634
737
|
for (const style of this.printStyles) style.remove();
|
|
635
738
|
const doc = this.host.ownerDocument;
|
|
@@ -639,11 +742,10 @@ export class EditorView {
|
|
|
639
742
|
this.printStyles = parents.map((parent) => {
|
|
640
743
|
const style = doc.createElement("style");
|
|
641
744
|
style.setAttribute("data-foli-print", "");
|
|
642
|
-
style.textContent = printStyles(this.editor.settings, this.id);
|
|
745
|
+
style.textContent = printStyles(this.editor.settings, sheets, this.id);
|
|
643
746
|
parent.append(style);
|
|
644
747
|
return style;
|
|
645
748
|
});
|
|
646
|
-
this.invalidateLayout();
|
|
647
749
|
}
|
|
648
750
|
|
|
649
751
|
/** Forgets every measurement: the next flow measures the whole document again. */
|
|
@@ -654,6 +756,9 @@ export class EditorView {
|
|
|
654
756
|
|
|
655
757
|
/** Re-flows the pages once web fonts have loaded: line heights and wrapping change. */
|
|
656
758
|
private onFontsLoaded = (): void => {
|
|
759
|
+
// v3 line heights come from the fonts' measured ratios: render every block again.
|
|
760
|
+
this.ratios.clear();
|
|
761
|
+
this.fontsChanged = true;
|
|
657
762
|
this.invalidateLayout();
|
|
658
763
|
this.scheduleRelayout();
|
|
659
764
|
};
|
|
@@ -685,7 +790,7 @@ export class EditorView {
|
|
|
685
790
|
if (!desc || !measured || this.stale.has(desc.node)) continue;
|
|
686
791
|
// A hidden host (display: none) reports every block as 0x0: nothing to re-flow.
|
|
687
792
|
if (!entry.contentRect.width) continue;
|
|
688
|
-
scale ??= layoutScale(this.pages[0] as HTMLElement, this.
|
|
793
|
+
scale ??= layoutScale(this.pages[0] as HTMLElement, (this.sheets[0] as Sheet).settings);
|
|
689
794
|
let height = 0;
|
|
690
795
|
for (const fragment of desc.fragments) height += fragment.getBoundingClientRect().height;
|
|
691
796
|
// A block is measured whole; each fragment of a split one keeps its padding and border,
|
|
@@ -732,9 +837,9 @@ export class EditorView {
|
|
|
732
837
|
return null;
|
|
733
838
|
}
|
|
734
839
|
|
|
735
|
-
/** Whether `node` holds blocks rather than content: the host
|
|
840
|
+
/** Whether `node` holds blocks rather than content: the host or a page. */
|
|
736
841
|
private holdsBlocks(node: Node): boolean {
|
|
737
|
-
return node === this.host || this.pages.includes(node as HTMLElement)
|
|
842
|
+
return node === this.host || this.pages.includes(node as HTMLElement);
|
|
738
843
|
}
|
|
739
844
|
|
|
740
845
|
/** Index of the first block that starts at or after `pos` (the block count if none). */
|
|
@@ -808,7 +913,9 @@ export class EditorView {
|
|
|
808
913
|
// browser shows it at the start of the next line at a soft wrap.
|
|
809
914
|
const split = desc.splits.indexOf(offset);
|
|
810
915
|
if (split >= 0) return fragmentStart(desc, split + 1);
|
|
811
|
-
|
|
916
|
+
// Deleted text hidden by the review view takes no caret: the shown text next to it does.
|
|
917
|
+
const shown = desc.leaves.filter((leaf) => !hiddenIn(leaf.dom, desc.dom));
|
|
918
|
+
for (const leaf of shown) {
|
|
812
919
|
if (
|
|
813
920
|
leaf.dom.nodeType === Node.TEXT_NODE &&
|
|
814
921
|
leaf.offset <= offset &&
|
|
@@ -816,14 +923,22 @@ export class EditorView {
|
|
|
816
923
|
)
|
|
817
924
|
return { node: leaf.dom, offset: offset - leaf.offset };
|
|
818
925
|
}
|
|
819
|
-
const next =
|
|
820
|
-
if (next)
|
|
821
|
-
|
|
926
|
+
const next = shown.find((leaf) => leaf.offset >= offset);
|
|
927
|
+
if (next) {
|
|
928
|
+
if (next.dom.nodeType === Node.TEXT_NODE) return { node: next.dom, offset: 0 };
|
|
929
|
+
return before(next.dom);
|
|
930
|
+
}
|
|
931
|
+
const last = shown[shown.length - 1];
|
|
822
932
|
if (last) {
|
|
933
|
+
if (last.dom.nodeType === Node.TEXT_NODE)
|
|
934
|
+
return { node: last.dom, offset: (last.dom as Text).length };
|
|
823
935
|
const point = before(last.dom);
|
|
824
936
|
return { node: point.node, offset: point.offset + 1 };
|
|
825
937
|
}
|
|
826
|
-
|
|
938
|
+
// An empty block: before its <br> (after a list number shown in front of it).
|
|
939
|
+
const tail = desc.dom.lastChild;
|
|
940
|
+
const end = tail?.nodeName === "BR" ? desc.dom.childNodes.length - 1 : 0;
|
|
941
|
+
return { node: desc.dom, offset: end };
|
|
827
942
|
}
|
|
828
943
|
|
|
829
944
|
// ---------- selection ----------
|
|
@@ -1088,19 +1203,25 @@ export class EditorView {
|
|
|
1088
1203
|
const $pos = this.state.doc.resolve(pos);
|
|
1089
1204
|
const node = $pos.parent.maybeChild($pos.index());
|
|
1090
1205
|
const href = node && type.isInSet(node.marks)?.attrs.href;
|
|
1091
|
-
return
|
|
1206
|
+
return isSafeHref(href) ? href : null;
|
|
1092
1207
|
}
|
|
1093
1208
|
|
|
1094
1209
|
private onKeyDown = (event: KeyboardEvent): void => {
|
|
1095
1210
|
if (event.isComposing || this.composing) return;
|
|
1096
1211
|
this.syncSelection();
|
|
1097
|
-
if (this.crossFragmentEdge(event)) {
|
|
1212
|
+
if (this.crossFragmentEdge(event) || this.crossListNumber(event)) {
|
|
1098
1213
|
event.preventDefault();
|
|
1099
1214
|
return;
|
|
1100
1215
|
}
|
|
1101
1216
|
const name = keyName(event);
|
|
1102
1217
|
// Ctrl/Cmd+Shift+V: the paste event that follows uses only the plain text.
|
|
1103
1218
|
this.plainPaste = name === "Mod-Shift-v";
|
|
1219
|
+
// Tab and Shift+Tab move the focus on in read-only mode, and right after Escape where Tab
|
|
1220
|
+
// would type a tab (no keyboard trap).
|
|
1221
|
+
const tab = name === "Tab" || name === "Shift-Tab";
|
|
1222
|
+
const leave = tab && (this.tabLeaves || !this.editable);
|
|
1223
|
+
if (event.key !== "Shift") this.tabLeaves = name === "Escape";
|
|
1224
|
+
if (leave) return;
|
|
1104
1225
|
// Like `editor.handleKey`, but through the view's dispatch, so read-only mode drops edits.
|
|
1105
1226
|
// A key no command takes keeps its default (Tab outside a list moves the focus on).
|
|
1106
1227
|
if (this.runKey(name)) {
|
|
@@ -1193,6 +1314,30 @@ export class EditorView {
|
|
|
1193
1314
|
return true;
|
|
1194
1315
|
}
|
|
1195
1316
|
|
|
1317
|
+
/**
|
|
1318
|
+
* A v3 list number is not text, but browsers stop the caret before it as well as after: Right
|
|
1319
|
+
* at the end of the paragraph before, or Left at a numbered paragraph's start, would seem to do
|
|
1320
|
+
* nothing. Moves over the paragraph boundary in one step (word moves are left to the browser).
|
|
1321
|
+
*/
|
|
1322
|
+
private crossListNumber(event: KeyboardEvent): boolean {
|
|
1323
|
+
const forward = event.key === "ArrowRight";
|
|
1324
|
+
if (!this.word || (!forward && event.key !== "ArrowLeft")) return false;
|
|
1325
|
+
if (event.metaKey || event.ctrlKey || event.altKey) return false;
|
|
1326
|
+
const { selection, doc } = this.state;
|
|
1327
|
+
if (!(selection instanceof TextSelection) || (!event.shiftKey && !selection.empty))
|
|
1328
|
+
return false;
|
|
1329
|
+
const { $head } = selection;
|
|
1330
|
+
if ($head.depth !== 1 || $head.parentOffset !== (forward ? $head.parent.content.size : 0))
|
|
1331
|
+
return false;
|
|
1332
|
+
const numbered = $head.index(0) + (forward ? 1 : 0);
|
|
1333
|
+
const other = doc.maybeChild($head.index(0) + (forward ? 1 : -1));
|
|
1334
|
+
if (!other?.isTextblock || !this.word.hasMarker(numbered)) return false;
|
|
1335
|
+
const head = forward ? $head.after() + 1 : $head.before() - 1;
|
|
1336
|
+
const anchor = event.shiftKey ? selection.anchor : head;
|
|
1337
|
+
this.dispatch(this.state.tr.setSelection(TextSelection.create(doc, anchor, head)));
|
|
1338
|
+
return true;
|
|
1339
|
+
}
|
|
1340
|
+
|
|
1196
1341
|
/** Safety net: a DOM change that was not prevented is read back into the model. */
|
|
1197
1342
|
private onInput = (): void => {
|
|
1198
1343
|
if (!this.composing) this.flush();
|
|
@@ -1277,6 +1422,8 @@ export class EditorView {
|
|
|
1277
1422
|
if (node === sel?.focusNode) caret = domText.length + sel.focusOffset;
|
|
1278
1423
|
domText += (node as Text).data;
|
|
1279
1424
|
} else if (hardBreaks.has(node)) domText += HARD_BREAK;
|
|
1425
|
+
// Shown but not document text (a list number, opaque content): never read back.
|
|
1426
|
+
else if ((node as Element).getAttribute?.("contenteditable") === "false") return;
|
|
1280
1427
|
else node.childNodes.forEach(walk);
|
|
1281
1428
|
};
|
|
1282
1429
|
let first = true;
|
|
@@ -1307,7 +1454,14 @@ export class EditorView {
|
|
|
1307
1454
|
this.blocks.forEach(({ node: block }, i) => {
|
|
1308
1455
|
if (i > 0) modelText += BLOCK_SEPARATOR;
|
|
1309
1456
|
lineStarts.push(modelText.length);
|
|
1310
|
-
|
|
1457
|
+
block.forEach((child) => {
|
|
1458
|
+
// A tab is text in the DOM (a "\t" text node).
|
|
1459
|
+
modelText += child.isText
|
|
1460
|
+
? (child.text ?? "")
|
|
1461
|
+
: child.type.name === "tab"
|
|
1462
|
+
? "\t"
|
|
1463
|
+
: HARD_BREAK;
|
|
1464
|
+
});
|
|
1311
1465
|
});
|
|
1312
1466
|
/** Model position of an offset in `modelText`. */
|
|
1313
1467
|
const posAt = (offset: number): number => {
|
|
@@ -1383,11 +1537,13 @@ export class EditorView {
|
|
|
1383
1537
|
const { selection, doc } = this.state;
|
|
1384
1538
|
if (selection.empty || !event.clipboardData) return;
|
|
1385
1539
|
event.preventDefault();
|
|
1540
|
+
const context = wordContext(this.state) as WordContext;
|
|
1386
1541
|
const { text, html, json } = serializeRange(
|
|
1387
1542
|
doc,
|
|
1388
1543
|
selection.from,
|
|
1389
1544
|
selection.to,
|
|
1390
1545
|
this.host.ownerDocument,
|
|
1546
|
+
{ context, ratio: this.lineRatio },
|
|
1391
1547
|
);
|
|
1392
1548
|
event.clipboardData.setData("text/plain", text);
|
|
1393
1549
|
event.clipboardData.setData("text/html", html);
|
|
@@ -1442,14 +1598,22 @@ export class EditorView {
|
|
|
1442
1598
|
};
|
|
1443
1599
|
if (command(state, dispatch)) return applied;
|
|
1444
1600
|
}
|
|
1445
|
-
const marks = (state.storedMarks ?? marksAt(state.selection.$from)).map((m) =>
|
|
1446
|
-
|
|
1447
|
-
|
|
1601
|
+
const marks = typedMarks(state.storedMarks ?? marksAt(state.selection.$from)).map((m) =>
|
|
1602
|
+
m.toJSON(),
|
|
1603
|
+
);
|
|
1604
|
+
// The document takes the styles, lists and comments the content needs (HTML is read as
|
|
1605
|
+
// schema v2 content, then migrated).
|
|
1606
|
+
const pasted = docFromClipboard(data, marks, (found) =>
|
|
1607
|
+
found.html
|
|
1608
|
+
? fitPastedV2(state, parseContentV2(found.content))
|
|
1609
|
+
: fitPaste(state, found.content, found.word),
|
|
1610
|
+
);
|
|
1611
|
+
// One empty paragraph: nothing to paste (a lone opaque block is something).
|
|
1448
1612
|
const only = pasted?.doc.childCount === 1 ? pasted.doc.firstChild : null;
|
|
1449
1613
|
if (!pasted || (only?.isTextblock && !only.content.size)) return false;
|
|
1450
|
-
const { doc, open } = pasted;
|
|
1451
|
-
// As a slice (text joins the text around the caret); where
|
|
1452
|
-
//
|
|
1614
|
+
const { doc, open, envelope } = pasted;
|
|
1615
|
+
// As a slice (text joins the text around the caret); where that cannot be (an opaque block
|
|
1616
|
+
// at an end), as whole blocks at the caret.
|
|
1453
1617
|
const asSlice = (tr: Transaction) => {
|
|
1454
1618
|
const $pos = tr.selection.$from;
|
|
1455
1619
|
if (!open && $pos.depth === 1 && $pos.parent.isTextblock && !$pos.parent.content.size) {
|
|
@@ -1457,9 +1621,9 @@ export class EditorView {
|
|
|
1457
1621
|
tr.replace(from, $pos.after(), new Slice(doc.content, 0, 0));
|
|
1458
1622
|
tr.setSelection(Selection.near(tr.doc.resolve(from + doc.content.size), -1));
|
|
1459
1623
|
} else {
|
|
1460
|
-
// A slice opens into its first and last blocks:
|
|
1624
|
+
// A slice opens into its first and last blocks: an opaque block there has no inside.
|
|
1461
1625
|
if (doc.firstChild?.isLeaf || doc.lastChild?.isLeaf) {
|
|
1462
|
-
throw new TransformError("
|
|
1626
|
+
throw new TransformError("An opaque block at an end is pasted as a block");
|
|
1463
1627
|
}
|
|
1464
1628
|
tr.replaceSelection(doc.slice(1, doc.content.size - 1));
|
|
1465
1629
|
}
|
|
@@ -1474,6 +1638,11 @@ export class EditorView {
|
|
|
1474
1638
|
try {
|
|
1475
1639
|
tr.deleteSelection();
|
|
1476
1640
|
strategy(tr);
|
|
1641
|
+
const current = wordContext(state)?.envelope;
|
|
1642
|
+
if (envelope && current && envelope !== current) {
|
|
1643
|
+
withEnvelope(tr, envelope);
|
|
1644
|
+
setComments(tr, current.comments, envelope.comments);
|
|
1645
|
+
}
|
|
1477
1646
|
} catch (error) {
|
|
1478
1647
|
if (error instanceof TransformError) continue;
|
|
1479
1648
|
throw error;
|
|
@@ -1489,96 +1658,25 @@ export class EditorView {
|
|
|
1489
1658
|
}
|
|
1490
1659
|
|
|
1491
1660
|
/**
|
|
1492
|
-
* Pastes whole blocks at the caret (after deleting the selection)
|
|
1493
|
-
*
|
|
1494
|
-
* empty item, go before or after the item, or between its two halves); other blocks go before
|
|
1495
|
-
* or after the caret's textblock (replacing it when empty) where its parent takes them, else
|
|
1496
|
-
* between its two halves. Throws TransformError where the schema does not allow it.
|
|
1661
|
+
* Pastes whole blocks at the caret (after deleting the selection): before or after the caret's
|
|
1662
|
+
* paragraph, else between its two halves (an empty one is replaced as a slice first).
|
|
1497
1663
|
*/
|
|
1498
1664
|
function insertBlocks(tr: Transaction, doc: ModelNode): void {
|
|
1499
1665
|
const $pos = tr.selection.$from;
|
|
1500
1666
|
if (!$pos.parent.isTextblock) throw new TransformError("No textblock at the caret");
|
|
1501
1667
|
const atStart = $pos.parentOffset === 0;
|
|
1502
1668
|
const atEnd = $pos.parentOffset === $pos.parent.content.size;
|
|
1503
|
-
const only = doc.childCount === 1 ? doc.firstChild : null;
|
|
1504
|
-
const itemType = tr.doc.type.schema.nodes.listItem;
|
|
1505
|
-
const inItem = $pos.depth >= 2 && $pos.node(-1).type === itemType;
|
|
1506
1669
|
let at: number;
|
|
1507
|
-
|
|
1508
|
-
if (
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
if (atStart && atEnd && item.childCount === 1) {
|
|
1513
|
-
at = $pos.before(depth);
|
|
1514
|
-
tr.delete(at, $pos.after(depth));
|
|
1515
|
-
} else if (atEnd) {
|
|
1516
|
-
// After the item (and its nested list, if it has one): no item left without its text.
|
|
1517
|
-
at = $pos.after(depth);
|
|
1518
|
-
} else if (atStart && $pos.index(depth) === 0) at = $pos.before(depth);
|
|
1519
|
-
else if (atStart) {
|
|
1520
|
-
// At a later block's start: split the item between blocks, not inside the text.
|
|
1521
|
-
const boundary = $pos.before();
|
|
1522
|
-
tr.split(boundary, 1);
|
|
1523
|
-
at = boundary + 1;
|
|
1524
|
-
} else {
|
|
1525
|
-
tr.split($pos.pos, 2);
|
|
1526
|
-
at = $pos.pos + 2;
|
|
1527
|
-
}
|
|
1528
|
-
} else {
|
|
1529
|
-
// Before, after, or in place of the textblock where its parent takes the blocks there (a
|
|
1530
|
-
// list item's first paragraph must stay first); else between its two halves.
|
|
1531
|
-
const parent = $pos.node(-1);
|
|
1532
|
-
const index = $pos.index(-1);
|
|
1533
|
-
if (atStart && atEnd && parent.canReplace(index, index + 1, content)) {
|
|
1534
|
-
at = $pos.before();
|
|
1535
|
-
tr.delete(at, $pos.after());
|
|
1536
|
-
} else if (atStart && parent.canReplace(index, index, content)) at = $pos.before();
|
|
1537
|
-
else if (atEnd && parent.canReplace(index + 1, index + 1, content)) at = $pos.after();
|
|
1538
|
-
else {
|
|
1539
|
-
tr.split($pos.pos);
|
|
1540
|
-
at = $pos.pos + 1;
|
|
1541
|
-
}
|
|
1670
|
+
if (atStart) at = $pos.before();
|
|
1671
|
+
else if (atEnd) at = $pos.after();
|
|
1672
|
+
else {
|
|
1673
|
+
tr.split($pos.pos);
|
|
1674
|
+
at = $pos.pos + 1;
|
|
1542
1675
|
}
|
|
1543
|
-
tr.insert(at, content);
|
|
1544
|
-
tr.setSelection(Selection.near(tr.doc.resolve(at + content.size), -1));
|
|
1545
|
-
}
|
|
1546
|
-
|
|
1547
|
-
/** The first block element inside a container element (through nested containers), or null. */
|
|
1548
|
-
function firstBlockIn(element: Element): Element | null {
|
|
1549
|
-
let child = element.firstElementChild;
|
|
1550
|
-
while (isWrapper(child)) child = child.firstElementChild;
|
|
1551
|
-
return child;
|
|
1552
|
-
}
|
|
1553
|
-
|
|
1554
|
-
/**
|
|
1555
|
-
* Sets what a container element shows on a page: an ordered list item's number (so numbering
|
|
1556
|
-
* continues across pages), and no marker on an item continued from the previous page (its
|
|
1557
|
-
* first block is on an earlier page, or this is a continued fragment of it).
|
|
1558
|
-
*/
|
|
1559
|
-
function styleContainer(
|
|
1560
|
-
element: HTMLElement,
|
|
1561
|
-
container: Container,
|
|
1562
|
-
parent: Container | undefined,
|
|
1563
|
-
item: { block: number; fragment: number },
|
|
1564
|
-
): void {
|
|
1565
|
-
if (element.localName !== "li") return;
|
|
1566
|
-
const continued = item.block !== container.firstBlock || item.fragment > 0;
|
|
1567
|
-
const start = parent?.node.type.name === "orderedList" ? parent.node.attrs.start : undefined;
|
|
1568
|
-
const value = typeof start === "number" ? String(start + container.index) : null;
|
|
1569
|
-
// Written only when they change (checked against what was written last, without reading the
|
|
1570
|
-
// DOM): this runs for every list item on every render, and every write invalidates style.
|
|
1571
|
-
const state = `${continued}|${value}`;
|
|
1572
|
-
if (containerStates.get(element) === state) return;
|
|
1573
|
-
containerStates.set(element, state);
|
|
1574
|
-
element.style.listStyleType = continued ? "none" : "";
|
|
1575
|
-
if (value === null) element.removeAttribute("value");
|
|
1576
|
-
else element.setAttribute("value", value);
|
|
1676
|
+
tr.insert(at, doc.content);
|
|
1677
|
+
tr.setSelection(Selection.near(tr.doc.resolve(at + doc.content.size), -1));
|
|
1577
1678
|
}
|
|
1578
1679
|
|
|
1579
|
-
/** What `styleContainer` last wrote on each list item element. */
|
|
1580
|
-
const containerStates = new WeakMap<HTMLElement, string>();
|
|
1581
|
-
|
|
1582
1680
|
/**
|
|
1583
1681
|
* Client rects are scaled by CSS `zoom` or a `transform` on an ancestor, while margins and the
|
|
1584
1682
|
* page geometry are in layout px: the ratio between the two for a page. From the page's width
|
|
@@ -1620,34 +1718,84 @@ function measureBox(desc: BlockDesc, scale: number): Measure {
|
|
|
1620
1718
|
}
|
|
1621
1719
|
|
|
1622
1720
|
/**
|
|
1623
|
-
* What the flow needs of a block: its measurement, its lines (on demand),
|
|
1624
|
-
*
|
|
1721
|
+
* What the flow needs of a block: its measurement, its lines (on demand), Word's spacing before
|
|
1722
|
+
* and after (next to its neighbours), keep with next, keep lines together (not for a block
|
|
1723
|
+
* taller than a page, or one with a page break in it), page breaks ending their line's page,
|
|
1724
|
+
* and a page break before the next block ending this one's page.
|
|
1625
1725
|
*/
|
|
1626
|
-
function
|
|
1726
|
+
function wordBox(
|
|
1727
|
+
blocks: readonly WordBlockDesc[],
|
|
1728
|
+
i: number,
|
|
1729
|
+
measure: Measure,
|
|
1730
|
+
lines: () => Lines,
|
|
1731
|
+
layout: BlockLayout,
|
|
1732
|
+
): BlockBox {
|
|
1733
|
+
const pageHeight = contentHeight(layout.sheet.settings);
|
|
1734
|
+
const desc = blocks[i] as WordBlockDesc;
|
|
1735
|
+
const next = blocks[i + 1];
|
|
1736
|
+
const { before, after } = spacing(blocks[i - 1]?.word, desc.word, next?.word);
|
|
1737
|
+
const pageBreaks = desc.leaves.filter(
|
|
1738
|
+
(leaf) =>
|
|
1739
|
+
leaf.dom.nodeType !== Node.TEXT_NODE && (leaf.dom as Element).hasAttribute(PAGE_BREAK_ATTR),
|
|
1740
|
+
);
|
|
1741
|
+
const hasBreak = pageBreaks.length > 0;
|
|
1742
|
+
const kept = desc.word.keepLines && !hasBreak && measure.height <= pageHeight;
|
|
1743
|
+
const bottoms = () => (kept ? [measure.height] : lines().bottoms);
|
|
1627
1744
|
return {
|
|
1628
1745
|
height: measure.height,
|
|
1629
|
-
marginTop:
|
|
1630
|
-
marginBottom:
|
|
1631
|
-
|
|
1632
|
-
|
|
1746
|
+
marginTop: before,
|
|
1747
|
+
marginBottom: after,
|
|
1748
|
+
keepWithNext: desc.word.keepNext,
|
|
1749
|
+
pageHeight,
|
|
1750
|
+
lines: bottoms,
|
|
1633
1751
|
breaksAfter: () => {
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
(leaf.dom as Element).hasAttribute(PAGE_BREAK_ATTR),
|
|
1638
|
-
);
|
|
1639
|
-
if (!breaks.length) return [];
|
|
1640
|
-
const { starts } = lines();
|
|
1641
|
-
// The line holding the break: the last one starting at or before it.
|
|
1642
|
-
return breaks.map((leaf) => {
|
|
1752
|
+
// The line holding each break: the last one starting at or before it.
|
|
1753
|
+
const breaks = pageBreaks.map((leaf) => {
|
|
1754
|
+
const { starts } = lines();
|
|
1643
1755
|
let line = starts.length - 1;
|
|
1644
1756
|
while (line > 0 && (starts[line] as number) > leaf.offset) line--;
|
|
1645
1757
|
return line;
|
|
1646
1758
|
});
|
|
1759
|
+
if (next?.word.pageBreakBefore || layout.breakAfter) breaks.push(bottoms().length - 1);
|
|
1760
|
+
return breaks;
|
|
1647
1761
|
},
|
|
1648
1762
|
};
|
|
1649
1763
|
}
|
|
1650
1764
|
|
|
1765
|
+
/**
|
|
1766
|
+
* Writes Word's space above each v3 block as its top margin: the space after the block above
|
|
1767
|
+
* and the space before this one, on the same page; at the top of a page only its space before,
|
|
1768
|
+
* and only on the first page or after an explicit break (a page the text flowed onto drops it).
|
|
1769
|
+
* Written only when it changes.
|
|
1770
|
+
*/
|
|
1771
|
+
function applyGaps(
|
|
1772
|
+
blocks: readonly BlockDesc[],
|
|
1773
|
+
pages: readonly (readonly number[])[],
|
|
1774
|
+
boxes: readonly BlockBox[],
|
|
1775
|
+
): void {
|
|
1776
|
+
blocks.forEach((desc, i) => {
|
|
1777
|
+
const page = pages[i]?.[0] ?? 0;
|
|
1778
|
+
const above = pages[i - 1];
|
|
1779
|
+
const box = boxes[i] as BlockBox;
|
|
1780
|
+
let gap: number;
|
|
1781
|
+
if (above && above[above.length - 1] === page)
|
|
1782
|
+
gap = (boxes[i - 1] as BlockBox).marginBottom + box.marginTop;
|
|
1783
|
+
else {
|
|
1784
|
+
const prior = boxes[i - 1];
|
|
1785
|
+
const breaks = prior?.breaksAfter?.() ?? [];
|
|
1786
|
+
const forced =
|
|
1787
|
+
page === 0 || (!!prior && breaks.length > 0 && breaks.includes(prior.lines().length - 1));
|
|
1788
|
+
gap = forced ? box.marginTop : 0;
|
|
1789
|
+
}
|
|
1790
|
+
if (gaps.get(desc.dom) === gap) return;
|
|
1791
|
+
gaps.set(desc.dom, gap);
|
|
1792
|
+
desc.dom.style.marginTop = `${gap}px`;
|
|
1793
|
+
});
|
|
1794
|
+
}
|
|
1795
|
+
|
|
1796
|
+
/** The gap `applyGaps` last wrote on each block element (the style serializes it rounded). */
|
|
1797
|
+
const gaps = new WeakMap<HTMLElement, number>();
|
|
1798
|
+
|
|
1651
1799
|
/**
|
|
1652
1800
|
* What the next flow can reuse of the last one: the unchanged blocks before the first changed
|
|
1653
1801
|
* (or stale) one, and the unchanged tail after the last.
|
|
@@ -1656,6 +1804,7 @@ function reuseOf(
|
|
|
1656
1804
|
last: { nodes: readonly ModelNode[]; result: FlowResult },
|
|
1657
1805
|
nodes: readonly ModelNode[],
|
|
1658
1806
|
stale: ReadonlySet<ModelNode>,
|
|
1807
|
+
margin = 0,
|
|
1659
1808
|
): Reuse {
|
|
1660
1809
|
const before = last.nodes;
|
|
1661
1810
|
const same = (a: ModelNode | undefined, b: ModelNode | undefined) =>
|
|
@@ -1669,6 +1818,9 @@ function reuseOf(
|
|
|
1669
1818
|
same(nodes[nodes.length - 1 - tail], before[before.length - 1 - tail])
|
|
1670
1819
|
)
|
|
1671
1820
|
tail++;
|
|
1821
|
+
// Blocks whose box depends on a changed neighbour are not reused either.
|
|
1822
|
+
from = Math.max(0, from - margin);
|
|
1823
|
+
tail = Math.max(0, tail - margin);
|
|
1672
1824
|
const delta = nodes.length - before.length;
|
|
1673
1825
|
return {
|
|
1674
1826
|
previous: last.result,
|