@folijs/view 0.5.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 +33 -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/layout/flow.d.ts +193 -20
- package/dist/layout/flow.d.ts.map +1 -1
- package/dist/layout/flow.js +789 -101
- 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 +220 -7
- package/dist/view.d.ts.map +1 -1
- package/dist/view.js +1753 -208
- package/dist/view.js.map +1 -1
- package/dist/word/blocks.d.ts +30 -1
- package/dist/word/blocks.d.ts.map +1 -1
- package/dist/word/blocks.js +193 -40
- 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 +17 -5
- package/dist/word/edit.d.ts.map +1 -1
- package/dist/word/edit.js +157 -26
- 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 +18 -13
- package/dist/word/pages.d.ts.map +1 -1
- package/dist/word/pages.js +420 -229
- 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/package.json +2 -2
- package/src/clipboard.ts +3 -0
- package/src/layout/flow.ts +945 -110
- package/src/print.ts +48 -0
- package/src/view.ts +1957 -229
- package/src/word/blocks.ts +219 -36
- package/src/word/columns.ts +66 -0
- package/src/word/controls.ts +447 -0
- package/src/word/edit.ts +175 -23
- package/src/word/matches.ts +99 -0
- package/src/word/notes.ts +451 -0
- package/src/word/pages.ts +501 -220
- package/src/word/regions.ts +469 -0
package/src/word/edit.ts
CHANGED
|
@@ -11,12 +11,15 @@
|
|
|
11
11
|
*/
|
|
12
12
|
import {
|
|
13
13
|
type CellFormat,
|
|
14
|
+
type FieldDisplay,
|
|
15
|
+
fieldDisplay,
|
|
14
16
|
isSafeHref,
|
|
15
17
|
type ListMarker,
|
|
16
18
|
type Mark,
|
|
17
19
|
type MarkJSON,
|
|
18
20
|
type Node as ModelNode,
|
|
19
21
|
type RunFormat,
|
|
22
|
+
referencesIn,
|
|
20
23
|
type WordContext,
|
|
21
24
|
} from "@folijs/core";
|
|
22
25
|
import type { BlockDesc, LeafDesc } from "../render.js";
|
|
@@ -31,9 +34,14 @@ import {
|
|
|
31
34
|
imageSource,
|
|
32
35
|
latin,
|
|
33
36
|
lineHeight,
|
|
37
|
+
marginAbove,
|
|
34
38
|
markerElement,
|
|
39
|
+
NOTE_NUMBER_ATTR,
|
|
40
|
+
NOTE_REF_ATTR,
|
|
41
|
+
onlyPictures,
|
|
35
42
|
opaqueBlock,
|
|
36
43
|
opaqueText,
|
|
44
|
+
PICTURE_ATTR,
|
|
37
45
|
PX_PER_TWIP,
|
|
38
46
|
parseOpaque,
|
|
39
47
|
pictureElement,
|
|
@@ -41,9 +49,24 @@ import {
|
|
|
41
49
|
styleParagraph,
|
|
42
50
|
styleRun,
|
|
43
51
|
tagOf,
|
|
52
|
+
WORD_COLUMN_BREAK_ATTR,
|
|
44
53
|
WORD_PAGE_BREAK_ATTR,
|
|
45
54
|
} from "./blocks.js";
|
|
46
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
|
+
|
|
47
70
|
const REVIEW_MARKS = new Set(["insertion", "deletion", "formatChange"]);
|
|
48
71
|
|
|
49
72
|
/** Whether a paragraph has tracked changes: in its text, its mark or its formatting. */
|
|
@@ -67,6 +90,18 @@ export interface WordInfo {
|
|
|
67
90
|
/** The paragraph's style and whether it drops spacing next to the same style. */
|
|
68
91
|
style: string | null;
|
|
69
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;
|
|
70
105
|
}
|
|
71
106
|
|
|
72
107
|
/** A rendered v3 block: a view block plus its flow information and what it was rendered for. */
|
|
@@ -87,7 +122,7 @@ export const FLOAT_ANCHORS = new WeakMap<Element, HTMLElement>();
|
|
|
87
122
|
* render: list numbers and comment ranges depend on the blocks before.
|
|
88
123
|
*/
|
|
89
124
|
export class WordRenderer {
|
|
90
|
-
|
|
125
|
+
#context: WordContext;
|
|
91
126
|
readonly #doc: Document;
|
|
92
127
|
readonly #ratio: (family: string) => number;
|
|
93
128
|
/** For the clipboard (`clipboardBlocks`): links as links, list numbers as Word's HTML has them. */
|
|
@@ -99,6 +134,17 @@ export class WordRenderer {
|
|
|
99
134
|
#markers: (ListMarker | null)[] = [];
|
|
100
135
|
/** Comment ranges open at the start of each block. */
|
|
101
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[] = [];
|
|
102
148
|
|
|
103
149
|
constructor(
|
|
104
150
|
context: WordContext,
|
|
@@ -107,7 +153,9 @@ export class WordRenderer {
|
|
|
107
153
|
clipboard = false,
|
|
108
154
|
review: ReviewView = "markup",
|
|
109
155
|
image?: ImageSource,
|
|
156
|
+
codes = false,
|
|
110
157
|
) {
|
|
158
|
+
this.#codes = codes;
|
|
111
159
|
this.#context = context;
|
|
112
160
|
this.#doc = doc;
|
|
113
161
|
this.#ratio = ratio;
|
|
@@ -120,11 +168,36 @@ export class WordRenderer {
|
|
|
120
168
|
return this.#context;
|
|
121
169
|
}
|
|
122
170
|
|
|
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
|
+
|
|
123
182
|
/**
|
|
124
183
|
* Numbering and comment state for `blocks`, the blocks rendered, in document order (the
|
|
125
|
-
* paragraphs in table cells among them: Word numbers lists through tables).
|
|
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.
|
|
126
186
|
*/
|
|
127
|
-
prepare(
|
|
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
|
+
: [];
|
|
128
201
|
const attrs: Record<string, unknown>[] = [];
|
|
129
202
|
const comments: number[][] = [];
|
|
130
203
|
const open = new Set<number>();
|
|
@@ -139,6 +212,14 @@ export class WordRenderer {
|
|
|
139
212
|
}
|
|
140
213
|
this.#markers = this.#context.resolver.markers(attrs);
|
|
141
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
|
+
: [];
|
|
142
223
|
}
|
|
143
224
|
|
|
144
225
|
/** Whether the block at `index` (of those prepared) shows a list number. */
|
|
@@ -149,7 +230,12 @@ export class WordRenderer {
|
|
|
149
230
|
/** What a block's rendering depends on at `index`, besides its node (and its cell's format). */
|
|
150
231
|
key(index: number, cell?: CellFormat | null): string {
|
|
151
232
|
const inCell = cell ? `|${cell.style ?? ""}:${cell.conditions.join(",")}` : "";
|
|
152
|
-
|
|
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}`;
|
|
153
239
|
}
|
|
154
240
|
|
|
155
241
|
/** A block; `cell` is the format of the table cell it is in, if any. */
|
|
@@ -164,11 +250,18 @@ export class WordRenderer {
|
|
|
164
250
|
const { paragraph, run } = resolver.paragraph(node.attrs, cell ?? undefined);
|
|
165
251
|
const line = (r: RunFormat) => lineHeight(paragraph, r, this.#ratio(fontStack(latin(r))));
|
|
166
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));
|
|
167
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[]);
|
|
168
261
|
const fragments = [0, ...splits].map((_, i, all) => {
|
|
169
262
|
const fragment = doc.createElement(tagOf(paragraph));
|
|
170
263
|
styleParagraph(fragment, paragraph);
|
|
171
|
-
styleRun(fragment,
|
|
264
|
+
styleRun(fragment, mark, line(mark));
|
|
172
265
|
if (all.length > 1) {
|
|
173
266
|
fragment.dataset.fragment = String(i);
|
|
174
267
|
// The first-line indent is the paragraph's first line only.
|
|
@@ -179,6 +272,7 @@ export class WordRenderer {
|
|
|
179
272
|
fragment.style.textAlignLast = "justify";
|
|
180
273
|
}
|
|
181
274
|
if (changed && !this.#clipboard) fragment.style.boxShadow = CHANGE_BAR;
|
|
275
|
+
if (display.gone) fragment.style.display = "none";
|
|
182
276
|
return fragment;
|
|
183
277
|
});
|
|
184
278
|
const dom = fragments[0] as HTMLElement;
|
|
@@ -201,13 +295,23 @@ export class WordRenderer {
|
|
|
201
295
|
const floats = new Map<HTMLElement, HTMLElement[]>();
|
|
202
296
|
let geometry: FloatGeometry | undefined;
|
|
203
297
|
const comments = new Set(this.#comments[index] ?? []);
|
|
204
|
-
node.forEach((child, offset) => {
|
|
298
|
+
node.forEach((child, offset, childIndex) => {
|
|
205
299
|
const name = child.type.name;
|
|
206
300
|
if (name === "commentStart") comments.add(child.attrs.id as number);
|
|
207
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
|
+
};
|
|
208
312
|
const leafElement = (element: HTMLElement) => {
|
|
209
313
|
leaves.push({ dom: element, offset, size: child.nodeSize });
|
|
210
|
-
|
|
314
|
+
holder(offset).append(this.#wrap(element, child.marks, comments));
|
|
211
315
|
};
|
|
212
316
|
switch (name) {
|
|
213
317
|
case "text":
|
|
@@ -215,11 +319,11 @@ export class WordRenderer {
|
|
|
215
319
|
case "hardBreak":
|
|
216
320
|
case "columnBreak":
|
|
217
321
|
case "pageBreak": {
|
|
218
|
-
// shortcut: a column break ends the line; Foli lays out one column.
|
|
219
322
|
const br = doc.createElement("br");
|
|
220
323
|
if (name === "pageBreak") br.setAttribute(WORD_PAGE_BREAK_ATTR, "");
|
|
324
|
+
if (name === "columnBreak") br.setAttribute(WORD_COLUMN_BREAK_ATTR, "");
|
|
221
325
|
leaves.push({ dom: br, offset, size: 1 });
|
|
222
|
-
|
|
326
|
+
holder(offset).append(br);
|
|
223
327
|
return;
|
|
224
328
|
}
|
|
225
329
|
case "tab": {
|
|
@@ -228,14 +332,14 @@ export class WordRenderer {
|
|
|
228
332
|
const tab = doc.createTextNode("\t");
|
|
229
333
|
span.append(tab);
|
|
230
334
|
leaves.push({ dom: tab, offset, size: 1 });
|
|
231
|
-
|
|
335
|
+
holder(offset).append(this.#wrap(span, child.marks, comments));
|
|
232
336
|
return;
|
|
233
337
|
}
|
|
234
338
|
case "picture": {
|
|
235
339
|
// Not editable inside (no browser resize handles); one position, like opaque content.
|
|
236
340
|
const span = doc.createElement("span");
|
|
237
341
|
span.setAttribute("contenteditable", "false");
|
|
238
|
-
if (child.attrs.float && !this.#clipboard) {
|
|
342
|
+
if (child.attrs.float && !this.#clipboard && !hidden) {
|
|
239
343
|
// Floating: drawn at the fragment's start (ADR-0006), its place in the text an
|
|
240
344
|
// empty anchor.
|
|
241
345
|
geometry ??= floatGeometry(this.#context.envelope.section);
|
|
@@ -248,6 +352,18 @@ export class WordRenderer {
|
|
|
248
352
|
leafElement(span);
|
|
249
353
|
return;
|
|
250
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);
|
|
365
|
+
return;
|
|
366
|
+
}
|
|
251
367
|
case "opaqueInline": {
|
|
252
368
|
const span = this.#run(run, child.marks, line);
|
|
253
369
|
const xml = parseOpaque(String(child.attrs.xml ?? ""), doc);
|
|
@@ -258,12 +374,25 @@ export class WordRenderer {
|
|
|
258
374
|
return;
|
|
259
375
|
}
|
|
260
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
|
+
}
|
|
261
390
|
// Bookmarks, comment anchors and reference marks: nothing to see, a place to map to.
|
|
262
391
|
const span = doc.createElement("span");
|
|
263
392
|
if (name === "commentReference")
|
|
264
393
|
span.dataset.foliCommentReference = String(child.attrs.id);
|
|
265
394
|
leaves.push({ dom: span, offset, size: child.nodeSize });
|
|
266
|
-
|
|
395
|
+
holder(offset).append(span);
|
|
267
396
|
return;
|
|
268
397
|
}
|
|
269
398
|
}
|
|
@@ -275,20 +404,36 @@ export class WordRenderer {
|
|
|
275
404
|
leaves.push({ dom: leaf, offset: from, size: to - from });
|
|
276
405
|
const span = this.#run(run, child.marks, line);
|
|
277
406
|
span.append(leaf);
|
|
278
|
-
|
|
407
|
+
holder(from).append(this.#wrap(span, child.marks, comments));
|
|
279
408
|
from = to;
|
|
280
409
|
}
|
|
281
410
|
});
|
|
282
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";
|
|
283
426
|
// An empty paragraph, or one ending in a line break, shows its last line; after a page
|
|
284
427
|
// break the paragraph mark stays on the break's line, as in Word.
|
|
285
428
|
let last: ModelNode | null = null;
|
|
286
429
|
for (let i = node.childCount - 1; i >= 0 && !last; i--) {
|
|
287
430
|
const child = node.child(i);
|
|
431
|
+
if (display.hidden.has(i)) continue;
|
|
288
432
|
// A floating picture is not in the line: a paragraph holding only one shows its empty line.
|
|
289
433
|
if (
|
|
290
|
-
|
|
291
|
-
|
|
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))
|
|
292
437
|
)
|
|
293
438
|
last = child;
|
|
294
439
|
}
|
|
@@ -297,19 +442,20 @@ export class WordRenderer {
|
|
|
297
442
|
const { before, after } = paragraph.spacing;
|
|
298
443
|
// Word's automatic spacing (HTML): 14 pt.
|
|
299
444
|
const word: WordInfo = {
|
|
300
|
-
before: (paragraph.spacing.beforeAutospacing ? 280 : before) * PX_PER_TWIP,
|
|
301
|
-
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,
|
|
302
447
|
keepNext: paragraph.keepNext,
|
|
303
448
|
keepLines: paragraph.keepLines,
|
|
304
449
|
pageBreakBefore: paragraph.pageBreakBefore,
|
|
305
450
|
style: paragraph.style,
|
|
306
451
|
contextual: paragraph.contextualSpacing,
|
|
452
|
+
empty: !last && !marker,
|
|
307
453
|
};
|
|
308
454
|
if (cell && !this.#clipboard) {
|
|
309
455
|
// In a cell its spacing is its margins (the page flow spaces top-level blocks); a
|
|
310
456
|
// fragment continued on the next page has none at the split.
|
|
311
457
|
// shortcut: contextual spacing between paragraphs in a cell is not applied.
|
|
312
|
-
dom.style.marginTop = `${word.before}px`;
|
|
458
|
+
dom.style.marginTop = `${marginAbove(word.before, this.#above[index] ?? 0, resolver)}px`;
|
|
313
459
|
(fragments[fragments.length - 1] as HTMLElement).style.marginBottom = `${word.after}px`;
|
|
314
460
|
}
|
|
315
461
|
if (this.#clipboard) {
|
|
@@ -398,6 +544,7 @@ export class WordRenderer {
|
|
|
398
544
|
pageBreakBefore: false,
|
|
399
545
|
style: null,
|
|
400
546
|
contextual: false,
|
|
547
|
+
empty: false,
|
|
401
548
|
},
|
|
402
549
|
};
|
|
403
550
|
}
|
|
@@ -443,16 +590,21 @@ export function clipboardBlocks(
|
|
|
443
590
|
|
|
444
591
|
/**
|
|
445
592
|
* A block's space before and after next to its neighbours: Word ignores the space between
|
|
446
|
-
* 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`).
|
|
447
596
|
*/
|
|
448
597
|
export function spacing(
|
|
449
598
|
above: WordInfo | undefined,
|
|
450
599
|
block: WordInfo,
|
|
451
600
|
below: WordInfo | undefined,
|
|
601
|
+
adds = false,
|
|
452
602
|
): { before: number; after: number } {
|
|
453
603
|
const same = (other: WordInfo | undefined) => other !== undefined && other.style === block.style;
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
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 };
|
|
458
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
|
+
}
|