@folijs/view 0.5.0 → 1.0.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.
Files changed (82) hide show
  1. package/CHANGELOG.md +194 -0
  2. package/README.md +1 -1
  3. package/dist/clipboard.d.ts +1 -49
  4. package/dist/clipboard.d.ts.map +1 -1
  5. package/dist/clipboard.js +65 -13
  6. package/dist/clipboard.js.map +1 -1
  7. package/dist/extension.d.ts +90 -0
  8. package/dist/extension.d.ts.map +1 -0
  9. package/dist/extension.js +152 -0
  10. package/dist/extension.js.map +1 -0
  11. package/dist/index.d.ts +2 -1
  12. package/dist/index.d.ts.map +1 -1
  13. package/dist/index.js.map +1 -1
  14. package/dist/layout/flow.d.ts +1 -109
  15. package/dist/layout/flow.d.ts.map +1 -1
  16. package/dist/layout/flow.js +808 -103
  17. package/dist/layout/flow.js.map +1 -1
  18. package/dist/print.d.ts +6 -0
  19. package/dist/print.d.ts.map +1 -0
  20. package/dist/print.js +35 -0
  21. package/dist/print.js.map +1 -0
  22. package/dist/render.d.ts +1 -24
  23. package/dist/render.d.ts.map +1 -1
  24. package/dist/render.js +4 -1
  25. package/dist/render.js.map +1 -1
  26. package/dist/view.d.ts +244 -64
  27. package/dist/view.d.ts.map +1 -1
  28. package/dist/view.js +1945 -215
  29. package/dist/view.js.map +1 -1
  30. package/dist/word/blocks.d.ts +0 -146
  31. package/dist/word/blocks.d.ts.map +1 -1
  32. package/dist/word/blocks.js +294 -59
  33. package/dist/word/blocks.js.map +1 -1
  34. package/dist/word/columns.d.ts +2 -0
  35. package/dist/word/columns.d.ts.map +1 -0
  36. package/dist/word/columns.js +63 -0
  37. package/dist/word/columns.js.map +1 -0
  38. package/dist/word/controls.d.ts +2 -0
  39. package/dist/word/controls.d.ts.map +1 -0
  40. package/dist/word/controls.js +416 -0
  41. package/dist/word/controls.js.map +1 -0
  42. package/dist/word/edit.d.ts +1 -71
  43. package/dist/word/edit.d.ts.map +1 -1
  44. package/dist/word/edit.js +338 -29
  45. package/dist/word/edit.js.map +1 -1
  46. package/dist/word/matches.d.ts +2 -0
  47. package/dist/word/matches.d.ts.map +1 -0
  48. package/dist/word/matches.js +102 -0
  49. package/dist/word/matches.js.map +1 -0
  50. package/dist/word/notes.d.ts +2 -0
  51. package/dist/word/notes.d.ts.map +1 -0
  52. package/dist/word/notes.js +403 -0
  53. package/dist/word/notes.js.map +1 -0
  54. package/dist/word/pages.d.ts +30 -20
  55. package/dist/word/pages.d.ts.map +1 -1
  56. package/dist/word/pages.js +421 -229
  57. package/dist/word/pages.js.map +1 -1
  58. package/dist/word/regions.d.ts +2 -0
  59. package/dist/word/regions.d.ts.map +1 -0
  60. package/dist/word/regions.js +388 -0
  61. package/dist/word/regions.js.map +1 -0
  62. package/dist/word/table.d.ts +1 -78
  63. package/dist/word/table.d.ts.map +1 -1
  64. package/dist/word/table.js +13 -3
  65. package/dist/word/table.js.map +1 -1
  66. package/package.json +6 -3
  67. package/src/clipboard.ts +93 -20
  68. package/src/extension.ts +267 -0
  69. package/src/index.ts +10 -0
  70. package/src/layout/flow.ts +997 -116
  71. package/src/print.ts +55 -0
  72. package/src/render.ts +6 -1
  73. package/src/view.ts +2178 -245
  74. package/src/word/blocks.ts +339 -62
  75. package/src/word/columns.ts +81 -0
  76. package/src/word/controls.ts +457 -0
  77. package/src/word/edit.ts +381 -31
  78. package/src/word/matches.ts +104 -0
  79. package/src/word/notes.ts +464 -0
  80. package/src/word/pages.ts +518 -221
  81. package/src/word/regions.ts +488 -0
  82. package/src/word/table.ts +33 -8
package/dist/word/edit.js CHANGED
@@ -9,8 +9,23 @@
9
9
  * keeps opaque is shown from its XML and cannot be edited: an opaque block is a block leaf, an
10
10
  * opaque inline node a leaf element.
11
11
  */
12
- import { isSafeHref, } from "@folijs/core";
13
- import { CHANGE_BAR, changeElement, floatElements, floatGeometry, fontStack, imageSource, latin, lineHeight, markerElement, opaqueBlock, opaqueText, PX_PER_TWIP, parseOpaque, pictureElement, styleParagraph, styleRun, tagOf, WORD_PAGE_BREAK_ATTR, } from "./blocks.js";
12
+ import { fieldDisplay, isSafeHref, } from "@folijs/core";
13
+ import { referencesIn, } from "@folijs/core/internal";
14
+ import { applyAttrs, nodeViewDOM, reportError, } from "../extension.js";
15
+ import { CHANGE_BAR, changeElement, floatElements, floatGeometry, fontStack, imageSource, latin, lineHeight, marginAbove, markerElement, NOTE_NUMBER_ATTR, NOTE_REF_ATTR, onlyPictures, opaqueBlock, opaqueText, PICTURE_ATTR, PX_PER_TWIP, parseOpaque, pictureElement, styleParagraph, styleRun, tagOf, WORD_COLUMN_BREAK_ATTR, WORD_PAGE_BREAK_ATTR, } from "./blocks.js";
16
+ /** Inline nodes with nothing to show: a line holding only these is still an empty line. */
17
+ const NOT_IN_LINE = new Set([
18
+ "bookmarkStart",
19
+ "bookmarkEnd",
20
+ "commentStart",
21
+ "commentEnd",
22
+ "controlStart",
23
+ "controlEnd",
24
+ "fieldStart",
25
+ "fieldCode",
26
+ "fieldSeparator",
27
+ "fieldEnd",
28
+ ]);
14
29
  const REVIEW_MARKS = new Set(["insertion", "deletion", "formatChange"]);
15
30
  /** Whether a paragraph has tracked changes: in its text, its mark or its formatting. */
16
31
  function hasChanges(node) {
@@ -23,14 +38,25 @@ function hasChanges(node) {
23
38
  });
24
39
  return found;
25
40
  }
41
+ /** A paragraph's inline nodes, by type name. */
42
+ function childTypes(node) {
43
+ const types = [];
44
+ if (node.type.name === "paragraph")
45
+ node.forEach((child) => {
46
+ types.push(child.type.name);
47
+ });
48
+ return types;
49
+ }
26
50
  /**
27
51
  * A floating picture's holder (drawn first in its paragraph, the view's own DOM) to its anchor:
28
52
  * the empty, non-editable span at the picture's place in the text, which positions map to.
53
+ * @internal
29
54
  */
30
55
  export const FLOAT_ANCHORS = new WeakMap();
31
56
  /**
32
57
  * Renders the blocks of one v3 document for the editable view. `prepare` runs before each
33
58
  * render: list numbers and comment ranges depend on the blocks before.
59
+ * @internal
34
60
  */
35
61
  export class WordRenderer {
36
62
  #context;
@@ -45,9 +71,30 @@ export class WordRenderer {
45
71
  #markers = [];
46
72
  /** Comment ranges open at the start of each block. */
47
73
  #comments = [];
48
- constructor(context, doc, ratio, clipboard = false, review = "markup", image) {
74
+ /** Where a document adds paragraph spacing: the space after the paragraph above, in a cell. */
75
+ #above = [];
76
+ /** Whether fields show their codes. */
77
+ #codes;
78
+ /** What each block shows of its fields. */
79
+ #fields = [];
80
+ /** What note references show, by story, and what a note's own mark shows. */
81
+ #notes = new Map();
82
+ #noteMark = "";
83
+ /** The numbers each block shows. */
84
+ #noteKeys = [];
85
+ /** Node and mark views (`ViewExtension`), none on the clipboard. */
86
+ views = null;
87
+ /** Where node views' drawings are recorded, for their `destroy` (the view's list). */
88
+ drawn = null;
89
+ #dom;
90
+ /** Each block's decorations, relative to its content (`decorationsByBlock`), and as keys. */
91
+ #decorations = [];
92
+ #decorationKeys = [];
93
+ constructor(context, doc, ratio, clipboard = false, review = "markup", image, codes = false) {
94
+ this.#codes = codes;
49
95
  this.#context = context;
50
96
  this.#doc = doc;
97
+ this.#dom = nodeViewDOM(doc);
51
98
  this.#ratio = ratio;
52
99
  this.#clipboard = clipboard;
53
100
  this.#review = review;
@@ -56,11 +103,37 @@ export class WordRenderer {
56
103
  get context() {
57
104
  return this.#context;
58
105
  }
106
+ /** The envelope moved on without touching what blocks look like (a story was edited). */
107
+ set context(context) {
108
+ this.#context = context;
109
+ }
110
+ /** Decorations by block index (of those prepared): set before `key` and `render`. */
111
+ set decorations(list) {
112
+ this.#decorations = list;
113
+ this.#decorationKeys = list.map((own) => (own ? JSON.stringify(own) : ""));
114
+ }
115
+ /** What notes show: set before `prepare`. */
116
+ setNotes(references, mark = "") {
117
+ this.#notes = references;
118
+ this.#noteMark = mark;
119
+ }
59
120
  /**
60
121
  * Numbering and comment state for `blocks`, the blocks rendered, in document order (the
61
- * paragraphs in table cells among them: Word numbers lists through tables).
122
+ * paragraphs in table cells among them: Word numbers lists through tables). `aboveInCell`
123
+ * gives the paragraph before one in its table cell, with that cell's format.
62
124
  */
63
- prepare(blocks) {
125
+ prepare(blocks, aboveInCell) {
126
+ const { resolver } = this.#context;
127
+ this.#above =
128
+ resolver.addsParagraphSpacing && aboveInCell
129
+ ? blocks.map((_, i) => {
130
+ const previous = aboveInCell(i);
131
+ if (previous?.node.type.name !== "paragraph")
132
+ return 0;
133
+ const { spacing } = resolver.paragraph(previous.node.attrs, previous.cell).paragraph;
134
+ return (spacing.afterAutospacing ? 280 : spacing.after) * PX_PER_TWIP;
135
+ })
136
+ : [];
64
137
  const attrs = [];
65
138
  const comments = [];
66
139
  const open = new Set();
@@ -77,6 +150,12 @@ export class WordRenderer {
77
150
  }
78
151
  this.#markers = this.#context.resolver.markers(attrs);
79
152
  this.#comments = comments;
153
+ this.#fields = fieldDisplay(blocks.map(childTypes), this.#codes);
154
+ this.#noteKeys = this.#notes.size
155
+ ? blocks.map((node) => referencesIn(node)
156
+ .map((ref) => this.#notes.get(ref.story) ?? "")
157
+ .join(" "))
158
+ : [];
80
159
  }
81
160
  /** Whether the block at `index` (of those prepared) shows a list number. */
82
161
  hasMarker(index) {
@@ -85,21 +164,35 @@ export class WordRenderer {
85
164
  /** What a block's rendering depends on at `index`, besides its node (and its cell's format). */
86
165
  key(index, cell) {
87
166
  const inCell = cell ? `|${cell.style ?? ""}:${cell.conditions.join(",")}` : "";
88
- return `${this.#markers[index]?.text ?? ""}|${(this.#comments[index] ?? []).join(" ")}${inCell}`;
167
+ const above = this.#above[index] ? `|${this.#above[index]}` : "";
168
+ // A field opened or closed in an earlier block changes what shows here.
169
+ const field = this.#fields[index];
170
+ const fields = field?.hidden.size || field?.gone ? `|${[...field.hidden]}${field.gone}` : "";
171
+ const notes = this.#noteKeys[index] ? `|${this.#noteKeys[index]}` : "";
172
+ const decorations = this.#decorationKeys[index] ? `|${this.#decorationKeys[index]}` : "";
173
+ return `${this.#markers[index]?.text ?? ""}|${(this.#comments[index] ?? []).join(" ")}${inCell}${above}${fields}${notes}|${this.#noteMark}${decorations}`;
89
174
  }
90
175
  /** A block; `cell` is the format of the table cell it is in, if any. */
91
176
  render(node, index, splits = [], cell) {
177
+ const decorations = this.#decorations[index] ?? [];
92
178
  if (node.type.name !== "paragraph")
93
- return this.#opaque(node, index);
179
+ return this.#opaque(node, index, splits, !!cell);
94
180
  const { resolver } = this.#context;
95
181
  const { paragraph, run } = resolver.paragraph(node.attrs, cell ?? undefined);
96
182
  const line = (r) => lineHeight(paragraph, r, this.#ratio(fontStack(latin(r))));
97
183
  const doc = this.#doc;
184
+ const codes = this.#codes;
185
+ // On the clipboard a block stands by itself.
186
+ const display = this.#clipboard
187
+ ? fieldDisplay([childTypes(node)], false)[0]
188
+ : (this.#fields[index] ?? fieldDisplay([[]], false)[0]);
98
189
  const changed = this.#review !== "final" && hasChanges(node);
190
+ // The line's own height is the paragraph mark's, as in `renderBlocks`.
191
+ const mark = resolver.run(run, (node.attrs.markFormat ?? []));
99
192
  const fragments = [0, ...splits].map((_, i, all) => {
100
193
  const fragment = doc.createElement(tagOf(paragraph));
101
194
  styleParagraph(fragment, paragraph);
102
- styleRun(fragment, run, line(run));
195
+ styleRun(fragment, mark, line(mark));
103
196
  if (all.length > 1) {
104
197
  fragment.dataset.fragment = String(i);
105
198
  // The first-line indent is the paragraph's first line only.
@@ -112,6 +205,11 @@ export class WordRenderer {
112
205
  }
113
206
  if (changed && !this.#clipboard)
114
207
  fragment.style.boxShadow = CHANGE_BAR;
208
+ if (display.gone)
209
+ fragment.style.display = "none";
210
+ for (const d of decorations)
211
+ if (d.node && d.from < 0)
212
+ applyAttrs(fragment, d.attrs);
115
213
  return fragment;
116
214
  });
117
215
  const dom = fragments[0];
@@ -137,15 +235,33 @@ export class WordRenderer {
137
235
  const floats = new Map();
138
236
  let geometry;
139
237
  const comments = new Set(this.#comments[index] ?? []);
140
- node.forEach((child, offset) => {
238
+ /** Inline decorations over content `from`..`to`, and node ones on a leaf there. */
239
+ const decorate = (element, from, to, leaf) => {
240
+ for (const d of decorations)
241
+ if (d.node ? leaf && d.from === from : d.from <= from && d.to >= to)
242
+ applyAttrs(element, d.attrs);
243
+ };
244
+ node.forEach((child, offset, childIndex) => {
141
245
  const name = child.type.name;
142
246
  if (name === "commentStart")
143
247
  comments.add(child.attrs.id);
144
248
  if (name === "commentEnd")
145
249
  comments.delete(child.attrs.id);
250
+ const hidden = display.hidden.has(childIndex);
251
+ /** Where this node's DOM goes: out of sight when it is hidden, as hidden deletions are. */
252
+ const holder = (at) => {
253
+ const fragment = fragmentAt(at);
254
+ if (!hidden)
255
+ return fragment;
256
+ const span = doc.createElement("span");
257
+ span.style.display = "none";
258
+ fragment.append(span);
259
+ return span;
260
+ };
146
261
  const leafElement = (element) => {
262
+ decorate(element, offset, offset + child.nodeSize, true);
147
263
  leaves.push({ dom: element, offset, size: child.nodeSize });
148
- fragmentAt(offset).append(this.#wrap(element, child.marks, comments));
264
+ holder(offset).append(this.#wrap(element, child.marks, comments));
149
265
  };
150
266
  switch (name) {
151
267
  case "text":
@@ -153,28 +269,30 @@ export class WordRenderer {
153
269
  case "hardBreak":
154
270
  case "columnBreak":
155
271
  case "pageBreak": {
156
- // shortcut: a column break ends the line; Foli lays out one column.
157
272
  const br = doc.createElement("br");
158
273
  if (name === "pageBreak")
159
274
  br.setAttribute(WORD_PAGE_BREAK_ATTR, "");
275
+ if (name === "columnBreak")
276
+ br.setAttribute(WORD_COLUMN_BREAK_ATTR, "");
160
277
  leaves.push({ dom: br, offset, size: 1 });
161
- fragmentAt(offset).append(br);
278
+ holder(offset).append(br);
162
279
  return;
163
280
  }
164
281
  case "tab": {
165
282
  // A tab is text to the line layout (tab stops) and to the read-back.
166
283
  const span = this.#run(run, child.marks, line);
284
+ decorate(span, offset, offset + 1, true);
167
285
  const tab = doc.createTextNode("\t");
168
286
  span.append(tab);
169
287
  leaves.push({ dom: tab, offset, size: 1 });
170
- fragmentAt(offset).append(this.#wrap(span, child.marks, comments));
288
+ holder(offset).append(this.#wrap(span, child.marks, comments));
171
289
  return;
172
290
  }
173
291
  case "picture": {
174
292
  // Not editable inside (no browser resize handles); one position, like opaque content.
175
293
  const span = doc.createElement("span");
176
294
  span.setAttribute("contenteditable", "false");
177
- if (child.attrs.float && !this.#clipboard) {
295
+ if (child.attrs.float && !this.#clipboard && !hidden) {
178
296
  // Floating: drawn at the fragment's start (ADR-0006), its place in the text an
179
297
  // empty anchor.
180
298
  geometry ??= floatGeometry(this.#context.envelope.section);
@@ -190,6 +308,19 @@ export class WordRenderer {
190
308
  leafElement(span);
191
309
  return;
192
310
  }
311
+ case "noteReference":
312
+ case "noteMark": {
313
+ // Drawn, never document text: the number comes from where the note is.
314
+ const span = this.#run(run, child.marks, line);
315
+ const story = String(child.attrs.story);
316
+ span.textContent = name === "noteMark" ? this.#noteMark : (this.#notes.get(story) ?? "");
317
+ span.setAttribute("contenteditable", "false");
318
+ span.setAttribute(NOTE_NUMBER_ATTR, "");
319
+ if (name === "noteReference")
320
+ span.setAttribute(NOTE_REF_ATTR, story);
321
+ leafElement(span);
322
+ return;
323
+ }
193
324
  case "opaqueInline": {
194
325
  const span = this.#run(run, child.marks, line);
195
326
  const xml = parseOpaque(String(child.attrs.xml ?? ""), doc);
@@ -200,37 +331,85 @@ export class WordRenderer {
200
331
  return;
201
332
  }
202
333
  default: {
334
+ const stands = child.type.spec.text;
335
+ if (stands) {
336
+ // A custom or foreign node (ADR-0008): its node view or its text, one position, as
337
+ // opaque content.
338
+ const span = this.#run(run, child.marks, line);
339
+ const view = this.#clipboard ? undefined : this.views?.nodes.get(name);
340
+ const drawn = view && this.#draw(view, child, span);
341
+ if (drawn)
342
+ span.append(drawn);
343
+ else
344
+ span.textContent = stands(child.attrs);
345
+ span.dataset.foliCustom = String(child.attrs.type ?? name);
346
+ span.setAttribute("contenteditable", "false");
347
+ leafElement(span);
348
+ return;
349
+ }
350
+ // A field's code and braces, when codes show: drawn, not document text.
351
+ const shown = display.braces.get(childIndex) ??
352
+ (codes && !hidden && name === "fieldCode" ? String(child.attrs.text ?? "") : "");
353
+ if (shown) {
354
+ const span = this.#run(run, child.marks, line);
355
+ span.textContent = shown;
356
+ span.dataset.foliFieldCode = "";
357
+ span.setAttribute("contenteditable", "false");
358
+ leaves.push({ dom: span, offset, size: child.nodeSize });
359
+ fragmentAt(offset).append(span);
360
+ return;
361
+ }
203
362
  // Bookmarks, comment anchors and reference marks: nothing to see, a place to map to.
204
363
  const span = doc.createElement("span");
205
364
  if (name === "commentReference")
206
365
  span.dataset.foliCommentReference = String(child.attrs.id);
207
366
  leaves.push({ dom: span, offset, size: child.nodeSize });
208
- fragmentAt(offset).append(span);
367
+ holder(offset).append(span);
209
368
  return;
210
369
  }
211
370
  }
212
371
  const text = child.text ?? "";
213
- const cuts = splits.filter((split) => split > offset && split < offset + text.length);
372
+ const inside = (at) => at > offset && at < offset + text.length;
373
+ const cuts = splits.filter(inside);
374
+ // Inline decorations start and end runs of their own.
375
+ for (const d of decorations)
376
+ if (!d.node)
377
+ for (const at of [d.from, d.to])
378
+ if (inside(at) && !cuts.includes(at))
379
+ cuts.push(at);
380
+ cuts.sort((a, b) => a - b);
214
381
  let from = offset;
215
382
  for (const to of [...cuts, offset + text.length]) {
216
383
  const leaf = doc.createTextNode(text.slice(from - offset, to - offset));
217
384
  leaves.push({ dom: leaf, offset: from, size: to - from });
218
385
  const span = this.#run(run, child.marks, line);
386
+ decorate(span, from, to, false);
219
387
  span.append(leaf);
220
- fragmentAt(from).append(this.#wrap(span, child.marks, comments));
388
+ holder(from).append(this.#wrap(span, child.marks, comments));
221
389
  from = to;
222
390
  }
223
391
  });
224
392
  for (const [fragment, nodes] of floats)
225
393
  fragment.prepend(...nodes);
394
+ const inline = [];
395
+ node.forEach((child) => {
396
+ inline.push(child);
397
+ });
398
+ if (onlyPictures(inline, (child) => child.type.name, (child) => !!child.attrs.float))
399
+ for (const fragment of fragments)
400
+ for (const picture of fragment.querySelectorAll(`[${PICTURE_ATTR}]`))
401
+ picture.style.verticalAlign = "bottom";
226
402
  // An empty paragraph, or one ending in a line break, shows its last line; after a page
227
403
  // break the paragraph mark stays on the break's line, as in Word.
228
404
  let last = null;
229
405
  for (let i = node.childCount - 1; i >= 0 && !last; i--) {
230
406
  const child = node.child(i);
407
+ if (display.hidden.has(i))
408
+ continue;
231
409
  // A floating picture is not in the line: a paragraph holding only one shows its empty line.
232
- if (!["bookmarkStart", "bookmarkEnd", "commentStart", "commentEnd"].includes(child.type.name) &&
233
- !(child.type.name === "picture" && child.attrs.float))
410
+ if (display.braces.has(i) ||
411
+ (codes && child.type.name === "fieldCode") ||
412
+ (!NOT_IN_LINE.has(child.type.name) && !(child.type.name === "picture" && child.attrs.float)))
234
413
  last = child;
235
414
  }
236
415
  if (!last || last.type.name === "hardBreak" || last.type.name === "columnBreak")
@@ -238,19 +417,20 @@ export class WordRenderer {
238
417
  const { before, after } = paragraph.spacing;
239
418
  // Word's automatic spacing (HTML): 14 pt.
240
419
  const word = {
241
- before: (paragraph.spacing.beforeAutospacing ? 280 : before) * PX_PER_TWIP,
242
- after: (paragraph.spacing.afterAutospacing ? 280 : after) * PX_PER_TWIP,
420
+ before: display.gone ? 0 : (paragraph.spacing.beforeAutospacing ? 280 : before) * PX_PER_TWIP,
421
+ after: display.gone ? 0 : (paragraph.spacing.afterAutospacing ? 280 : after) * PX_PER_TWIP,
243
422
  keepNext: paragraph.keepNext,
244
423
  keepLines: paragraph.keepLines,
245
424
  pageBreakBefore: paragraph.pageBreakBefore,
246
425
  style: paragraph.style,
247
426
  contextual: paragraph.contextualSpacing,
427
+ empty: !last && !marker,
248
428
  };
249
429
  if (cell && !this.#clipboard) {
250
430
  // In a cell its spacing is its margins (the page flow spaces top-level blocks); a
251
431
  // fragment continued on the next page has none at the split.
252
432
  // shortcut: contextual spacing between paragraphs in a cell is not applied.
253
- dom.style.marginTop = `${word.before}px`;
433
+ dom.style.marginTop = `${marginAbove(word.before, this.#above[index] ?? 0, resolver)}px`;
254
434
  fragments[fragments.length - 1].style.marginBottom = `${word.after}px`;
255
435
  }
256
436
  if (this.#clipboard) {
@@ -272,6 +452,24 @@ export class WordRenderer {
272
452
  const run = this.#context.resolver.run(paragraphRun, json);
273
453
  const span = this.#doc.createElement("span");
274
454
  styleRun(span, run, line(run));
455
+ // Custom marks (ADR-0008): named for CSS, styled by their mark views.
456
+ const custom = [];
457
+ for (const mark of marks) {
458
+ const type = mark.type.name === "foreign" ? String(mark.attrs.type) : mark.type.name;
459
+ if (!type.includes(":"))
460
+ continue;
461
+ custom.push(type);
462
+ const view = this.views?.marks.get(mark.type.name);
463
+ try {
464
+ if (view)
465
+ applyAttrs(span, view(mark));
466
+ }
467
+ catch (error) {
468
+ reportError(error);
469
+ }
470
+ }
471
+ if (custom.length)
472
+ span.dataset.foliMark = custom.join(" ");
275
473
  return span;
276
474
  }
277
475
  /** Tracked changes and comment shading around a rendered run. */
@@ -304,11 +502,100 @@ export class WordRenderer {
304
502
  a.append(outer);
305
503
  return a;
306
504
  }
505
+ /** A node view's drawing of `node` in `box`, recorded; null (reported) when `render` throws. */
506
+ #draw(view, node, box) {
507
+ try {
508
+ const root = view.render(node, this.#dom);
509
+ this.drawn?.push({ view, root, box });
510
+ return root;
511
+ }
512
+ catch (error) {
513
+ reportError(error);
514
+ return null;
515
+ }
516
+ }
517
+ /**
518
+ * A custom block drawn by its node view (ADR-0008): one fragment, or one per page it is split
519
+ * across, each drawing the node again and showing its part (`splits`, layout px) through a clip.
520
+ */
521
+ #nodeView(node, index, view, splits, inCell) {
522
+ const doc = this.#doc;
523
+ const tops = [0, ...splits];
524
+ let root;
525
+ const fragments = tops.map((top, i) => {
526
+ const box = doc.createElement("div");
527
+ box.dataset.foliCustom = node.type.name;
528
+ box.setAttribute("contenteditable", "false");
529
+ // Its root's margins stay inside it: measured as the flow places it.
530
+ box.style.display = "flow-root";
531
+ const drawn = this.#draw(view, node, box) ?? doc.createTextNode(node.type.spec.text?.(node.attrs) ?? "");
532
+ root ??= drawn;
533
+ if (tops.length > 1) {
534
+ box.dataset.fragment = String(i);
535
+ box.style.overflow = "hidden";
536
+ const next = tops[i + 1];
537
+ if (next !== undefined)
538
+ box.style.height = `${next - top}px`;
539
+ // The parts after the first are drawn again: read once.
540
+ if (i > 0) {
541
+ box.setAttribute("aria-hidden", "true");
542
+ box.setAttribute("inert", "");
543
+ }
544
+ const inner = doc.createElement("div");
545
+ inner.style.marginTop = `${-top}px`;
546
+ inner.append(drawn);
547
+ box.append(inner);
548
+ }
549
+ else
550
+ box.append(drawn);
551
+ for (const d of this.#decorations[index] ?? [])
552
+ if (d.node && d.from < 0)
553
+ applyAttrs(box, d.attrs);
554
+ return box;
555
+ });
556
+ const dom = fragments[0];
557
+ // shortcut: a custom block in a table cell is kept whole (cells split at their lines); split it there when a node view needs it.
558
+ const split = view.split !== false && !inCell;
559
+ const parts = () => {
560
+ const { top, height } = dom.getBoundingClientRect();
561
+ const bottoms = [];
562
+ const children = root?.nodeType === Node.ELEMENT_NODE ? root.children : [];
563
+ for (const child of children) {
564
+ const bottom = child.getBoundingClientRect().bottom - top;
565
+ if (bottom > (bottoms[bottoms.length - 1] ?? 0) && bottom < height)
566
+ bottoms.push(bottom);
567
+ }
568
+ bottoms.push(height);
569
+ return bottoms;
570
+ };
571
+ return {
572
+ node,
573
+ dom,
574
+ fragments,
575
+ splits,
576
+ leaves: [],
577
+ key: this.key(index),
578
+ word: leafInfo(!split),
579
+ ...(split ? { parts } : {}),
580
+ };
581
+ }
307
582
  /** An opaque block, shown from its XML: a block leaf, not editable inside. */
308
- #opaque(node, index) {
583
+ #opaque(node, index, splits, inCell) {
584
+ const view = this.#clipboard ? undefined : this.views?.nodes.get(node.type.name);
585
+ if (view)
586
+ return this.#nodeView(node, index, view, splits, inCell);
309
587
  const shown = opaqueBlock(node.toJSON(), index, this.#context.resolver, this.#doc, this.#ratio, this.#image, floatGeometry(this.#context.envelope.section));
310
588
  const dom = shown.element;
311
589
  dom.setAttribute("contenteditable", "false");
590
+ const stands = node.type.spec.text;
591
+ if (stands) {
592
+ // A custom or foreign block (ADR-0008): its text in a box.
593
+ dom.textContent = stands(node.attrs);
594
+ dom.dataset.foliCustom = String(node.attrs.type ?? node.type.name);
595
+ }
596
+ for (const d of this.#decorations[index] ?? [])
597
+ if (d.node && d.from < 0)
598
+ applyAttrs(dom, d.attrs);
312
599
  return {
313
600
  node,
314
601
  dom,
@@ -324,10 +611,24 @@ export class WordRenderer {
324
611
  pageBreakBefore: false,
325
612
  style: null,
326
613
  contextual: false,
614
+ empty: false,
327
615
  },
328
616
  };
329
617
  }
330
618
  }
619
+ /** What the flow needs of a block that is not a paragraph: no spacing, keep rules or breaks. */
620
+ function leafInfo(keepLines) {
621
+ return {
622
+ before: 0,
623
+ after: 0,
624
+ keepNext: false,
625
+ keepLines,
626
+ pageBreakBefore: false,
627
+ style: null,
628
+ contextual: false,
629
+ empty: false,
630
+ };
631
+ }
331
632
  /** An element's style attribute, ready for one more declaration. */
332
633
  function styleOf(element) {
333
634
  const style = element.getAttribute("style")?.trim() ?? "";
@@ -337,6 +638,7 @@ function styleOf(element) {
337
638
  * The HTML of copied v3 blocks `nodes`: drawn as the editor draws them, with their list numbers
338
639
  * in `doc` (from top-level block `first`; -1 for text from inside one paragraph, which takes no
339
640
  * number), without what only the editor needs (comment shading, `contenteditable`).
641
+ * @internal
340
642
  */
341
643
  export function clipboardBlocks(context, doc, nodes, first, ownerDocument, ratio) {
342
644
  const renderer = new WordRenderer(context, ownerDocument, ratio, true);
@@ -357,13 +659,20 @@ export function clipboardBlocks(context, doc, nodes, first, ownerDocument, ratio
357
659
  }
358
660
  /**
359
661
  * A block's space before and after next to its neighbours: Word ignores the space between
360
- * paragraphs of the same style that ask for it (contextual spacing).
662
+ * paragraphs of the same style that ask for it (contextual spacing). When only the first asks,
663
+ * its space after still eats into the second's space before, unless the document adds the two
664
+ * (`adds`).
665
+ * @internal
361
666
  */
362
- export function spacing(above, block, below) {
667
+ export function spacing(above, block, below, adds = false) {
363
668
  const same = (other) => other !== undefined && other.style === block.style;
364
- return {
365
- before: same(above) && block.contextual ? 0 : block.before,
366
- after: same(below) && block.contextual ? 0 : block.after,
367
- };
669
+ let { before } = block;
670
+ if (above && same(above)) {
671
+ if (block.contextual)
672
+ before = 0;
673
+ else if (above.contextual && !adds)
674
+ before = Math.max(0, block.before - above.after);
675
+ }
676
+ return { before, after: same(below) && block.contextual ? 0 : block.after };
368
677
  }
369
678
  //# sourceMappingURL=edit.js.map