@folijs/view 0.4.0 → 0.6.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 +59 -0
- package/dist/clipboard.d.ts +3 -1
- package/dist/clipboard.d.ts.map +1 -1
- package/dist/clipboard.js.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/layout/flow.d.ts +195 -20
- package/dist/layout/flow.d.ts.map +1 -1
- package/dist/layout/flow.js +790 -102
- package/dist/layout/flow.js.map +1 -1
- package/dist/print.d.ts +22 -0
- package/dist/print.d.ts.map +1 -0
- package/dist/print.js +31 -0
- package/dist/print.js.map +1 -0
- package/dist/view.d.ts +407 -8
- package/dist/view.d.ts.map +1 -1
- package/dist/view.js +2956 -263
- package/dist/view.js.map +1 -1
- package/dist/word/blocks.d.ts +99 -2
- package/dist/word/blocks.d.ts.map +1 -1
- package/dist/word/blocks.js +535 -31
- package/dist/word/blocks.js.map +1 -1
- package/dist/word/columns.d.ts +23 -0
- package/dist/word/columns.d.ts.map +1 -0
- package/dist/word/columns.js +51 -0
- package/dist/word/columns.js.map +1 -0
- package/dist/word/controls.d.ts +63 -0
- package/dist/word/controls.d.ts.map +1 -0
- package/dist/word/controls.js +411 -0
- package/dist/word/controls.js.map +1 -0
- package/dist/word/edit.d.ts +32 -11
- package/dist/word/edit.d.ts.map +1 -1
- package/dist/word/edit.js +215 -33
- package/dist/word/edit.js.map +1 -1
- package/dist/word/matches.d.ts +23 -0
- package/dist/word/matches.d.ts.map +1 -0
- package/dist/word/matches.js +97 -0
- package/dist/word/matches.js.map +1 -0
- package/dist/word/notes.d.ts +105 -0
- package/dist/word/notes.d.ts.map +1 -0
- package/dist/word/notes.js +392 -0
- package/dist/word/notes.js.map +1 -0
- package/dist/word/pages.d.ts +26 -13
- package/dist/word/pages.d.ts.map +1 -1
- package/dist/word/pages.js +439 -203
- package/dist/word/pages.js.map +1 -1
- package/dist/word/regions.d.ts +113 -0
- package/dist/word/regions.d.ts.map +1 -0
- package/dist/word/regions.js +374 -0
- package/dist/word/regions.js.map +1 -0
- package/dist/word/table.d.ts +79 -0
- package/dist/word/table.d.ts.map +1 -0
- package/dist/word/table.js +210 -0
- package/dist/word/table.js.map +1 -0
- package/package.json +2 -2
- package/src/clipboard.ts +3 -0
- package/src/index.ts +8 -1
- package/src/layout/flow.ts +948 -111
- package/src/print.ts +48 -0
- package/src/view.ts +3291 -273
- package/src/word/blocks.ts +611 -28
- package/src/word/columns.ts +66 -0
- package/src/word/controls.ts +447 -0
- package/src/word/edit.ts +253 -30
- package/src/word/matches.ts +99 -0
- package/src/word/notes.ts +451 -0
- package/src/word/pages.ts +524 -192
- package/src/word/regions.ts +469 -0
- package/src/word/table.ts +267 -0
package/src/word/edit.ts
CHANGED
|
@@ -10,33 +10,63 @@
|
|
|
10
10
|
* opaque inline node a leaf element.
|
|
11
11
|
*/
|
|
12
12
|
import {
|
|
13
|
+
type CellFormat,
|
|
14
|
+
type FieldDisplay,
|
|
15
|
+
fieldDisplay,
|
|
13
16
|
isSafeHref,
|
|
14
17
|
type ListMarker,
|
|
15
18
|
type Mark,
|
|
16
19
|
type MarkJSON,
|
|
17
20
|
type Node as ModelNode,
|
|
18
21
|
type RunFormat,
|
|
22
|
+
referencesIn,
|
|
19
23
|
type WordContext,
|
|
20
24
|
} from "@folijs/core";
|
|
21
25
|
import type { BlockDesc, LeafDesc } from "../render.js";
|
|
22
26
|
import {
|
|
23
27
|
CHANGE_BAR,
|
|
24
28
|
changeElement,
|
|
29
|
+
type FloatGeometry,
|
|
30
|
+
floatElements,
|
|
31
|
+
floatGeometry,
|
|
25
32
|
fontStack,
|
|
33
|
+
type ImageSource,
|
|
34
|
+
imageSource,
|
|
26
35
|
latin,
|
|
27
36
|
lineHeight,
|
|
37
|
+
marginAbove,
|
|
28
38
|
markerElement,
|
|
39
|
+
NOTE_NUMBER_ATTR,
|
|
40
|
+
NOTE_REF_ATTR,
|
|
41
|
+
onlyPictures,
|
|
29
42
|
opaqueBlock,
|
|
30
43
|
opaqueText,
|
|
44
|
+
PICTURE_ATTR,
|
|
31
45
|
PX_PER_TWIP,
|
|
32
46
|
parseOpaque,
|
|
47
|
+
pictureElement,
|
|
33
48
|
type ReviewView,
|
|
34
49
|
styleParagraph,
|
|
35
50
|
styleRun,
|
|
36
51
|
tagOf,
|
|
52
|
+
WORD_COLUMN_BREAK_ATTR,
|
|
37
53
|
WORD_PAGE_BREAK_ATTR,
|
|
38
54
|
} from "./blocks.js";
|
|
39
55
|
|
|
56
|
+
/** Inline nodes with nothing to show: a line holding only these is still an empty line. */
|
|
57
|
+
const NOT_IN_LINE = new Set([
|
|
58
|
+
"bookmarkStart",
|
|
59
|
+
"bookmarkEnd",
|
|
60
|
+
"commentStart",
|
|
61
|
+
"commentEnd",
|
|
62
|
+
"controlStart",
|
|
63
|
+
"controlEnd",
|
|
64
|
+
"fieldStart",
|
|
65
|
+
"fieldCode",
|
|
66
|
+
"fieldSeparator",
|
|
67
|
+
"fieldEnd",
|
|
68
|
+
]);
|
|
69
|
+
|
|
40
70
|
const REVIEW_MARKS = new Set(["insertion", "deletion", "formatChange"]);
|
|
41
71
|
|
|
42
72
|
/** Whether a paragraph has tracked changes: in its text, its mark or its formatting. */
|
|
@@ -60,6 +90,18 @@ export interface WordInfo {
|
|
|
60
90
|
/** The paragraph's style and whether it drops spacing next to the same style. */
|
|
61
91
|
style: string | null;
|
|
62
92
|
contextual: boolean;
|
|
93
|
+
/** A paragraph with nothing to show in its line. */
|
|
94
|
+
empty: boolean;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** A paragraph's inline nodes, by type name. */
|
|
98
|
+
function childTypes(node: ModelNode): string[] {
|
|
99
|
+
const types: string[] = [];
|
|
100
|
+
if (node.type.name === "paragraph")
|
|
101
|
+
node.forEach((child) => {
|
|
102
|
+
types.push(child.type.name);
|
|
103
|
+
});
|
|
104
|
+
return types;
|
|
63
105
|
}
|
|
64
106
|
|
|
65
107
|
/** A rendered v3 block: a view block plus its flow information and what it was rendered for. */
|
|
@@ -69,21 +111,40 @@ export interface WordBlockDesc extends BlockDesc {
|
|
|
69
111
|
readonly key: string;
|
|
70
112
|
}
|
|
71
113
|
|
|
114
|
+
/**
|
|
115
|
+
* A floating picture's holder (drawn first in its paragraph, the view's own DOM) to its anchor:
|
|
116
|
+
* the empty, non-editable span at the picture's place in the text, which positions map to.
|
|
117
|
+
*/
|
|
118
|
+
export const FLOAT_ANCHORS = new WeakMap<Element, HTMLElement>();
|
|
119
|
+
|
|
72
120
|
/**
|
|
73
121
|
* Renders the blocks of one v3 document for the editable view. `prepare` runs before each
|
|
74
122
|
* render: list numbers and comment ranges depend on the blocks before.
|
|
75
123
|
*/
|
|
76
124
|
export class WordRenderer {
|
|
77
|
-
|
|
125
|
+
#context: WordContext;
|
|
78
126
|
readonly #doc: Document;
|
|
79
127
|
readonly #ratio: (family: string) => number;
|
|
80
128
|
/** For the clipboard (`clipboardBlocks`): links as links, list numbers as Word's HTML has them. */
|
|
81
129
|
readonly #clipboard: boolean;
|
|
82
130
|
/** How tracked changes show. */
|
|
83
131
|
readonly #review: ReviewView;
|
|
132
|
+
/** Where pictures' images are shown from. */
|
|
133
|
+
readonly #image: ImageSource | undefined;
|
|
84
134
|
#markers: (ListMarker | null)[] = [];
|
|
85
135
|
/** Comment ranges open at the start of each block. */
|
|
86
136
|
#comments: number[][] = [];
|
|
137
|
+
/** Where a document adds paragraph spacing: the space after the paragraph above, in a cell. */
|
|
138
|
+
#above: number[] = [];
|
|
139
|
+
/** Whether fields show their codes. */
|
|
140
|
+
readonly #codes: boolean;
|
|
141
|
+
/** What each block shows of its fields. */
|
|
142
|
+
#fields: FieldDisplay[] = [];
|
|
143
|
+
/** What note references show, by story, and what a note's own mark shows. */
|
|
144
|
+
#notes: ReadonlyMap<string, string> = new Map();
|
|
145
|
+
#noteMark = "";
|
|
146
|
+
/** The numbers each block shows. */
|
|
147
|
+
#noteKeys: string[] = [];
|
|
87
148
|
|
|
88
149
|
constructor(
|
|
89
150
|
context: WordContext,
|
|
@@ -91,24 +152,56 @@ export class WordRenderer {
|
|
|
91
152
|
ratio: (family: string) => number,
|
|
92
153
|
clipboard = false,
|
|
93
154
|
review: ReviewView = "markup",
|
|
155
|
+
image?: ImageSource,
|
|
156
|
+
codes = false,
|
|
94
157
|
) {
|
|
158
|
+
this.#codes = codes;
|
|
95
159
|
this.#context = context;
|
|
96
160
|
this.#doc = doc;
|
|
97
161
|
this.#ratio = ratio;
|
|
98
162
|
this.#clipboard = clipboard;
|
|
99
163
|
this.#review = review;
|
|
164
|
+
this.#image = image ?? imageSource(context.envelope.resources);
|
|
100
165
|
}
|
|
101
166
|
|
|
102
167
|
get context(): WordContext {
|
|
103
168
|
return this.#context;
|
|
104
169
|
}
|
|
105
170
|
|
|
106
|
-
/**
|
|
107
|
-
|
|
171
|
+
/** The envelope moved on without touching what blocks look like (a story was edited). */
|
|
172
|
+
set context(context: WordContext) {
|
|
173
|
+
this.#context = context;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** What notes show: set before `prepare`. */
|
|
177
|
+
setNotes(references: ReadonlyMap<string, string>, mark = ""): void {
|
|
178
|
+
this.#notes = references;
|
|
179
|
+
this.#noteMark = mark;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Numbering and comment state for `blocks`, the blocks rendered, in document order (the
|
|
184
|
+
* paragraphs in table cells among them: Word numbers lists through tables). `aboveInCell`
|
|
185
|
+
* gives the paragraph before one in its table cell, with that cell's format.
|
|
186
|
+
*/
|
|
187
|
+
prepare(
|
|
188
|
+
blocks: readonly ModelNode[],
|
|
189
|
+
aboveInCell?: (index: number) => { node: ModelNode; cell: CellFormat } | null,
|
|
190
|
+
): void {
|
|
191
|
+
const { resolver } = this.#context;
|
|
192
|
+
this.#above =
|
|
193
|
+
resolver.addsParagraphSpacing && aboveInCell
|
|
194
|
+
? blocks.map((_, i) => {
|
|
195
|
+
const previous = aboveInCell(i);
|
|
196
|
+
if (previous?.node.type.name !== "paragraph") return 0;
|
|
197
|
+
const { spacing } = resolver.paragraph(previous.node.attrs, previous.cell).paragraph;
|
|
198
|
+
return (spacing.afterAutospacing ? 280 : spacing.after) * PX_PER_TWIP;
|
|
199
|
+
})
|
|
200
|
+
: [];
|
|
108
201
|
const attrs: Record<string, unknown>[] = [];
|
|
109
202
|
const comments: number[][] = [];
|
|
110
203
|
const open = new Set<number>();
|
|
111
|
-
|
|
204
|
+
for (const node of blocks) {
|
|
112
205
|
attrs.push(node.type.name === "paragraph" ? node.attrs : { numbering: { id: 0 } });
|
|
113
206
|
comments.push([...open]);
|
|
114
207
|
node.forEach((child) => {
|
|
@@ -116,32 +209,59 @@ export class WordRenderer {
|
|
|
116
209
|
if (child.type.name === "commentStart") open.add(id);
|
|
117
210
|
else if (child.type.name === "commentEnd") open.delete(id);
|
|
118
211
|
});
|
|
119
|
-
}
|
|
212
|
+
}
|
|
120
213
|
this.#markers = this.#context.resolver.markers(attrs);
|
|
121
214
|
this.#comments = comments;
|
|
215
|
+
this.#fields = fieldDisplay(blocks.map(childTypes), this.#codes);
|
|
216
|
+
this.#noteKeys = this.#notes.size
|
|
217
|
+
? blocks.map((node) =>
|
|
218
|
+
referencesIn(node)
|
|
219
|
+
.map((ref) => this.#notes.get(ref.story) ?? "")
|
|
220
|
+
.join(" "),
|
|
221
|
+
)
|
|
222
|
+
: [];
|
|
122
223
|
}
|
|
123
224
|
|
|
124
|
-
/** Whether the
|
|
225
|
+
/** Whether the block at `index` (of those prepared) shows a list number. */
|
|
125
226
|
hasMarker(index: number): boolean {
|
|
126
227
|
return !!this.#markers[index];
|
|
127
228
|
}
|
|
128
229
|
|
|
129
|
-
/** What a block's rendering depends on at `index`, besides its node. */
|
|
130
|
-
key(index: number): string {
|
|
131
|
-
|
|
230
|
+
/** What a block's rendering depends on at `index`, besides its node (and its cell's format). */
|
|
231
|
+
key(index: number, cell?: CellFormat | null): string {
|
|
232
|
+
const inCell = cell ? `|${cell.style ?? ""}:${cell.conditions.join(",")}` : "";
|
|
233
|
+
const above = this.#above[index] ? `|${this.#above[index]}` : "";
|
|
234
|
+
// A field opened or closed in an earlier block changes what shows here.
|
|
235
|
+
const field = this.#fields[index];
|
|
236
|
+
const fields = field?.hidden.size || field?.gone ? `|${[...field.hidden]}${field.gone}` : "";
|
|
237
|
+
const notes = this.#noteKeys[index] ? `|${this.#noteKeys[index]}` : "";
|
|
238
|
+
return `${this.#markers[index]?.text ?? ""}|${(this.#comments[index] ?? []).join(" ")}${inCell}${above}${fields}${notes}|${this.#noteMark}`;
|
|
132
239
|
}
|
|
133
240
|
|
|
134
|
-
|
|
241
|
+
/** A block; `cell` is the format of the table cell it is in, if any. */
|
|
242
|
+
render(
|
|
243
|
+
node: ModelNode,
|
|
244
|
+
index: number,
|
|
245
|
+
splits: readonly number[] = [],
|
|
246
|
+
cell?: CellFormat | null,
|
|
247
|
+
): WordBlockDesc {
|
|
135
248
|
if (node.type.name !== "paragraph") return this.#opaque(node, index);
|
|
136
249
|
const { resolver } = this.#context;
|
|
137
|
-
const { paragraph, run } = resolver.paragraph(node.attrs);
|
|
250
|
+
const { paragraph, run } = resolver.paragraph(node.attrs, cell ?? undefined);
|
|
138
251
|
const line = (r: RunFormat) => lineHeight(paragraph, r, this.#ratio(fontStack(latin(r))));
|
|
139
252
|
const doc = this.#doc;
|
|
253
|
+
const codes = this.#codes;
|
|
254
|
+
// On the clipboard a block stands by itself.
|
|
255
|
+
const display = this.#clipboard
|
|
256
|
+
? (fieldDisplay([childTypes(node)], false)[0] as FieldDisplay)
|
|
257
|
+
: (this.#fields[index] ?? (fieldDisplay([[]], false)[0] as FieldDisplay));
|
|
140
258
|
const changed = this.#review !== "final" && hasChanges(node);
|
|
259
|
+
// The line's own height is the paragraph mark's, as in `renderBlocks`.
|
|
260
|
+
const mark = resolver.run(run, (node.attrs.markFormat ?? []) as MarkJSON[]);
|
|
141
261
|
const fragments = [0, ...splits].map((_, i, all) => {
|
|
142
262
|
const fragment = doc.createElement(tagOf(paragraph));
|
|
143
263
|
styleParagraph(fragment, paragraph);
|
|
144
|
-
styleRun(fragment,
|
|
264
|
+
styleRun(fragment, mark, line(mark));
|
|
145
265
|
if (all.length > 1) {
|
|
146
266
|
fragment.dataset.fragment = String(i);
|
|
147
267
|
// The first-line indent is the paragraph's first line only.
|
|
@@ -152,6 +272,7 @@ export class WordRenderer {
|
|
|
152
272
|
fragment.style.textAlignLast = "justify";
|
|
153
273
|
}
|
|
154
274
|
if (changed && !this.#clipboard) fragment.style.boxShadow = CHANGE_BAR;
|
|
275
|
+
if (display.gone) fragment.style.display = "none";
|
|
155
276
|
return fragment;
|
|
156
277
|
});
|
|
157
278
|
const dom = fragments[0] as HTMLElement;
|
|
@@ -170,14 +291,27 @@ export class WordRenderer {
|
|
|
170
291
|
return fragments[i] as HTMLElement;
|
|
171
292
|
};
|
|
172
293
|
const leaves: LeafDesc[] = [];
|
|
294
|
+
// Floating pictures go first in the fragment their anchor is in, in order.
|
|
295
|
+
const floats = new Map<HTMLElement, HTMLElement[]>();
|
|
296
|
+
let geometry: FloatGeometry | undefined;
|
|
173
297
|
const comments = new Set(this.#comments[index] ?? []);
|
|
174
|
-
node.forEach((child, offset) => {
|
|
298
|
+
node.forEach((child, offset, childIndex) => {
|
|
175
299
|
const name = child.type.name;
|
|
176
300
|
if (name === "commentStart") comments.add(child.attrs.id as number);
|
|
177
301
|
if (name === "commentEnd") comments.delete(child.attrs.id as number);
|
|
302
|
+
const hidden = display.hidden.has(childIndex);
|
|
303
|
+
/** Where this node's DOM goes: out of sight when it is hidden, as hidden deletions are. */
|
|
304
|
+
const holder = (at: number): HTMLElement => {
|
|
305
|
+
const fragment = fragmentAt(at);
|
|
306
|
+
if (!hidden) return fragment;
|
|
307
|
+
const span = doc.createElement("span");
|
|
308
|
+
span.style.display = "none";
|
|
309
|
+
fragment.append(span);
|
|
310
|
+
return span;
|
|
311
|
+
};
|
|
178
312
|
const leafElement = (element: HTMLElement) => {
|
|
179
313
|
leaves.push({ dom: element, offset, size: child.nodeSize });
|
|
180
|
-
|
|
314
|
+
holder(offset).append(this.#wrap(element, child.marks, comments));
|
|
181
315
|
};
|
|
182
316
|
switch (name) {
|
|
183
317
|
case "text":
|
|
@@ -185,11 +319,11 @@ export class WordRenderer {
|
|
|
185
319
|
case "hardBreak":
|
|
186
320
|
case "columnBreak":
|
|
187
321
|
case "pageBreak": {
|
|
188
|
-
// shortcut: a column break ends the line; Foli lays out one column.
|
|
189
322
|
const br = doc.createElement("br");
|
|
190
323
|
if (name === "pageBreak") br.setAttribute(WORD_PAGE_BREAK_ATTR, "");
|
|
324
|
+
if (name === "columnBreak") br.setAttribute(WORD_COLUMN_BREAK_ATTR, "");
|
|
191
325
|
leaves.push({ dom: br, offset, size: 1 });
|
|
192
|
-
|
|
326
|
+
holder(offset).append(br);
|
|
193
327
|
return;
|
|
194
328
|
}
|
|
195
329
|
case "tab": {
|
|
@@ -198,7 +332,36 @@ export class WordRenderer {
|
|
|
198
332
|
const tab = doc.createTextNode("\t");
|
|
199
333
|
span.append(tab);
|
|
200
334
|
leaves.push({ dom: tab, offset, size: 1 });
|
|
201
|
-
|
|
335
|
+
holder(offset).append(this.#wrap(span, child.marks, comments));
|
|
336
|
+
return;
|
|
337
|
+
}
|
|
338
|
+
case "picture": {
|
|
339
|
+
// Not editable inside (no browser resize handles); one position, like opaque content.
|
|
340
|
+
const span = doc.createElement("span");
|
|
341
|
+
span.setAttribute("contenteditable", "false");
|
|
342
|
+
if (child.attrs.float && !this.#clipboard && !hidden) {
|
|
343
|
+
// Floating: drawn at the fragment's start (ADR-0006), its place in the text an
|
|
344
|
+
// empty anchor.
|
|
345
|
+
geometry ??= floatGeometry(this.#context.envelope.section);
|
|
346
|
+
const { nodes, holder } = floatElements(child.attrs, doc, this.#image, geometry);
|
|
347
|
+
for (const node of nodes) node.setAttribute("contenteditable", "false");
|
|
348
|
+
FLOAT_ANCHORS.set(holder, span);
|
|
349
|
+
const fragment = fragmentAt(offset);
|
|
350
|
+
floats.set(fragment, [...(floats.get(fragment) ?? []), ...nodes]);
|
|
351
|
+
} else span.append(pictureElement(child.attrs, doc, this.#image));
|
|
352
|
+
leafElement(span);
|
|
353
|
+
return;
|
|
354
|
+
}
|
|
355
|
+
case "noteReference":
|
|
356
|
+
case "noteMark": {
|
|
357
|
+
// Drawn, never document text: the number comes from where the note is.
|
|
358
|
+
const span = this.#run(run, child.marks, line);
|
|
359
|
+
const story = String(child.attrs.story);
|
|
360
|
+
span.textContent = name === "noteMark" ? this.#noteMark : (this.#notes.get(story) ?? "");
|
|
361
|
+
span.setAttribute("contenteditable", "false");
|
|
362
|
+
span.setAttribute(NOTE_NUMBER_ATTR, "");
|
|
363
|
+
if (name === "noteReference") span.setAttribute(NOTE_REF_ATTR, story);
|
|
364
|
+
leafElement(span);
|
|
202
365
|
return;
|
|
203
366
|
}
|
|
204
367
|
case "opaqueInline": {
|
|
@@ -211,12 +374,25 @@ export class WordRenderer {
|
|
|
211
374
|
return;
|
|
212
375
|
}
|
|
213
376
|
default: {
|
|
377
|
+
// A field's code and braces, when codes show: drawn, not document text.
|
|
378
|
+
const shown =
|
|
379
|
+
display.braces.get(childIndex) ??
|
|
380
|
+
(codes && !hidden && name === "fieldCode" ? String(child.attrs.text ?? "") : "");
|
|
381
|
+
if (shown) {
|
|
382
|
+
const span = this.#run(run, child.marks, line);
|
|
383
|
+
span.textContent = shown;
|
|
384
|
+
span.dataset.foliFieldCode = "";
|
|
385
|
+
span.setAttribute("contenteditable", "false");
|
|
386
|
+
leaves.push({ dom: span, offset, size: child.nodeSize });
|
|
387
|
+
fragmentAt(offset).append(span);
|
|
388
|
+
return;
|
|
389
|
+
}
|
|
214
390
|
// Bookmarks, comment anchors and reference marks: nothing to see, a place to map to.
|
|
215
391
|
const span = doc.createElement("span");
|
|
216
392
|
if (name === "commentReference")
|
|
217
393
|
span.dataset.foliCommentReference = String(child.attrs.id);
|
|
218
394
|
leaves.push({ dom: span, offset, size: child.nodeSize });
|
|
219
|
-
|
|
395
|
+
holder(offset).append(span);
|
|
220
396
|
return;
|
|
221
397
|
}
|
|
222
398
|
}
|
|
@@ -228,16 +404,37 @@ export class WordRenderer {
|
|
|
228
404
|
leaves.push({ dom: leaf, offset: from, size: to - from });
|
|
229
405
|
const span = this.#run(run, child.marks, line);
|
|
230
406
|
span.append(leaf);
|
|
231
|
-
|
|
407
|
+
holder(from).append(this.#wrap(span, child.marks, comments));
|
|
232
408
|
from = to;
|
|
233
409
|
}
|
|
234
410
|
});
|
|
411
|
+
for (const [fragment, nodes] of floats) fragment.prepend(...nodes);
|
|
412
|
+
const inline: ModelNode[] = [];
|
|
413
|
+
node.forEach((child) => {
|
|
414
|
+
inline.push(child);
|
|
415
|
+
});
|
|
416
|
+
if (
|
|
417
|
+
onlyPictures(
|
|
418
|
+
inline,
|
|
419
|
+
(child) => child.type.name,
|
|
420
|
+
(child) => !!child.attrs.float,
|
|
421
|
+
)
|
|
422
|
+
)
|
|
423
|
+
for (const fragment of fragments)
|
|
424
|
+
for (const picture of fragment.querySelectorAll<HTMLElement>(`[${PICTURE_ATTR}]`))
|
|
425
|
+
picture.style.verticalAlign = "bottom";
|
|
235
426
|
// An empty paragraph, or one ending in a line break, shows its last line; after a page
|
|
236
427
|
// break the paragraph mark stays on the break's line, as in Word.
|
|
237
428
|
let last: ModelNode | null = null;
|
|
238
429
|
for (let i = node.childCount - 1; i >= 0 && !last; i--) {
|
|
239
430
|
const child = node.child(i);
|
|
240
|
-
if (
|
|
431
|
+
if (display.hidden.has(i)) continue;
|
|
432
|
+
// A floating picture is not in the line: a paragraph holding only one shows its empty line.
|
|
433
|
+
if (
|
|
434
|
+
display.braces.has(i) ||
|
|
435
|
+
(codes && child.type.name === "fieldCode") ||
|
|
436
|
+
(!NOT_IN_LINE.has(child.type.name) && !(child.type.name === "picture" && child.attrs.float))
|
|
437
|
+
)
|
|
241
438
|
last = child;
|
|
242
439
|
}
|
|
243
440
|
if (!last || last.type.name === "hardBreak" || last.type.name === "columnBreak")
|
|
@@ -245,14 +442,22 @@ export class WordRenderer {
|
|
|
245
442
|
const { before, after } = paragraph.spacing;
|
|
246
443
|
// Word's automatic spacing (HTML): 14 pt.
|
|
247
444
|
const word: WordInfo = {
|
|
248
|
-
before: (paragraph.spacing.beforeAutospacing ? 280 : before) * PX_PER_TWIP,
|
|
249
|
-
after: (paragraph.spacing.afterAutospacing ? 280 : after) * PX_PER_TWIP,
|
|
445
|
+
before: display.gone ? 0 : (paragraph.spacing.beforeAutospacing ? 280 : before) * PX_PER_TWIP,
|
|
446
|
+
after: display.gone ? 0 : (paragraph.spacing.afterAutospacing ? 280 : after) * PX_PER_TWIP,
|
|
250
447
|
keepNext: paragraph.keepNext,
|
|
251
448
|
keepLines: paragraph.keepLines,
|
|
252
449
|
pageBreakBefore: paragraph.pageBreakBefore,
|
|
253
450
|
style: paragraph.style,
|
|
254
451
|
contextual: paragraph.contextualSpacing,
|
|
452
|
+
empty: !last && !marker,
|
|
255
453
|
};
|
|
454
|
+
if (cell && !this.#clipboard) {
|
|
455
|
+
// In a cell its spacing is its margins (the page flow spaces top-level blocks); a
|
|
456
|
+
// fragment continued on the next page has none at the split.
|
|
457
|
+
// shortcut: contextual spacing between paragraphs in a cell is not applied.
|
|
458
|
+
dom.style.marginTop = `${marginAbove(word.before, this.#above[index] ?? 0, resolver)}px`;
|
|
459
|
+
(fragments[fragments.length - 1] as HTMLElement).style.marginBottom = `${word.after}px`;
|
|
460
|
+
}
|
|
256
461
|
if (this.#clipboard) {
|
|
257
462
|
// The flow places the spacing in the editor; elsewhere it is the paragraph's margins.
|
|
258
463
|
dom.style.marginTop = `${word.before}px`;
|
|
@@ -264,7 +469,7 @@ export class WordRenderer {
|
|
|
264
469
|
dom.setAttribute("style", `${styleOf(dom)}mso-list:${list}`);
|
|
265
470
|
}
|
|
266
471
|
}
|
|
267
|
-
return { node, dom, fragments, splits, leaves, word, key: this.key(index) };
|
|
472
|
+
return { node, dom, fragments, splits, leaves, word, key: this.key(index, cell) };
|
|
268
473
|
}
|
|
269
474
|
|
|
270
475
|
/** A span for text with `marks` over its paragraph's run formatting. */
|
|
@@ -313,7 +518,15 @@ export class WordRenderer {
|
|
|
313
518
|
|
|
314
519
|
/** An opaque block, shown from its XML: a block leaf, not editable inside. */
|
|
315
520
|
#opaque(node: ModelNode, index: number): WordBlockDesc {
|
|
316
|
-
const shown = opaqueBlock(
|
|
521
|
+
const shown = opaqueBlock(
|
|
522
|
+
node.toJSON(),
|
|
523
|
+
index,
|
|
524
|
+
this.#context.resolver,
|
|
525
|
+
this.#doc,
|
|
526
|
+
this.#ratio,
|
|
527
|
+
this.#image,
|
|
528
|
+
floatGeometry(this.#context.envelope.section),
|
|
529
|
+
);
|
|
317
530
|
const dom = shown.element;
|
|
318
531
|
dom.setAttribute("contenteditable", "false");
|
|
319
532
|
return {
|
|
@@ -331,6 +544,7 @@ export class WordRenderer {
|
|
|
331
544
|
pageBreakBefore: false,
|
|
332
545
|
style: null,
|
|
333
546
|
contextual: false,
|
|
547
|
+
empty: false,
|
|
334
548
|
},
|
|
335
549
|
};
|
|
336
550
|
}
|
|
@@ -356,7 +570,11 @@ export function clipboardBlocks(
|
|
|
356
570
|
ratio: (family: string) => number,
|
|
357
571
|
): HTMLElement[] {
|
|
358
572
|
const renderer = new WordRenderer(context, ownerDocument, ratio, true);
|
|
359
|
-
|
|
573
|
+
const top: ModelNode[] = [];
|
|
574
|
+
doc.forEach((node) => {
|
|
575
|
+
top.push(node);
|
|
576
|
+
});
|
|
577
|
+
renderer.prepare(top);
|
|
360
578
|
return nodes.map((node, i) => {
|
|
361
579
|
const { dom } = renderer.render(node, first < 0 ? -1 : first + i);
|
|
362
580
|
for (const element of [dom, ...dom.querySelectorAll("*")])
|
|
@@ -372,16 +590,21 @@ export function clipboardBlocks(
|
|
|
372
590
|
|
|
373
591
|
/**
|
|
374
592
|
* A block's space before and after next to its neighbours: Word ignores the space between
|
|
375
|
-
* paragraphs of the same style that ask for it (contextual spacing).
|
|
593
|
+
* paragraphs of the same style that ask for it (contextual spacing). When only the first asks,
|
|
594
|
+
* its space after still eats into the second's space before, unless the document adds the two
|
|
595
|
+
* (`adds`).
|
|
376
596
|
*/
|
|
377
597
|
export function spacing(
|
|
378
598
|
above: WordInfo | undefined,
|
|
379
599
|
block: WordInfo,
|
|
380
600
|
below: WordInfo | undefined,
|
|
601
|
+
adds = false,
|
|
381
602
|
): { before: number; after: number } {
|
|
382
603
|
const same = (other: WordInfo | undefined) => other !== undefined && other.style === block.style;
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
604
|
+
let { before } = block;
|
|
605
|
+
if (above && same(above)) {
|
|
606
|
+
if (block.contextual) before = 0;
|
|
607
|
+
else if (above.contextual && !adds) before = Math.max(0, block.before - above.after);
|
|
608
|
+
}
|
|
609
|
+
return { before, after: same(below) && block.contextual ? 0 : block.after };
|
|
387
610
|
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What find marks on the pages (11.11), through the CSS Custom Highlight API: ranges kept beside
|
|
3
|
+
* the DOM, so nothing is added to the pages, the document or what the view reads back.
|
|
4
|
+
* shortcut: a browser without the API marks nothing (the current match is still selected and
|
|
5
|
+
* scrolled to on Next); draw boxes if one that matters lacks it.
|
|
6
|
+
*/
|
|
7
|
+
import { type FindOptions, findInText } from "@folijs/core";
|
|
8
|
+
import { PAGE_FIELD_ATTR } from "./regions.js";
|
|
9
|
+
|
|
10
|
+
const ALL = "foli-find";
|
|
11
|
+
const CURRENT = "foli-find-current";
|
|
12
|
+
|
|
13
|
+
export const MATCH_STYLES =
|
|
14
|
+
`::highlight(${ALL}){background-color:#ffe17a}` +
|
|
15
|
+
`::highlight(${CURRENT}){background-color:#f5a04a}` +
|
|
16
|
+
`@media print{::highlight(${ALL}),::highlight(${CURRENT}){background-color:transparent}}` +
|
|
17
|
+
`@media (forced-colors:active){::highlight(${ALL}){background-color:Mark;color:MarkText}` +
|
|
18
|
+
`::highlight(${CURRENT}){background-color:Highlight;color:HighlightText}}`;
|
|
19
|
+
|
|
20
|
+
interface HighlightWindow {
|
|
21
|
+
CSS?: { highlights?: Map<string, Set<Range> & { priority: number }> };
|
|
22
|
+
Highlight?: new () => Set<Range> & { priority: number };
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** One view's marks; views share the document's two highlights. */
|
|
26
|
+
export class MatchMarks {
|
|
27
|
+
private all: Range[] = [];
|
|
28
|
+
private current: Range[] = [];
|
|
29
|
+
|
|
30
|
+
constructor(private readonly doc: Document) {}
|
|
31
|
+
|
|
32
|
+
private highlight(name: string, priority: number) {
|
|
33
|
+
const win = this.doc.defaultView as unknown as HighlightWindow | null;
|
|
34
|
+
const registry = win?.CSS?.highlights;
|
|
35
|
+
if (!registry || !win?.Highlight) return null;
|
|
36
|
+
let found = registry.get(name);
|
|
37
|
+
if (!found) {
|
|
38
|
+
found = new win.Highlight();
|
|
39
|
+
found.priority = priority;
|
|
40
|
+
registry.set(name, found);
|
|
41
|
+
}
|
|
42
|
+
return found;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
set(all: Range[], current: Range[]): void {
|
|
46
|
+
const allMarks = this.highlight(ALL, 0);
|
|
47
|
+
const currentMarks = this.highlight(CURRENT, 1);
|
|
48
|
+
if (!allMarks || !currentMarks) return;
|
|
49
|
+
for (const range of this.all) allMarks.delete(range);
|
|
50
|
+
for (const range of this.current) currentMarks.delete(range);
|
|
51
|
+
for (const range of all) allMarks.add(range);
|
|
52
|
+
for (const range of current) currentMarks.add(range);
|
|
53
|
+
this.all = all;
|
|
54
|
+
this.current = current;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Where `query` is in drawn copies of a story (read-only, with their pages' numbers filled in):
|
|
60
|
+
* looked for in each paragraph's text, as the text shown there has no positions.
|
|
61
|
+
*/
|
|
62
|
+
export function rangesInText(root: Element, query: string, options: FindOptions): Range[] {
|
|
63
|
+
const doc = root.ownerDocument;
|
|
64
|
+
const groups = new Map<Element, Text[]>();
|
|
65
|
+
const walker = doc.createTreeWalker(root, NodeFilter.SHOW_TEXT);
|
|
66
|
+
for (let node = walker.nextNode(); node; node = walker.nextNode()) {
|
|
67
|
+
const block = node.parentElement?.closest("p,h1,h2,h3,h4,h5,h6");
|
|
68
|
+
if (!block) continue;
|
|
69
|
+
const list = groups.get(block);
|
|
70
|
+
if (list) list.push(node as Text);
|
|
71
|
+
else groups.set(block, [node as Text]);
|
|
72
|
+
}
|
|
73
|
+
const out: Range[] = [];
|
|
74
|
+
for (const nodes of groups.values()) {
|
|
75
|
+
// A page number drawn there is no text of the story: nothing runs across it.
|
|
76
|
+
const text = nodes
|
|
77
|
+
.map((node) =>
|
|
78
|
+
node.parentElement?.closest(`[${PAGE_FIELD_ATTR}]`)
|
|
79
|
+
? "\uFFFC".repeat(node.length)
|
|
80
|
+
: node.data,
|
|
81
|
+
)
|
|
82
|
+
.join("");
|
|
83
|
+
for (const [from, to] of findInText(text, query, options)) {
|
|
84
|
+
let at = 0;
|
|
85
|
+
for (const node of nodes) {
|
|
86
|
+
const start = Math.max(from - at, 0);
|
|
87
|
+
const end = Math.min(to - at, node.length);
|
|
88
|
+
if (start < end) {
|
|
89
|
+
const range = doc.createRange();
|
|
90
|
+
range.setStart(node, start);
|
|
91
|
+
range.setEnd(node, end);
|
|
92
|
+
out.push(range);
|
|
93
|
+
}
|
|
94
|
+
at += node.length;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return out;
|
|
99
|
+
}
|