@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,232 @@
1
+ import type {
2
+ CompatibilityReport,
3
+ EditorWarning,
4
+ ExportDocxOptions,
5
+ ExportResult,
6
+ PersistedEditorSnapshot,
7
+ RuntimeRenderSnapshot,
8
+ } from "../api/public-types.ts";
9
+ import { createCanonicalDocumentId } from "../core/state/editor-state.ts";
10
+ import type { ImportDiagnosticsResult } from "../validation/import-diagnostics.ts";
11
+
12
+ export interface ReadOnlyDiagnosticsRuntime {
13
+ getRenderSnapshot(): RuntimeRenderSnapshot;
14
+ getPersistedSnapshot(): PersistedEditorSnapshot;
15
+ getCompatibilityReport(): CompatibilityReport;
16
+ getWarnings(): EditorWarning[];
17
+ dispatch(command: { type: string }): never;
18
+ undo(): never;
19
+ redo(): never;
20
+ exportDocx(options?: ExportDocxOptions): Promise<ExportResult>;
21
+ }
22
+
23
+ export function createReadOnlyDiagnosticsRuntime(input: {
24
+ documentId: string;
25
+ sourceLabel?: string;
26
+ editorBuild: string;
27
+ generatedAt: string;
28
+ diagnostics: ImportDiagnosticsResult;
29
+ }): ReadOnlyDiagnosticsRuntime {
30
+ const persistedSnapshot = createDiagnosticsSnapshot(input);
31
+ const renderSnapshot = createDiagnosticsRenderSnapshot(input, persistedSnapshot);
32
+
33
+ return {
34
+ getRenderSnapshot() {
35
+ return renderSnapshot;
36
+ },
37
+ getPersistedSnapshot() {
38
+ return persistedSnapshot;
39
+ },
40
+ getCompatibilityReport() {
41
+ return persistedSnapshot.compatibility;
42
+ },
43
+ getWarnings() {
44
+ return persistedSnapshot.warningLog;
45
+ },
46
+ dispatch(command) {
47
+ throw createReadOnlyDiagnosticsModeError(command.type);
48
+ },
49
+ undo() {
50
+ throw createReadOnlyDiagnosticsModeError("history.undo");
51
+ },
52
+ redo() {
53
+ throw createReadOnlyDiagnosticsModeError("history.redo");
54
+ },
55
+ async exportDocx(options) {
56
+ void options;
57
+ throw new Error(
58
+ "DOCX export is blocked in read-only diagnostics mode because the imported package is not safe to rewrite.",
59
+ );
60
+ },
61
+ };
62
+ }
63
+
64
+ function createDiagnosticsSnapshot(input: {
65
+ documentId: string;
66
+ editorBuild: string;
67
+ generatedAt: string;
68
+ diagnostics: ImportDiagnosticsResult;
69
+ }): PersistedEditorSnapshot {
70
+ const docId = createCanonicalDocumentId(input.documentId);
71
+ const diagnosticId = `diagnostic:${sanitizeDiagnosticsToken(input.documentId)}`;
72
+
73
+ return {
74
+ snapshotVersion: "persisted-editor-snapshot/1",
75
+ schemaVersion: "cds/1.0.0",
76
+ documentId: input.documentId,
77
+ docId,
78
+ createdAt: input.generatedAt,
79
+ updatedAt: input.generatedAt,
80
+ savedAt: input.generatedAt,
81
+ editorBuild: input.editorBuild,
82
+ canonicalDocument: {
83
+ schemaVersion: "cds/1.0.0",
84
+ docId,
85
+ createdAt: input.generatedAt,
86
+ updatedAt: input.generatedAt,
87
+ metadata: {
88
+ customProperties: {},
89
+ importMode: input.diagnostics.mode,
90
+ },
91
+ styles: {
92
+ paragraphs: {},
93
+ characters: {},
94
+ tables: {},
95
+ },
96
+ numbering: {
97
+ abstractDefinitions: {},
98
+ instances: {},
99
+ },
100
+ media: {
101
+ items: {},
102
+ },
103
+ content: {
104
+ type: "doc",
105
+ children: [{ type: "paragraph", children: [] }],
106
+ },
107
+ review: {
108
+ comments: {},
109
+ revisions: {},
110
+ },
111
+ preservation: {
112
+ opaqueFragments: {},
113
+ packageParts: {},
114
+ },
115
+ diagnostics: {
116
+ warnings: [],
117
+ errors: [
118
+ {
119
+ diagnosticId,
120
+ code: input.diagnostics.fatalError.code,
121
+ message: input.diagnostics.fatalError.message,
122
+ isFatal: input.diagnostics.fatalError.isFatal,
123
+ source: input.diagnostics.fatalError.source,
124
+ details: input.diagnostics.fatalError.details,
125
+ },
126
+ ],
127
+ },
128
+ },
129
+ compatibility: {
130
+ reportVersion: "compatibility-report/1",
131
+ generatedAt: input.generatedAt,
132
+ blockExport: true,
133
+ featureEntries: [...input.diagnostics.diagnostics.featureEntries],
134
+ warnings: [...input.diagnostics.diagnostics.warnings],
135
+ errors: [...input.diagnostics.diagnostics.errors],
136
+ },
137
+ warningLog: [],
138
+ };
139
+ }
140
+
141
+ function createDiagnosticsRenderSnapshot(
142
+ input: {
143
+ documentId: string;
144
+ sourceLabel?: string;
145
+ diagnostics: ImportDiagnosticsResult;
146
+ },
147
+ persistedSnapshot: PersistedEditorSnapshot,
148
+ ): RuntimeRenderSnapshot {
149
+ return {
150
+ documentId: input.documentId,
151
+ sessionId: `${input.documentId}-diagnostics`,
152
+ sourceLabel: input.sourceLabel,
153
+ revisionToken: `${input.documentId}:diagnostics`,
154
+ isReady: true,
155
+ isDirty: false,
156
+ readOnly: true,
157
+ selection: collapsedSelection(),
158
+ documentStats: {
159
+ storyLength: 0,
160
+ commentCount: 0,
161
+ revisionCount: 0,
162
+ opaqueFragmentCount: 0,
163
+ },
164
+ comments: {
165
+ openCommentIds: [],
166
+ resolvedCommentIds: [],
167
+ detachedCommentIds: [],
168
+ totalCount: 0,
169
+ threads: [],
170
+ },
171
+ trackedChanges: {
172
+ pendingChangeIds: [],
173
+ acceptedChangeIds: [],
174
+ rejectedChangeIds: [],
175
+ detachedChangeIds: [],
176
+ actionableChangeIds: [],
177
+ preserveOnlyChangeIds: [],
178
+ totalCount: 0,
179
+ revisions: [],
180
+ },
181
+ compatibility: {
182
+ blockExport: true,
183
+ blockExportReasons: [persistedSnapshot.compatibility.errors[0]?.message ?? input.diagnostics.featureEntry.message],
184
+ warningCount: persistedSnapshot.compatibility.warnings.length,
185
+ errorCount: persistedSnapshot.compatibility.errors.length,
186
+ featureEntries: [...persistedSnapshot.compatibility.featureEntries],
187
+ },
188
+ warnings: [],
189
+ fatalError: input.diagnostics.fatalError,
190
+ commandState: {
191
+ canUndo: false,
192
+ canRedo: false,
193
+ readOnly: true,
194
+ },
195
+ };
196
+ }
197
+
198
+ function collapsedSelection(): RuntimeRenderSnapshot["selection"] {
199
+ return {
200
+ anchor: 0,
201
+ head: 0,
202
+ isCollapsed: true,
203
+ activeRange: {
204
+ kind: "range",
205
+ from: 0,
206
+ to: 0,
207
+ assoc: {
208
+ start: -1,
209
+ end: 1,
210
+ },
211
+ },
212
+ };
213
+ }
214
+
215
+ function createReadOnlyDiagnosticsModeError(commandType: string): Error {
216
+ const error = new Error(
217
+ `Command ${commandType} is blocked in read-only diagnostics mode.`,
218
+ );
219
+ Object.assign(error, {
220
+ code: "import_failed",
221
+ details: {
222
+ mode: "read-only-diagnostics",
223
+ commandType,
224
+ },
225
+ });
226
+ return error;
227
+ }
228
+
229
+ function sanitizeDiagnosticsToken(documentId: string): string {
230
+ const collapsed = documentId.replace(/[^A-Za-z0-9._-]/g, "-");
231
+ return collapsed.length > 0 ? `read-only-${collapsed}` : "read-only-diagnostics";
232
+ }
@@ -0,0 +1,44 @@
1
+ import type {
2
+ CommentThreadRecord,
3
+ EditorState,
4
+ EditorWarning,
5
+ } from "../core/state/editor-state.ts";
6
+ import type { TransactionMapping } from "../core/selection/mapping.ts";
7
+ import { remapCommentThreads } from "../review/store/comment-remapping.ts";
8
+
9
+ export interface ApplyReviewRuntimeMappingOptions {
10
+ state: Pick<EditorState, "document" | "warnings" | "runtime">;
11
+ mapping: TransactionMapping;
12
+ nextContent: unknown;
13
+ }
14
+
15
+ export interface ReviewRuntimeMappingResult {
16
+ comments: Record<string, CommentThreadRecord>;
17
+ warnings: EditorWarning[];
18
+ activeCommentId?: string;
19
+ detachedCommentIds: string[];
20
+ }
21
+
22
+ export function applyReviewRuntimeMapping(
23
+ options: ApplyReviewRuntimeMappingOptions,
24
+ ): ReviewRuntimeMappingResult {
25
+ const remapped = remapCommentThreads({
26
+ comments: options.state.document.review.comments,
27
+ mapping: options.mapping,
28
+ nextContent: options.nextContent,
29
+ existingWarnings: options.state.warnings,
30
+ });
31
+
32
+ const activeCommentId =
33
+ options.state.runtime.activeCommentId &&
34
+ remapped.comments[options.state.runtime.activeCommentId]
35
+ ? options.state.runtime.activeCommentId
36
+ : undefined;
37
+
38
+ return {
39
+ comments: remapped.comments,
40
+ warnings: remapped.warnings,
41
+ activeCommentId,
42
+ detachedCommentIds: remapped.detachedCommentIds,
43
+ };
44
+ }
@@ -0,0 +1,107 @@
1
+ import type { CanonicalDocumentEnvelope } from "../core/state/editor-state.ts";
2
+ import {
3
+ getReviewCommandIntent,
4
+ isSingleRevisionReviewCommand,
5
+ type ReviewCommand,
6
+ } from "../core/commands/review-commands.ts";
7
+ import type { TransactionMapping } from "../core/selection/mapping.ts";
8
+ import {
9
+ applyRevisionAction,
10
+ type RevisionActionOutcome,
11
+ } from "../review/store/revision-actions.ts";
12
+ import { type RevisionStore } from "../review/store/revision-store.ts";
13
+
14
+ export interface RevisionRuntimeState {
15
+ document: CanonicalDocumentEnvelope;
16
+ store: RevisionStore;
17
+ }
18
+
19
+ export interface ApplyRevisionRuntimeCommandOptions {
20
+ state: RevisionRuntimeState;
21
+ command: ReviewCommand;
22
+ timestamp: string;
23
+ }
24
+
25
+ export interface RevisionRuntimeCommandEffects {
26
+ appliedRevisionIds: string[];
27
+ skippedRevisions: RevisionActionOutcome[];
28
+ detachedRevisionIds: string[];
29
+ }
30
+
31
+ export interface RevisionRuntimeCommandResult extends RevisionRuntimeState {
32
+ outcomes: RevisionActionOutcome[];
33
+ mappings: Array<{ revisionId: string; mapping: TransactionMapping; steps: number }>;
34
+ effects: RevisionRuntimeCommandEffects;
35
+ }
36
+
37
+ export function applyRevisionRuntimeCommand(
38
+ options: ApplyRevisionRuntimeCommandOptions,
39
+ ): RevisionRuntimeCommandResult {
40
+ const revisionIds = isSingleRevisionReviewCommand(options.command)
41
+ ? [options.command.revisionId]
42
+ : listBatchRevisionIds(options.state.store);
43
+
44
+ const outcomes: RevisionActionOutcome[] = [];
45
+ const mappings: Array<{ revisionId: string; steps: number }> = [];
46
+ const appliedRevisionIds: string[] = [];
47
+ const detachedRevisionIds = new Set<string>();
48
+
49
+ let state = options.state;
50
+
51
+ for (const revisionId of revisionIds) {
52
+ const result = applyRevisionAction({
53
+ document: state.document,
54
+ store: state.store,
55
+ revisionId,
56
+ intent: getReviewCommandIntent(options.command),
57
+ timestamp: options.timestamp,
58
+ });
59
+
60
+ state = {
61
+ document: result.document,
62
+ store: result.store,
63
+ };
64
+ outcomes.push(result.outcome);
65
+ mappings.push({
66
+ revisionId,
67
+ mapping: result.mapping,
68
+ steps: result.mapping.steps.length,
69
+ });
70
+
71
+ if (result.outcome.kind === "applied") {
72
+ appliedRevisionIds.push(revisionId);
73
+ }
74
+
75
+ for (const detachedRevisionId of result.detachedRevisionIds) {
76
+ detachedRevisionIds.add(detachedRevisionId);
77
+ }
78
+ }
79
+
80
+ return {
81
+ ...state,
82
+ outcomes,
83
+ mappings,
84
+ effects: {
85
+ appliedRevisionIds,
86
+ skippedRevisions: outcomes.filter(
87
+ (outcome): outcome is Extract<RevisionActionOutcome, { kind: "skipped" }> =>
88
+ outcome.kind === "skipped",
89
+ ),
90
+ detachedRevisionIds: [...detachedRevisionIds],
91
+ },
92
+ };
93
+ }
94
+
95
+ function listBatchRevisionIds(store: RevisionStore): string[] {
96
+ return Object.values(store.revisions)
97
+ .filter((revision) => revision.status === "active")
98
+ .sort((left, right) => {
99
+ const createdAtDelta = left.createdAt.localeCompare(right.createdAt);
100
+ if (createdAtDelta !== 0) {
101
+ return createdAtDelta;
102
+ }
103
+
104
+ return left.revisionId.localeCompare(right.revisionId);
105
+ })
106
+ .map((revision) => revision.revisionId);
107
+ }
@@ -0,0 +1,138 @@
1
+ import type { RuntimeRenderSnapshot } from "../api/public-types";
2
+
3
+ /**
4
+ * Session capabilities derived from the runtime snapshot.
5
+ *
6
+ * All fields are computed from `RuntimeRenderSnapshot` — no local component
7
+ * state. The UI reads these to decide what controls to enable/disable, what
8
+ * chrome to show, and what mode the editor is in.
9
+ */
10
+ export interface SessionCapabilities {
11
+ // ── Session phase ──
12
+ /** Current lifecycle phase of the editor session. */
13
+ phase: "loading" | "ready" | "diagnostics" | "error";
14
+
15
+ /** Effective editor mode after accounting for runtime state. */
16
+ mode: "editing" | "review" | "read-only-diagnostics";
17
+
18
+ // ── Command capabilities ──
19
+ canUndo: boolean;
20
+ canRedo: boolean;
21
+ canEdit: boolean;
22
+ canAddComment: boolean;
23
+ canExport: boolean;
24
+ canAcceptChange: boolean;
25
+ canRejectChange: boolean;
26
+ canAcceptAll: boolean;
27
+ canRejectAll: boolean;
28
+
29
+ // ── Review visibility ──
30
+ /** Whether the review rail should be visible by default. */
31
+ reviewRailVisible: boolean;
32
+
33
+ /** Whether tracked changes exist in the document (display toggle is meaningful). */
34
+ trackChangesSupported: boolean;
35
+
36
+ // ── Trust posture ──
37
+ exportBlocked: boolean;
38
+ preserveOnlyCount: number;
39
+ unsupportedFatalCount: number;
40
+
41
+ // ── Health ──
42
+ /** Total count of health issues (preserve-only + unsupported-fatal + warnings). */
43
+ healthIssueCount: number;
44
+
45
+ // ── Status ──
46
+ isDirty: boolean;
47
+ isReady: boolean;
48
+ hasFatalError: boolean;
49
+ }
50
+
51
+ /**
52
+ * Derive UI capabilities from the runtime snapshot and requested review mode.
53
+ *
54
+ * This is a pure function with no side effects. Call it on every render to
55
+ * get the current capability state.
56
+ */
57
+ export function deriveCapabilities(
58
+ snapshot: RuntimeRenderSnapshot,
59
+ reviewMode: "editing" | "review",
60
+ ): SessionCapabilities {
61
+ const hasFatalError = Boolean(snapshot.fatalError);
62
+ const isReady = snapshot.isReady;
63
+ const isReadOnly = snapshot.readOnly;
64
+ const exportBlocked = snapshot.compatibility.blockExport;
65
+
66
+ // Phase derivation
67
+ const phase: SessionCapabilities["phase"] = !isReady
68
+ ? "loading"
69
+ : hasFatalError
70
+ ? "diagnostics"
71
+ : "ready";
72
+
73
+ // Mode derivation: if diagnostics or read-only, override the requested mode
74
+ const mode: SessionCapabilities["mode"] =
75
+ phase === "diagnostics"
76
+ ? "read-only-diagnostics"
77
+ : reviewMode;
78
+
79
+ // Command capabilities
80
+ const canEdit = isReady && !isReadOnly && !hasFatalError;
81
+ const canUndo = snapshot.commandState.canUndo && canEdit;
82
+ const canRedo = snapshot.commandState.canRedo && canEdit;
83
+ const canAddComment = canEdit && !snapshot.selection.isCollapsed;
84
+ const canExport = isReady && !exportBlocked && !hasFatalError;
85
+
86
+ // Revision capabilities
87
+ const actionableRevisions = snapshot.trackedChanges.revisions.filter(
88
+ (r) => r.status === "active" && r.actionability === "actionable",
89
+ );
90
+ const canAcceptChange = canEdit && actionableRevisions.some((r) => r.canAccept);
91
+ const canRejectChange = canEdit && actionableRevisions.some((r) => r.canReject);
92
+ const canAcceptAll = canEdit && actionableRevisions.length > 0;
93
+ const canRejectAll = canEdit && actionableRevisions.length > 0;
94
+
95
+ // Trust posture (computed before review visibility since it depends on these)
96
+ const preserveOnlyCount = snapshot.compatibility.featureEntries.filter(
97
+ (e) => e.featureClass === "preserve-only",
98
+ ).length;
99
+ const unsupportedFatalCount = snapshot.compatibility.featureEntries.filter(
100
+ (e) => e.featureClass === "unsupported-fatal",
101
+ ).length;
102
+
103
+ // Review visibility
104
+ const trackChangesSupported = snapshot.trackedChanges.totalCount > 0;
105
+ // Rail visible in review/diagnostics mode always.
106
+ // In editing mode, rail stays visible when there are trust/health concerns
107
+ // that the user must be able to reach (per DESIGN.md: health/trust info
108
+ // stays reachable in both modes).
109
+ const hasTrustConcerns = exportBlocked || preserveOnlyCount > 0 || unsupportedFatalCount > 0
110
+ || snapshot.warnings.length > 0 || hasFatalError;
111
+ const reviewRailVisible = mode === "review" || mode === "read-only-diagnostics"
112
+ || (mode === "editing" && hasTrustConcerns);
113
+
114
+ const healthIssueCount = preserveOnlyCount + unsupportedFatalCount + snapshot.warnings.length;
115
+
116
+ return {
117
+ phase,
118
+ mode,
119
+ canUndo,
120
+ canRedo,
121
+ canEdit,
122
+ canAddComment,
123
+ canExport,
124
+ canAcceptChange,
125
+ canRejectChange,
126
+ canAcceptAll,
127
+ canRejectAll,
128
+ reviewRailVisible,
129
+ trackChangesSupported,
130
+ exportBlocked,
131
+ preserveOnlyCount,
132
+ unsupportedFatalCount,
133
+ healthIssueCount,
134
+ isDirty: snapshot.isDirty,
135
+ isReady,
136
+ hasFatalError,
137
+ };
138
+ }