@beyondwork/docx-react-component 1.0.120 → 1.0.121

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 (116) hide show
  1. package/README.md +1 -0
  2. package/dist/api/public-types.cjs +1713 -55
  3. package/dist/api/public-types.d.cts +2 -2
  4. package/dist/api/public-types.d.ts +2 -2
  5. package/dist/api/public-types.js +6 -6
  6. package/dist/api/v3.cjs +4958 -406
  7. package/dist/api/v3.d.cts +3 -3
  8. package/dist/api/v3.d.ts +3 -3
  9. package/dist/api/v3.js +14 -14
  10. package/dist/{canonical-document-fNawStsc.d.cts → canonical-document-ByIqTd4s.d.cts} +9 -1
  11. package/dist/{canonical-document-fNawStsc.d.ts → canonical-document-ByIqTd4s.d.ts} +9 -1
  12. package/dist/{chunk-5RNMPLXU.js → chunk-37SEJQ3G.js} +4 -4
  13. package/dist/{chunk-FXGQM2JB.js → chunk-3OFSP2IX.js} +11 -5
  14. package/dist/{chunk-U5BSQXF4.js → chunk-3OHVK2D6.js} +70 -12
  15. package/dist/{chunk-AUQDC5BD.js → chunk-3TUQCHYT.js} +101 -2
  16. package/dist/{chunk-SJSMYEMU.js → chunk-B4YHWFE3.js} +3 -3
  17. package/dist/{chunk-XC56YLIS.js → chunk-C2LWJ4CZ.js} +4 -0
  18. package/dist/{chunk-VDIUVT46.js → chunk-CX42VC67.js} +1 -1
  19. package/dist/{chunk-KCHEAX4Z.js → chunk-EMDH4IQN.js} +148 -70
  20. package/dist/{chunk-TMQGWF7R.js → chunk-G3B2OBCZ.js} +352 -17
  21. package/dist/{chunk-VCL5MJMZ.js → chunk-GON2DNTE.js} +149 -28
  22. package/dist/{chunk-WVZX4NYG.js → chunk-GZW2ERUO.js} +601 -47
  23. package/dist/{chunk-WDNEPRFW.js → chunk-ICX54W4U.js} +1 -1
  24. package/dist/{chunk-FIGWJ43K.js → chunk-IT2DK3A7.js} +1883 -90
  25. package/dist/{chunk-2ZWFQ74R.js → chunk-OBCP6VTG.js} +1 -1
  26. package/dist/{chunk-FLNQY74K.js → chunk-OYGMRRR7.js} +1 -1
  27. package/dist/{chunk-MPYYBRVN.js → chunk-PCXTMEGY.js} +782 -124
  28. package/dist/{chunk-4JNUMMM7.js → chunk-PGGPPZ65.js} +17 -2
  29. package/dist/{chunk-KHZNNBTN.js → chunk-QFU7ZOAD.js} +43 -39
  30. package/dist/{chunk-4ZNQFWFM.js → chunk-QIO6V46H.js} +84 -4
  31. package/dist/{chunk-IQ2VJEF6.js → chunk-QNGJRZ2D.js} +1 -1
  32. package/dist/{chunk-BM5NSDII.js → chunk-S4ANOS2M.js} +2 -2
  33. package/dist/{chunk-AQA7OZ2R.js → chunk-TFSXOIAI.js} +959 -43
  34. package/dist/{chunk-NQZUGMLW.js → chunk-TMU7JMXX.js} +184 -32
  35. package/dist/{chunk-KD5K5XIA.js → chunk-UHQOUTAX.js} +568 -88
  36. package/dist/{chunk-327AIUXL.js → chunk-UWDWGQH5.js} +11 -4
  37. package/dist/{chunk-BBB4GSDB.js → chunk-XVFENXLK.js} +2 -2
  38. package/dist/{chunk-MUEN2WOG.js → chunk-ZKSDVHGH.js} +6 -3
  39. package/dist/compare.cjs +17 -2
  40. package/dist/compare.d.cts +1 -1
  41. package/dist/compare.d.ts +1 -1
  42. package/dist/compare.js +3 -3
  43. package/dist/core/commands/formatting-commands.d.cts +2 -2
  44. package/dist/core/commands/formatting-commands.d.ts +2 -2
  45. package/dist/core/commands/image-commands.cjs +814 -45
  46. package/dist/core/commands/image-commands.d.cts +2 -2
  47. package/dist/core/commands/image-commands.d.ts +2 -2
  48. package/dist/core/commands/image-commands.js +8 -8
  49. package/dist/core/commands/section-layout-commands.d.cts +2 -2
  50. package/dist/core/commands/section-layout-commands.d.ts +2 -2
  51. package/dist/core/commands/style-commands.d.cts +2 -2
  52. package/dist/core/commands/style-commands.d.ts +2 -2
  53. package/dist/core/commands/table-structure-commands.cjs +750 -42
  54. package/dist/core/commands/table-structure-commands.d.cts +2 -2
  55. package/dist/core/commands/table-structure-commands.d.ts +2 -2
  56. package/dist/core/commands/table-structure-commands.js +6 -6
  57. package/dist/core/commands/text-commands.cjs +910 -57
  58. package/dist/core/commands/text-commands.d.cts +2 -2
  59. package/dist/core/commands/text-commands.d.ts +2 -2
  60. package/dist/core/commands/text-commands.js +8 -8
  61. package/dist/core/selection/mapping.d.cts +2 -2
  62. package/dist/core/selection/mapping.d.ts +2 -2
  63. package/dist/core/state/editor-state.cjs +17 -2
  64. package/dist/core/state/editor-state.d.cts +2 -2
  65. package/dist/core/state/editor-state.d.ts +2 -2
  66. package/dist/core/state/editor-state.js +2 -2
  67. package/dist/index.cjs +6203 -625
  68. package/dist/index.d.cts +5 -5
  69. package/dist/index.d.ts +5 -5
  70. package/dist/index.js +299 -67
  71. package/dist/io/docx-session.cjs +354 -102
  72. package/dist/io/docx-session.d.cts +4 -4
  73. package/dist/io/docx-session.d.ts +4 -4
  74. package/dist/io/docx-session.js +5 -5
  75. package/dist/legal.cjs +183 -31
  76. package/dist/legal.d.cts +1 -1
  77. package/dist/legal.d.ts +1 -1
  78. package/dist/legal.js +3 -3
  79. package/dist/{loader-CaohrhNl.d.ts → loader-BF8ju_LK.d.ts} +22 -4
  80. package/dist/{loader-BpCyGnZl.d.cts → loader-g54WRvj1.d.cts} +22 -4
  81. package/dist/{public-types-Dpch9JG0.d.cts → public-types-D_y4Ptcj.d.cts} +747 -21
  82. package/dist/{public-types-C948HNVF.d.ts → public-types-Dl1jiWjk.d.ts} +747 -21
  83. package/dist/public-types.cjs +1713 -55
  84. package/dist/public-types.d.cts +2 -2
  85. package/dist/public-types.d.ts +2 -2
  86. package/dist/public-types.js +6 -6
  87. package/dist/runtime/collab.cjs +4 -0
  88. package/dist/runtime/collab.d.cts +3 -3
  89. package/dist/runtime/collab.d.ts +3 -3
  90. package/dist/runtime/collab.js +2 -2
  91. package/dist/runtime/document-runtime.cjs +3699 -507
  92. package/dist/runtime/document-runtime.d.cts +2 -2
  93. package/dist/runtime/document-runtime.d.ts +2 -2
  94. package/dist/runtime/document-runtime.js +18 -18
  95. package/dist/{session-Dqg17V3s.d.ts → session-C1EPAkcI.d.ts} +3 -3
  96. package/dist/{session-BlXE5zxz.d.cts → session-D15QOO0Q.d.cts} +3 -3
  97. package/dist/session.cjs +951 -104
  98. package/dist/session.d.cts +5 -5
  99. package/dist/session.d.ts +5 -5
  100. package/dist/session.js +10 -8
  101. package/dist/tailwind.cjs +1752 -91
  102. package/dist/tailwind.d.cts +2 -2
  103. package/dist/tailwind.d.ts +2 -2
  104. package/dist/tailwind.js +10 -10
  105. package/dist/{types-C9vZVpKy.d.cts → types-BoSRp2Vg.d.cts} +2 -2
  106. package/dist/{types-B1tlF1bq.d.ts → types-DEvRwq9C.d.ts} +2 -2
  107. package/dist/ui-tailwind/editor-surface/search-plugin.d.cts +3 -3
  108. package/dist/ui-tailwind/editor-surface/search-plugin.d.ts +3 -3
  109. package/dist/ui-tailwind/editor-surface/search-plugin.js +7 -7
  110. package/dist/ui-tailwind/theme/editor-theme.css +5 -5
  111. package/dist/ui-tailwind.cjs +1752 -91
  112. package/dist/ui-tailwind.d.cts +8 -8
  113. package/dist/ui-tailwind.d.ts +8 -8
  114. package/dist/ui-tailwind.js +10 -10
  115. package/package.json +17 -5
  116. package/dist/ui-tailwind/theme/tokens.css +0 -382
@@ -11,7 +11,7 @@ import {
11
11
  } from "./chunk-M6XR534O.js";
12
12
  import {
13
13
  buildBookmarkNameMap
14
- } from "./chunk-2ZWFQ74R.js";
14
+ } from "./chunk-OBCP6VTG.js";
15
15
  import {
16
16
  deleteSelectionOrBackward,
17
17
  deleteSelectionOrForward,
@@ -22,7 +22,7 @@ import {
22
22
  insertText,
23
23
  outdentParagraphAtSelection,
24
24
  splitParagraph
25
- } from "./chunk-FXGQM2JB.js";
25
+ } from "./chunk-3OFSP2IX.js";
26
26
  import {
27
27
  applyFormattingOperationToDocument,
28
28
  applyTextMarkOperationToDocumentRange,
@@ -32,19 +32,18 @@ import {
32
32
  insertImage,
33
33
  repositionFloatingImage,
34
34
  resizeImage
35
- } from "./chunk-5RNMPLXU.js";
35
+ } from "./chunk-37SEJQ3G.js";
36
36
  import {
37
37
  applyTextTransaction
38
- } from "./chunk-U5BSQXF4.js";
38
+ } from "./chunk-3OHVK2D6.js";
39
39
  import {
40
- applyTableStructureOperation,
41
40
  applyTableStructureOperationForEditableTarget,
42
41
  removeCellFromRow,
43
42
  removeTableRowPure
44
- } from "./chunk-BBB4GSDB.js";
43
+ } from "./chunk-XVFENXLK.js";
45
44
  import {
46
45
  resolveParagraphScope
47
- } from "./chunk-MUEN2WOG.js";
46
+ } from "./chunk-ZKSDVHGH.js";
48
47
  import {
49
48
  buildGeometryDebugEntry,
50
49
  collectLineBoxesForRegion,
@@ -52,7 +51,7 @@ import {
52
51
  createRenderKernel,
53
52
  incrementInvalidationCounter,
54
53
  recordPerfSample
55
- } from "./chunk-WVZX4NYG.js";
54
+ } from "./chunk-GZW2ERUO.js";
56
55
  import {
57
56
  AI_ACTION_POLICIES,
58
57
  buildFormattingDebugEntry,
@@ -69,12 +68,13 @@ import {
69
68
  findTextMatches,
70
69
  findTextWithStyleMatches,
71
70
  rebuildFieldRegistry,
71
+ resolveEditableCommandTarget,
72
72
  resolveEditableTableStructureTarget,
73
73
  resolveEditableTextTarget,
74
74
  resolveHeadingPath,
75
75
  resolveScope,
76
76
  serializeFragmentToWordML
77
- } from "./chunk-MPYYBRVN.js";
77
+ } from "./chunk-PCXTMEGY.js";
78
78
  import {
79
79
  ISSUE_METADATA_ID,
80
80
  LAYOUT_ENGINE_VERSION,
@@ -109,14 +109,14 @@ import {
109
109
  setWorkspaceMode,
110
110
  setZoomLevel,
111
111
  snapCommentAnchorAwayFromTable
112
- } from "./chunk-TMQGWF7R.js";
112
+ } from "./chunk-G3B2OBCZ.js";
113
113
  import {
114
114
  countLogicalPositions,
115
115
  createPlainText,
116
116
  logicalPositionToUnitIndex,
117
117
  parseTextStory,
118
118
  serializeTextStory
119
- } from "./chunk-4ZNQFWFM.js";
119
+ } from "./chunk-QIO6V46H.js";
120
120
  import {
121
121
  ThemeColorResolver,
122
122
  createEditorSurfaceSnapshot,
@@ -128,18 +128,18 @@ import {
128
128
  replaceStoryBlocks,
129
129
  resolvePageFieldDisplayText,
130
130
  storyTargetKey
131
- } from "./chunk-AUQDC5BD.js";
131
+ } from "./chunk-3TUQCHYT.js";
132
132
  import {
133
133
  createCommentSidebarProjection,
134
134
  createCommentStoreFromRuntimeComments
135
- } from "./chunk-VDIUVT46.js";
135
+ } from "./chunk-CX42VC67.js";
136
136
  import {
137
137
  createEditorState,
138
138
  createPersistedEditorSnapshot,
139
139
  createSelectionSnapshot,
140
140
  deriveDocumentStats,
141
141
  normalizeCommentThreadRecord
142
- } from "./chunk-FLNQY74K.js";
142
+ } from "./chunk-OYGMRRR7.js";
143
143
  import {
144
144
  buildCompatibilityReport,
145
145
  createDiagnostics,
@@ -152,12 +152,18 @@ import {
152
152
  persistedSnapshotFromEditorSessionState,
153
153
  remapRevisionStore,
154
154
  setRevisionStatus
155
- } from "./chunk-327AIUXL.js";
155
+ } from "./chunk-UWDWGQH5.js";
156
156
  import {
157
157
  isSupportedFieldFamily,
158
158
  parseTocLevelRange,
159
159
  resolveRefFieldText
160
- } from "./chunk-NQZUGMLW.js";
160
+ } from "./chunk-TMU7JMXX.js";
161
+ import {
162
+ collectEditableTargetRefs,
163
+ collectStoryBlockContexts,
164
+ createEditableTargetBlockCache,
165
+ createHeaderFooterStoryKey
166
+ } from "./chunk-TFSXOIAI.js";
161
167
  import {
162
168
  setActiveSerializeTelemetryBus
163
169
  } from "./chunk-EB6M3GE6.js";
@@ -170,7 +176,7 @@ import {
170
176
  createPublicRangeAnchor,
171
177
  toInternalAnchorProjection,
172
178
  toPublicAnchorProjection
173
- } from "./chunk-4JNUMMM7.js";
179
+ } from "./chunk-PGGPPZ65.js";
174
180
  import {
175
181
  DEFAULT_BOUNDARY_ASSOC,
176
182
  MAIN_STORY_TARGET,
@@ -1686,27 +1692,10 @@ function applyFragmentInsert(document, selection, fragment, context) {
1686
1692
  };
1687
1693
  }
1688
1694
  if (scope.kind === "table-cell") {
1689
- const spliced = insertFragmentBlocksAfterTableCellScope(
1690
- splitRoot,
1691
- scope,
1692
- fragment.blocks
1693
- );
1694
- if (!spliced) {
1695
- return {
1696
- changed: false,
1697
- document,
1698
- selection
1699
- };
1700
- }
1701
1695
  return {
1702
- changed: true,
1703
- document: {
1704
- ...splitResult.document,
1705
- updatedAt: context.timestamp,
1706
- content: spliced
1707
- },
1708
- selection: createSelectionSnapshot(0, 0),
1709
- mapping: createEmptyMapping()
1696
+ changed: false,
1697
+ document,
1698
+ selection
1710
1699
  };
1711
1700
  }
1712
1701
  const rightHalfIndex = scope.blockIndex + 1;
@@ -1814,35 +1803,6 @@ function insertBlocksAfterPathInBlocks(blocks, tokens, tokenIndex, fragmentBlock
1814
1803
  };
1815
1804
  });
1816
1805
  }
1817
- function insertFragmentBlocksAfterTableCellScope(root, scope, fragmentBlocks) {
1818
- return {
1819
- ...root,
1820
- children: root.children.map((block, blockIndex) => {
1821
- if (blockIndex !== scope.tableBlockIndex || block.type !== "table") {
1822
- return block;
1823
- }
1824
- return {
1825
- ...block,
1826
- rows: block.rows.map((row2, rowIndex) => {
1827
- if (rowIndex !== scope.rowIndex) return row2;
1828
- return {
1829
- ...row2,
1830
- cells: row2.cells.map(
1831
- (cell, cellIndex) => cellIndex === scope.cellIndex ? {
1832
- ...cell,
1833
- children: [
1834
- ...cell.children.slice(0, scope.childIndex + 1),
1835
- ...fragmentBlocks.map((candidate) => cloneBlock(candidate)),
1836
- ...cell.children.slice(scope.childIndex + 1)
1837
- ]
1838
- } : cell
1839
- )
1840
- };
1841
- })
1842
- };
1843
- })
1844
- };
1845
- }
1846
1806
  function parseCanonicalBlockPath(path) {
1847
1807
  const blockStart = path.indexOf("/block[");
1848
1808
  if (blockStart < 0) return null;
@@ -2006,6 +1966,9 @@ function executeEditorCommand(state, command, context) {
2006
1966
  );
2007
1967
  }
2008
1968
  case "table.apply-text": {
1969
+ if (!command.editableTarget || context.textTarget?.kind !== "table-paragraph") {
1970
+ return createTransaction(state, { historyBoundary: "skip", markDirty: false });
1971
+ }
2009
1972
  const textContext = context;
2010
1973
  const formatting = command.formatting ?? { mode: "match-replaced-range" };
2011
1974
  const suggestingResult = context.documentMode === "suggesting" ? applySuggestingInsert(state, command.operation.text, textContext) : void 0;
@@ -2588,20 +2551,15 @@ function executeEditorCommand(state, command, context) {
2588
2551
  });
2589
2552
  }
2590
2553
  case "table.apply-structure": {
2591
- if (!context.renderSnapshot) {
2554
+ if (!context.renderSnapshot || !command.editableTarget) {
2592
2555
  return createTransaction(state, { historyBoundary: "skip", markDirty: false });
2593
2556
  }
2594
- const result = command.editableTarget ? applyTableStructureOperationForEditableTarget(
2557
+ const result = applyTableStructureOperationForEditableTarget(
2595
2558
  state.document,
2596
2559
  context.renderSnapshot,
2597
2560
  command.selectionDescriptor,
2598
2561
  command.operation,
2599
2562
  command.editableTarget
2600
- ) : applyTableStructureOperation(
2601
- state.document,
2602
- context.renderSnapshot,
2603
- command.selectionDescriptor,
2604
- command.operation
2605
2563
  );
2606
2564
  return buildDocumentReplaceTransaction(state, context, {
2607
2565
  changed: result.changed,
@@ -2610,6 +2568,35 @@ function executeEditorCommand(state, command, context) {
2610
2568
  mapping: result.mapping
2611
2569
  });
2612
2570
  }
2571
+ case "field.refresh":
2572
+ case "toc.refresh":
2573
+ return createTransaction(state, { historyBoundary: "skip", markDirty: false });
2574
+ case "bookmark.edit-content": {
2575
+ const result = editBookmarkRangeContent(
2576
+ state.document,
2577
+ command.editableTarget,
2578
+ command.text,
2579
+ context.timestamp
2580
+ );
2581
+ return buildDocumentReplaceTransaction(state, context, {
2582
+ changed: result.changed,
2583
+ document: result.document,
2584
+ selection: state.selection
2585
+ });
2586
+ }
2587
+ case "hyperlink.update-destination": {
2588
+ const result = updateHyperlinkDestination(
2589
+ state.document,
2590
+ command.editableTarget,
2591
+ command.href,
2592
+ context.timestamp
2593
+ );
2594
+ return buildDocumentReplaceTransaction(state, context, {
2595
+ changed: result.changed,
2596
+ document: result.document,
2597
+ selection: state.selection
2598
+ });
2599
+ }
2613
2600
  case "image.insert": {
2614
2601
  const dataBytes = decodeBase64ToBytes(command.data);
2615
2602
  const result = insertImage(
@@ -2800,6 +2787,230 @@ function resolvePatchedContent(base, patch, contentChildren) {
2800
2787
  children: nextChildren
2801
2788
  };
2802
2789
  }
2790
+ function editBookmarkRangeContent(document, target, text, timestamp) {
2791
+ if (target.kind !== "bookmark-content-range" || target.commandFamily !== "link-bookmark") {
2792
+ return { changed: false, document };
2793
+ }
2794
+ const bookmarkId = target.link?.bookmarkId;
2795
+ if (!bookmarkId) return { changed: false, document };
2796
+ const replaced = replaceBlocksForEditableStory(document, target.storyKey, (blocks) => {
2797
+ const next = rewriteBookmarkRangeInBlocks(blocks, bookmarkId, text);
2798
+ return next === blocks ? null : next;
2799
+ });
2800
+ return replaced ? { changed: true, document: { ...replaced, updatedAt: timestamp } } : { changed: false, document };
2801
+ }
2802
+ function updateHyperlinkDestination(document, target, href, timestamp) {
2803
+ if (target.kind !== "hyperlink-destination" || target.commandFamily !== "link-bookmark") {
2804
+ return { changed: false, document };
2805
+ }
2806
+ const storyContext = collectStoryBlockContexts(document).find(
2807
+ (context) => context.storyKey === target.storyKey
2808
+ );
2809
+ if (!storyContext) return { changed: false, document };
2810
+ const relative = pathRelativeToStoryBase(target.blockPath, storyContext.basePath);
2811
+ if (!relative) return { changed: false, document };
2812
+ const nextBlocks = rewriteHyperlinkAtPath(storyContext.blocks, relative, href);
2813
+ if (!nextBlocks) return { changed: false, document };
2814
+ const replaced = replaceBlocksForEditableStory(document, target.storyKey, () => nextBlocks);
2815
+ return replaced ? { changed: true, document: { ...replaced, updatedAt: timestamp } } : { changed: false, document };
2816
+ }
2817
+ function rewriteBookmarkRangeInBlocks(blocks, bookmarkId, text) {
2818
+ let changed = false;
2819
+ const next = blocks.map((block) => {
2820
+ const rewritten = rewriteBookmarkRangeInBlock(block, bookmarkId, text);
2821
+ if (rewritten !== block) changed = true;
2822
+ return rewritten;
2823
+ });
2824
+ return changed ? next : blocks;
2825
+ }
2826
+ function rewriteBookmarkRangeInBlock(block, bookmarkId, text) {
2827
+ if (block.type === "paragraph") {
2828
+ const startIndex = block.children.findIndex(
2829
+ (child) => child.type === "bookmark_start" && child.bookmarkId === bookmarkId
2830
+ );
2831
+ const endIndex = block.children.findIndex(
2832
+ (child, index) => index > startIndex && child.type === "bookmark_end" && child.bookmarkId === bookmarkId
2833
+ );
2834
+ if (startIndex >= 0 && endIndex > startIndex) {
2835
+ const replacement = text.length > 0 ? [{ type: "text", text }] : [];
2836
+ return {
2837
+ ...block,
2838
+ children: [
2839
+ ...block.children.slice(0, startIndex + 1),
2840
+ ...replacement,
2841
+ ...block.children.slice(endIndex)
2842
+ ]
2843
+ };
2844
+ }
2845
+ return block;
2846
+ }
2847
+ if (block.type === "table") {
2848
+ let changed = false;
2849
+ const rows = block.rows.map((row2) => {
2850
+ let rowChanged = false;
2851
+ const cells = row2.cells.map((cell) => {
2852
+ const children = rewriteBookmarkRangeInBlocks(cell.children, bookmarkId, text);
2853
+ if (children !== cell.children) rowChanged = true;
2854
+ return children === cell.children ? cell : { ...cell, children: [...children] };
2855
+ });
2856
+ if (rowChanged) changed = true;
2857
+ return rowChanged ? { ...row2, cells } : row2;
2858
+ });
2859
+ return changed ? { ...block, rows } : block;
2860
+ }
2861
+ if (block.type === "sdt" || block.type === "custom_xml") {
2862
+ const children = rewriteBookmarkRangeInBlocks(block.children, bookmarkId, text);
2863
+ return children === block.children ? block : { ...block, children: [...children] };
2864
+ }
2865
+ return block;
2866
+ }
2867
+ function rewriteHyperlinkAtPath(blocks, relativePath, href) {
2868
+ const parts = relativePath.split("/");
2869
+ const rewritten = rewriteInlineAtPathInBlocks(blocks, parts, 0, (inline) => {
2870
+ if (inline.type !== "hyperlink") return inline;
2871
+ const hyperlink = inline;
2872
+ return hyperlink.href === href ? hyperlink : { ...hyperlink, href };
2873
+ });
2874
+ return rewritten === blocks ? null : [...rewritten];
2875
+ }
2876
+ function rewriteInlineAtPathInBlocks(blocks, parts, offset, update) {
2877
+ const blockIndex = parseIndexedPathPart(parts[offset], "block");
2878
+ if (blockIndex === null) return blocks;
2879
+ const block = blocks[blockIndex];
2880
+ if (!block) return blocks;
2881
+ const nextBlock = rewriteInlineAtPathInBlock(block, parts, offset + 1, update);
2882
+ if (nextBlock === block) return blocks;
2883
+ return blocks.map((candidate, index) => index === blockIndex ? nextBlock : candidate);
2884
+ }
2885
+ function rewriteInlineAtPathInBlock(block, parts, offset, update) {
2886
+ const inlineIndex = parseIndexedPathPart(parts[offset], "inline");
2887
+ if (inlineIndex !== null && block.type === "paragraph") {
2888
+ const inline = block.children[inlineIndex];
2889
+ if (!inline) return block;
2890
+ const nextInline = update(inline);
2891
+ if (nextInline === inline) return block;
2892
+ return {
2893
+ ...block,
2894
+ children: block.children.map((candidate, index) => index === inlineIndex ? nextInline : candidate)
2895
+ };
2896
+ }
2897
+ if (block.type === "sdt" || block.type === "custom_xml") {
2898
+ const children2 = rewriteInlineAtPathInBlocks(block.children, parts, offset, update);
2899
+ return children2 === block.children ? block : { ...block, children: [...children2] };
2900
+ }
2901
+ if (block.type !== "table") return block;
2902
+ const rowIndex = parseIndexedPathPart(parts[offset], "row");
2903
+ const cellIndex = parseIndexedPathPart(parts[offset + 1], "cell");
2904
+ if (rowIndex === null || cellIndex === null) return block;
2905
+ const row2 = block.rows[rowIndex];
2906
+ const cell = row2?.cells[cellIndex];
2907
+ if (!row2 || !cell) return block;
2908
+ const children = rewriteInlineAtPathInBlocks(cell.children, parts, offset + 2, update);
2909
+ if (children === cell.children) return block;
2910
+ return {
2911
+ ...block,
2912
+ rows: block.rows.map(
2913
+ (candidateRow, candidateRowIndex) => candidateRowIndex === rowIndex ? {
2914
+ ...candidateRow,
2915
+ cells: candidateRow.cells.map(
2916
+ (candidateCell, candidateCellIndex) => candidateCellIndex === cellIndex ? { ...candidateCell, children: [...children] } : candidateCell
2917
+ )
2918
+ } : candidateRow
2919
+ )
2920
+ };
2921
+ }
2922
+ function parseIndexedPathPart(part, kind) {
2923
+ if (!part) return null;
2924
+ const match = new RegExp(`^${kind}\\[(\\d+)\\]$`, "u").exec(part);
2925
+ if (!match) return null;
2926
+ const index = Number(match[1]);
2927
+ return Number.isSafeInteger(index) && index >= 0 ? index : null;
2928
+ }
2929
+ function pathRelativeToStoryBase(path, basePath) {
2930
+ if (path === basePath) return "";
2931
+ return path.startsWith(`${basePath}/`) ? path.slice(basePath.length + 1) : null;
2932
+ }
2933
+ function replaceBlocksForEditableStory(document, storyKey, replace) {
2934
+ if (storyKey === "main") {
2935
+ const children = replace(document.content.children);
2936
+ return children ? { ...document, content: { ...document.content, children: [...children] } } : null;
2937
+ }
2938
+ const headers = document.subParts?.headers ?? [];
2939
+ for (const header of headers) {
2940
+ if (createHeaderFooterStoryKey({
2941
+ kind: "header",
2942
+ relationshipId: header.relationshipId,
2943
+ variant: header.variant,
2944
+ ...header.sectionIndex !== void 0 ? { sectionIndex: header.sectionIndex } : {}
2945
+ }) === storyKey) {
2946
+ const blocks = replace(header.blocks);
2947
+ return blocks ? {
2948
+ ...document,
2949
+ subParts: {
2950
+ ...document.subParts,
2951
+ headers: headers.map((entry) => entry === header ? { ...entry, blocks: [...blocks] } : entry)
2952
+ }
2953
+ } : null;
2954
+ }
2955
+ }
2956
+ const footers = document.subParts?.footers ?? [];
2957
+ for (const footer of footers) {
2958
+ if (createHeaderFooterStoryKey({
2959
+ kind: "footer",
2960
+ relationshipId: footer.relationshipId,
2961
+ variant: footer.variant,
2962
+ ...footer.sectionIndex !== void 0 ? { sectionIndex: footer.sectionIndex } : {}
2963
+ }) === storyKey) {
2964
+ const blocks = replace(footer.blocks);
2965
+ return blocks ? {
2966
+ ...document,
2967
+ subParts: {
2968
+ ...document.subParts,
2969
+ footers: footers.map((entry) => entry === footer ? { ...entry, blocks: [...blocks] } : entry)
2970
+ }
2971
+ } : null;
2972
+ }
2973
+ }
2974
+ const footnoteMatch = /^footnote:(.+)$/u.exec(storyKey);
2975
+ const footnotes = document.subParts?.footnoteCollection?.footnotes;
2976
+ if (footnoteMatch && footnotes?.[footnoteMatch[1]]) {
2977
+ const note = footnotes[footnoteMatch[1]];
2978
+ const blocks = replace(note.blocks);
2979
+ return blocks ? {
2980
+ ...document,
2981
+ subParts: {
2982
+ ...document.subParts,
2983
+ footnoteCollection: {
2984
+ ...document.subParts.footnoteCollection,
2985
+ footnotes: {
2986
+ ...footnotes,
2987
+ [footnoteMatch[1]]: { ...note, blocks: [...blocks] }
2988
+ }
2989
+ }
2990
+ }
2991
+ } : null;
2992
+ }
2993
+ const endnoteMatch = /^endnote:(.+)$/u.exec(storyKey);
2994
+ const endnotes = document.subParts?.footnoteCollection?.endnotes;
2995
+ if (endnoteMatch && endnotes?.[endnoteMatch[1]]) {
2996
+ const note = endnotes[endnoteMatch[1]];
2997
+ const blocks = replace(note.blocks);
2998
+ return blocks ? {
2999
+ ...document,
3000
+ subParts: {
3001
+ ...document.subParts,
3002
+ footnoteCollection: {
3003
+ ...document.subParts.footnoteCollection,
3004
+ endnotes: {
3005
+ ...endnotes,
3006
+ [endnoteMatch[1]]: { ...note, blocks: [...blocks] }
3007
+ }
3008
+ }
3009
+ }
3010
+ } : null;
3011
+ }
3012
+ return null;
3013
+ }
2803
3014
  function buildDocumentReplaceTransaction(state, context, result) {
2804
3015
  if (!result.changed) {
2805
3016
  return createTransaction(state, { historyBoundary: "skip", markDirty: false });
@@ -9118,6 +9329,7 @@ function toPublicPageLocalStory(story) {
9118
9329
  variant: story.variant,
9119
9330
  relationshipId: story.relationshipId,
9120
9331
  ...story.sectionIndex !== void 0 ? { sectionIndex: story.sectionIndex } : {},
9332
+ previewText: story.previewText,
9121
9333
  resolvedFields: story.resolvedFields.map(toPublicResolvedStoryField),
9122
9334
  anchoredObjects: story.anchoredObjects.map(toPublicStoryAnchoredObject),
9123
9335
  measuredFrameHeightTwips: story.measuredFrameHeightTwips,
@@ -9251,6 +9463,7 @@ function toPublicTableFrameContexts(graph, tableRecords) {
9251
9463
  const columnContexts = buildTableFrameColumnContexts(tableBlock, tableFamily);
9252
9464
  const rowContexts = buildTableFrameRowContexts({
9253
9465
  tableFamily,
9466
+ tableBlock,
9254
9467
  rowRange,
9255
9468
  repeatedHeaderRowIndexes: repeatedHeaderRowIndexes2,
9256
9469
  splitRowCarry,
@@ -9269,6 +9482,8 @@ function toPublicTableFrameContexts(graph, tableRecords) {
9269
9482
  pageId: page.pageId,
9270
9483
  pageIndex: page.pageIndex,
9271
9484
  pageNumber: page.pageIndex + 1,
9485
+ pageStartOffset: page.startOffset,
9486
+ pageEndOffset: page.endOffset,
9272
9487
  fragmentId: fragment.fragmentId,
9273
9488
  rowRange,
9274
9489
  ...continuation !== void 0 ? {
@@ -9371,6 +9586,7 @@ function buildTableFrameRowContexts(input) {
9371
9586
  return {
9372
9587
  tableFamily: input.tableFamily,
9373
9588
  rowIndex,
9589
+ ...input.tableBlock?.rows[rowIndex]?.sourceRef !== void 0 ? { rowSourceRef: cloneSourceRef(input.tableBlock.rows[rowIndex].sourceRef) } : {},
9374
9590
  visualIndex,
9375
9591
  isRepeatedHeader: repeatedHeaderRows.has(rowIndex) && (rowIndex < input.rowRange.from || rowIndex >= input.rowRange.to),
9376
9592
  isInRowRange: rowIndex >= input.rowRange.from && rowIndex < input.rowRange.to,
@@ -9418,8 +9634,10 @@ function buildTableFrameCellContexts(input) {
9418
9634
  cells.push({
9419
9635
  tableFamily: rowContext.tableFamily,
9420
9636
  rowIndex: rowContext.rowIndex,
9637
+ ...row2.sourceRef !== void 0 ? { rowSourceRef: cloneSourceRef(row2.sourceRef) } : {},
9421
9638
  visualRowIndex: rowContext.visualIndex,
9422
9639
  columnIndex,
9640
+ ...cell.sourceRef !== void 0 ? { cellSourceRef: cloneSourceRef(cell.sourceRef) } : {},
9423
9641
  columnSpan,
9424
9642
  startTwips,
9425
9643
  widthTwips,
@@ -9548,6 +9766,12 @@ function cloneFragmentLayoutObject(layoutObject) {
9548
9766
  ...layoutObject.fieldRegions !== void 0 ? {
9549
9767
  fieldRegions: layoutObject.fieldRegions.map((region) => ({
9550
9768
  ...region,
9769
+ ...region.resultRange !== void 0 ? {
9770
+ resultRange: {
9771
+ fromParagraphIndex: region.resultRange.fromParagraphIndex,
9772
+ toParagraphIndex: region.resultRange.toParagraphIndex
9773
+ }
9774
+ } : {},
9551
9775
  ...region.sourceRef !== void 0 ? { sourceRef: { ...region.sourceRef } } : {},
9552
9776
  ...region.fieldEvidence !== void 0 ? {
9553
9777
  fieldEvidence: {
@@ -9557,6 +9781,20 @@ function cloneFragmentLayoutObject(layoutObject) {
9557
9781
  } : {}
9558
9782
  }))
9559
9783
  } : {},
9784
+ ...layoutObject.bookmarkRanges !== void 0 ? {
9785
+ bookmarkRanges: layoutObject.bookmarkRanges.map((range) => ({
9786
+ ...range,
9787
+ ...range.sourceRef !== void 0 ? { sourceRef: { ...range.sourceRef } } : {},
9788
+ ...range.startSourceRef !== void 0 ? { startSourceRef: { ...range.startSourceRef } } : {},
9789
+ ...range.endSourceRef !== void 0 ? { endSourceRef: { ...range.endSourceRef } } : {}
9790
+ }))
9791
+ } : {},
9792
+ ...layoutObject.hyperlinks !== void 0 ? {
9793
+ hyperlinks: layoutObject.hyperlinks.map((hyperlink) => ({
9794
+ ...hyperlink,
9795
+ ...hyperlink.sourceRef !== void 0 ? { sourceRef: { ...hyperlink.sourceRef } } : {}
9796
+ }))
9797
+ } : {},
9560
9798
  ...layoutObject.numbering !== void 0 ? { numbering: cloneNumberingLayoutFacts(layoutObject.numbering) } : {},
9561
9799
  ...layoutObject.numberingRows !== void 0 ? { numberingRows: layoutObject.numberingRows.map(cloneNumberingLayoutFacts) } : {}
9562
9800
  };
@@ -9564,6 +9802,10 @@ function cloneFragmentLayoutObject(layoutObject) {
9564
9802
  function cloneNumberingLayoutFacts(numbering) {
9565
9803
  return {
9566
9804
  ...numbering,
9805
+ ...numbering.sourceRef !== void 0 ? { sourceRef: { ...numbering.sourceRef } } : {},
9806
+ ...numbering.numberingSourceRef !== void 0 ? { numberingSourceRef: { ...numbering.numberingSourceRef } } : {},
9807
+ ...numbering.numberingInstanceSourceRef !== void 0 ? { numberingInstanceSourceRef: { ...numbering.numberingInstanceSourceRef } } : {},
9808
+ ...numbering.abstractNumberingSourceRef !== void 0 ? { abstractNumberingSourceRef: { ...numbering.abstractNumberingSourceRef } } : {},
9567
9809
  ...numbering.markerLane !== void 0 ? { markerLane: { ...numbering.markerLane } } : {},
9568
9810
  ...numbering.textColumn !== void 0 ? { textColumn: { ...numbering.textColumn } } : {},
9569
9811
  ...numbering.tabStops !== void 0 ? { tabStops: numbering.tabStops.map((tab) => ({ ...tab })) } : {}
@@ -9798,7 +10040,8 @@ function collectSurfaceTables(blocks, options = {}) {
9798
10040
  tablePath: blockPath,
9799
10041
  tableDepth: tableDepth + 1,
9800
10042
  insideSdt: options.insideSdt === true,
9801
- secondaryStory: options.secondaryStory === true
10043
+ secondaryStory: options.secondaryStory === true,
10044
+ ...block.sourceRef !== void 0 ? { sourceRef: block.sourceRef } : {}
9802
10045
  });
9803
10046
  tables.push({ table: block, tableFamily });
9804
10047
  for (const [rowIndex, row2] of block.rows.entries()) {
@@ -9837,6 +10080,7 @@ function tableFamilyIdentityForPath(input) {
9837
10080
  storyKey: input.storyKey,
9838
10081
  tableKey,
9839
10082
  tablePath: input.tablePath,
10083
+ ...input.sourceRef !== void 0 ? { sourceRef: cloneSourceRef(input.sourceRef) } : {},
9840
10084
  fragmentJoinKey: tableKey,
9841
10085
  tableDepth: input.tableDepth,
9842
10086
  ...indexes,
@@ -9845,6 +10089,9 @@ function tableFamilyIdentityForPath(input) {
9845
10089
  ...input.secondaryStory ? { secondaryStory: true } : {}
9846
10090
  };
9847
10091
  }
10092
+ function cloneSourceRef(sourceRef) {
10093
+ return { ...sourceRef };
10094
+ }
9848
10095
  function tablePathIndexes(tablePath) {
9849
10096
  const blockIndexes = [...tablePath.matchAll(/\/block\[(\d+)\]/gu)].map((match) => Number(match[1]));
9850
10097
  const storyBlockIndex = blockIndexes[0];
@@ -11843,7 +12090,6 @@ function createDocumentRuntime(options) {
11843
12090
  let viewportRangesKey = serializeViewportRanges(null);
11844
12091
  let viewportBlocksPerPageEstimate = null;
11845
12092
  const EDITING_CORRIDOR_BLOCK_RADIUS = 8;
11846
- const EDITING_CORRIDOR_MIN_BLOCKS = 24;
11847
12093
  const getRuntimeForLayoutFacet = () => {
11848
12094
  if (!runtimeRef) {
11849
12095
  throw new Error("Document runtime viewport methods are not initialized");
@@ -12035,6 +12281,24 @@ function createDocumentRuntime(options) {
12035
12281
  }
12036
12282
  let cachedSurface;
12037
12283
  let cachedFullSurface;
12284
+ const editableTargetBlockCache = createEditableTargetBlockCache();
12285
+ let cachedEditableTargetMap = null;
12286
+ function getEditableTargetsByBlockPath(document) {
12287
+ if (cachedEditableTargetMap !== null && cachedEditableTargetMap.document === document) {
12288
+ return cachedEditableTargetMap.map;
12289
+ }
12290
+ perfCounters.increment("runtime.editableTargets.builds");
12291
+ const targets = collectEditableTargetRefs(document, editableTargetBlockCache);
12292
+ const byBlockPath = /* @__PURE__ */ new Map();
12293
+ for (const target of targets) {
12294
+ byBlockPath.set(target.blockPath, target);
12295
+ }
12296
+ cachedEditableTargetMap = { document, map: byBlockPath };
12297
+ return byBlockPath;
12298
+ }
12299
+ function invalidateEditableTargetMap() {
12300
+ cachedEditableTargetMap = null;
12301
+ }
12038
12302
  let cachedCompatibility;
12039
12303
  let cachedComments;
12040
12304
  let cachedTrackedChanges;
@@ -12053,6 +12317,7 @@ function createDocumentRuntime(options) {
12053
12317
  }
12054
12318
  const snapshot = createEditorSurfaceSnapshot(document, state.selection, nextActiveStory, {
12055
12319
  viewportBlockRanges: null,
12320
+ editableTargetsByBlockPath: getEditableTargetsByBlockPath(document),
12056
12321
  ...effectiveMarkupModeProvider ? { getEffectiveMarkupMode: effectiveMarkupModeProvider } : {}
12057
12322
  });
12058
12323
  recordPerfSample("snapshot.surface");
@@ -12081,6 +12346,7 @@ function createDocumentRuntime(options) {
12081
12346
  }
12082
12347
  const snapshot = createEditorSurfaceSnapshot(document, state.selection, nextActiveStory, {
12083
12348
  viewportBlockRanges: surfaceViewportRanges,
12349
+ editableTargetsByBlockPath: getEditableTargetsByBlockPath(document),
12084
12350
  ...effectiveMarkupModeProvider ? { getEffectiveMarkupMode: effectiveMarkupModeProvider } : {}
12085
12351
  });
12086
12352
  recordPerfSample("snapshot.surface");
@@ -12117,7 +12383,7 @@ function createDocumentRuntime(options) {
12117
12383
  return enrichedSnapshot;
12118
12384
  }
12119
12385
  function getSelectionCorridorViewportRanges(previousSurface) {
12120
- if (!previousSurface || previousSurface.blocks.length < EDITING_CORRIDOR_MIN_BLOCKS) {
12386
+ if (!previousSurface) {
12121
12387
  return viewportBlockRanges;
12122
12388
  }
12123
12389
  const caretBlockIndex = findSurfaceBlockIndexForSelection(previousSurface, state.selection);
@@ -12847,6 +13113,7 @@ function createDocumentRuntime(options) {
12847
13113
  activeStory,
12848
13114
  {
12849
13115
  viewportBlockRanges,
13116
+ editableTargetsByBlockPath: getEditableTargetsByBlockPath(state.document),
12850
13117
  ...effectiveMarkupModeProvider ? { getEffectiveMarkupMode: effectiveMarkupModeProvider } : {}
12851
13118
  }
12852
13119
  );
@@ -12902,6 +13169,7 @@ function createDocumentRuntime(options) {
12902
13169
  function invalidateDerivedRuntimeCaches() {
12903
13170
  cachedSurface = void 0;
12904
13171
  cachedFullSurface = void 0;
13172
+ invalidateEditableTargetMap();
12905
13173
  cachedSurfaceFingerprint = null;
12906
13174
  cachedCompatibility = void 0;
12907
13175
  cachedComments = void 0;
@@ -13107,9 +13375,129 @@ function createDocumentRuntime(options) {
13107
13375
  return {
13108
13376
  kind: "accepted",
13109
13377
  command,
13110
- selection: createSelectionSnapshot(resolution.range.from, resolution.range.to)
13378
+ selection: createSelectionSnapshot(resolution.range.from, resolution.range.to),
13379
+ ...resolution.textTarget ? { textTarget: resolution.textTarget } : {}
13111
13380
  };
13112
13381
  }
13382
+ function prepareFragmentInsertCommandForExecution(command, document, surface, storyTarget) {
13383
+ if (!command.editableTarget) {
13384
+ return { kind: "accepted", selection: state.selection };
13385
+ }
13386
+ const resolution = resolveEditableTextTarget({
13387
+ document,
13388
+ surface,
13389
+ target: command.editableTarget,
13390
+ activeStoryKey: canonicalEditableTargetStoryKey(storyTarget)
13391
+ });
13392
+ if (resolution.kind === "rejected") {
13393
+ emit({
13394
+ type: "command_blocked",
13395
+ documentId: state.documentId,
13396
+ command: command.type,
13397
+ reasons: [{
13398
+ code: "unsupported_surface",
13399
+ message: resolution.blockedReason.message,
13400
+ detail: resolution.blockedReason.code
13401
+ }]
13402
+ });
13403
+ return resolution;
13404
+ }
13405
+ return {
13406
+ kind: "accepted",
13407
+ selection: createSelectionSnapshot(resolution.range.from, resolution.range.to),
13408
+ ...resolution.textTarget ? { textTarget: resolution.textTarget } : {}
13409
+ };
13410
+ }
13411
+ function prepareModeledTargetCommandForExecution(command, document, storyTarget) {
13412
+ const resolution = resolveEditableCommandTarget({
13413
+ document,
13414
+ target: command.editableTarget,
13415
+ activeStoryKey: canonicalEditableTargetStoryKey(storyTarget),
13416
+ commandFamilies: command.type === "field.refresh" || command.type === "toc.refresh" ? ["field"] : ["link-bookmark"],
13417
+ targetKinds: targetKindsForModeledCommand(command.type),
13418
+ allowGeneratedPosture: true
13419
+ });
13420
+ if (resolution.kind === "rejected") {
13421
+ emit({
13422
+ type: "command_blocked",
13423
+ documentId: state.documentId,
13424
+ command: command.type,
13425
+ reasons: [{
13426
+ code: "unsupported_surface",
13427
+ message: resolution.blockedReason.message,
13428
+ detail: resolution.blockedReason.code
13429
+ }]
13430
+ });
13431
+ return resolution;
13432
+ }
13433
+ return {
13434
+ kind: "accepted",
13435
+ command: {
13436
+ ...command,
13437
+ editableTarget: resolution.target
13438
+ }
13439
+ };
13440
+ }
13441
+ function targetKindsForModeledCommand(commandType) {
13442
+ switch (commandType) {
13443
+ case "field.refresh":
13444
+ return ["field-result-text", "field-region-refresh"];
13445
+ case "toc.refresh":
13446
+ return ["toc-region-refresh", "field-result-text"];
13447
+ case "bookmark.edit-content":
13448
+ return ["bookmark-content-range"];
13449
+ case "hyperlink.update-destination":
13450
+ return ["hyperlink-destination"];
13451
+ }
13452
+ }
13453
+ function executeRefreshCommand(command, baseState, context, storyTarget) {
13454
+ if (command.type === "field.refresh") {
13455
+ const refreshed2 = refreshDocumentFields(
13456
+ baseState.document,
13457
+ baseState.selection.head,
13458
+ storyTarget,
13459
+ getFullPageGraph(),
13460
+ command.options
13461
+ );
13462
+ if (!refreshed2.changed) return null;
13463
+ cachedFieldSnapshotEntry = null;
13464
+ return executeEditorCommand(
13465
+ baseState,
13466
+ {
13467
+ type: "document.replace",
13468
+ document: refreshed2.document,
13469
+ mapping: createEmptyMapping(),
13470
+ protectionSelection: refreshed2.protectionSelection,
13471
+ origin: command.origin
13472
+ },
13473
+ context
13474
+ );
13475
+ }
13476
+ const tocId = command.options?.tocId ?? command.editableTarget.field?.tocId;
13477
+ const refreshed = refreshDocumentTableOfContents(
13478
+ baseState.document,
13479
+ baseState.selection.head,
13480
+ storyTarget,
13481
+ {
13482
+ ...command.options,
13483
+ ...tocId !== void 0 ? { tocId } : {}
13484
+ },
13485
+ getFullDisplayPageNumber,
13486
+ getCachedDocumentNavigationSnapshot(baseState, storyTarget)
13487
+ );
13488
+ if (!refreshed.changed) return null;
13489
+ return executeEditorCommand(
13490
+ baseState,
13491
+ {
13492
+ type: "document.replace",
13493
+ document: refreshed.document,
13494
+ mapping: createEmptyMapping(),
13495
+ protectionSelection: refreshed.protectionSelection,
13496
+ origin: command.origin
13497
+ },
13498
+ context
13499
+ );
13500
+ }
13113
13501
  const runtime = {
13114
13502
  subscribe(listener) {
13115
13503
  listeners.add(listener);
@@ -13175,7 +13563,33 @@ function createDocumentRuntime(options) {
13175
13563
  });
13176
13564
  return;
13177
13565
  }
13178
- const commandSelection = getCommandSelection(command, state.selection);
13566
+ let executableCommand = command;
13567
+ let commandSelection = getCommandSelection(command, state.selection);
13568
+ let resolvedFragmentTextTarget;
13569
+ if (command.type === "fragment.insert" && command.editableTarget) {
13570
+ const prepared = prepareFragmentInsertCommandForExecution(
13571
+ command,
13572
+ state.document,
13573
+ cachedRenderSnapshot.surface?.blocks ?? [],
13574
+ activeStory
13575
+ );
13576
+ if (prepared.kind === "rejected") {
13577
+ return;
13578
+ }
13579
+ commandSelection = prepared.selection;
13580
+ resolvedFragmentTextTarget = prepared.textTarget;
13581
+ }
13582
+ if (command.type === "field.refresh" || command.type === "toc.refresh" || command.type === "bookmark.edit-content" || command.type === "hyperlink.update-destination") {
13583
+ const prepared = prepareModeledTargetCommandForExecution(
13584
+ command,
13585
+ state.document,
13586
+ activeStory
13587
+ );
13588
+ if (prepared.kind === "rejected") {
13589
+ return;
13590
+ }
13591
+ executableCommand = prepared.command;
13592
+ }
13179
13593
  if (isMutationCommand(command)) {
13180
13594
  const blockedReasons = workflowCoordinator.evaluateBlockedReasons(
13181
13595
  commandSelection,
@@ -13223,7 +13637,6 @@ function createDocumentRuntime(options) {
13223
13637
  return;
13224
13638
  }
13225
13639
  try {
13226
- let executableCommand = command;
13227
13640
  if (command.type === "table.apply-structure") {
13228
13641
  const prepared = prepareTableStructureCommandForExecution(
13229
13642
  command,
@@ -13239,12 +13652,17 @@ function createDocumentRuntime(options) {
13239
13652
  timestamp: normalizeCommandTimestamp(executableCommand.origin?.timestamp) ?? clock(),
13240
13653
  documentMode: workflowCoordinator.getEffectiveDocumentMode(commandSelection),
13241
13654
  defaultAuthorId: defaultAuthorId ?? void 0,
13242
- renderSnapshot: cachedRenderSnapshot
13655
+ renderSnapshot: cachedRenderSnapshot,
13656
+ ...resolvedFragmentTextTarget ? { textTarget: resolvedFragmentTextTarget } : {}
13243
13657
  };
13244
13658
  const preSelection = commandSelection;
13245
13659
  const preActiveStory = activeStory;
13246
13660
  const priorDocument = state.document;
13247
- const transaction = executeEditorCommand(state, executableCommand, context);
13661
+ const baseState = commandSelection === state.selection ? state : { ...state, selection: commandSelection };
13662
+ const transaction = executableCommand.type === "field.refresh" || executableCommand.type === "toc.refresh" ? executeRefreshCommand(executableCommand, baseState, context, activeStory) : executeEditorCommand(baseState, executableCommand, context);
13663
+ if (!transaction) {
13664
+ return;
13665
+ }
13248
13666
  commit(transaction);
13249
13667
  options.onCommandApplied?.(executableCommand, transaction, context, {
13250
13668
  preSelection,
@@ -13282,6 +13700,7 @@ function createDocumentRuntime(options) {
13282
13700
  replaySnapshot = cachedRenderSnapshot;
13283
13701
  }
13284
13702
  let executableCommand = command;
13703
+ let resolvedReplayTextTarget;
13285
13704
  if (command.type === "table.apply-structure") {
13286
13705
  const prepared = prepareTableStructureCommandForExecution(
13287
13706
  command,
@@ -13307,12 +13726,42 @@ function createDocumentRuntime(options) {
13307
13726
  selection: prepared.selection
13308
13727
  };
13309
13728
  executableCommand = prepared.command;
13729
+ resolvedReplayTextTarget = prepared.textTarget;
13730
+ } else if (command.type === "fragment.insert" && command.editableTarget) {
13731
+ const prepared = prepareFragmentInsertCommandForExecution(
13732
+ command,
13733
+ replayState.document,
13734
+ replaySnapshot.surface?.blocks ?? [],
13735
+ replayStory
13736
+ );
13737
+ if (prepared.kind === "rejected") {
13738
+ return;
13739
+ }
13740
+ replayState = {
13741
+ ...replayState,
13742
+ selection: prepared.selection
13743
+ };
13744
+ resolvedReplayTextTarget = prepared.textTarget;
13745
+ } else if (command.type === "field.refresh" || command.type === "toc.refresh" || command.type === "bookmark.edit-content" || command.type === "hyperlink.update-destination") {
13746
+ const prepared = prepareModeledTargetCommandForExecution(
13747
+ command,
13748
+ replayState.document,
13749
+ replayStory
13750
+ );
13751
+ if (prepared.kind === "rejected") {
13752
+ return;
13753
+ }
13754
+ executableCommand = prepared.command;
13310
13755
  }
13311
13756
  const replayContext = {
13312
13757
  ...context,
13313
- renderSnapshot: replaySnapshot
13758
+ renderSnapshot: replaySnapshot,
13759
+ ...resolvedReplayTextTarget ? { textTarget: resolvedReplayTextTarget } : {}
13314
13760
  };
13315
- const transaction = executeEditorCommand(replayState, executableCommand, replayContext);
13761
+ const transaction = executableCommand.type === "field.refresh" || executableCommand.type === "toc.refresh" ? executeRefreshCommand(executableCommand, replayState, replayContext, replayStory) : executeEditorCommand(replayState, executableCommand, replayContext);
13762
+ if (!transaction) {
13763
+ return;
13764
+ }
13316
13765
  if (crossStoryReplay) {
13317
13766
  commitRemote({
13318
13767
  ...transaction,
@@ -13394,6 +13843,7 @@ function createDocumentRuntime(options) {
13394
13843
  snapshotForCommand = cachedRenderSnapshot;
13395
13844
  }
13396
13845
  let executableCommand = command;
13846
+ let resolvedReplayTextTarget;
13397
13847
  if (command.type === "table.apply-structure") {
13398
13848
  const prepared = prepareTableStructureCommandForExecution(
13399
13849
  command,
@@ -13419,12 +13869,42 @@ function createDocumentRuntime(options) {
13419
13869
  selection: prepared.selection
13420
13870
  };
13421
13871
  executableCommand = prepared.command;
13872
+ resolvedReplayTextTarget = prepared.textTarget;
13873
+ } else if (command.type === "fragment.insert" && command.editableTarget) {
13874
+ const prepared = prepareFragmentInsertCommandForExecution(
13875
+ command,
13876
+ stateForCommand.document,
13877
+ snapshotForCommand.surface?.blocks ?? [],
13878
+ replayStory
13879
+ );
13880
+ if (prepared.kind === "rejected") {
13881
+ continue;
13882
+ }
13883
+ stateForCommand = {
13884
+ ...stateForCommand,
13885
+ selection: prepared.selection
13886
+ };
13887
+ resolvedReplayTextTarget = prepared.textTarget;
13888
+ } else if (command.type === "field.refresh" || command.type === "toc.refresh" || command.type === "bookmark.edit-content" || command.type === "hyperlink.update-destination") {
13889
+ const prepared = prepareModeledTargetCommandForExecution(
13890
+ command,
13891
+ stateForCommand.document,
13892
+ replayStory
13893
+ );
13894
+ if (prepared.kind === "rejected") {
13895
+ continue;
13896
+ }
13897
+ executableCommand = prepared.command;
13422
13898
  }
13423
13899
  const replayContext = {
13424
13900
  ...context,
13425
- renderSnapshot: snapshotForCommand
13901
+ renderSnapshot: snapshotForCommand,
13902
+ ...resolvedReplayTextTarget ? { textTarget: resolvedReplayTextTarget } : {}
13426
13903
  };
13427
- const transaction = executeEditorCommand(stateForCommand, executableCommand, replayContext);
13904
+ const transaction = executableCommand.type === "field.refresh" || executableCommand.type === "toc.refresh" ? executeRefreshCommand(executableCommand, stateForCommand, replayContext, replayStory) : executeEditorCommand(stateForCommand, executableCommand, replayContext);
13905
+ if (!transaction) {
13906
+ continue;
13907
+ }
13428
13908
  replayState = transaction.nextState;
13429
13909
  stepsAccumulator.push(...transaction.mapping.steps);
13430
13910
  anyDirty = anyDirty || transaction.markDirty;
@@ -13837,13 +14317,13 @@ function createDocumentRuntime(options) {
13837
14317
  }
13838
14318
  return changed;
13839
14319
  },
13840
- insertFragment(fragment, target) {
14320
+ insertFragment(fragment, target, editContext) {
13841
14321
  actionDepth += 1;
13842
14322
  actionStack.push("insertFragment");
13843
14323
  try {
13844
14324
  const timestamp = clock();
13845
14325
  const selection = target ? createSelectionFromPublicAnchor(target) : state.selection;
13846
- const editableTarget = inferEditableTargetForFragmentInsert(
14326
+ const editableTarget = editContext?.editableTarget && isTableParagraphEditableTarget(editContext.editableTarget) ? editContext.editableTarget : inferEditableTargetForFragmentInsert(
13847
14327
  cachedRenderSnapshot.surface?.blocks ?? [],
13848
14328
  selection
13849
14329
  );
@@ -16463,7 +16943,7 @@ function findEditableTargetInSurfaceCell(cell, position) {
16463
16943
  return findEditableTargetAtSurfacePosition(cell.content, position);
16464
16944
  }
16465
16945
  function isTableParagraphEditableTarget(target) {
16466
- return target.commandFamily === "text-leaf" && (target.kind === "table-cell-paragraph-text" || target.kind === "nested-table-cell-paragraph-text" || target.kind === "sdt-table-cell-paragraph-text");
16946
+ return target.editability === "editable" && target.posture.blockers.length === 0 && target.commandFamily === "text-leaf" && (target.kind === "table-cell-paragraph-text" || target.kind === "nested-table-cell-paragraph-text" || target.kind === "sdt-table-cell-paragraph-text");
16467
16947
  }
16468
16948
  function extractSelectionFragment(document, selection, activeStory) {
16469
16949
  const from = Math.min(selection.anchor, selection.head);