@portone/docx-editor 0.2.1 → 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.
Files changed (253) hide show
  1. package/CHANGELOG.md +155 -0
  2. package/CONTRIBUTING.md +5 -1
  3. package/dist/DocxEditor.d.ts +6 -0
  4. package/dist/DocxEditor.js +13 -29
  5. package/dist/core.d.ts +23 -8
  6. package/dist/core.js +9 -4
  7. package/dist/docx/cloning.d.ts +38 -0
  8. package/dist/docx/cloning.js +74 -0
  9. package/dist/docx/commentOnlyChange.d.ts +12 -7
  10. package/dist/docx/commentOnlyChange.js +9 -156
  11. package/dist/docx/comments/constants.d.ts +0 -1
  12. package/dist/docx/comments/constants.js +0 -2
  13. package/dist/docx/comments/grammar.d.ts +10 -4
  14. package/dist/docx/comments/grammar.js +12 -2
  15. package/dist/docx/comments/{verifying.d.ts → parts.d.ts} +26 -18
  16. package/dist/docx/comments/parts.js +279 -0
  17. package/dist/docx/comments/people.d.ts +11 -2
  18. package/dist/docx/comments/people.js +16 -92
  19. package/dist/docx/comments/policy.d.ts +7 -0
  20. package/dist/docx/comments/policy.js +43 -0
  21. package/dist/docx/comments/reading.js +8 -10
  22. package/dist/docx/comments/writing.d.ts +19 -7
  23. package/dist/docx/comments/writing.js +59 -112
  24. package/dist/docx/documentSettings.d.ts +7 -0
  25. package/dist/docx/documentSettings.js +10 -2
  26. package/dist/docx/exportDocx.d.ts +40 -5
  27. package/dist/docx/exportDocx.js +88 -74
  28. package/dist/docx/exportRefs.d.ts +5 -2
  29. package/dist/docx/exportRefs.js +3 -1
  30. package/dist/docx/fidelity.d.ts +43 -0
  31. package/dist/docx/fidelity.js +64 -0
  32. package/dist/docx/formatting/attrs.d.ts +45 -0
  33. package/dist/docx/formatting/attrs.js +86 -0
  34. package/dist/docx/formatting/context.d.ts +39 -0
  35. package/dist/docx/formatting/context.js +60 -0
  36. package/dist/docx/formatting/direct.d.ts +6 -5
  37. package/dist/docx/formatting/direct.js +52 -78
  38. package/dist/docx/formatting/resolve.d.ts +51 -0
  39. package/dist/docx/formatting/resolve.js +160 -0
  40. package/dist/docx/formatting/runProperties.d.ts +93 -0
  41. package/dist/docx/formatting/runProperties.js +316 -0
  42. package/dist/docx/formatting/styles.d.ts +25 -2
  43. package/dist/docx/formatting/styles.js +87 -6
  44. package/dist/docx/formatting/tabStops.js +7 -22
  45. package/dist/docx/formatting.d.ts +4 -1
  46. package/dist/docx/formatting.js +4 -1
  47. package/dist/docx/headersFooters.js +6 -13
  48. package/dist/docx/hyperlink.d.ts +14 -4
  49. package/dist/docx/hyperlink.js +4 -1
  50. package/dist/docx/identities.d.ts +67 -0
  51. package/dist/docx/identities.js +174 -0
  52. package/dist/docx/importDocx.d.ts +12 -1
  53. package/dist/docx/importDocx.js +90 -88
  54. package/dist/docx/importParagraph.d.ts +1 -1
  55. package/dist/docx/importParagraph.js +4 -3
  56. package/dist/docx/importTable.d.ts +2 -2
  57. package/dist/docx/importTable.js +69 -56
  58. package/dist/docx/invariants.d.ts +33 -0
  59. package/dist/docx/invariants.js +269 -0
  60. package/dist/docx/media.d.ts +6 -4
  61. package/dist/docx/media.js +6 -37
  62. package/dist/docx/newLists.d.ts +50 -0
  63. package/dist/docx/newLists.js +59 -0
  64. package/dist/docx/notes.js +3 -7
  65. package/dist/docx/numberingPlanner.d.ts +11 -0
  66. package/dist/docx/numberingPlanner.js +50 -0
  67. package/dist/docx/packageParts.d.ts +42 -0
  68. package/dist/docx/packageParts.js +135 -0
  69. package/dist/docx/pageGeometry.d.ts +2 -0
  70. package/dist/docx/pageGeometry.js +18 -9
  71. package/dist/docx/paraProps.d.ts +10 -22
  72. package/dist/docx/paraProps.js +47 -76
  73. package/dist/docx/partPlan.d.ts +36 -0
  74. package/dist/docx/partPlan.js +59 -0
  75. package/dist/docx/protectionPolicy.d.ts +127 -0
  76. package/dist/docx/protectionPolicy.js +169 -0
  77. package/dist/docx/relationships.d.ts +1 -1
  78. package/dist/docx/relationships.js +8 -13
  79. package/dist/docx/runProps.d.ts +9 -22
  80. package/dist/docx/runProps.js +15 -168
  81. package/dist/docx/scan.js +20 -52
  82. package/dist/docx/sdt.js +9 -34
  83. package/dist/docx/sdtProps.d.ts +8 -1
  84. package/dist/docx/sdtProps.js +10 -0
  85. package/dist/docx/serializeBlock.d.ts +2 -0
  86. package/dist/docx/serializeBlock.js +8 -5
  87. package/dist/docx/serializeTable.js +32 -15
  88. package/dist/docx/session.d.ts +42 -14
  89. package/dist/docx/session.js +53 -17
  90. package/dist/docx/tableFormatting/conditions.d.ts +76 -0
  91. package/dist/docx/tableFormatting/conditions.js +196 -0
  92. package/dist/docx/tableFormatting/editing.d.ts +7 -3
  93. package/dist/docx/tableFormatting/editing.js +111 -143
  94. package/dist/docx/tableFormatting/reading.d.ts +63 -23
  95. package/dist/docx/tableFormatting/reading.js +132 -38
  96. package/dist/docx/tableFormatting.d.ts +1 -0
  97. package/dist/docx/tableFormatting.js +1 -0
  98. package/dist/docx/tableTemplate.js +30 -19
  99. package/dist/download.d.ts +8 -5
  100. package/dist/download.js +2 -0
  101. package/dist/editor/clipboard/inlineFormatting.js +19 -30
  102. package/dist/editor/commands/comments/editing.d.ts +6 -2
  103. package/dist/editor/commands/comments/editing.js +19 -31
  104. package/dist/editor/commands/exportQueries.d.ts +15 -0
  105. package/dist/editor/commands/exportQueries.js +14 -0
  106. package/dist/editor/commands/fidelityQueries.d.ts +12 -0
  107. package/dist/editor/commands/fidelityQueries.js +8 -0
  108. package/dist/editor/commands/formatting/editing.d.ts +2 -2
  109. package/dist/editor/commands/formatting/editing.js +26 -108
  110. package/dist/editor/commands/formatting/propertyCommands.d.ts +21 -0
  111. package/dist/editor/commands/formatting/propertyCommands.js +108 -0
  112. package/dist/editor/commands/formatting/shared.d.ts +3 -3
  113. package/dist/editor/commands/formatting/shared.js +5 -2
  114. package/dist/editor/commands/indentCommands.js +5 -4
  115. package/dist/editor/commands/index.d.ts +11 -0
  116. package/dist/editor/commands/index.js +5 -0
  117. package/dist/editor/commands/linkCommands.js +5 -6
  118. package/dist/editor/commands/listCommands.d.ts +6 -3
  119. package/dist/editor/commands/listCommands.js +73 -30
  120. package/dist/editor/commands/lockCommands.d.ts +7 -1
  121. package/dist/editor/commands/lockCommands.js +35 -46
  122. package/dist/editor/commands/paragraphCommands.js +33 -31
  123. package/dist/editor/commands/spacingCommands.js +1 -1
  124. package/dist/editor/createEditor.d.ts +17 -29
  125. package/dist/editor/createEditor.js +58 -58
  126. package/dist/editor/documentStyles.d.ts +11 -35
  127. package/dist/editor/documentStyles.js +9 -52
  128. package/dist/editor/editorDocument.d.ts +57 -0
  129. package/dist/editor/editorDocument.js +86 -0
  130. package/dist/editor/externalClipboard.d.ts +13 -2
  131. package/dist/editor/externalClipboard.js +76 -75
  132. package/dist/editor/insertTable.js +4 -3
  133. package/dist/editor/paragraphEdits.d.ts +27 -19
  134. package/dist/editor/paragraphEdits.js +18 -19
  135. package/dist/editor/paragraphPlacement.d.ts +11 -0
  136. package/dist/editor/paragraphPlacement.js +18 -0
  137. package/dist/editor/plugins/displayDerivation.d.ts +47 -0
  138. package/dist/editor/plugins/displayDerivation.js +78 -0
  139. package/dist/editor/plugins/imagePaste.js +11 -7
  140. package/dist/editor/plugins/keymap.js +18 -3
  141. package/dist/editor/plugins/numberingDecorations.d.ts +14 -14
  142. package/dist/editor/plugins/numberingDecorations.js +50 -19
  143. package/dist/editor/plugins/paragraphDisplay.d.ts +7 -0
  144. package/dist/editor/plugins/paragraphDisplay.js +57 -0
  145. package/dist/editor/plugins/tabLayout.js +1 -1
  146. package/dist/editor/plugins/tableDisplay.d.ts +6 -0
  147. package/dist/editor/plugins/tableDisplay.js +30 -0
  148. package/dist/index.d.ts +2 -0
  149. package/dist/model/format.d.ts +80 -8
  150. package/dist/model/format.js +100 -22
  151. package/dist/model/tabStops.d.ts +9 -0
  152. package/dist/model/tabStops.js +18 -0
  153. package/dist/numbering/listRegistry.d.ts +37 -0
  154. package/dist/numbering/listRegistry.js +112 -0
  155. package/dist/numbering/listTemplate.d.ts +26 -17
  156. package/dist/numbering/listTemplate.js +88 -25
  157. package/dist/numbering/markers.d.ts +15 -6
  158. package/dist/numbering/markers.js +26 -59
  159. package/dist/numbering/parseNumbering.d.ts +84 -6
  160. package/dist/numbering/parseNumbering.js +131 -44
  161. package/dist/numbering/spellers.d.ts +15 -0
  162. package/dist/numbering/spellers.js +96 -0
  163. package/dist/numbering/writeNumbering.d.ts +8 -7
  164. package/dist/numbering/writeNumbering.js +28 -39
  165. package/dist/ooxml/childOrder.d.ts +34 -0
  166. package/dist/ooxml/childOrder.js +496 -0
  167. package/dist/ooxml/conformance.d.ts +28 -0
  168. package/dist/ooxml/conformance.js +20 -0
  169. package/dist/ooxml/element.d.ts +24 -11
  170. package/dist/ooxml/element.js +31 -12
  171. package/dist/ooxml/errors.d.ts +6 -5
  172. package/dist/ooxml/image.d.ts +4 -0
  173. package/dist/ooxml/image.js +2 -5
  174. package/dist/ooxml/names.d.ts +9 -0
  175. package/dist/ooxml/names.js +8 -1
  176. package/dist/ooxml/partSplice.d.ts +67 -0
  177. package/dist/ooxml/partSplice.js +176 -0
  178. package/dist/ooxml/props.d.ts +112 -0
  179. package/dist/{docx/propsXml.js → ooxml/props.js} +72 -200
  180. package/dist/ooxml/simpleTypes.d.ts +103 -0
  181. package/dist/ooxml/simpleTypes.js +182 -0
  182. package/dist/ooxml/tabStops.js +8 -27
  183. package/dist/ooxml/tagScan.d.ts +34 -0
  184. package/dist/ooxml/tagScan.js +108 -0
  185. package/dist/ooxml/units.d.ts +25 -13
  186. package/dist/ooxml/units.js +65 -31
  187. package/dist/ooxml/xml.d.ts +35 -15
  188. package/dist/ooxml/xml.js +43 -11
  189. package/dist/page/PageGuides.d.ts +1 -1
  190. package/dist/page/PageGuides.js +0 -13
  191. package/dist/page/blockKinds.d.ts +61 -0
  192. package/dist/page/blockKinds.js +11 -0
  193. package/dist/page/kinds/index.d.ts +6 -0
  194. package/dist/page/kinds/index.js +10 -0
  195. package/dist/page/kinds/paragraphKind.d.ts +10 -0
  196. package/dist/page/kinds/paragraphKind.js +71 -0
  197. package/dist/page/kinds/tableKind.d.ts +10 -0
  198. package/dist/page/kinds/tableKind.js +178 -0
  199. package/dist/page/measureBlocks.d.ts +3 -14
  200. package/dist/page/measureBlocks.js +24 -45
  201. package/dist/page/pageDecorations.d.ts +27 -44
  202. package/dist/page/pageDecorations.js +70 -152
  203. package/dist/page/pageLayout.d.ts +2 -29
  204. package/dist/page/pageLayout.js +39 -16
  205. package/dist/page/usePageLayout.d.ts +2 -24
  206. package/dist/page/usePageLayout.js +5 -48
  207. package/dist/schema/attrRoles.d.ts +25 -18
  208. package/dist/schema/attrRoles.js +110 -68
  209. package/dist/schema/displayDerivation.d.ts +82 -0
  210. package/dist/schema/displayDerivation.js +130 -0
  211. package/dist/schema/docxSchema.d.ts +3 -0
  212. package/dist/schema/docxSchema.js +58 -17
  213. package/dist/schema/editGuard.d.ts +1 -1
  214. package/dist/schema/guards.d.ts +37 -14
  215. package/dist/schema/guards.js +29 -3
  216. package/dist/schema/index.d.ts +2 -0
  217. package/dist/schema/index.js +2 -0
  218. package/dist/schema/locks.d.ts +11 -44
  219. package/dist/schema/locks.js +0 -9
  220. package/dist/schema/preservedGuards.d.ts +26 -6
  221. package/dist/schema/preservedGuards.js +31 -1
  222. package/dist/schema/protection.d.ts +7 -1
  223. package/dist/schema/protection.js +2 -1
  224. package/dist/schema/sourceEquality.d.ts +1 -9
  225. package/dist/schema/sourceEquality.js +1 -27
  226. package/dist/styles/classNames.d.ts +13 -2
  227. package/dist/styles/classNames.js +13 -2
  228. package/dist/styles/inlineStyle.js +15 -6
  229. package/dist/styles.css +16 -18
  230. package/dist/table/cellFormatting.d.ts +8 -1
  231. package/dist/table/cellFormatting.js +11 -20
  232. package/dist/table/commands.js +20 -18
  233. package/dist/table/format.d.ts +3 -0
  234. package/dist/table/format.js +3 -9
  235. package/dist/table/gridBorders.d.ts +24 -30
  236. package/dist/table/gridBorders.js +35 -59
  237. package/dist/table/merge.d.ts +2 -6
  238. package/dist/table/merge.js +5 -5
  239. package/package.json +7 -3
  240. package/dist/docx/comments/contentTypes.d.ts +0 -7
  241. package/dist/docx/comments/contentTypes.js +0 -38
  242. package/dist/docx/comments/verifying.js +0 -206
  243. package/dist/docx/formatting/effectiveParagraph.d.ts +0 -15
  244. package/dist/docx/formatting/effectiveParagraph.js +0 -81
  245. package/dist/docx/propsXml.d.ts +0 -67
  246. package/dist/docx/uniqueControls.d.ts +0 -14
  247. package/dist/docx/uniqueControls.js +0 -62
  248. package/dist/editor/plugins/commentReservations.d.ts +0 -5
  249. package/dist/editor/plugins/commentReservations.js +0 -26
  250. package/dist/editor/plugins/styledParagraphs.d.ts +0 -15
  251. package/dist/editor/plugins/styledParagraphs.js +0 -65
  252. package/dist/page/tableMeasurements.d.ts +0 -18
  253. package/dist/page/tableMeasurements.js +0 -119
@@ -1,26 +1,23 @@
1
1
  // src/docx/paraProps.ts
2
2
  import {
3
- attrPairs,
4
3
  elementXml,
4
+ wAttrValue,
5
5
  withoutAttrs
6
6
  } from "../ooxml/element.js";
7
7
  import { wName } from "../ooxml/names.js";
8
8
  import { setAttr } from "../ooxml/precedence.js";
9
- import { childByLocalName, localPart } from "../ooxml/xml.js";
10
9
  import {
11
- layerParagraphFormat,
12
- layerRunFormat,
13
- NO_STYLES,
14
- paragraphStyleFormat,
15
- readParagraphFormat
16
- } from "./formatting.js";
17
- import {
18
- P_PR_ORDER,
10
+ childElement,
19
11
  parseProps,
20
- parsePropsXml,
21
12
  renderProps,
22
- setPropsChild
23
- } from "./propsXml.js";
13
+ setChild
14
+ } from "../ooxml/props.js";
15
+ import {
16
+ ST_DecimalNumber,
17
+ ST_SignedTwipsMeasure,
18
+ TWIPS_PER_PT
19
+ } from "../ooxml/simpleTypes.js";
20
+ import { LINE_UNITS_PER_LINE } from "./formatting.js";
24
21
  var LEFT_IND_ATTRS = ["left", "start", "leftChars", "startChars"];
25
22
  var LEFT_TWIPS_IND_ATTRS = ["left", "start"];
26
23
  var HANGING_IND_ATTRS = ["hanging", "hangingChars"];
@@ -29,15 +26,6 @@ var FIRST_LINE_IND_ATTRS = [
29
26
  "firstLine",
30
27
  "firstLineChars"
31
28
  ];
32
- function readParagraphProps(pPr, styles = NO_STYLES, defaultStyleId = null) {
33
- const direct = pPr === null ? null : readParagraphFormat(parsePropsXml(pPr));
34
- const style = paragraphStyleFormat(pPr, styles, defaultStyleId);
35
- return layerParagraphFormat(style?.paragraph ?? {}, direct);
36
- }
37
- function readParagraphStyleRun(pPr, styles = NO_STYLES, defaultStyleId = null) {
38
- const style = paragraphStyleFormat(pPr, styles, defaultStyleId);
39
- return layerRunFormat(style?.run ?? {}, null);
40
- }
41
29
  function numPrXml(ref) {
42
30
  if (!ref) return null;
43
31
  return elementXml(
@@ -66,91 +54,75 @@ function indXml(attrs) {
66
54
  function nextIndXml(ind, change) {
67
55
  if (change.kind === "keep") return void 0;
68
56
  if (change.kind === "clearHanging") {
69
- return indXml(withoutAttrs(attrPairs(ind), HANGING_IND_ATTRS));
57
+ return indXml(withoutAttrs(ind, HANGING_IND_ATTRS));
70
58
  }
71
59
  const dropped = [...LEFT_IND_ATTRS, ...FIRST_LINE_IND_ATTRS];
72
60
  return indXml([
73
61
  ...levelIndAttrs(change.indent),
74
- ...withoutAttrs(attrPairs(ind), dropped)
62
+ ...withoutAttrs(ind, dropped)
75
63
  ]);
76
64
  }
77
- function leftIndAttrs(ind, leftTwips) {
78
- const original = attrPairs(ind);
79
- const slot = original.find(
80
- ([name]) => LEFT_TWIPS_IND_ATTRS.includes(localPart(name))
81
- )?.[0];
65
+ function leftIndAttrs(original, leftTwips) {
66
+ const slot = LEFT_TWIPS_IND_ATTRS.find(
67
+ (name) => wAttrValue(original, name) !== null
68
+ );
82
69
  const written = leftTwips > 0 ? `${leftTwips}` : null;
83
70
  const kept = withoutAttrs(
84
71
  original,
85
- LEFT_IND_ATTRS.filter(
86
- (name) => slot === void 0 || name !== localPart(slot)
87
- )
72
+ LEFT_IND_ATTRS.filter((name) => name !== slot)
88
73
  );
89
- if (slot !== void 0) return setAttr(kept, "ind", localPart(slot), written);
74
+ if (slot !== void 0) return setAttr(kept, "ind", slot, written);
90
75
  return written === null ? kept : [[wName("left"), written], ...kept];
91
76
  }
92
77
  var EMPTY_P_PR = { tag: "w:pPr", attrs: null, children: [] };
93
- function editParagraphProps(pPr, styles, defaultStyleId, plan) {
78
+ function editParagraphProps(pPr, plan) {
94
79
  const props = pPr === null ? EMPTY_P_PR : parseProps(pPr);
95
- const element = pPr === null ? null : parsePropsXml(pPr);
96
- if (!props || pPr !== null && !element) return null;
80
+ if (!props) return null;
81
+ const edits = plan(props);
82
+ if (!edits) return null;
97
83
  const rendered = renderProps(
98
- plan(element).reduce(
99
- (kept, [name, xml]) => setPropsChild(kept, name, xml, P_PR_ORDER),
100
- props
101
- )
84
+ edits.reduce((kept, [name, xml]) => setChild(kept, name, xml), props)
102
85
  );
103
- const next = rendered === "" ? null : rendered;
104
- return {
105
- pPr: next,
106
- format: readParagraphProps(next, styles, defaultStyleId),
107
- styleRun: readParagraphStyleRun(next, styles, defaultStyleId)
108
- };
86
+ return { pPr: rendered === "" ? null : rendered };
109
87
  }
110
- function withListNumbering(pPr, change, styles = NO_STYLES, defaultStyleId = null) {
111
- return editParagraphProps(pPr, styles, defaultStyleId, (element) => {
112
- const ind = nextIndXml(
113
- element ? childByLocalName(element, "ind") : null,
114
- change.indent
115
- );
88
+ function withListNumbering(pPr, change) {
89
+ return editParagraphProps(pPr, (props) => {
90
+ const current = childElement(props, "ind");
91
+ if (!current) return null;
92
+ const ind = nextIndXml(current.attrs, change.indent);
116
93
  const numPr = ["numPr", numPrXml(change.numbering)];
117
94
  return ind === void 0 ? [numPr] : [numPr, ["ind", ind]];
118
95
  });
119
96
  }
120
- function withLeftIndent(pPr, leftTwips, styles = NO_STYLES, defaultStyleId = null) {
121
- return editParagraphProps(pPr, styles, defaultStyleId, (element) => {
122
- const ind = element ? childByLocalName(element, "ind") : null;
123
- return [["ind", indXml(leftIndAttrs(ind, leftTwips))]];
97
+ function withLeftIndent(pPr, leftTwips) {
98
+ return editParagraphProps(pPr, (props) => {
99
+ const ind = childElement(props, "ind");
100
+ if (!ind) return null;
101
+ return [["ind", indXml(leftIndAttrs(ind.attrs, leftTwips))]];
124
102
  });
125
103
  }
126
104
  function spacingAttrs(spacing, edits) {
127
- const changed = attrPairs(spacing).map(
128
- ([name, value]) => [
129
- name,
130
- edits.find(([edited]) => edited === localPart(name))?.[1] ?? value
131
- ]
132
- );
133
105
  return edits.reduce(
134
106
  (attrs, [name, value]) => setAttr(attrs, "spacing", name, value),
135
- changed
107
+ spacing
136
108
  );
137
109
  }
138
- function withLineSpacing(pPr, spacing, styles = NO_STYLES, defaultStyleId = null) {
139
- const line = spacing.rule === "auto" ? Math.round(spacing.lines * 240) : Math.round(spacing.pt * 20);
140
- return editParagraphProps(pPr, styles, defaultStyleId, (element) => {
141
- const current = element ? childByLocalName(element, "spacing") : null;
142
- const attrs = spacingAttrs(current, [
143
- ["line", `${line}`],
110
+ function withLineSpacing(pPr, spacing) {
111
+ const line = spacing.rule === "auto" ? ST_DecimalNumber.format(Math.round(spacing.lines * LINE_UNITS_PER_LINE)) : ST_SignedTwipsMeasure.format(Math.round(spacing.pt * TWIPS_PER_PT));
112
+ if (line === null) return null;
113
+ return editParagraphProps(pPr, (props) => {
114
+ const current = childElement(props, "spacing");
115
+ if (!current) return null;
116
+ const attrs = spacingAttrs(current.attrs, [
117
+ ["line", line],
144
118
  ["lineRule", spacing.rule]
145
119
  ]);
146
120
  return [["spacing", elementXml(wName("spacing"), attrs)]];
147
121
  });
148
122
  }
149
- function withParagraphStyle(pPr, styleId, styles = NO_STYLES, defaultStyleId = null) {
123
+ function withParagraphStyle(pPr, styleId) {
150
124
  const pStyle = styleId === null ? null : elementXml(wName("pStyle"), [[wName("val"), styleId]]);
151
- return editParagraphProps(pPr, styles, defaultStyleId, () => [
152
- ["pStyle", pStyle]
153
- ]);
125
+ return editParagraphProps(pPr, () => [["pStyle", pStyle]]);
154
126
  }
155
127
  var JC_BY_ALIGN = {
156
128
  left: "left",
@@ -158,12 +130,11 @@ var JC_BY_ALIGN = {
158
130
  right: "right",
159
131
  justify: "both"
160
132
  };
161
- function withParagraphAlign(pPr, align, styles = NO_STYLES, defaultStyleId = null) {
133
+ function withParagraphAlign(pPr, align) {
162
134
  const jc = elementXml(wName("jc"), [[wName("val"), JC_BY_ALIGN[align]]]);
163
- return editParagraphProps(pPr, styles, defaultStyleId, () => [["jc", jc]]);
135
+ return editParagraphProps(pPr, () => [["jc", jc]]);
164
136
  }
165
137
  export {
166
- readParagraphProps,
167
138
  withLeftIndent,
168
139
  withLineSpacing,
169
140
  withListNumbering,
@@ -0,0 +1,36 @@
1
+ /**
2
+ * The contract every part an export writes beside the body is written under.
3
+ *
4
+ * A planner answers with the parts it rewrites, keyed by path, and declares what a part it adds
5
+ * needs through the two writers the context carries: a relationship from the main part and a
6
+ * content type. Those two parts are written once at the end from everything every planner asked
7
+ * for, so no planner writes either of them itself and none can write over what another declared.
8
+ */
9
+ import type { Node as PMNode } from "prosemirror-model";
10
+ import { type ContentTypeWriter } from "./packageParts";
11
+ import { type RelationshipWriter } from "./relationships";
12
+ import type { SessionStore } from "./session";
13
+ export interface PartPlanContext {
14
+ readonly relationships: RelationshipWriter;
15
+ readonly contentTypes: ContentTypeWriter;
16
+ }
17
+ export interface PartPlanner {
18
+ readonly name: string;
19
+ /** The parts to write, keyed by path, and null when the document gives this planner nothing to write */
20
+ plan(doc: PMNode, session: SessionStore, context: PartPlanContext): ReadonlyMap<string, Uint8Array> | null;
21
+ }
22
+ /**
23
+ * Every part the planners write, folded into one map in planner order.
24
+ *
25
+ * The body and context-owned parts are reserved. Earlier writes include media planned before
26
+ * the body, so no later planner can overwrite those bytes either. Part names are compared
27
+ * without regard to case, as package part names are.
28
+ */
29
+ export declare function runPartPlanners(planners: readonly PartPlanner[], doc: PMNode, session: SessionStore, context: PartPlanContext, prior?: ReadonlyMap<string, Uint8Array>): Map<string, Uint8Array>;
30
+ /**
31
+ * Every rewritten XML part has to read back as XML, and a refusal names the part.
32
+ *
33
+ * The parts are spliced as text, so this is where a splice that cut in the wrong place shows up:
34
+ * before the package is repacked rather than when somebody opens it.
35
+ */
36
+ export declare function assertPartsParse(replacements: ReadonlyMap<string, Uint8Array>, contentTypes?: Uint8Array): void;
@@ -0,0 +1,59 @@
1
+ // src/docx/partPlan.ts
2
+ import { DocxExportError } from "../ooxml/errors.js";
3
+ import { decodeUtf8, parseXml } from "../ooxml/xml.js";
4
+ import {
5
+ CONTENT_TYPES_PATH,
6
+ declaredXmlParts
7
+ } from "./packageParts.js";
8
+ import { relsPathOf } from "./relationships.js";
9
+ function runPartPlanners(planners, doc, session, context, prior = /* @__PURE__ */ new Map()) {
10
+ const owned = new Set(
11
+ [
12
+ CONTENT_TYPES_PATH,
13
+ relsPathOf(session.mainPartPath),
14
+ session.mainPartPath
15
+ ].map((path) => path.toLowerCase())
16
+ );
17
+ const parts = new Map(prior);
18
+ const written = new Set(
19
+ Array.from(prior.keys(), (path) => path.toLowerCase())
20
+ );
21
+ for (const planner of planners) {
22
+ for (const [path, bytes] of planner.plan(doc, session, context) ?? []) {
23
+ if (owned.has(path.toLowerCase())) {
24
+ throw new Error(
25
+ `the ${planner.name} planner wrote ${path}, which another export writer owns`
26
+ );
27
+ }
28
+ if (written.has(path.toLowerCase())) {
29
+ throw new Error(
30
+ `the ${planner.name} planner wrote ${path}, which an earlier writer wrote already`
31
+ );
32
+ }
33
+ parts.set(path, bytes);
34
+ written.add(path.toLowerCase());
35
+ }
36
+ }
37
+ return parts;
38
+ }
39
+ var XML_PART = /\.(?:xml|rels)$/i;
40
+ function assertPartsParse(replacements, contentTypes) {
41
+ const paths = [...replacements.keys()];
42
+ const declared = contentTypes && paths.some((path) => !XML_PART.test(path)) ? declaredXmlParts(contentTypes, paths) : /* @__PURE__ */ new Set();
43
+ for (const [path, bytes] of replacements) {
44
+ if (!XML_PART.test(path) && !declared.has(path)) continue;
45
+ try {
46
+ parseXml(decodeUtf8(bytes).text);
47
+ } catch (cause) {
48
+ throw new DocxExportError(
49
+ "malformed-xml",
50
+ `${path} as written could not be parsed`,
51
+ { cause }
52
+ );
53
+ }
54
+ }
55
+ }
56
+ export {
57
+ assertPartsParse,
58
+ runPartPlanners
59
+ };
@@ -0,0 +1,127 @@
1
+ /**
2
+ * What a protection level lets a change rewrite, as one value every side of the package reads.
3
+ *
4
+ * A level says three things: which parts of the package a change may write, what an entry in one
5
+ * of those parts may look like, and how the story reads once the markup the level is about is
6
+ * taken out of it. The part planners write those parts and the verifier judges them, and each
7
+ * carried its own copy of the first two with nothing but a test holding them together. Declaring
8
+ * them once means a part the writer starts adding is a part the verifier already knows.
9
+ *
10
+ * A level with no policy registered is one no file is judged under: `protectionPolicyFor` answers
11
+ * undefined, and a caller holding the level alone has nothing to run.
12
+ */
13
+ import { type XmlParser } from "../ooxml/xml";
14
+ import type { EditableComments, EditingProtection } from "../schema/protection";
15
+ import { type DocxBytes } from "./importDocx";
16
+ import type { SessionStore } from "./session";
17
+ import { type Story } from "./storyProjection";
18
+ /** One entry of a story part: the element read out of it, and its text for the byte-for-byte case */
19
+ export interface StoryEntry {
20
+ el: Element;
21
+ xml: string;
22
+ }
23
+ /**
24
+ * How a part's entries are read.
25
+ *
26
+ * `arrived` is the reference a submission is held against rather than something to judge, so it
27
+ * takes what it can read and passes over the rest, leaving an entry standing on something
28
+ * unreadable to be judged as one that appeared. `submitted` is what is judged.
29
+ */
30
+ export type EntryReading = "arrived" | "submitted";
31
+ /** What a judgement may be told about the reader beyond the two documents */
32
+ export interface PolicyOptions {
33
+ editableComments: EditableComments;
34
+ }
35
+ /** What a judgement over two files may be told beyond that */
36
+ export interface VerifyOptions extends PolicyOptions {
37
+ xmlParser?: XmlParser;
38
+ }
39
+ /** One package part a protection lets an editor rewrite, and how a rewritten entry is judged */
40
+ export interface StoryPartKind {
41
+ relType: string;
42
+ contentType: string;
43
+ /** Where the reader found this part, and null for a package that holds none */
44
+ pathIn(session: SessionStore): string | null;
45
+ /**
46
+ * Where the part goes when it is written: where it already sits, or the first name in the
47
+ * story's own folder that no part of the package has taken.
48
+ */
49
+ writePathIn(session: SessionStore): string;
50
+ /**
51
+ * The part's entries keyed by the id the story or a sibling part refers to them by, and null
52
+ * for a submitted part this editor's writer could not have put out at all.
53
+ */
54
+ entriesIn(session: SessionStore, reading: EntryReading): ReadonlyMap<string, StoryEntry> | null;
55
+ /**
56
+ * Whether the element the entries stand in came back as it left, save for the compatibility
57
+ * markup the writer declares on a part it writes a thread key into.
58
+ */
59
+ rootKept(before: SessionStore, after: SessionStore): boolean;
60
+ /** The keys this file still stands behind, which is what an entry has to be keyed by */
61
+ referents(story: Story): ReadonlySet<string>;
62
+ /** Grammar alone: whether the entry is one this editor's writer could have put out, whoever it belongs to */
63
+ wellFormed(entry: Element): boolean;
64
+ /**
65
+ * Whether the entry came back differing from the one that arrived in nothing but a change this
66
+ * protection leaves to everyone. Such an entry is nobody's rewrite, so it is held neither to
67
+ * the grammar this editor writes in nor to who owns it.
68
+ */
69
+ anyonesChange(entry: Element, original: Element): boolean;
70
+ /**
71
+ * Permission alone: whether `authorId` may have written (`original === null`) or rewritten this
72
+ * entry under `options`. `session` is the submission's, so a kind can look across at a sibling
73
+ * part, the way a comment's author resolves through the people part.
74
+ */
75
+ allowed(entry: Element, original: Element | null, authorId: string, options: PolicyOptions, session: SessionStore): boolean;
76
+ }
77
+ /** The two reasons the package comparison answers with, whatever the policy */
78
+ export type PackageReason = "part-changed" | "relationship-changed";
79
+ /**
80
+ * The shape every verdict takes: a story reason carries no part, a part reason names the part it
81
+ * was reached over. A public verdict keeps its own declaration and is one instance of this.
82
+ */
83
+ export type ChangeVerdict<StoryReason extends string, PartReason extends string> = {
84
+ ok: true;
85
+ } | {
86
+ ok: false;
87
+ reason: StoryReason;
88
+ } | {
89
+ ok: false;
90
+ reason: PartReason;
91
+ part: string;
92
+ };
93
+ export interface ProtectionPolicy<StoryReason extends string, PartReason extends string> {
94
+ level: EditingProtection;
95
+ parts: readonly StoryPartKind[];
96
+ /** The reason a rewritten entry is refused under when no part takes it as well formed and allowed */
97
+ rejectedMarkup: PartReason;
98
+ /** Whether the story reads the same once this protection's own markup is taken out */
99
+ storyKept(before: Story, after: Story, authorId: string, options: PolicyOptions): ChangeVerdict<StoryReason, PartReason>;
100
+ }
101
+ /** Records the policy under its level and hands it back, so a module declares and registers at once */
102
+ export declare function registerProtectionPolicy<S extends string, P extends string>(policy: ProtectionPolicy<S, P>): ProtectionPolicy<S, P>;
103
+ /**
104
+ * The policy for a level, and undefined for a level no loaded module declares one for, `none` and
105
+ * `readOnly` among them.
106
+ */
107
+ export declare function protectionPolicyFor(level: EditingProtection): ProtectionPolicy<string, string> | undefined;
108
+ /**
109
+ * Whether the parts this protection lets an edit rewrite came back holding only entries this
110
+ * editor writes, each of them one this author was in a position to write.
111
+ *
112
+ * This runs after the story has been judged, so a comment rewritten, re-anchored or deleted by the
113
+ * wrong hand is already named for what it is. What is left to this is everything the story cannot
114
+ * show: markup forged into a body, an entry nothing refers to, an identity recorded for somebody
115
+ * else.
116
+ */
117
+ export declare function partsKept<P extends string>(policy: ProtectionPolicy<string, P>, before: Story, after: Story, authorId: string, options: PolicyOptions): ChangeVerdict<never, P>;
118
+ /**
119
+ * Whether the submitted file differs from the original in nothing this protection forbids.
120
+ *
121
+ * Both files are opened inside the one parser scope, so the parser named here is the parser both
122
+ * reads go through even though neither `importDocx` call is given it. The package is judged first,
123
+ * then the story, then the parts the policy excused from the package comparison: the parts are
124
+ * last so that an edit the wrong hand made is named for the edit rather than for the part it was
125
+ * written across.
126
+ */
127
+ export declare function verifyChange<S extends string, P extends string>(policy: ProtectionPolicy<S, P>, original: DocxBytes, submitted: DocxBytes, authorId: string, { xmlParser, ...options }: VerifyOptions): ChangeVerdict<S, P | PackageReason>;
@@ -0,0 +1,169 @@
1
+ // src/docx/protectionPolicy.ts
2
+ import {
3
+ decodeUtf8,
4
+ elementChildren,
5
+ parseXml,
6
+ withXmlParser
7
+ } from "../ooxml/xml.js";
8
+ import { importDocx } from "./importDocx.js";
9
+ import { CONTENT_TYPES_PATH } from "./packageParts.js";
10
+ import {
11
+ readRelationships,
12
+ relsPathOf
13
+ } from "./relationships.js";
14
+ import { isModelledBlock } from "./storyProjection.js";
15
+ var policies = /* @__PURE__ */ new Map();
16
+ function registerProtectionPolicy(policy) {
17
+ policies.set(policy.level, policy);
18
+ return policy;
19
+ }
20
+ function protectionPolicyFor(level) {
21
+ return policies.get(level);
22
+ }
23
+ function sameBytes(before, after) {
24
+ return before.length === after.length && before.every((byte, index) => byte === after[index]);
25
+ }
26
+ function partPathsOf(policy, session) {
27
+ return policy.parts.map((kind) => kind.pathIn(session));
28
+ }
29
+ function excusedPaths(policy, session) {
30
+ return partPathsOf(policy, session).filter(
31
+ (path) => path !== null
32
+ );
33
+ }
34
+ function aroundTheStory(session) {
35
+ const preserved = session.blocks.filter((block) => !isModelledBlock(block.node)).map((block) => block.xml).join("");
36
+ return session.documentPrefix + preserved + session.documentSuffix;
37
+ }
38
+ function relationshipsKept(before, after, relTypes) {
39
+ if (new Set(before.map((entry) => entry.id)).size !== before.length || new Set(after.map((entry) => entry.id)).size !== after.length) {
40
+ return false;
41
+ }
42
+ const now = new Map(after.map((entry) => [entry.id, entry]));
43
+ const kept = before.every((entry) => {
44
+ const current = now.get(entry.id);
45
+ return current !== void 0 && current.type === entry.type && current.target === entry.target && current.external === entry.external;
46
+ });
47
+ const ids = new Set(before.map((entry) => entry.id));
48
+ const parts = after.filter((entry) => !entry.external);
49
+ return kept && after.filter((entry) => !ids.has(entry.id)).every(
50
+ (entry) => relTypes.includes(entry.type) && !entry.external && parts.filter((other) => other.type === entry.type).length === 1
51
+ );
52
+ }
53
+ function contentTypes(bytes) {
54
+ if (bytes === void 0) return /* @__PURE__ */ new Map();
55
+ const declared = /* @__PURE__ */ new Map();
56
+ for (const el of elementChildren(
57
+ parseXml(decodeUtf8(bytes).text).documentElement
58
+ )) {
59
+ const key = el.localName === "Default" ? el.getAttribute("Extension") : el.localName === "Override" ? el.getAttribute("PartName") : null;
60
+ if (key !== null) declared.set(key, el.getAttribute("ContentType") ?? "");
61
+ }
62
+ return declared;
63
+ }
64
+ function contentTypesKept(before, after, declarable) {
65
+ for (const [key, type] of before) {
66
+ if (after.get(key) !== type) return false;
67
+ }
68
+ for (const [key, type] of after) {
69
+ if (!before.has(key) && !declarable.includes(type)) return false;
70
+ }
71
+ return true;
72
+ }
73
+ function gainedPartsAreNew(policy, before, after) {
74
+ const had = partPathsOf(policy, before);
75
+ return partPathsOf(policy, after).every(
76
+ (path, kind) => path === null || path === had[kind] || !before.parts.has(path)
77
+ );
78
+ }
79
+ function packageKept(policy, before, after) {
80
+ if (before.mainPartPath !== after.mainPartPath) {
81
+ return { ok: false, reason: "part-changed", part: before.mainPartPath };
82
+ }
83
+ const relsPath = relsPathOf(before.mainPartPath);
84
+ const untouched = /* @__PURE__ */ new Set([
85
+ before.mainPartPath,
86
+ relsPath,
87
+ CONTENT_TYPES_PATH,
88
+ ...excusedPaths(policy, before),
89
+ ...excusedPaths(policy, after)
90
+ ]);
91
+ for (const path of /* @__PURE__ */ new Set([...before.parts.keys(), ...after.parts.keys()])) {
92
+ if (untouched.has(path)) continue;
93
+ const was = before.parts.get(path);
94
+ const now = after.parts.get(path);
95
+ if (was === void 0 || now === void 0 || !sameBytes(was, now)) {
96
+ return { ok: false, reason: "part-changed", part: path };
97
+ }
98
+ }
99
+ if (!relationshipsKept(
100
+ readRelationships(before.parts, relsPath),
101
+ readRelationships(after.parts, relsPath),
102
+ policy.parts.map((kind) => kind.relType)
103
+ ) || !gainedPartsAreNew(policy, before, after)) {
104
+ return { ok: false, reason: "relationship-changed", part: relsPath };
105
+ }
106
+ if (!contentTypesKept(
107
+ contentTypes(before.parts.get(CONTENT_TYPES_PATH)),
108
+ contentTypes(after.parts.get(CONTENT_TYPES_PATH)),
109
+ policy.parts.map((kind) => kind.contentType)
110
+ )) {
111
+ return { ok: false, reason: "part-changed", part: CONTENT_TYPES_PATH };
112
+ }
113
+ if (aroundTheStory(before) !== aroundTheStory(after)) {
114
+ return { ok: false, reason: "part-changed", part: before.mainPartPath };
115
+ }
116
+ return { ok: true };
117
+ }
118
+ function partKept(kind, before, after, authorId, options) {
119
+ const arrived = kind.entriesIn(before.session, "arrived") ?? /* @__PURE__ */ new Map();
120
+ const submitted = kind.entriesIn(after.session, "submitted");
121
+ if (submitted === null) return false;
122
+ if (!kind.rootKept(before.session, after.session)) return false;
123
+ const stoodBehindNow = kind.referents(after);
124
+ for (const [id, entry] of submitted) {
125
+ const original = arrived.get(id);
126
+ if (original && original.xml === entry.xml) continue;
127
+ if (!stoodBehindNow.has(id)) return false;
128
+ if (original && kind.anyonesChange(entry.el, original.el)) continue;
129
+ if (!kind.wellFormed(entry.el) || !kind.allowed(
130
+ entry.el,
131
+ original?.el ?? null,
132
+ authorId,
133
+ options,
134
+ after.session
135
+ )) {
136
+ return false;
137
+ }
138
+ }
139
+ const stoodBehindBefore = kind.referents(before);
140
+ return Array.from(arrived.keys()).every(
141
+ (id) => stoodBehindBefore.has(id) || submitted.has(id)
142
+ );
143
+ }
144
+ function partsKept(policy, before, after, authorId, options) {
145
+ for (const kind of policy.parts) {
146
+ const path = kind.pathIn(after.session) ?? kind.pathIn(before.session);
147
+ if (path === null) continue;
148
+ if (!partKept(kind, before, after, authorId, options)) {
149
+ return { ok: false, reason: policy.rejectedMarkup, part: path };
150
+ }
151
+ }
152
+ return { ok: true };
153
+ }
154
+ function verifyChange(policy, original, submitted, authorId, { xmlParser, ...options }) {
155
+ return withXmlParser(xmlParser, () => {
156
+ const before = importDocx(original);
157
+ const after = importDocx(submitted);
158
+ const packaged = packageKept(policy, before.session, after.session);
159
+ if (!packaged.ok) return packaged;
160
+ const story = policy.storyKept(before, after, authorId, options);
161
+ return story.ok ? partsKept(policy, before, after, authorId, options) : story;
162
+ });
163
+ }
164
+ export {
165
+ partsKept,
166
+ protectionPolicyFor,
167
+ registerProtectionPolicy,
168
+ verifyChange
169
+ };
@@ -20,7 +20,7 @@ export interface Relationship {
20
20
  /** Set when the target is a file outside the package */
21
21
  external: boolean;
22
22
  }
23
- export declare function readRelationships(parts: Map<string, Uint8Array>, relsPath: string): Relationship[];
23
+ export declare function readRelationships(parts: ReadonlyMap<string, Uint8Array>, relsPath: string): Relationship[];
24
24
  /** A relationship an export puts in. The target is escaped here, so hand it in as it reads */
25
25
  export interface NewRelationship {
26
26
  type: string;
@@ -1,6 +1,6 @@
1
1
  // src/docx/relationships.ts
2
2
  import { elementXml } from "../ooxml/element.js";
3
- import { DocxExportError } from "../ooxml/errors.js";
3
+ import { rootPrefixOf, splicePart } from "../ooxml/partSplice.js";
4
4
  import {
5
5
  decodeUtf8,
6
6
  elementChildren,
@@ -47,9 +47,9 @@ function takeRelId(taken) {
47
47
  }
48
48
  }
49
49
  }
50
- function relationshipXml(added) {
50
+ function relationshipXml(added, prefix) {
51
51
  const mode = added.external ? [["TargetMode", "External"]] : [];
52
- return elementXml("Relationship", [
52
+ return elementXml(`${prefix}Relationship`, [
53
53
  ["Id", added.id],
54
54
  ["Type", added.type],
55
55
  ["Target", added.target],
@@ -57,23 +57,18 @@ function relationshipXml(added) {
57
57
  ]);
58
58
  }
59
59
  function relationshipsPart(original, added) {
60
- const entries = added.map(relationshipXml).join("");
61
60
  if (!original) {
61
+ const entries2 = added.map((entry) => relationshipXml(entry, "")).join("");
62
62
  return encodeUtf8(
63
- `<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Relationships xmlns="${RELATIONSHIPS_NS}">${entries}</Relationships>`,
63
+ `<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Relationships xmlns="${RELATIONSHIPS_NS}">${entries2}</Relationships>`,
64
64
  false
65
65
  );
66
66
  }
67
67
  const { text, hadBom } = decodeUtf8(original);
68
- const closing = text.lastIndexOf("</Relationships>");
69
- if (closing === -1) {
70
- throw new DocxExportError(
71
- "malformed-xml",
72
- "the relationships part has no closing tag"
73
- );
74
- }
68
+ const prefix = rootPrefixOf(text);
69
+ const entries = added.map((entry) => relationshipXml(entry, prefix)).join("");
75
70
  return encodeUtf8(
76
- text.slice(0, closing) + entries + text.slice(closing),
71
+ splicePart(text, { root: "Relationships", append: entries }),
77
72
  hadBom
78
73
  );
79
74
  }