@beyondwork/docx-react-component 1.0.11 → 1.0.13

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 (40) hide show
  1. package/README.md +8 -2
  2. package/package.json +35 -21
  3. package/src/api/public-types.ts +103 -1
  4. package/src/core/commands/formatting-commands.ts +742 -0
  5. package/src/core/commands/image-commands.ts +84 -2
  6. package/src/core/commands/structural-helpers.ts +309 -0
  7. package/src/core/commands/table-structure-commands.ts +721 -0
  8. package/src/core/commands/text-commands.ts +166 -1
  9. package/src/core/state/editor-state.ts +318 -9
  10. package/src/formats/xlsx/io/parse-sheet.ts +177 -7
  11. package/src/formats/xlsx/io/parse-styles.ts +2 -0
  12. package/src/formats/xlsx/io/xlsx-session.ts +18 -12
  13. package/src/formats/xlsx/model/sheet.ts +81 -1
  14. package/src/formats/xlsx/model/workbook.ts +10 -6
  15. package/src/io/docx-session.ts +392 -22
  16. package/src/io/export/export-session.ts +55 -0
  17. package/src/io/export/serialize-footnotes.ts +5 -20
  18. package/src/io/export/serialize-headers-footers.ts +5 -31
  19. package/src/io/export/serialize-main-document.ts +78 -5
  20. package/src/io/normalize/normalize-text.ts +90 -1
  21. package/src/io/ooxml/parse-footnotes.ts +68 -5
  22. package/src/io/ooxml/parse-headers-footers.ts +67 -9
  23. package/src/io/ooxml/parse-main-document.ts +169 -6
  24. package/src/io/opc/package-reader.ts +3 -3
  25. package/src/io/source-package-provenance.ts +241 -0
  26. package/src/model/canonical-document.ts +450 -2
  27. package/src/model/cds-1.0.0.ts +5 -2
  28. package/src/model/snapshot.ts +190 -19
  29. package/src/preservation/package-preservation.ts +0 -7
  30. package/src/runtime/document-runtime.ts +7 -1
  31. package/src/runtime/read-only-diagnostics-runtime.ts +1 -1
  32. package/src/runtime/surface-projection.ts +200 -17
  33. package/src/runtime/table-commands.ts +79 -0
  34. package/src/runtime/table-schema.ts +9 -0
  35. package/src/ui/WordReviewEditor.tsx +708 -16
  36. package/src/ui-tailwind/editor-surface/pm-schema.ts +121 -5
  37. package/src/ui-tailwind/editor-surface/pm-state-from-snapshot.ts +73 -7
  38. package/src/ui-tailwind/editor-surface/search-plugin.ts +76 -16
  39. package/src/ui-tailwind/editor-surface/tw-prosemirror-surface.tsx +162 -14
  40. package/src/validation/compatibility-engine.ts +208 -0
@@ -8,7 +8,11 @@ import {
8
8
  import { createSelectionSnapshot, type CanonicalDocumentEnvelope, type SelectionSnapshot } from "../state/editor-state.ts";
9
9
  import { TextTransactionError } from "../state/text-transaction.ts";
10
10
  import type { TransactionMapping } from "../selection/mapping.ts";
11
- import type { MediaItem } from "../../model/canonical-document.ts";
11
+ import type { MediaItem, ParagraphNode } from "../../model/canonical-document.ts";
12
+ import {
13
+ replaceParagraphScope,
14
+ resolveParagraphScope,
15
+ } from "./structural-helpers.ts";
12
16
 
13
17
  export interface ImageCommandContext {
14
18
  timestamp: string;
@@ -33,7 +37,85 @@ export interface InsertImageResult {
33
37
  export function insertImage(
34
38
  document: CanonicalDocumentEnvelope,
35
39
  selection: SelectionSnapshot,
36
- data: Uint8Array | string,
40
+ _data: Uint8Array | string,
41
+ mimeType: string,
42
+ _width?: number,
43
+ _height?: number,
44
+ context: ImageCommandContext = { timestamp: new Date().toISOString() },
45
+ ): InsertImageResult {
46
+ const scope = resolveParagraphScope(document, selection);
47
+ if (!scope) {
48
+ throw new TextTransactionError(
49
+ "unsupported_content",
50
+ "Image insertion is only supported when the selection stays within one paragraph-backed scope.",
51
+ );
52
+ }
53
+
54
+ const localDocument: CanonicalDocumentEnvelope = {
55
+ ...document,
56
+ content: {
57
+ type: "doc",
58
+ children: [scope.paragraph],
59
+ },
60
+ };
61
+ const localSelection = createSelectionSnapshot(
62
+ selection.anchor - scope.paragraphStart,
63
+ selection.head - scope.paragraphStart,
64
+ );
65
+ const localResult = insertImageIntoParagraph(
66
+ localDocument,
67
+ localSelection,
68
+ _data,
69
+ mimeType,
70
+ _width,
71
+ _height,
72
+ context,
73
+ );
74
+ const nextDocument = replaceParagraphScope(
75
+ {
76
+ ...document,
77
+ media: localResult.document.media,
78
+ },
79
+ scope,
80
+ [
81
+ {
82
+ ...scope.paragraph,
83
+ children:
84
+ localResult.document.content.type === "doc"
85
+ ? (localResult.document.content.children.find(
86
+ (block): block is ParagraphNode => block.type === "paragraph",
87
+ )?.children ?? scope.paragraph.children)
88
+ : scope.paragraph.children,
89
+ },
90
+ ],
91
+ );
92
+
93
+ return {
94
+ ...localResult,
95
+ document: {
96
+ ...nextDocument,
97
+ updatedAt: context.timestamp,
98
+ media: localResult.document.media,
99
+ },
100
+ selection: createSelectionSnapshot(
101
+ localResult.selection.anchor + scope.paragraphStart,
102
+ localResult.selection.head + scope.paragraphStart,
103
+ ),
104
+ mapping: {
105
+ ...localResult.mapping,
106
+ steps: localResult.mapping.steps.map((step) => ({
107
+ ...step,
108
+ from: step.from + scope.paragraphStart,
109
+ to: step.to + scope.paragraphStart,
110
+ })),
111
+ },
112
+ };
113
+ }
114
+
115
+ function insertImageIntoParagraph(
116
+ document: CanonicalDocumentEnvelope,
117
+ selection: SelectionSnapshot,
118
+ _data: Uint8Array | string,
37
119
  mimeType: string,
38
120
  width?: number,
39
121
  height?: number,
@@ -0,0 +1,309 @@
1
+ import {
2
+ createSelectionSnapshot,
3
+ type CanonicalDocumentEnvelope,
4
+ type SelectionSnapshot,
5
+ } from "../state/editor-state.ts";
6
+ import type { TransactionMapping } from "../selection/mapping.ts";
7
+ import type {
8
+ DocumentRootNode,
9
+ ParagraphNode,
10
+ TableCellNode,
11
+ TableNode,
12
+ TableRowNode,
13
+ } from "../../model/canonical-document.ts";
14
+ import { createEditorSurfaceSnapshot } from "../../runtime/surface-projection.ts";
15
+
16
+ export interface TopLevelParagraphScope {
17
+ kind: "top-level";
18
+ blockIndex: number;
19
+ paragraph: ParagraphNode;
20
+ paragraphStart: number;
21
+ }
22
+
23
+ export interface TableParagraphScope {
24
+ kind: "table-cell";
25
+ tableBlockIndex: number;
26
+ rowIndex: number;
27
+ cellIndex: number;
28
+ childIndex: number;
29
+ paragraph: ParagraphNode;
30
+ paragraphStart: number;
31
+ }
32
+
33
+ export type ParagraphScope = TopLevelParagraphScope | TableParagraphScope;
34
+
35
+ export interface StructuralMutationResult {
36
+ changed: boolean;
37
+ document: CanonicalDocumentEnvelope;
38
+ selection: SelectionSnapshot;
39
+ mapping?: TransactionMapping;
40
+ }
41
+
42
+ const DEFAULT_TABLE_COLUMN_WIDTH = 2400;
43
+
44
+ export function createNoopStructuralMutation(
45
+ document: CanonicalDocumentEnvelope,
46
+ selection: SelectionSnapshot,
47
+ ): StructuralMutationResult {
48
+ return {
49
+ changed: false,
50
+ document,
51
+ selection,
52
+ };
53
+ }
54
+
55
+ export function resolveParagraphScope(
56
+ document: CanonicalDocumentEnvelope,
57
+ selection: SelectionSnapshot,
58
+ ): ParagraphScope | null {
59
+ const root = asDocumentRoot(document);
60
+ if (!root) {
61
+ return null;
62
+ }
63
+
64
+ const surface = createEditorSurfaceSnapshot(document, selection);
65
+ const selectionFrom = Math.min(selection.anchor, selection.head);
66
+ const selectionTo = Math.max(selection.anchor, selection.head);
67
+
68
+ for (let blockIndex = 0; blockIndex < root.children.length; blockIndex += 1) {
69
+ const block = root.children[blockIndex];
70
+ const surfaceBlock = surface.blocks[blockIndex];
71
+
72
+ if (!surfaceBlock) {
73
+ continue;
74
+ }
75
+
76
+ if (
77
+ block.type === "paragraph" &&
78
+ surfaceBlock.kind === "paragraph" &&
79
+ selectionWithinRange(selectionFrom, selectionTo, surfaceBlock.from, surfaceBlock.to)
80
+ ) {
81
+ return {
82
+ kind: "top-level",
83
+ blockIndex,
84
+ paragraph: cloneParagraph(block),
85
+ paragraphStart: surfaceBlock.from,
86
+ };
87
+ }
88
+
89
+ if (block.type !== "table" || surfaceBlock.kind !== "table") {
90
+ continue;
91
+ }
92
+
93
+ for (let rowIndex = 0; rowIndex < block.rows.length; rowIndex += 1) {
94
+ const row = block.rows[rowIndex];
95
+ const surfaceRow = surfaceBlock.rows[rowIndex];
96
+ if (!surfaceRow) {
97
+ continue;
98
+ }
99
+
100
+ for (let cellIndex = 0; cellIndex < row.cells.length; cellIndex += 1) {
101
+ const cell = row.cells[cellIndex];
102
+ const surfaceCell = surfaceRow.cells[cellIndex];
103
+ if (!surfaceCell) {
104
+ continue;
105
+ }
106
+
107
+ for (let childIndex = 0; childIndex < cell.children.length; childIndex += 1) {
108
+ const child = cell.children[childIndex];
109
+ const surfaceChild = surfaceCell.content[childIndex];
110
+ if (
111
+ child?.type === "paragraph" &&
112
+ surfaceChild?.kind === "paragraph" &&
113
+ selectionWithinRange(selectionFrom, selectionTo, surfaceChild.from, surfaceChild.to)
114
+ ) {
115
+ return {
116
+ kind: "table-cell",
117
+ tableBlockIndex: blockIndex,
118
+ rowIndex,
119
+ cellIndex,
120
+ childIndex,
121
+ paragraph: cloneParagraph(child),
122
+ paragraphStart: surfaceChild.from,
123
+ };
124
+ }
125
+ }
126
+ }
127
+ }
128
+ }
129
+
130
+ return null;
131
+ }
132
+
133
+ export function replaceParagraphScope(
134
+ document: CanonicalDocumentEnvelope,
135
+ scope: ParagraphScope,
136
+ replacementParagraphs: ParagraphNode[],
137
+ ): CanonicalDocumentEnvelope {
138
+ const root = asDocumentRoot(document);
139
+ if (!root) {
140
+ return document;
141
+ }
142
+
143
+ const nextRoot: DocumentRootNode =
144
+ scope.kind === "top-level"
145
+ ? {
146
+ ...root,
147
+ children: [
148
+ ...root.children.slice(0, scope.blockIndex),
149
+ ...replacementParagraphs,
150
+ ...root.children.slice(scope.blockIndex + 1),
151
+ ],
152
+ }
153
+ : {
154
+ ...root,
155
+ children: root.children.map((child, blockIndex) => {
156
+ if (blockIndex !== scope.tableBlockIndex || child.type !== "table") {
157
+ return child;
158
+ }
159
+
160
+ return {
161
+ ...child,
162
+ rows: child.rows.map((row, rowIndex) => {
163
+ if (rowIndex !== scope.rowIndex) {
164
+ return row;
165
+ }
166
+
167
+ return {
168
+ ...row,
169
+ cells: row.cells.map((cell, cellIndex) => {
170
+ if (cellIndex !== scope.cellIndex) {
171
+ return cell;
172
+ }
173
+
174
+ return {
175
+ ...cell,
176
+ children: [
177
+ ...cell.children.slice(0, scope.childIndex),
178
+ ...replacementParagraphs,
179
+ ...cell.children.slice(scope.childIndex + 1),
180
+ ],
181
+ };
182
+ }),
183
+ };
184
+ }),
185
+ };
186
+ }),
187
+ };
188
+
189
+ return {
190
+ ...document,
191
+ content: nextRoot,
192
+ };
193
+ }
194
+
195
+ export function createEmptyParagraph(): ParagraphNode {
196
+ return {
197
+ type: "paragraph",
198
+ children: [],
199
+ };
200
+ }
201
+
202
+ export function createInsertedTableBlock(
203
+ rows: number,
204
+ columns: number,
205
+ styleId?: string,
206
+ ): TableNode {
207
+ const safeRows = Math.max(1, Math.floor(rows));
208
+ const safeColumns = Math.max(1, Math.floor(columns));
209
+ const nextRows: TableRowNode[] = [];
210
+
211
+ for (let rowIndex = 0; rowIndex < safeRows; rowIndex += 1) {
212
+ const cells: TableCellNode[] = [];
213
+ for (let columnIndex = 0; columnIndex < safeColumns; columnIndex += 1) {
214
+ cells.push({
215
+ type: "table_cell",
216
+ children: [createEmptyParagraph()],
217
+ });
218
+ }
219
+ nextRows.push({
220
+ type: "table_row",
221
+ cells,
222
+ });
223
+ }
224
+
225
+ return {
226
+ type: "table",
227
+ ...(styleId ? { styleId } : {}),
228
+ gridColumns: Array.from({ length: safeColumns }, () => DEFAULT_TABLE_COLUMN_WIDTH),
229
+ rows: nextRows,
230
+ };
231
+ }
232
+
233
+ export function resolveInsertedTableStyleId(
234
+ document: CanonicalDocumentEnvelope,
235
+ ): string | undefined {
236
+ const styles = document.styles?.tables;
237
+ if (!styles || typeof styles !== "object") {
238
+ return undefined;
239
+ }
240
+ return "TableGrid" in styles ? "TableGrid" : undefined;
241
+ }
242
+
243
+ export function findTableCellParagraphSelection(
244
+ document: CanonicalDocumentEnvelope,
245
+ tableBlockIndex: number,
246
+ rowIndex: number,
247
+ cellIndex: number,
248
+ ): SelectionSnapshot | null {
249
+ const surface = createEditorSurfaceSnapshot(document, createSelectionSnapshot(0, 0));
250
+ const tableBlock = surface.blocks[tableBlockIndex];
251
+ if (!tableBlock || tableBlock.kind !== "table") {
252
+ return null;
253
+ }
254
+
255
+ const paragraph = tableBlock.rows[rowIndex]?.cells[cellIndex]?.content.find(
256
+ (block) => block.kind === "paragraph",
257
+ );
258
+ if (!paragraph || paragraph.kind !== "paragraph") {
259
+ return null;
260
+ }
261
+
262
+ return createSelectionSnapshot(paragraph.from, paragraph.from);
263
+ }
264
+
265
+ export function findTopLevelParagraphSelectionNearBlock(
266
+ document: CanonicalDocumentEnvelope,
267
+ preferredBlockIndex: number,
268
+ ): SelectionSnapshot | null {
269
+ const surface = createEditorSurfaceSnapshot(document, createSelectionSnapshot(0, 0));
270
+
271
+ for (let index = preferredBlockIndex; index < surface.blocks.length; index += 1) {
272
+ const block = surface.blocks[index];
273
+ if (block?.kind === "paragraph") {
274
+ return createSelectionSnapshot(block.from, block.from);
275
+ }
276
+ }
277
+
278
+ for (let index = Math.min(preferredBlockIndex - 1, surface.blocks.length - 1); index >= 0; index -= 1) {
279
+ const block = surface.blocks[index];
280
+ if (block?.kind === "paragraph") {
281
+ return createSelectionSnapshot(block.from, block.from);
282
+ }
283
+ }
284
+
285
+ return null;
286
+ }
287
+
288
+ function asDocumentRoot(
289
+ document: CanonicalDocumentEnvelope,
290
+ ): DocumentRootNode | null {
291
+ const root = document.content;
292
+ if (!root || root.type !== "doc" || !Array.isArray(root.children)) {
293
+ return null;
294
+ }
295
+ return root;
296
+ }
297
+
298
+ function selectionWithinRange(
299
+ selectionFrom: number,
300
+ selectionTo: number,
301
+ rangeFrom: number,
302
+ rangeTo: number,
303
+ ): boolean {
304
+ return selectionFrom >= rangeFrom && selectionTo <= rangeTo;
305
+ }
306
+
307
+ function cloneParagraph(paragraph: ParagraphNode): ParagraphNode {
308
+ return structuredClone(paragraph) as ParagraphNode;
309
+ }