@stll/folio-core 0.72.0 → 0.74.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 (244) hide show
  1. package/README.md +12 -6
  2. package/dist/ai-edits/applyTypes.d.ts +85 -0
  3. package/dist/ai-edits/applyTypes.js +24 -0
  4. package/dist/ai-edits/clean-text.d.ts +7 -1
  5. package/dist/ai-edits/clean-text.js +23 -2
  6. package/dist/ai-edits/finalSectionReview.d.ts +1 -1
  7. package/dist/ai-edits/headless.d.ts +29 -147
  8. package/dist/ai-edits/headless.js +215 -1639
  9. package/dist/ai-edits/index.d.ts +3 -3
  10. package/dist/ai-edits/index.js +2 -3
  11. package/dist/ai-edits/pending-suggestions.d.ts +14 -4
  12. package/dist/ai-edits/pending-suggestions.js +183 -90
  13. package/dist/ai-edits/pendingJoin.js +6 -1
  14. package/dist/ai-edits/retired-comment-receipts.d.ts +1 -1
  15. package/dist/ai-edits/snapshot.js +9 -4
  16. package/dist/ai-edits/table-cell-mutations.d.ts +1 -1
  17. package/dist/ai-edits/table-cell-mutations.js +2 -2
  18. package/dist/ai-edits/table-geometry.d.ts +1 -1
  19. package/dist/ai-edits/table-geometry.js +1 -1
  20. package/dist/ai-edits/table-row-column-mutations.d.ts +1 -1
  21. package/dist/ai-edits/table-row-column-mutations.js +1 -1
  22. package/dist/ai-edits/tableNodes.d.ts +38 -0
  23. package/dist/ai-edits/tableNodes.js +86 -0
  24. package/dist/ai-edits/tracked-batch-fidelity.d.ts +1 -1
  25. package/dist/ai-edits/tracked-batch-fidelity.js +1 -1
  26. package/dist/ai-edits/undefinedReferences.d.ts +7 -0
  27. package/dist/canonical/batchCommentReferences.js +1 -0
  28. package/dist/canonical/canonicalInlineGaps.d.ts +56 -0
  29. package/dist/canonical/canonicalInlineGaps.js +118 -0
  30. package/dist/canonical/canonicalInlineProjection.d.ts +4 -13
  31. package/dist/canonical/canonicalInlineProjection.js +5 -27
  32. package/dist/canonical/canonicalProjectionBlocks.d.ts +4 -4
  33. package/dist/canonical/canonicalPublicOperations.js +282 -75
  34. package/dist/canonical/canonicalSelection.js +1 -1
  35. package/dist/canonical/canonicalSession.d.ts +9 -1
  36. package/dist/canonical/canonicalSession.js +106 -33
  37. package/dist/compare/compare.d.ts +1 -1
  38. package/dist/compare/inline-atoms-model.d.ts +1 -1
  39. package/dist/compare/inline-atoms.d.ts +2 -2
  40. package/dist/compare/inline-provenance-model.d.ts +1 -1
  41. package/dist/compare/inline-provenance.d.ts +2 -2
  42. package/dist/compat/eigenpal.d.ts +3 -3
  43. package/dist/compat/eigenpal.js +2 -3
  44. package/dist/controller/autocompleteSuggestion.js +1 -1
  45. package/dist/controller/canonicalClipboard.d.ts +13 -3
  46. package/dist/controller/canonicalClipboard.js +186 -50
  47. package/dist/controller/canonicalClipboardResources.d.ts +9 -1
  48. package/dist/controller/canonicalClipboardResources.js +22 -2
  49. package/dist/controller/canonicalComposition.js +1 -1
  50. package/dist/controller/canonicalInput.js +1 -1
  51. package/dist/controller/canonicalPresence.js +1 -1
  52. package/dist/controller/canonicalReview.d.ts +1 -1
  53. package/dist/controller/canonicalReview.js +1 -1
  54. package/dist/controller/canonicalRoomAuthority.js +1 -1
  55. package/dist/controller/canonicalRoomWire.js +1 -1
  56. package/dist/controller/canonicalStructure.js +1 -1
  57. package/dist/controller/canonicalTabRoom.js +1 -1
  58. package/dist/controller/canonicalWatermark.d.ts +10 -0
  59. package/dist/controller/canonicalWatermark.js +37 -0
  60. package/dist/controller/canonicalYjsRoom.js +1 -1
  61. package/dist/controller/hiddenEditorApi.d.ts +1 -1
  62. package/dist/controller/hiddenEditorApi.js +2 -17
  63. package/dist/controller/hiddenEditorManager.d.ts +4 -3
  64. package/dist/controller/hiddenEditorManager.js +79 -23
  65. package/dist/document-operations.d.ts +1 -14
  66. package/dist/document-operations.js +1 -278
  67. package/dist/docx/canonicalSave.d.ts +1 -1
  68. package/dist/docx/canonicalSave.js +1 -1
  69. package/dist/docx/canonicalSessionInput.js +1 -1
  70. package/dist/docx/containerChildren.js +1 -1
  71. package/dist/docx/createDocx.d.ts +4 -0
  72. package/dist/docx/createDocx.js +6 -0
  73. package/dist/docx/paragraphParser.js +2 -2
  74. package/dist/docx/paragraphPropertySource.d.ts +1 -1
  75. package/dist/docx/parser.js +1 -1
  76. package/dist/docx/reviewResolutionProvenance.d.ts +3 -2
  77. package/dist/docx/reviewResolutionProvenance.js +31 -8
  78. package/dist/docx/revisionIdNormalization.js +30 -7
  79. package/dist/docx/rezip.d.ts +1 -1
  80. package/dist/docx/rezip.js +1 -1
  81. package/dist/docx/serializer/tableSerializer.js +4 -36
  82. package/dist/docx/server/templatePlan/apply.d.ts +15 -0
  83. package/dist/docx/server/templatePlan/apply.js +658 -0
  84. package/dist/docx/server/templatePlan/blocks.d.ts +97 -0
  85. package/dist/docx/server/templatePlan/blocks.js +355 -0
  86. package/dist/docx/server/templatePlan/builders.d.ts +72 -0
  87. package/dist/docx/server/templatePlan/builders.js +237 -0
  88. package/dist/docx/server/templatePlan/emit.d.ts +118 -0
  89. package/dist/docx/server/templatePlan/emit.js +466 -0
  90. package/dist/docx/server/templatePlan/emphasis.d.ts +62 -0
  91. package/dist/docx/server/templatePlan/emphasis.js +173 -0
  92. package/dist/docx/server/templatePlan/inspect.d.ts +22 -0
  93. package/dist/docx/server/templatePlan/inspect.js +230 -0
  94. package/dist/docx/server/templatePlan/labels.d.ts +45 -0
  95. package/dist/docx/server/templatePlan/labels.js +155 -0
  96. package/dist/docx/server/templatePlan/package.d.ts +34 -0
  97. package/dist/docx/server/templatePlan/package.js +99 -0
  98. package/dist/docx/server/templatePlan/plan.d.ts +145 -0
  99. package/dist/docx/server/templatePlan/plan.js +875 -0
  100. package/dist/docx/server/templatePlan/planShape.d.ts +9 -0
  101. package/dist/docx/server/templatePlan/planShape.js +180 -0
  102. package/dist/docx/server/templatePlan/resources.d.ts +41 -0
  103. package/dist/docx/server/templatePlan/resources.js +136 -0
  104. package/dist/docx/server/templatePlan/story.d.ts +128 -0
  105. package/dist/docx/server/templatePlan/story.js +440 -0
  106. package/dist/docx/server/templatePlan/types.d.ts +442 -0
  107. package/dist/docx/server/templatePlan/types.js +29 -0
  108. package/dist/docx/server/templatePlan/verify.d.ts +40 -0
  109. package/dist/docx/server/templatePlan/verify.js +976 -0
  110. package/dist/docx/server/templatePlan/xml.d.ts +74 -0
  111. package/dist/docx/server/templatePlan/xml.js +125 -0
  112. package/dist/docx/settingsHeaderFooterUpdate.d.ts +9 -0
  113. package/dist/docx/settingsHeaderFooterUpdate.js +45 -10
  114. package/dist/docx/structuralXmlPatch.js +9 -1
  115. package/dist/docx/tableGridXml.d.ts +30 -0
  116. package/dist/docx/tableGridXml.js +54 -0
  117. package/dist/docx/tableParser.js +7 -0
  118. package/dist/i18n/loadMessages.d.ts +5 -0
  119. package/dist/i18n/loadMessages.js +46 -0
  120. package/dist/i18n/locales.d.ts +3 -0
  121. package/dist/i18n/locales.js +17 -0
  122. package/dist/i18n/messages.d.ts +3 -2
  123. package/dist/i18n/messages.js +4 -16
  124. package/dist/index.d.ts +3 -3
  125. package/dist/index.js +2 -3
  126. package/dist/internal/clipboardTransformPlugin.d.ts +3 -0
  127. package/dist/internal/clipboardTransformPlugin.js +13 -0
  128. package/dist/internal/extensionRuntime.d.ts +7 -0
  129. package/dist/internal/extensionRuntime.js +28 -0
  130. package/dist/internal/starterKitExtensions.d.ts +20 -0
  131. package/dist/internal/starterKitExtensions.js +123 -0
  132. package/dist/internal/wholeStoryRevisionResolution.js +1 -1
  133. package/dist/managers/ContextMenuManager.js +2 -1
  134. package/dist/managers/editorShortcuts.d.ts +1 -1
  135. package/dist/prosemirror/anchoredTextBoxes.d.ts +1 -1
  136. package/dist/prosemirror/attrs/index.js +1 -1
  137. package/dist/prosemirror/canonicalCommands.d.ts +1 -1
  138. package/dist/prosemirror/canonicalSelectionRange.d.ts +1 -1
  139. package/dist/prosemirror/canonicalSelectionRange.js +1 -1
  140. package/dist/prosemirror/canonicalTableCommands.js +3 -3
  141. package/dist/prosemirror/commands/comments.js +4 -4
  142. package/dist/prosemirror/commands/contentControls.d.ts +1 -1
  143. package/dist/prosemirror/commands/contentControls.js +1 -1
  144. package/dist/prosemirror/commands/formatPainter.d.ts +1 -1
  145. package/dist/prosemirror/commands/formatting.d.ts +1 -1
  146. package/dist/prosemirror/commands/pastePlainText.d.ts +1 -1
  147. package/dist/prosemirror/commands/pastePlainText.js +2 -3
  148. package/dist/prosemirror/commands/replaceRangeText.js +1 -1
  149. package/dist/prosemirror/commands/sectionBreak.js +1 -1
  150. package/dist/prosemirror/commands/tableCellMergeResolution.d.ts +1 -1
  151. package/dist/prosemirror/commands/tableMergeFoldDecisions.d.ts +1 -1
  152. package/dist/prosemirror/commentRecords.d.ts +1 -1
  153. package/dist/prosemirror/commentRecords.js +2 -2
  154. package/dist/prosemirror/contentControlRevisions.d.ts +1 -1
  155. package/dist/prosemirror/conversion/fromProseDoc.d.ts +0 -5
  156. package/dist/prosemirror/conversion/fromProseDoc.js +2 -9
  157. package/dist/prosemirror/conversion/index.d.ts +2 -2
  158. package/dist/prosemirror/conversion/index.js +2 -2
  159. package/dist/prosemirror/documentStructureCounts.d.ts +24 -0
  160. package/dist/prosemirror/documentStructureCounts.js +76 -0
  161. package/dist/prosemirror/extensions/ExtensionManager.d.ts +1 -1
  162. package/dist/prosemirror/extensions/ExtensionManager.js +4 -22
  163. package/dist/prosemirror/extensions/StarterKit.d.ts +4 -27
  164. package/dist/prosemirror/extensions/StarterKit.js +29 -115
  165. package/dist/prosemirror/extensions/core/ParagraphExtension.js +1 -1
  166. package/dist/prosemirror/extensions/core/historyCommands.d.ts +16 -0
  167. package/dist/prosemirror/extensions/core/historyCommands.js +19 -0
  168. package/dist/prosemirror/extensions/features/AutoBidiDetectionExtension.js +1 -1
  169. package/dist/prosemirror/extensions/features/BaseKeymapExtension.js +1 -1
  170. package/dist/prosemirror/extensions/features/ListExtension.js +7 -2
  171. package/dist/prosemirror/extensions/features/pastedCellSections.d.ts +1 -1
  172. package/dist/prosemirror/extensions/index.d.ts +2 -1
  173. package/dist/prosemirror/extensions/marks/FootnoteRefExtension.js +8 -3
  174. package/dist/prosemirror/extensions/marks/markUtils.d.ts +1 -1
  175. package/dist/prosemirror/extensions/nodes/TableExtension.d.ts +1 -1
  176. package/dist/prosemirror/extensions/nodes/TableExtension.js +8 -41
  177. package/dist/prosemirror/extensions/types.d.ts +1 -1
  178. package/dist/prosemirror/findReplaceSelection.d.ts +1 -1
  179. package/dist/prosemirror/hyperlinkRemoval.d.ts +1 -1
  180. package/dist/prosemirror/hyperlinkRemoval.js +7 -2
  181. package/dist/prosemirror/index.d.ts +3 -3
  182. package/dist/prosemirror/index.js +3 -3
  183. package/dist/prosemirror/insertionGaps.d.ts +1 -1
  184. package/dist/prosemirror/keptRangeBoundaries.d.ts +1 -1
  185. package/dist/prosemirror/keptRangeBoundaries.js +1 -1
  186. package/dist/prosemirror/listNumbering.d.ts +1 -1
  187. package/dist/prosemirror/listNumbering.js +1 -1
  188. package/dist/prosemirror/lockedControls.d.ts +1 -1
  189. package/dist/prosemirror/markupViewProjection.d.ts +1 -1
  190. package/dist/prosemirror/noteReferenceOccurrences.d.ts +1 -1
  191. package/dist/prosemirror/noteReferenceOccurrences.js +1 -1
  192. package/dist/prosemirror/noteReferenceReview.d.ts +2 -56
  193. package/dist/prosemirror/noteReferenceReview.js +2 -243
  194. package/dist/prosemirror/{extensions/features/ParaIdAllocatorExtension.d.ts → paraIds.d.ts} +13 -23
  195. package/dist/prosemirror/paraIds.js +152 -0
  196. package/dist/prosemirror/paragraphChangeTrackerMeta.d.ts +97 -0
  197. package/dist/prosemirror/paragraphChangeTrackerMeta.js +87 -0
  198. package/dist/prosemirror/paragraphMarkJoin.d.ts +1 -1
  199. package/dist/prosemirror/paragraphPropertyCarry.d.ts +1 -1
  200. package/dist/prosemirror/pastedCommentCopies.d.ts +1 -1
  201. package/dist/prosemirror/plugins/createDocScanPlugin.d.ts +1 -1
  202. package/dist/prosemirror/plugins/documentNumbering.d.ts +1 -1
  203. package/dist/prosemirror/plugins/documentNumbering.js +1 -1
  204. package/dist/prosemirror/plugins/documentStyles.js +1 -1
  205. package/dist/prosemirror/plugins/templateDirectives.d.ts +1 -1
  206. package/dist/prosemirror/rebaseParagraphRunFormatting.d.ts +1 -1
  207. package/dist/prosemirror/rebaseParagraphRuns.d.ts +1 -1
  208. package/dist/prosemirror/rejoinRunCarriers.d.ts +1 -1
  209. package/dist/prosemirror/replacedAnnotations.d.ts +1 -1
  210. package/dist/prosemirror/replacedAnnotations.js +17 -2
  211. package/dist/prosemirror/revisionCarriers.d.ts +1 -1
  212. package/dist/prosemirror/runFormattingInlineCarriers.d.ts +1 -1
  213. package/dist/prosemirror/sectionMarkEdits.d.ts +1 -1
  214. package/dist/prosemirror/selectionBounds.d.ts +10 -0
  215. package/dist/prosemirror/selectionBounds.js +22 -0
  216. package/dist/prosemirror/storyListNumbering.d.ts +1 -1
  217. package/dist/prosemirror/suggestionCommands.d.ts +47 -0
  218. package/dist/prosemirror/suggestionCommands.js +31 -0
  219. package/dist/prosemirror/tableCellPaste.d.ts +1 -1
  220. package/dist/prosemirror/tableCellPaste.js +1 -1
  221. package/dist/prosemirror/tableGridMutation.d.ts +1 -1
  222. package/dist/prosemirror/tableRunIn.js +1 -1
  223. package/dist/prosemirror/textInput.d.ts +1 -1
  224. package/dist/prosemirror/textInput.js +1 -1
  225. package/dist/server.d.ts +7 -2
  226. package/dist/server.js +6 -1
  227. package/dist/types/canonicalCapabilities.d.ts +13 -92
  228. package/dist/types/canonicalCapabilities.js +11 -85
  229. package/package.json +4 -4
  230. package/dist/ai-edits/apply.d.ts +0 -202
  231. package/dist/ai-edits/apply.js +0 -6759
  232. package/dist/ai-edits/note-story-inventory.d.ts +0 -5
  233. package/dist/ai-edits/note-story-inventory.js +0 -10
  234. package/dist/ai-edits/revisionStretches.d.ts +0 -32
  235. package/dist/ai-edits/revisionStretches.js +0 -147
  236. package/dist/prosemirror/extensions/core/HistoryExtension.d.ts +0 -18
  237. package/dist/prosemirror/extensions/core/HistoryExtension.js +0 -122
  238. package/dist/prosemirror/extensions/features/ParaIdAllocatorExtension.js +0 -408
  239. package/dist/prosemirror/extensions/features/ParagraphChangeTrackerExtension.d.ts +0 -135
  240. package/dist/prosemirror/extensions/features/ParagraphChangeTrackerExtension.js +0 -566
  241. package/dist/prosemirror/plugins/suggestionMode.d.ts +0 -92
  242. package/dist/prosemirror/plugins/suggestionMode.js +0 -1515
  243. package/dist/prosemirror/tablePositionedBookmarks.d.ts +0 -8
  244. package/dist/prosemirror/tablePositionedBookmarks.js +0 -80
@@ -0,0 +1,237 @@
1
+ import { spliceXml } from "../../selectiveXmlPatch.js";
2
+ import { updateSettingsOnOffChild } from "../../settingsHeaderFooterUpdate.js";
3
+ import { sha256Hex } from "./package.js";
4
+ import { elementChildren, escapeXmlAttribute, escapeXmlText, innerRangeOf, isW, rangeOf, readRawPart, wAttr, wChild, wName, walkElements } from "./xml.js";
5
+ // src/docx/server/templatePlan/builders.ts
6
+ /**
7
+ * Raw OOXML builders a template plan is applied with: a hidden bookmark around
8
+ * a paragraph's content, a complex REF field with a cached result, the
9
+ * update-on-open setting, and freezing a field to its cached result.
10
+ */
11
+ const RELATIONSHIPS_NAMESPACE = "http://schemas.openxmlformats.org/officeDocument/2006/relationships";
12
+ const REF_BOOKMARK_PREFIX = "_Ref";
13
+ const REF_BOOKMARK_DIGITS = 9;
14
+ const REF_BOOKMARK_SPACE = 10 ** REF_BOOKMARK_DIGITS;
15
+ /**
16
+ * A hidden bookmark name in the form a cross-reference target takes: `_Ref`
17
+ * and nine digits, derived from `seed` and stepped past the names in `taken`.
18
+ * The name is added to `taken`.
19
+ */
20
+ const mintRefBookmarkName = (seed, taken) => {
21
+ let value = Number.parseInt(sha256Hex(seed).slice(0, 12), 16) % REF_BOOKMARK_SPACE;
22
+ for (;;) {
23
+ const name = `${REF_BOOKMARK_PREFIX}${String(value).padStart(REF_BOOKMARK_DIGITS, "0")}`;
24
+ if (!taken.has(name)) {
25
+ taken.add(name);
26
+ return name;
27
+ }
28
+ value = (value + 1) % REF_BOOKMARK_SPACE;
29
+ }
30
+ };
31
+ /** The `w:id` values `w:bookmarkStart` elements under `root` already use. */
32
+ const bookmarkIdsOf = (root) => {
33
+ const ids = [];
34
+ walkElements(root, (element) => {
35
+ if (!isW(element, "bookmarkStart") && !isW(element, "bookmarkEnd")) return;
36
+ const id = Number.parseInt(wAttr(element, "id") ?? "", 10);
37
+ if (Number.isInteger(id)) ids.push(id);
38
+ });
39
+ return ids;
40
+ };
41
+ const bookmarkStartXml = ({ prefix, id, name }) => `<${prefix}:bookmarkStart ${prefix}:id="${id}" ${prefix}:name="${escapeXmlAttribute(name)}"/>`;
42
+ const bookmarkEndXml = ({ prefix, id }) => `<${prefix}:bookmarkEnd ${prefix}:id="${id}"/>`;
43
+ /**
44
+ * `paragraphXml` with its content, everything after `w:pPr`, inside a
45
+ * bookmark. Null when the markup is not one paragraph.
46
+ */
47
+ const wrapParagraphContentInBookmark = ({ paragraphXml, prefixes, id, name }) => {
48
+ const declared = `<wrap xmlns:${prefixes.w}="${prefixes.wUri}" xmlns:${prefixes.r}="${RELATIONSHIPS_NAMESPACE}">${paragraphXml}</wrap>`;
49
+ const offset = declared.indexOf(">") + 1;
50
+ const part = readRawPart("paragraph", declared);
51
+ const paragraph = part === null ? void 0 : elementChildren(part.root).at(0);
52
+ if (part === null || paragraph === void 0 || !isW(paragraph, "p") || elementChildren(part.root).length !== 1) return null;
53
+ const start = bookmarkStartXml({
54
+ prefix: prefixes.w,
55
+ id,
56
+ name
57
+ });
58
+ const end = bookmarkEndXml({
59
+ prefix: prefixes.w,
60
+ id
61
+ });
62
+ const inner = innerRangeOf(declared, paragraph);
63
+ if (inner === null) {
64
+ const name_ = paragraph.name ?? `${prefixes.w}:p`;
65
+ return `${paragraphXml.slice(0, paragraphXml.lastIndexOf("/>"))}>${start}${end}</${name_}>`;
66
+ }
67
+ const properties = wChild(paragraph, "pPr");
68
+ const startAt = ((properties === null ? null : rangeOf(properties)?.end ?? null) ?? inner.start) - offset;
69
+ const endAt = inner.end - offset;
70
+ return spliceXml(paragraphXml, startAt === endAt ? [{
71
+ start: startAt,
72
+ end: startAt,
73
+ newXml: `${start}${end}`
74
+ }] : [{
75
+ start: startAt,
76
+ end: startAt,
77
+ newXml: start
78
+ }, {
79
+ start: endAt,
80
+ end: endAt,
81
+ newXml: end
82
+ }]);
83
+ };
84
+ /** A complex field: begin, instruction, separate, the cached result, end. */
85
+ const complexFieldXml = ({ prefix, instruction, resultRunsXml, runPropertiesXml }) => {
86
+ const char = (type) => `<${prefix}:r>${runPropertiesXml}<${prefix}:fldChar ${prefix}:fldCharType="${type}"/></${prefix}:r>`;
87
+ return `${char("begin")}<${prefix}:r>${runPropertiesXml}<${prefix}:instrText xml:space="preserve">${escapeXmlText(instruction)}</${prefix}:instrText></${prefix}:r>${char("separate")}${resultRunsXml}${char("end")}`;
88
+ };
89
+ /** The REF switches that take no argument. */
90
+ const REF_SWITCH = /^\\[fhnprtw]$/u;
91
+ /**
92
+ * The field code of a REF to `bookmark` with `switches` (`\n \t \h`). Null
93
+ * when the switches hold anything but single-letter switches.
94
+ */
95
+ const refFieldInstruction = (bookmark, switches) => {
96
+ const parts = switches.trim().split(/\s+/u).filter(Boolean);
97
+ return parts.every((part) => REF_SWITCH.test(part)) ? ` REF ${bookmark} ${parts.join(" ")}${parts.length === 0 ? "" : " "}` : null;
98
+ };
99
+ /** The bookmark a REF, PAGEREF or NOTEREF instruction names. */
100
+ const fieldBookmarkOf = (instruction) => /^\s*(?:REF|PAGEREF|NOTEREF)\s+"?(?<name>[^\s"\\]+)/iu.exec(instruction)?.groups?.["name"] ?? null;
101
+ /**
102
+ * `settingsXml` with `w:updateFields` set (`true`) or absent (`false`), at its
103
+ * place in the schema's child order. Null when the part is not a settings part.
104
+ */
105
+ const setUpdateFieldsOnOpen = (settingsXml, enabled) => updateSettingsOnOffChild(settingsXml, "updateFields", enabled ? true : void 0);
106
+ const fieldKindOfInstruction = (instruction) => /^\s*\\?(?<name>[A-Za-z][A-Za-z0-9]*)/u.exec(instruction)?.groups?.["name"]?.toUpperCase() ?? "";
107
+ const textOf = (element) => (element.elements ?? []).map((child) => child.type === "text" ? String(child.text ?? "") : "").join("");
108
+ /** Drop splices that lie inside an earlier one, so none overlap. */
109
+ const withoutContained = (splices) => {
110
+ const kept = [];
111
+ let coveredUntil = -1;
112
+ for (const splice of splices.toSorted((left, right) => left.start - right.start || right.end - left.end)) {
113
+ if (splice.start < coveredUntil) continue;
114
+ kept.push(splice);
115
+ coveredUntil = Math.max(coveredUntil, splice.end);
116
+ }
117
+ return kept;
118
+ };
119
+ /**
120
+ * Replace every field of the given kinds under `part` by its cached result:
121
+ * a simple field keeps its runs, a complex field loses its field characters
122
+ * and its code. A field nested in a kept result is judged on its own; one
123
+ * nested in another field's code is part of that code.
124
+ */
125
+ const freezeFields = (part, kinds) => {
126
+ const splices = [];
127
+ const counts = /* @__PURE__ */ new Map();
128
+ const count = (instruction) => {
129
+ const kind = fieldKindOfInstruction(instruction);
130
+ counts.set(kind, (counts.get(kind) ?? 0) + 1);
131
+ };
132
+ const remove = (element) => {
133
+ const range = rangeOf(element);
134
+ if (range !== null) splices.push({
135
+ ...range,
136
+ newXml: ""
137
+ });
138
+ };
139
+ const open = [];
140
+ const wanted = (instruction) => kinds.has(fieldKindOfInstruction(instruction));
141
+ /** The outermost open field whose code the walk is inside. */
142
+ const codeOwner = (fields = open) => fields.find(({ phase }) => phase === "code");
143
+ const freeze = (field, closing) => {
144
+ field.frozen = true;
145
+ count(field.instruction);
146
+ for (const pending of field.pending) remove(pending);
147
+ remove(closing);
148
+ };
149
+ const visitRunChild = (child) => {
150
+ if (wName(child) !== "fldChar") {
151
+ const top = open.at(-1);
152
+ if (wName(child) === "instrText" && top?.phase === "code") top.instruction += textOf(child);
153
+ const owner = codeOwner();
154
+ if (owner === void 0) visit(child);
155
+ else owner.pending.push(child);
156
+ return;
157
+ }
158
+ const type = wAttr(child, "fldCharType");
159
+ if (type === "begin") {
160
+ const owner = codeOwner();
161
+ const field = {
162
+ pending: [],
163
+ instruction: "",
164
+ phase: "code",
165
+ candidate: owner === void 0,
166
+ frozen: false
167
+ };
168
+ (owner ?? field).pending.push(child);
169
+ open.push(field);
170
+ return;
171
+ }
172
+ const top = open.at(-1);
173
+ if (top === void 0) return;
174
+ if (type === "separate") {
175
+ if (top.phase === "result") return;
176
+ const owner = codeOwner(open.slice(0, -1));
177
+ top.phase = "result";
178
+ if (owner !== void 0) owner.pending.push(child);
179
+ else if (top.candidate && wanted(top.instruction)) freeze(top, child);
180
+ return;
181
+ }
182
+ if (type === "end") {
183
+ open.pop();
184
+ const owner = codeOwner();
185
+ if (owner !== void 0) owner.pending.push(child);
186
+ else if (top.frozen) remove(child);
187
+ else if (top.candidate && top.phase === "code" && wanted(top.instruction)) freeze(top, child);
188
+ }
189
+ };
190
+ const visit = (element) => {
191
+ const name = wName(element);
192
+ if (name === "r") {
193
+ for (const child of elementChildren(element)) if (wName(child) !== "rPr") visitRunChild(child);
194
+ return;
195
+ }
196
+ if (name === "fldSimple") {
197
+ const owner = codeOwner();
198
+ if (owner !== void 0) {
199
+ owner.pending.push(element);
200
+ return;
201
+ }
202
+ const instruction = wAttr(element, "instr") ?? "";
203
+ const range = rangeOf(element);
204
+ const declaresPrefix = Object.keys(element.attributes ?? {}).some((attribute) => attribute === "xmlns" || attribute.startsWith("xmlns:"));
205
+ if (wanted(instruction) && range !== null && !declaresPrefix) {
206
+ count(instruction);
207
+ const inner = innerRangeOf(part.xml, element);
208
+ if (inner === null) {
209
+ splices.push({
210
+ ...range,
211
+ newXml: ""
212
+ });
213
+ return;
214
+ }
215
+ splices.push({
216
+ start: range.start,
217
+ end: inner.start,
218
+ newXml: ""
219
+ }, {
220
+ start: inner.end,
221
+ end: range.end,
222
+ newXml: ""
223
+ });
224
+ }
225
+ }
226
+ for (const child of elementChildren(element)) visit(child);
227
+ };
228
+ visit(part.root);
229
+ return {
230
+ splices: withoutContained(splices),
231
+ frozen: [...counts].map(([kind, total]) => ({
232
+ kind,
233
+ count: total
234
+ }))
235
+ };
236
+ };
237
+ export { bookmarkEndXml, bookmarkIdsOf, bookmarkStartXml, complexFieldXml, fieldBookmarkOf, freezeFields, mintRefBookmarkName, refFieldInstruction, setUpdateFieldsOnOpen, withoutContained, wrapParagraphContentInBookmark };
@@ -0,0 +1,118 @@
1
+ /**
2
+ * The markup of what a resolved plan places: paragraphs, tables and notes
3
+ * rebuilt from source content in the template's own spelling. Text, tabs,
4
+ * breaks, note references, links and stated emphasis travel; fonts, sizes,
5
+ * colours and direct paragraph formatting stay behind.
6
+ */
7
+ import { document_d_exports } from "../../../types/document.js";
8
+ import { XmlElement } from "../../xmlParser.js";
9
+ import { AnalyzedDocument, AnalyzedNote } from "./blocks.js";
10
+ import { BookmarkWrap } from "./builders.js";
11
+ import { ExpectedSegment, PieceOrigin, ResolvedParagraph, SignatureContext } from "./plan.js";
12
+ import { AtomLink, StoryField } from "./story.js";
13
+ import { TemplatePlanError } from "./types.js";
14
+ import { PartPrefixes } from "./xml.js";
15
+ import { Result } from "better-result";
16
+ export type RelationshipsEditor = {
17
+ path: string;
18
+ add: (relationship: {
19
+ type: string;
20
+ target: string;
21
+ external: boolean;
22
+ }) => string;
23
+ /** The part's markup with the added relationships, or null when none were added. */
24
+ xml: () => string | null;
25
+ };
26
+ /**
27
+ * An editor over a relationships part that may not exist yet. Null when the
28
+ * part exists and its root is not a relationships root.
29
+ */
30
+ export declare const createRelationshipsEditor: (path: string, existing: string | null) => RelationshipsEditor | null;
31
+ export type TemplateCharacterStyles = {
32
+ hyperlink: string | null;
33
+ footnoteReference: string | null;
34
+ endnoteReference: string | null;
35
+ footnoteText: string | null;
36
+ endnoteText: string | null;
37
+ };
38
+ /** The template styles conventional marks and note text are written with. */
39
+ export declare const templateCharacterStyles: (template: Pick<AnalyzedDocument, "styles">) => TemplateCharacterStyles;
40
+ export type NoteKind = "footnote" | "endnote";
41
+ /** A media part of the source, read ahead so the emit stays synchronous. */
42
+ export type SourceMedia = {
43
+ bytes: Uint8Array;
44
+ extension: string;
45
+ };
46
+ export type EmitShared = {
47
+ styles: TemplateCharacterStyles;
48
+ /** The source relationships of each part pieces come from. */
49
+ sourceRelationships: Record<PieceOrigin, document_d_exports.RelationshipMap>;
50
+ /** Source media by the package path it is stored at. */
51
+ sourceMedia: ReadonlyMap<string, SourceMedia>;
52
+ resolveSourceTarget: (origin: PieceOrigin, target: string) => string;
53
+ /** The new id of a source note; allocates one the first time a note is met. */
54
+ noteId: (kind: NoteKind, sourceId: string) => number;
55
+ /** Register a media part of the output and return its absolute target. */
56
+ addMedia: (sourcePath: string, media: SourceMedia) => string;
57
+ nextDrawingId: () => number;
58
+ };
59
+ export type EmitContext = EmitShared & {
60
+ prefixes: PartPrefixes;
61
+ relationships: RelationshipsEditor;
62
+ };
63
+ export type LinkAddress = {
64
+ url: string;
65
+ anchor: string | null;
66
+ };
67
+ /**
68
+ * The external address a link leads to, or null for a link the output cannot
69
+ * carry: one to a bookmark of the source, or one whose relationship is
70
+ * missing or does not name an external address.
71
+ */
72
+ export declare const linkAddress: (link: AtomLink | null, relationships: document_d_exports.RelationshipMap) => LinkAddress | null;
73
+ /** One string per address, for comparing what a link leads to. */
74
+ export declare const linkAddressKey: (address: LinkAddress | null) => string;
75
+ /**
76
+ * A WordprocessingML subtree re-spelled under `prefix`. Elements and
77
+ * attributes of other namespaces are left out: this copies structure such as
78
+ * a table grid, never content.
79
+ */
80
+ export declare const wSubtreeXml: (element: XmlElement, prefix: string) => string;
81
+ type RunsResult = Result<string, TemplatePlanError>;
82
+ /** The runs of a segment list, references written as complex REF fields. */
83
+ export declare const segmentsRunsXml: (segments: readonly ExpectedSegment[], context: EmitContext, instructionOf: (segment: ExpectedSegment) => string | null) => RunsResult;
84
+ export type ParagraphEmit = {
85
+ paragraph: ResolvedParagraph;
86
+ bookmark: BookmarkWrap | undefined;
87
+ instructionOf: (segment: ExpectedSegment) => string | null;
88
+ /** Whether the paragraph's style binds a list that `numbering: null` must cancel. */
89
+ styleBindsList: boolean;
90
+ };
91
+ /** Runs of a placed paragraph, inside its bookmark when it is a reference target. */
92
+ export declare const paragraphContentXml: ({ paragraph, bookmark, instructionOf }: ParagraphEmit, context: EmitContext) => RunsResult;
93
+ /** A whole `w:p` for a placed source paragraph. */
94
+ export declare const paragraphXml: (emit: ParagraphEmit, context: EmitContext) => RunsResult;
95
+ export type TableEmit = {
96
+ table: XmlElement;
97
+ /** A table style of the template, or null to keep the source's own borders. */
98
+ styleId: string | null;
99
+ paragraphOf: (element: XmlElement) => RunsResult;
100
+ };
101
+ /**
102
+ * A source table rebuilt: its grid, spans and merges, with every cell
103
+ * paragraph placed like a body paragraph. Without a template table style the
104
+ * source's borders are kept so the grid stays visible.
105
+ */
106
+ export declare const tableXml: ({ table, styleId, paragraphOf }: TableEmit, context: EmitContext) => RunsResult;
107
+ export type NoteEmit = {
108
+ kind: NoteKind;
109
+ id: number;
110
+ note: AnalyzedNote;
111
+ /** Styles of the source, to read what the note's runs state. */
112
+ source: SignatureContext;
113
+ /** The fields of the source notes part. */
114
+ fields: readonly StoryField[];
115
+ };
116
+ /** A source note under its new id, its paragraphs in the template's note style. */
117
+ export declare const noteXml: ({ kind, id, note, source, fields }: NoteEmit, context: EmitContext) => RunsResult;
118
+ export {};