Skip to content

Commit 39f0eae

Browse files
committed
Fixed stale doc in replaceStep.ts, added test.
1 parent 1c9a5c9 commit 39f0eae

2 files changed

Lines changed: 81 additions & 1 deletion

File tree

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
2+
/* eslint-disable @typescript-eslint/no-non-null-assertion */
3+
import { EditorState } from "prosemirror-state";
4+
import { Fragment, Slice } from "prosemirror-model";
5+
import { type ReplaceStep, replaceStep } from "prosemirror-transform";
6+
import { assert, describe, expect, it } from "vitest";
7+
8+
import { type TaggedNode, testBuilders } from "../testing/testBuilders.js";
9+
import { suggestChanges } from "../plugin.js";
10+
import { transformToSuggestionTransaction } from "../withSuggestChanges.js";
11+
12+
describe("withSuggestChanges", () => {
13+
it("should wrap an insertion in a mark", () => {
14+
// Create document with all tags at once
15+
const doc = testBuilders.doc(
16+
testBuilders.paragraph({ id: null }, "This is a <a>test<b> paragraph."),
17+
testBuilders.paragraph(
18+
{ id: null },
19+
"This is a <c>test<d> <e>test<f> paragraph.",
20+
),
21+
) as TaggedNode;
22+
23+
const editorState = EditorState.create({
24+
doc,
25+
plugins: [suggestChanges()],
26+
});
27+
28+
const originalTransaction = editorState.tr;
29+
30+
// Step 1: Replace "test" with "WORK" in first paragraph
31+
const step1 = replaceStep(
32+
doc,
33+
doc.tag["a"]!,
34+
doc.tag["b"],
35+
new Slice(Fragment.from(testBuilders.schema.text("WORK")), 0, 0),
36+
) as ReplaceStep | null;
37+
assert(step1, "Could not create step1");
38+
originalTransaction.step(step1);
39+
40+
// Step 2: Replace first "test" with "WORK" in second paragraph
41+
// Use the updated document from the transaction
42+
const step2 = replaceStep(
43+
originalTransaction.doc,
44+
doc.tag["c"]!,
45+
doc.tag["d"],
46+
new Slice(Fragment.from(testBuilders.schema.text("WORK")), 0, 0),
47+
) as ReplaceStep | null;
48+
assert(step2, "Could not create step2");
49+
originalTransaction.step(step2);
50+
51+
// Step 3: Replace second "test" with "WORK" in second paragraph
52+
// Use the updated document from the transaction
53+
const step3 = replaceStep(
54+
originalTransaction.doc,
55+
doc.tag["e"]!,
56+
doc.tag["f"],
57+
new Slice(Fragment.from(testBuilders.schema.text("WORK")), 0, 0),
58+
) as ReplaceStep | null;
59+
assert(step3, "Could not create step3");
60+
originalTransaction.step(step3);
61+
62+
// Verify the transaction produces expected result
63+
const originalTrState = editorState.apply(originalTransaction);
64+
expect(originalTrState.doc.textContent).toEqual(
65+
"This is a WORK paragraph.This is a WORK WORK paragraph.",
66+
);
67+
68+
// Transform to suggestion transaction
69+
const suggestedTr = transformToSuggestionTransaction(
70+
originalTransaction,
71+
editorState,
72+
);
73+
const newState = editorState.apply(suggestedTr);
74+
75+
// Should pass without error - document should be modified
76+
expect(newState.doc.toJSON()).not.toEqual(doc.toJSON());
77+
});
78+
});

src/replaceStep.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@ export function suggestReplaceStep(
8484
let stepTo = rebasePos(step.to, prevSteps, trackedTransaction.steps);
8585

8686
if (state.selection.empty && stepFrom !== stepTo) {
87-
trackedTransaction.setSelection(TextSelection.near(doc.resolve(stepFrom)));
87+
trackedTransaction.setSelection(
88+
TextSelection.near(trackedTransaction.doc.resolve(stepFrom)),
89+
);
8890
}
8991

9092
// Make a list of any existing insertions that fall within the

0 commit comments

Comments
 (0)