Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .yarn/versions/4a39fbbd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
releases:
"@handlewithcare/prosemirror-suggest-changes": patch
50 changes: 50 additions & 0 deletions src/__tests__/replaceStep.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -736,4 +736,54 @@ describe("ReplaceStep", () => {
`Expected ${trackedState.doc} to match ${expected}`,
);
});

it("should handle replacements over insertion ranges with multiple text segments", () => {
const doc = testBuilders.doc(
testBuilders.paragraph(
testBuilders.insertion({ id: "0" }, "before"),
testBuilders.insertion({ id: "0" }, "<a>plain"),
testBuilders.insertion({ id: "0" }, testBuilders.strong("bold")),
testBuilders.insertion({ id: "0" }, testBuilders.em("italic<b>both")),
),
) as TaggedNode;

// Replace content between <a> and <b> with same text but without marks
const step = replaceStep(
doc,
doc.tag["a"]!,
doc.tag["b"],
new Slice(
Fragment.from(testBuilders.schema.text("plainbolditalic")),
0,
0,
),
) as ReplaceStep | null;

assert(step, "Could not create test ReplaceStep");

const editorState = EditorState.create({
doc,
selection: TextSelection.atEnd(doc),
});

const originalTransaction = editorState.tr;
originalTransaction.step(step);

const trackedTransaction = editorState.tr;
suggestReplaceStep(trackedTransaction, editorState, doc, step, [], 1);

const trackedState = editorState.apply(trackedTransaction);

const expected = testBuilders.doc(
testBuilders.paragraph(
testBuilders.insertion({ id: "0" }, "beforeplainbolditalic"),
testBuilders.insertion({ id: "0" }, testBuilders.em("both")),
),
);

assert(
eq(trackedState.doc, expected),
`Expected ${trackedState.doc} to match ${expected}`,
);
});
});
6 changes: 6 additions & 0 deletions src/replaceStep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ export function suggestReplaceStep(
});

// Delete the previously-inserted ranges for real
// ranges are reverted, applying them in this order saves rebasing
// since deletions won't affect earlier deletions
insertedRanges.reverse();
for (const range of insertedRanges) {
trackedTransaction.delete(range.from, range.to);
}
Expand Down Expand Up @@ -212,6 +215,9 @@ export function suggestReplaceStep(
);
}

// TODO: This could break if there's already a deletion-insertion-deletion-insertion combination
// This is the code that creates those combinations, doing this twice in a row could break it

// Detect when a new mark directly abuts an existing mark with
// a different id and merge them
if (nodeAfter && markAfter && markAfter.attrs["id"] !== markId) {
Expand Down