@portone/docx-editor 0.6.2 → 0.6.4

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 (107) hide show
  1. package/CHANGELOG.md +91 -0
  2. package/dist/DocxEditor.js +2 -2
  3. package/dist/docx/comments/writing.d.ts +8 -0
  4. package/dist/docx/comments/writing.js +9 -2
  5. package/dist/docx/headersFooters.d.ts +7 -0
  6. package/dist/docx/headersFooters.js +8 -15
  7. package/dist/docx/identities.d.ts +9 -2
  8. package/dist/docx/identities.js +45 -12
  9. package/dist/docx/importDocx.js +2 -0
  10. package/dist/docx/importParagraph.d.ts +4 -0
  11. package/dist/docx/importParagraph.js +2 -0
  12. package/dist/docx/importPolicy.d.ts +2 -2
  13. package/dist/docx/importPolicy.js +12 -5
  14. package/dist/docx/importPreserved.d.ts +3 -3
  15. package/dist/docx/importSdtBlock.d.ts +23 -0
  16. package/dist/docx/importSdtBlock.js +33 -0
  17. package/dist/docx/importTable.js +35 -24
  18. package/dist/docx/invariants.js +60 -3
  19. package/dist/docx/notes/numbering.js +4 -4
  20. package/dist/docx/notes/references.d.ts +0 -2
  21. package/dist/docx/notes/references.js +2 -7
  22. package/dist/docx/numberingPlanner.d.ts +3 -0
  23. package/dist/docx/numberingPlanner.js +8 -2
  24. package/dist/docx/packageParts.d.ts +9 -0
  25. package/dist/docx/packageParts.js +16 -0
  26. package/dist/docx/packagePrefixes.d.ts +12 -0
  27. package/dist/docx/packagePrefixes.js +18 -0
  28. package/dist/docx/partPlan.js +3 -5
  29. package/dist/docx/sdt.d.ts +79 -9
  30. package/dist/docx/sdt.js +121 -26
  31. package/dist/docx/sections.d.ts +13 -13
  32. package/dist/docx/sections.js +34 -20
  33. package/dist/docx/serializeBlock.js +10 -1
  34. package/dist/docx/serializeParagraph.d.ts +2 -1
  35. package/dist/docx/serializeParagraph.js +6 -1
  36. package/dist/docx/serializeSdtBlock.d.ts +16 -0
  37. package/dist/docx/serializeSdtBlock.js +11 -0
  38. package/dist/docx/serializeTable.d.ts +8 -2
  39. package/dist/docx/serializeTable.js +14 -20
  40. package/dist/docx/story.d.ts +10 -4
  41. package/dist/docx/story.js +25 -20
  42. package/dist/docx/storyBlocks.d.ts +32 -0
  43. package/dist/docx/storyBlocks.js +65 -0
  44. package/dist/docx/storyParts.d.ts +3 -0
  45. package/dist/docx/storyParts.js +4 -1
  46. package/dist/docx/wrappers.d.ts +33 -15
  47. package/dist/docx/wrappers.js +45 -26
  48. package/dist/editor/blockControlEdits.d.ts +15 -0
  49. package/dist/editor/blockControlEdits.js +79 -0
  50. package/dist/editor/clipboard/normalizers.js +1 -1
  51. package/dist/editor/commands/lockCommands.d.ts +5 -1
  52. package/dist/editor/commands/lockCommands.js +51 -23
  53. package/dist/editor/createEditor.js +4 -0
  54. package/dist/editor/inlineControlEdits.d.ts +20 -0
  55. package/dist/editor/inlineControlEdits.js +69 -0
  56. package/dist/editor/notes/noteSurface.js +4 -3
  57. package/dist/editor/plugins/controlLifecycle.d.ts +18 -0
  58. package/dist/editor/plugins/controlLifecycle.js +130 -0
  59. package/dist/editor/plugins/keymap.js +34 -5
  60. package/dist/editor/plugins/rowResize.js +2 -4
  61. package/dist/editor/stories/storyState.d.ts +2 -0
  62. package/dist/editor/stories/storyState.js +2 -0
  63. package/dist/ooxml/element.js +3 -2
  64. package/dist/ooxml/errors.d.ts +11 -0
  65. package/dist/ooxml/fragment.js +3 -2
  66. package/dist/ooxml/names.d.ts +4 -0
  67. package/dist/ooxml/names.js +8 -2
  68. package/dist/ooxml/partPrefixes.d.ts +21 -0
  69. package/dist/ooxml/partPrefixes.js +164 -0
  70. package/dist/ooxml/partSplice.d.ts +6 -1
  71. package/dist/ooxml/partSplice.js +30 -10
  72. package/dist/ooxml/tagScan.d.ts +24 -2
  73. package/dist/ooxml/tagScan.js +26 -7
  74. package/dist/ooxml/xml.d.ts +8 -0
  75. package/dist/ooxml/xml.js +27 -5
  76. package/dist/page/blockKinds.d.ts +24 -2
  77. package/dist/page/blockKinds.js +6 -1
  78. package/dist/page/kinds/index.d.ts +5 -0
  79. package/dist/page/kinds/index.js +2 -0
  80. package/dist/page/kinds/sdtBlockKind.d.ts +14 -0
  81. package/dist/page/kinds/sdtBlockKind.js +143 -0
  82. package/dist/page/measureBlocks.js +4 -2
  83. package/dist/page/pageDecorations.js +3 -2
  84. package/dist/page/pageLayout.d.ts +16 -15
  85. package/dist/page/pageLayout.js +134 -67
  86. package/dist/page/usePageLayout.d.ts +2 -2
  87. package/dist/page/usePageLayout.js +5 -5
  88. package/dist/schema/attrRoles.js +56 -1
  89. package/dist/schema/clipboard.js +11 -0
  90. package/dist/schema/controlAttrs.d.ts +119 -0
  91. package/dist/schema/controlAttrs.js +137 -0
  92. package/dist/schema/controlEdges.d.ts +22 -0
  93. package/dist/schema/controlEdges.js +50 -0
  94. package/dist/schema/docxSchema.d.ts +1 -1
  95. package/dist/schema/docxSchema.js +235 -37
  96. package/dist/schema/editGuard.d.ts +1 -1
  97. package/dist/schema/guards.js +2 -0
  98. package/dist/schema/locks.d.ts +56 -8
  99. package/dist/schema/locks.js +115 -56
  100. package/dist/styles/classNames.d.ts +10 -0
  101. package/dist/styles/classNames.js +10 -0
  102. package/dist/styles.css +44 -2
  103. package/dist/table/merge.js +11 -3
  104. package/dist/table/rowResize.d.ts +9 -0
  105. package/dist/table/rowResize.js +9 -5
  106. package/dist/ui/TextMenu.js +2 -0
  107. package/package.json +3 -3
@@ -0,0 +1,137 @@
1
+ // src/schema/controlAttrs.ts
2
+ var SDT_BLOCK_NODE = "sdtBlock";
3
+ var SDT_EMPTY_BLOCK_NODE = "sdtEmpty";
4
+ var SDT_EMPTY_INLINE_NODE = "sdtEmptyInline";
5
+ var OWN_CONTROL_ATTRS = {
6
+ prefix: "sdtPrefix",
7
+ contentsLocked: "contentsLocked",
8
+ deletionLocked: "deletionLocked",
9
+ group: "group",
10
+ temporary: "temporary",
11
+ showingPlaceholder: "showingPlaceholder"
12
+ };
13
+ var WRAPPED_CONTROL_ATTRS = {
14
+ prefix: "sdtPrefix",
15
+ contentsLocked: "sdtContentsLocked",
16
+ deletionLocked: "sdtDeletionLocked",
17
+ group: "sdtGroup",
18
+ temporary: "sdtTemporary",
19
+ showingPlaceholder: "sdtShowingPlaceholder"
20
+ };
21
+ var NO_CONTROL = {
22
+ prefix: null,
23
+ contentsLocked: false,
24
+ deletionLocked: false,
25
+ group: false,
26
+ temporary: false,
27
+ showingPlaceholder: false
28
+ };
29
+ var PREFIX_DOM_ATTR = "data-sdt-prefix";
30
+ var FLAG_DOM_ATTRS = {
31
+ contentsLocked: "data-sdt-contents-locked",
32
+ deletionLocked: "data-sdt-deletion-locked",
33
+ group: "data-sdt-group",
34
+ temporary: "data-sdt-temporary",
35
+ showingPlaceholder: "data-sdt-placeholder"
36
+ };
37
+ function controlAttrs(names, facts) {
38
+ return {
39
+ [names.prefix]: facts.prefix,
40
+ [names.contentsLocked]: facts.contentsLocked,
41
+ [names.deletionLocked]: facts.deletionLocked,
42
+ [names.group]: facts.group,
43
+ [names.temporary]: facts.temporary,
44
+ [names.showingPlaceholder]: facts.showingPlaceholder
45
+ };
46
+ }
47
+ function controlFactsOf(names, attrs) {
48
+ const prefix = attrs[names.prefix];
49
+ return {
50
+ prefix: typeof prefix === "string" ? prefix : null,
51
+ contentsLocked: attrs[names.contentsLocked] === true,
52
+ deletionLocked: attrs[names.deletionLocked] === true,
53
+ group: attrs[names.group] === true,
54
+ temporary: attrs[names.temporary] === true,
55
+ showingPlaceholder: attrs[names.showingPlaceholder] === true
56
+ };
57
+ }
58
+ function controlAttrSpecs(names) {
59
+ return Object.fromEntries(
60
+ Object.entries(controlAttrs(names, NO_CONTROL)).map(([attr, value]) => [
61
+ attr,
62
+ { default: value }
63
+ ])
64
+ );
65
+ }
66
+ var drawn = (flag) => flag ? "1" : void 0;
67
+ function controlFlagsToDom(facts) {
68
+ return {
69
+ [FLAG_DOM_ATTRS.contentsLocked]: drawn(facts.contentsLocked),
70
+ [FLAG_DOM_ATTRS.deletionLocked]: drawn(facts.deletionLocked),
71
+ [FLAG_DOM_ATTRS.group]: drawn(facts.group),
72
+ [FLAG_DOM_ATTRS.temporary]: drawn(facts.temporary),
73
+ [FLAG_DOM_ATTRS.showingPlaceholder]: drawn(facts.showingPlaceholder)
74
+ };
75
+ }
76
+ function controlFlagsFromDom(dom) {
77
+ const set = (attr) => dom.getAttribute(attr) === "1";
78
+ return {
79
+ contentsLocked: set(FLAG_DOM_ATTRS.contentsLocked),
80
+ deletionLocked: set(FLAG_DOM_ATTRS.deletionLocked),
81
+ group: set(FLAG_DOM_ATTRS.group),
82
+ temporary: set(FLAG_DOM_ATTRS.temporary),
83
+ showingPlaceholder: set(FLAG_DOM_ATTRS.showingPlaceholder)
84
+ };
85
+ }
86
+ function controlToDom(facts) {
87
+ return {
88
+ [PREFIX_DOM_ATTR]: facts.prefix ?? void 0,
89
+ ...controlFlagsToDom(facts)
90
+ };
91
+ }
92
+ function controlClassName(facts, base, locked) {
93
+ return facts.contentsLocked || facts.group ? `${base} ${locked}` : base;
94
+ }
95
+ function controlAttrsFromDom(names, dom, prefix) {
96
+ return controlAttrs(names, { prefix, ...controlFlagsFromDom(dom) });
97
+ }
98
+ var WRAPPED_TABLE_ROLES = ["cell", "row"];
99
+ function isBlockControl(node) {
100
+ return node?.type.name === SDT_BLOCK_NODE;
101
+ }
102
+ function isEmptyBlockControl(node) {
103
+ return node?.type.name === SDT_EMPTY_BLOCK_NODE;
104
+ }
105
+ function isEmptyInlineControl(node) {
106
+ return node?.type.name === SDT_EMPTY_INLINE_NODE;
107
+ }
108
+ function controlAttrsOf(node) {
109
+ if (!node) return null;
110
+ if (isBlockControl(node) || isEmptyBlockControl(node) || isEmptyInlineControl(node)) {
111
+ return OWN_CONTROL_ATTRS;
112
+ }
113
+ const role = node.type.spec.tableRole;
114
+ if (typeof role !== "string" || !WRAPPED_TABLE_ROLES.includes(role)) {
115
+ return null;
116
+ }
117
+ return typeof node.attrs[WRAPPED_CONTROL_ATTRS.prefix] === "string" ? WRAPPED_CONTROL_ATTRS : null;
118
+ }
119
+ export {
120
+ NO_CONTROL,
121
+ OWN_CONTROL_ATTRS,
122
+ PREFIX_DOM_ATTR,
123
+ SDT_BLOCK_NODE,
124
+ SDT_EMPTY_BLOCK_NODE,
125
+ SDT_EMPTY_INLINE_NODE,
126
+ WRAPPED_CONTROL_ATTRS,
127
+ controlAttrSpecs,
128
+ controlAttrs,
129
+ controlAttrsFromDom,
130
+ controlAttrsOf,
131
+ controlClassName,
132
+ controlFactsOf,
133
+ controlToDom,
134
+ isBlockControl,
135
+ isEmptyBlockControl,
136
+ isEmptyInlineControl
137
+ };
@@ -0,0 +1,22 @@
1
+ /**
2
+ * The edge of a block-level content control, which a keystroke's join may not carry blocks across
3
+ * (`spec/notes/contentControls.md`).
4
+ *
5
+ * A control is not isolating (`./docxSchema`), so ProseMirror's base keymap will join across its
6
+ * edge: the join a keystroke builds there may not carry blocks into a control or out of one, and
7
+ * it is refused here rather than in the keymap, as one rule the whole guard list asks (`./guards`).
8
+ *
9
+ * Judging one step at a time is what confines the rule to the join. A cut and a paste, and the
10
+ * drag that is the two in one transaction, are the user saying where the text goes; taking the
11
+ * control away whole carries nothing across at all, and `./locks` answers that under the deletion
12
+ * clause.
13
+ */
14
+ import type { StepGuard } from "./editGuard";
15
+ /**
16
+ * The edges of the document's block controls, as `./guards` registers them.
17
+ *
18
+ * The history pass lifts it for the reason it lifts the locks: every step replayed is the reverse
19
+ * of one that passed the guard when it was made, and a control the user deleted whole is put back
20
+ * whole by the step that undoes it.
21
+ */
22
+ export declare const controlEdgeGuard: StepGuard;
@@ -0,0 +1,50 @@
1
+ // src/schema/controlEdges.ts
2
+ import {
3
+ ReplaceAroundStep,
4
+ ReplaceStep
5
+ } from "prosemirror-transform";
6
+ import { isBlockControl } from "./controlAttrs.js";
7
+ import { controlLifted, historyReplay } from "./locks.js";
8
+ function controlsAround($pos) {
9
+ const starts = [];
10
+ for (let depth = 1; depth <= $pos.depth; depth += 1) {
11
+ if (isBlockControl($pos.node(depth))) starts.push($pos.before(depth));
12
+ }
13
+ return starts;
14
+ }
15
+ function sameControls(a, b) {
16
+ return a.length === b.length && a.every((start, index) => start === b[index]);
17
+ }
18
+ function crossesAnEdge(doc, from, to) {
19
+ if (from === to) return false;
20
+ return !sameControls(
21
+ controlsAround(doc.resolve(from)),
22
+ controlsAround(doc.resolve(to))
23
+ );
24
+ }
25
+ function movesGapAcross(step, before, after) {
26
+ if (!(step instanceof ReplaceAroundStep)) return false;
27
+ const map = step.getMap();
28
+ const was = controlsAround(before.resolve(step.gapFrom)).map(
29
+ (start) => map.map(start, -1)
30
+ );
31
+ return !sameControls(
32
+ was,
33
+ controlsAround(after.resolve(step.from + step.insert))
34
+ );
35
+ }
36
+ function stepAllowed(step, before, after) {
37
+ if (!(step instanceof ReplaceStep || step instanceof ReplaceAroundStep)) {
38
+ return true;
39
+ }
40
+ return !crossesAnEdge(before, step.from, step.to) && !movesGapAcross(step, before, after);
41
+ }
42
+ var controlEdgeGuard = {
43
+ name: "controlEdge",
44
+ liftedBy: [controlLifted, historyReplay],
45
+ step: (step, before, after) => stepAllowed(step, before, after),
46
+ shuts: (intent, state) => intent.kind === "replace" && crossesAnEdge(state.doc, intent.from, intent.to)
47
+ };
48
+ export {
49
+ controlEdgeGuard
50
+ };
@@ -23,4 +23,4 @@ export declare function isPageBreak(brAttrs: unknown): boolean;
23
23
  * ends the line where it stood, and `chip` draws a small box naming the element.
24
24
  */
25
25
  export type PreservedDisplay = "hidden" | "text" | "break" | "chip";
26
- export declare const docxSchema: Schema<"commentEnd" | "commentReference" | "commentStart" | "doc" | "hardBreak" | "image" | "noteReference" | "paragraph" | "rawBlock" | "rawInline" | "rawRunContent" | "table" | "tableCell" | "tableRow" | "text", "link" | "run" | "sdt" | "tab">;
26
+ export declare const docxSchema: Schema<"commentEnd" | "commentReference" | "commentStart" | "doc" | "hardBreak" | "image" | "noteReference" | "paragraph" | "rawBlock" | "rawInline" | "rawRunContent" | "sdtBlock" | "sdtEmpty" | "sdtEmptyInline" | "table" | "tableCell" | "tableRow" | "text", "link" | "run" | "sdt" | "tab">;
@@ -34,6 +34,19 @@ import {
34
34
  rowStyle,
35
35
  tableStyle
36
36
  } from "../styles/inlineStyle.js";
37
+ import {
38
+ controlAttrSpecs,
39
+ controlAttrsFromDom,
40
+ controlClassName,
41
+ controlFactsOf,
42
+ controlToDom,
43
+ OWN_CONTROL_ATTRS,
44
+ PREFIX_DOM_ATTR,
45
+ SDT_BLOCK_NODE,
46
+ SDT_EMPTY_BLOCK_NODE,
47
+ SDT_EMPTY_INLINE_NODE,
48
+ WRAPPED_CONTROL_ATTRS
49
+ } from "./controlAttrs.js";
37
50
  import { imageNodeSpec, runMarkSpec } from "./rendering.js";
38
51
  import { noteName } from "./stories.js";
39
52
  import { WRAPPER_ATTRS, WRAPPER_GROUP } from "./wrappers.js";
@@ -379,6 +392,12 @@ var docxSchema = new Schema({
379
392
  /** The whole `<w:trPr>...</w:trPr>` XML */
380
393
  trPr: { default: null },
381
394
  format: { default: null },
395
+ /**
396
+ * What the content control this row sat inside states (`CT_SdtRow`, §17.5.2.30), under the
397
+ * same `sdt`-prefixed names a wrapped cell carries (`./controlAttrs`), since both are one
398
+ * node the file put a `w:sdt` around.
399
+ */
400
+ ...controlAttrSpecs(WRAPPED_CONTROL_ATTRS),
382
401
  /** The markers that stood ahead of this row's first cell (`docx/importTable`) */
383
402
  leadingXml: { default: null },
384
403
  /** The markers that stood between this row and the next one, under the table */
@@ -386,15 +405,21 @@ var docxSchema = new Schema({
386
405
  },
387
406
  toDOM(node) {
388
407
  const format = toRowFormat(node.attrs.format);
408
+ const control = controlFactsOf(WRAPPED_CONTROL_ATTRS, node.attrs);
389
409
  return [
390
410
  "tr",
391
411
  {
392
- class: editorClassNames.tableRow,
412
+ class: controlClassName(
413
+ control,
414
+ editorClassNames.tableRow,
415
+ editorClassNames.rowLocked
416
+ ),
393
417
  style: rowStyle(format),
394
418
  "data-trattrs": text(node.attrs.trAttrs),
395
419
  "data-tblprex": text(node.attrs.tblPrEx),
396
420
  "data-trpr": text(node.attrs.trPr),
397
421
  "data-fmt": formatJson(format),
422
+ ...controlToDom(control),
398
423
  "data-leading": text(node.attrs.leadingXml),
399
424
  "data-trailing": text(node.attrs.trailingXml)
400
425
  },
@@ -408,9 +433,10 @@ var docxSchema = new Schema({
408
433
  const trAttrs = rawXml(dom, "data-trattrs", ATTRIBUTES);
409
434
  const tblPrEx = rawXml(dom, "data-tblprex", ELEMENT("tblPrEx"));
410
435
  const trPr = rawXml(dom, "data-trpr", ELEMENT("trPr"));
436
+ const sdtPrefix = rawXml(dom, PREFIX_DOM_ATTR, SDT_PREFIX);
411
437
  const leadingXml = rawXml(dom, "data-leading", ANY_ELEMENTS);
412
438
  const trailingXml = rawXml(dom, "data-trailing", ANY_ELEMENTS);
413
- if (trAttrs === false || tblPrEx === false || trPr === false || leadingXml === false || trailingXml === false) {
439
+ if (trAttrs === false || tblPrEx === false || trPr === false || sdtPrefix === false || leadingXml === false || trailingXml === false) {
414
440
  return false;
415
441
  }
416
442
  return {
@@ -418,6 +444,7 @@ var docxSchema = new Schema({
418
444
  tblPrEx,
419
445
  trPr,
420
446
  format: toRowFormat(parseJson(dom.getAttribute("data-fmt"))),
447
+ ...controlAttrsFromDom(WRAPPED_CONTROL_ATTRS, dom, sdtPrefix),
421
448
  leadingXml,
422
449
  trailingXml
423
450
  };
@@ -445,15 +472,13 @@ var docxSchema = new Schema({
445
472
  /** The cell width read out of tcPr */
446
473
  tcW: { default: null },
447
474
  format: { default: null },
448
- /** The opening XML of the `<w:sdt>` content control this cell sat inside */
449
- sdtPrefix: { default: null },
450
475
  /**
451
- * The two clauses of that control's lock, which mean the same as `contentsLocked` and
452
- * `deletionLocked` on the sdt mark. Both only ever stand alongside an `sdtPrefix`, since
453
- * the lock lives inside that very XML.
476
+ * What the content control this cell sat inside states, under names of its own so that it
477
+ * stands beside the cell's (`./controlAttrs`). The lock's two clauses mean the same as
478
+ * `contentsLocked` and `deletionLocked` on the sdt mark, and all of them only ever stand
479
+ * alongside an `sdtPrefix`, since the control's own XML is where they are read from.
454
480
  */
455
- sdtContentsLocked: { default: false },
456
- sdtDeletionLocked: { default: false },
481
+ ...controlAttrSpecs(WRAPPED_CONTROL_ATTRS),
457
482
  /** The markers that stood between this cell and the next one, under the row */
458
483
  trailingXml: { default: null }
459
484
  },
@@ -462,11 +487,15 @@ var docxSchema = new Schema({
462
487
  const colspan = spanCount(node.attrs.colspan);
463
488
  const rowspan = spanCount(node.attrs.rowspan);
464
489
  const colwidth = toColWidth(node.attrs.colwidth);
465
- const locked = node.attrs.sdtContentsLocked === true;
490
+ const control = controlFactsOf(WRAPPED_CONTROL_ATTRS, node.attrs);
466
491
  return [
467
492
  "td",
468
493
  {
469
- class: locked ? `${editorClassNames.tableCell} ${editorClassNames.cellLocked}` : editorClassNames.tableCell,
494
+ class: controlClassName(
495
+ control,
496
+ editorClassNames.tableCell,
497
+ editorClassNames.cellLocked
498
+ ),
470
499
  style: cellStyle(format),
471
500
  colspan: colspan > 1 ? `${colspan}` : void 0,
472
501
  rowspan: rowspan > 1 ? `${rowspan}` : void 0,
@@ -475,9 +504,7 @@ var docxSchema = new Schema({
475
504
  "data-tcpr": text(node.attrs.tcPr),
476
505
  "data-tcw": formatJson(toTableWidth(node.attrs.tcW)),
477
506
  "data-fmt": formatJson(format),
478
- "data-sdt-prefix": text(node.attrs.sdtPrefix),
479
- "data-sdt-contents-locked": locked ? "1" : void 0,
480
- "data-sdt-deletion-locked": node.attrs.sdtDeletionLocked === true ? "1" : void 0,
507
+ ...controlToDom(control),
481
508
  "data-trailing": text(node.attrs.trailingXml)
482
509
  },
483
510
  0
@@ -489,7 +516,7 @@ var docxSchema = new Schema({
489
516
  getAttrs: (dom) => {
490
517
  const tcAttrs = rawXml(dom, "data-tcattrs", ATTRIBUTES);
491
518
  const tcPr = rawXml(dom, "data-tcpr", ELEMENT("tcPr"));
492
- const sdtPrefix = rawXml(dom, "data-sdt-prefix", SDT_PREFIX);
519
+ const sdtPrefix = rawXml(dom, PREFIX_DOM_ATTR, SDT_PREFIX);
493
520
  const trailingXml = rawXml(dom, "data-trailing", ANY_ELEMENTS);
494
521
  if (tcAttrs === false || tcPr === false || sdtPrefix === false || trailingXml === false) {
495
522
  return false;
@@ -504,15 +531,132 @@ var docxSchema = new Schema({
504
531
  tcPr,
505
532
  tcW: toTableWidth(parseJson(dom.getAttribute("data-tcw"))),
506
533
  format: toCellFormat(parseJson(dom.getAttribute("data-fmt"))),
507
- sdtPrefix,
508
- sdtContentsLocked: dom.getAttribute("data-sdt-contents-locked") === "1",
509
- sdtDeletionLocked: dom.getAttribute("data-sdt-deletion-locked") === "1",
534
+ ...controlAttrsFromDom(WRAPPED_CONTROL_ATTRS, dom, sdtPrefix),
510
535
  trailingXml
511
536
  };
512
537
  }
513
538
  }
514
539
  ]
515
540
  },
541
+ /**
542
+ * The blocks a block-level content control (`w:sdt` under the body, a cell, or another
543
+ * control) holds, as a container of its own.
544
+ *
545
+ * How deeply controls are nested is what the tree says here, where the inline mark has to
546
+ * carry it as `depth`, since several marks stand on one and the same text.
547
+ */
548
+ [SDT_BLOCK_NODE]: {
549
+ group: "block modelled",
550
+ content: "block+",
551
+ // A control may be deleted whole when its lock allows it, so it has to be selectable; it is
552
+ // not isolating, because an isolating node is one a deletion cannot reach across at all
553
+ selectable: true,
554
+ attrs: {
555
+ /**
556
+ * The fragment of the session this control was opened from, as a table names its own.
557
+ * null for one opened inside a cell or inside another control, which was never a fragment
558
+ * of its own.
559
+ */
560
+ srcId: { default: null },
561
+ /** Which control of the document this is, counted as the file was opened (`docx/wrappers`) */
562
+ key: { default: 0 },
563
+ /**
564
+ * What the control states, under the same names the inline mark carries them by
565
+ * (`./controlAttrs`): the opening XML of the `<w:sdt>`, the two clauses of its lock,
566
+ * whether it is a `w:group`, and what it says about outliving an edit of its contents
567
+ * (`editor/plugins/controlLifecycle`).
568
+ */
569
+ ...controlAttrSpecs(OWN_CONTROL_ATTRS)
570
+ },
571
+ toDOM(node) {
572
+ const control = controlFactsOf(OWN_CONTROL_ATTRS, node.attrs);
573
+ return [
574
+ "div",
575
+ {
576
+ class: controlClassName(
577
+ control,
578
+ editorClassNames.sdtBlock,
579
+ editorClassNames.sdtLocked
580
+ ),
581
+ "data-src": text(node.attrs.srcId),
582
+ "data-key": numberText(node.attrs.key),
583
+ ...controlToDom(control)
584
+ },
585
+ 0
586
+ ];
587
+ },
588
+ parseDOM: [
589
+ {
590
+ tag: `div.${editorClassNames.sdtBlock}`,
591
+ getAttrs: (dom) => {
592
+ const prefix = rawXml(dom, PREFIX_DOM_ATTR, SDT_PREFIX);
593
+ if (prefix === null || prefix === false) return false;
594
+ return {
595
+ srcId: srcIdOf(dom),
596
+ key: parseInt10(dom.getAttribute("data-key"), 0),
597
+ ...controlAttrsFromDom(OWN_CONTROL_ATTRS, dom, prefix)
598
+ };
599
+ }
600
+ }
601
+ ]
602
+ },
603
+ /**
604
+ * A content control holding nothing at all, which is a `w:sdtContent` with no element inside
605
+ * it or none written at all (§17.5.2.34).
606
+ *
607
+ * It is no container: `block+` has no way to say "nothing", and a paragraph made up for it
608
+ * would write a blank line the file never held. So it stands as an atom that draws nothing,
609
+ * which is what a server's condition-failed clause is meant to look like, and what it states
610
+ * about itself rides back out in the same attributes the container carries.
611
+ */
612
+ [SDT_EMPTY_BLOCK_NODE]: {
613
+ group: "block modelled",
614
+ atom: true,
615
+ // It draws nothing, so a node selection would put the user in a state with nothing on screen
616
+ // to show it: it goes with a stretch that covers it instead
617
+ selectable: false,
618
+ attrs: {
619
+ /**
620
+ * The fragment of the session this control was opened from, as the container names its
621
+ * own. null for one opened inside a cell or inside another control.
622
+ */
623
+ srcId: { default: null },
624
+ /** Which control of the document this is, counted as the file was opened (`docx/wrappers`) */
625
+ key: { default: 0 },
626
+ /** What the control states, under the names the container carries them by (`./controlAttrs`) */
627
+ ...controlAttrSpecs(OWN_CONTROL_ATTRS)
628
+ },
629
+ toDOM(node) {
630
+ const control = controlFactsOf(OWN_CONTROL_ATTRS, node.attrs);
631
+ return [
632
+ "div",
633
+ {
634
+ class: controlClassName(
635
+ control,
636
+ editorClassNames.sdtEmpty,
637
+ editorClassNames.sdtLocked
638
+ ),
639
+ "data-src": text(node.attrs.srcId),
640
+ "data-key": numberText(node.attrs.key),
641
+ ...controlToDom(control)
642
+ }
643
+ ];
644
+ },
645
+ parseDOM: [
646
+ {
647
+ tag: `div.${editorClassNames.sdtEmpty}`,
648
+ getAttrs: (dom) => {
649
+ const prefix = rawXml(dom, PREFIX_DOM_ATTR, SDT_PREFIX);
650
+ if (prefix === null || prefix === false) return false;
651
+ return {
652
+ srcId: srcIdOf(dom),
653
+ key: parseInt10(dom.getAttribute("data-key"), 0),
654
+ ...controlAttrsFromDom(OWN_CONTROL_ATTRS, dom, prefix)
655
+ };
656
+ }
657
+ }
658
+ ]
659
+ },
516
660
  /**
517
661
  * The one node a block the editor has no model for stands as, wherever it stood: under the
518
662
  * body, or inside a table cell.
@@ -861,6 +1005,60 @@ var docxSchema = new Schema({
861
1005
  }
862
1006
  ]
863
1007
  },
1008
+ /**
1009
+ * A content control holding nothing that the file wrote inside a paragraph, which is the
1010
+ * `sdtEmpty` node's inline counterpart (§17.5.2.34).
1011
+ *
1012
+ * The inline control is otherwise a mark on the runs it wrapped (`./wrappers`), and a control
1013
+ * holding no run leaves a mark nothing to sit on. So it stands as an atom of its own, taking
1014
+ * no width between the words either side of it, and what it states about itself rides back out
1015
+ * in the attributes every other carrier uses.
1016
+ *
1017
+ * It wears the wrapper marks rather than carrying a `depth` of its own: what it stands inside
1018
+ * is what has to close around it on export, while the control it is has nothing to nest.
1019
+ */
1020
+ [SDT_EMPTY_INLINE_NODE]: {
1021
+ group: "inline",
1022
+ inline: true,
1023
+ atom: true,
1024
+ marks: "wrapper",
1025
+ // No selection settles on it, for the reason `sdtEmpty` above states
1026
+ selectable: false,
1027
+ attrs: {
1028
+ /** Which control of the document this is, counted as the file was opened (`docx/wrappers`) */
1029
+ key: { default: 0 },
1030
+ /** What the control states, under the names every carrier holds them by (`./controlAttrs`) */
1031
+ ...controlAttrSpecs(OWN_CONTROL_ATTRS)
1032
+ },
1033
+ toDOM(node) {
1034
+ const control = controlFactsOf(OWN_CONTROL_ATTRS, node.attrs);
1035
+ return [
1036
+ "span",
1037
+ {
1038
+ class: controlClassName(
1039
+ control,
1040
+ editorClassNames.sdtEmptyInline,
1041
+ editorClassNames.sdtLocked
1042
+ ),
1043
+ "data-key": numberText(node.attrs.key),
1044
+ ...controlToDom(control)
1045
+ }
1046
+ ];
1047
+ },
1048
+ parseDOM: [
1049
+ {
1050
+ tag: `span.${editorClassNames.sdtEmptyInline}`,
1051
+ getAttrs: (dom) => {
1052
+ const prefix = rawXml(dom, PREFIX_DOM_ATTR, SDT_PREFIX);
1053
+ if (prefix === null || prefix === false) return false;
1054
+ return {
1055
+ key: parseInt10(dom.getAttribute("data-key"), 0),
1056
+ ...controlAttrsFromDom(OWN_CONTROL_ATTRS, dom, prefix)
1057
+ };
1058
+ }
1059
+ }
1060
+ ]
1061
+ },
864
1062
  /** The node that carries a non-run element inside a paragraph (a bookmark, for instance) exactly as it came */
865
1063
  rawInline: {
866
1064
  group: "inline",
@@ -904,31 +1102,33 @@ var docxSchema = new Schema({
904
1102
  // which stands inside which is what `depth` says (`./wrappers`)
905
1103
  excludes: "",
906
1104
  attrs: {
907
- /** The opening XML of the `<w:sdt>`, the same string a wrapped cell carries */
908
- sdtPrefix: { default: null },
909
1105
  ...WRAPPER_ATTRS,
910
1106
  /**
911
- * The two clauses of the control's lock: whether its contents may not be edited, and
912
- * whether the control itself may not be deleted, not even whole. The `w:lock` inside
913
- * sdtPrefix is where both come from, and reading it once on import saves parsing that
914
- * string again on every draw.
915
- * The two are independent: a control may be un-editable yet removable, or editable yet
916
- * not removable.
1107
+ * What the control states, under the names a block-level control carries them by too
1108
+ * (`./controlAttrs`): the opening XML of the `<w:sdt>`, which a wrapped cell carries as
1109
+ * well; the two clauses of its lock, which are independent, so a control may be
1110
+ * un-editable yet removable or editable yet not removable; whether it is a `w:group`,
1111
+ * which shuts its contents without a `w:lock` saying so and which the editor never writes
1112
+ * (`spec/notes/contentControls.md`); and `w:temporary` (§17.5.2.43) and `w:showingPlcHdr`
1113
+ * (§17.5.2.39), which `editor/plugins/controlLifecycle` acts on.
1114
+ * All of them are read off the prefix on import, which saves parsing that string again on
1115
+ * every draw.
917
1116
  */
918
- contentsLocked: { default: false },
919
- deletionLocked: { default: false }
1117
+ ...controlAttrSpecs(OWN_CONTROL_ATTRS)
920
1118
  },
921
1119
  toDOM(mark) {
922
- const locked = mark.attrs.contentsLocked === true;
1120
+ const control = controlFactsOf(OWN_CONTROL_ATTRS, mark.attrs);
923
1121
  return [
924
1122
  "span",
925
1123
  {
926
- class: locked ? `${editorClassNames.sdt} ${editorClassNames.sdtLocked}` : editorClassNames.sdt,
927
- "data-sdt-prefix": text(mark.attrs.sdtPrefix),
1124
+ class: controlClassName(
1125
+ control,
1126
+ editorClassNames.sdt,
1127
+ editorClassNames.sdtLocked
1128
+ ),
928
1129
  "data-key": numberText(mark.attrs.key),
929
1130
  "data-depth": numberText(mark.attrs.depth),
930
- "data-sdt-contents-locked": locked ? "1" : void 0,
931
- "data-sdt-deletion-locked": mark.attrs.deletionLocked === true ? "1" : void 0
1131
+ ...controlToDom(control)
932
1132
  },
933
1133
  0
934
1134
  ];
@@ -937,14 +1137,12 @@ var docxSchema = new Schema({
937
1137
  {
938
1138
  tag: `span.${editorClassNames.sdt}`,
939
1139
  getAttrs: (dom) => {
940
- const prefix = rawXml(dom, "data-sdt-prefix", SDT_PREFIX);
1140
+ const prefix = rawXml(dom, PREFIX_DOM_ATTR, SDT_PREFIX);
941
1141
  if (prefix === null || prefix === false) return false;
942
1142
  return {
943
- sdtPrefix: prefix,
944
1143
  key: parseInt10(dom.getAttribute("data-key"), 0),
945
1144
  depth: parseInt10(dom.getAttribute("data-depth"), 0),
946
- contentsLocked: dom.getAttribute("data-sdt-contents-locked") === "1",
947
- deletionLocked: dom.getAttribute("data-sdt-deletion-locked") === "1"
1145
+ ...controlAttrsFromDom(OWN_CONTROL_ATTRS, dom, prefix)
948
1146
  };
949
1147
  }
950
1148
  }
@@ -46,7 +46,7 @@ export type EditIntent =
46
46
  * against the places it puts that guard to the test, and a place naming a guard that no longer
47
47
  * stands has to be a mistake the compiler catches rather than an annotation answering for nothing.
48
48
  */
49
- export type EditGuardName = "protection" | "lock" | "preserved" | "section";
49
+ export type EditGuardName = "protection" | "lock" | "controlEdge" | "preserved" | "section";
50
50
  /** What every guard answers, whichever of the two judgements it is written as */
51
51
  interface GuardCommon {
52
52
  readonly name: EditGuardName;
@@ -1,4 +1,5 @@
1
1
  // src/schema/guards.ts
2
+ import { controlEdgeGuard } from "./controlEdges.js";
2
3
  import { changesOnlyDisplayAttrs, displayOnly } from "./displayDerivation.js";
3
4
  import {
4
5
  transactionReaches
@@ -36,6 +37,7 @@ var protectionGuard = {
36
37
  var EDIT_GUARDS = [
37
38
  protectionGuard,
38
39
  lockGuard,
40
+ controlEdgeGuard,
39
41
  preservedGuard,
40
42
  sectionGuard
41
43
  ];