@stll/folio-core 0.76.0 → 0.77.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 (86) hide show
  1. package/dist/ai-edits/headless.d.ts +6 -4
  2. package/dist/ai-edits/headless.js +32 -15
  3. package/dist/ai-edits/pending-suggestions.d.ts +9 -0
  4. package/dist/ai-edits/pending-suggestions.js +11 -0
  5. package/dist/ai-edits/read.js +24 -1
  6. package/dist/ai-edits/table-geometry.js +12 -4
  7. package/dist/canonical/canonicalInlineProjection.d.ts +2 -2
  8. package/dist/canonical/canonicalInlineProjection.js +2 -2
  9. package/dist/canonical/canonicalProjectionBlocks.d.ts +10 -1
  10. package/dist/canonical/canonicalPublicOperations.js +13 -38
  11. package/dist/canonical/canonicalSeedContent.d.ts +18 -3
  12. package/dist/canonical/canonicalSeedContent.js +28 -15
  13. package/dist/canonical/canonicalSession.d.ts +22 -12
  14. package/dist/canonical/canonicalSession.js +178 -186
  15. package/dist/controller/canonicalClipboard.d.ts +2 -1
  16. package/dist/controller/canonicalClipboard.js +312 -47
  17. package/dist/controller/canonicalComposition.js +67 -14
  18. package/dist/controller/canonicalStoryEditor.js +2 -0
  19. package/dist/controller/hiddenEditorManager.js +2 -0
  20. package/dist/docx/importedIdCollisions.js +29 -4
  21. package/dist/docx/server/templatePlan/apply.js +47 -2
  22. package/dist/docx/server/templatePlan/blocks.d.ts +27 -7
  23. package/dist/docx/server/templatePlan/blocks.js +103 -12
  24. package/dist/docx/server/templatePlan/builders.d.ts +6 -0
  25. package/dist/docx/server/templatePlan/builders.js +7 -1
  26. package/dist/docx/server/templatePlan/emit.d.ts +2 -0
  27. package/dist/docx/server/templatePlan/emit.js +108 -6
  28. package/dist/docx/server/templatePlan/plan.d.ts +24 -1
  29. package/dist/docx/server/templatePlan/plan.js +165 -12
  30. package/dist/docx/server/templatePlan/planShape.js +13 -2
  31. package/dist/docx/server/templatePlan/revisions.d.ts +55 -0
  32. package/dist/docx/server/templatePlan/revisions.js +276 -2
  33. package/dist/docx/server/templatePlan/story.d.ts +46 -1
  34. package/dist/docx/server/templatePlan/story.js +125 -11
  35. package/dist/docx/server/templatePlan/types.d.ts +97 -1
  36. package/dist/docx/server/templatePlan/verify.js +63 -9
  37. package/dist/docx/settingsHeaderFooterUpdate.d.ts +1 -1
  38. package/dist/docx/tableGridXml.d.ts +3 -7
  39. package/dist/docx/tableGridXml.js +5 -13
  40. package/dist/layout-bridge/convert/footnoteLayout.js +34 -8
  41. package/dist/prosemirror/canonicalMarkupDocument.d.ts +20 -0
  42. package/dist/prosemirror/canonicalMarkupDocument.js +73 -0
  43. package/dist/prosemirror/canonicalTableCommands.d.ts +1 -1
  44. package/dist/prosemirror/canonicalTableCommands.js +1 -3
  45. package/dist/prosemirror/conversion/markupViewSource.d.ts +17 -0
  46. package/dist/prosemirror/conversion/markupViewSource.js +11 -0
  47. package/dist/prosemirror/conversion/toProseDoc.d.ts +3 -0
  48. package/dist/prosemirror/conversion/toProseDoc.js +31 -14
  49. package/dist/prosemirror/independentMarkupProjection.d.ts +27 -0
  50. package/dist/prosemirror/independentMarkupProjection.js +147 -0
  51. package/dist/prosemirror/markupViewNotes.d.ts +1 -1
  52. package/dist/prosemirror/markupViewNotes.js +24 -27
  53. package/dist/prosemirror/markupViewPositionMap.d.ts +16 -0
  54. package/dist/prosemirror/markupViewPositionMap.js +316 -0
  55. package/dist/prosemirror/markupViewProjection.d.ts +3 -46
  56. package/dist/prosemirror/markupViewProjection.js +90 -17
  57. package/dist/prosemirror/replacedAnnotations.d.ts +4 -78
  58. package/dist/prosemirror/replacedAnnotations.js +11 -194
  59. package/dist/prosemirror/styles/styleResolver.d.ts +1 -0
  60. package/dist/prosemirror/styles/styleResolver.js +4 -1
  61. package/dist/server.d.ts +2 -2
  62. package/dist/types/canonicalCapabilities.d.ts +3 -10
  63. package/dist/types/canonicalCapabilities.js +3 -10
  64. package/package.json +2 -2
  65. package/dist/internal/revisionResolutionEdits.d.ts +0 -11
  66. package/dist/internal/revisionResolutionEdits.js +0 -18
  67. package/dist/internal/revisionResolutionInline.d.ts +0 -24
  68. package/dist/internal/revisionResolutionInline.js +0 -302
  69. package/dist/internal/revisionResolutionStep.d.ts +0 -3
  70. package/dist/internal/revisionResolutionStep.js +0 -113
  71. package/dist/internal/revisionResolutionTracking.d.ts +0 -9
  72. package/dist/internal/revisionResolutionTracking.js +0 -124
  73. package/dist/internal/wholeStoryRevisionResolution.d.ts +0 -56
  74. package/dist/internal/wholeStoryRevisionResolution.js +0 -783
  75. package/dist/prosemirror/commands/resolveAllTableChanges.d.ts +0 -38
  76. package/dist/prosemirror/commands/resolveAllTableChanges.js +0 -642
  77. package/dist/prosemirror/commands/resolveNodePropertyChangeAttrs.d.ts +0 -13
  78. package/dist/prosemirror/commands/resolveNodePropertyChangeAttrs.js +0 -45
  79. package/dist/prosemirror/commands/tableCellMergeResolution.d.ts +0 -8
  80. package/dist/prosemirror/commands/tableCellMergeResolution.js +0 -56
  81. package/dist/prosemirror/commands/tableMergeFoldDecisions.d.ts +0 -60
  82. package/dist/prosemirror/commands/tableMergeFoldDecisions.js +0 -78
  83. package/dist/prosemirror/rejoinRunCarriers.d.ts +0 -42
  84. package/dist/prosemirror/rejoinRunCarriers.js +0 -65
  85. package/dist/prosemirror/runPropertyChangeResolution.d.ts +0 -15
  86. package/dist/prosemirror/runPropertyChangeResolution.js +0 -34
@@ -0,0 +1,316 @@
1
+ import "./commentReferenceIntegrity.js";
2
+ import "./extensions/nodes/BlockMoveRangeBoundaryExtension.js";
3
+ import "./extensions/nodes/MoveRangeBoundaryExtension.js";
4
+ import "./extensions/nodes/RangeAnchorExtension.js";
5
+ import { isUsableParaId } from "./paraIds.js";
6
+ import { trackedRevisionPathOf } from "./trackedRevisionPath.js";
7
+ import { StepMap } from "prosemirror-transform";
8
+ // src/prosemirror/markupViewPositionMap.ts
9
+ const NO_REVISION_LAYERS = [];
10
+ const opposingRevision = (decision) => decision === "accept" ? "deletion" : "insertion";
11
+ const atomKey = (node) => {
12
+ const marker = node.attrs["marker"];
13
+ if (typeof marker === "object" && marker !== null && "type" in marker && typeof marker.type === "string" && "id" in marker && typeof marker.id === "number") return `leaf:${node.type.name}:${marker.type}:${marker.id}`;
14
+ const id = node.attrs["commentId"] ?? node.attrs["noteId"] ?? node.attrs["imageId"] ?? node.attrs["id"];
15
+ return typeof id === "string" || typeof id === "number" ? `leaf:${node.type.name}:${id}` : `leaf:${node.type.name}`;
16
+ };
17
+ const storySpans = (doc, decision) => {
18
+ const spans = [];
19
+ const ends = /* @__PURE__ */ new Map();
20
+ const opposed = decision === void 0 ? void 0 : opposingRevision(decision);
21
+ const visit = (node, start, parent) => {
22
+ const name = node.type.name;
23
+ const paraId = name === "paragraph" ? node.attrs["paraId"] : void 0;
24
+ const paragraphId = isUsableParaId(paraId) ? paraId.toUpperCase() : parent.paragraphId;
25
+ const cellMarker = node.attrs["cellMarker"];
26
+ const paths = node.marks.length === 0 ? NO_REVISION_LAYERS : node.marks.flatMap((mark) => mark.type.name === "insertion" || mark.type.name === "deletion" ? trackedRevisionPathOf(mark) : NO_REVISION_LAYERS);
27
+ const provenance = paths.length === 0 ? {} : { provenance: paths.map(({ type, revisionId }) => `${type}:${revisionId}`).join("/") };
28
+ const pending = parent.pending || opposed !== void 0 && (name === "moveRangeBoundary" || name === "blockMoveRangeBoundary" || name === "tableRow" && node.attrs[opposed === "insertion" ? "trIns" : "trDel"] != null || typeof cellMarker === "object" && cellMarker !== null && "kind" in cellMarker && cellMarker.kind === (opposed === "insertion" ? "ins" : "del") || paths.some(({ type }) => opposed === "deletion" ? type === "deletion" || type === "moveFrom" : type === "insertion" || type === "moveTo"));
29
+ if (node.text !== void 0) {
30
+ spans.push({
31
+ start,
32
+ width: node.text.length,
33
+ key: "text",
34
+ text: node.text,
35
+ pending,
36
+ paragraphId,
37
+ ...provenance
38
+ });
39
+ return;
40
+ }
41
+ if (node.isLeaf) {
42
+ spans.push({
43
+ start,
44
+ width: 1,
45
+ key: atomKey(node),
46
+ pending,
47
+ paragraphId,
48
+ ...provenance
49
+ });
50
+ return;
51
+ }
52
+ spans.push({
53
+ start,
54
+ width: 1,
55
+ key: `open:${name}`,
56
+ pending,
57
+ paragraphId
58
+ });
59
+ const scope = {
60
+ pending,
61
+ paragraphId
62
+ };
63
+ node.forEach((child, offset) => visit(child, start + offset + 1, scope));
64
+ if (paragraphId !== void 0 && isUsableParaId(paraId)) ends.set(paragraphId, ends.has(paragraphId) ? null : spans.length);
65
+ spans.push({
66
+ start: start + node.nodeSize - 1,
67
+ width: 1,
68
+ key: `close:${name}`,
69
+ pending,
70
+ paragraphId
71
+ });
72
+ };
73
+ const root = {
74
+ pending: false,
75
+ paragraphId: void 0
76
+ };
77
+ doc.forEach((child, start) => visit(child, start, root));
78
+ return {
79
+ spans,
80
+ ends
81
+ };
82
+ };
83
+ /**
84
+ * Match by span, across text node seams. A pending span can disappear unless
85
+ * the result still names its provenance. Keep the candidate only when it
86
+ * accounts for the entire resolved region.
87
+ */
88
+ const matchStream = ({ before, after }, skipPending) => {
89
+ const matches = [];
90
+ let leftIndex = 0;
91
+ let rightIndex = 0;
92
+ let leftOffset = 0;
93
+ let rightOffset = 0;
94
+ for (;;) {
95
+ const right = after[rightIndex];
96
+ let left = before[leftIndex];
97
+ while (skipPending && leftOffset === 0 && left?.pending === true && (left.provenance === void 0 || left.provenance !== right?.provenance)) left = before[++leftIndex];
98
+ if (!left || !right) return left || right ? void 0 : matches;
99
+ const width = Math.min(left.width - leftOffset, right.width - rightOffset);
100
+ if (left.key !== right.key || right.provenance !== void 0 && left.provenance !== right.provenance || left.text?.slice(leftOffset, leftOffset + width) !== right.text?.slice(rightOffset, rightOffset + width)) return void 0;
101
+ const leftStart = left.start + leftOffset;
102
+ const rightStart = right.start + rightOffset;
103
+ const previous = matches.at(-1);
104
+ if (previous && previous.before + previous.width === leftStart && previous.after + previous.width === rightStart) previous.width += width;
105
+ else matches.push({
106
+ before: leftStart,
107
+ after: rightStart,
108
+ width
109
+ });
110
+ leftOffset += width;
111
+ rightOffset += width;
112
+ if (leftOffset === left.width) {
113
+ leftIndex++;
114
+ leftOffset = 0;
115
+ }
116
+ if (rightOffset === right.width) {
117
+ rightIndex++;
118
+ rightOffset = 0;
119
+ }
120
+ }
121
+ };
122
+ const unitsOf = (spans) => {
123
+ const units = [];
124
+ for (const { start, width, key, text, pending, provenance, paragraphId } of spans) for (let offset = 0; offset < width; offset++) units.push({
125
+ position: start + offset,
126
+ key,
127
+ character: text?.charAt(offset),
128
+ pending,
129
+ provenance,
130
+ paragraphId
131
+ });
132
+ return units;
133
+ };
134
+ const sameUnit = (left, right) => left.key === right.key && left.character === right.character;
135
+ /** Limit ambiguous matching to a small region between surviving paragraph IDs. */
136
+ const MAX_ALIGNMENT_CELLS = 65536;
137
+ const MAX_ALIGNMENT_EDGE = 2048;
138
+ const sameStandingSpan = (left, right) => left !== void 0 && right !== void 0 && left.key === right.key && left.width === right.width && left.text === right.text && left.provenance === right.provenance;
139
+ /** Keep matching edge spans when the interior exceeds the alignment budget. */
140
+ const matchStandingEdges = ({ before, after }, match) => {
141
+ const sameSpan = (left, right) => left?.pending === false && sameStandingSpan(left, right) && left.paragraphId === right?.paragraphId;
142
+ let start = 0;
143
+ for (const span of before) {
144
+ const target = after[start];
145
+ if (!target || !sameSpan(span, target)) break;
146
+ match({
147
+ before: span.start,
148
+ after: target.start,
149
+ width: span.width
150
+ });
151
+ start++;
152
+ }
153
+ let oldEnd = before.length;
154
+ let newEnd = after.length;
155
+ while (oldEnd > start && newEnd > start && sameSpan(before[oldEnd - 1], after[newEnd - 1])) {
156
+ oldEnd--;
157
+ newEnd--;
158
+ }
159
+ for (const span of before.slice(oldEnd)) {
160
+ const target = after[newEnd++];
161
+ if (target) match({
162
+ before: span.start,
163
+ after: target.start,
164
+ width: span.width
165
+ });
166
+ }
167
+ };
168
+ const isAddressEdge = (span) => span.key.startsWith("open:") || span.key.startsWith("close:") || span.key.startsWith(`leaf:commentReference:`) || span.key.startsWith(`leaf:moveRangeBoundary`) || span.key.startsWith(`leaf:blockMoveRangeBoundary`) || span.key.startsWith(`leaf:rangeAnchor`);
169
+ /** The addresses a forward scan passes before its next content match. */
170
+ const addressGap = (spans) => {
171
+ let index = 0;
172
+ return (to) => {
173
+ const edges = [];
174
+ for (;;) {
175
+ const span = spans[index];
176
+ if (span === void 0 || span.start >= to) return edges;
177
+ index++;
178
+ if (isAddressEdge(span)) edges.push(span);
179
+ }
180
+ };
181
+ };
182
+ /** Resolution can join long runs while retiring orphaned comment references. */
183
+ const matchContent = ({ before, after }, match) => {
184
+ const content = {
185
+ before: before.filter((span) => !isAddressEdge(span)),
186
+ after: after.filter((span) => !isAddressEdge(span))
187
+ };
188
+ if (content.before.length === before.length && content.after.length === after.length) return false;
189
+ const aligned = matchStream(content, true) ?? matchStream(content, false);
190
+ if (aligned === void 0 || aligned.length === 0) return false;
191
+ const leftGap = addressGap(before);
192
+ const rightGap = addressGap(after);
193
+ for (const span of aligned) {
194
+ matchBounded({
195
+ before: leftGap(span.before),
196
+ after: rightGap(span.after)
197
+ }, match);
198
+ match(span);
199
+ }
200
+ matchBounded({
201
+ before: leftGap(Number.POSITIVE_INFINITY),
202
+ after: rightGap(Number.POSITIVE_INFINITY)
203
+ }, match);
204
+ return true;
205
+ };
206
+ /** Maximize retained positions, favouring ordinary text when repeats tie. */
207
+ const matchBounded = ({ before, after }, match) => {
208
+ const beforeWidth = before.reduce((total, span) => total + span.width, 0);
209
+ const afterWidth = after.reduce((total, span) => total + span.width, 0);
210
+ if (beforeWidth === 0 || afterWidth === 0 || Math.max(beforeWidth, afterWidth) > MAX_ALIGNMENT_EDGE || beforeWidth * afterWidth > MAX_ALIGNMENT_CELLS) {
211
+ matchStandingEdges({
212
+ before,
213
+ after
214
+ }, match);
215
+ return;
216
+ }
217
+ const left = unitsOf(before);
218
+ const right = unitsOf(after);
219
+ const rowSize = right.length + 1;
220
+ const scores = new Float64Array((left.length + 1) * rowSize);
221
+ const paragraphWeight = Math.min(left.length, right.length) + 1;
222
+ const provenanceWeight = paragraphWeight * paragraphWeight;
223
+ const matchWeight = provenanceWeight * paragraphWeight;
224
+ const weight = (l, r) => matchWeight + Number(!l.pending) + (r.paragraphId !== void 0 && l.paragraphId === r.paragraphId ? paragraphWeight : 0) + (r.provenance !== void 0 && l.provenance === r.provenance ? provenanceWeight : 0);
225
+ const reversedRight = right.toReversed();
226
+ for (const [offset, l] of left.toReversed().entries()) {
227
+ const i = left.length - 1 - offset;
228
+ for (const [rightOffset, r] of reversedRight.entries()) {
229
+ const j = right.length - 1 - rightOffset;
230
+ const at = i * rowSize + j;
231
+ scores[at] = Math.max(scores[at + rowSize] ?? 0, scores[at + 1] ?? 0, sameUnit(l, r) ? (scores[at + rowSize + 1] ?? 0) + weight(l, r) : 0);
232
+ }
233
+ }
234
+ let i = 0;
235
+ let j = 0;
236
+ const span = {
237
+ before: 0,
238
+ after: 0,
239
+ width: 1
240
+ };
241
+ for (;;) {
242
+ const l = left[i];
243
+ const r = right[j];
244
+ if (!l || !r) return;
245
+ const at = i * rowSize + j;
246
+ if (sameUnit(l, r) && scores[at] === (scores[at + rowSize + 1] ?? 0) + weight(l, r)) {
247
+ span.before = l.position;
248
+ span.after = r.position;
249
+ match(span);
250
+ i++;
251
+ j++;
252
+ } else if (scores[at] === scores[at + rowSize]) i++;
253
+ else j++;
254
+ }
255
+ };
256
+ const matchRegion = (region, match) => {
257
+ const aligned = matchStream(region, true) ?? matchStream(region, false);
258
+ if (aligned === void 0) {
259
+ if (!matchContent(region, match)) matchBounded(region, match);
260
+ return;
261
+ }
262
+ for (const span of aligned) match(span);
263
+ };
264
+ /**
265
+ * Match positions between authored and resolved projections. A surviving
266
+ * paragraph's closing edge anchors the region: on a join, preceding content
267
+ * can move into that paragraph, before its original opening edge. PM
268
+ * positions outside monotone matches are replacement ranges.
269
+ */
270
+ const markupViewPositionMap = ({ before, after, decision }) => {
271
+ if (before === after) return StepMap.empty;
272
+ const left = storySpans(before, decision);
273
+ const right = storySpans(after);
274
+ const ranges = [];
275
+ let oldEnd = 0;
276
+ let newEnd = 0;
277
+ const match = ({ before: oldStart, after: newStart, width }) => {
278
+ if (oldStart !== oldEnd || newStart !== newEnd) ranges.push(oldEnd, oldStart - oldEnd, newStart - newEnd);
279
+ oldEnd = oldStart + width;
280
+ newEnd = newStart + width;
281
+ };
282
+ let oldIndex = 0;
283
+ let newIndex = 0;
284
+ const anchor = {
285
+ before: 0,
286
+ after: 0,
287
+ width: 1
288
+ };
289
+ for (const [id, leftIndex] of left.ends) {
290
+ const rightIndex = right.ends.get(id);
291
+ if (leftIndex == null || rightIndex == null || leftIndex < oldIndex || rightIndex < newIndex) continue;
292
+ const l = left.spans[leftIndex];
293
+ const r = right.spans[rightIndex];
294
+ if (!l || !r) continue;
295
+ matchRegion({
296
+ before: left.spans.slice(oldIndex, leftIndex),
297
+ after: right.spans.slice(newIndex, rightIndex)
298
+ }, match);
299
+ anchor.before = l.start;
300
+ anchor.after = r.start;
301
+ match(anchor);
302
+ oldIndex = leftIndex + 1;
303
+ newIndex = rightIndex + 1;
304
+ }
305
+ matchRegion({
306
+ before: left.spans.slice(oldIndex),
307
+ after: right.spans.slice(newIndex)
308
+ }, match);
309
+ match({
310
+ before: before.content.size,
311
+ after: after.content.size,
312
+ width: 0
313
+ });
314
+ return ranges.length > 0 ? new StepMap(ranges) : StepMap.empty;
315
+ };
316
+ export { markupViewPositionMap };
@@ -15,56 +15,13 @@
15
15
  * layout can address every painted character by the editor position it shows.
16
16
  */
17
17
  import { indexedPositionMap } from "../internal/indexedPositionMap.js";
18
- import { RevisionResolutionMode } from "../internal/revisionResolutionInline.js";
19
- import { RemovedSectionReference } from "../internal/sectionEndpointResolution.js";
20
18
  import { DisplayMode } from "../managers/EditorModeManager.js";
21
19
  import { document_d_exports } from "../types/document.js";
22
20
  import { DeepReadonly } from "../types/documentModel.js";
23
21
  import { EditorState } from "prosemirror-state";
24
22
  import { Node } from "prosemirror-model";
25
- import { Mapping, Step, StepMap } from "prosemirror-transform";
26
- /** Resolve every revision in the state's story with the state's own styles and numbering. */
27
- export declare const resolveStateStory: (state: EditorState, mode: RevisionResolutionMode) => {
28
- resolved: Node;
29
- mapping: Mapping;
30
- positionMap: StepMap;
31
- steps: Step[];
32
- changedRanges: {
33
- from: number;
34
- to: number;
35
- }[];
36
- preStructureMap: StepMap;
37
- tableRanges: {
38
- node: Node;
39
- inputNode: Node;
40
- sourceNodeSize: number;
41
- position: number;
42
- inputMap: StepMap;
43
- positionMap: StepMap;
44
- ranges: readonly {
45
- from: number;
46
- to: number;
47
- }[];
48
- paragraphOffsets: readonly {
49
- source: number;
50
- final: number;
51
- }[];
52
- }[];
53
- transfers: {
54
- displacedToken: string | null;
55
- selectedToken: string | null;
56
- }[];
57
- structuralRanges: {
58
- from: number;
59
- to: number;
60
- }[];
61
- structuralMap: StepMap;
62
- structural: boolean;
63
- failed: boolean;
64
- removedEndpointCount: number;
65
- removedReferences: RemovedSectionReference[];
66
- keptBoundaries: Set<Node>;
67
- };
23
+ import { RevisionDecision } from "@stll/docx-core/ops";
24
+ import { StepMap } from "prosemirror-transform";
68
25
  type PositionRange = {
69
26
  from: number;
70
27
  to: number;
@@ -101,7 +58,7 @@ export type MarkupViewProjection =
101
58
  * plugins, which may change while the document node stays the same.
102
59
  */
103
60
  /** How a view resolves the revisions it reads, or null when it shows them inline. */
104
- export declare const markupViewResolutionMode: (view: DisplayMode) => RevisionResolutionMode | null;
61
+ export declare const markupViewResolutionMode: (view: DisplayMode) => RevisionDecision | null;
105
62
  export declare const projectMarkupView: (state: EditorState, view: DisplayMode) => MarkupViewProjection;
106
63
  /**
107
64
  * Where a caret at editor `position` is painted in a view. A position the view
@@ -1,18 +1,13 @@
1
1
  import { indexedPositionMap } from "../internal/indexedPositionMap.js";
2
- import { finalRevisionParagraphRanges } from "../internal/revisionResolutionTracking.js";
3
- import { resolveWholeStory } from "../internal/wholeStoryRevisionResolution.js";
4
- import { createDocumentNumberingPlugin, getDocumentNumbering } from "./plugins/documentNumbering.js";
5
- import { getDocumentStyleResolver } from "./plugins/documentStyleState.js";
2
+ import { expectTableCellAttrs, expectTableRowAttrs } from "./attrs/index.js";
3
+ import { canonicalMarkupDocument } from "./canonicalMarkupDocument.js";
4
+ import { markupViewPositionMap } from "./markupViewPositionMap.js";
5
+ import { createDocumentNumberingPlugin } from "./plugins/documentNumbering.js";
6
6
  import { createDocumentStyleContextPlugin } from "./plugins/documentStyles.js";
7
+ import { getFolioNodeRevisionCarriers } from "./revisionCarriers.js";
8
+ import { getTableCellMergeChange } from "./tableCellMergeRevision.js";
7
9
  import { EditorState, Selection } from "prosemirror-state";
8
10
  // src/prosemirror/markupViewProjection.ts
9
- /** Resolve every revision in the state's story with the state's own styles and numbering. */
10
- const resolveStateStory = (state, mode) => resolveWholeStory({
11
- doc: state.doc,
12
- mode,
13
- styleResolver: getDocumentStyleResolver(state),
14
- numbering: getDocumentNumbering(state)
15
- });
16
11
  /**
17
12
  * Every view's reading of the document. Total over `DisplayMode`, so a new view
18
13
  * cannot ship without a decision about what it lays out.
@@ -37,6 +32,84 @@ const MARKUP_VIEW_RESOLUTIONS = {
37
32
  };
38
33
  const AUTHORED = { type: "authored" };
39
34
  const projectionCache = /* @__PURE__ */ new WeakMap();
35
+ const documentViews = /* @__PURE__ */ new WeakMap();
36
+ const hasStoredMergeRevision = (cells) => Array.isArray(cells) && cells.some(getTableCellMergeChange);
37
+ const retainedTableChange = (node) => {
38
+ switch (node.type.name) {
39
+ case "tableRow": return expectTableRowAttrs(node).trIns == null ? void 0 : (retained) => retained.type === node.type && expectTableRowAttrs(retained).trIns == null;
40
+ case "tableCell":
41
+ case "tableHeader": {
42
+ const { cellMarker, rowspan, _docxVMergeContinuationCells } = expectTableCellAttrs(node);
43
+ const inserted = cellMarker?.kind === "ins";
44
+ const storedRevision = hasStoredMergeRevision(_docxVMergeContinuationCells);
45
+ if (!inserted && rowspan <= 1 && !storedRevision) return void 0;
46
+ return (retained) => {
47
+ if (retained.type !== node.type) return false;
48
+ const after = expectTableCellAttrs(retained);
49
+ return inserted && after.cellMarker?.kind !== "ins" || rowspan > after.rowspan || storedRevision && !hasStoredMergeRevision(after._docxVMergeContinuationCells);
50
+ };
51
+ }
52
+ default: return;
53
+ }
54
+ };
55
+ /** Paragraphs affected by accepted revisions, including retained rows and cells. */
56
+ const changeBars = (before, { doc, positionMap }) => {
57
+ const toView = indexedPositionMap(positionMap);
58
+ const changed = [];
59
+ const tableChanges = /* @__PURE__ */ new Map();
60
+ before.descendants((node, position) => {
61
+ const tableChange = retainedTableChange(node);
62
+ if (tableChange !== void 0) {
63
+ const from = toView(position, 1);
64
+ if (toView(position + 1, -1) === from + 1) tableChanges.set(from, tableChange);
65
+ }
66
+ if (node.type.name !== "paragraph") return true;
67
+ let hasRevision = getFolioNodeRevisionCarriers(node, position).length > 0;
68
+ node.forEach((child) => {
69
+ hasRevision ||= child.marks.some(({ type }) => type.name === "insertion" || type.name === "deletion" || type.name === "runPropertyChange");
70
+ });
71
+ if (hasRevision) {
72
+ const from = toView(position, 1);
73
+ const to = toView(position + node.nodeSize, -1);
74
+ if (from < to) changed.push({
75
+ from,
76
+ to
77
+ });
78
+ }
79
+ return false;
80
+ });
81
+ if (tableChanges.size > 0) doc.descendants((node, position) => {
82
+ if (tableChanges.get(position)?.(node)) changed.push({
83
+ from: position,
84
+ to: position + node.nodeSize
85
+ });
86
+ return node.type.name !== "paragraph";
87
+ });
88
+ return changed.filter(({ from, to }) => from >= 0 && to <= doc.content.size);
89
+ };
90
+ const viewForDecision = (state, decision) => {
91
+ let views = documentViews.get(state);
92
+ if (views === void 0) {
93
+ views = /* @__PURE__ */ new Map();
94
+ documentViews.set(state, views);
95
+ }
96
+ const cached = views.get(decision);
97
+ if (cached !== void 0) return cached;
98
+ const resolved = canonicalMarkupDocument({
99
+ state,
100
+ decision
101
+ });
102
+ const view = resolved.doc.eq(state.doc) ? null : {
103
+ ...resolved,
104
+ positionMap: resolved.positionMap ?? markupViewPositionMap({
105
+ before: state.doc,
106
+ after: resolved.doc,
107
+ decision
108
+ })
109
+ };
110
+ views.set(decision, view);
111
+ return view;
112
+ };
40
113
  /**
41
114
  * The document a markup view lays out, for the state's current document.
42
115
  * Cached per editor state: the resolver also reads its styles and numbering
@@ -53,15 +126,15 @@ const projectMarkupView = (state, view) => {
53
126
  let byView = projectionCache.get(state);
54
127
  const cached = byView?.get(view);
55
128
  if (cached) return cached;
56
- const result = resolveStateStory(state, resolution.mode);
57
- const projection = result.resolved.eq(state.doc) ? AUTHORED : {
129
+ const result = viewForDecision(state, resolution.mode);
130
+ const projection = result === null ? AUTHORED : {
58
131
  type: "resolved",
59
- doc: result.resolved,
132
+ doc: result.doc,
60
133
  positionMap: result.positionMap,
61
134
  toView: indexedPositionMap(result.positionMap),
62
135
  toEditor: indexedPositionMap(result.positionMap.invert()),
63
- changeBarRanges: resolution.indicators === "change-bars" ? finalRevisionParagraphRanges(result) : [],
64
- completeness: result.failed ? "partial" : "complete"
136
+ changeBarRanges: resolution.indicators === "change-bars" ? result.changeBarRanges ?? changeBars(state.doc, result) : [],
137
+ completeness: result.partial ? "partial" : "complete"
65
138
  };
66
139
  if (!byView) {
67
140
  byView = /* @__PURE__ */ new Map();
@@ -102,4 +175,4 @@ const projectStoryMarkupView = ({ doc, markupView, styles, numbering }) => {
102
175
  });
103
176
  return projectMarkupView(state, markupView);
104
177
  };
105
- export { markupViewResolutionMode, projectMarkupView, projectStoryMarkupView, resolveStateStory, visibleCaretPosition };
178
+ export { markupViewResolutionMode, projectMarkupView, projectStoryMarkupView, visibleCaretPosition };
@@ -17,9 +17,9 @@
17
17
  * span. It can propose a point at the replacement start for an emptied range;
18
18
  * downstream ranged-comment exhaustion may retire that comment. The applier
19
19
  * decides alignment once for direct and tracked edits.
20
- * Canonical replacements accept only coverage the source anchors already
21
- * express. Pure deletion follows ranged-comment exhaustion; unproven point
22
- * transitions refuse before mutation.
20
+ * Canonical replacements place comment anchors by the docx-core policy
21
+ * beside `rangeEndAfterDeletion`: markers inside the replaced words stay,
22
+ * in order, before the new words, and a range left covering nothing goes.
23
23
  *
24
24
  * - `comment` mark: carried over its aligned replacement span.
25
25
  * - `hyperlink` mark: the replacement carries the first link the span held, so
@@ -59,10 +59,8 @@
59
59
  * already gives the replacement the formatting of the text it replaces.
60
60
  *
61
61
  * Removing the owning block removes its comment. Point anchors proposed by
62
- * the alignment helper do not override ranged-comment retirement; the
63
- * canonical guard refuses point transitions whose outcome is not proven.
62
+ * the alignment helper do not override ranged-comment retirement.
64
63
  */
65
- import { document_d_exports } from "../types/document.js";
66
64
  import { Transaction } from "prosemirror-state";
67
65
  import { Fragment, Mark, Node, Schema } from "prosemirror-model";
68
66
  /**
@@ -117,78 +115,6 @@ export declare const surveyReplacedAnnotations: (doc: Node, from: number, to: nu
117
115
  * carries too.
118
116
  */
119
117
  export declare const surveyAcceptedReplacedAnnotations: (doc: Node, from: number, to: number) => ReplacedAnnotations;
120
- /**
121
- * How a replacement removes the words it changes, which decides what becomes
122
- * of a comment on them.
123
- */
124
- export type ReplacementRemoval = {
125
- /**
126
- * Removed outright: a thread whose root keeps no accepted word outside
127
- * the changes goes with its words, markers and replies, as the direct
128
- * edit retires every thread it exhausts, so its markers need no
129
- * alignment.
130
- */
131
- type: "direct";
132
- comments: readonly document_d_exports.Comment[];
133
- } | {
134
- /**
135
- * Tracked: the deleted words keep their comment ranges, so rejecting
136
- * gives every range back and accepting drops a comment whose words all
137
- * went. Only a point comment or a selected marker still needs the
138
- * comment to move.
139
- */
140
- type: "tracked";
141
- } | {
142
- /**
143
- * Recorded as a suggestion: its changes neither keep a comment on the
144
- * words they remove nor retire one, so every comment they reach must
145
- * already align.
146
- */
147
- type: "suggested";
148
- };
149
- type ReplacementChange = {
150
- from: number;
151
- to: number;
152
- text: string;
153
- markers: "retained" | "selected";
154
- };
155
- type ReplacementCommentAlignmentOptions = {
156
- doc: Node;
157
- source: {
158
- from: number;
159
- to: number;
160
- };
161
- retiredCommentIds?: ReadonlySet<number> | undefined;
162
- removal: ReplacementRemoval;
163
- changes: readonly ReplacementChange[];
164
- };
165
- type DirectReplacementSplit<Change extends ReplacementChange> = {
166
- changes: Change[];
167
- /** Threads the unsplit replacement takes whole. */
168
- retiredCommentIds: ReadonlySet<number>;
169
- };
170
- /**
171
- * A direct replacement's changes cut at each boundary of a comment it keeps
172
- * that stands inside a changed stretch: the words before the boundary go, and
173
- * the new words follow every such boundary, where accepting the tracked
174
- * replacement leaves them. A range ending in the stretch ends before the new
175
- * words; one starting in it holds them.
176
- */
177
- export declare const splitAtKeptCommentBoundaries: <Change extends ReplacementChange>({ doc, changes, comments }: {
178
- doc: Node;
179
- changes: readonly Change[];
180
- comments: readonly document_d_exports.Comment[];
181
- }) => DirectReplacementSplit<Change>;
182
- /**
183
- * Whether source boundaries already express the replacement's aligned ranges.
184
- * A changed stretch lends no comment to its replacement: the new words go
185
- * past the end of every range the stretch reaches, and a direct replacement
186
- * takes every thread it leaves nothing to cover. Pure deletions within
187
- * surviving source content follow the ranged-comment exhaustion owner; other
188
- * collapses need a point policy. Decide over the whole replacement, before
189
- * any of its fragments are applied.
190
- */
191
- export declare const preservesReplacementCommentAlignment: ({ doc, source, changes, retiredCommentIds, removal }: ReplacementCommentAlignmentOptions) => boolean;
192
118
  type AnnotatedReplacementOptions = {
193
119
  annotations: ReplacedAnnotations;
194
120
  /** The replacement's own inline content, already carrying its formatting. */