@beyondwork/docx-react-component 1.0.27 → 1.0.29

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 (356) hide show
  1. package/dist/canonical-document-BLEbzL2J.d.cts +844 -0
  2. package/dist/canonical-document-BLEbzL2J.d.ts +844 -0
  3. package/dist/chunk-2FJS5GZM.js +763 -0
  4. package/dist/chunk-2FJS5GZM.js.map +1 -0
  5. package/{src/core/commands/section-layout-commands.ts → dist/chunk-2OQBZS3F.js} +106 -340
  6. package/dist/chunk-2OQBZS3F.js.map +1 -0
  7. package/dist/chunk-2S7W4KFO.js +127 -0
  8. package/dist/chunk-2S7W4KFO.js.map +1 -0
  9. package/dist/chunk-2TG72QSW.js +3874 -0
  10. package/dist/chunk-2TG72QSW.js.map +1 -0
  11. package/{src/core/commands/table-structure-commands.ts → dist/chunk-36QNIZBO.js} +126 -315
  12. package/dist/chunk-36QNIZBO.js.map +1 -0
  13. package/dist/chunk-4AQOYAW4.js +3069 -0
  14. package/dist/chunk-4AQOYAW4.js.map +1 -0
  15. package/dist/chunk-4D5EWJ3P.js +77 -0
  16. package/dist/chunk-4D5EWJ3P.js.map +1 -0
  17. package/dist/chunk-5FN54NDH.js +2257 -0
  18. package/dist/chunk-5FN54NDH.js.map +1 -0
  19. package/dist/chunk-BOYGQYRQ.js +7306 -0
  20. package/dist/chunk-BOYGQYRQ.js.map +1 -0
  21. package/dist/chunk-CN3XMECL.js +212 -0
  22. package/dist/chunk-CN3XMECL.js.map +1 -0
  23. package/dist/chunk-EBI3BX6U.js +164 -0
  24. package/dist/chunk-EBI3BX6U.js.map +1 -0
  25. package/dist/chunk-EILUG3VB.js +1275 -0
  26. package/dist/chunk-EILUG3VB.js.map +1 -0
  27. package/dist/chunk-FUDY333O.js +70 -0
  28. package/dist/chunk-FUDY333O.js.map +1 -0
  29. package/dist/chunk-GBVOWFIK.js +1237 -0
  30. package/dist/chunk-GBVOWFIK.js.map +1 -0
  31. package/dist/chunk-H4TQ3H3Y.js +262 -0
  32. package/dist/chunk-H4TQ3H3Y.js.map +1 -0
  33. package/{src/core/commands/style-commands.ts → dist/chunk-JGB3IXZO.js} +40 -113
  34. package/dist/chunk-JGB3IXZO.js.map +1 -0
  35. package/dist/chunk-KD2QRQPY.js +4342 -0
  36. package/dist/chunk-KD2QRQPY.js.map +1 -0
  37. package/dist/chunk-KLMXQVYK.js +369 -0
  38. package/dist/chunk-KLMXQVYK.js.map +1 -0
  39. package/dist/chunk-KZUG5KFQ.js +214 -0
  40. package/dist/chunk-KZUG5KFQ.js.map +1 -0
  41. package/{src/core/state/text-transaction.ts → dist/chunk-QDAQ4CJU.js} +79 -236
  42. package/dist/chunk-QDAQ4CJU.js.map +1 -0
  43. package/{src/legal/bookmarks.ts → dist/chunk-RMH72RZI.js} +44 -130
  44. package/dist/chunk-RMH72RZI.js.map +1 -0
  45. package/dist/chunk-SWKWQZXM.js +117 -0
  46. package/dist/chunk-SWKWQZXM.js.map +1 -0
  47. package/{src/core/commands/formatting-commands.ts → dist/chunk-TJBP2K4T.js} +196 -536
  48. package/dist/chunk-TJBP2K4T.js.map +1 -0
  49. package/dist/chunk-TLCEAQDQ.js +542 -0
  50. package/dist/chunk-TLCEAQDQ.js.map +1 -0
  51. package/{src/core/commands/text-commands.ts → dist/chunk-UZXBISGO.js} +86 -142
  52. package/dist/chunk-UZXBISGO.js.map +1 -0
  53. package/dist/chunk-WGBAKP3Q.js +3220 -0
  54. package/dist/chunk-WGBAKP3Q.js.map +1 -0
  55. package/dist/compare/index.cjs +5475 -0
  56. package/dist/compare/index.cjs.map +1 -0
  57. package/dist/compare/index.d.cts +114 -0
  58. package/dist/compare/index.d.ts +114 -0
  59. package/dist/compare/index.js +731 -0
  60. package/dist/compare/index.js.map +1 -0
  61. package/dist/core/commands/formatting-commands.cjs +828 -0
  62. package/dist/core/commands/formatting-commands.cjs.map +1 -0
  63. package/dist/core/commands/formatting-commands.d.cts +63 -0
  64. package/dist/core/commands/formatting-commands.d.ts +63 -0
  65. package/dist/core/commands/formatting-commands.js +37 -0
  66. package/dist/core/commands/formatting-commands.js.map +1 -0
  67. package/dist/core/commands/image-commands.cjs +2023 -0
  68. package/dist/core/commands/image-commands.cjs.map +1 -0
  69. package/dist/core/commands/image-commands.d.cts +58 -0
  70. package/dist/core/commands/image-commands.d.ts +58 -0
  71. package/dist/core/commands/image-commands.js +18 -0
  72. package/dist/core/commands/image-commands.js.map +1 -0
  73. package/dist/core/commands/section-layout-commands.cjs +477 -0
  74. package/dist/core/commands/section-layout-commands.cjs.map +1 -0
  75. package/dist/core/commands/section-layout-commands.d.cts +62 -0
  76. package/dist/core/commands/section-layout-commands.d.ts +62 -0
  77. package/dist/core/commands/section-layout-commands.js +21 -0
  78. package/dist/core/commands/section-layout-commands.js.map +1 -0
  79. package/dist/core/commands/style-commands.cjs +214 -0
  80. package/dist/core/commands/style-commands.cjs.map +1 -0
  81. package/dist/core/commands/style-commands.d.cts +13 -0
  82. package/dist/core/commands/style-commands.d.ts +13 -0
  83. package/dist/core/commands/style-commands.js +9 -0
  84. package/dist/core/commands/style-commands.js.map +1 -0
  85. package/dist/core/commands/table-structure-commands.cjs +1883 -0
  86. package/dist/core/commands/table-structure-commands.cjs.map +1 -0
  87. package/dist/core/commands/table-structure-commands.d.cts +59 -0
  88. package/dist/core/commands/table-structure-commands.d.ts +59 -0
  89. package/dist/core/commands/table-structure-commands.js +12 -0
  90. package/dist/core/commands/table-structure-commands.js.map +1 -0
  91. package/dist/core/commands/text-commands.cjs +2391 -0
  92. package/dist/core/commands/text-commands.cjs.map +1 -0
  93. package/dist/core/commands/text-commands.d.cts +24 -0
  94. package/dist/core/commands/text-commands.d.ts +24 -0
  95. package/dist/core/commands/text-commands.js +28 -0
  96. package/dist/core/commands/text-commands.js.map +1 -0
  97. package/dist/core/selection/mapping.cjs +200 -0
  98. package/dist/core/selection/mapping.cjs.map +1 -0
  99. package/dist/core/selection/mapping.d.cts +2 -0
  100. package/dist/core/selection/mapping.d.ts +2 -0
  101. package/dist/core/selection/mapping.js +31 -0
  102. package/dist/core/selection/mapping.js.map +1 -0
  103. package/dist/core/state/editor-state.cjs +2278 -0
  104. package/dist/core/state/editor-state.cjs.map +1 -0
  105. package/dist/core/state/editor-state.d.cts +2 -0
  106. package/dist/core/state/editor-state.d.ts +2 -0
  107. package/dist/core/state/editor-state.js +26 -0
  108. package/dist/core/state/editor-state.js.map +1 -0
  109. package/dist/index.cjs +38553 -0
  110. package/dist/index.cjs.map +1 -0
  111. package/dist/index.d.cts +15 -0
  112. package/dist/index.d.ts +15 -0
  113. package/dist/index.js +7856 -0
  114. package/dist/index.js.map +1 -0
  115. package/dist/io/docx-session.cjs +16236 -0
  116. package/dist/io/docx-session.cjs.map +1 -0
  117. package/dist/io/docx-session.d.cts +21 -0
  118. package/dist/io/docx-session.d.ts +21 -0
  119. package/dist/io/docx-session.js +18 -0
  120. package/dist/io/docx-session.js.map +1 -0
  121. package/dist/legal/index.cjs +3900 -0
  122. package/dist/legal/index.cjs.map +1 -0
  123. package/dist/legal/index.d.cts +86 -0
  124. package/dist/legal/index.d.ts +86 -0
  125. package/dist/legal/index.js +616 -0
  126. package/dist/legal/index.js.map +1 -0
  127. package/dist/public-types-7ZL_94cz.d.ts +1573 -0
  128. package/dist/public-types-CeMaDueh.d.cts +1573 -0
  129. package/dist/public-types.cjs +19 -0
  130. package/dist/public-types.cjs.map +1 -0
  131. package/dist/public-types.d.cts +2 -0
  132. package/dist/public-types.d.ts +2 -0
  133. package/dist/public-types.js +1 -0
  134. package/dist/public-types.js.map +1 -0
  135. package/dist/runtime/document-runtime.cjs +11140 -0
  136. package/dist/runtime/document-runtime.cjs.map +1 -0
  137. package/dist/runtime/document-runtime.d.cts +231 -0
  138. package/dist/runtime/document-runtime.d.ts +231 -0
  139. package/dist/runtime/document-runtime.js +21 -0
  140. package/dist/runtime/document-runtime.js.map +1 -0
  141. package/dist/structural-helpers-CilgOVhh.d.cts +10 -0
  142. package/dist/structural-helpers-q0Gd-eBN.d.ts +10 -0
  143. package/dist/ui-tailwind/editor-surface/search-plugin.cjs +313 -0
  144. package/dist/ui-tailwind/editor-surface/search-plugin.cjs.map +1 -0
  145. package/dist/ui-tailwind/editor-surface/search-plugin.d.cts +67 -0
  146. package/dist/ui-tailwind/editor-surface/search-plugin.d.ts +67 -0
  147. package/dist/ui-tailwind/editor-surface/search-plugin.js +23 -0
  148. package/dist/ui-tailwind/editor-surface/search-plugin.js.map +1 -0
  149. package/dist/ui-tailwind/index.cjs +4833 -0
  150. package/dist/ui-tailwind/index.cjs.map +1 -0
  151. package/dist/ui-tailwind/index.d.cts +617 -0
  152. package/dist/ui-tailwind/index.d.ts +617 -0
  153. package/dist/ui-tailwind/index.js +575 -0
  154. package/dist/ui-tailwind/index.js.map +1 -0
  155. package/package.json +64 -54
  156. package/src/README.md +0 -85
  157. package/src/api/README.md +0 -26
  158. package/src/api/public-types.ts +0 -1418
  159. package/src/api/session-state.ts +0 -60
  160. package/src/compare/diff-engine.ts +0 -623
  161. package/src/compare/export-redlines.ts +0 -280
  162. package/src/compare/index.ts +0 -25
  163. package/src/compare/snapshot.ts +0 -97
  164. package/src/component-inventory.md +0 -99
  165. package/src/core/README.md +0 -10
  166. package/src/core/commands/README.md +0 -3
  167. package/src/core/commands/image-commands.ts +0 -373
  168. package/src/core/commands/index.ts +0 -1757
  169. package/src/core/commands/list-commands.ts +0 -565
  170. package/src/core/commands/paragraph-layout-commands.ts +0 -339
  171. package/src/core/commands/review-commands.ts +0 -108
  172. package/src/core/commands/structural-helpers.ts +0 -309
  173. package/src/core/schema/README.md +0 -3
  174. package/src/core/schema/text-schema.ts +0 -516
  175. package/src/core/search/search-text.ts +0 -357
  176. package/src/core/selection/README.md +0 -3
  177. package/src/core/selection/mapping.ts +0 -289
  178. package/src/core/selection/review-anchors.ts +0 -183
  179. package/src/core/state/README.md +0 -3
  180. package/src/core/state/editor-state.ts +0 -892
  181. package/src/formats/xlsx/io/parse-shared-strings.ts +0 -41
  182. package/src/formats/xlsx/io/parse-sheet.ts +0 -459
  183. package/src/formats/xlsx/io/parse-styles.ts +0 -59
  184. package/src/formats/xlsx/io/parse-workbook.ts +0 -75
  185. package/src/formats/xlsx/io/serialize-shared-strings.ts +0 -72
  186. package/src/formats/xlsx/io/serialize-sheet.ts +0 -333
  187. package/src/formats/xlsx/io/serialize-styles.ts +0 -98
  188. package/src/formats/xlsx/io/serialize-workbook.ts +0 -429
  189. package/src/formats/xlsx/io/xlsx-session.ts +0 -314
  190. package/src/formats/xlsx/model/cell.ts +0 -189
  191. package/src/formats/xlsx/model/sheet.ts +0 -326
  192. package/src/formats/xlsx/model/styles.ts +0 -118
  193. package/src/formats/xlsx/model/workbook.ts +0 -453
  194. package/src/formats/xlsx/runtime/cell-commands.ts +0 -567
  195. package/src/formats/xlsx/runtime/sheet-commands.ts +0 -206
  196. package/src/formats/xlsx/runtime/workbook-runtime.ts +0 -177
  197. package/src/formats/xlsx/runtime/workbook-transaction.ts +0 -822
  198. package/src/index.ts +0 -101
  199. package/src/io/README.md +0 -10
  200. package/src/io/docx-session.ts +0 -2882
  201. package/src/io/export/README.md +0 -3
  202. package/src/io/export/export-session.ts +0 -220
  203. package/src/io/export/minimal-docx.ts +0 -115
  204. package/src/io/export/reattach-preserved-parts.ts +0 -54
  205. package/src/io/export/serialize-comments.ts +0 -947
  206. package/src/io/export/serialize-footnotes.ts +0 -399
  207. package/src/io/export/serialize-headers-footers.ts +0 -372
  208. package/src/io/export/serialize-main-document.ts +0 -1376
  209. package/src/io/export/serialize-numbering.ts +0 -118
  210. package/src/io/export/serialize-revisions.ts +0 -389
  211. package/src/io/export/serialize-runtime-revisions.ts +0 -269
  212. package/src/io/export/serialize-tables.ts +0 -174
  213. package/src/io/export/split-review-boundaries.ts +0 -356
  214. package/src/io/normalize/README.md +0 -3
  215. package/src/io/normalize/normalize-text.ts +0 -639
  216. package/src/io/ooxml/README.md +0 -3
  217. package/src/io/ooxml/highlight-colors.ts +0 -39
  218. package/src/io/ooxml/numbering-sentinels.ts +0 -44
  219. package/src/io/ooxml/parse-comments.ts +0 -846
  220. package/src/io/ooxml/parse-complex-content.ts +0 -287
  221. package/src/io/ooxml/parse-fields.ts +0 -834
  222. package/src/io/ooxml/parse-footnotes.ts +0 -896
  223. package/src/io/ooxml/parse-headers-footers.ts +0 -1169
  224. package/src/io/ooxml/parse-inline-media.ts +0 -461
  225. package/src/io/ooxml/parse-main-document.ts +0 -2877
  226. package/src/io/ooxml/parse-numbering.ts +0 -432
  227. package/src/io/ooxml/parse-revisions.ts +0 -931
  228. package/src/io/ooxml/parse-settings.ts +0 -184
  229. package/src/io/ooxml/parse-shapes.ts +0 -296
  230. package/src/io/ooxml/parse-styles.ts +0 -463
  231. package/src/io/ooxml/parse-tables.ts +0 -618
  232. package/src/io/ooxml/parse-theme.ts +0 -346
  233. package/src/io/ooxml/part-manifest.ts +0 -136
  234. package/src/io/ooxml/revision-boundaries.ts +0 -351
  235. package/src/io/opc/README.md +0 -3
  236. package/src/io/opc/corrupt-package.ts +0 -166
  237. package/src/io/opc/docx-package.ts +0 -74
  238. package/src/io/opc/package-reader.ts +0 -325
  239. package/src/io/opc/package-writer.ts +0 -273
  240. package/src/io/source-package-provenance.ts +0 -241
  241. package/src/legal/cross-references.ts +0 -414
  242. package/src/legal/defined-terms.ts +0 -203
  243. package/src/legal/index.ts +0 -32
  244. package/src/legal/signature-blocks.ts +0 -259
  245. package/src/model/README.md +0 -3
  246. package/src/model/canonical-document.ts +0 -2632
  247. package/src/model/cds-1.0.0.ts +0 -212
  248. package/src/model/snapshot.ts +0 -649
  249. package/src/preservation/README.md +0 -3
  250. package/src/preservation/markup-compatibility.ts +0 -48
  251. package/src/preservation/opaque-fragment-store.ts +0 -89
  252. package/src/preservation/opaque-region.ts +0 -233
  253. package/src/preservation/package-preservation.ts +0 -113
  254. package/src/preservation/preserved-part-manifest.ts +0 -56
  255. package/src/preservation/relationship-retention.ts +0 -57
  256. package/src/preservation/store.ts +0 -185
  257. package/src/review/README.md +0 -16
  258. package/src/review/store/README.md +0 -3
  259. package/src/review/store/comment-anchors.ts +0 -70
  260. package/src/review/store/comment-remapping.ts +0 -154
  261. package/src/review/store/comment-store.ts +0 -331
  262. package/src/review/store/comment-thread.ts +0 -109
  263. package/src/review/store/revision-actions.ts +0 -394
  264. package/src/review/store/revision-store.ts +0 -312
  265. package/src/review/store/revision-types.ts +0 -171
  266. package/src/review/store/runtime-comment-store.ts +0 -43
  267. package/src/runtime/README.md +0 -3
  268. package/src/runtime/ai-action-policy.ts +0 -764
  269. package/src/runtime/collab-review-sync.ts +0 -254
  270. package/src/runtime/document-layout.ts +0 -332
  271. package/src/runtime/document-navigation.ts +0 -603
  272. package/src/runtime/document-runtime.ts +0 -3159
  273. package/src/runtime/document-search.ts +0 -145
  274. package/src/runtime/numbering-prefix.ts +0 -216
  275. package/src/runtime/page-layout-estimation.ts +0 -212
  276. package/src/runtime/read-only-diagnostics-runtime.ts +0 -241
  277. package/src/runtime/review-runtime.ts +0 -44
  278. package/src/runtime/revision-runtime.ts +0 -107
  279. package/src/runtime/session-capabilities.ts +0 -192
  280. package/src/runtime/story-context.ts +0 -164
  281. package/src/runtime/story-targeting.ts +0 -162
  282. package/src/runtime/surface-projection.ts +0 -1357
  283. package/src/runtime/table-commands.ts +0 -173
  284. package/src/runtime/table-schema.ts +0 -309
  285. package/src/runtime/view-state.ts +0 -477
  286. package/src/runtime/virtualized-rendering.ts +0 -258
  287. package/src/runtime/workflow-markup.ts +0 -353
  288. package/src/ui/README.md +0 -30
  289. package/src/ui/WordReviewEditor.tsx +0 -4097
  290. package/src/ui/browser-export.ts +0 -52
  291. package/src/ui/comments/README.md +0 -3
  292. package/src/ui/compatibility/README.md +0 -3
  293. package/src/ui/editor-command-bag.ts +0 -120
  294. package/src/ui/editor-runtime-boundary.ts +0 -1457
  295. package/src/ui/editor-shell-view.tsx +0 -142
  296. package/src/ui/editor-surface/README.md +0 -3
  297. package/src/ui/editor-surface-controller.tsx +0 -63
  298. package/src/ui/headless/comment-decoration-model.ts +0 -124
  299. package/src/ui/headless/preserve-editor-selection.ts +0 -5
  300. package/src/ui/headless/revision-decoration-model.ts +0 -128
  301. package/src/ui/headless/selection-helpers.ts +0 -54
  302. package/src/ui/headless/selection-toolbar-model.ts +0 -34
  303. package/src/ui/headless/use-editor-keyboard.ts +0 -103
  304. package/src/ui/review/README.md +0 -3
  305. package/src/ui/runtime-snapshot-selectors.ts +0 -197
  306. package/src/ui/shared/revision-filters.ts +0 -31
  307. package/src/ui/status/README.md +0 -3
  308. package/src/ui/theme/README.md +0 -3
  309. package/src/ui/toolbar/README.md +0 -3
  310. package/src/ui/workflow-surface-blocked-rails.ts +0 -94
  311. package/src/ui-tailwind/chrome/tw-alert-banner.tsx +0 -64
  312. package/src/ui-tailwind/chrome/tw-image-context-toolbar.tsx +0 -129
  313. package/src/ui-tailwind/chrome/tw-layout-panel.tsx +0 -114
  314. package/src/ui-tailwind/chrome/tw-object-context-toolbar.tsx +0 -34
  315. package/src/ui-tailwind/chrome/tw-page-ruler.tsx +0 -386
  316. package/src/ui-tailwind/chrome/tw-selection-toolbar.tsx +0 -186
  317. package/src/ui-tailwind/chrome/tw-suggestion-card.tsx +0 -139
  318. package/src/ui-tailwind/chrome/tw-table-context-toolbar.tsx +0 -128
  319. package/src/ui-tailwind/chrome/tw-unsaved-modal.tsx +0 -58
  320. package/src/ui-tailwind/chrome/use-before-unload.ts +0 -20
  321. package/src/ui-tailwind/editor-surface/perf-probe.ts +0 -179
  322. package/src/ui-tailwind/editor-surface/pm-collab-plugins.ts +0 -40
  323. package/src/ui-tailwind/editor-surface/pm-command-bridge.ts +0 -178
  324. package/src/ui-tailwind/editor-surface/pm-contextual-ui.ts +0 -31
  325. package/src/ui-tailwind/editor-surface/pm-decorations.ts +0 -427
  326. package/src/ui-tailwind/editor-surface/pm-position-map.ts +0 -123
  327. package/src/ui-tailwind/editor-surface/pm-schema.ts +0 -876
  328. package/src/ui-tailwind/editor-surface/pm-state-from-snapshot.ts +0 -504
  329. package/src/ui-tailwind/editor-surface/search-plugin.ts +0 -168
  330. package/src/ui-tailwind/editor-surface/surface-build-keys.ts +0 -61
  331. package/src/ui-tailwind/editor-surface/tw-caret.tsx +0 -12
  332. package/src/ui-tailwind/editor-surface/tw-editor-surface.tsx +0 -150
  333. package/src/ui-tailwind/editor-surface/tw-inline-token.tsx +0 -129
  334. package/src/ui-tailwind/editor-surface/tw-opaque-block.tsx +0 -58
  335. package/src/ui-tailwind/editor-surface/tw-paragraph-block.tsx +0 -151
  336. package/src/ui-tailwind/editor-surface/tw-prosemirror-surface.tsx +0 -973
  337. package/src/ui-tailwind/editor-surface/tw-segment-view.tsx +0 -111
  338. package/src/ui-tailwind/editor-surface/tw-table-node-view.tsx +0 -436
  339. package/src/ui-tailwind/index.ts +0 -62
  340. package/src/ui-tailwind/page-chrome-model.ts +0 -27
  341. package/src/ui-tailwind/review/tw-comment-sidebar.tsx +0 -406
  342. package/src/ui-tailwind/review/tw-health-panel.tsx +0 -149
  343. package/src/ui-tailwind/review/tw-review-rail.tsx +0 -120
  344. package/src/ui-tailwind/review/tw-revision-sidebar.tsx +0 -164
  345. package/src/ui-tailwind/status/tw-status-bar.tsx +0 -61
  346. package/src/ui-tailwind/toolbar/tw-toolbar-icon-button.tsx +0 -52
  347. package/src/ui-tailwind/toolbar/tw-toolbar.tsx +0 -1064
  348. package/src/ui-tailwind/tw-review-workspace.tsx +0 -1417
  349. package/src/validation/README.md +0 -3
  350. package/src/validation/compatibility-engine.ts +0 -634
  351. package/src/validation/compatibility-report.ts +0 -161
  352. package/src/validation/diagnostics.ts +0 -204
  353. package/src/validation/docx-comment-proof.ts +0 -707
  354. package/src/validation/import-diagnostics.ts +0 -128
  355. package/src/validation/low-priority-word-surfaces.ts +0 -373
  356. /package/{src → dist}/ui-tailwind/theme/editor-theme.css +0 -0
@@ -1,1376 +0,0 @@
1
- import type {
2
- AltChunkNode,
3
- BorderSpec,
4
- CustomXmlNode,
5
- DocumentRootNode,
6
- InlineNode,
7
- MediaCatalog,
8
- ParagraphNode,
9
- PreservationStore,
10
- SdtNode,
11
- SectionProperties,
12
- TableNode,
13
- TableCellNode,
14
- TextMark,
15
- } from "../../model/canonical-document.ts";
16
- import type { OpcRelationship } from "../ooxml/part-manifest.ts";
17
- import type { RevisionParagraphBoundary } from "../ooxml/revision-boundaries.ts";
18
- import { getOpaqueFragment } from "../../preservation/store.ts";
19
- import { retainRelationshipsForFragment } from "../../preservation/relationship-retention.ts";
20
- import { serializeParagraphNumberingProperties } from "./serialize-numbering.ts";
21
-
22
- const HYPERLINK_RELATIONSHIP_TYPE =
23
- "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink";
24
-
25
- export interface SerializedMainDocument {
26
- documentXml: string;
27
- relationships: OpcRelationship[];
28
- paragraphBoundaries: RevisionParagraphBoundary[];
29
- }
30
-
31
- export interface SerializeMainDocumentOptions {
32
- documentAttributes?: Record<string, string>;
33
- media?: MediaCatalog;
34
- finalSectionProperties?: SectionProperties;
35
- }
36
-
37
- interface SerializationState {
38
- nextHyperlinkRelationshipIndex: number;
39
- relationships: OpcRelationship[];
40
- existingRelationshipMap: Map<string, OpcRelationship>;
41
- retainedRelationshipIds: Set<string>;
42
- media: MediaCatalog;
43
- preservation: PreservationStore;
44
- }
45
-
46
- interface InlineSerializationResult {
47
- xml: string;
48
- cursor: number;
49
- boundaries: Map<number, number>;
50
- }
51
-
52
- interface ParagraphSerializationResult {
53
- xml: string;
54
- nextCursor: number;
55
- boundary: RevisionParagraphBoundary;
56
- }
57
-
58
- export function serializeMainDocument(
59
- content: DocumentRootNode,
60
- preservation: PreservationStore = { opaqueFragments: {}, packageParts: {} },
61
- existingRelationships: readonly OpcRelationship[] = [],
62
- options: SerializeMainDocumentOptions = {},
63
- ): SerializedMainDocument {
64
- const nextRelationshipIndex =
65
- existingRelationships.reduce((maxIndex, relationship) => {
66
- const match = /^rIdHyperlink(\d+)$/.exec(relationship.id);
67
- return match ? Math.max(maxIndex, Number.parseInt(match[1] ?? "0", 10)) : maxIndex;
68
- }, 0) + 1;
69
- const state: SerializationState = {
70
- nextHyperlinkRelationshipIndex: nextRelationshipIndex,
71
- relationships: existingRelationships.filter(
72
- (relationship) => relationship.type !== HYPERLINK_RELATIONSHIP_TYPE,
73
- ).map(cloneRelationship),
74
- existingRelationshipMap: new Map(
75
- existingRelationships.map((relationship) => [relationship.id, cloneRelationship(relationship)]),
76
- ),
77
- retainedRelationshipIds: new Set(
78
- existingRelationships
79
- .filter((relationship) => relationship.type !== HYPERLINK_RELATIONSHIP_TYPE)
80
- .map((relationship) => relationship.id),
81
- ),
82
- media: options.media ?? { items: {} },
83
- preservation,
84
- };
85
- const documentOpen = `<w:document${serializeDocumentAttributes(options.documentAttributes, content)}>`;
86
- const prefix = [
87
- `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>`,
88
- documentOpen,
89
- ` <w:body>`,
90
- ].join("\n");
91
- const suffix = `</w:body>\n</w:document>`;
92
- const bodyPieces: string[] = [];
93
- const paragraphBoundaries: RevisionParagraphBoundary[] = [];
94
- let bodyLength = 0;
95
- let sectionPropertiesXml = options.finalSectionProperties
96
- ? serializeSectionPropertiesXml(options.finalSectionProperties)
97
- : "<w:sectPr/>";
98
- let cursor = 0;
99
- let paragraphIndex = -1;
100
- let previousWasParagraph = false;
101
-
102
- for (const block of content.children) {
103
- if (block.type === "paragraph") {
104
- if (previousWasParagraph) {
105
- cursor += 1;
106
- }
107
-
108
- paragraphIndex += 1;
109
- const serializedParagraph = serializeParagraph(
110
- block,
111
- state,
112
- cursor,
113
- paragraphIndex,
114
- );
115
- const paragraphOffset = prefix.length + bodyLength;
116
- bodyPieces.push(serializedParagraph.xml);
117
- bodyLength += serializedParagraph.xml.length;
118
- paragraphBoundaries.push(
119
- offsetParagraphBoundary(serializedParagraph.boundary, paragraphOffset),
120
- );
121
- cursor = serializedParagraph.nextCursor;
122
- previousWasParagraph = true;
123
- continue;
124
- }
125
-
126
- if (block.type === "table") {
127
- const tableXml = serializeTableNode(block, state);
128
- bodyPieces.push(tableXml);
129
- bodyLength += tableXml.length;
130
- cursor += 1;
131
- previousWasParagraph = false;
132
- continue;
133
- }
134
-
135
- if (block.type === "sdt") {
136
- const sdtXml = serializeSdtNode(block, state);
137
- bodyPieces.push(sdtXml);
138
- bodyLength += sdtXml.length;
139
- previousWasParagraph = false;
140
- continue;
141
- }
142
-
143
- if (block.type === "custom_xml") {
144
- const customXml = serializeCustomXmlNode(block, state);
145
- bodyPieces.push(customXml);
146
- bodyLength += customXml.length;
147
- previousWasParagraph = false;
148
- continue;
149
- }
150
-
151
- if (block.type === "alt_chunk") {
152
- const altChunkXml = serializeAltChunkNode(block);
153
- bodyPieces.push(altChunkXml);
154
- bodyLength += altChunkXml.length;
155
- cursor += 1;
156
- previousWasParagraph = false;
157
- continue;
158
- }
159
-
160
- if (block.type === "section_break") {
161
- // Inline section breaks must be emitted as a paragraph with <w:sectPr>
162
- // in its <w:pPr> element (OOXML compliance). The body-level <w:sectPr>
163
- // is reserved for the final section only.
164
- let inlineSectPr: string;
165
- if (block.sectionPropertiesXml ?? block.propertiesXml) {
166
- inlineSectPr = block.sectionPropertiesXml ?? block.propertiesXml!;
167
- } else if (block.sectionProperties) {
168
- inlineSectPr = serializeSectionPropertiesXml(block.sectionProperties);
169
- } else {
170
- inlineSectPr = "<w:sectPr/>";
171
- }
172
- const sectionParagraphXml = `<w:p><w:pPr>${inlineSectPr}</w:pPr></w:p>`;
173
- bodyPieces.push(sectionParagraphXml);
174
- bodyLength += sectionParagraphXml.length;
175
- cursor += 1;
176
- previousWasParagraph = false;
177
- continue;
178
- }
179
-
180
- const blockXml = serializeOpaqueBlock(block, state);
181
- if (looksLikeSectionPropertiesXml(blockXml)) {
182
- sectionPropertiesXml = blockXml;
183
- } else {
184
- bodyPieces.push(blockXml);
185
- bodyLength += blockXml.length;
186
- }
187
- cursor += 1;
188
- previousWasParagraph = false;
189
- }
190
-
191
- const bodyXml = bodyPieces.join("");
192
- const documentXml = `${prefix}${bodyXml || "<w:p><w:r><w:t></w:t></w:r></w:p>"}${sectionPropertiesXml}${suffix}`;
193
-
194
- return {
195
- documentXml,
196
- relationships: state.relationships,
197
- paragraphBoundaries,
198
- };
199
- }
200
-
201
- function serializeOpaqueBlock(
202
- block: Extract<DocumentRootNode["children"][number], { type: "opaque_block" }>,
203
- state: SerializationState,
204
- ): string {
205
- return lookupOpaqueXml(block.fragmentId, state);
206
- }
207
-
208
- function serializeTableNode(
209
- table: TableNode,
210
- state: SerializationState,
211
- ): string {
212
- const propertiesXml = table.propertiesXml ?? buildTablePropertiesXml(table);
213
- const gridXml =
214
- table.gridColumns.length > 0
215
- ? `<w:tblGrid>${table.gridColumns
216
- .map((width) => `<w:gridCol w:w="${width}"/>`)
217
- .join("")}</w:tblGrid>`
218
- : "";
219
- const rowsXml = table.rows
220
- .map((row) => {
221
- const rowPropertiesXml = row.propertiesXml ?? buildTableRowPropertiesXml(row);
222
- const cellsXml = row.cells
223
- .map((cell) => serializeTableCellNode(cell, state))
224
- .join("");
225
- return `<w:tr>${rowPropertiesXml}${cellsXml}</w:tr>`;
226
- })
227
- .join("");
228
- return `<w:tbl>${propertiesXml}${gridXml}${rowsXml}</w:tbl>`;
229
- }
230
-
231
- function serializeTableCellNode(
232
- cell: TableCellNode,
233
- state: SerializationState,
234
- ): string {
235
- const propertiesXml = cell.propertiesXml ?? buildCellPropertiesXml(cell);
236
- const blocksXml = cell.children
237
- .map((child) => serializeBlockNode(child, state))
238
- .join("");
239
- return `<w:tc>${propertiesXml}${blocksXml || "<w:p/>"}</w:tc>`;
240
- }
241
-
242
- function serializeBlockNode(
243
- block: DocumentRootNode["children"][number],
244
- state: SerializationState,
245
- ): string {
246
- switch (block.type) {
247
- case "paragraph":
248
- return serializeTableCellParagraph(block, state);
249
- case "table":
250
- return serializeTableNode(block, state);
251
- case "sdt":
252
- return serializeSdtNode(block, state);
253
- case "custom_xml":
254
- return serializeCustomXmlNode(block, state);
255
- case "alt_chunk":
256
- return serializeAltChunkNode(block);
257
- case "opaque_block":
258
- return lookupOpaqueXml(block.fragmentId, state);
259
- case "section_break":
260
- if (block.sectionPropertiesXml ?? block.propertiesXml) {
261
- return block.sectionPropertiesXml ?? block.propertiesXml!;
262
- }
263
- if (block.sectionProperties) return serializeSectionPropertiesXml(block.sectionProperties);
264
- return "<w:sectPr/>";
265
- }
266
- }
267
-
268
- function buildCellPropertiesXml(cell: TableCellNode): string {
269
- const children: string[] = [];
270
- if (cell.gridSpan && cell.gridSpan > 1) {
271
- children.push(`<w:gridSpan w:val="${cell.gridSpan}"/>`);
272
- }
273
- if (cell.verticalMerge) {
274
- children.push(
275
- cell.verticalMerge === "restart"
276
- ? `<w:vMerge w:val="restart"/>`
277
- : `<w:vMerge/>`,
278
- );
279
- }
280
- return children.length > 0 ? `<w:tcPr>${children.join("")}</w:tcPr>` : "";
281
- }
282
-
283
- function buildTableRowPropertiesXml(row: TableNode["rows"][number]): string {
284
- const children: string[] = [];
285
- if (row.gridBefore !== undefined) {
286
- children.push(`<w:gridBefore w:val="${row.gridBefore}"/>`);
287
- }
288
- if (row.widthBefore) {
289
- children.push(`<w:wBefore w:w="${row.widthBefore.value}" w:type="${row.widthBefore.type}"/>`);
290
- }
291
- if (row.gridAfter !== undefined) {
292
- children.push(`<w:gridAfter w:val="${row.gridAfter}"/>`);
293
- }
294
- if (row.widthAfter) {
295
- children.push(`<w:wAfter w:w="${row.widthAfter.value}" w:type="${row.widthAfter.type}"/>`);
296
- }
297
- if (children.length === 0) {
298
- return "";
299
- }
300
- return `<w:trPr>${children.join("")}</w:trPr>`;
301
- }
302
-
303
- function serializeSdtNode(
304
- block: SdtNode,
305
- state: SerializationState,
306
- ): string {
307
- const propertiesXml = block.properties.propertiesXml ?? buildSdtPropertiesXml(block);
308
- const childrenXml = block.children.map((child) => serializeBlockNode(child, state)).join("") || "<w:p/>";
309
- return `<w:sdt>${propertiesXml}<w:sdtContent>${childrenXml}</w:sdtContent></w:sdt>`;
310
- }
311
-
312
- function serializeCustomXmlNode(
313
- block: CustomXmlNode,
314
- state: SerializationState,
315
- ): string {
316
- const attrs: string[] = [];
317
- if (block.uri) {
318
- attrs.push(`w:uri="${escapeAttribute(block.uri)}"`);
319
- }
320
- if (block.element) {
321
- attrs.push(`w:element="${escapeAttribute(block.element)}"`);
322
- }
323
- const attrXml = attrs.length > 0 ? ` ${attrs.join(" ")}` : "";
324
- const childrenXml = block.children.map((child) => serializeBlockNode(child, state)).join("");
325
- return `<w:customXml${attrXml}>${childrenXml || "<w:p/>"}</w:customXml>`;
326
- }
327
-
328
- function serializeAltChunkNode(
329
- block: AltChunkNode,
330
- ): string {
331
- return `<w:altChunk r:id="${escapeAttribute(block.relationshipId)}"/>`;
332
- }
333
-
334
- function buildSdtPropertiesXml(block: SdtNode): string {
335
- const children: string[] = [];
336
- if (block.properties.alias) {
337
- children.push(`<w:alias w:val="${escapeAttribute(block.properties.alias)}"/>`);
338
- }
339
- if (block.properties.tag) {
340
- children.push(`<w:tag w:val="${escapeAttribute(block.properties.tag)}"/>`);
341
- }
342
- if (block.properties.lock) {
343
- children.push(`<w:lock w:val="${escapeAttribute(block.properties.lock)}"/>`);
344
- }
345
- if (block.properties.showingPlcHdr) {
346
- children.push(`<w:showingPlcHdr/>`);
347
- }
348
- if (block.properties.checkbox) {
349
- const cb = block.properties.checkbox;
350
- const cbParts: string[] = [];
351
- cbParts.push(`<w14:checked w14:val="${cb.checked ? "1" : "0"}"/>`);
352
- if (cb.checkedChar) cbParts.push(`<w14:checkedState w14:val="${escapeAttribute(cb.checkedChar)}"/>`);
353
- if (cb.uncheckedChar) cbParts.push(`<w14:uncheckedState w14:val="${escapeAttribute(cb.uncheckedChar)}"/>`);
354
- children.push(`<w14:checkbox>${cbParts.join("")}</w14:checkbox>`);
355
- } else if (block.properties.datePicker) {
356
- const dp = block.properties.datePicker;
357
- const dateAttrs = dp.fullDate ? ` w:fullDate="${escapeAttribute(dp.fullDate)}"` : "";
358
- const dpParts: string[] = [];
359
- if (dp.dateFormat) dpParts.push(`<w:dateFormat w:val="${escapeAttribute(dp.dateFormat)}"/>`);
360
- if (dp.lid) dpParts.push(`<w:lid w:val="${escapeAttribute(dp.lid)}"/>`);
361
- children.push(dpParts.length > 0 ? `<w:date${dateAttrs}>${dpParts.join("")}</w:date>` : `<w:date${dateAttrs}/>`);
362
- } else if (block.properties.dropdownList) {
363
- const items = block.properties.dropdownList.map((item) => {
364
- const dt = item.displayText ? ` w:displayText="${escapeAttribute(item.displayText)}"` : "";
365
- return `<w:listItem${dt} w:value="${escapeAttribute(item.value)}"/>`;
366
- }).join("");
367
- children.push(`<w:dropDownList>${items}</w:dropDownList>`);
368
- } else if (block.properties.comboBox) {
369
- const items = block.properties.comboBox.map((item) => {
370
- const dt = item.displayText ? ` w:displayText="${escapeAttribute(item.displayText)}"` : "";
371
- return `<w:listItem${dt} w:value="${escapeAttribute(item.value)}"/>`;
372
- }).join("");
373
- children.push(`<w:comboBox>${items}</w:comboBox>`);
374
- } else if (block.properties.sdtType === "plainText") {
375
- children.push(`<w:text/>`);
376
- } else if (block.properties.sdtType === "richText") {
377
- children.push(`<w:richText/>`);
378
- } else if (block.properties.sdtType) {
379
- children.push(`<w:${block.properties.sdtType}/>`);
380
- }
381
- return children.length > 0 ? `<w:sdtPr>${children.join("")}</w:sdtPr>` : "<w:sdtPr/>";
382
- }
383
-
384
- function serializeTableCellParagraph(
385
- paragraph: ParagraphNode,
386
- state: SerializationState,
387
- ): string {
388
- let xml = "<w:p>";
389
- const paragraphPropertiesXml = buildParagraphPropertiesXml(paragraph);
390
- if (paragraphPropertiesXml.length > 0) {
391
- xml += paragraphPropertiesXml;
392
- }
393
- const childrenXml = paragraph.children.map((child) => serializeTableInlineNode(child, state)).join("");
394
- xml += childrenXml || "<w:r><w:t></w:t></w:r>";
395
- xml += "</w:p>";
396
- return xml;
397
- }
398
-
399
- function serializeTableInlineNode(
400
- node: InlineNode,
401
- state: SerializationState,
402
- ): string {
403
- switch (node.type) {
404
- case "text": {
405
- const marks = node.marks;
406
- const properties = serializeRunPropertiesFromMarks(marks);
407
- const preserve = requiresPreservedSpace(node.text) ? ` xml:space="preserve"` : "";
408
- return `<w:r>${properties}<w:t${preserve}>${escapeXml(node.text)}</w:t></w:r>`;
409
- }
410
- case "tab":
411
- return "<w:r><w:tab/></w:r>";
412
- case "column_break":
413
- return "<w:r><w:br w:type=\"column\"/></w:r>";
414
- case "hard_break":
415
- return "<w:r><w:br/></w:r>";
416
- case "symbol": {
417
- const properties = serializeRunPropertiesFromMarks(node.marks);
418
- const fontAttribute = node.font ? ` w:font="${escapeAttribute(node.font)}"` : "";
419
- return `<w:r>${properties}<w:sym${fontAttribute} w:char="${escapeAttribute(node.char)}"/></w:r>`;
420
- }
421
- case "image":
422
- return serializeImageNode(node, state);
423
- case "opaque_inline":
424
- return lookupOpaqueXml(node.fragmentId, state);
425
- case "chart_preview":
426
- case "smartart_preview":
427
- case "shape":
428
- case "wordart":
429
- case "vml_shape":
430
- return wrapInlineRawXml(node.rawXml);
431
- case "hyperlink": {
432
- const hyperlinkOpen = node.href.startsWith("#")
433
- ? `<w:hyperlink w:anchor="${escapeAttribute(node.href.slice(1))}">`
434
- : (() => {
435
- const relationshipId = `rIdHyperlink${state.nextHyperlinkRelationshipIndex}`;
436
- state.nextHyperlinkRelationshipIndex += 1;
437
- state.relationships.push({
438
- id: relationshipId,
439
- type: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink",
440
- target: node.href,
441
- targetMode: "external",
442
- });
443
- state.retainedRelationshipIds.add(relationshipId);
444
- return `<w:hyperlink r:id="${relationshipId}">`;
445
- })();
446
- const childrenXml = node.children.map((child) => serializeTableInlineNode(child, state)).join("");
447
- return `${hyperlinkOpen}${childrenXml}</w:hyperlink>`;
448
- }
449
- case "field": {
450
- if (node.children && node.children.length > 0) {
451
- const childrenXml = node.children
452
- .map((child) => serializeTableInlineNode(child, state))
453
- .join("");
454
- if (node.fieldType === "complex") {
455
- return (
456
- `<w:r><w:fldChar w:fldCharType="begin"/></w:r>` +
457
- `<w:r><w:instrText xml:space="preserve"> ${escapeXml(node.instruction)} </w:instrText></w:r>` +
458
- `<w:r><w:fldChar w:fldCharType="separate"/></w:r>` +
459
- childrenXml +
460
- `<w:r><w:fldChar w:fldCharType="end"/></w:r>`
461
- );
462
- }
463
- return `<w:fldSimple w:instr="${escapeAttribute(node.instruction)}">${childrenXml}</w:fldSimple>`;
464
- }
465
- return `<w:fldSimple w:instr="${escapeAttribute(node.instruction)}"/>`;
466
- }
467
- case "bookmark_start":
468
- return (
469
- `<w:bookmarkStart w:id="${escapeAttribute(node.bookmarkId)}"` +
470
- ` w:name="${escapeAttribute(node.name)}"/>`
471
- );
472
- case "bookmark_end":
473
- return `<w:bookmarkEnd w:id="${escapeAttribute(node.bookmarkId)}"/>`;
474
- case "footnote_ref": {
475
- const refElement =
476
- node.noteKind === "footnote"
477
- ? `<w:footnoteReference w:id="${escapeAttribute(node.noteId)}"/>`
478
- : `<w:endnoteReference w:id="${escapeAttribute(node.noteId)}"/>`;
479
- const styleVal =
480
- node.noteKind === "footnote"
481
- ? "FootnoteReference"
482
- : "EndnoteReference";
483
- return `<w:r><w:rPr><w:rStyle w:val="${styleVal}"/></w:rPr>${refElement}</w:r>`;
484
- }
485
- default:
486
- return "";
487
- }
488
- }
489
-
490
- function buildParagraphPropertiesXml(paragraph: ParagraphNode): string {
491
- const children: string[] = [];
492
-
493
- if (paragraph.styleId) {
494
- children.push(`<w:pStyle w:val="${escapeAttribute(paragraph.styleId)}"/>`);
495
- }
496
- if (paragraph.keepNext) {
497
- children.push("<w:keepNext/>");
498
- }
499
- if (paragraph.keepLines) {
500
- children.push("<w:keepLines/>");
501
- }
502
- if (paragraph.pageBreakBefore) {
503
- children.push("<w:pageBreakBefore/>");
504
- }
505
- if (paragraph.widowControl) {
506
- children.push("<w:widowControl/>");
507
- }
508
- if (paragraph.outlineLevel !== undefined) {
509
- children.push(`<w:outlineLvl w:val="${paragraph.outlineLevel}"/>`);
510
- }
511
- if (paragraph.numbering) {
512
- children.push(serializeParagraphNumberingProperties(paragraph.numbering));
513
- }
514
- if (paragraph.spacing) {
515
- const s = paragraph.spacing;
516
- const attrs: string[] = [];
517
- if (s.before !== undefined) attrs.push(`w:before="${s.before}"`);
518
- if (s.after !== undefined) attrs.push(`w:after="${s.after}"`);
519
- if (s.line !== undefined) attrs.push(`w:line="${s.line}"`);
520
- if (s.lineRule !== undefined) attrs.push(`w:lineRule="${s.lineRule}"`);
521
- if (attrs.length > 0) children.push(`<w:spacing ${attrs.join(" ")}/>`);
522
- }
523
- if (paragraph.contextualSpacing !== undefined) {
524
- children.push(
525
- paragraph.contextualSpacing
526
- ? "<w:contextualSpacing/>"
527
- : `<w:contextualSpacing w:val="0"/>`,
528
- );
529
- }
530
- if (paragraph.indentation) {
531
- const ind = paragraph.indentation;
532
- const attrs: string[] = [];
533
- if (ind.left !== undefined) attrs.push(`w:left="${ind.left}"`);
534
- if (ind.right !== undefined) attrs.push(`w:right="${ind.right}"`);
535
- if (ind.firstLine !== undefined) attrs.push(`w:firstLine="${ind.firstLine}"`);
536
- if (ind.hanging !== undefined) attrs.push(`w:hanging="${ind.hanging}"`);
537
- if (attrs.length > 0) children.push(`<w:ind ${attrs.join(" ")}/>`);
538
- }
539
- if (paragraph.alignment) {
540
- children.push(`<w:jc w:val="${paragraph.alignment}"/>`);
541
- }
542
- if (paragraph.borders) {
543
- const bordersXml = serializeParagraphBorders(paragraph.borders);
544
- if (bordersXml) {
545
- children.push(bordersXml);
546
- }
547
- }
548
- if (paragraph.shading) {
549
- const shadingXml = serializeParagraphShading(paragraph.shading);
550
- if (shadingXml) {
551
- children.push(shadingXml);
552
- }
553
- }
554
- if (paragraph.bidi) {
555
- children.push("<w:bidi/>");
556
- }
557
- if (paragraph.suppressLineNumbers) {
558
- children.push("<w:suppressLineNumbers/>");
559
- }
560
- if (paragraph.cnfStyle) {
561
- children.push(`<w:cnfStyle w:val="${escapeAttribute(paragraph.cnfStyle)}"/>`);
562
- }
563
- if (paragraph.tabStops && paragraph.tabStops.length > 0) {
564
- const tabsXml = paragraph.tabStops.map((tab) => {
565
- const leaderAttr = tab.leader ? ` w:leader="${tab.leader}"` : "";
566
- return `<w:tab w:val="${tab.align}" w:pos="${tab.position}"${leaderAttr}/>`;
567
- }).join("");
568
- children.push(`<w:tabs>${tabsXml}</w:tabs>`);
569
- }
570
-
571
- if (children.length === 0) return "";
572
- return `<w:pPr>${children.join("")}</w:pPr>`;
573
- }
574
-
575
- function buildTablePropertiesXml(table: TableNode): string {
576
- const children: string[] = [];
577
- if (table.styleId) {
578
- children.push(`<w:tblStyle w:val="${escapeAttribute(table.styleId)}"/>`);
579
- }
580
- if (table.tblLook) {
581
- const attrs: string[] = [];
582
- if (table.tblLook.val) {
583
- attrs.push(`w:val="${escapeAttribute(table.tblLook.val)}"`);
584
- }
585
- for (const [key, attr] of [
586
- ["firstRow", "w:firstRow"],
587
- ["lastRow", "w:lastRow"],
588
- ["firstColumn", "w:firstColumn"],
589
- ["lastColumn", "w:lastColumn"],
590
- ["noHBand", "w:noHBand"],
591
- ["noVBand", "w:noVBand"],
592
- ] as const) {
593
- const value = table.tblLook[key];
594
- if (value !== undefined) {
595
- attrs.push(`${attr}="${value ? "1" : "0"}"`);
596
- }
597
- }
598
- if (attrs.length > 0) {
599
- children.push(`<w:tblLook ${attrs.join(" ")}/>`);
600
- }
601
- }
602
- return children.length > 0 ? `<w:tblPr>${children.join("")}</w:tblPr>` : "";
603
- }
604
-
605
- function serializeParagraphBorders(borders: ParagraphNode["borders"]): string {
606
- if (!borders) {
607
- return "";
608
- }
609
- const parts: string[] = [];
610
- for (const [name, border] of [
611
- ["top", borders.top],
612
- ["left", borders.left],
613
- ["bottom", borders.bottom],
614
- ["right", borders.right],
615
- ["bar", borders.bar],
616
- ["between", borders.between],
617
- ] as const) {
618
- const xml = serializeBorder(name, border);
619
- if (xml) {
620
- parts.push(xml);
621
- }
622
- }
623
- return parts.length > 0 ? `<w:pBdr>${parts.join("")}</w:pBdr>` : "";
624
- }
625
-
626
- function serializeBorder(name: string, border: BorderSpec | undefined): string {
627
- if (!border) {
628
- return "";
629
- }
630
- const attrs: string[] = [];
631
- if (border.value) attrs.push(`w:val="${escapeAttribute(border.value)}"`);
632
- if (border.size !== undefined) attrs.push(`w:sz="${border.size}"`);
633
- if (border.space !== undefined) attrs.push(`w:space="${border.space}"`);
634
- if (border.color) attrs.push(`w:color="${escapeAttribute(border.color)}"`);
635
- return attrs.length > 0 ? `<w:${name} ${attrs.join(" ")}/>` : "";
636
- }
637
-
638
- function serializeParagraphShading(shading: ParagraphNode["shading"]): string {
639
- if (!shading) {
640
- return "";
641
- }
642
- const attrs: string[] = [];
643
- if (shading.val) attrs.push(`w:val="${escapeAttribute(shading.val)}"`);
644
- if (shading.color) attrs.push(`w:color="${escapeAttribute(shading.color)}"`);
645
- if (shading.fill) attrs.push(`w:fill="${escapeAttribute(shading.fill)}"`);
646
- return attrs.length > 0 ? `<w:shd ${attrs.join(" ")}/>` : "";
647
- }
648
-
649
- function serializeRunPropertiesFromMarks(marks: TextMark[] | undefined): string {
650
- return serializeRunProperties(marks);
651
- }
652
-
653
- function serializeParagraph(
654
- paragraph: ParagraphNode,
655
- state: SerializationState,
656
- cursor: number,
657
- paragraphIndex: number,
658
- ): ParagraphSerializationResult {
659
- let xml = "<w:p>";
660
- const boundaries = new Map<number, number>();
661
- const paragraphStart = 0;
662
- const paragraphStartTagEnd = xml.length;
663
- boundaries.set(cursor, paragraphStartTagEnd);
664
-
665
- let paragraphPropertiesStart: number | undefined;
666
- let paragraphPropertiesEnd: number | undefined;
667
-
668
- const paragraphPropertiesXml = buildParagraphPropertiesXml(paragraph);
669
- if (paragraphPropertiesXml.length > 0) {
670
- paragraphPropertiesStart = xml.length;
671
- xml += paragraphPropertiesXml;
672
- paragraphPropertiesEnd = xml.length;
673
- }
674
-
675
- const children = serializeParagraphChildren(paragraph.children, state, cursor, xml.length);
676
- xml += children.xml;
677
- const contentEmpty = children.xml.length === 0;
678
- if (contentEmpty) {
679
- xml += "<w:r><w:t></w:t></w:r>";
680
- }
681
- const paragraphEndTagStart = xml.length;
682
- xml += "</w:p>";
683
-
684
- if (!children.boundaries.has(children.cursor)) {
685
- children.boundaries.set(children.cursor, paragraphEndTagStart);
686
- }
687
-
688
- return {
689
- xml,
690
- nextCursor: children.cursor,
691
- boundary: {
692
- paragraphIndex,
693
- start: cursor,
694
- end: children.cursor,
695
- boundaries: children.boundaries,
696
- paragraphStart,
697
- paragraphStartTagEnd,
698
- paragraphEndTagStart,
699
- paragraphEnd: xml.length,
700
- ...(paragraphPropertiesStart !== undefined
701
- ? { paragraphPropertiesStart }
702
- : {}),
703
- ...(paragraphPropertiesEnd !== undefined ? { paragraphPropertiesEnd } : {}),
704
- },
705
- };
706
- }
707
-
708
- function serializeParagraphChildren(
709
- children: InlineNode[],
710
- state: SerializationState,
711
- cursor: number,
712
- xmlOffset: number,
713
- ): InlineSerializationResult {
714
- const pieces: string[] = [];
715
- const boundaries = new Map<number, number>();
716
- let nextCursor = cursor;
717
- let nextOffset = xmlOffset;
718
- boundaries.set(nextCursor, nextOffset);
719
-
720
- for (const child of children) {
721
- const result = serializeInlineNode(child, state, nextCursor, nextOffset);
722
- pieces.push(result.xml);
723
- for (const [position, index] of result.boundaries) {
724
- boundaries.set(position, index);
725
- }
726
- nextCursor = result.cursor;
727
- nextOffset += result.xml.length;
728
- }
729
-
730
- return {
731
- xml: pieces.join(""),
732
- cursor: nextCursor,
733
- boundaries,
734
- };
735
- }
736
-
737
- type RunPiece =
738
- | { kind: "text"; text: string; marks?: TextMark[] }
739
- | { kind: "tab" }
740
- | { kind: "hard_break" };
741
-
742
- function serializeRunPiece(piece: RunPiece): string {
743
- switch (piece.kind) {
744
- case "text":
745
- return serializeText(piece.text);
746
- case "tab":
747
- return "<w:tab/>";
748
- case "hard_break":
749
- return "<w:br/>";
750
- }
751
- }
752
-
753
- function serializeText(text: string): string {
754
- const preserve = requiresPreservedSpace(text) ? ` xml:space="preserve"` : "";
755
- return `<w:t${preserve}>${escapeXml(text)}</w:t>`;
756
- }
757
-
758
- function serializeInlineNode(
759
- node: InlineNode,
760
- state: SerializationState,
761
- cursor: number,
762
- xmlOffset: number,
763
- ): InlineSerializationResult {
764
- switch (node.type) {
765
- case "text": {
766
- const xml = serializeRun({
767
- kind: "text",
768
- text: node.text,
769
- ...(node.marks && node.marks.length > 0 ? { marks: node.marks } : {}),
770
- });
771
- const boundaries = new Map<number, number>();
772
- boundaries.set(cursor, xmlOffset);
773
- boundaries.set(cursor + Array.from(node.text).length, xmlOffset + xml.length);
774
- return {
775
- xml,
776
- cursor: cursor + Array.from(node.text).length,
777
- boundaries,
778
- };
779
- }
780
- case "tab": {
781
- const xml = serializeRun({ kind: "tab" });
782
- const boundaries = new Map<number, number>();
783
- boundaries.set(cursor, xmlOffset);
784
- boundaries.set(cursor + 1, xmlOffset + xml.length);
785
- return {
786
- xml,
787
- cursor: cursor + 1,
788
- boundaries,
789
- };
790
- }
791
- case "column_break": {
792
- const xml = `<w:r><w:br w:type="column"/></w:r>`;
793
- const boundaries = new Map<number, number>();
794
- boundaries.set(cursor, xmlOffset);
795
- boundaries.set(cursor + 1, xmlOffset + xml.length);
796
- return {
797
- xml,
798
- cursor: cursor + 1,
799
- boundaries,
800
- };
801
- }
802
- case "hard_break": {
803
- const xml = serializeRun({ kind: "hard_break" });
804
- const boundaries = new Map<number, number>();
805
- boundaries.set(cursor, xmlOffset);
806
- boundaries.set(cursor + 1, xmlOffset + xml.length);
807
- return {
808
- xml,
809
- cursor: cursor + 1,
810
- boundaries,
811
- };
812
- }
813
- case "symbol": {
814
- const xml = serializeTableInlineNode(node, state);
815
- const boundaries = new Map<number, number>();
816
- boundaries.set(cursor, xmlOffset);
817
- boundaries.set(cursor + 1, xmlOffset + xml.length);
818
- return {
819
- xml,
820
- cursor: cursor + 1,
821
- boundaries,
822
- };
823
- }
824
- case "image": {
825
- const xml = serializeImageNode(node, state);
826
- const boundaries = new Map<number, number>();
827
- boundaries.set(cursor, xmlOffset);
828
- boundaries.set(cursor + 1, xmlOffset + xml.length);
829
- return {
830
- xml,
831
- cursor: cursor + 1,
832
- boundaries,
833
- };
834
- }
835
- case "opaque_inline": {
836
- const xml = lookupOpaqueXml(node.fragmentId, state);
837
- const boundaries = new Map<number, number>();
838
- boundaries.set(cursor, xmlOffset);
839
- boundaries.set(cursor + 1, xmlOffset + xml.length);
840
- return {
841
- xml,
842
- cursor: cursor + 1,
843
- boundaries,
844
- };
845
- }
846
- case "chart_preview":
847
- case "smartart_preview":
848
- case "shape":
849
- case "wordart":
850
- case "vml_shape": {
851
- // Reattach original XML unchanged for lossless round-trip.
852
- const xml = wrapInlineRawXml(node.rawXml);
853
- const boundaries = new Map<number, number>();
854
- boundaries.set(cursor, xmlOffset);
855
- boundaries.set(cursor + 1, xmlOffset + xml.length);
856
- return {
857
- xml,
858
- cursor: cursor + 1,
859
- boundaries,
860
- };
861
- }
862
- case "hyperlink": {
863
- const hyperlinkOpen = node.href.startsWith("#")
864
- ? `<w:hyperlink w:anchor="${escapeAttribute(node.href.slice(1))}">`
865
- : (() => {
866
- const relationshipId = `rIdHyperlink${state.nextHyperlinkRelationshipIndex}`;
867
- state.nextHyperlinkRelationshipIndex += 1;
868
- state.relationships.push({
869
- id: relationshipId,
870
- type: HYPERLINK_RELATIONSHIP_TYPE,
871
- target: node.href,
872
- targetMode: "external",
873
- });
874
- state.retainedRelationshipIds.add(relationshipId);
875
- return `<w:hyperlink r:id="${relationshipId}">`;
876
- })();
877
- const hyperlinkClose = "</w:hyperlink>";
878
- const boundaries = new Map<number, number>();
879
- let nextCursor = cursor;
880
- let nextOffset = xmlOffset + hyperlinkOpen.length;
881
- boundaries.set(cursor, nextOffset);
882
- const children: string[] = [];
883
-
884
- for (const child of node.children) {
885
- const result = serializeInlineNode(child, state, nextCursor, nextOffset);
886
- children.push(result.xml);
887
- for (const [position, index] of result.boundaries) {
888
- boundaries.set(position, index);
889
- }
890
- nextCursor = result.cursor;
891
- nextOffset += result.xml.length;
892
- }
893
-
894
- boundaries.set(nextCursor, nextOffset);
895
- return {
896
- xml: `${hyperlinkOpen}${children.join("")}${hyperlinkClose}`,
897
- cursor: nextCursor,
898
- boundaries,
899
- };
900
- }
901
- case "field": {
902
- if (node.children && node.children.length > 0) {
903
- const boundaries = new Map<number, number>();
904
- boundaries.set(cursor, xmlOffset);
905
- let nextCursor = cursor;
906
- let nextOffset = xmlOffset;
907
-
908
- if (node.fieldType === "complex") {
909
- const beginXml =
910
- `<w:r><w:fldChar w:fldCharType="begin"/></w:r>` +
911
- `<w:r><w:instrText xml:space="preserve"> ${escapeXml(node.instruction)} </w:instrText></w:r>` +
912
- `<w:r><w:fldChar w:fldCharType="separate"/></w:r>`;
913
- nextOffset += beginXml.length;
914
-
915
- const children: string[] = [beginXml];
916
- for (const child of node.children) {
917
- const result = serializeInlineNode(child, state, nextCursor, nextOffset);
918
- children.push(result.xml);
919
- for (const [position, index] of result.boundaries) {
920
- boundaries.set(position, index);
921
- }
922
- nextCursor = result.cursor;
923
- nextOffset += result.xml.length;
924
- }
925
- const endXml = `<w:r><w:fldChar w:fldCharType="end"/></w:r>`;
926
- children.push(endXml);
927
- nextOffset += endXml.length;
928
- boundaries.set(nextCursor, nextOffset);
929
- return { xml: children.join(""), cursor: nextCursor, boundaries };
930
- }
931
-
932
- // Simple field with children
933
- const openXml = `<w:fldSimple w:instr="${escapeAttribute(node.instruction)}">`;
934
- nextOffset += openXml.length;
935
- const children: string[] = [openXml];
936
- for (const child of node.children) {
937
- const result = serializeInlineNode(child, state, nextCursor, nextOffset);
938
- children.push(result.xml);
939
- for (const [position, index] of result.boundaries) {
940
- boundaries.set(position, index);
941
- }
942
- nextCursor = result.cursor;
943
- nextOffset += result.xml.length;
944
- }
945
- children.push("</w:fldSimple>");
946
- nextOffset += "</w:fldSimple>".length;
947
- boundaries.set(nextCursor, nextOffset);
948
- return { xml: children.join(""), cursor: nextCursor, boundaries };
949
- }
950
-
951
- const xml = `<w:fldSimple w:instr="${escapeAttribute(node.instruction)}"/>`;
952
- const boundaries = new Map<number, number>();
953
- boundaries.set(cursor, xmlOffset);
954
- boundaries.set(cursor + 1, xmlOffset + xml.length);
955
- return {
956
- xml,
957
- cursor: cursor + 1,
958
- boundaries,
959
- };
960
- }
961
- case "bookmark_start": {
962
- const xml =
963
- `<w:bookmarkStart w:id="${escapeAttribute(node.bookmarkId)}"` +
964
- ` w:name="${escapeAttribute(node.name)}"/>`;
965
- const boundaries = new Map<number, number>();
966
- boundaries.set(cursor, xmlOffset);
967
- boundaries.set(cursor, xmlOffset + xml.length);
968
- return { xml, cursor, boundaries };
969
- }
970
- case "bookmark_end": {
971
- const xml = `<w:bookmarkEnd w:id="${escapeAttribute(node.bookmarkId)}"/>`;
972
- const boundaries = new Map<number, number>();
973
- boundaries.set(cursor, xmlOffset);
974
- boundaries.set(cursor, xmlOffset + xml.length);
975
- return { xml, cursor, boundaries };
976
- }
977
- case "footnote_ref": {
978
- const refElement =
979
- node.noteKind === "footnote"
980
- ? `<w:footnoteReference w:id="${escapeAttribute(node.noteId)}"/>`
981
- : `<w:endnoteReference w:id="${escapeAttribute(node.noteId)}"/>`;
982
- const styleVal =
983
- node.noteKind === "footnote"
984
- ? "FootnoteReference"
985
- : "EndnoteReference";
986
- const xml = `<w:r><w:rPr><w:rStyle w:val="${styleVal}"/></w:rPr>${refElement}</w:r>`;
987
- const boundaries = new Map<number, number>();
988
- boundaries.set(cursor, xmlOffset);
989
- boundaries.set(cursor + 1, xmlOffset + xml.length);
990
- return {
991
- xml,
992
- cursor: cursor + 1,
993
- boundaries,
994
- };
995
- }
996
- default: {
997
- const boundaries = new Map<number, number>();
998
- boundaries.set(cursor, xmlOffset);
999
- return { xml: "", cursor, boundaries };
1000
- }
1001
- }
1002
- }
1003
-
1004
- function serializeImageNode(
1005
- node: Extract<InlineNode, { type: "image" }>,
1006
- state: SerializationState,
1007
- ): string {
1008
- const placementXml = typeof node.placementXml === "string" ? node.placementXml.trim() : "";
1009
- if (placementXml.length > 0) {
1010
- return placementXml.startsWith("<w:r") ? placementXml : `<w:r>${placementXml}</w:r>`;
1011
- }
1012
-
1013
- const mediaItem = state.media.items[node.mediaId];
1014
- if (mediaItem?.relationshipId && state.existingRelationshipMap.has(mediaItem.relationshipId)) {
1015
- const altText = node.altText ?? mediaItem.altText ?? mediaItem.filename;
1016
- return `<w:r><w:drawing><wp:inline xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"><wp:extent cx="9525" cy="9525"/><wp:docPr id="1" name="${escapeAttribute(mediaItem.filename)}" descr="${escapeAttribute(altText ?? "")}"/><wp:cNvGraphicFramePr/><a:graphic><a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture"><pic:pic><pic:nvPicPr><pic:cNvPr id="0" name="${escapeAttribute(mediaItem.filename)}"/><pic:cNvPicPr/></pic:nvPicPr><pic:blipFill><a:blip r:embed="${escapeAttribute(mediaItem.relationshipId)}"/><a:stretch><a:fillRect/></a:stretch></pic:blipFill><pic:spPr><a:xfrm><a:off x="0" y="0"/><a:ext cx="9525" cy="9525"/></a:xfrm><a:prstGeom prst="rect"><a:avLst/></a:prstGeom></pic:spPr></pic:pic></a:graphicData></a:graphic></wp:inline></w:drawing></w:r>`;
1017
- }
1018
-
1019
- return serializeRun({
1020
- kind: "text",
1021
- text: node.altText ?? "[Image]",
1022
- });
1023
- }
1024
-
1025
- function serializeRun(piece: RunPiece): string {
1026
- const marks = piece.kind === "text" ? piece.marks : undefined;
1027
- const properties = serializeRunProperties(marks);
1028
- const content = serializeRunPiece(piece);
1029
- return `<w:r>${properties}${content}</w:r>`;
1030
- }
1031
-
1032
- function serializeRunProperties(marks: TextMark[] | undefined): string {
1033
- if (!marks || marks.length === 0) {
1034
- return "";
1035
- }
1036
-
1037
- const markParts: string[] = [];
1038
- for (const mark of marks) {
1039
- switch (mark.type) {
1040
- case "bold":
1041
- markParts.push("<w:b/>");
1042
- break;
1043
- case "italic":
1044
- markParts.push("<w:i/>");
1045
- break;
1046
- case "underline":
1047
- markParts.push(`<w:u w:val="single"/>`);
1048
- break;
1049
- case "strikethrough":
1050
- markParts.push("<w:strike/>");
1051
- break;
1052
- case "doubleStrikethrough":
1053
- markParts.push("<w:dstrike/>");
1054
- break;
1055
- case "vanish":
1056
- markParts.push("<w:vanish/>");
1057
- break;
1058
- case "lang":
1059
- markParts.push(`<w:lang w:val="${escapeAttribute(mark.val)}"/>`);
1060
- break;
1061
- case "highlight":
1062
- markParts.push(`<w:highlight w:val="${escapeAttribute(mark.val)}"/>`);
1063
- break;
1064
- case "backgroundColor":
1065
- markParts.push(
1066
- `<w:shd w:val="clear" w:color="auto" w:fill="${escapeAttribute(mark.color)}"/>`,
1067
- );
1068
- break;
1069
- case "charSpacing":
1070
- markParts.push(`<w:spacing w:val="${mark.val}"/>`);
1071
- break;
1072
- case "kerning":
1073
- markParts.push(`<w:kern w:val="${mark.val}"/>`);
1074
- break;
1075
- case "emboss":
1076
- markParts.push("<w:emboss/>");
1077
- break;
1078
- case "imprint":
1079
- markParts.push("<w:imprint/>");
1080
- break;
1081
- case "shadow":
1082
- markParts.push("<w:shadow/>");
1083
- break;
1084
- case "position":
1085
- markParts.push(`<w:position w:val="${mark.val}"/>`);
1086
- break;
1087
- case "textFill":
1088
- markParts.push(mark.xml);
1089
- break;
1090
- case "fontFamily":
1091
- markParts.push(`<w:rFonts w:ascii="${escapeAttribute(mark.val)}" w:hAnsi="${escapeAttribute(mark.val)}"/>`);
1092
- break;
1093
- case "fontSize":
1094
- markParts.push(`<w:sz w:val="${mark.val}"/>`);
1095
- break;
1096
- case "textColor":
1097
- markParts.push(`<w:color w:val="${escapeAttribute(mark.color)}"/>`);
1098
- break;
1099
- case "smallCaps":
1100
- markParts.push("<w:smallCaps/>");
1101
- break;
1102
- case "allCaps":
1103
- markParts.push("<w:caps/>");
1104
- break;
1105
- }
1106
- }
1107
-
1108
- const children = markParts.join("");
1109
- return children.length > 0 ? `<w:rPr>${children}</w:rPr>` : "";
1110
- }
1111
-
1112
- function requiresPreservedSpace(text: string): boolean {
1113
- return /^\s/.test(text) || /\s$/.test(text) || text.includes(" ");
1114
- }
1115
-
1116
- function lookupOpaqueXml(fragmentId: string, state: SerializationState): string {
1117
- const fragment = getOpaqueFragment(state.preservation, fragmentId);
1118
- if (!fragment || fragment.payloadKind !== "xml-subtree") {
1119
- throw new Error(`Missing preserved OOXML fragment ${fragmentId} during serialization.`);
1120
- }
1121
-
1122
- retainRelationshipsForFragment(
1123
- fragment,
1124
- state.relationships,
1125
- state.existingRelationshipMap,
1126
- state.retainedRelationshipIds,
1127
- );
1128
- return fragment.payloadReference;
1129
- }
1130
-
1131
- function cloneRelationship(relationship: OpcRelationship): OpcRelationship {
1132
- return { ...relationship };
1133
- }
1134
-
1135
- function looksLikeSectionPropertiesXml(xml: string): boolean {
1136
- return /^<[^>]*:?sectPr(?:\s|>|\/)/.test(xml.trim());
1137
- }
1138
-
1139
- function escapeXml(value: string): string {
1140
- return value
1141
- .replace(/&/g, "&amp;")
1142
- .replace(/</g, "&lt;")
1143
- .replace(/>/g, "&gt;");
1144
- }
1145
-
1146
- function escapeAttribute(value: string): string {
1147
- return escapeXml(value).replace(/"/g, "&quot;");
1148
- }
1149
-
1150
- function offsetParagraphBoundary(
1151
- boundary: RevisionParagraphBoundary,
1152
- offset: number,
1153
- ): RevisionParagraphBoundary {
1154
- return {
1155
- ...boundary,
1156
- boundaries: new Map(
1157
- [...boundary.boundaries.entries()].map(([position, index]) => [
1158
- position,
1159
- index + offset,
1160
- ]),
1161
- ),
1162
- paragraphStart: boundary.paragraphStart + offset,
1163
- paragraphStartTagEnd: boundary.paragraphStartTagEnd + offset,
1164
- paragraphEndTagStart: boundary.paragraphEndTagStart + offset,
1165
- paragraphEnd: boundary.paragraphEnd + offset,
1166
- ...(boundary.paragraphPropertiesStart !== undefined
1167
- ? { paragraphPropertiesStart: boundary.paragraphPropertiesStart + offset }
1168
- : {}),
1169
- ...(boundary.paragraphPropertiesEnd !== undefined
1170
- ? { paragraphPropertiesEnd: boundary.paragraphPropertiesEnd + offset }
1171
- : {}),
1172
- ...(boundary.paragraphRunPropertiesStart !== undefined
1173
- ? { paragraphRunPropertiesStart: boundary.paragraphRunPropertiesStart + offset }
1174
- : {}),
1175
- ...(boundary.paragraphRunPropertiesEnd !== undefined
1176
- ? { paragraphRunPropertiesEnd: boundary.paragraphRunPropertiesEnd + offset }
1177
- : {}),
1178
- };
1179
- }
1180
-
1181
- function serializeDocumentAttributes(
1182
- attributes: Record<string, string> | undefined,
1183
- content?: DocumentRootNode,
1184
- ): string {
1185
- const merged = {
1186
- "xmlns:r": "http://schemas.openxmlformats.org/officeDocument/2006/relationships",
1187
- "xmlns:w": "http://schemas.openxmlformats.org/wordprocessingml/2006/main",
1188
- ...(content && documentNeedsW14Namespace(content)
1189
- ? { "xmlns:w14": "http://schemas.microsoft.com/office/word/2010/wordml" }
1190
- : {}),
1191
- ...(attributes ?? {}),
1192
- };
1193
-
1194
- return Object.entries(merged)
1195
- .map(([name, value]) => ` ${name}="${escapeAttribute(value)}"`)
1196
- .join("");
1197
- }
1198
-
1199
- export function serializeSectionPropertiesXml(props: SectionProperties): string {
1200
- const children: string[] = [];
1201
-
1202
- // Header references
1203
- if (props.headerReferences) {
1204
- for (const ref of props.headerReferences) {
1205
- children.push(`<w:headerReference w:type="${escapeAttribute(ref.variant)}" r:id="${escapeAttribute(ref.relationshipId)}"/>`);
1206
- }
1207
- }
1208
-
1209
- // Footer references
1210
- if (props.footerReferences) {
1211
- for (const ref of props.footerReferences) {
1212
- children.push(`<w:footerReference w:type="${escapeAttribute(ref.variant)}" r:id="${escapeAttribute(ref.relationshipId)}"/>`);
1213
- }
1214
- }
1215
-
1216
- // Section type
1217
- if (props.sectionType) {
1218
- children.push(`<w:type w:val="${escapeAttribute(props.sectionType)}"/>`);
1219
- }
1220
-
1221
- // Page size
1222
- if (props.pageSize) {
1223
- let pgSz = `<w:pgSz w:w="${props.pageSize.width}" w:h="${props.pageSize.height}"`;
1224
- if (props.pageSize.orientation) {
1225
- pgSz += ` w:orient="${props.pageSize.orientation}"`;
1226
- }
1227
- pgSz += "/>";
1228
- children.push(pgSz);
1229
- }
1230
-
1231
- // Page margins
1232
- if (props.pageMargins) {
1233
- let pgMar = `<w:pgMar w:top="${props.pageMargins.top}" w:right="${props.pageMargins.right}" w:bottom="${props.pageMargins.bottom}" w:left="${props.pageMargins.left}"`;
1234
- if (props.pageMargins.header !== undefined) pgMar += ` w:header="${props.pageMargins.header}"`;
1235
- if (props.pageMargins.footer !== undefined) pgMar += ` w:footer="${props.pageMargins.footer}"`;
1236
- if (props.pageMargins.gutter !== undefined) pgMar += ` w:gutter="${props.pageMargins.gutter}"`;
1237
- pgMar += "/>";
1238
- children.push(pgMar);
1239
- }
1240
-
1241
- // Columns
1242
- if (props.columns) {
1243
- let cols = "<w:cols";
1244
- if (props.columns.count !== undefined) cols += ` w:num="${props.columns.count}"`;
1245
- if (props.columns.space !== undefined) cols += ` w:space="${props.columns.space}"`;
1246
- if (props.columns.equalWidth !== undefined) cols += ` w:equalWidth="${props.columns.equalWidth ? "1" : "0"}"`;
1247
- if (props.columns.separator) cols += ` w:sep="1"`;
1248
- if (props.columns.columns && props.columns.columns.length > 0) {
1249
- cols += ">";
1250
- for (const col of props.columns.columns) {
1251
- let colXml = `<w:col w:w="${col.width}"`;
1252
- if (col.space !== undefined) colXml += ` w:space="${col.space}"`;
1253
- colXml += "/>";
1254
- cols += colXml;
1255
- }
1256
- cols += "</w:cols>";
1257
- } else {
1258
- cols += "/>";
1259
- }
1260
- children.push(cols);
1261
- }
1262
-
1263
- // Page numbering
1264
- if (props.pageNumbering) {
1265
- let pgNum = "<w:pgNumType";
1266
- if (props.pageNumbering.format) pgNum += ` w:fmt="${escapeAttribute(props.pageNumbering.format)}"`;
1267
- if (props.pageNumbering.start !== undefined) pgNum += ` w:start="${props.pageNumbering.start}"`;
1268
- if (props.pageNumbering.chapStyle) pgNum += ` w:chapStyle="${escapeAttribute(props.pageNumbering.chapStyle)}"`;
1269
- if (props.pageNumbering.chapSep) pgNum += ` w:chapSep="${escapeAttribute(props.pageNumbering.chapSep)}"`;
1270
- pgNum += "/>";
1271
- children.push(pgNum);
1272
- }
1273
-
1274
- if (props.lineNumbering) {
1275
- let lineNumbering = "<w:lnNumType";
1276
- if (props.lineNumbering.countBy !== undefined) {
1277
- lineNumbering += ` w:countBy="${props.lineNumbering.countBy}"`;
1278
- }
1279
- if (props.lineNumbering.start !== undefined) {
1280
- lineNumbering += ` w:start="${props.lineNumbering.start}"`;
1281
- }
1282
- if (props.lineNumbering.distance !== undefined) {
1283
- lineNumbering += ` w:distance="${props.lineNumbering.distance}"`;
1284
- }
1285
- if (props.lineNumbering.restart) {
1286
- lineNumbering += ` w:restart="${escapeAttribute(props.lineNumbering.restart)}"`;
1287
- }
1288
- lineNumbering += "/>";
1289
- children.push(lineNumbering);
1290
- }
1291
-
1292
- if (props.pageBorders) {
1293
- const attrs: string[] = [];
1294
- if (props.pageBorders.offsetFrom) {
1295
- attrs.push(`w:offsetFrom="${escapeAttribute(props.pageBorders.offsetFrom)}"`);
1296
- }
1297
- if (props.pageBorders.display) {
1298
- attrs.push(`w:display="${escapeAttribute(props.pageBorders.display)}"`);
1299
- }
1300
- if (props.pageBorders.zOrder) {
1301
- attrs.push(`w:zOrder="${escapeAttribute(props.pageBorders.zOrder)}"`);
1302
- }
1303
- const borderXml = [
1304
- serializeBorder("top", props.pageBorders.top),
1305
- serializeBorder("left", props.pageBorders.left),
1306
- serializeBorder("bottom", props.pageBorders.bottom),
1307
- serializeBorder("right", props.pageBorders.right),
1308
- ].filter((entry) => entry.length > 0);
1309
- if (attrs.length > 0 || borderXml.length > 0) {
1310
- children.push(
1311
- `<w:pgBorders${attrs.length > 0 ? ` ${attrs.join(" ")}` : ""}>${borderXml.join("")}</w:pgBorders>`,
1312
- );
1313
- }
1314
- }
1315
-
1316
- // Title page
1317
- if (props.titlePage) {
1318
- children.push("<w:titlePg/>");
1319
- }
1320
-
1321
- if (props.documentGrid) {
1322
- const attrs: string[] = [];
1323
- if (props.documentGrid.type) {
1324
- attrs.push(`w:type="${escapeAttribute(props.documentGrid.type)}"`);
1325
- }
1326
- if (props.documentGrid.linePitch !== undefined) {
1327
- attrs.push(`w:linePitch="${props.documentGrid.linePitch}"`);
1328
- }
1329
- if (props.documentGrid.charSpace !== undefined) {
1330
- attrs.push(`w:charSpace="${props.documentGrid.charSpace}"`);
1331
- }
1332
- if (attrs.length > 0) {
1333
- children.push(`<w:docGrid ${attrs.join(" ")}/>`);
1334
- }
1335
- }
1336
-
1337
- if (children.length === 0) {
1338
- return "<w:sectPr/>";
1339
- }
1340
-
1341
- return `<w:sectPr>${children.join("")}</w:sectPr>`;
1342
- }
1343
-
1344
- function wrapInlineRawXml(rawXml: string): string {
1345
- const trimmed = rawXml.trimStart();
1346
- return trimmed.startsWith("<w:r") ? rawXml : `<w:r>${rawXml}</w:r>`;
1347
- }
1348
-
1349
- function documentNeedsW14Namespace(content: DocumentRootNode): boolean {
1350
- const blockQueue = [...content.children];
1351
- while (blockQueue.length > 0) {
1352
- const block = blockQueue.shift();
1353
- if (!block) {
1354
- continue;
1355
- }
1356
- if (block.type === "paragraph") {
1357
- for (const child of block.children) {
1358
- if (
1359
- (child.type === "text" || child.type === "symbol") &&
1360
- child.marks?.some((mark) => mark.type === "textFill")
1361
- ) {
1362
- return true;
1363
- }
1364
- }
1365
- continue;
1366
- }
1367
- if (block.type === "table") {
1368
- for (const row of block.rows) {
1369
- for (const cell of row.cells) {
1370
- blockQueue.push(...cell.children);
1371
- }
1372
- }
1373
- }
1374
- }
1375
- return false;
1376
- }