@beyondwork/docx-react-component 1.0.27 → 1.0.29

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 (356) hide show
  1. package/dist/canonical-document-BLEbzL2J.d.cts +844 -0
  2. package/dist/canonical-document-BLEbzL2J.d.ts +844 -0
  3. package/dist/chunk-2FJS5GZM.js +763 -0
  4. package/dist/chunk-2FJS5GZM.js.map +1 -0
  5. package/{src/core/commands/section-layout-commands.ts → dist/chunk-2OQBZS3F.js} +106 -340
  6. package/dist/chunk-2OQBZS3F.js.map +1 -0
  7. package/dist/chunk-2S7W4KFO.js +127 -0
  8. package/dist/chunk-2S7W4KFO.js.map +1 -0
  9. package/dist/chunk-2TG72QSW.js +3874 -0
  10. package/dist/chunk-2TG72QSW.js.map +1 -0
  11. package/{src/core/commands/table-structure-commands.ts → dist/chunk-36QNIZBO.js} +126 -315
  12. package/dist/chunk-36QNIZBO.js.map +1 -0
  13. package/dist/chunk-4AQOYAW4.js +3069 -0
  14. package/dist/chunk-4AQOYAW4.js.map +1 -0
  15. package/dist/chunk-4D5EWJ3P.js +77 -0
  16. package/dist/chunk-4D5EWJ3P.js.map +1 -0
  17. package/dist/chunk-5FN54NDH.js +2257 -0
  18. package/dist/chunk-5FN54NDH.js.map +1 -0
  19. package/dist/chunk-BOYGQYRQ.js +7306 -0
  20. package/dist/chunk-BOYGQYRQ.js.map +1 -0
  21. package/dist/chunk-CN3XMECL.js +212 -0
  22. package/dist/chunk-CN3XMECL.js.map +1 -0
  23. package/dist/chunk-EBI3BX6U.js +164 -0
  24. package/dist/chunk-EBI3BX6U.js.map +1 -0
  25. package/dist/chunk-EILUG3VB.js +1275 -0
  26. package/dist/chunk-EILUG3VB.js.map +1 -0
  27. package/dist/chunk-FUDY333O.js +70 -0
  28. package/dist/chunk-FUDY333O.js.map +1 -0
  29. package/dist/chunk-GBVOWFIK.js +1237 -0
  30. package/dist/chunk-GBVOWFIK.js.map +1 -0
  31. package/dist/chunk-H4TQ3H3Y.js +262 -0
  32. package/dist/chunk-H4TQ3H3Y.js.map +1 -0
  33. package/{src/core/commands/style-commands.ts → dist/chunk-JGB3IXZO.js} +40 -113
  34. package/dist/chunk-JGB3IXZO.js.map +1 -0
  35. package/dist/chunk-KD2QRQPY.js +4342 -0
  36. package/dist/chunk-KD2QRQPY.js.map +1 -0
  37. package/dist/chunk-KLMXQVYK.js +369 -0
  38. package/dist/chunk-KLMXQVYK.js.map +1 -0
  39. package/dist/chunk-KZUG5KFQ.js +214 -0
  40. package/dist/chunk-KZUG5KFQ.js.map +1 -0
  41. package/{src/core/state/text-transaction.ts → dist/chunk-QDAQ4CJU.js} +79 -236
  42. package/dist/chunk-QDAQ4CJU.js.map +1 -0
  43. package/{src/legal/bookmarks.ts → dist/chunk-RMH72RZI.js} +44 -130
  44. package/dist/chunk-RMH72RZI.js.map +1 -0
  45. package/dist/chunk-SWKWQZXM.js +117 -0
  46. package/dist/chunk-SWKWQZXM.js.map +1 -0
  47. package/{src/core/commands/formatting-commands.ts → dist/chunk-TJBP2K4T.js} +196 -536
  48. package/dist/chunk-TJBP2K4T.js.map +1 -0
  49. package/dist/chunk-TLCEAQDQ.js +542 -0
  50. package/dist/chunk-TLCEAQDQ.js.map +1 -0
  51. package/{src/core/commands/text-commands.ts → dist/chunk-UZXBISGO.js} +86 -142
  52. package/dist/chunk-UZXBISGO.js.map +1 -0
  53. package/dist/chunk-WGBAKP3Q.js +3220 -0
  54. package/dist/chunk-WGBAKP3Q.js.map +1 -0
  55. package/dist/compare/index.cjs +5475 -0
  56. package/dist/compare/index.cjs.map +1 -0
  57. package/dist/compare/index.d.cts +114 -0
  58. package/dist/compare/index.d.ts +114 -0
  59. package/dist/compare/index.js +731 -0
  60. package/dist/compare/index.js.map +1 -0
  61. package/dist/core/commands/formatting-commands.cjs +828 -0
  62. package/dist/core/commands/formatting-commands.cjs.map +1 -0
  63. package/dist/core/commands/formatting-commands.d.cts +63 -0
  64. package/dist/core/commands/formatting-commands.d.ts +63 -0
  65. package/dist/core/commands/formatting-commands.js +37 -0
  66. package/dist/core/commands/formatting-commands.js.map +1 -0
  67. package/dist/core/commands/image-commands.cjs +2023 -0
  68. package/dist/core/commands/image-commands.cjs.map +1 -0
  69. package/dist/core/commands/image-commands.d.cts +58 -0
  70. package/dist/core/commands/image-commands.d.ts +58 -0
  71. package/dist/core/commands/image-commands.js +18 -0
  72. package/dist/core/commands/image-commands.js.map +1 -0
  73. package/dist/core/commands/section-layout-commands.cjs +477 -0
  74. package/dist/core/commands/section-layout-commands.cjs.map +1 -0
  75. package/dist/core/commands/section-layout-commands.d.cts +62 -0
  76. package/dist/core/commands/section-layout-commands.d.ts +62 -0
  77. package/dist/core/commands/section-layout-commands.js +21 -0
  78. package/dist/core/commands/section-layout-commands.js.map +1 -0
  79. package/dist/core/commands/style-commands.cjs +214 -0
  80. package/dist/core/commands/style-commands.cjs.map +1 -0
  81. package/dist/core/commands/style-commands.d.cts +13 -0
  82. package/dist/core/commands/style-commands.d.ts +13 -0
  83. package/dist/core/commands/style-commands.js +9 -0
  84. package/dist/core/commands/style-commands.js.map +1 -0
  85. package/dist/core/commands/table-structure-commands.cjs +1883 -0
  86. package/dist/core/commands/table-structure-commands.cjs.map +1 -0
  87. package/dist/core/commands/table-structure-commands.d.cts +59 -0
  88. package/dist/core/commands/table-structure-commands.d.ts +59 -0
  89. package/dist/core/commands/table-structure-commands.js +12 -0
  90. package/dist/core/commands/table-structure-commands.js.map +1 -0
  91. package/dist/core/commands/text-commands.cjs +2391 -0
  92. package/dist/core/commands/text-commands.cjs.map +1 -0
  93. package/dist/core/commands/text-commands.d.cts +24 -0
  94. package/dist/core/commands/text-commands.d.ts +24 -0
  95. package/dist/core/commands/text-commands.js +28 -0
  96. package/dist/core/commands/text-commands.js.map +1 -0
  97. package/dist/core/selection/mapping.cjs +200 -0
  98. package/dist/core/selection/mapping.cjs.map +1 -0
  99. package/dist/core/selection/mapping.d.cts +2 -0
  100. package/dist/core/selection/mapping.d.ts +2 -0
  101. package/dist/core/selection/mapping.js +31 -0
  102. package/dist/core/selection/mapping.js.map +1 -0
  103. package/dist/core/state/editor-state.cjs +2278 -0
  104. package/dist/core/state/editor-state.cjs.map +1 -0
  105. package/dist/core/state/editor-state.d.cts +2 -0
  106. package/dist/core/state/editor-state.d.ts +2 -0
  107. package/dist/core/state/editor-state.js +26 -0
  108. package/dist/core/state/editor-state.js.map +1 -0
  109. package/dist/index.cjs +38553 -0
  110. package/dist/index.cjs.map +1 -0
  111. package/dist/index.d.cts +15 -0
  112. package/dist/index.d.ts +15 -0
  113. package/dist/index.js +7856 -0
  114. package/dist/index.js.map +1 -0
  115. package/dist/io/docx-session.cjs +16236 -0
  116. package/dist/io/docx-session.cjs.map +1 -0
  117. package/dist/io/docx-session.d.cts +21 -0
  118. package/dist/io/docx-session.d.ts +21 -0
  119. package/dist/io/docx-session.js +18 -0
  120. package/dist/io/docx-session.js.map +1 -0
  121. package/dist/legal/index.cjs +3900 -0
  122. package/dist/legal/index.cjs.map +1 -0
  123. package/dist/legal/index.d.cts +86 -0
  124. package/dist/legal/index.d.ts +86 -0
  125. package/dist/legal/index.js +616 -0
  126. package/dist/legal/index.js.map +1 -0
  127. package/dist/public-types-7ZL_94cz.d.ts +1573 -0
  128. package/dist/public-types-CeMaDueh.d.cts +1573 -0
  129. package/dist/public-types.cjs +19 -0
  130. package/dist/public-types.cjs.map +1 -0
  131. package/dist/public-types.d.cts +2 -0
  132. package/dist/public-types.d.ts +2 -0
  133. package/dist/public-types.js +1 -0
  134. package/dist/public-types.js.map +1 -0
  135. package/dist/runtime/document-runtime.cjs +11140 -0
  136. package/dist/runtime/document-runtime.cjs.map +1 -0
  137. package/dist/runtime/document-runtime.d.cts +231 -0
  138. package/dist/runtime/document-runtime.d.ts +231 -0
  139. package/dist/runtime/document-runtime.js +21 -0
  140. package/dist/runtime/document-runtime.js.map +1 -0
  141. package/dist/structural-helpers-CilgOVhh.d.cts +10 -0
  142. package/dist/structural-helpers-q0Gd-eBN.d.ts +10 -0
  143. package/dist/ui-tailwind/editor-surface/search-plugin.cjs +313 -0
  144. package/dist/ui-tailwind/editor-surface/search-plugin.cjs.map +1 -0
  145. package/dist/ui-tailwind/editor-surface/search-plugin.d.cts +67 -0
  146. package/dist/ui-tailwind/editor-surface/search-plugin.d.ts +67 -0
  147. package/dist/ui-tailwind/editor-surface/search-plugin.js +23 -0
  148. package/dist/ui-tailwind/editor-surface/search-plugin.js.map +1 -0
  149. package/dist/ui-tailwind/index.cjs +4833 -0
  150. package/dist/ui-tailwind/index.cjs.map +1 -0
  151. package/dist/ui-tailwind/index.d.cts +617 -0
  152. package/dist/ui-tailwind/index.d.ts +617 -0
  153. package/dist/ui-tailwind/index.js +575 -0
  154. package/dist/ui-tailwind/index.js.map +1 -0
  155. package/package.json +64 -54
  156. package/src/README.md +0 -85
  157. package/src/api/README.md +0 -26
  158. package/src/api/public-types.ts +0 -1418
  159. package/src/api/session-state.ts +0 -60
  160. package/src/compare/diff-engine.ts +0 -623
  161. package/src/compare/export-redlines.ts +0 -280
  162. package/src/compare/index.ts +0 -25
  163. package/src/compare/snapshot.ts +0 -97
  164. package/src/component-inventory.md +0 -99
  165. package/src/core/README.md +0 -10
  166. package/src/core/commands/README.md +0 -3
  167. package/src/core/commands/image-commands.ts +0 -373
  168. package/src/core/commands/index.ts +0 -1757
  169. package/src/core/commands/list-commands.ts +0 -565
  170. package/src/core/commands/paragraph-layout-commands.ts +0 -339
  171. package/src/core/commands/review-commands.ts +0 -108
  172. package/src/core/commands/structural-helpers.ts +0 -309
  173. package/src/core/schema/README.md +0 -3
  174. package/src/core/schema/text-schema.ts +0 -516
  175. package/src/core/search/search-text.ts +0 -357
  176. package/src/core/selection/README.md +0 -3
  177. package/src/core/selection/mapping.ts +0 -289
  178. package/src/core/selection/review-anchors.ts +0 -183
  179. package/src/core/state/README.md +0 -3
  180. package/src/core/state/editor-state.ts +0 -892
  181. package/src/formats/xlsx/io/parse-shared-strings.ts +0 -41
  182. package/src/formats/xlsx/io/parse-sheet.ts +0 -459
  183. package/src/formats/xlsx/io/parse-styles.ts +0 -59
  184. package/src/formats/xlsx/io/parse-workbook.ts +0 -75
  185. package/src/formats/xlsx/io/serialize-shared-strings.ts +0 -72
  186. package/src/formats/xlsx/io/serialize-sheet.ts +0 -333
  187. package/src/formats/xlsx/io/serialize-styles.ts +0 -98
  188. package/src/formats/xlsx/io/serialize-workbook.ts +0 -429
  189. package/src/formats/xlsx/io/xlsx-session.ts +0 -314
  190. package/src/formats/xlsx/model/cell.ts +0 -189
  191. package/src/formats/xlsx/model/sheet.ts +0 -326
  192. package/src/formats/xlsx/model/styles.ts +0 -118
  193. package/src/formats/xlsx/model/workbook.ts +0 -453
  194. package/src/formats/xlsx/runtime/cell-commands.ts +0 -567
  195. package/src/formats/xlsx/runtime/sheet-commands.ts +0 -206
  196. package/src/formats/xlsx/runtime/workbook-runtime.ts +0 -177
  197. package/src/formats/xlsx/runtime/workbook-transaction.ts +0 -822
  198. package/src/index.ts +0 -101
  199. package/src/io/README.md +0 -10
  200. package/src/io/docx-session.ts +0 -2882
  201. package/src/io/export/README.md +0 -3
  202. package/src/io/export/export-session.ts +0 -220
  203. package/src/io/export/minimal-docx.ts +0 -115
  204. package/src/io/export/reattach-preserved-parts.ts +0 -54
  205. package/src/io/export/serialize-comments.ts +0 -947
  206. package/src/io/export/serialize-footnotes.ts +0 -399
  207. package/src/io/export/serialize-headers-footers.ts +0 -372
  208. package/src/io/export/serialize-main-document.ts +0 -1376
  209. package/src/io/export/serialize-numbering.ts +0 -118
  210. package/src/io/export/serialize-revisions.ts +0 -389
  211. package/src/io/export/serialize-runtime-revisions.ts +0 -269
  212. package/src/io/export/serialize-tables.ts +0 -174
  213. package/src/io/export/split-review-boundaries.ts +0 -356
  214. package/src/io/normalize/README.md +0 -3
  215. package/src/io/normalize/normalize-text.ts +0 -639
  216. package/src/io/ooxml/README.md +0 -3
  217. package/src/io/ooxml/highlight-colors.ts +0 -39
  218. package/src/io/ooxml/numbering-sentinels.ts +0 -44
  219. package/src/io/ooxml/parse-comments.ts +0 -846
  220. package/src/io/ooxml/parse-complex-content.ts +0 -287
  221. package/src/io/ooxml/parse-fields.ts +0 -834
  222. package/src/io/ooxml/parse-footnotes.ts +0 -896
  223. package/src/io/ooxml/parse-headers-footers.ts +0 -1169
  224. package/src/io/ooxml/parse-inline-media.ts +0 -461
  225. package/src/io/ooxml/parse-main-document.ts +0 -2877
  226. package/src/io/ooxml/parse-numbering.ts +0 -432
  227. package/src/io/ooxml/parse-revisions.ts +0 -931
  228. package/src/io/ooxml/parse-settings.ts +0 -184
  229. package/src/io/ooxml/parse-shapes.ts +0 -296
  230. package/src/io/ooxml/parse-styles.ts +0 -463
  231. package/src/io/ooxml/parse-tables.ts +0 -618
  232. package/src/io/ooxml/parse-theme.ts +0 -346
  233. package/src/io/ooxml/part-manifest.ts +0 -136
  234. package/src/io/ooxml/revision-boundaries.ts +0 -351
  235. package/src/io/opc/README.md +0 -3
  236. package/src/io/opc/corrupt-package.ts +0 -166
  237. package/src/io/opc/docx-package.ts +0 -74
  238. package/src/io/opc/package-reader.ts +0 -325
  239. package/src/io/opc/package-writer.ts +0 -273
  240. package/src/io/source-package-provenance.ts +0 -241
  241. package/src/legal/cross-references.ts +0 -414
  242. package/src/legal/defined-terms.ts +0 -203
  243. package/src/legal/index.ts +0 -32
  244. package/src/legal/signature-blocks.ts +0 -259
  245. package/src/model/README.md +0 -3
  246. package/src/model/canonical-document.ts +0 -2632
  247. package/src/model/cds-1.0.0.ts +0 -212
  248. package/src/model/snapshot.ts +0 -649
  249. package/src/preservation/README.md +0 -3
  250. package/src/preservation/markup-compatibility.ts +0 -48
  251. package/src/preservation/opaque-fragment-store.ts +0 -89
  252. package/src/preservation/opaque-region.ts +0 -233
  253. package/src/preservation/package-preservation.ts +0 -113
  254. package/src/preservation/preserved-part-manifest.ts +0 -56
  255. package/src/preservation/relationship-retention.ts +0 -57
  256. package/src/preservation/store.ts +0 -185
  257. package/src/review/README.md +0 -16
  258. package/src/review/store/README.md +0 -3
  259. package/src/review/store/comment-anchors.ts +0 -70
  260. package/src/review/store/comment-remapping.ts +0 -154
  261. package/src/review/store/comment-store.ts +0 -331
  262. package/src/review/store/comment-thread.ts +0 -109
  263. package/src/review/store/revision-actions.ts +0 -394
  264. package/src/review/store/revision-store.ts +0 -312
  265. package/src/review/store/revision-types.ts +0 -171
  266. package/src/review/store/runtime-comment-store.ts +0 -43
  267. package/src/runtime/README.md +0 -3
  268. package/src/runtime/ai-action-policy.ts +0 -764
  269. package/src/runtime/collab-review-sync.ts +0 -254
  270. package/src/runtime/document-layout.ts +0 -332
  271. package/src/runtime/document-navigation.ts +0 -603
  272. package/src/runtime/document-runtime.ts +0 -3159
  273. package/src/runtime/document-search.ts +0 -145
  274. package/src/runtime/numbering-prefix.ts +0 -216
  275. package/src/runtime/page-layout-estimation.ts +0 -212
  276. package/src/runtime/read-only-diagnostics-runtime.ts +0 -241
  277. package/src/runtime/review-runtime.ts +0 -44
  278. package/src/runtime/revision-runtime.ts +0 -107
  279. package/src/runtime/session-capabilities.ts +0 -192
  280. package/src/runtime/story-context.ts +0 -164
  281. package/src/runtime/story-targeting.ts +0 -162
  282. package/src/runtime/surface-projection.ts +0 -1357
  283. package/src/runtime/table-commands.ts +0 -173
  284. package/src/runtime/table-schema.ts +0 -309
  285. package/src/runtime/view-state.ts +0 -477
  286. package/src/runtime/virtualized-rendering.ts +0 -258
  287. package/src/runtime/workflow-markup.ts +0 -353
  288. package/src/ui/README.md +0 -30
  289. package/src/ui/WordReviewEditor.tsx +0 -4097
  290. package/src/ui/browser-export.ts +0 -52
  291. package/src/ui/comments/README.md +0 -3
  292. package/src/ui/compatibility/README.md +0 -3
  293. package/src/ui/editor-command-bag.ts +0 -120
  294. package/src/ui/editor-runtime-boundary.ts +0 -1457
  295. package/src/ui/editor-shell-view.tsx +0 -142
  296. package/src/ui/editor-surface/README.md +0 -3
  297. package/src/ui/editor-surface-controller.tsx +0 -63
  298. package/src/ui/headless/comment-decoration-model.ts +0 -124
  299. package/src/ui/headless/preserve-editor-selection.ts +0 -5
  300. package/src/ui/headless/revision-decoration-model.ts +0 -128
  301. package/src/ui/headless/selection-helpers.ts +0 -54
  302. package/src/ui/headless/selection-toolbar-model.ts +0 -34
  303. package/src/ui/headless/use-editor-keyboard.ts +0 -103
  304. package/src/ui/review/README.md +0 -3
  305. package/src/ui/runtime-snapshot-selectors.ts +0 -197
  306. package/src/ui/shared/revision-filters.ts +0 -31
  307. package/src/ui/status/README.md +0 -3
  308. package/src/ui/theme/README.md +0 -3
  309. package/src/ui/toolbar/README.md +0 -3
  310. package/src/ui/workflow-surface-blocked-rails.ts +0 -94
  311. package/src/ui-tailwind/chrome/tw-alert-banner.tsx +0 -64
  312. package/src/ui-tailwind/chrome/tw-image-context-toolbar.tsx +0 -129
  313. package/src/ui-tailwind/chrome/tw-layout-panel.tsx +0 -114
  314. package/src/ui-tailwind/chrome/tw-object-context-toolbar.tsx +0 -34
  315. package/src/ui-tailwind/chrome/tw-page-ruler.tsx +0 -386
  316. package/src/ui-tailwind/chrome/tw-selection-toolbar.tsx +0 -186
  317. package/src/ui-tailwind/chrome/tw-suggestion-card.tsx +0 -139
  318. package/src/ui-tailwind/chrome/tw-table-context-toolbar.tsx +0 -128
  319. package/src/ui-tailwind/chrome/tw-unsaved-modal.tsx +0 -58
  320. package/src/ui-tailwind/chrome/use-before-unload.ts +0 -20
  321. package/src/ui-tailwind/editor-surface/perf-probe.ts +0 -179
  322. package/src/ui-tailwind/editor-surface/pm-collab-plugins.ts +0 -40
  323. package/src/ui-tailwind/editor-surface/pm-command-bridge.ts +0 -178
  324. package/src/ui-tailwind/editor-surface/pm-contextual-ui.ts +0 -31
  325. package/src/ui-tailwind/editor-surface/pm-decorations.ts +0 -427
  326. package/src/ui-tailwind/editor-surface/pm-position-map.ts +0 -123
  327. package/src/ui-tailwind/editor-surface/pm-schema.ts +0 -876
  328. package/src/ui-tailwind/editor-surface/pm-state-from-snapshot.ts +0 -504
  329. package/src/ui-tailwind/editor-surface/search-plugin.ts +0 -168
  330. package/src/ui-tailwind/editor-surface/surface-build-keys.ts +0 -61
  331. package/src/ui-tailwind/editor-surface/tw-caret.tsx +0 -12
  332. package/src/ui-tailwind/editor-surface/tw-editor-surface.tsx +0 -150
  333. package/src/ui-tailwind/editor-surface/tw-inline-token.tsx +0 -129
  334. package/src/ui-tailwind/editor-surface/tw-opaque-block.tsx +0 -58
  335. package/src/ui-tailwind/editor-surface/tw-paragraph-block.tsx +0 -151
  336. package/src/ui-tailwind/editor-surface/tw-prosemirror-surface.tsx +0 -973
  337. package/src/ui-tailwind/editor-surface/tw-segment-view.tsx +0 -111
  338. package/src/ui-tailwind/editor-surface/tw-table-node-view.tsx +0 -436
  339. package/src/ui-tailwind/index.ts +0 -62
  340. package/src/ui-tailwind/page-chrome-model.ts +0 -27
  341. package/src/ui-tailwind/review/tw-comment-sidebar.tsx +0 -406
  342. package/src/ui-tailwind/review/tw-health-panel.tsx +0 -149
  343. package/src/ui-tailwind/review/tw-review-rail.tsx +0 -120
  344. package/src/ui-tailwind/review/tw-revision-sidebar.tsx +0 -164
  345. package/src/ui-tailwind/status/tw-status-bar.tsx +0 -61
  346. package/src/ui-tailwind/toolbar/tw-toolbar-icon-button.tsx +0 -52
  347. package/src/ui-tailwind/toolbar/tw-toolbar.tsx +0 -1064
  348. package/src/ui-tailwind/tw-review-workspace.tsx +0 -1417
  349. package/src/validation/README.md +0 -3
  350. package/src/validation/compatibility-engine.ts +0 -634
  351. package/src/validation/compatibility-report.ts +0 -161
  352. package/src/validation/diagnostics.ts +0 -204
  353. package/src/validation/docx-comment-proof.ts +0 -707
  354. package/src/validation/import-diagnostics.ts +0 -128
  355. package/src/validation/low-priority-word-surfaces.ts +0 -373
  356. /package/{src → dist}/ui-tailwind/theme/editor-theme.css +0 -0
@@ -1,1757 +0,0 @@
1
- import {
2
- createSelectionSnapshot,
3
- type CanonicalDocumentEnvelope,
4
- type CommentThreadRecord,
5
- type EditorState,
6
- type EditorWarning,
7
- normalizeCommentThreadRecord,
8
- type SelectionSnapshot,
9
- } from "../state/editor-state.ts";
10
- import {
11
- areAnchorsEqual,
12
- createDetachedAnchor,
13
- createEmptyMapping,
14
- createNodeAnchor,
15
- createRangeAnchor,
16
- DEFAULT_BOUNDARY_ASSOC,
17
- mapAnchor,
18
- type BoundaryAssoc,
19
- type EditorAnchorProjection,
20
- type MappingStep,
21
- type TransactionMapping,
22
- } from "../selection/mapping.ts";
23
- import {
24
- createAcceptRevisionCommand,
25
- createRejectRevisionCommand,
26
- type ReviewCommand,
27
- } from "./review-commands.ts";
28
- import {
29
- deleteSelectionOrBackward,
30
- deleteSelectionOrForward,
31
- insertHardBreak,
32
- insertTab,
33
- insertText,
34
- splitParagraph,
35
- } from "./text-commands.ts";
36
- import type { RevisionRecord as CanonicalRevisionRecord } from "../../model/canonical-document.ts";
37
- import { remapCommentThreads } from "../../review/store/comment-remapping.ts";
38
- import { applyRevisionRuntimeCommand } from "../../runtime/revision-runtime.ts";
39
- import type { RevisionStore } from "../../review/store/revision-store.ts";
40
-
41
- export interface CommandOrigin {
42
- source:
43
- | "keyboard"
44
- | "toolbar"
45
- | "context_menu"
46
- | "comment_panel"
47
- | "review_panel"
48
- | "api"
49
- | "runtime";
50
- timestamp: string;
51
- }
52
-
53
- export type EditorCommand =
54
- | {
55
- type: "selection.set";
56
- selection: SelectionSnapshot;
57
- origin?: CommandOrigin;
58
- }
59
- | {
60
- type: "document.replace";
61
- document: CanonicalDocumentEnvelope;
62
- mapping?: TransactionMapping;
63
- selection?: SelectionSnapshot;
64
- protectionSelection?: SelectionSnapshot;
65
- origin?: CommandOrigin;
66
- }
67
- | {
68
- type: "text.insert";
69
- text: string;
70
- origin?: CommandOrigin;
71
- }
72
- | {
73
- type: "text.delete-backward";
74
- origin?: CommandOrigin;
75
- }
76
- | {
77
- type: "text.delete-forward";
78
- origin?: CommandOrigin;
79
- }
80
- | {
81
- type: "text.insert-tab";
82
- origin?: CommandOrigin;
83
- }
84
- | {
85
- type: "text.insert-hard-break";
86
- origin?: CommandOrigin;
87
- }
88
- | {
89
- type: "paragraph.split";
90
- origin?: CommandOrigin;
91
- }
92
- | {
93
- type: "runtime.set-read-only";
94
- readOnly: boolean;
95
- origin?: CommandOrigin;
96
- }
97
- | {
98
- type: "runtime.focus";
99
- focused: boolean;
100
- origin?: CommandOrigin;
101
- }
102
- | {
103
- type: "warning.add";
104
- warning: EditorWarning;
105
- origin?: CommandOrigin;
106
- }
107
- | {
108
- type: "warning.clear";
109
- warningId: string;
110
- origin?: CommandOrigin;
111
- }
112
- | {
113
- type: "comment.add";
114
- comment: CommentThreadRecord;
115
- selection?: SelectionSnapshot;
116
- origin?: CommandOrigin;
117
- }
118
- | {
119
- type: "comment.open";
120
- commentId: string;
121
- origin?: CommandOrigin;
122
- }
123
- | {
124
- type: "comment.resolve";
125
- commentId: string;
126
- resolvedBy?: string;
127
- origin?: CommandOrigin;
128
- }
129
- | {
130
- type: "comment.reopen";
131
- commentId: string;
132
- origin?: CommandOrigin;
133
- }
134
- | {
135
- type: "comment.add-reply";
136
- commentId: string;
137
- body: string;
138
- authorId?: string;
139
- origin?: CommandOrigin;
140
- }
141
- | {
142
- type: "comment.edit-body";
143
- commentId: string;
144
- body: string;
145
- origin?: CommandOrigin;
146
- }
147
- | {
148
- type: "change.accept";
149
- changeId: string;
150
- origin?: CommandOrigin;
151
- }
152
- | {
153
- type: "change.reject";
154
- changeId: string;
155
- origin?: CommandOrigin;
156
- }
157
- | {
158
- type: "change.accept-all";
159
- origin?: CommandOrigin;
160
- }
161
- | {
162
- type: "change.reject-all";
163
- origin?: CommandOrigin;
164
- }
165
- | {
166
- type: "history.undo";
167
- origin?: CommandOrigin;
168
- }
169
- | {
170
- type: "history.redo";
171
- origin?: CommandOrigin;
172
- };
173
-
174
- export interface TransactionEffects {
175
- warningsAdded: EditorWarning[];
176
- warningsCleared: Array<{ warningId: string; code: EditorWarning["code"] }>;
177
- commentAdded?: { commentId: string; anchor: EditorAnchorProjection };
178
- commentResolved?: { commentId: string };
179
- commentReopened?: { commentId: string };
180
- commentReplyAdded?: { commentId: string };
181
- commentBodyEdited?: { commentId: string };
182
- changeAccepted?: { changeId: string };
183
- changeRejected?: { changeId: string };
184
- revisionAuthored?: { changeId: string; kind: "insertion" | "deletion" };
185
- commandBlocked?: { code: string; message: string };
186
- }
187
-
188
- export interface EditorTransaction {
189
- nextState: EditorState;
190
- mapping: TransactionMapping;
191
- effects: TransactionEffects;
192
- historyBoundary: "push" | "skip";
193
- markDirty: boolean;
194
- }
195
-
196
- export interface CommandExecutionContext {
197
- timestamp: string;
198
- documentMode?: "editing" | "suggesting" | "viewing";
199
- defaultAuthorId?: string;
200
- }
201
-
202
- export function executeEditorCommand(
203
- state: EditorState,
204
- command: Exclude<EditorCommand, { type: "history.undo" } | { type: "history.redo" }>,
205
- context: CommandExecutionContext,
206
- ): EditorTransaction {
207
- switch (command.type) {
208
- case "selection.set":
209
- return createTransaction(
210
- {
211
- ...state,
212
- selection: normalizeSelection(command.selection),
213
- },
214
- {
215
- historyBoundary: "skip",
216
- markDirty: false,
217
- },
218
- );
219
- case "document.replace": {
220
- const mapping = command.mapping ?? createEmptyMapping();
221
- const selection =
222
- command.selection ?? remapSelection(state.selection, mapping);
223
- const reviewState = remapReviewStateAfterContentChange(
224
- state,
225
- command.document,
226
- mapping,
227
- );
228
-
229
- return createTransaction(
230
- {
231
- ...state,
232
- document: reviewState.document,
233
- selection,
234
- warnings: reviewState.warnings,
235
- runtime: {
236
- ...state.runtime,
237
- activeCommentId: reviewState.activeCommentId,
238
- },
239
- compatibility: {
240
- ...state.compatibility,
241
- generatedAt: context.timestamp,
242
- warnings: reviewState.warnings,
243
- featureEntries: state.compatibility.featureEntries.map((entry) =>
244
- entry.affectedAnchor
245
- ? {
246
- ...entry,
247
- affectedAnchor: mapAnchor(entry.affectedAnchor, mapping),
248
- }
249
- : entry,
250
- ),
251
- },
252
- },
253
- {
254
- historyBoundary: "push",
255
- markDirty: true,
256
- mapping,
257
- effects: reviewState.effects,
258
- },
259
- );
260
- }
261
- case "text.insert": {
262
- const suggestingResult = context.documentMode === "suggesting"
263
- ? applySuggestingInsert(state, command.text, context)
264
- : undefined;
265
- if (suggestingResult) return suggestingResult;
266
- return applyTextCommand(state, context.timestamp, (document, selection) =>
267
- insertText(document, selection, command.text, context),
268
- );
269
- }
270
- case "text.delete-backward": {
271
- const suggestingResult = context.documentMode === "suggesting"
272
- ? applySuggestingDelete(state, "backward", context)
273
- : undefined;
274
- if (suggestingResult) return suggestingResult;
275
- return applyTextCommand(state, context.timestamp, (document, selection) =>
276
- deleteSelectionOrBackward(document, selection, context),
277
- );
278
- }
279
- case "text.delete-forward": {
280
- const suggestingResult = context.documentMode === "suggesting"
281
- ? applySuggestingDelete(state, "forward", context)
282
- : undefined;
283
- if (suggestingResult) return suggestingResult;
284
- return applyTextCommand(state, context.timestamp, (document, selection) =>
285
- deleteSelectionOrForward(document, selection, context),
286
- );
287
- }
288
- case "text.insert-tab": {
289
- const suggestingResult = context.documentMode === "suggesting"
290
- ? applySuggestingInsertUnit(state, "tab", context)
291
- : undefined;
292
- if (suggestingResult) return suggestingResult;
293
- return applyTextCommand(state, context.timestamp, (document, selection) =>
294
- insertTab(document, selection, context),
295
- );
296
- }
297
- case "text.insert-hard-break": {
298
- const suggestingResult = context.documentMode === "suggesting"
299
- ? applySuggestingInsertUnit(state, "hard_break", context)
300
- : undefined;
301
- if (suggestingResult) return suggestingResult;
302
- return applyTextCommand(state, context.timestamp, (document, selection) =>
303
- insertHardBreak(document, selection, context),
304
- );
305
- }
306
- case "paragraph.split":
307
- if (context.documentMode === "suggesting") {
308
- return createTransaction(state, {
309
- historyBoundary: "skip",
310
- markDirty: false,
311
- effects: {
312
- commandBlocked: {
313
- code: "suggesting_unsupported",
314
- message: "Paragraph splits are not supported in suggesting mode.",
315
- },
316
- },
317
- });
318
- }
319
- return applyTextCommand(state, context.timestamp, (document, selection) =>
320
- splitParagraph(document, selection, context),
321
- );
322
- case "runtime.set-read-only":
323
- return createTransaction(
324
- {
325
- ...state,
326
- readOnly: command.readOnly,
327
- },
328
- {
329
- historyBoundary: "skip",
330
- markDirty: false,
331
- },
332
- );
333
- case "runtime.focus":
334
- return createTransaction(
335
- {
336
- ...state,
337
- runtime: {
338
- ...state.runtime,
339
- hasFocus: command.focused,
340
- },
341
- },
342
- {
343
- historyBoundary: "skip",
344
- markDirty: false,
345
- },
346
- );
347
- case "warning.add": {
348
- if (state.warnings.some((warning) => warning.warningId === command.warning.warningId)) {
349
- return createTransaction(state, {
350
- historyBoundary: "skip",
351
- markDirty: false,
352
- });
353
- }
354
-
355
- const warnings = [...state.warnings, command.warning];
356
- return createTransaction(
357
- {
358
- ...state,
359
- warnings,
360
- compatibility: {
361
- ...state.compatibility,
362
- generatedAt: context.timestamp,
363
- warnings,
364
- },
365
- },
366
- {
367
- historyBoundary: "skip",
368
- markDirty: false,
369
- effects: {
370
- warningsAdded: [command.warning],
371
- },
372
- },
373
- );
374
- }
375
- case "warning.clear": {
376
- const existing = state.warnings.find((warning) => warning.warningId === command.warningId);
377
-
378
- if (!existing) {
379
- return createTransaction(state, {
380
- historyBoundary: "skip",
381
- markDirty: false,
382
- });
383
- }
384
-
385
- const warnings = state.warnings.filter((warning) => warning.warningId !== command.warningId);
386
- return createTransaction(
387
- {
388
- ...state,
389
- warnings,
390
- compatibility: {
391
- ...state.compatibility,
392
- generatedAt: context.timestamp,
393
- warnings,
394
- },
395
- },
396
- {
397
- historyBoundary: "skip",
398
- markDirty: false,
399
- effects: {
400
- warningsCleared: [
401
- {
402
- warningId: existing.warningId,
403
- code: existing.code,
404
- },
405
- ],
406
- },
407
- },
408
- );
409
- }
410
- case "comment.add": {
411
- const comments = {
412
- ...state.document.review.comments,
413
- [command.comment.commentId]: normalizeCommentThreadRecord(command.comment),
414
- };
415
-
416
- return createTransaction(
417
- {
418
- ...state,
419
- document: {
420
- ...state.document,
421
- updatedAt: context.timestamp,
422
- review: {
423
- ...state.document.review,
424
- comments,
425
- },
426
- },
427
- runtime: {
428
- ...state.runtime,
429
- activeCommentId: command.comment.commentId,
430
- },
431
- },
432
- {
433
- historyBoundary: "push",
434
- markDirty: true,
435
- effects: {
436
- commentAdded: {
437
- commentId: command.comment.commentId,
438
- anchor: command.comment.anchor,
439
- },
440
- },
441
- },
442
- );
443
- }
444
- case "comment.open":
445
- return createTransaction(
446
- {
447
- ...state,
448
- runtime: {
449
- ...state.runtime,
450
- activeCommentId: command.commentId,
451
- },
452
- },
453
- {
454
- historyBoundary: "skip",
455
- markDirty: false,
456
- },
457
- );
458
- case "comment.resolve": {
459
- const existing = state.document.review.comments[command.commentId];
460
- if (!existing) {
461
- return createTransaction(state, {
462
- historyBoundary: "skip",
463
- markDirty: false,
464
- });
465
- }
466
-
467
- const comments = {
468
- ...state.document.review.comments,
469
- [command.commentId]: {
470
- ...existing,
471
- status: (existing.anchor.kind === "detached" ? "detached" : "resolved") as "detached" | "resolved",
472
- resolution: {
473
- resolvedAt: context.timestamp,
474
- resolvedBy:
475
- command.resolvedBy ??
476
- existing.createdBy ??
477
- existing.authorId ??
478
- existing.entries?.[0]?.authorId ??
479
- "unknown",
480
- },
481
- isResolved: true,
482
- resolvedAt: context.timestamp,
483
- },
484
- };
485
-
486
- return createTransaction(
487
- {
488
- ...state,
489
- document: {
490
- ...state.document,
491
- updatedAt: context.timestamp,
492
- review: {
493
- ...state.document.review,
494
- comments,
495
- },
496
- },
497
- },
498
- {
499
- historyBoundary: "push",
500
- markDirty: true,
501
- effects: {
502
- commentResolved: {
503
- commentId: command.commentId,
504
- },
505
- },
506
- },
507
- );
508
- }
509
- case "comment.reopen": {
510
- const existingThread = state.document.review.comments[command.commentId];
511
- if (!existingThread || existingThread.status === "detached") {
512
- return createTransaction(state, { historyBoundary: "skip", markDirty: false });
513
- }
514
- const reopenedComments = {
515
- ...state.document.review.comments,
516
- [command.commentId]: {
517
- ...existingThread,
518
- status: "open" as const,
519
- isResolved: false,
520
- resolvedAt: undefined,
521
- resolution: undefined,
522
- },
523
- };
524
- return createTransaction(
525
- {
526
- ...state,
527
- document: {
528
- ...state.document,
529
- updatedAt: context.timestamp,
530
- review: { ...state.document.review, comments: reopenedComments },
531
- },
532
- runtime: { ...state.runtime, activeCommentId: command.commentId },
533
- },
534
- {
535
- historyBoundary: "push",
536
- markDirty: true,
537
- effects: { commentReopened: { commentId: command.commentId } },
538
- },
539
- );
540
- }
541
- case "comment.add-reply": {
542
- const threadForReply = state.document.review.comments[command.commentId];
543
- if (!threadForReply) {
544
- return createTransaction(state, { historyBoundary: "skip", markDirty: false });
545
- }
546
- // Block reply on resolved or detached threads (must reopen first)
547
- if (threadForReply.status === "resolved" || threadForReply.status === "detached") {
548
- return createTransaction(state, { historyBoundary: "skip", markDirty: false });
549
- }
550
- const entryId = `${command.commentId}-entry-${(threadForReply.entries?.length ?? 0) + 1}`;
551
- const newEntry = {
552
- entryId,
553
- authorId: command.authorId ?? threadForReply.createdBy ?? "unknown",
554
- body: command.body,
555
- createdAt: context.timestamp,
556
- };
557
- const updatedThread = {
558
- ...threadForReply,
559
- entries: [...(threadForReply.entries ?? []), newEntry],
560
- };
561
- const replyComments = {
562
- ...state.document.review.comments,
563
- [command.commentId]: updatedThread,
564
- };
565
- return createTransaction(
566
- {
567
- ...state,
568
- document: {
569
- ...state.document,
570
- updatedAt: context.timestamp,
571
- review: { ...state.document.review, comments: replyComments },
572
- },
573
- },
574
- {
575
- historyBoundary: "push",
576
- markDirty: true,
577
- effects: { commentReplyAdded: { commentId: command.commentId } },
578
- },
579
- );
580
- }
581
- case "comment.edit-body": {
582
- const threadToEdit = state.document.review.comments[command.commentId];
583
- if (!threadToEdit || !threadToEdit.entries?.length) {
584
- return createTransaction(state, { historyBoundary: "skip", markDirty: false });
585
- }
586
- const editedEntries = [...threadToEdit.entries];
587
- editedEntries[0] = { ...editedEntries[0], body: command.body };
588
- const editedThread = {
589
- ...threadToEdit,
590
- body: command.body,
591
- entries: editedEntries,
592
- };
593
- const editedComments = {
594
- ...state.document.review.comments,
595
- [command.commentId]: editedThread,
596
- };
597
- return createTransaction(
598
- {
599
- ...state,
600
- document: {
601
- ...state.document,
602
- updatedAt: context.timestamp,
603
- review: { ...state.document.review, comments: editedComments },
604
- },
605
- },
606
- {
607
- historyBoundary: "push",
608
- markDirty: true,
609
- effects: { commentBodyEdited: { commentId: command.commentId } },
610
- },
611
- );
612
- }
613
- case "change.accept":
614
- return applyReviewCommand(
615
- state,
616
- createAcceptRevisionCommand(command.changeId, command.origin),
617
- context.timestamp,
618
- );
619
- case "change.reject":
620
- return applyReviewCommand(
621
- state,
622
- createRejectRevisionCommand(command.changeId, command.origin),
623
- context.timestamp,
624
- );
625
- case "change.accept-all":
626
- return applyReviewCommand(
627
- state,
628
- {
629
- type: "review.accept-all-revisions",
630
- ...(command.origin ? { origin: command.origin } : {}),
631
- },
632
- context.timestamp,
633
- );
634
- case "change.reject-all":
635
- return applyReviewCommand(
636
- state,
637
- {
638
- type: "review.reject-all-revisions",
639
- ...(command.origin ? { origin: command.origin } : {}),
640
- },
641
- context.timestamp,
642
- );
643
- }
644
- }
645
-
646
- export function remapSelection(
647
- selection: SelectionSnapshot,
648
- mapping: TransactionMapping,
649
- ): SelectionSnapshot {
650
- const activeRange = mapAnchor(selection.activeRange, mapping);
651
-
652
- if (activeRange.kind === "range") {
653
- return {
654
- anchor: activeRange.range.from,
655
- head: activeRange.range.to,
656
- isCollapsed: activeRange.range.from === activeRange.range.to,
657
- activeRange,
658
- };
659
- }
660
-
661
- if (activeRange.kind === "node") {
662
- return {
663
- anchor: activeRange.at,
664
- head: activeRange.at,
665
- isCollapsed: true,
666
- activeRange,
667
- };
668
- }
669
-
670
- return {
671
- anchor: selection.anchor,
672
- head: selection.head,
673
- isCollapsed: selection.anchor === selection.head,
674
- activeRange,
675
- };
676
- }
677
-
678
- export function selectionChanged(
679
- left: SelectionSnapshot,
680
- right: SelectionSnapshot,
681
- ): boolean {
682
- return (
683
- left.anchor !== right.anchor ||
684
- left.head !== right.head ||
685
- left.isCollapsed !== right.isCollapsed ||
686
- !areAnchorsEqual(left.activeRange, right.activeRange)
687
- );
688
- }
689
-
690
- function createTransaction(
691
- nextState: EditorState,
692
- options: {
693
- mapping?: TransactionMapping;
694
- historyBoundary: "push" | "skip";
695
- markDirty: boolean;
696
- effects?: Partial<TransactionEffects>;
697
- },
698
- ): EditorTransaction {
699
- return {
700
- nextState,
701
- mapping: options.mapping ?? createEmptyMapping(),
702
- historyBoundary: options.historyBoundary,
703
- markDirty: options.markDirty,
704
- effects: {
705
- warningsAdded: options.effects?.warningsAdded ?? [],
706
- warningsCleared: options.effects?.warningsCleared ?? [],
707
- commentAdded: options.effects?.commentAdded,
708
- commentResolved: options.effects?.commentResolved,
709
- changeAccepted: options.effects?.changeAccepted,
710
- changeRejected: options.effects?.changeRejected,
711
- revisionAuthored: options.effects?.revisionAuthored,
712
- commandBlocked: options.effects?.commandBlocked,
713
- },
714
- };
715
- }
716
-
717
- function normalizeSelection(selection: SelectionSnapshot): SelectionSnapshot {
718
- const activeRange = normalizeSelectionAnchor(
719
- selection.activeRange,
720
- selection.anchor,
721
- selection.head,
722
- );
723
-
724
- if (activeRange.kind === "range") {
725
- return {
726
- anchor: activeRange.range.from,
727
- head: activeRange.range.to,
728
- isCollapsed: activeRange.range.from === activeRange.range.to,
729
- activeRange,
730
- };
731
- }
732
-
733
- if (activeRange.kind === "node") {
734
- return {
735
- anchor: activeRange.at,
736
- head: activeRange.at,
737
- isCollapsed: true,
738
- activeRange,
739
- };
740
- }
741
-
742
- return {
743
- anchor: activeRange.lastKnownRange.from,
744
- head: activeRange.lastKnownRange.to,
745
- isCollapsed: activeRange.lastKnownRange.from === activeRange.lastKnownRange.to,
746
- activeRange,
747
- };
748
- }
749
-
750
- function normalizeSelectionAnchor(
751
- value: SelectionSnapshot["activeRange"],
752
- anchor: number,
753
- head: number,
754
- ): EditorAnchorProjection {
755
- if (!value || typeof value !== "object") {
756
- return createRangeAnchor(anchor, head, DEFAULT_BOUNDARY_ASSOC);
757
- }
758
-
759
- const record = value as unknown as Record<string, unknown>;
760
- switch (record.kind) {
761
- case "range": {
762
- const assoc = normalizeBoundaryAssoc(record.assoc);
763
- const internalRange = record.range;
764
- if (
765
- internalRange &&
766
- typeof internalRange === "object" &&
767
- typeof (internalRange as { from?: unknown }).from === "number" &&
768
- typeof (internalRange as { to?: unknown }).to === "number"
769
- ) {
770
- return createRangeAnchor(
771
- (internalRange as { from: number }).from,
772
- (internalRange as { to: number }).to,
773
- assoc,
774
- );
775
- }
776
-
777
- if (typeof record.from === "number" && typeof record.to === "number") {
778
- return createRangeAnchor(record.from, record.to, assoc);
779
- }
780
-
781
- return createRangeAnchor(anchor, head, assoc);
782
- }
783
- case "node":
784
- return createNodeAnchor(
785
- typeof record.at === "number" ? record.at : anchor,
786
- normalizeAssoc(record.assoc),
787
- );
788
- case "detached": {
789
- const lastKnownRange = record.lastKnownRange;
790
- if (
791
- lastKnownRange &&
792
- typeof lastKnownRange === "object" &&
793
- typeof (lastKnownRange as { from?: unknown }).from === "number" &&
794
- typeof (lastKnownRange as { to?: unknown }).to === "number"
795
- ) {
796
- return createDetachedAnchor(
797
- {
798
- from: (lastKnownRange as { from: number }).from,
799
- to: (lastKnownRange as { to: number }).to,
800
- },
801
- normalizeDetachedReason(record.reason),
802
- );
803
- }
804
-
805
- return createDetachedAnchor({ from: anchor, to: head }, normalizeDetachedReason(record.reason));
806
- }
807
- default:
808
- return createRangeAnchor(anchor, head, DEFAULT_BOUNDARY_ASSOC);
809
- }
810
- }
811
-
812
- function normalizeBoundaryAssoc(value: unknown): BoundaryAssoc {
813
- if (value && typeof value === "object") {
814
- const record = value as unknown as Record<string, unknown>;
815
- return {
816
- start: normalizeAssoc(record.start),
817
- end: normalizeAssoc(record.end),
818
- };
819
- }
820
-
821
- return DEFAULT_BOUNDARY_ASSOC;
822
- }
823
-
824
- function normalizeAssoc(value: unknown): -1 | 1 {
825
- return value === -1 ? -1 : 1;
826
- }
827
-
828
- function normalizeDetachedReason(
829
- value: unknown,
830
- ): "deleted" | "invalidatedByStructureChange" | "importAmbiguity" {
831
- if (
832
- value === "deleted" ||
833
- value === "invalidatedByStructureChange" ||
834
- value === "importAmbiguity"
835
- ) {
836
- return value;
837
- }
838
-
839
- return "importAmbiguity";
840
- }
841
-
842
- function applyTextCommand(
843
- state: EditorState,
844
- timestamp: string,
845
- apply: (
846
- document: CanonicalDocumentEnvelope,
847
- selection: SelectionSnapshot,
848
- ) => {
849
- document: CanonicalDocumentEnvelope;
850
- selection: SelectionSnapshot;
851
- mapping: TransactionMapping;
852
- },
853
- ): EditorTransaction {
854
- if (state.readOnly) {
855
- return createTransaction(state, {
856
- historyBoundary: "skip",
857
- markDirty: false,
858
- });
859
- }
860
-
861
- const result = apply(state.document, state.selection);
862
- const reviewState = remapReviewStateAfterContentChange(
863
- state,
864
- result.document,
865
- result.mapping,
866
- );
867
-
868
- return createTransaction(
869
- {
870
- ...state,
871
- document: reviewState.document,
872
- selection: result.selection,
873
- warnings: reviewState.warnings,
874
- runtime: {
875
- ...state.runtime,
876
- activeCommentId: reviewState.activeCommentId,
877
- },
878
- },
879
- {
880
- historyBoundary: "push",
881
- markDirty: true,
882
- mapping: result.mapping,
883
- effects: reviewState.effects,
884
- },
885
- );
886
- }
887
-
888
- function applyReviewCommand(
889
- state: EditorState,
890
- command: ReviewCommand,
891
- timestamp: string,
892
- ): EditorTransaction {
893
- if (state.readOnly) {
894
- return createTransaction(state, {
895
- historyBoundary: "skip",
896
- markDirty: false,
897
- });
898
- }
899
-
900
- const result = applyRevisionRuntimeCommand({
901
- state: {
902
- document: state.document,
903
- store: createRevisionStoreFromState(state),
904
- },
905
- command,
906
- timestamp,
907
- });
908
- const hasAppliedOutcome = result.outcomes.some((outcome) => outcome.kind === "applied");
909
-
910
- if (!hasAppliedOutcome) {
911
- return createTransaction(state, {
912
- historyBoundary: "skip",
913
- markDirty: false,
914
- });
915
- }
916
-
917
- let selection = state.selection;
918
- let comments = result.document.review.comments;
919
- let warnings = state.warnings;
920
- let activeCommentId = state.runtime.activeCommentId;
921
- const mappingSteps: MappingStep[] = [];
922
-
923
- for (const entry of result.mappings) {
924
- if (entry.steps === 0) {
925
- continue;
926
- }
927
-
928
- selection = remapSelection(selection, entry.mapping);
929
- mappingSteps.push(...entry.mapping.steps);
930
-
931
- const remappedComments = remapCommentThreads({
932
- comments,
933
- mapping: entry.mapping,
934
- nextContent: result.document.content,
935
- existingWarnings: mapWarningsThroughMapping(warnings, entry.mapping),
936
- });
937
- comments = remappedComments.comments;
938
- warnings = remappedComments.warnings;
939
- activeCommentId =
940
- activeCommentId && comments[activeCommentId]
941
- ? activeCommentId
942
- : undefined;
943
- }
944
-
945
- warnings = mergeDetachedRevisionWarnings(result.store, warnings);
946
-
947
- const nextDocument = {
948
- ...result.document,
949
- review: {
950
- ...result.document.review,
951
- comments,
952
- revisions: toEditorRevisionRecords(result.store),
953
- },
954
- };
955
- const appliedRevisionIds = result.effects.appliedRevisionIds;
956
-
957
- return createTransaction(
958
- {
959
- ...state,
960
- document: nextDocument,
961
- selection,
962
- warnings,
963
- runtime: {
964
- ...state.runtime,
965
- activeCommentId,
966
- },
967
- },
968
- {
969
- historyBoundary: "push",
970
- markDirty: true,
971
- mapping: combineMappingSteps(mappingSteps),
972
- effects: {
973
- ...buildWarningEffects(state.warnings, warnings),
974
- changeAccepted:
975
- command.type === "review.accept-revision" && appliedRevisionIds[0]
976
- ? {
977
- changeId: appliedRevisionIds[0],
978
- }
979
- : undefined,
980
- changeRejected:
981
- command.type === "review.reject-revision" && appliedRevisionIds[0]
982
- ? {
983
- changeId: appliedRevisionIds[0],
984
- }
985
- : undefined,
986
- },
987
- },
988
- );
989
- }
990
-
991
- function remapReviewStateAfterContentChange(
992
- state: EditorState,
993
- nextDocument: CanonicalDocumentEnvelope,
994
- mapping: TransactionMapping,
995
- ): {
996
- document: CanonicalDocumentEnvelope;
997
- warnings: EditorWarning[];
998
- activeCommentId?: string;
999
- effects: Pick<TransactionEffects, "warningsAdded" | "warningsCleared">;
1000
- } {
1001
- const mappedWarnings = mapWarningsThroughMapping(state.warnings, mapping);
1002
- const remappedComments = remapCommentThreads({
1003
- comments: nextDocument.review.comments,
1004
- mapping,
1005
- nextContent: nextDocument.content,
1006
- existingWarnings: mappedWarnings,
1007
- });
1008
- const revisions = Object.fromEntries(
1009
- Object.entries(nextDocument.review.revisions).map(([changeId, revision]) => [
1010
- changeId,
1011
- {
1012
- ...revision,
1013
- anchor: mapAnchor(revision.anchor, mapping),
1014
- },
1015
- ]),
1016
- );
1017
- const activeCommentId =
1018
- state.runtime.activeCommentId &&
1019
- remappedComments.comments[state.runtime.activeCommentId]
1020
- ? state.runtime.activeCommentId
1021
- : undefined;
1022
- const warnings = remappedComments.warnings;
1023
-
1024
- return {
1025
- document: {
1026
- ...nextDocument,
1027
- review: {
1028
- ...nextDocument.review,
1029
- comments: remappedComments.comments,
1030
- revisions,
1031
- },
1032
- },
1033
- warnings,
1034
- activeCommentId,
1035
- effects: buildWarningEffects(state.warnings, warnings),
1036
- };
1037
- }
1038
-
1039
- function createRevisionStoreFromState(
1040
- state: Pick<EditorState, "document">,
1041
- ): RevisionStore {
1042
- return {
1043
- version: "revision-store/1",
1044
- revisions: Object.fromEntries(
1045
- Object.values(state.document.review.revisions).map((revision) => [
1046
- revision.changeId,
1047
- {
1048
- revisionId: revision.changeId,
1049
- kind: revision.kind,
1050
- anchor: revision.anchor,
1051
- authorId: revision.authorId ?? "unknown",
1052
- createdAt: revision.createdAt,
1053
- status: revision.status === "open" ? "active" : revision.status,
1054
- warningIds: [...(revision.warningIds ?? [])],
1055
- metadata: {
1056
- source: revision.metadata?.source ?? "runtime",
1057
- storyTarget: revision.metadata?.storyTarget,
1058
- preserveOnlyReason: revision.metadata?.preserveOnlyReason,
1059
- importedRevisionForm: revision.metadata?.importedRevisionForm,
1060
- originalRevisionType: revision.metadata?.originalRevisionType,
1061
- ooxmlRevisionId: revision.metadata?.ooxmlRevisionId,
1062
- },
1063
- },
1064
- ]),
1065
- ),
1066
- };
1067
- }
1068
-
1069
- function toEditorRevisionRecords(
1070
- store: RevisionStore,
1071
- ): EditorState["document"]["review"]["revisions"] {
1072
- return Object.fromEntries(
1073
- Object.values(store.revisions).map((revision) => [
1074
- revision.revisionId,
1075
- {
1076
- changeId: revision.revisionId,
1077
- kind: revision.kind,
1078
- anchor: revision.anchor,
1079
- authorId: revision.authorId,
1080
- createdAt: revision.createdAt,
1081
- warningIds: [...revision.warningIds],
1082
- metadata: {
1083
- source: revision.metadata.source,
1084
- storyTarget: revision.metadata.storyTarget,
1085
- preserveOnlyReason: revision.metadata.preserveOnlyReason,
1086
- importedRevisionForm: revision.metadata.importedRevisionForm,
1087
- originalRevisionType: revision.metadata.originalRevisionType,
1088
- ooxmlRevisionId: revision.metadata.ooxmlRevisionId,
1089
- },
1090
- status: revision.status === "active" ? "open" : revision.status,
1091
- },
1092
- ]),
1093
- );
1094
- }
1095
-
1096
- function mapWarningsThroughMapping(
1097
- warnings: EditorWarning[],
1098
- mapping: TransactionMapping,
1099
- ): EditorWarning[] {
1100
- return warnings.map((warning) =>
1101
- warning.affectedAnchor
1102
- ? {
1103
- ...warning,
1104
- affectedAnchor: mapAnchor(warning.affectedAnchor, mapping),
1105
- }
1106
- : warning,
1107
- );
1108
- }
1109
-
1110
- function buildWarningEffects(
1111
- previousWarnings: EditorWarning[],
1112
- nextWarnings: EditorWarning[],
1113
- ): Pick<TransactionEffects, "warningsAdded" | "warningsCleared"> {
1114
- const previousById = new Set(previousWarnings.map((warning) => warning.warningId));
1115
- const nextById = new Set(nextWarnings.map((warning) => warning.warningId));
1116
-
1117
- return {
1118
- warningsAdded: nextWarnings.filter((warning) => !previousById.has(warning.warningId)),
1119
- warningsCleared: previousWarnings
1120
- .filter((warning) => !nextById.has(warning.warningId))
1121
- .map((warning) => ({
1122
- warningId: warning.warningId,
1123
- code: warning.code,
1124
- })),
1125
- };
1126
- }
1127
-
1128
- function mergeDetachedRevisionWarnings(
1129
- store: RevisionStore,
1130
- existingWarnings: EditorWarning[],
1131
- ): EditorWarning[] {
1132
- const retainedWarnings = existingWarnings.filter(
1133
- (warning) =>
1134
- warning.code !== "revision_anchor_detached" ||
1135
- !warning.details ||
1136
- typeof warning.details.changeId !== "string" ||
1137
- store.revisions[warning.details.changeId]?.status === "detached",
1138
- );
1139
-
1140
- const knownDetachedIds = new Set(
1141
- retainedWarnings
1142
- .filter((warning) => warning.code === "revision_anchor_detached")
1143
- .map((warning) =>
1144
- typeof warning.details?.changeId === "string"
1145
- ? warning.details.changeId
1146
- : undefined,
1147
- )
1148
- .filter((value): value is string => Boolean(value)),
1149
- );
1150
-
1151
- const detachedWarnings = Object.values(store.revisions)
1152
- .filter(
1153
- (revision) =>
1154
- revision.status === "detached" && !knownDetachedIds.has(revision.revisionId),
1155
- )
1156
- .map((revision) => ({
1157
- warningId: `warning:revision-anchor-detached:${revision.revisionId}`,
1158
- code: "revision_anchor_detached" as const,
1159
- severity: "warning" as const,
1160
- message: `Revision ${revision.revisionId} detached after review remapping.`,
1161
- source: "review" as const,
1162
- affectedAnchor: revision.anchor.kind === "detached" ? revision.anchor : undefined,
1163
- details: {
1164
- changeId: revision.revisionId,
1165
- reason:
1166
- revision.anchor.kind === "detached"
1167
- ? revision.anchor.reason
1168
- : undefined,
1169
- },
1170
- }));
1171
-
1172
- return [...retainedWarnings, ...detachedWarnings];
1173
- }
1174
-
1175
- function combineMappingSteps(
1176
- steps: MappingStep[],
1177
- ): TransactionMapping {
1178
- return steps.length > 0
1179
- ? {
1180
- steps,
1181
- }
1182
- : createEmptyMapping();
1183
- }
1184
-
1185
- // ---------------------------------------------------------------------------
1186
- // Suggesting mode: creates revision records instead of (or alongside) text mutations
1187
- // ---------------------------------------------------------------------------
1188
-
1189
- let suggestingRevisionCounter = 0;
1190
-
1191
- function createSuggestingRevisionId(
1192
- existing: Record<string, unknown>,
1193
- timestamp: string,
1194
- ): string {
1195
- suggestingRevisionCounter += 1;
1196
- const ts = timestamp.replace(/[^0-9]/gu, "");
1197
- let id = `change-${ts}-s${suggestingRevisionCounter}`;
1198
- while (existing[id]) {
1199
- suggestingRevisionCounter += 1;
1200
- id = `change-${ts}-s${suggestingRevisionCounter}`;
1201
- }
1202
- return id;
1203
- }
1204
-
1205
- function createAuthoredRevision(
1206
- existing: Record<string, unknown>,
1207
- kind: "insertion" | "deletion",
1208
- from: number,
1209
- to: number,
1210
- authorId: string,
1211
- timestamp: string,
1212
- ): CanonicalRevisionRecord {
1213
- const changeId = createSuggestingRevisionId(existing, timestamp);
1214
- return {
1215
- changeId,
1216
- kind,
1217
- anchor: {
1218
- kind: "range",
1219
- range: { from, to },
1220
- assoc: { start: 1, end: -1 },
1221
- },
1222
- authorId,
1223
- createdAt: timestamp,
1224
- warningIds: [],
1225
- metadata: {
1226
- source: "runtime",
1227
- },
1228
- status: "open",
1229
- };
1230
- }
1231
-
1232
- function createSuggestingUnsupportedTransaction(
1233
- state: EditorState,
1234
- message: string,
1235
- ): EditorTransaction {
1236
- return createTransaction(state, {
1237
- historyBoundary: "skip",
1238
- markDirty: false,
1239
- effects: {
1240
- commandBlocked: {
1241
- code: "suggesting_unsupported",
1242
- message,
1243
- },
1244
- },
1245
- });
1246
- }
1247
-
1248
- function isSingleParagraphSuggestingRange(
1249
- document: CanonicalDocumentEnvelope,
1250
- from: number,
1251
- to: number,
1252
- ): boolean {
1253
- const ranges: Array<{ start: number; end: number }> = [];
1254
- let cursor = 0;
1255
- let previousWasParagraph = false;
1256
-
1257
- for (const block of document.content.children) {
1258
- if (block.type === "paragraph") {
1259
- if (previousWasParagraph) {
1260
- cursor += 1;
1261
- }
1262
- const start = cursor;
1263
- cursor += block.children.reduce<number>((size, child) => {
1264
- if (child.type === "text") {
1265
- return size + child.text.length;
1266
- }
1267
- if (child.type === "hyperlink") {
1268
- return size + child.children.reduce<number>((childSize, entry) => {
1269
- if (entry.type === "text") {
1270
- return childSize + entry.text.length;
1271
- }
1272
- return childSize + 1;
1273
- }, 0);
1274
- }
1275
- return size + 1;
1276
- }, 0);
1277
- ranges.push({ start, end: cursor });
1278
- previousWasParagraph = true;
1279
- continue;
1280
- }
1281
-
1282
- cursor += 1;
1283
- previousWasParagraph = false;
1284
- }
1285
-
1286
- return ranges.some((range) => from >= range.start && to <= range.end);
1287
- }
1288
-
1289
- function applySuggestingInsert(
1290
- state: EditorState,
1291
- text: string,
1292
- context: CommandExecutionContext,
1293
- ): EditorTransaction | undefined {
1294
- if (state.readOnly) {
1295
- return createTransaction(state, { historyBoundary: "skip", markDirty: false });
1296
- }
1297
-
1298
- const authorId = context.defaultAuthorId ?? "unknown";
1299
- const selection = state.selection;
1300
- const from = Math.min(selection.anchor, selection.head);
1301
- const to = Math.max(selection.anchor, selection.head);
1302
- const isCollapsed = from === to;
1303
-
1304
- if (!isCollapsed && !isSingleParagraphSuggestingRange(state.document, from, to)) {
1305
- return createSuggestingUnsupportedTransaction(
1306
- state,
1307
- "Suggesting mode does not yet support multi-paragraph replacement ranges.",
1308
- );
1309
- }
1310
-
1311
- if (isCollapsed) {
1312
- // Pure insertion at cursor: apply normally, then create or extend an insertion revision.
1313
- const result = insertText(state.document, selection, text, { timestamp: context.timestamp });
1314
- const insertedFrom = from;
1315
- const insertedTo = from + Array.from(text).length;
1316
-
1317
- // Remap existing review state first (comments, pre-existing revisions)
1318
- const reviewState = remapReviewStateAfterContentChange(
1319
- state,
1320
- result.document,
1321
- result.mapping,
1322
- );
1323
-
1324
- // If there is an authored insertion revision that ends exactly where this
1325
- // text was inserted (i.e. the cursor was at the end of a prior insertion),
1326
- // extend that revision to cover the new text rather than creating a new one.
1327
- // This groups consecutive keystrokes into a single tracked change.
1328
- const adjacentInsertion = findAdjacentAuthoredInsertion(
1329
- reviewState.document.review.revisions,
1330
- insertedFrom,
1331
- );
1332
-
1333
- let finalRevisionId: string;
1334
- let finalRevisions: Record<string, CanonicalRevisionRecord>;
1335
-
1336
- if (adjacentInsertion && adjacentInsertion.anchor.kind === "range") {
1337
- const extended: CanonicalRevisionRecord = {
1338
- ...adjacentInsertion,
1339
- anchor: {
1340
- kind: "range",
1341
- range: { from: adjacentInsertion.anchor.range.from, to: insertedTo },
1342
- assoc: { start: 1, end: -1 },
1343
- },
1344
- };
1345
- finalRevisionId = extended.changeId;
1346
- finalRevisions = {
1347
- ...reviewState.document.review.revisions,
1348
- [extended.changeId]: extended,
1349
- };
1350
- } else {
1351
- const revision = createAuthoredRevision(
1352
- reviewState.document.review.revisions,
1353
- "insertion",
1354
- insertedFrom,
1355
- insertedTo,
1356
- authorId,
1357
- context.timestamp,
1358
- );
1359
- finalRevisionId = revision.changeId;
1360
- finalRevisions = {
1361
- ...reviewState.document.review.revisions,
1362
- [revision.changeId]: revision,
1363
- };
1364
- }
1365
-
1366
- const finalDocument: CanonicalDocumentEnvelope = {
1367
- ...reviewState.document,
1368
- review: {
1369
- ...reviewState.document.review,
1370
- revisions: finalRevisions,
1371
- },
1372
- };
1373
-
1374
- return createTransaction(
1375
- {
1376
- ...state,
1377
- document: finalDocument,
1378
- selection: result.selection,
1379
- warnings: reviewState.warnings,
1380
- runtime: {
1381
- ...state.runtime,
1382
- activeCommentId: reviewState.activeCommentId,
1383
- },
1384
- },
1385
- {
1386
- historyBoundary: "push",
1387
- markDirty: true,
1388
- mapping: result.mapping,
1389
- effects: {
1390
- ...reviewState.effects,
1391
- revisionAuthored: { changeId: finalRevisionId, kind: "insertion" },
1392
- },
1393
- },
1394
- );
1395
- }
1396
-
1397
- // Non-collapsed selection: mark selected text as deletion, insert new text after selection
1398
- // Step 1: Insert new text at the end of the selection
1399
- const insertSelection = createSelectionSnapshot(to, to);
1400
- const result = insertText(state.document, insertSelection, text, { timestamp: context.timestamp });
1401
- const insertedFrom = to;
1402
- const insertedTo = to + Array.from(text).length;
1403
-
1404
- // Step 2: Remap existing review state through the mapping first
1405
- const reviewState = remapReviewStateAfterContentChange(
1406
- state,
1407
- result.document,
1408
- result.mapping,
1409
- );
1410
-
1411
- // Step 3: Create deletion revision for the selected range (text stays in place).
1412
- // Deletion range uses pre-mapping positions since content was not removed.
1413
- const deletionRevision = createAuthoredRevision(
1414
- reviewState.document.review.revisions,
1415
- "deletion",
1416
- from,
1417
- to,
1418
- authorId,
1419
- context.timestamp,
1420
- );
1421
-
1422
- // Step 4: Create insertion revision for the new text (positions already correct)
1423
- const insertionRevision = createAuthoredRevision(
1424
- {
1425
- ...reviewState.document.review.revisions,
1426
- [deletionRevision.changeId]: deletionRevision,
1427
- },
1428
- "insertion",
1429
- insertedFrom,
1430
- insertedTo,
1431
- authorId,
1432
- context.timestamp,
1433
- );
1434
-
1435
- const finalDocument: CanonicalDocumentEnvelope = {
1436
- ...reviewState.document,
1437
- review: {
1438
- ...reviewState.document.review,
1439
- revisions: {
1440
- ...reviewState.document.review.revisions,
1441
- [deletionRevision.changeId]: deletionRevision,
1442
- [insertionRevision.changeId]: insertionRevision,
1443
- },
1444
- },
1445
- };
1446
-
1447
- // Move cursor to after inserted text
1448
- const caretPos = insertedTo;
1449
-
1450
- return createTransaction(
1451
- {
1452
- ...state,
1453
- document: finalDocument,
1454
- selection: createSelectionSnapshot(caretPos, caretPos),
1455
- warnings: reviewState.warnings,
1456
- runtime: {
1457
- ...state.runtime,
1458
- activeCommentId: reviewState.activeCommentId,
1459
- },
1460
- },
1461
- {
1462
- historyBoundary: "push",
1463
- markDirty: true,
1464
- mapping: result.mapping,
1465
- effects: {
1466
- ...reviewState.effects,
1467
- revisionAuthored: { changeId: insertionRevision.changeId, kind: "insertion" },
1468
- },
1469
- },
1470
- );
1471
- }
1472
-
1473
- function applySuggestingDelete(
1474
- state: EditorState,
1475
- direction: "backward" | "forward",
1476
- context: CommandExecutionContext,
1477
- ): EditorTransaction | undefined {
1478
- if (state.readOnly) {
1479
- return createTransaction(state, { historyBoundary: "skip", markDirty: false });
1480
- }
1481
-
1482
- const authorId = context.defaultAuthorId ?? "unknown";
1483
- const selection = state.selection;
1484
- const from = Math.min(selection.anchor, selection.head);
1485
- const to = Math.max(selection.anchor, selection.head);
1486
- const isCollapsed = from === to;
1487
-
1488
- if (!isCollapsed && !isSingleParagraphSuggestingRange(state.document, from, to)) {
1489
- return createSuggestingUnsupportedTransaction(
1490
- state,
1491
- "Suggesting mode does not yet support multi-paragraph deletion ranges.",
1492
- );
1493
- }
1494
-
1495
- let deleteFrom: number;
1496
- let deleteTo: number;
1497
-
1498
- if (isCollapsed) {
1499
- if (direction === "backward") {
1500
- deleteFrom = Math.max(0, from - 1);
1501
- deleteTo = from;
1502
- } else {
1503
- deleteFrom = from;
1504
- deleteTo = from + 1;
1505
- }
1506
- } else {
1507
- deleteFrom = from;
1508
- deleteTo = to;
1509
- }
1510
-
1511
- // Validate the deletion range doesn't cross protected content
1512
- if (deleteFrom === deleteTo) {
1513
- return createTransaction(state, { historyBoundary: "skip", markDirty: false });
1514
- }
1515
-
1516
- // Check if the range overlaps an existing deletion revision from this suggesting session
1517
- // If so, extend the existing revision rather than creating a new one
1518
- const existingDeletion = findOverlappingAuthoredDeletion(
1519
- state.document.review.revisions,
1520
- deleteFrom,
1521
- deleteTo,
1522
- );
1523
-
1524
- if (existingDeletion) {
1525
- // Extend the existing deletion revision to cover the new range
1526
- const extendedFrom = Math.min(
1527
- existingDeletion.anchor.kind === "range" ? existingDeletion.anchor.range.from : deleteFrom,
1528
- deleteFrom,
1529
- );
1530
- const extendedTo = Math.max(
1531
- existingDeletion.anchor.kind === "range" ? existingDeletion.anchor.range.to : deleteTo,
1532
- deleteTo,
1533
- );
1534
-
1535
- const updatedRevision: CanonicalRevisionRecord = {
1536
- ...existingDeletion,
1537
- anchor: {
1538
- kind: "range",
1539
- range: { from: extendedFrom, to: extendedTo },
1540
- assoc: { start: 1, end: -1 },
1541
- },
1542
- };
1543
-
1544
- const nextDocument: CanonicalDocumentEnvelope = {
1545
- ...state.document,
1546
- updatedAt: context.timestamp,
1547
- review: {
1548
- ...state.document.review,
1549
- revisions: {
1550
- ...state.document.review.revisions,
1551
- [updatedRevision.changeId]: updatedRevision,
1552
- },
1553
- },
1554
- };
1555
-
1556
- const caretPos = direction === "backward" ? deleteFrom : deleteTo;
1557
-
1558
- return createTransaction(
1559
- {
1560
- ...state,
1561
- document: nextDocument,
1562
- selection: createSelectionSnapshot(caretPos, caretPos),
1563
- },
1564
- {
1565
- historyBoundary: "push",
1566
- markDirty: true,
1567
- effects: {
1568
- revisionAuthored: { changeId: updatedRevision.changeId, kind: "deletion" },
1569
- },
1570
- },
1571
- );
1572
- }
1573
-
1574
- // Create a new deletion revision — text stays in the document
1575
- const revision = createAuthoredRevision(
1576
- state.document.review.revisions,
1577
- "deletion",
1578
- deleteFrom,
1579
- deleteTo,
1580
- authorId,
1581
- context.timestamp,
1582
- );
1583
-
1584
- const nextDocument: CanonicalDocumentEnvelope = {
1585
- ...state.document,
1586
- updatedAt: context.timestamp,
1587
- review: {
1588
- ...state.document.review,
1589
- revisions: {
1590
- ...state.document.review.revisions,
1591
- [revision.changeId]: revision,
1592
- },
1593
- },
1594
- };
1595
-
1596
- const caretPos = direction === "backward" ? deleteFrom : deleteTo;
1597
-
1598
- return createTransaction(
1599
- {
1600
- ...state,
1601
- document: nextDocument,
1602
- selection: createSelectionSnapshot(caretPos, caretPos),
1603
- },
1604
- {
1605
- historyBoundary: "push",
1606
- markDirty: true,
1607
- effects: {
1608
- revisionAuthored: { changeId: revision.changeId, kind: "deletion" },
1609
- },
1610
- },
1611
- );
1612
- }
1613
-
1614
- function applySuggestingInsertUnit(
1615
- state: EditorState,
1616
- unitType: "tab" | "hard_break",
1617
- context: CommandExecutionContext,
1618
- ): EditorTransaction | undefined {
1619
- if (state.readOnly) {
1620
- return createTransaction(state, { historyBoundary: "skip", markDirty: false });
1621
- }
1622
-
1623
- const authorId = context.defaultAuthorId ?? "unknown";
1624
- const selection = state.selection;
1625
- const from = Math.min(selection.anchor, selection.head);
1626
- const to = Math.max(selection.anchor, selection.head);
1627
-
1628
- if (from !== to && !isSingleParagraphSuggestingRange(state.document, from, to)) {
1629
- return createSuggestingUnsupportedTransaction(
1630
- state,
1631
- "Suggesting mode does not yet support multi-paragraph replacement ranges.",
1632
- );
1633
- }
1634
-
1635
- // Insert the unit at the end of the selection (or at cursor if collapsed)
1636
- const insertPos = to;
1637
- const insertSelection = createSelectionSnapshot(insertPos, insertPos);
1638
- const applyFn = unitType === "tab" ? insertTab : insertHardBreak;
1639
- const result = applyFn(state.document, insertSelection, { timestamp: context.timestamp });
1640
-
1641
- // Remap existing review state first, before adding new revisions
1642
- const reviewState = remapReviewStateAfterContentChange(
1643
- state,
1644
- result.document,
1645
- result.mapping,
1646
- );
1647
-
1648
- // If non-collapsed, mark selected range as deletion (positions are pre-mapping, content preserved)
1649
- const deletionRevision = from !== to
1650
- ? createAuthoredRevision(
1651
- reviewState.document.review.revisions,
1652
- "deletion",
1653
- from,
1654
- to,
1655
- authorId,
1656
- context.timestamp,
1657
- )
1658
- : undefined;
1659
-
1660
- // Create insertion revision for the single inserted unit (positions already correct)
1661
- const insertionRevision = createAuthoredRevision(
1662
- {
1663
- ...reviewState.document.review.revisions,
1664
- ...(deletionRevision ? { [deletionRevision.changeId]: deletionRevision } : {}),
1665
- },
1666
- "insertion",
1667
- insertPos,
1668
- insertPos + 1,
1669
- authorId,
1670
- context.timestamp,
1671
- );
1672
-
1673
- const finalDocument: CanonicalDocumentEnvelope = {
1674
- ...reviewState.document,
1675
- review: {
1676
- ...reviewState.document.review,
1677
- revisions: {
1678
- ...reviewState.document.review.revisions,
1679
- ...(deletionRevision ? { [deletionRevision.changeId]: deletionRevision } : {}),
1680
- [insertionRevision.changeId]: insertionRevision,
1681
- },
1682
- },
1683
- };
1684
-
1685
- return createTransaction(
1686
- {
1687
- ...state,
1688
- document: finalDocument,
1689
- selection: result.selection,
1690
- warnings: reviewState.warnings,
1691
- runtime: {
1692
- ...state.runtime,
1693
- activeCommentId: reviewState.activeCommentId,
1694
- },
1695
- },
1696
- {
1697
- historyBoundary: "push",
1698
- markDirty: true,
1699
- mapping: result.mapping,
1700
- effects: {
1701
- ...reviewState.effects,
1702
- revisionAuthored: { changeId: insertionRevision.changeId, kind: "insertion" },
1703
- },
1704
- },
1705
- );
1706
- }
1707
-
1708
- function findOverlappingAuthoredDeletion(
1709
- revisions: Record<string, CanonicalRevisionRecord>,
1710
- from: number,
1711
- to: number,
1712
- ): CanonicalRevisionRecord | undefined {
1713
- for (const revision of Object.values(revisions)) {
1714
- if (
1715
- revision.kind === "deletion" &&
1716
- revision.status === "open" &&
1717
- revision.metadata?.source === "runtime" &&
1718
- revision.anchor.kind === "range"
1719
- ) {
1720
- const revFrom = revision.anchor.range.from;
1721
- const revTo = revision.anchor.range.to;
1722
- // Adjacent or overlapping: the deletion is directly next to or overlapping the range
1723
- if (
1724
- (from >= revFrom && from <= revTo) ||
1725
- (to >= revFrom && to <= revTo) ||
1726
- (from === revTo) ||
1727
- (to === revFrom)
1728
- ) {
1729
- return revision;
1730
- }
1731
- }
1732
- }
1733
- return undefined;
1734
- }
1735
-
1736
- /**
1737
- * Find an open authored insertion revision whose end position equals `cursorAt`.
1738
- * Used to extend an existing insertion when the user keeps typing at the same
1739
- * position rather than creating a new revision for every character.
1740
- */
1741
- function findAdjacentAuthoredInsertion(
1742
- revisions: Record<string, CanonicalRevisionRecord>,
1743
- cursorAt: number,
1744
- ): CanonicalRevisionRecord | undefined {
1745
- for (const revision of Object.values(revisions)) {
1746
- if (
1747
- revision.kind === "insertion" &&
1748
- revision.status === "open" &&
1749
- revision.metadata?.source === "runtime" &&
1750
- revision.anchor.kind === "range" &&
1751
- revision.anchor.range.to === cursorAt
1752
- ) {
1753
- return revision;
1754
- }
1755
- }
1756
- return undefined;
1757
- }