@beyondwork/docx-react-component 1.0.1 → 1.0.2

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 +76 -46
  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 +320 -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 +1504 -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,160 @@
1
+ import {
2
+ type CompatibilityDiagnostics,
3
+ type CompatibilityFeatureClass,
4
+ type CompatibilityFeatureEntry,
5
+ type EditorError,
6
+ type EditorWarning,
7
+ createDiagnostics,
8
+ diagnosticsBlockExport,
9
+ summarizeFeatureClasses,
10
+ } from "./diagnostics.ts";
11
+
12
+ export const COMPATIBILITY_REPORT_VERSION = "compatibility-report/1" as const;
13
+
14
+ export const COMPATIBILITY_FEATURE_KEYS = [
15
+ "paragraphs",
16
+ "runs",
17
+ "whitespace",
18
+ "headings",
19
+ "lists",
20
+ "restart-numbering",
21
+ "tables",
22
+ "merged-cells",
23
+ "inline-images",
24
+ "hyperlinks",
25
+ "comments-single-paragraph",
26
+ "comment-threads",
27
+ "tracked-insertions",
28
+ "tracked-deletions",
29
+ "comments-multi-paragraph-ranges",
30
+ "tracked-moves",
31
+ "formatting-revisions",
32
+ "structural-revisions",
33
+ "sections",
34
+ "headers-footers",
35
+ "notes",
36
+ "fields",
37
+ "content-controls",
38
+ "custom-xml",
39
+ "alt-chunk",
40
+ "embedded-objects",
41
+ "floating-drawings",
42
+ "alternate-content",
43
+ "unknown-ooxml",
44
+ "unknown-package-parts",
45
+ "package-integrity",
46
+ "relationship-integrity",
47
+ ] as const;
48
+
49
+ export type CompatibilityFeatureKey =
50
+ (typeof COMPATIBILITY_FEATURE_KEYS)[number];
51
+
52
+ export interface CompatibilityReport<
53
+ FeatureKey extends string = CompatibilityFeatureKey,
54
+ FeatureClass extends CompatibilityFeatureClass = CompatibilityFeatureClass,
55
+ > extends CompatibilityDiagnostics<FeatureKey, FeatureClass> {
56
+ reportVersion: typeof COMPATIBILITY_REPORT_VERSION;
57
+ generatedAt: string;
58
+ blockExport: boolean;
59
+ }
60
+
61
+ export interface CreateCompatibilityReportInput<
62
+ FeatureKey extends string = CompatibilityFeatureKey,
63
+ FeatureClass extends CompatibilityFeatureClass = CompatibilityFeatureClass,
64
+ > extends Partial<CompatibilityDiagnostics<FeatureKey, FeatureClass>> {
65
+ generatedAt: string;
66
+ blockExport?: boolean;
67
+ }
68
+
69
+ export interface CompatibilityReportSummary {
70
+ generatedAt: string;
71
+ blockExport: boolean;
72
+ featureClassCounts: Record<CompatibilityFeatureClass, number>;
73
+ warningCount: number;
74
+ errorCount: number;
75
+ }
76
+
77
+ export function createCompatibilityReport<
78
+ FeatureKey extends string = CompatibilityFeatureKey,
79
+ FeatureClass extends CompatibilityFeatureClass = CompatibilityFeatureClass,
80
+ >(
81
+ input: CreateCompatibilityReportInput<FeatureKey, FeatureClass>,
82
+ ): CompatibilityReport<FeatureKey, FeatureClass> {
83
+ const diagnostics = createDiagnostics(input);
84
+
85
+ return Object.freeze({
86
+ reportVersion: COMPATIBILITY_REPORT_VERSION,
87
+ generatedAt: input.generatedAt,
88
+ blockExport: diagnosticsBlockExport(diagnostics, input.blockExport),
89
+ featureEntries: diagnostics.featureEntries,
90
+ warnings: diagnostics.warnings,
91
+ errors: diagnostics.errors,
92
+ });
93
+ }
94
+
95
+ export function mergeCompatibilityReports<
96
+ FeatureKey extends string = CompatibilityFeatureKey,
97
+ FeatureClass extends CompatibilityFeatureClass = CompatibilityFeatureClass,
98
+ >(
99
+ reports: readonly CompatibilityReport<FeatureKey, FeatureClass>[],
100
+ options: {
101
+ generatedAt: string;
102
+ blockExport?: boolean;
103
+ },
104
+ ): CompatibilityReport<FeatureKey, FeatureClass> {
105
+ return createCompatibilityReport({
106
+ generatedAt: options.generatedAt,
107
+ blockExport:
108
+ options.blockExport ?? reports.some((report) => report.blockExport),
109
+ featureEntries: flattenUnique(reports.flatMap((report) => report.featureEntries)),
110
+ warnings: flattenUnique(reports.flatMap((report) => report.warnings)),
111
+ errors: flattenUnique(reports.flatMap((report) => report.errors)),
112
+ });
113
+ }
114
+
115
+ export function summarizeCompatibilityReport<
116
+ FeatureKey extends string = CompatibilityFeatureKey,
117
+ FeatureClass extends CompatibilityFeatureClass = CompatibilityFeatureClass,
118
+ >(
119
+ report: CompatibilityReport<FeatureKey, FeatureClass>,
120
+ ): CompatibilityReportSummary {
121
+ return {
122
+ generatedAt: report.generatedAt,
123
+ blockExport: report.blockExport,
124
+ featureClassCounts: summarizeFeatureClasses(report.featureEntries),
125
+ warningCount: report.warnings.length,
126
+ errorCount: report.errors.length,
127
+ };
128
+ }
129
+
130
+ export function findFeatureEntry<
131
+ FeatureKey extends string = CompatibilityFeatureKey,
132
+ FeatureClass extends CompatibilityFeatureClass = CompatibilityFeatureClass,
133
+ >(
134
+ report: CompatibilityReport<FeatureKey, FeatureClass>,
135
+ featureEntryId: string,
136
+ ): CompatibilityFeatureEntry<FeatureKey, FeatureClass> | undefined {
137
+ return report.featureEntries.find(
138
+ (featureEntry) => featureEntry.featureEntryId === featureEntryId,
139
+ );
140
+ }
141
+
142
+ export type {
143
+ CompatibilityDiagnostics,
144
+ CompatibilityFeatureClass,
145
+ CompatibilityFeatureEntry,
146
+ EditorError,
147
+ EditorWarning,
148
+ } from "./diagnostics.ts";
149
+
150
+ function flattenUnique<Value extends { [key: string]: unknown }>(
151
+ items: readonly Value[],
152
+ ): readonly Value[] {
153
+ const deduped = new Map<string, Value>();
154
+
155
+ for (const item of items) {
156
+ deduped.set(JSON.stringify(item), item);
157
+ }
158
+
159
+ return Object.freeze([...deduped.values()]);
160
+ }
@@ -0,0 +1,203 @@
1
+ export const COMPATIBILITY_FEATURE_CLASSES = [
2
+ "supported-roundtrip",
3
+ "preserve-only",
4
+ "unsupported-fatal",
5
+ ] as const;
6
+
7
+ export type CompatibilityFeatureClass =
8
+ (typeof COMPATIBILITY_FEATURE_CLASSES)[number];
9
+
10
+ export const EDITOR_WARNING_CODES = [
11
+ "unsupported_ooxml_preserved",
12
+ "unsupported_ooxml_locked",
13
+ "import_normalized",
14
+ "export_roundtrip_risk",
15
+ "comment_anchor_detached",
16
+ "revision_anchor_detached",
17
+ "large_document_degraded",
18
+ "font_substitution",
19
+ "image_missing",
20
+ ] as const;
21
+
22
+ export type EditorWarningCode = (typeof EDITOR_WARNING_CODES)[number];
23
+
24
+ export const EDITOR_WARNING_SOURCES = [
25
+ "import",
26
+ "runtime",
27
+ "review",
28
+ "preservation",
29
+ "validation",
30
+ "export",
31
+ ] as const;
32
+
33
+ export type EditorWarningSource = (typeof EDITOR_WARNING_SOURCES)[number];
34
+
35
+ export const EDITOR_ERROR_CODES = [
36
+ "import_failed",
37
+ "export_failed",
38
+ "package_corrupt",
39
+ "validation_failed",
40
+ "datastore_failed",
41
+ "internal_invariant",
42
+ ] as const;
43
+
44
+ export type EditorErrorCode = (typeof EDITOR_ERROR_CODES)[number];
45
+
46
+ export const EDITOR_ERROR_SOURCES = [
47
+ "import",
48
+ "runtime",
49
+ "validation",
50
+ "datastore",
51
+ "export",
52
+ ] as const;
53
+
54
+ export type EditorErrorSource = (typeof EDITOR_ERROR_SOURCES)[number];
55
+
56
+ export type EditorAnchorProjection =
57
+ | {
58
+ kind: "range";
59
+ from: number;
60
+ to: number;
61
+ assoc: {
62
+ start: -1 | 1;
63
+ end: -1 | 1;
64
+ };
65
+ }
66
+ | {
67
+ kind: "node";
68
+ at: number;
69
+ assoc: -1 | 1;
70
+ }
71
+ | {
72
+ kind: "detached";
73
+ lastKnownRange: {
74
+ from: number;
75
+ to: number;
76
+ };
77
+ reason: "deleted" | "invalidatedByStructureChange" | "importAmbiguity";
78
+ };
79
+
80
+ export interface EditorWarning {
81
+ warningId: string;
82
+ code: EditorWarningCode;
83
+ severity: "info" | "warning";
84
+ message: string;
85
+ source: EditorWarningSource;
86
+ affectedAnchor?: EditorAnchorProjection;
87
+ featureEntryId?: string;
88
+ details?: Record<string, unknown>;
89
+ }
90
+
91
+ export interface EditorError {
92
+ errorId: string;
93
+ code: EditorErrorCode;
94
+ message: string;
95
+ isFatal: boolean;
96
+ source: EditorErrorSource;
97
+ details?: Record<string, unknown>;
98
+ }
99
+
100
+ export interface CompatibilityFeatureEntry<
101
+ FeatureKey extends string = string,
102
+ FeatureClass extends CompatibilityFeatureClass = CompatibilityFeatureClass,
103
+ > {
104
+ featureEntryId: string;
105
+ featureKey: FeatureKey;
106
+ featureClass: FeatureClass;
107
+ message: string;
108
+ affectedAnchor?: EditorAnchorProjection;
109
+ details?: Record<string, unknown>;
110
+ }
111
+
112
+ export interface CompatibilityDiagnostics<
113
+ FeatureKey extends string = string,
114
+ FeatureClass extends CompatibilityFeatureClass = CompatibilityFeatureClass,
115
+ > {
116
+ featureEntries: readonly CompatibilityFeatureEntry<FeatureKey, FeatureClass>[];
117
+ warnings: readonly EditorWarning[];
118
+ errors: readonly EditorError[];
119
+ }
120
+
121
+ const EXPORT_BLOCKING_ERROR_CODES = new Set<EditorErrorCode>([
122
+ "import_failed",
123
+ "export_failed",
124
+ "package_corrupt",
125
+ "validation_failed",
126
+ ]);
127
+
128
+ export function isCompatibilityFeatureClass(
129
+ value: string,
130
+ ): value is CompatibilityFeatureClass {
131
+ return COMPATIBILITY_FEATURE_CLASSES.includes(
132
+ value as CompatibilityFeatureClass,
133
+ );
134
+ }
135
+
136
+ export function isEditorWarningCode(value: string): value is EditorWarningCode {
137
+ return EDITOR_WARNING_CODES.includes(value as EditorWarningCode);
138
+ }
139
+
140
+ export function isEditorErrorCode(value: string): value is EditorErrorCode {
141
+ return EDITOR_ERROR_CODES.includes(value as EditorErrorCode);
142
+ }
143
+
144
+ export function createDiagnostics<
145
+ FeatureKey extends string = string,
146
+ FeatureClass extends CompatibilityFeatureClass = CompatibilityFeatureClass,
147
+ >(
148
+ input: Partial<CompatibilityDiagnostics<FeatureKey, FeatureClass>> = {},
149
+ ): CompatibilityDiagnostics<FeatureKey, FeatureClass> {
150
+ return {
151
+ featureEntries: freezeArray(input.featureEntries ?? []),
152
+ warnings: freezeArray(input.warnings ?? []),
153
+ errors: freezeArray(input.errors ?? []),
154
+ };
155
+ }
156
+
157
+ export function collectWarningIds(warnings: readonly EditorWarning[]): string[] {
158
+ return warnings.map((warning) => warning.warningId);
159
+ }
160
+
161
+ export function summarizeFeatureClasses<
162
+ FeatureKey extends string,
163
+ FeatureClass extends CompatibilityFeatureClass,
164
+ >(
165
+ featureEntries: readonly CompatibilityFeatureEntry<FeatureKey, FeatureClass>[],
166
+ ): Record<CompatibilityFeatureClass, number> {
167
+ return featureEntries.reduce<Record<CompatibilityFeatureClass, number>>(
168
+ (counts, entry) => {
169
+ counts[entry.featureClass] += 1;
170
+ return counts;
171
+ },
172
+ {
173
+ "supported-roundtrip": 0,
174
+ "preserve-only": 0,
175
+ "unsupported-fatal": 0,
176
+ },
177
+ );
178
+ }
179
+
180
+ export function diagnosticsBlockExport<
181
+ FeatureKey extends string,
182
+ FeatureClass extends CompatibilityFeatureClass,
183
+ >(
184
+ diagnostics: CompatibilityDiagnostics<FeatureKey, FeatureClass>,
185
+ explicitBlockExport = false,
186
+ ): boolean {
187
+ if (explicitBlockExport) {
188
+ return true;
189
+ }
190
+
191
+ return (
192
+ diagnostics.featureEntries.some(
193
+ (entry) => entry.featureClass === "unsupported-fatal",
194
+ ) ||
195
+ diagnostics.errors.some(
196
+ (error) => error.isFatal || EXPORT_BLOCKING_ERROR_CODES.has(error.code),
197
+ )
198
+ );
199
+ }
200
+
201
+ function freezeArray<Value>(items: readonly Value[]): readonly Value[] {
202
+ return Object.freeze([...items]);
203
+ }
@@ -0,0 +1,128 @@
1
+ import type {
2
+ CompatibilityFeatureEntry,
3
+ EditorError,
4
+ } from "../api/public-types.ts";
5
+ import {
6
+ createCorruptPackageError,
7
+ createCorruptPackageDiagnosticsToken,
8
+ type CorruptPackageIssue,
9
+ } from "../io/opc/corrupt-package.ts";
10
+ import {
11
+ createDiagnostics,
12
+ diagnosticsBlockExport,
13
+ type CompatibilityDiagnostics,
14
+ } from "./diagnostics.ts";
15
+
16
+ export const IMPORT_FAILURE_STAGES = ["package", "validation"] as const;
17
+
18
+ export type ImportFailureStage = (typeof IMPORT_FAILURE_STAGES)[number];
19
+
20
+ export interface ImportDiagnosticsResult {
21
+ mode: "read-only-diagnostics";
22
+ readOnly: true;
23
+ blockExport: true;
24
+ fatalError: EditorError;
25
+ diagnostics: CompatibilityDiagnostics;
26
+ featureEntry: CompatibilityFeatureEntry;
27
+ }
28
+
29
+ export function createPackageImportDiagnostics(input: {
30
+ issue: CorruptPackageIssue;
31
+ }): ImportDiagnosticsResult {
32
+ const fatalError = createCorruptPackageError(input.issue);
33
+ const featureEntry = {
34
+ featureEntryId: `feature:${fatalError.errorId}`,
35
+ featureKey: "malformed-package",
36
+ featureClass: "unsupported-fatal",
37
+ message: input.issue.message,
38
+ details: {
39
+ stage: "package",
40
+ reason: input.issue.reason,
41
+ diagnosticsToken: createCorruptPackageDiagnosticsToken(input.issue),
42
+ },
43
+ } satisfies CompatibilityFeatureEntry;
44
+
45
+ const diagnostics = createDiagnostics({
46
+ featureEntries: [featureEntry],
47
+ warnings: [],
48
+ errors: [fatalError],
49
+ });
50
+
51
+ return createReadOnlyDiagnosticsResult(featureEntry, diagnostics, fatalError);
52
+ }
53
+
54
+ export function createValidationImportDiagnostics(input: {
55
+ message: string;
56
+ source?: EditorError["source"];
57
+ details?: Record<string, unknown>;
58
+ }): ImportDiagnosticsResult {
59
+ const diagnosticsToken = createImportDiagnosticsToken("validation", input.message);
60
+ const fatalError: EditorError = {
61
+ errorId: `error:validation_failed:${diagnosticsToken}`,
62
+ code: "validation_failed",
63
+ message: input.message,
64
+ isFatal: true,
65
+ source: input.source ?? "validation",
66
+ details: {
67
+ stage: "validation",
68
+ diagnosticsToken,
69
+ ...(input.details ?? {}),
70
+ },
71
+ };
72
+ const featureEntry = {
73
+ featureEntryId: `feature:${fatalError.errorId}`,
74
+ featureKey: "openxml-validation",
75
+ featureClass: "unsupported-fatal",
76
+ message: input.message,
77
+ details: {
78
+ stage: "validation",
79
+ diagnosticsToken,
80
+ },
81
+ } satisfies CompatibilityFeatureEntry;
82
+ const diagnostics = createDiagnostics({
83
+ featureEntries: [featureEntry],
84
+ warnings: [],
85
+ errors: [fatalError],
86
+ });
87
+
88
+ return createReadOnlyDiagnosticsResult(featureEntry, diagnostics, fatalError);
89
+ }
90
+
91
+ export function getImportFailureStage(error: Pick<EditorError, "code" | "details">): ImportFailureStage {
92
+ if (error.code === "package_corrupt") {
93
+ return "package";
94
+ }
95
+
96
+ const stage = error.details?.stage;
97
+ return stage === "package" ? "package" : "validation";
98
+ }
99
+
100
+ function createReadOnlyDiagnosticsResult(
101
+ featureEntry: CompatibilityFeatureEntry,
102
+ diagnostics: CompatibilityDiagnostics,
103
+ fatalError: EditorError,
104
+ ): ImportDiagnosticsResult {
105
+ if (!diagnosticsBlockExport(diagnostics, true)) {
106
+ throw new Error("Import diagnostics must conservatively block export.");
107
+ }
108
+
109
+ return {
110
+ mode: "read-only-diagnostics",
111
+ readOnly: true,
112
+ blockExport: true,
113
+ fatalError,
114
+ diagnostics,
115
+ featureEntry,
116
+ };
117
+ }
118
+
119
+ function createImportDiagnosticsToken(stage: ImportFailureStage, message: string): string {
120
+ let hash = 2166136261;
121
+ const text = `${stage}:${message}`;
122
+ for (let index = 0; index < text.length; index += 1) {
123
+ hash ^= text.charCodeAt(index);
124
+ hash = Math.imul(hash, 16777619) >>> 0;
125
+ }
126
+
127
+ return `${stage}-${hash.toString(16).padStart(8, "0")}`;
128
+ }