Skip to content

Commit 1881c50

Browse files
committed
Fixed stale doc in replaceStep.ts, added test.
(cherry picked from commit b71c031)
1 parent 1c9a5c9 commit 1881c50

2 files changed

Lines changed: 88 additions & 1 deletion

File tree

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import { EditorState, TextSelection } from "prosemirror-state";
2+
import { Step } from "prosemirror-transform";
3+
import { describe, expect, it } from "vitest";
4+
5+
import { testBuilders } from "../testing/testBuilders.js";
6+
import { suggestChanges } from "../plugin.js";
7+
import { transformToSuggestionTransaction } from "../withSuggestChanges.js";
8+
9+
const steps = [
10+
{
11+
stepType: "replace",
12+
from: 11,
13+
to: 15,
14+
slice: {
15+
content: [
16+
{
17+
type: "text",
18+
text: "WORK",
19+
},
20+
],
21+
},
22+
},
23+
{
24+
stepType: "replace",
25+
from: 38,
26+
to: 42,
27+
slice: {
28+
content: [
29+
{
30+
type: "text",
31+
text: "WORK",
32+
},
33+
],
34+
},
35+
},
36+
{
37+
stepType: "replace",
38+
from: 43,
39+
to: 47,
40+
slice: {
41+
content: [
42+
{
43+
type: "text",
44+
text: "WORK",
45+
},
46+
],
47+
},
48+
},
49+
];
50+
51+
describe("withSuggestChanges", () => {
52+
it("should wrap an insertion in a mark", () => {
53+
// should match initialJson
54+
const doc = testBuilders.doc(
55+
testBuilders.paragraph({ id: null }, "This is a test paragraph."),
56+
testBuilders.paragraph({ id: null }, "This is a test test paragraph."),
57+
);
58+
59+
const end = TextSelection.atEnd(doc);
60+
const trSteps = steps.map((step) => Step.fromJSON(doc.type.schema, step));
61+
const editorState = EditorState.create({
62+
doc,
63+
selection: end,
64+
plugins: [suggestChanges()],
65+
});
66+
67+
const originalTransaction = editorState.tr;
68+
trSteps.forEach((s) => {
69+
originalTransaction.step(s);
70+
});
71+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
72+
expect(originalTransaction.steps.map((s) => s.toJSON())).toEqual(steps);
73+
const originalTrState = editorState.apply(originalTransaction);
74+
expect(originalTrState.doc.textContent).toEqual(
75+
"This is a WORK paragraph.This is a WORK WORK paragraph.",
76+
);
77+
const suggestedTr = transformToSuggestionTransaction(
78+
originalTransaction,
79+
editorState,
80+
);
81+
const newState = editorState.apply(suggestedTr);
82+
// Should pass without error
83+
expect(newState.doc.toJSON()).not.toEqual(doc.toJSON());
84+
});
85+
});

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)