@beyondwork/docx-react-component 1.0.1 → 1.0.3

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 (172) hide show
  1. package/README.md +44 -104
  2. package/package.json +50 -30
  3. package/src/README.md +85 -0
  4. package/src/api/README.md +22 -0
  5. package/src/api/public-types.ts +525 -0
  6. package/src/compare/diff-engine.ts +530 -0
  7. package/src/compare/export-redlines.ts +162 -0
  8. package/src/compare/snapshot.ts +37 -0
  9. package/src/component-inventory.md +99 -0
  10. package/src/core/README.md +10 -0
  11. package/src/core/commands/README.md +3 -0
  12. package/src/core/commands/formatting-commands.ts +161 -0
  13. package/src/core/commands/image-commands.ts +144 -0
  14. package/src/core/commands/index.ts +1013 -0
  15. package/src/core/commands/list-commands.ts +370 -0
  16. package/src/core/commands/review-commands.ts +108 -0
  17. package/src/core/commands/text-commands.ts +119 -0
  18. package/src/core/schema/README.md +3 -0
  19. package/src/core/schema/text-schema.ts +512 -0
  20. package/src/core/selection/README.md +3 -0
  21. package/src/core/selection/mapping.ts +238 -0
  22. package/src/core/selection/review-anchors.ts +94 -0
  23. package/src/core/state/README.md +3 -0
  24. package/src/core/state/editor-state.ts +580 -0
  25. package/src/core/state/text-transaction.ts +276 -0
  26. package/src/formats/xlsx/io/parse-shared-strings.ts +41 -0
  27. package/src/formats/xlsx/io/parse-sheet.ts +289 -0
  28. package/src/formats/xlsx/io/parse-styles.ts +57 -0
  29. package/src/formats/xlsx/io/parse-workbook.ts +75 -0
  30. package/src/formats/xlsx/io/xlsx-session.ts +306 -0
  31. package/src/formats/xlsx/model/cell.ts +189 -0
  32. package/src/formats/xlsx/model/sheet.ts +244 -0
  33. package/src/formats/xlsx/model/styles.ts +118 -0
  34. package/src/formats/xlsx/model/workbook.ts +449 -0
  35. package/src/index.ts +45 -0
  36. package/src/io/README.md +10 -0
  37. package/src/io/docx-session.ts +1763 -0
  38. package/src/io/export/README.md +3 -0
  39. package/src/io/export/export-session.ts +165 -0
  40. package/src/io/export/minimal-docx.ts +115 -0
  41. package/src/io/export/reattach-preserved-parts.ts +54 -0
  42. package/src/io/export/serialize-comments.ts +876 -0
  43. package/src/io/export/serialize-footnotes.ts +217 -0
  44. package/src/io/export/serialize-headers-footers.ts +200 -0
  45. package/src/io/export/serialize-main-document.ts +982 -0
  46. package/src/io/export/serialize-numbering.ts +97 -0
  47. package/src/io/export/serialize-revisions.ts +389 -0
  48. package/src/io/export/serialize-runtime-revisions.ts +265 -0
  49. package/src/io/export/serialize-tables.ts +147 -0
  50. package/src/io/export/split-review-boundaries.ts +194 -0
  51. package/src/io/normalize/README.md +3 -0
  52. package/src/io/normalize/normalize-text.ts +437 -0
  53. package/src/io/ooxml/README.md +3 -0
  54. package/src/io/ooxml/parse-comments.ts +779 -0
  55. package/src/io/ooxml/parse-complex-content.ts +287 -0
  56. package/src/io/ooxml/parse-fields.ts +438 -0
  57. package/src/io/ooxml/parse-footnotes.ts +403 -0
  58. package/src/io/ooxml/parse-headers-footers.ts +483 -0
  59. package/src/io/ooxml/parse-inline-media.ts +431 -0
  60. package/src/io/ooxml/parse-main-document.ts +1846 -0
  61. package/src/io/ooxml/parse-numbering.ts +425 -0
  62. package/src/io/ooxml/parse-revisions.ts +658 -0
  63. package/src/io/ooxml/parse-shapes.ts +271 -0
  64. package/src/io/ooxml/parse-tables.ts +568 -0
  65. package/src/io/ooxml/parse-theme.ts +314 -0
  66. package/src/io/ooxml/part-manifest.ts +136 -0
  67. package/src/io/ooxml/revision-boundaries.ts +351 -0
  68. package/src/io/opc/README.md +3 -0
  69. package/src/io/opc/corrupt-package.ts +166 -0
  70. package/src/io/opc/docx-package.ts +74 -0
  71. package/src/io/opc/package-reader.ts +325 -0
  72. package/src/io/opc/package-writer.ts +273 -0
  73. package/src/legal/bookmarks.ts +196 -0
  74. package/src/legal/cross-references.ts +356 -0
  75. package/src/legal/defined-terms.ts +203 -0
  76. package/src/model/README.md +3 -0
  77. package/src/model/canonical-document.ts +1911 -0
  78. package/src/model/cds-1.0.0.ts +196 -0
  79. package/src/model/snapshot.ts +393 -0
  80. package/src/preservation/README.md +3 -0
  81. package/src/preservation/markup-compatibility.ts +48 -0
  82. package/src/preservation/opaque-fragment-store.ts +89 -0
  83. package/src/preservation/opaque-region.ts +233 -0
  84. package/src/preservation/package-preservation.ts +120 -0
  85. package/src/preservation/preserved-part-manifest.ts +56 -0
  86. package/src/preservation/relationship-retention.ts +57 -0
  87. package/src/preservation/store.ts +185 -0
  88. package/src/review/README.md +16 -0
  89. package/src/review/store/README.md +3 -0
  90. package/src/review/store/comment-anchors.ts +70 -0
  91. package/src/review/store/comment-remapping.ts +154 -0
  92. package/src/review/store/comment-store.ts +331 -0
  93. package/src/review/store/comment-thread.ts +109 -0
  94. package/src/review/store/revision-actions.ts +394 -0
  95. package/src/review/store/revision-store.ts +303 -0
  96. package/src/review/store/revision-types.ts +168 -0
  97. package/src/review/store/runtime-comment-store.ts +43 -0
  98. package/src/runtime/README.md +3 -0
  99. package/src/runtime/ai-action-policy.ts +764 -0
  100. package/src/runtime/document-runtime.ts +967 -0
  101. package/src/runtime/read-only-diagnostics-runtime.ts +232 -0
  102. package/src/runtime/review-runtime.ts +44 -0
  103. package/src/runtime/revision-runtime.ts +107 -0
  104. package/src/runtime/session-capabilities.ts +138 -0
  105. package/src/runtime/surface-projection.ts +570 -0
  106. package/src/runtime/table-commands.ts +87 -0
  107. package/src/runtime/table-schema.ts +140 -0
  108. package/src/runtime/virtualized-rendering.ts +258 -0
  109. package/src/ui/README.md +30 -0
  110. package/src/ui/WordReviewEditor.tsx +1506 -0
  111. package/src/ui/comments/README.md +3 -0
  112. package/src/ui/compatibility/README.md +3 -0
  113. package/src/ui/editor-surface/README.md +3 -0
  114. package/src/ui/headless/comment-decoration-model.ts +124 -0
  115. package/src/ui/headless/revision-decoration-model.ts +128 -0
  116. package/src/ui/headless/selection-helpers.ts +34 -0
  117. package/src/ui/headless/use-editor-keyboard.ts +98 -0
  118. package/src/ui/review/README.md +3 -0
  119. package/src/ui/shared/revision-filters.ts +31 -0
  120. package/src/ui/status/README.md +3 -0
  121. package/src/ui/theme/README.md +3 -0
  122. package/src/ui/toolbar/README.md +3 -0
  123. package/src/ui-tailwind/chrome/tw-alert-banner.tsx +48 -0
  124. package/src/ui-tailwind/chrome/tw-selection-toolbar.tsx +44 -0
  125. package/src/ui-tailwind/chrome/tw-unsaved-modal.tsx +58 -0
  126. package/src/ui-tailwind/chrome/use-before-unload.ts +20 -0
  127. package/src/ui-tailwind/editor-surface/pm-command-bridge.ts +139 -0
  128. package/src/ui-tailwind/editor-surface/pm-decorations.ts +98 -0
  129. package/src/ui-tailwind/editor-surface/pm-position-map.ts +123 -0
  130. package/src/ui-tailwind/editor-surface/pm-schema.ts +452 -0
  131. package/src/ui-tailwind/editor-surface/pm-state-from-snapshot.ts +327 -0
  132. package/src/ui-tailwind/editor-surface/search-plugin.ts +157 -0
  133. package/src/ui-tailwind/editor-surface/tw-caret.tsx +12 -0
  134. package/src/ui-tailwind/editor-surface/tw-editor-surface.tsx +150 -0
  135. package/src/ui-tailwind/editor-surface/tw-inline-token.tsx +118 -0
  136. package/src/ui-tailwind/editor-surface/tw-opaque-block.tsx +52 -0
  137. package/src/ui-tailwind/editor-surface/tw-paragraph-block.tsx +151 -0
  138. package/src/ui-tailwind/editor-surface/tw-prosemirror-surface.tsx +215 -0
  139. package/src/ui-tailwind/editor-surface/tw-segment-view.tsx +111 -0
  140. package/src/ui-tailwind/editor-surface/tw-table-node-view.tsx +122 -0
  141. package/src/ui-tailwind/index.ts +61 -0
  142. package/src/ui-tailwind/review/tw-comment-sidebar.tsx +276 -0
  143. package/src/ui-tailwind/review/tw-health-panel.tsx +120 -0
  144. package/src/ui-tailwind/review/tw-review-rail.tsx +120 -0
  145. package/src/ui-tailwind/review/tw-revision-sidebar.tsx +164 -0
  146. package/src/ui-tailwind/status/tw-status-bar.tsx +58 -0
  147. package/src/ui-tailwind/theme/editor-theme.css +190 -0
  148. package/src/ui-tailwind/toolbar/tw-toolbar-icon-button.tsx +48 -0
  149. package/src/ui-tailwind/toolbar/tw-toolbar.tsx +231 -0
  150. package/src/ui-tailwind/tw-review-workspace.tsx +140 -0
  151. package/src/validation/README.md +3 -0
  152. package/src/validation/compatibility-engine.ts +317 -0
  153. package/src/validation/compatibility-report.ts +160 -0
  154. package/src/validation/diagnostics.ts +203 -0
  155. package/src/validation/import-diagnostics.ts +128 -0
  156. package/src/validation/low-priority-word-surfaces.ts +373 -0
  157. package/dist/chunk-32W6IVQE.js +0 -7725
  158. package/dist/chunk-32W6IVQE.js.map +0 -1
  159. package/dist/index.cjs +0 -23722
  160. package/dist/index.cjs.map +0 -1
  161. package/dist/index.d.cts +0 -7
  162. package/dist/index.d.ts +0 -7
  163. package/dist/index.js +0 -16011
  164. package/dist/index.js.map +0 -1
  165. package/dist/public-types-DqCURAz8.d.cts +0 -1152
  166. package/dist/public-types-DqCURAz8.d.ts +0 -1152
  167. package/dist/tailwind.cjs +0 -8295
  168. package/dist/tailwind.cjs.map +0 -1
  169. package/dist/tailwind.d.cts +0 -323
  170. package/dist/tailwind.d.ts +0 -323
  171. package/dist/tailwind.js +0 -553
  172. package/dist/tailwind.js.map +0 -1
@@ -0,0 +1,876 @@
1
+ import type { CommentEntry, CommentThread } from "../../review/store/comment-store.ts";
2
+ import type { RevisionParagraphBoundary } from "../ooxml/revision-boundaries.ts";
3
+ import type { ImportedCommentDefinition } from "../ooxml/parse-comments.ts";
4
+
5
+ interface XmlElementNode {
6
+ type: "element";
7
+ name: string;
8
+ attributes: Record<string, string>;
9
+ children: XmlNode[];
10
+ start: number;
11
+ end: number;
12
+ }
13
+
14
+ interface XmlTextNode {
15
+ type: "text";
16
+ text: string;
17
+ start: number;
18
+ end: number;
19
+ }
20
+
21
+ type XmlNode = XmlElementNode | XmlTextNode;
22
+
23
+ interface ParagraphBoundaryMap {
24
+ paragraphIndex: number;
25
+ start: number;
26
+ end: number;
27
+ boundaries: Map<number, number>;
28
+ }
29
+
30
+ interface SerializableCommentEntry {
31
+ thread: CommentThread;
32
+ entry: CommentEntry;
33
+ exportCommentId: string;
34
+ paraId: string;
35
+ durableId?: string;
36
+ isRoot: boolean;
37
+ }
38
+
39
+ export interface SerializedCommentsResult {
40
+ commentsXml: string;
41
+ serializedCommentIds: string[];
42
+ commentsExtendedXml?: string;
43
+ commentsIdsXml?: string;
44
+ peopleXml?: string;
45
+ }
46
+
47
+ export interface SerializeMergedCommentsXmlOptions {
48
+ preservedDefinitions?: readonly ImportedCommentDefinition[];
49
+ sourceRootTag?: string;
50
+ sourceExtendedRootTag?: string;
51
+ sourceIdsRootTag?: string;
52
+ sourcePeopleRootTag?: string;
53
+ peopleAuthors?: readonly string[];
54
+ exportCommentIds?: ReadonlyMap<string, string>;
55
+ }
56
+
57
+ export interface SerializeCommentAnchorsOptions {
58
+ exportCommentIds?: ReadonlyMap<string, string>;
59
+ }
60
+
61
+ export interface SerializedCommentDocumentResult {
62
+ documentXml: string;
63
+ serializedCommentIds: string[];
64
+ skippedCommentIds: string[];
65
+ }
66
+
67
+ const DEFAULT_COMMENTS_ROOT_TAG =
68
+ `<w:comments xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="w14">`;
69
+ const DEFAULT_COMMENTS_EXTENDED_ROOT_TAG =
70
+ `<w15:commentsEx xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml">`;
71
+ const DEFAULT_COMMENTS_IDS_ROOT_TAG =
72
+ `<w16cid:commentsIds xmlns:w16cid="http://schemas.microsoft.com/office/word/2016/wordml/cid">`;
73
+ const DEFAULT_PEOPLE_ROOT_TAG =
74
+ `<w15:people xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml">`;
75
+
76
+ export function serializeCommentsXml(
77
+ threads: readonly CommentThread[],
78
+ options: {
79
+ exportCommentIds?: ReadonlyMap<string, string>;
80
+ } = {},
81
+ ): SerializedCommentsResult {
82
+ return serializeMergedCommentsXml(threads, {
83
+ exportCommentIds: options.exportCommentIds,
84
+ });
85
+ }
86
+
87
+ export function serializeMergedCommentsXml(
88
+ threads: readonly CommentThread[],
89
+ options: SerializeMergedCommentsXmlOptions = {},
90
+ ): SerializedCommentsResult {
91
+ const serializableThreads = threads.filter((thread) => thread.entries.length > 0);
92
+ const preservedDefinitions = (options.preservedDefinitions ?? [])
93
+ .slice()
94
+ .sort((left, right) => left.order - right.order);
95
+ const runtimeThreadById = new Map(
96
+ serializableThreads.map((thread) => [thread.commentId, thread]),
97
+ );
98
+ const exportCommentIds =
99
+ options.exportCommentIds ??
100
+ createCommentExportIdMap(serializableThreads, preservedDefinitions);
101
+ const serializableEntries = createSerializableEntries(
102
+ serializableThreads,
103
+ preservedDefinitions,
104
+ exportCommentIds,
105
+ );
106
+ const serializedEntryByOoxmlId = new Map(
107
+ serializableEntries.map((entry) => [entry.exportCommentId, entry]),
108
+ );
109
+ const emittedThreadIds = new Set<string>();
110
+ const mergedComments: string[] = [];
111
+
112
+ for (const definition of preservedDefinitions) {
113
+ const ownedThread = runtimeThreadById.get(definition.commentId);
114
+ if (ownedThread) {
115
+ if (emittedThreadIds.has(ownedThread.commentId)) {
116
+ continue;
117
+ }
118
+ mergedComments.push(
119
+ ...serializeThreadEntries(
120
+ serializableEntries.filter((entry) => entry.thread.commentId === ownedThread.commentId),
121
+ ),
122
+ );
123
+ emittedThreadIds.add(ownedThread.commentId);
124
+ continue;
125
+ }
126
+
127
+ if (serializedEntryByOoxmlId.has(definition.commentId)) {
128
+ continue;
129
+ }
130
+
131
+ mergedComments.push(definition.rawXml);
132
+ }
133
+
134
+ for (const thread of serializableThreads) {
135
+ if (emittedThreadIds.has(thread.commentId)) {
136
+ continue;
137
+ }
138
+
139
+ mergedComments.push(
140
+ ...serializeThreadEntries(
141
+ serializableEntries.filter((entry) => entry.thread.commentId === thread.commentId),
142
+ ),
143
+ );
144
+ emittedThreadIds.add(thread.commentId);
145
+ }
146
+
147
+ const mergedExtendedEntries = [
148
+ ...serializePreservedCommentExtensions(
149
+ preservedDefinitions.filter((definition) => !serializedEntryByOoxmlId.has(definition.commentId)),
150
+ ),
151
+ ...serializableEntries
152
+ .map((entry) => serializeCommentExtension(entry))
153
+ .filter((xml): xml is string => Boolean(xml)),
154
+ ];
155
+ const mergedDurableIds = [
156
+ ...serializePreservedCommentIds(
157
+ preservedDefinitions.filter((definition) => !serializedEntryByOoxmlId.has(definition.commentId)),
158
+ ),
159
+ ...serializableEntries
160
+ .map((entry) => serializeCommentDurableId(entry))
161
+ .filter((xml): xml is string => Boolean(xml)),
162
+ ];
163
+ const peopleAuthors = new Set([
164
+ ...(options.peopleAuthors ?? []),
165
+ ...preservedDefinitions
166
+ .map((definition) => definition.authorId)
167
+ .filter((authorId): authorId is string => typeof authorId === "string" && authorId.length > 0),
168
+ ...serializableEntries
169
+ .map((entry) => entry.entry.authorId)
170
+ .filter((authorId): authorId is string => typeof authorId === "string" && authorId.length > 0),
171
+ ]);
172
+
173
+ return {
174
+ commentsXml: [
175
+ `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>`,
176
+ normalizeCommentsRootTag(options.sourceRootTag ?? DEFAULT_COMMENTS_ROOT_TAG),
177
+ ...mergedComments,
178
+ `</w:comments>`,
179
+ ].join("\n"),
180
+ serializedCommentIds: serializableThreads.map((thread) => thread.commentId),
181
+ commentsExtendedXml:
182
+ mergedExtendedEntries.length > 0
183
+ ? [
184
+ `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>`,
185
+ options.sourceExtendedRootTag ?? DEFAULT_COMMENTS_EXTENDED_ROOT_TAG,
186
+ ...mergedExtendedEntries,
187
+ `</w15:commentsEx>`,
188
+ ].join("\n")
189
+ : undefined,
190
+ commentsIdsXml:
191
+ mergedDurableIds.length > 0
192
+ ? [
193
+ `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>`,
194
+ options.sourceIdsRootTag ?? DEFAULT_COMMENTS_IDS_ROOT_TAG,
195
+ ...mergedDurableIds,
196
+ `</w16cid:commentsIds>`,
197
+ ].join("\n")
198
+ : undefined,
199
+ peopleXml:
200
+ peopleAuthors.size > 0
201
+ ? [
202
+ `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>`,
203
+ options.sourcePeopleRootTag ?? DEFAULT_PEOPLE_ROOT_TAG,
204
+ ...[...peopleAuthors]
205
+ .sort((left, right) => left.localeCompare(right))
206
+ .map(
207
+ (authorId) =>
208
+ `<w15:person w15:author="${escapeAttribute(authorId)}" />`,
209
+ ),
210
+ `</w15:people>`,
211
+ ].join("\n")
212
+ : undefined,
213
+ };
214
+ }
215
+
216
+ function normalizeCommentsRootTag(rootTag: string): string {
217
+ let normalized = rootTag;
218
+ if (!/\bxmlns:w14=/u.test(normalized)) {
219
+ normalized = normalized.replace(
220
+ />$/u,
221
+ ` xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml">`,
222
+ );
223
+ }
224
+ if (!/\bxmlns:mc=/u.test(normalized)) {
225
+ normalized = normalized.replace(
226
+ />$/u,
227
+ ` xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006">`,
228
+ );
229
+ }
230
+ if (!/\bmc:Ignorable=/u.test(normalized)) {
231
+ normalized = normalized.replace(/>$/u, ` mc:Ignorable="w14">`);
232
+ } else if (!/\bmc:Ignorable="[^"]*\bw14\b/u.test(normalized)) {
233
+ normalized = normalized.replace(
234
+ /\bmc:Ignorable="([^"]*)"/u,
235
+ (_match, value: string) =>
236
+ `mc:Ignorable="${`${value} w14`.trim().replace(/\s+/gu, " ")}"`,
237
+ );
238
+ }
239
+ return normalized;
240
+ }
241
+
242
+ export function serializeCommentAnchorsIntoDocumentXml(
243
+ documentXml: string,
244
+ threads: readonly CommentThread[],
245
+ paragraphs: readonly Pick<
246
+ RevisionParagraphBoundary,
247
+ "paragraphIndex" | "start" | "end" | "boundaries"
248
+ >[] = mapParagraphBoundaries(documentXml),
249
+ options: SerializeCommentAnchorsOptions = {},
250
+ ): SerializedCommentDocumentResult {
251
+ const insertions = new Map<number, string[]>();
252
+ const serializedCommentIds: string[] = [];
253
+ const skippedCommentIds: string[] = [];
254
+ const exportCommentIds =
255
+ options.exportCommentIds ?? createCommentExportIdMap(threads);
256
+
257
+ for (const thread of threads) {
258
+ if (thread.anchor.kind !== "range") {
259
+ skippedCommentIds.push(thread.commentId);
260
+ continue;
261
+ }
262
+
263
+ const paragraph = paragraphs.find(
264
+ (candidate) =>
265
+ thread.anchor.range.from >= candidate.start &&
266
+ thread.anchor.range.to <= candidate.end,
267
+ );
268
+
269
+ if (!paragraph) {
270
+ skippedCommentIds.push(thread.commentId);
271
+ continue;
272
+ }
273
+
274
+ const startIndex = paragraph.boundaries.get(thread.anchor.range.from);
275
+ const endIndex = paragraph.boundaries.get(thread.anchor.range.to);
276
+
277
+ if (startIndex === undefined || endIndex === undefined) {
278
+ skippedCommentIds.push(thread.commentId);
279
+ continue;
280
+ }
281
+
282
+ const exportCommentId = exportCommentIds.get(thread.commentId) ?? thread.commentId;
283
+ pushInsertion(
284
+ insertions,
285
+ startIndex,
286
+ `<w:commentRangeStart w:id="${escapeAttribute(exportCommentId)}"/>`,
287
+ );
288
+ pushInsertion(
289
+ insertions,
290
+ endIndex,
291
+ `<w:commentRangeEnd w:id="${escapeAttribute(exportCommentId)}"/>`,
292
+ );
293
+ pushInsertion(
294
+ insertions,
295
+ endIndex,
296
+ `<w:r><w:commentReference w:id="${escapeAttribute(exportCommentId)}"/></w:r>`,
297
+ );
298
+ serializedCommentIds.push(thread.commentId);
299
+ }
300
+
301
+ const sortedInsertions = [...insertions.entries()].sort((left, right) => left[0] - right[0]);
302
+ let cursor = 0;
303
+ let output = "";
304
+
305
+ for (const [index, snippets] of sortedInsertions) {
306
+ output += documentXml.slice(cursor, index);
307
+ output += snippets.join("");
308
+ cursor = index;
309
+ }
310
+
311
+ output += documentXml.slice(cursor);
312
+
313
+ return {
314
+ documentXml: output,
315
+ serializedCommentIds,
316
+ skippedCommentIds,
317
+ };
318
+ }
319
+
320
+ export function createCommentExportIdMap(
321
+ threads: readonly CommentThread[],
322
+ preservedDefinitions: readonly ImportedCommentDefinition[] = [],
323
+ ): ReadonlyMap<string, string> {
324
+ const exportIds = new Map<string, string>();
325
+ const reservedNumericIds = new Set<number>();
326
+
327
+ for (const definition of preservedDefinitions) {
328
+ const numericId = parseOoxmlNumericId(definition.commentId);
329
+ if (numericId !== undefined) {
330
+ reservedNumericIds.add(numericId);
331
+ }
332
+ }
333
+
334
+ for (const thread of threads) {
335
+ const rootEntry = getRootEntry(thread);
336
+ const preferredValue =
337
+ thread.metadata?.rootOoxmlCommentId ??
338
+ rootEntry?.metadata?.ooxmlCommentId ??
339
+ thread.commentId;
340
+ const numericId = parseOoxmlNumericId(preferredValue);
341
+ if (numericId !== undefined && !reservedNumericIds.has(numericId)) {
342
+ exportIds.set(thread.commentId, String(numericId));
343
+ reservedNumericIds.add(numericId);
344
+ }
345
+ }
346
+
347
+ let nextGeneratedId =
348
+ reservedNumericIds.size > 0 ? Math.max(...reservedNumericIds) + 1 : 1;
349
+
350
+ for (const thread of threads) {
351
+ if (exportIds.has(thread.commentId)) {
352
+ continue;
353
+ }
354
+
355
+ while (reservedNumericIds.has(nextGeneratedId)) {
356
+ nextGeneratedId += 1;
357
+ }
358
+
359
+ exportIds.set(thread.commentId, String(nextGeneratedId));
360
+ reservedNumericIds.add(nextGeneratedId);
361
+ nextGeneratedId += 1;
362
+ }
363
+
364
+ return exportIds;
365
+ }
366
+
367
+ function createSerializableEntries(
368
+ threads: readonly CommentThread[],
369
+ preservedDefinitions: readonly ImportedCommentDefinition[],
370
+ exportCommentIds: ReadonlyMap<string, string>,
371
+ ): SerializableCommentEntry[] {
372
+ const entries: SerializableCommentEntry[] = [];
373
+ const reservedNumericIds = new Set<number>();
374
+
375
+ for (const definition of preservedDefinitions) {
376
+ const numericId = parseOoxmlNumericId(definition.commentId);
377
+ if (numericId !== undefined) {
378
+ reservedNumericIds.add(numericId);
379
+ }
380
+ }
381
+
382
+ for (const thread of threads) {
383
+ const rootEntry = getRootEntry(thread);
384
+ if (!rootEntry) {
385
+ continue;
386
+ }
387
+
388
+ const rootExportCommentId = exportCommentIds.get(thread.commentId) ?? thread.commentId;
389
+ const rootNumericId = parseOoxmlNumericId(rootExportCommentId);
390
+ if (rootNumericId !== undefined) {
391
+ reservedNumericIds.add(rootNumericId);
392
+ }
393
+
394
+ const rootParaId =
395
+ rootEntry.metadata?.paraId ??
396
+ thread.metadata?.rootParaId ??
397
+ generateParaId(thread.commentId, 0);
398
+ const rootDurableId =
399
+ rootEntry.metadata?.durableId ??
400
+ createDurableId(rootEntry, rootExportCommentId, 0);
401
+
402
+ entries.push({
403
+ thread,
404
+ entry: rootEntry,
405
+ exportCommentId: rootExportCommentId,
406
+ paraId: rootParaId,
407
+ durableId: rootDurableId,
408
+ isRoot: true,
409
+ });
410
+
411
+ for (let index = 1; index < thread.entries.length; index += 1) {
412
+ const entry = thread.entries[index];
413
+ if (!entry) {
414
+ continue;
415
+ }
416
+
417
+ const preferredCommentId = entry.metadata?.ooxmlCommentId;
418
+ let exportCommentId: string | undefined;
419
+ const preferredNumericId = preferredCommentId
420
+ ? parseOoxmlNumericId(preferredCommentId)
421
+ : undefined;
422
+ if (
423
+ preferredNumericId !== undefined &&
424
+ !reservedNumericIds.has(preferredNumericId)
425
+ ) {
426
+ exportCommentId = String(preferredNumericId);
427
+ reservedNumericIds.add(preferredNumericId);
428
+ } else {
429
+ let nextGeneratedId =
430
+ reservedNumericIds.size > 0 ? Math.max(...reservedNumericIds) + 1 : 1;
431
+ while (reservedNumericIds.has(nextGeneratedId)) {
432
+ nextGeneratedId += 1;
433
+ }
434
+ exportCommentId = String(nextGeneratedId);
435
+ reservedNumericIds.add(nextGeneratedId);
436
+ }
437
+
438
+ entries.push({
439
+ thread,
440
+ entry,
441
+ exportCommentId,
442
+ paraId: entry.metadata?.paraId ?? generateParaId(thread.commentId, index),
443
+ durableId:
444
+ entry.metadata?.durableId ??
445
+ createDurableId(entry, exportCommentId, index),
446
+ isRoot: false,
447
+ });
448
+ }
449
+ }
450
+
451
+ return entries;
452
+ }
453
+
454
+ function serializeThreadEntries(entries: readonly SerializableCommentEntry[]): string[] {
455
+ return entries.map((entry) => serializeCommentEntry(entry));
456
+ }
457
+
458
+ function serializeCommentEntry(entry: SerializableCommentEntry): string {
459
+ const author = escapeAttribute(entry.entry.authorId);
460
+ const createdAt = escapeAttribute(entry.entry.createdAt);
461
+ const initials = entry.entry.metadata?.initials;
462
+ const paragraphXml = serializeCommentParagraphs(entry.entry.body, entry.paraId);
463
+
464
+ return `<w:comment w:id="${escapeAttribute(entry.exportCommentId)}"${initials ? ` w:initials="${escapeAttribute(initials)}"` : ""} w:author="${author}" w:date="${createdAt}">${paragraphXml}</w:comment>`;
465
+ }
466
+
467
+ function serializeCommentParagraphs(body: string, paraId: string): string {
468
+ const paragraphs = body.length > 0 ? body.split("\n") : [""];
469
+ const textId = deriveTextIdFromParaId(paraId);
470
+ return paragraphs
471
+ .map((paragraph, index) => {
472
+ const attributes =
473
+ index === 0
474
+ ? ` w14:paraId="${escapeAttribute(paraId)}" w14:textId="${escapeAttribute(textId)}"`
475
+ : "";
476
+ return `<w:p${attributes}><w:r>${serializeText(paragraph)}</w:r></w:p>`;
477
+ })
478
+ .join("");
479
+ }
480
+
481
+ function serializePreservedCommentExtensions(
482
+ definitions: readonly ImportedCommentDefinition[],
483
+ ): string[] {
484
+ return definitions
485
+ .map((definition) =>
486
+ definition.paraId
487
+ ? serializePreservedCommentExtension(definition)
488
+ : undefined,
489
+ )
490
+ .filter((xml): xml is string => Boolean(xml));
491
+ }
492
+
493
+ function serializePreservedCommentExtension(
494
+ definition: ImportedCommentDefinition,
495
+ ): string {
496
+ const doneValue = definition.isDone ? "true" : "false";
497
+ return `<w15:commentEx w15:paraId="${escapeAttribute(definition.paraId!)}"${definition.parentParaId ? ` w15:paraIdParent="${escapeAttribute(definition.parentParaId)}"` : ""} w15:done="${doneValue}" />`;
498
+ }
499
+
500
+ function serializeCommentExtension(
501
+ entry: SerializableCommentEntry,
502
+ ): string | undefined {
503
+ return `<w15:commentEx w15:paraId="${escapeAttribute(entry.paraId)}"${entry.isRoot ? "" : ` w15:paraIdParent="${escapeAttribute(findRootParaId(entry.thread, entry.paraId))}"`} w15:done="${entry.isRoot && entry.thread.status === "resolved" ? "true" : "false"}" />`;
504
+ }
505
+
506
+ function serializePreservedCommentIds(
507
+ definitions: readonly ImportedCommentDefinition[],
508
+ ): string[] {
509
+ return definitions
510
+ .map((definition) =>
511
+ definition.paraId && definition.durableId
512
+ ? `<w16cid:commentId w16cid:paraId="${escapeAttribute(definition.paraId)}" w16cid:durableId="${escapeAttribute(definition.durableId)}" />`
513
+ : undefined,
514
+ )
515
+ .filter((xml): xml is string => Boolean(xml));
516
+ }
517
+
518
+ function serializeCommentDurableId(
519
+ entry: SerializableCommentEntry,
520
+ ): string | undefined {
521
+ if (!entry.durableId) {
522
+ return undefined;
523
+ }
524
+
525
+ return `<w16cid:commentId w16cid:paraId="${escapeAttribute(entry.paraId)}" w16cid:durableId="${escapeAttribute(entry.durableId)}" />`;
526
+ }
527
+
528
+ function getRootEntry(thread: CommentThread): CommentEntry | undefined {
529
+ return thread.entries[0];
530
+ }
531
+
532
+ function findRootParaId(thread: CommentThread, fallback: string): string {
533
+ return (
534
+ thread.entries[0]?.metadata?.paraId ??
535
+ thread.metadata?.rootParaId ??
536
+ fallback
537
+ );
538
+ }
539
+
540
+ function generateParaId(seed: string, index: number): string {
541
+ const normalized = `${seed}:${index}`;
542
+ let hash = 0;
543
+
544
+ for (let cursor = 0; cursor < normalized.length; cursor += 1) {
545
+ hash = (hash * 31 + normalized.charCodeAt(cursor)) >>> 0;
546
+ }
547
+
548
+ return hash.toString(16).toUpperCase().padStart(8, "0").slice(-8);
549
+ }
550
+
551
+ function createDurableId(
552
+ entry: CommentEntry,
553
+ exportCommentId: string,
554
+ index: number,
555
+ ): string {
556
+ const seed = `${entry.entryId}:${exportCommentId}:${index}`;
557
+ let hash = 0n;
558
+
559
+ for (let cursor = 0; cursor < seed.length; cursor += 1) {
560
+ hash = (hash * 131n + BigInt(seed.charCodeAt(cursor))) & 0xffffffffffffffffn;
561
+ }
562
+
563
+ return hash.toString(16).toUpperCase().padStart(16, "0").slice(-16);
564
+ }
565
+
566
+ function deriveTextIdFromParaId(paraId: string): string {
567
+ return paraId.slice(-8).padStart(8, "0");
568
+ }
569
+
570
+ function parseOoxmlNumericId(value: string): number | undefined {
571
+ if (!/^-?\d+$/u.test(value)) {
572
+ return undefined;
573
+ }
574
+
575
+ const numericId = Number.parseInt(value, 10);
576
+ return Number.isFinite(numericId) ? numericId : undefined;
577
+ }
578
+
579
+ function mapParagraphBoundaries(documentXml: string): ParagraphBoundaryMap[] {
580
+ const root = parseXml(documentXml);
581
+ const documentElement = findChildElement(root, "document");
582
+ const bodyElement = findChildElement(documentElement, "body");
583
+ const paragraphs: ParagraphBoundaryMap[] = [];
584
+ let globalCursor = 0;
585
+ let paragraphIndex = -1;
586
+ let previousWasParagraph = false;
587
+
588
+ for (const child of bodyElement.children) {
589
+ if (child.type !== "element") {
590
+ continue;
591
+ }
592
+
593
+ if (localName(child.name) !== "p") {
594
+ globalCursor += 1;
595
+ previousWasParagraph = false;
596
+ continue;
597
+ }
598
+
599
+ if (previousWasParagraph) {
600
+ globalCursor += 1;
601
+ }
602
+ paragraphIndex += 1;
603
+ const boundaries = new Map<number, number>();
604
+ boundaries.set(globalCursor, child.start + openingTagLength(documentXml, child.start));
605
+
606
+ walkParagraphForBoundaries(
607
+ child,
608
+ documentXml,
609
+ boundaries,
610
+ () => globalCursor,
611
+ (next) => {
612
+ globalCursor = next;
613
+ },
614
+ );
615
+
616
+ if (!boundaries.has(globalCursor)) {
617
+ boundaries.set(globalCursor, child.end - 4);
618
+ }
619
+ paragraphs.push({
620
+ paragraphIndex,
621
+ start: Math.min(...boundaries.keys()),
622
+ end: Math.max(...boundaries.keys()),
623
+ boundaries,
624
+ });
625
+ previousWasParagraph = true;
626
+ }
627
+
628
+ return paragraphs;
629
+ }
630
+
631
+ function walkParagraphForBoundaries(
632
+ paragraph: XmlElementNode,
633
+ sourceXml: string,
634
+ boundaries: Map<number, number>,
635
+ getCursor: () => number,
636
+ setCursor: (next: number) => void,
637
+ ): void {
638
+ for (const child of paragraph.children) {
639
+ walkInlineNodeForBoundaries(child, sourceXml, boundaries, getCursor, setCursor);
640
+ }
641
+ }
642
+
643
+ function walkInlineNodeForBoundaries(
644
+ node: XmlNode,
645
+ sourceXml: string,
646
+ boundaries: Map<number, number>,
647
+ getCursor: () => number,
648
+ setCursor: (next: number) => void,
649
+ ): void {
650
+ if (node.type !== "element") {
651
+ return;
652
+ }
653
+
654
+ switch (localName(node.name)) {
655
+ case "r": {
656
+ if (!boundaries.has(getCursor())) {
657
+ boundaries.set(getCursor(), node.start);
658
+ }
659
+
660
+ for (const child of node.children) {
661
+ walkInlineNodeForBoundaries(child, sourceXml, boundaries, getCursor, setCursor);
662
+ }
663
+
664
+ boundaries.set(getCursor(), node.end);
665
+ return;
666
+ }
667
+ case "t": {
668
+ const text = node.children
669
+ .filter((child): child is XmlTextNode => child.type === "text")
670
+ .map((child) => child.text)
671
+ .join("");
672
+ setCursor(getCursor() + Array.from(text).length);
673
+ return;
674
+ }
675
+ case "tab":
676
+ case "br": {
677
+ const startCursor = getCursor();
678
+ boundaries.set(startCursor, node.start);
679
+ const nextCursor = startCursor + 1;
680
+ boundaries.set(nextCursor, node.end);
681
+ setCursor(nextCursor);
682
+ return;
683
+ }
684
+ default:
685
+ for (const child of node.children) {
686
+ walkInlineNodeForBoundaries(child, sourceXml, boundaries, getCursor, setCursor);
687
+ }
688
+ }
689
+ }
690
+
691
+ function pushInsertion(
692
+ insertions: Map<number, string[]>,
693
+ index: number,
694
+ xml: string,
695
+ ): void {
696
+ const bucket = insertions.get(index);
697
+ if (bucket) {
698
+ bucket.push(xml);
699
+ return;
700
+ }
701
+
702
+ insertions.set(index, [xml]);
703
+ }
704
+
705
+ function serializeText(text: string): string {
706
+ const preserve = requiresPreservedSpace(text) ? ` xml:space="preserve"` : "";
707
+ return `<w:t${preserve}>${escapeXml(text)}</w:t>`;
708
+ }
709
+
710
+ function requiresPreservedSpace(text: string): boolean {
711
+ return /^\s/u.test(text) || /\s$/u.test(text) || /\s{2,}/u.test(text);
712
+ }
713
+
714
+ function escapeXml(value: string): string {
715
+ return value
716
+ .replace(/&/g, "&amp;")
717
+ .replace(/</g, "&lt;")
718
+ .replace(/>/g, "&gt;");
719
+ }
720
+
721
+ function escapeAttribute(value: string): string {
722
+ return escapeXml(value).replace(/"/g, "&quot;");
723
+ }
724
+
725
+ function openingTagLength(xml: string, start: number): number {
726
+ const end = xml.indexOf(">", start);
727
+ if (end < 0) {
728
+ throw new Error("Malformed XML: missing tag close.");
729
+ }
730
+
731
+ return end - start + 1;
732
+ }
733
+
734
+ function parseXml(xml: string): XmlElementNode {
735
+ const root: XmlElementNode = {
736
+ type: "element",
737
+ name: "#document",
738
+ attributes: {},
739
+ children: [],
740
+ start: 0,
741
+ end: xml.length,
742
+ };
743
+ const stack: XmlElementNode[] = [root];
744
+ const tokenPattern =
745
+ /<!--[\s\S]*?-->|<\?[\s\S]*?\?>|<!DOCTYPE[\s\S]*?>|<!\[CDATA\[[\s\S]*?\]\]>|<[^>]+>|[^<]+/gu;
746
+
747
+ for (const match of xml.matchAll(tokenPattern)) {
748
+ const token = match[0] ?? "";
749
+ const start = match.index ?? 0;
750
+ const end = start + token.length;
751
+
752
+ if (token.startsWith("<?") || token.startsWith("<!DOCTYPE") || token.startsWith("<!--")) {
753
+ continue;
754
+ }
755
+
756
+ if (token.startsWith("<![CDATA[")) {
757
+ const text = token.slice(9, -3);
758
+ stack[stack.length - 1]?.children.push({
759
+ type: "text",
760
+ text,
761
+ start,
762
+ end,
763
+ });
764
+ continue;
765
+ }
766
+
767
+ if (token.startsWith("</")) {
768
+ const node = stack.pop();
769
+ if (!node) {
770
+ throw new Error("Malformed XML: unexpected closing tag.");
771
+ }
772
+ node.end = end;
773
+ continue;
774
+ }
775
+
776
+ if (token.startsWith("<")) {
777
+ const selfClosing = /\/>$/.test(token);
778
+ const tagBody = token.slice(1, token.length - (selfClosing ? 2 : 1)).trim();
779
+ const { name, attributes } = parseTag(tagBody);
780
+ const node: XmlElementNode = {
781
+ type: "element",
782
+ name,
783
+ attributes,
784
+ children: [],
785
+ start,
786
+ end,
787
+ };
788
+ stack[stack.length - 1]?.children.push(node);
789
+ if (!selfClosing) {
790
+ stack.push(node);
791
+ }
792
+ continue;
793
+ }
794
+
795
+ const text = decodeXmlText(token);
796
+ if (text.length > 0) {
797
+ stack[stack.length - 1]?.children.push({
798
+ type: "text",
799
+ text,
800
+ start,
801
+ end,
802
+ });
803
+ }
804
+ }
805
+
806
+ if (stack.length !== 1) {
807
+ throw new Error("Malformed XML: unclosed tag.");
808
+ }
809
+
810
+ return root;
811
+ }
812
+
813
+ function parseTag(tagBody: string): { name: string; attributes: Record<string, string> } {
814
+ const whitespaceIndex = tagBody.search(/\s/u);
815
+ const name = whitespaceIndex === -1 ? tagBody : tagBody.slice(0, whitespaceIndex);
816
+ const rawAttributes = whitespaceIndex === -1 ? "" : tagBody.slice(whitespaceIndex + 1);
817
+ const attributes: Record<string, string> = {};
818
+ const pattern = /([A-Za-z_][A-Za-z0-9:._-]*)\s*=\s*("([^"]*)"|'([^']*)')/gu;
819
+
820
+ for (const match of rawAttributes.matchAll(pattern)) {
821
+ const key = match[1];
822
+ const value = match[3] ?? match[4] ?? "";
823
+ if (key) {
824
+ attributes[key] = decodeXmlText(value);
825
+ }
826
+ }
827
+
828
+ return { name, attributes };
829
+ }
830
+
831
+ function findChildElement(node: XmlElementNode, name: string): XmlElementNode {
832
+ const match = node.children.find(
833
+ (child): child is XmlElementNode =>
834
+ child.type === "element" && localName(child.name) === name,
835
+ );
836
+
837
+ if (!match) {
838
+ throw new Error(`Expected XML element ${name}.`);
839
+ }
840
+
841
+ return match;
842
+ }
843
+
844
+ function localName(name: string): string {
845
+ const index = name.indexOf(":");
846
+ return index === -1 ? name : name.slice(index + 1);
847
+ }
848
+
849
+ function decodeXmlText(text: string): string {
850
+ return text.replace(
851
+ /&(?:#x([0-9A-Fa-f]+)|#([0-9]+)|([A-Za-z]+));/gu,
852
+ (_, hex, dec, named) => {
853
+ if (hex) {
854
+ return String.fromCodePoint(Number.parseInt(hex, 16));
855
+ }
856
+ if (dec) {
857
+ return String.fromCodePoint(Number.parseInt(dec, 10));
858
+ }
859
+
860
+ switch (named) {
861
+ case "amp":
862
+ return "&";
863
+ case "lt":
864
+ return "<";
865
+ case "gt":
866
+ return ">";
867
+ case "quot":
868
+ return "\"";
869
+ case "apos":
870
+ return "'";
871
+ default:
872
+ return `&${named};`;
873
+ }
874
+ },
875
+ );
876
+ }