@folijs/view 0.3.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 +27 -0
- package/dist/view.d.ts +2 -0
- package/dist/view.d.ts.map +1 -1
- package/dist/view.js +35 -8
- package/dist/view.js.map +1 -1
- package/dist/word/blocks.d.ts +14 -2
- package/dist/word/blocks.d.ts.map +1 -1
- package/dist/word/blocks.js +49 -11
- package/dist/word/blocks.js.map +1 -1
- package/dist/word/edit.d.ts +4 -2
- package/dist/word/edit.d.ts.map +1 -1
- package/dist/word/edit.js +37 -15
- package/dist/word/edit.js.map +1 -1
- package/package.json +2 -2
- package/src/view.ts +46 -7
- package/src/word/blocks.ts +61 -12
- package/src/word/edit.ts +39 -12
package/src/word/blocks.ts
CHANGED
|
@@ -22,10 +22,60 @@ export const WORD_PAGE_BREAK_ATTR = "data-page-break";
|
|
|
22
22
|
export const PX_PER_TWIP = 96 / 1440;
|
|
23
23
|
|
|
24
24
|
/** Which version of tracked changes to show. */
|
|
25
|
-
|
|
25
|
+
/** How tracked changes show (`@folijs/core`'s, for `renderDocument` too). */
|
|
26
|
+
export type { ReviewView } from "@folijs/core";
|
|
27
|
+
|
|
28
|
+
import type { ReviewView } from "@folijs/core";
|
|
29
|
+
|
|
30
|
+
/** Colors for the authors of changes (contrast 4.5:1 or more on white), picked by name. */
|
|
31
|
+
const AUTHOR_COLORS = [
|
|
32
|
+
"#B5082E",
|
|
33
|
+
"#1F5FAD",
|
|
34
|
+
"#2E7D32",
|
|
35
|
+
"#8E24AA",
|
|
36
|
+
"#A64B00",
|
|
37
|
+
"#00737F",
|
|
38
|
+
"#AD1457",
|
|
39
|
+
"#5D4037",
|
|
40
|
+
];
|
|
41
|
+
|
|
42
|
+
/** The color of `author`'s changes: always the same for a name. */
|
|
43
|
+
export function authorColor(author: string): string {
|
|
44
|
+
let hash = 0;
|
|
45
|
+
for (const c of author) hash = (hash * 31 + (c.codePointAt(0) ?? 0)) >>> 0;
|
|
46
|
+
return AUTHOR_COLORS[hash % AUTHOR_COLORS.length] as string;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** The bar beside a paragraph with changes (`markup` and `simple`), drawn in the margin. */
|
|
50
|
+
export const CHANGE_BAR = "-4px 0 0 0 #6B6B6B";
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* The element around an inserted or deleted run: `ins` or `del` (named for screen readers by
|
|
54
|
+
* its title), styled for `review`. Deleted text not shown is hidden, not left out: the editable
|
|
55
|
+
* view maps positions through it.
|
|
56
|
+
*/
|
|
57
|
+
export function changeElement(
|
|
58
|
+
doc: Document,
|
|
59
|
+
inserted: boolean,
|
|
60
|
+
author: string,
|
|
61
|
+
date: string | null,
|
|
62
|
+
review: ReviewView,
|
|
63
|
+
): HTMLElement {
|
|
64
|
+
const element = doc.createElement(inserted ? "ins" : "del");
|
|
65
|
+
const when = date ? `, ${date.slice(0, 10)}` : "";
|
|
66
|
+
element.title = `${inserted ? "Inserted" : "Deleted"} by ${author || "an unknown author"}${when}`;
|
|
67
|
+
if (review === "markup") {
|
|
68
|
+
element.style.color = authorColor(author);
|
|
69
|
+
element.style.textDecorationLine = inserted ? "underline" : "line-through";
|
|
70
|
+
} else {
|
|
71
|
+
element.style.textDecorationLine = "none";
|
|
72
|
+
if (!inserted) element.style.display = "none";
|
|
73
|
+
}
|
|
74
|
+
return element;
|
|
75
|
+
}
|
|
26
76
|
|
|
27
77
|
export interface BlockOptions {
|
|
28
|
-
/** `markup` (the default): insertions underlined, deletions struck through
|
|
78
|
+
/** `markup` (the default): insertions underlined, deletions struck through; `simple`, `final`: as if accepted. */
|
|
29
79
|
review?: ReviewView;
|
|
30
80
|
/**
|
|
31
81
|
* The ratio of a font's normal line height to its size (`fontFamily`: a CSS font-family
|
|
@@ -288,13 +338,13 @@ export function renderBlocks(
|
|
|
288
338
|
const resolver = new Resolver(document);
|
|
289
339
|
const content = document.content.content ?? [];
|
|
290
340
|
const attrsOf = (node: NodeJSON) => (node.attrs ?? {}) as Record<string, unknown>;
|
|
291
|
-
// In the final
|
|
292
|
-
// table, and the document's last mark cannot be deleted).
|
|
341
|
+
// In the final and simple views a paragraph whose mark was deleted joins the next paragraph
|
|
342
|
+
// (not a table, and the document's last mark cannot be deleted).
|
|
293
343
|
const joins = (index: number) => {
|
|
294
344
|
const node = content[index];
|
|
295
345
|
const revision = node && (attrsOf(node).revision as { deletion?: unknown } | null | undefined);
|
|
296
346
|
return (
|
|
297
|
-
review
|
|
347
|
+
review !== "markup" &&
|
|
298
348
|
node?.type === "paragraph" &&
|
|
299
349
|
!!revision?.deletion &&
|
|
300
350
|
content[index + 1]?.type === "paragraph"
|
|
@@ -467,7 +517,7 @@ function renderInline(
|
|
|
467
517
|
const marks = (node.marks ?? []) as MarkJSON[];
|
|
468
518
|
const inserted = marks.some((m) => m.type === "insertion");
|
|
469
519
|
const deleted = marks.some((m) => m.type === "deletion");
|
|
470
|
-
if (deleted && review
|
|
520
|
+
if (deleted && review !== "markup") continue;
|
|
471
521
|
const attrs = (node.attrs ?? {}) as Record<string, unknown>;
|
|
472
522
|
switch (node.type) {
|
|
473
523
|
case "commentStart":
|
|
@@ -525,12 +575,11 @@ function renderInline(
|
|
|
525
575
|
element.style.backgroundColor = "rgba(255, 213, 79, 0.35)";
|
|
526
576
|
}
|
|
527
577
|
if (review === "markup" && (inserted || deleted)) {
|
|
528
|
-
|
|
529
|
-
const
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
wrapper
|
|
533
|
-
wrapper.style.textDecorationLine = inserted ? "underline" : "line-through";
|
|
578
|
+
// Inserted, then deleted: shown as deleted, in the deleting author's color.
|
|
579
|
+
const change = marks.find((m) => m.type === (deleted ? "deletion" : "insertion"))?.attrs;
|
|
580
|
+
const author = String(change?.author ?? "");
|
|
581
|
+
const date = (change?.date as string | null | undefined) ?? null;
|
|
582
|
+
const wrapper = changeElement(doc, !deleted, author, date, review);
|
|
534
583
|
wrapper.append(element);
|
|
535
584
|
element = wrapper;
|
|
536
585
|
}
|
package/src/word/edit.ts
CHANGED
|
@@ -4,7 +4,8 @@
|
|
|
4
4
|
* fragments at content offsets like the v2 blocks (`render.ts`), and what the page flow needs
|
|
5
5
|
* of each: Word's paragraph spacing and keep rules. Built with createElement/textContent only.
|
|
6
6
|
*
|
|
7
|
-
* Tracked changes
|
|
7
|
+
* Tracked changes show as the view's review mode says (deleted text hidden, not left out, in the
|
|
8
|
+
* simple and final views: positions map through it). Content the model
|
|
8
9
|
* keeps opaque is shown from its XML and cannot be edited: an opaque block is a block leaf, an
|
|
9
10
|
* opaque inline node a leaf element.
|
|
10
11
|
*/
|
|
@@ -19,6 +20,8 @@ import {
|
|
|
19
20
|
} from "@folijs/core";
|
|
20
21
|
import type { BlockDesc, LeafDesc } from "../render.js";
|
|
21
22
|
import {
|
|
23
|
+
CHANGE_BAR,
|
|
24
|
+
changeElement,
|
|
22
25
|
fontStack,
|
|
23
26
|
latin,
|
|
24
27
|
lineHeight,
|
|
@@ -27,12 +30,25 @@ import {
|
|
|
27
30
|
opaqueText,
|
|
28
31
|
PX_PER_TWIP,
|
|
29
32
|
parseOpaque,
|
|
33
|
+
type ReviewView,
|
|
30
34
|
styleParagraph,
|
|
31
35
|
styleRun,
|
|
32
36
|
tagOf,
|
|
33
37
|
WORD_PAGE_BREAK_ATTR,
|
|
34
38
|
} from "./blocks.js";
|
|
35
39
|
|
|
40
|
+
const REVIEW_MARKS = new Set(["insertion", "deletion", "formatChange"]);
|
|
41
|
+
|
|
42
|
+
/** Whether a paragraph has tracked changes: in its text, its mark or its formatting. */
|
|
43
|
+
function hasChanges(node: ModelNode): boolean {
|
|
44
|
+
if (node.attrs.revision || node.attrs.formatChange) return true;
|
|
45
|
+
let found = false;
|
|
46
|
+
node.forEach((child) => {
|
|
47
|
+
if (child.marks.some((m) => REVIEW_MARKS.has(m.type.name))) found = true;
|
|
48
|
+
});
|
|
49
|
+
return found;
|
|
50
|
+
}
|
|
51
|
+
|
|
36
52
|
/** What the page flow needs to know about a v3 block. */
|
|
37
53
|
export interface WordInfo {
|
|
38
54
|
/** Space before and after in CSS px (Word adds them: the flow places them). */
|
|
@@ -63,6 +79,8 @@ export class WordRenderer {
|
|
|
63
79
|
readonly #ratio: (family: string) => number;
|
|
64
80
|
/** For the clipboard (`clipboardBlocks`): links as links, list numbers as Word's HTML has them. */
|
|
65
81
|
readonly #clipboard: boolean;
|
|
82
|
+
/** How tracked changes show. */
|
|
83
|
+
readonly #review: ReviewView;
|
|
66
84
|
#markers: (ListMarker | null)[] = [];
|
|
67
85
|
/** Comment ranges open at the start of each block. */
|
|
68
86
|
#comments: number[][] = [];
|
|
@@ -72,11 +90,13 @@ export class WordRenderer {
|
|
|
72
90
|
doc: Document,
|
|
73
91
|
ratio: (family: string) => number,
|
|
74
92
|
clipboard = false,
|
|
93
|
+
review: ReviewView = "markup",
|
|
75
94
|
) {
|
|
76
95
|
this.#context = context;
|
|
77
96
|
this.#doc = doc;
|
|
78
97
|
this.#ratio = ratio;
|
|
79
98
|
this.#clipboard = clipboard;
|
|
99
|
+
this.#review = review;
|
|
80
100
|
}
|
|
81
101
|
|
|
82
102
|
get context(): WordContext {
|
|
@@ -117,6 +137,7 @@ export class WordRenderer {
|
|
|
117
137
|
const { paragraph, run } = resolver.paragraph(node.attrs);
|
|
118
138
|
const line = (r: RunFormat) => lineHeight(paragraph, r, this.#ratio(fontStack(latin(r))));
|
|
119
139
|
const doc = this.#doc;
|
|
140
|
+
const changed = this.#review !== "final" && hasChanges(node);
|
|
120
141
|
const fragments = [0, ...splits].map((_, i, all) => {
|
|
121
142
|
const fragment = doc.createElement(tagOf(paragraph));
|
|
122
143
|
styleParagraph(fragment, paragraph);
|
|
@@ -130,6 +151,7 @@ export class WordRenderer {
|
|
|
130
151
|
if (i < all.length - 1 && fragment.style.textAlign === "justify")
|
|
131
152
|
fragment.style.textAlignLast = "justify";
|
|
132
153
|
}
|
|
154
|
+
if (changed && !this.#clipboard) fragment.style.boxShadow = CHANGE_BAR;
|
|
133
155
|
return fragment;
|
|
134
156
|
});
|
|
135
157
|
const dom = fragments[0] as HTMLElement;
|
|
@@ -155,7 +177,7 @@ export class WordRenderer {
|
|
|
155
177
|
if (name === "commentEnd") comments.delete(child.attrs.id as number);
|
|
156
178
|
const leafElement = (element: HTMLElement) => {
|
|
157
179
|
leaves.push({ dom: element, offset, size: child.nodeSize });
|
|
158
|
-
fragmentAt(offset).append(this.#wrap(element, child.marks, comments
|
|
180
|
+
fragmentAt(offset).append(this.#wrap(element, child.marks, comments));
|
|
159
181
|
};
|
|
160
182
|
switch (name) {
|
|
161
183
|
case "text":
|
|
@@ -176,7 +198,7 @@ export class WordRenderer {
|
|
|
176
198
|
const tab = doc.createTextNode("\t");
|
|
177
199
|
span.append(tab);
|
|
178
200
|
leaves.push({ dom: tab, offset, size: 1 });
|
|
179
|
-
fragmentAt(offset).append(this.#wrap(span, child.marks, comments
|
|
201
|
+
fragmentAt(offset).append(this.#wrap(span, child.marks, comments));
|
|
180
202
|
return;
|
|
181
203
|
}
|
|
182
204
|
case "opaqueInline": {
|
|
@@ -206,7 +228,7 @@ export class WordRenderer {
|
|
|
206
228
|
leaves.push({ dom: leaf, offset: from, size: to - from });
|
|
207
229
|
const span = this.#run(run, child.marks, line);
|
|
208
230
|
span.append(leaf);
|
|
209
|
-
fragmentAt(from).append(this.#wrap(span, child.marks, comments
|
|
231
|
+
fragmentAt(from).append(this.#wrap(span, child.marks, comments));
|
|
210
232
|
from = to;
|
|
211
233
|
}
|
|
212
234
|
});
|
|
@@ -259,17 +281,22 @@ export class WordRenderer {
|
|
|
259
281
|
}
|
|
260
282
|
|
|
261
283
|
/** Tracked changes and comment shading around a rendered run. */
|
|
262
|
-
#wrap(element: HTMLElement, marks: readonly Mark[],
|
|
263
|
-
if (
|
|
264
|
-
|
|
284
|
+
#wrap(element: HTMLElement, marks: readonly Mark[], comments: ReadonlySet<number>): HTMLElement {
|
|
285
|
+
if (comments.size && !this.#clipboard) {
|
|
286
|
+
// The comments whose range holds this text: the comments pane finds and marks it.
|
|
287
|
+
element.dataset.foliComments = [...comments].join(" ");
|
|
288
|
+
if (!element.style.backgroundColor)
|
|
289
|
+
element.style.backgroundColor = "rgba(255, 213, 79, 0.35)";
|
|
290
|
+
}
|
|
265
291
|
let outer = element;
|
|
266
|
-
|
|
292
|
+
// Text inserted, then deleted, shows as deleted (in the deleting author's color).
|
|
293
|
+
const change =
|
|
294
|
+
marks.find((m) => m.type.name === "deletion") ??
|
|
295
|
+
marks.find((m) => m.type.name === "insertion");
|
|
267
296
|
if (change) {
|
|
297
|
+
const { author, date } = change.attrs as { author: string; date: string | null };
|
|
268
298
|
const inserted = change.type.name === "insertion";
|
|
269
|
-
outer = this.#doc
|
|
270
|
-
if (change.attrs.author) outer.title = String(change.attrs.author);
|
|
271
|
-
outer.style.color = "#B5082E";
|
|
272
|
-
outer.style.textDecorationLine = inserted ? "underline" : "line-through";
|
|
299
|
+
outer = changeElement(this.#doc, inserted, author, date, this.#review);
|
|
273
300
|
outer.append(element);
|
|
274
301
|
}
|
|
275
302
|
// Schema v3 keeps any target (`file:`…): only a safe one is a link in the page or on the
|