@stll/folio-core 0.68.0 → 0.69.0

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 (177) hide show
  1. package/dist/ai-edits/headless.js +2 -2
  2. package/dist/ai-edits/newListNumbering.d.ts +14 -1
  3. package/dist/ai-edits/newListNumbering.js +24 -1
  4. package/dist/ai-edits/pending-suggestions.js +2 -2
  5. package/dist/controller/canonicalClipboard.js +2 -2
  6. package/dist/controller/canonicalComposition.js +34 -5
  7. package/dist/controller/canonicalInput.js +4 -6
  8. package/dist/controller/canonicalLockGuard.d.ts +17 -2
  9. package/dist/controller/canonicalLockGuard.js +46 -1
  10. package/dist/controller/canonicalProjectionBlocks.d.ts +79 -0
  11. package/dist/controller/canonicalProjectionBlocks.js +75 -0
  12. package/dist/controller/canonicalPublicOperations.js +476 -100
  13. package/dist/controller/canonicalSeedContent.d.ts +12 -0
  14. package/dist/controller/canonicalSeedContent.js +131 -0
  15. package/dist/controller/canonicalSession.d.ts +53 -21
  16. package/dist/controller/canonicalSession.js +489 -210
  17. package/dist/controller/canonicalSourceNormalization.d.ts +21 -0
  18. package/dist/controller/canonicalSourceNormalization.js +148 -0
  19. package/dist/controller/canonicalStoryEditor.d.ts +1 -1
  20. package/dist/controller/canonicalStoryEditor.js +16 -12
  21. package/dist/controller/folioEditor.js +3 -0
  22. package/dist/controller/headerFooterEditorManager.d.ts +5 -0
  23. package/dist/controller/headerFooterEditorManager.js +7 -2
  24. package/dist/controller/hiddenEditorApi.d.ts +11 -2
  25. package/dist/controller/hiddenEditorApi.js +3 -0
  26. package/dist/controller/hiddenEditorManager.d.ts +7 -0
  27. package/dist/controller/hiddenEditorManager.js +66 -11
  28. package/dist/controller/layoutPipeline.js +2 -1
  29. package/dist/controller/layoutSession.d.ts +3 -0
  30. package/dist/controller/layoutSession.js +3 -1
  31. package/dist/controller/noteEditorManager.d.ts +5 -0
  32. package/dist/controller/noteEditorManager.js +7 -2
  33. package/dist/display-list/dom/renderDisplayListToDom.js +9 -7
  34. package/dist/document-operations.d.ts +1 -1
  35. package/dist/docx/canonicalSave.js +7 -2
  36. package/dist/docx/commentAnchorIndex.d.ts +19 -0
  37. package/dist/docx/commentAnchorIndex.js +55 -1
  38. package/dist/docx/containerChildren.js +1 -1
  39. package/dist/docx/contentTypeRegistry.js +1 -1
  40. package/dist/docx/hostCommentThreads.d.ts +11 -1
  41. package/dist/docx/hostCommentThreads.js +25 -2
  42. package/dist/docx/listModelNormalization.js +4 -5
  43. package/dist/docx/listNumberingInstances.d.ts +1 -1
  44. package/dist/docx/listNumberingInstances.js +2 -2
  45. package/dist/docx/numberingIds.d.ts +6 -0
  46. package/dist/docx/numberingIds.js +6 -1
  47. package/dist/docx/numberingReferenceNormalization.d.ts +4 -0
  48. package/dist/docx/numberingReferenceNormalization.js +12 -2
  49. package/dist/docx/paragraphPropertySource.d.ts +22 -0
  50. package/dist/docx/paragraphPropertySource.js +85 -16
  51. package/dist/docx/parser.js +2 -0
  52. package/dist/docx/removeHeaderFooterParts.js +1 -1
  53. package/dist/docx/reviewResolutionProvenance.js +72 -19
  54. package/dist/docx/serializer/documentSerializer.js +2 -1
  55. package/dist/docx/serializer/partNamespaces.d.ts +1 -1
  56. package/dist/docx/serializer/partNamespaces.js +1 -1
  57. package/dist/docx/server/build.d.ts +1 -1
  58. package/dist/docx/storyBlockReplay.js +15 -2
  59. package/dist/docx/structuralXmlPatch.js +25 -4
  60. package/dist/docx/xmlParser.d.ts +1 -1
  61. package/dist/docx/xmlParser.js +2 -3
  62. package/dist/i18n/messages/ar.gen.js +1 -0
  63. package/dist/i18n/messages/catalogs.gen.d.ts +17 -0
  64. package/dist/i18n/messages/catalogs.gen.js +17 -0
  65. package/dist/i18n/messages/cs.gen.js +1 -0
  66. package/dist/i18n/messages/de.gen.js +1 -0
  67. package/dist/i18n/messages/en.gen.js +1 -0
  68. package/dist/i18n/messages/es.gen.js +1 -0
  69. package/dist/i18n/messages/et.gen.js +1 -0
  70. package/dist/i18n/messages/fr.gen.js +1 -0
  71. package/dist/i18n/messages/he.gen.js +1 -0
  72. package/dist/i18n/messages/hi.gen.js +1 -0
  73. package/dist/i18n/messages/hu.gen.js +1 -0
  74. package/dist/i18n/messages/lt.gen.js +1 -0
  75. package/dist/i18n/messages/lv.gen.js +1 -0
  76. package/dist/i18n/messages/messages.gen.d.ts +1 -0
  77. package/dist/i18n/messages/pl.gen.js +1 -0
  78. package/dist/i18n/messages/pt-BR.gen.js +1 -0
  79. package/dist/i18n/messages/sk.gen.js +1 -0
  80. package/dist/i18n/messages/tr.gen.js +1 -0
  81. package/dist/i18n/messages/zh-CN.gen.js +1 -0
  82. package/dist/internal/pageBreakRunDescendantIndex.d.ts +7 -2
  83. package/dist/internal/pageBreakRunDescendantIndex.js +12 -3
  84. package/dist/internal/subtreeHolds.d.ts +12 -0
  85. package/dist/internal/subtreeHolds.js +25 -0
  86. package/dist/internal/wholeStoryRevisionResolution.js +1 -1
  87. package/dist/layout-bridge/cellSelectionHighlight.js +3 -3
  88. package/dist/layout-bridge/convert/flowBlockPostprocessing.js +20 -12
  89. package/dist/layout-bridge/convert/flowConversionShared.d.ts +15 -1
  90. package/dist/layout-bridge/convert/flowConversionShared.js +9 -1
  91. package/dist/layout-bridge/convert/paragraphAttrs.d.ts +15 -4
  92. package/dist/layout-bridge/convert/paragraphAttrs.js +11 -4
  93. package/dist/layout-bridge/convert/paragraphBlockCache.d.ts +26 -0
  94. package/dist/layout-bridge/convert/paragraphBlockCache.js +137 -0
  95. package/dist/layout-bridge/convert/paragraphConversion.d.ts +12 -2
  96. package/dist/layout-bridge/convert/paragraphConversion.js +37 -5
  97. package/dist/layout-bridge/convert/paragraphRuns.d.ts +2 -2
  98. package/dist/layout-bridge/convert/sectionBoundaries.js +16 -0
  99. package/dist/layout-bridge/convert/toFlowBlocks.js +144 -15
  100. package/dist/layout-bridge/convert/topLevelConversion.d.ts +74 -0
  101. package/dist/layout-bridge/convert/topLevelConversion.js +109 -0
  102. package/dist/layout-bridge/dom/clickToPositionDom.js +41 -40
  103. package/dist/layout-bridge/dom/findBodyPmSpans.js +4 -3
  104. package/dist/layout-bridge/dom/findHfPmSpans.js +10 -15
  105. package/dist/layout-bridge/dom/imeCaretAnchor.d.ts +8 -0
  106. package/dist/layout-bridge/dom/imeCaretAnchor.js +71 -7
  107. package/dist/layout-bridge/dom/textStreamDom.js +3 -2
  108. package/dist/layout-bridge/headerFooterLayout.js +12 -11
  109. package/dist/layout-bridge/tableInsertHover.js +2 -1
  110. package/dist/layout-engine/measure/cache.d.ts +2 -0
  111. package/dist/layout-engine/measure/cache.js +6 -1
  112. package/dist/layout-engine/measure/paragraphKeyMemo.d.ts +8 -0
  113. package/dist/layout-engine/measure/paragraphKeyMemo.js +92 -0
  114. package/dist/layout-engine/types.d.ts +6 -0
  115. package/dist/layout-painter/imageLayout.js +2 -1
  116. package/dist/layout-painter/pageShellStyles.d.ts +10 -0
  117. package/dist/layout-painter/pageShellStyles.js +32 -0
  118. package/dist/layout-painter/reconcileRenderedContent.d.ts +19 -0
  119. package/dist/layout-painter/reconcileRenderedContent.js +117 -0
  120. package/dist/layout-painter/renderFragment.js +4 -6
  121. package/dist/layout-painter/renderImage.js +2 -2
  122. package/dist/layout-painter/renderPage.js +8 -29
  123. package/dist/layout-painter/renderParagraph.js +3 -3
  124. package/dist/layout-painter/renderTable.js +9 -12
  125. package/dist/layout-painter/renderTextBox.js +2 -2
  126. package/dist/layout-painter/sdtBoundary.js +3 -2
  127. package/dist/paged-layout/blockGeometry.js +3 -2
  128. package/dist/paged-layout/editorScrollRoot.d.ts +9 -0
  129. package/dist/paged-layout/editorScrollRoot.js +10 -1
  130. package/dist/paged-layout/rangeProjection.js +5 -4
  131. package/dist/paged-layout/scrollToPmPosition.js +3 -3
  132. package/dist/paged-layout/selectionViewportRect.d.ts +6 -0
  133. package/dist/paged-layout/selectionViewportRect.js +8 -2
  134. package/dist/prosemirror/attrs/index.js +1 -0
  135. package/dist/prosemirror/commands/comments.d.ts +5 -2
  136. package/dist/prosemirror/commands/comments.js +88 -8
  137. package/dist/prosemirror/commentRecords.d.ts +137 -0
  138. package/dist/prosemirror/commentRecords.js +400 -0
  139. package/dist/prosemirror/conversion/fromProseDoc.js +9 -3
  140. package/dist/prosemirror/conversion/index.d.ts +1 -1
  141. package/dist/prosemirror/conversion/toProseDoc.d.ts +13 -4
  142. package/dist/prosemirror/conversion/toProseDoc.js +126 -38
  143. package/dist/prosemirror/extensions/features/ParagraphChangeTrackerExtension.d.ts +22 -0
  144. package/dist/prosemirror/extensions/features/ParagraphChangeTrackerExtension.js +61 -32
  145. package/dist/prosemirror/extensions/marks/CommentExtension.js +3 -1
  146. package/dist/prosemirror/extensions/nodes/CommentReferenceExtension.js +3 -1
  147. package/dist/prosemirror/index.d.ts +1 -1
  148. package/dist/prosemirror/nativeSelection.d.ts +33 -0
  149. package/dist/prosemirror/nativeSelection.js +74 -0
  150. package/dist/prosemirror/numberedRefFields.d.ts +2 -0
  151. package/dist/prosemirror/numberedRefFields.js +16 -5
  152. package/dist/prosemirror/paragraphMarkJoin.d.ts +6 -8
  153. package/dist/prosemirror/paragraphMarkJoin.js +155 -16
  154. package/dist/prosemirror/plugins/contentControlWidgets.js +2 -2
  155. package/dist/prosemirror/plugins/documentNumbering.js +33 -14
  156. package/dist/prosemirror/plugins/suggestionMode.js +13 -1
  157. package/dist/prosemirror/runIdentityAcrossRevisions.d.ts +23 -1
  158. package/dist/prosemirror/runIdentityAcrossRevisions.js +57 -8
  159. package/dist/prosemirror/schema/marks.d.ts +7 -1
  160. package/dist/prosemirror/storyListNumbering.js +7 -2
  161. package/dist/prosemirror/tableResize.d.ts +10 -9
  162. package/dist/prosemirror/tableResize.js +60 -49
  163. package/dist/prosemirror/textInput.d.ts +30 -1
  164. package/dist/prosemirror/textInput.js +144 -17
  165. package/dist/prosemirror/utils/visualLineNavigation.js +20 -18
  166. package/dist/prosemirror/validation.d.ts +19 -0
  167. package/dist/prosemirror/validation.js +78 -5
  168. package/dist/render-dom/HeaderFooterSelectionOverlay.js +5 -4
  169. package/dist/render-dom/RenderedDomContext.js +7 -6
  170. package/dist/types/canonicalCapabilities.d.ts +12 -11
  171. package/dist/types/canonicalCapabilities.js +12 -13
  172. package/dist/utils/findMatchView.js +4 -3
  173. package/dist/utils/formatToStyle.d.ts +1 -1
  174. package/dist/utils/mergeDocumentContent.js +2 -2
  175. package/dist/utils/paintedPositions.d.ts +60 -0
  176. package/dist/utils/paintedPositions.js +101 -0
  177. package/package.json +2 -3
@@ -7,6 +7,15 @@ const getEditorScrollRoot = (element) => element?.closest(EDITOR_SCROLL_ROOT_SEL
7
7
  const scrollEditorTo = (element, options) => {
8
8
  getEditorScrollRoot(element)?.scrollTo(options);
9
9
  };
10
+ /**
11
+ * Scroll a box that only clips (such as the hidden editor's wrapper) back to
12
+ * its origin. It is not the editor's scroll root; the browser scrolls it to
13
+ * reveal a caret, and nothing it shows may move with that scroll.
14
+ */
15
+ const scrollClipBoxToOrigin = (box) => {
16
+ if (box.scrollLeft !== 0) box.scrollLeft = 0;
17
+ if (box.scrollTop !== 0) box.scrollTop = 0;
18
+ };
10
19
  const scrollEditorBy = (element, delta) => {
11
20
  const root = getEditorScrollRoot(element);
12
21
  if (root) scrollEditorTo(root, {
@@ -76,4 +85,4 @@ const scrollEditorElementIntoView = (element, { block = "center", behavior, marg
76
85
  behavior: behavior ?? navigationScrollBehavior(clampedTop - root.scrollTop, root.clientHeight)
77
86
  });
78
87
  };
79
- export { getEditorScrollRoot, scrollEditorBy, scrollEditorElementIntoView, scrollEditorTo };
88
+ export { getEditorScrollRoot, scrollClipBoxToOrigin, scrollEditorBy, scrollEditorElementIntoView, scrollEditorTo };
@@ -1,5 +1,6 @@
1
1
  import { findBodyPmSpans } from "../layout-bridge/dom/findBodyPmSpans.js";
2
2
  import { createTextStreamRange, descendantTextNodes, totalTextLength } from "../layout-bridge/dom/textStreamDom.js";
3
+ import { readPaintedEnd, readPaintedStart } from "../utils/paintedPositions.js";
3
4
  // src/paged-layout/rangeProjection.ts
4
5
  /**
5
6
  * Shared range → paged-canvas rectangle projection.
@@ -22,8 +23,8 @@ const pageIndexOf = (el) => {
22
23
  const domRectsForRange = (pmSpans, overlayRect, zoom, from, to) => {
23
24
  const rects = [];
24
25
  for (const spanEl of pmSpans) {
25
- const pmStart = Number(spanEl.dataset["pmStart"]);
26
- if (!(Number(spanEl.dataset["pmEnd"]) > from && pmStart < to)) continue;
26
+ const pmStart = readPaintedStart(spanEl) ?? NaN;
27
+ if (!((readPaintedEnd(spanEl) ?? NaN) > from && pmStart < to)) continue;
27
28
  if (spanEl.classList.contains("layout-run-tab")) {
28
29
  const spanRect = spanEl.getBoundingClientRect();
29
30
  rects.push({
@@ -119,8 +120,8 @@ const collectRunFontsAtPmPositions = (pagesContainer, positions) => {
119
120
  const remaining = new Set(positions);
120
121
  for (const spanEl of findBodyPmSpans(pagesContainer)) {
121
122
  if (remaining.size === 0) break;
122
- const pmStart = Number(spanEl.dataset["pmStart"]);
123
- const pmEnd = Number(spanEl.dataset["pmEnd"]);
123
+ const pmStart = readPaintedStart(spanEl) ?? NaN;
124
+ const pmEnd = readPaintedEnd(spanEl) ?? NaN;
124
125
  if (Number.isNaN(pmStart) || Number.isNaN(pmEnd)) continue;
125
126
  for (const pos of remaining) {
126
127
  if (!(pmStart <= pos && pos < pmEnd)) continue;
@@ -1,5 +1,6 @@
1
1
  import { findBodyPmAnchor, findBodyPmAnchors } from "../layout-bridge/dom/findBodyPmSpans.js";
2
2
  import { findPageShellForPmPos, renderPagesAround } from "../layout-painter/renderPage.js";
3
+ import { readPaintedEnd, readPaintedStart } from "../utils/paintedPositions.js";
3
4
  import { getEditorScrollRoot, scrollEditorElementIntoView } from "./editorScrollRoot.js";
4
5
  import { isValidPmScrollPosition } from "./scrollNavigation.js";
5
6
  // src/paged-layout/scrollToPmPosition.ts
@@ -22,13 +23,12 @@ const scrollPagesToPmPosition = (pageContainer, pmPos) => {
22
23
  const anchorRanges = (root) => {
23
24
  const ranges = [];
24
25
  for (const element of findBodyPmAnchors(root)) {
25
- const start = Number(element.dataset["pmStart"]);
26
+ const start = readPaintedStart(element) ?? NaN;
26
27
  if (Number.isNaN(start)) continue;
27
- const endAttr = element.dataset["pmEnd"];
28
28
  ranges.push({
29
29
  element,
30
30
  start,
31
- end: endAttr === void 0 ? start : Number(endAttr)
31
+ end: readPaintedEnd(element) ?? start
32
32
  });
33
33
  }
34
34
  return ranges;
@@ -3,6 +3,12 @@ import { EditorView } from "prosemirror-view";
3
3
  export declare const FOLIO_SELECTION_RECT_ATTRIBUTE = "data-folio-selection-rect";
4
4
  /** Attribute SelectionOverlay stamps on the painted collapsed caret. */
5
5
  export declare const FOLIO_CARET_RECT_ATTRIBUTE = "data-folio-caret-rect";
6
+ /**
7
+ * Attribute the paged editor sets on its selection overlay while typed
8
+ * content lays out again: the caret the overlay still holds stands where the
9
+ * text was, so it is hidden, and it does not anchor anything.
10
+ */
11
+ export declare const FOLIO_INPUT_HIDDEN_ATTRIBUTE = "data-folio-input-hidden";
6
12
  /**
7
13
  * Union bounding rect (client coordinates) of the painted selection
8
14
  * highlights mirroring `view`'s selection. Returns null for a collapsed
@@ -4,6 +4,12 @@ const FOLIO_SELECTION_RECT_ATTRIBUTE = "data-folio-selection-rect";
4
4
  /** Attribute SelectionOverlay stamps on the painted collapsed caret. */
5
5
  const FOLIO_CARET_RECT_ATTRIBUTE = "data-folio-caret-rect";
6
6
  /**
7
+ * Attribute the paged editor sets on its selection overlay while typed
8
+ * content lays out again: the caret the overlay still holds stands where the
9
+ * text was, so it is hidden, and it does not anchor anything.
10
+ */
11
+ const FOLIO_INPUT_HIDDEN_ATTRIBUTE = "data-folio-input-hidden";
12
+ /**
7
13
  * Union bounding rect (client coordinates) of the painted selection
8
14
  * highlights mirroring `view`'s selection. Returns null for a collapsed
9
15
  * selection or while the overlay has not painted yet — the selection
@@ -46,9 +52,9 @@ const getFolioCaretViewportRect = (view) => {
46
52
  const scrollContainer = view.dom.closest("[data-folio-scroll]");
47
53
  if (!scrollContainer) return null;
48
54
  const caret = scrollContainer.querySelector(`[${FOLIO_CARET_RECT_ATTRIBUTE}]`);
49
- if (!caret) return null;
55
+ if (!caret || caret.closest(`[data-folio-input-hidden]`)) return null;
50
56
  const rect = caret.getBoundingClientRect();
51
57
  if (rect.height <= 0) return null;
52
58
  return rect;
53
59
  };
54
- export { FOLIO_CARET_RECT_ATTRIBUTE, FOLIO_SELECTION_RECT_ATTRIBUTE, getFolioCaretViewportRect, getFolioSelectionViewportRect };
60
+ export { FOLIO_CARET_RECT_ATTRIBUTE, FOLIO_INPUT_HIDDEN_ATTRIBUTE, FOLIO_SELECTION_RECT_ATTRIBUTE, getFolioCaretViewportRect, getFolioSelectionViewportRect };
@@ -979,6 +979,7 @@ const readCommentMarkAttrs = (mark) => {
979
979
  expectMarkType(mark, "comment", issues);
980
980
  requiredNumber(attrs, "commentId", "comment.attrs.commentId", issues);
981
981
  optionalBoolean(attrs, "point", "comment.attrs.point", issues);
982
+ optionalString(attrs, "suggestionId", "comment.attrs.suggestionId", issues);
982
983
  return attrsResult(attrs, issues);
983
984
  };
984
985
  const expectCommentMarkAttrs = (mark) => expectCachedMarkAttrs(mark, commentAttrsCache, readCommentMarkAttrs, "comment attrs");
@@ -99,13 +99,16 @@ export declare function acceptAIEditRevision(revisionIds: number | readonly numb
99
99
  * See {@link acceptAIEditRevision} for the id semantics.
100
100
  */
101
101
  export declare function rejectAIEditRevision(revisionIds: number | readonly number[]): Command;
102
- export type SuggestionKind = "insertion" | "deletion" | "formatting" | "insertBlock" | "insertTable" | "insertRow" | "deleteRow" | "insertColumn" | "deleteColumn";
102
+ /** The ids of the comments a pending suggestion proposes. */
103
+ export declare function suggestionCommentIds(state: EditorState, suggestionId: string): number[];
104
+ export type SuggestionKind = "insertion" | "deletion" | "formatting" | "insertBlock" | "insertTable" | "insertRow" | "deleteRow" | "insertColumn" | "deleteColumn" | "comment";
103
105
  /**
104
106
  * How a suggestion is applied when accepted:
105
107
  * - `"tracked"` — converts to a normal OOXML tracked change (`w:ins`/`w:del`,
106
108
  * paragraph-mark `w:ins`, row/cell `w:ins`/`w:del`);
107
109
  * - `"direct"` — no OOXML tracked representation exists (a whole inserted
108
- * table), so accepting applies it directly;
110
+ * table), so accepting applies it directly; a suggestion that only
111
+ * proposes a comment is accepted as an ordinary comment;
109
112
  * - `"mixed"` — a heterogeneous group (one `suggestionId` spanning a whole
110
113
  * inserted table AND other edits) whose parts apply both ways. Accept still
111
114
  * resolves the whole group in one transaction; this flag lets the host
@@ -3,8 +3,9 @@ import { markParagraphPropertySourceTransfers } from "../../docx/paragraphProper
3
3
  import { RevisionResolutionStep } from "../../internal/revisionResolutionStep.js";
4
4
  import { finalRevisionParagraphRanges } from "../../internal/revisionResolutionTracking.js";
5
5
  import { deleteTextBoxesAnchoredAt, droppedTextBoxAnchorIds, moveTextBoxesPastNextParagraph } from "../anchoredTextBoxes.js";
6
- import { expectFieldAttrs, expectParagraphAttrs, expectRunPropertyChangeMarkAttrs, expectTrackedChangeMarkAttrs } from "../attrs/index.js";
6
+ import { expectCommentMarkAttrs, expectFieldAttrs, expectParagraphAttrs, expectRunPropertyChangeMarkAttrs, expectTrackedChangeMarkAttrs } from "../attrs/index.js";
7
7
  import { isCommentRangeBoundaryNode, readCommentRangeBoundaryAttrs } from "../commentRangeBoundaryAttrs.js";
8
+ import { retireThreadsResolvedAway } from "../commentRecords.js";
8
9
  import { addedBreakCarrierBefore, finalParagraphsOf } from "../containerFinalParagraph.js";
9
10
  import { enclosingRevisionIds, resolutionRemovesControl, withoutResolvedEnclosures } from "../contentControlRevisions.js";
10
11
  import { copyOriginBoundaryAttrsAfterResolution } from "../copyOriginResolution.js";
@@ -48,6 +49,7 @@ import { Fragment } from "prosemirror-model";
48
49
  import { TableMap } from "prosemirror-tables";
49
50
  import { Mapping } from "prosemirror-transform";
50
51
  import { paragraphMarkOutcome } from "@stll/docx-core/ops";
52
+ import { closeHistory } from "prosemirror-history";
51
53
  // src/prosemirror/commands/comments.ts
52
54
  /**
53
55
  * Comment and Track Changes Commands
@@ -86,6 +88,15 @@ function removeCommentMark(commentId) {
86
88
  return true;
87
89
  };
88
90
  }
91
+ /**
92
+ * What every review resolution does before it is dispatched: it retires the
93
+ * comment threads it left without an anchor, and it is an undo step of its
94
+ * own, never merged into the edit it resolves.
95
+ */
96
+ const finishResolution = (state, tr) => {
97
+ retireThreadsResolvedAway(state, tr);
98
+ closeHistory(tr);
99
+ };
89
100
  const keptIn = (range) => range.removals.flatMap(({ kept }) => kept);
90
101
  function resolveChangeUnguarded(from, to, mode, revisionIds) {
91
102
  return (state, dispatch) => {
@@ -548,6 +559,7 @@ function resolveChangeUnguarded(from, to, mode, revisionIds) {
548
559
  for (const carrier of carriers.toReversed()) tr.delete(carrier.position, carrier.position + carrier.size);
549
560
  }
550
561
  normalizeKeptRangeBoundaries(tr, keptBoundaries);
562
+ finishResolution(state, tr);
551
563
  if (tr.steps.length > 0) dispatch(tr);
552
564
  }
553
565
  return true;
@@ -981,6 +993,7 @@ const resolveAllChanges = (mode) => (state, dispatch) => {
981
993
  removedEndpointCount: result.removedEndpointCount,
982
994
  removedReferences: result.removedReferences
983
995
  });
996
+ finishResolution(state, tr);
984
997
  dispatch(tr);
985
998
  return true;
986
999
  };
@@ -1177,6 +1190,46 @@ const SUGGESTION_MARK_NAMES = /* @__PURE__ */ new Set([
1177
1190
  "runPropertyChange"
1178
1191
  ]);
1179
1192
  const isSuggestionMark = (mark) => mark.attrs["provenance"] === "suggested" && SUGGESTION_MARK_NAMES.has(mark.type.name);
1193
+ /**
1194
+ * The suggestion a comment mark belongs to: a comment a suggestion proposed
1195
+ * is pending with it, never saved, accepted and rejected with it.
1196
+ */
1197
+ const suggestedCommentOf = (mark) => {
1198
+ if (mark.type.name !== "comment") return null;
1199
+ const suggestionId = mark.attrs["suggestionId"];
1200
+ return typeof suggestionId === "string" && suggestionId.length > 0 ? suggestionId : null;
1201
+ };
1202
+ /** The ids of the comments a pending suggestion proposes. */
1203
+ function suggestionCommentIds(state, suggestionId) {
1204
+ return [...collectSuggestions(state).get(suggestionId)?.commentIds ?? []];
1205
+ }
1206
+ /**
1207
+ * Remove the comment marks of the suggestions `matches` selects, or, with
1208
+ * `keep`, clear their suggestion so they stay as ordinary comments. Mark
1209
+ * steps do not shift positions.
1210
+ */
1211
+ const resolveSuggestedCommentMarks = (tr, matches, keep) => {
1212
+ const found = [];
1213
+ tr.doc.descendants((node, pos) => {
1214
+ if (!node.isInline) return void 0;
1215
+ for (const mark of node.marks) {
1216
+ const suggestionId = suggestedCommentOf(mark);
1217
+ if (suggestionId !== null && matches(suggestionId)) found.push({
1218
+ from: pos,
1219
+ to: pos + node.nodeSize,
1220
+ mark
1221
+ });
1222
+ }
1223
+ });
1224
+ for (const { from, to, mark } of found) {
1225
+ tr.removeMark(from, to, mark);
1226
+ if (keep) tr.addMark(from, to, mark.type.create({
1227
+ ...mark.attrs,
1228
+ suggestionId: null
1229
+ }));
1230
+ }
1231
+ return found.length > 0;
1232
+ };
1180
1233
  const suggestionKindOf = (mark) => {
1181
1234
  if (mark.type.name === "insertion") return "insertion";
1182
1235
  if (mark.type.name === "deletion") return "deletion";
@@ -1267,7 +1320,8 @@ const collectSuggestions = (state) => {
1267
1320
  const created = {
1268
1321
  segments: [],
1269
1322
  kinds: /* @__PURE__ */ new Set(),
1270
- revisionIds: /* @__PURE__ */ new Set()
1323
+ revisionIds: /* @__PURE__ */ new Set(),
1324
+ commentIds: /* @__PURE__ */ new Set()
1271
1325
  };
1272
1326
  bySuggestion.set(suggestionId, created);
1273
1327
  return created;
@@ -1309,6 +1363,17 @@ const collectSuggestions = (state) => {
1309
1363
  }
1310
1364
  if (!node.isInline) return;
1311
1365
  for (const mark of node.marks) {
1366
+ const commentSuggestion = suggestedCommentOf(mark);
1367
+ if (commentSuggestion !== null) {
1368
+ const entry = entryFor(commentSuggestion);
1369
+ entry.segments.push({
1370
+ from: pos,
1371
+ to: pos + node.nodeSize
1372
+ });
1373
+ entry.kinds.add("comment");
1374
+ entry.commentIds.add(expectCommentMarkAttrs(mark).commentId);
1375
+ continue;
1376
+ }
1312
1377
  if (!isSuggestionMark(mark)) continue;
1313
1378
  const suggestionId = mark.attrs["suggestionId"];
1314
1379
  if (typeof suggestionId !== "string" || suggestionId.length === 0) continue;
@@ -1331,8 +1396,10 @@ const collectSuggestions = (state) => {
1331
1396
  * operations with one `suggestionId`) is `"mixed"`.
1332
1397
  */
1333
1398
  const suggestionAppliedAs = (kinds) => {
1399
+ const changes = [...kinds].filter((kind) => kind !== "comment");
1400
+ if (changes.length === 0) return "direct";
1334
1401
  if (!kinds.has("insertTable")) return "tracked";
1335
- return [...kinds].some((kind) => kind !== "insertTable") ? "mixed" : "direct";
1402
+ return changes.some((kind) => kind !== "insertTable") ? "mixed" : "direct";
1336
1403
  };
1337
1404
  /** Merge sorted, possibly adjacent/overlapping segments into contiguous ranges. */
1338
1405
  const mergeSegments = (segments) => {
@@ -1357,7 +1424,8 @@ const SUGGESTION_KIND_ORDER = [
1357
1424
  "insertRow",
1358
1425
  "deleteRow",
1359
1426
  "insertColumn",
1360
- "deleteColumn"
1427
+ "deleteColumn",
1428
+ "comment"
1361
1429
  ];
1362
1430
  /**
1363
1431
  * List every suggestion in the document for host consumption: its id, the
@@ -1847,8 +1915,12 @@ const acceptSuggestions = (matchesSuggestion, options) => {
1847
1915
  shouldRotate: (_node, position) => directlyMarkedParagraphPositions.has(position),
1848
1916
  fallbackAuthor: options.author
1849
1917
  })) return false;
1918
+ if (resolveSuggestedCommentMarks(tr, matchesSuggestion, true)) changed = true;
1850
1919
  if (!changed) return false;
1851
- if (dispatch) dispatch(tr);
1920
+ if (dispatch) {
1921
+ finishResolution(state, tr);
1922
+ dispatch(tr);
1923
+ }
1852
1924
  return true;
1853
1925
  };
1854
1926
  };
@@ -1911,8 +1983,12 @@ function rejectSuggestion(suggestionId) {
1911
1983
  tr: workingTr
1912
1984
  })
1913
1985
  })) return false;
1986
+ resolveSuggestedCommentMarks(workingTr, (id) => id === suggestionId, false);
1914
1987
  if (workingTr.steps.length === 0) return false;
1915
- if (dispatch) dispatch(workingTr);
1988
+ if (dispatch) {
1989
+ finishResolution(state, workingTr);
1990
+ dispatch(workingTr);
1991
+ }
1916
1992
  return true;
1917
1993
  };
1918
1994
  }
@@ -1949,8 +2025,12 @@ function rejectAllSuggestions() {
1949
2025
  tr: workingTr
1950
2026
  })
1951
2027
  })) return false;
2028
+ resolveSuggestedCommentMarks(workingTr, () => true, false);
1952
2029
  if (workingTr.steps.length === 0) return false;
1953
- if (dispatch) dispatch(workingTr);
2030
+ if (dispatch) {
2031
+ finishResolution(state, workingTr);
2032
+ dispatch(workingTr);
2033
+ }
1954
2034
  return true;
1955
2035
  };
1956
2036
  }
@@ -2161,4 +2241,4 @@ function findPreviousChange(state, startPos) {
2161
2241
  if (result.value === null && startPos < state.doc.content.size) return findPreviousChange(state, state.doc.content.size);
2162
2242
  return result.value;
2163
2243
  }
2164
- export { acceptAIEditRevision, acceptAllChanges, acceptAllSuggestions, acceptChange, acceptSuggestion, addCommentMark, changeResolutionLockViolation, findAIEditRevisionRange, findChangeAtPosition, findNextChange, findParagraphBoundaryChangeAtPosition, findPreviousChange, findSuggestionRange, getSuggestions, lockHeldRevisions, rejectAIEditRevision, rejectAllChanges, rejectAllSuggestions, rejectChange, rejectSuggestion, removeCommentMark, resolveAllChangesInHeadlessState, resolveAllChangesInHeadlessStateWithMapping, revisionResolutionLockViolation, suggestionIdOfRevision, trackedChangeEntryRevisionIds };
2244
+ export { acceptAIEditRevision, acceptAllChanges, acceptAllSuggestions, acceptChange, acceptSuggestion, addCommentMark, changeResolutionLockViolation, findAIEditRevisionRange, findChangeAtPosition, findNextChange, findParagraphBoundaryChangeAtPosition, findPreviousChange, findSuggestionRange, getSuggestions, lockHeldRevisions, rejectAIEditRevision, rejectAllChanges, rejectAllSuggestions, rejectChange, rejectSuggestion, removeCommentMark, resolveAllChangesInHeadlessState, resolveAllChangesInHeadlessStateWithMapping, revisionResolutionLockViolation, suggestionCommentIds, suggestionIdOfRevision, trackedChangeEntryRevisionIds };
@@ -0,0 +1,137 @@
1
+ import { Comment } from "../types/content.js";
2
+ import { Node, Schema } from "prosemirror-model";
3
+ import { EditorState, Plugin, Transaction } from "prosemirror-state";
4
+ import { EditorView } from "prosemirror-view";
5
+ import { Step, StepMap, StepResult } from "prosemirror-transform";
6
+ export type CommentRecords = readonly Comment[];
7
+ /**
8
+ * Replace the session's comment records. The document is unchanged, so the
9
+ * step maps nothing; its inverse puts back the records it replaced.
10
+ */
11
+ export declare class CommentRecordsStep extends Step {
12
+ readonly before: CommentRecords;
13
+ readonly after: CommentRecords;
14
+ constructor(before: CommentRecords, after: CommentRecords);
15
+ apply(doc: Node): StepResult;
16
+ getMap(): StepMap;
17
+ invert(): CommentRecordsStep;
18
+ map(): CommentRecordsStep;
19
+ merge(other: Step): CommentRecordsStep | null;
20
+ toJSON(): unknown;
21
+ static fromJSON(_schema: Schema, json: unknown): CommentRecordsStep;
22
+ }
23
+ /**
24
+ * Who changed the records: `edit` for user edits and history, which hosts
25
+ * report to their listeners; `sync` when a host wrote its own value in.
26
+ */
27
+ export type CommentRecordsOrigin = "edit" | "sync";
28
+ type CommentRecordsPluginState = {
29
+ records: CommentRecords;
30
+ origin: CommentRecordsOrigin;
31
+ };
32
+ /**
33
+ * Told the records after every change; `null` when the view's state stops
34
+ * holding records (another session took the view over, or it was destroyed).
35
+ */
36
+ type CommentRecordsListener = (records: CommentRecords | null, origin: CommentRecordsOrigin) => void;
37
+ /**
38
+ * The plugin that holds a session's comment records, starting from `initial`
39
+ * (the loaded document's comments).
40
+ */
41
+ export declare function createCommentRecordsPlugin(initial: CommentRecords): Plugin<CommentRecordsPluginState>;
42
+ /** The session's comment records, or `null` when the state holds none. */
43
+ export declare function commentRecordsOf(state: EditorState): CommentRecords | null;
44
+ /**
45
+ * Call `listener` whenever the view's comment records change, including a
46
+ * state the view is given for a newly loaded document. Returns the
47
+ * unsubscribe function.
48
+ */
49
+ export declare function subscribeCommentRecords(view: EditorView, listener: CommentRecordsListener): () => void;
50
+ /** The records a transaction ends on, given the state it started from. */
51
+ export declare function commentRecordsAfter(state: EditorState, transaction: Transaction): CommentRecords | null;
52
+ /**
53
+ * Add the step replacing the records `transaction` ends on with `next`.
54
+ * Returns false when the state holds no records.
55
+ */
56
+ export declare function stepCommentRecords(state: EditorState, transaction: Transaction, next: CommentRecords): boolean;
57
+ /**
58
+ * A transaction writing a host's value into the records: kept out of the
59
+ * undo history and reported as `sync`, so it is not echoed back to the host.
60
+ */
61
+ export declare function syncCommentRecords(state: EditorState, next: CommentRecords): Transaction | null;
62
+ /**
63
+ * Whether a transaction only writes a host's comment list in: no document
64
+ * content and no user edit changes, so it neither marks the document changed
65
+ * nor needs an editable document.
66
+ */
67
+ export declare function isCommentRecordsSync(transaction: Transaction): boolean;
68
+ /** A comment and every reply whose parent chain reaches it. */
69
+ export declare function commentThreadIds(records: CommentRecords, id: number): Set<number>;
70
+ /**
71
+ * An undoable edit replacing the records with `next`, for a host change that
72
+ * touches no anchor (a reply, a resolution, a list the host computed).
73
+ */
74
+ export declare function editCommentRecords(state: EditorState, next: CommentRecords): Transaction | null;
75
+ /** Append comments (new threads or replies) to the records. */
76
+ export declare function appendCommentRecords(state: EditorState, additions: readonly Comment[]): Transaction | null;
77
+ /** Mark one comment resolved or open again. */
78
+ export declare function setCommentResolved(state: EditorState, id: number, done: boolean): Transaction | null;
79
+ export type CommentRange = {
80
+ from: number;
81
+ to: number;
82
+ };
83
+ type DispatchingView = Pick<EditorView, "state" | "dispatch">;
84
+ /**
85
+ * Anchor a new thread on `range` and add its record, in one transaction.
86
+ * Returns null when the range is empty or out of the document.
87
+ */
88
+ export declare function addCommentThread(state: EditorState, range: CommentRange, comment: Comment): Transaction | null;
89
+ /**
90
+ * Show the add-comment flow's highlight on `range`. The highlight is not an
91
+ * edit: it stays out of the undo history, so undoing a comment never brings
92
+ * it back.
93
+ */
94
+ export declare function markPendingComment(view: DispatchingView, range: CommentRange, options?: {
95
+ selectEnd?: boolean;
96
+ }): boolean;
97
+ /** Remove the add-comment flow's highlight from `range`. */
98
+ export declare function clearPendingComment(view: DispatchingView, range: CommentRange): void;
99
+ /**
100
+ * The add-comment flow's commit: the highlight on `range` gives way to the
101
+ * new thread's mark, and the thread joins the records, as one undoable edit.
102
+ */
103
+ export declare function commitCommentThread(view: DispatchingView, range: CommentRange, comment: Comment): boolean;
104
+ /**
105
+ * Remove every anchor of the comments in `ids` from the transaction's
106
+ * document: their `comment` marks, references and range boundaries.
107
+ */
108
+ export declare function removeCommentAnchors(transaction: Transaction, ids: ReadonlySet<number>): void;
109
+ /** Delete a thread: its record, every reply under it, and its anchors. */
110
+ export declare function deleteCommentThread(state: EditorState, id: number): Transaction | null;
111
+ /**
112
+ * The comment ids a document names through a mark, reference or range
113
+ * boundary.
114
+ */
115
+ export declare function commentIdsNamedIn(doc: Node): Set<number>;
116
+ /**
117
+ * Remove the threads a review resolution left without any anchor: accepting
118
+ * the deletion of everything a comment covers, or rejecting the suggestion
119
+ * that inserted it, retires the comment with its replies, in the same
120
+ * transaction. Other edits keep records whose anchors are gone for now (a
121
+ * cut keeps the thread for the paste).
122
+ */
123
+ export declare function retireThreadsResolvedAway(state: EditorState, transaction: Transaction): void;
124
+ /**
125
+ * Add the records of `created` comments that `transaction`'s document names
126
+ * and the records lack, so comments a document operation anchors join the
127
+ * records in the operation's own transaction.
128
+ */
129
+ export declare function recordCreatedComments(state: EditorState, transaction: Transaction, created: readonly Comment[]): void;
130
+ /**
131
+ * `view` for a document operation: its dispatch adds the records of the
132
+ * `created` comments the operation anchors, in the operation's transaction.
133
+ */
134
+ export declare function recordingCommentsView(view: DispatchingView, created: readonly Comment[]): DispatchingView;
135
+ /** Panic unless the state holds records; for callers that require them. */
136
+ export declare function expectCommentRecords(state: EditorState): CommentRecords;
137
+ export {};