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/3b133141.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
releases:
"@handlewithcare/prosemirror-suggest-changes": patch
79 changes: 79 additions & 0 deletions src/__tests__/withSuggestChanges.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,85 @@ describe("withSuggestChanges", () => {
);
const newState = editorState.apply(suggestedTr);

// Should pass without error - document should be modified
expect(newState.doc.toJSON()).not.toEqual(doc.toJSON());
});
it("shouldn't throw nodeSize errors on multiline edits", () => {
// Create document with all tags for the replacements
const doc = testBuilders.doc(
testBuilders.paragraph({ id: null }, "<a>test<b> <c>test<d>"),
testBuilders.paragraph({ id: null }, "<e>test<f> <g>test<h>"),
) as TaggedNode;

const editorState = EditorState.create({
doc,
plugins: [suggestChanges()],
});

const originalTransaction = editorState.tr;

// Step 1: Replace first "test" with "WORKKKKKKK"
const step1 = replaceStep(
doc,
doc.tag["a"]!,
doc.tag["b"],
new Slice(Fragment.from(testBuilders.schema.text("WORKKKKKKK")), 0, 0),
) as ReplaceStep | null;
assert(step1, "Could not create step1");
originalTransaction.step(step1);

// Step 2: Replace second "test" with "WORKKKKKKK"
// Create step with original positions, then map it
const step2 = replaceStep(
doc,
doc.tag["c"]!,
doc.tag["d"],
new Slice(Fragment.from(testBuilders.schema.text("WORKKKKKKK")), 0, 0),
) as ReplaceStep | null;
assert(step2, "Could not create step2");
const mappedStep2 = step2.map(originalTransaction.mapping);
assert(mappedStep2, "Could not map step2");
originalTransaction.step(mappedStep2);

// Step 3: Replace third "test" with "WORKKKKKKK"
// Create step with original positions, then map it
const step3 = replaceStep(
doc,
doc.tag["e"]!,
doc.tag["f"],
new Slice(Fragment.from(testBuilders.schema.text("WORKKKKKKK")), 0, 0),
) as ReplaceStep | null;
assert(step3, "Could not create step3");
const mappedStep3 = step3.map(originalTransaction.mapping);
assert(mappedStep3, "Could not map step3");
originalTransaction.step(mappedStep3);

// Step 4: Replace fourth "test" with "WORKKKKKKK"
// Create step with original positions, then map it
const step4 = replaceStep(
doc,
doc.tag["g"]!,
doc.tag["h"],
new Slice(Fragment.from(testBuilders.schema.text("WORKKKKKKK")), 0, 0),
) as ReplaceStep | null;
assert(step4, "Could not create step4");
const mappedStep4 = step4.map(originalTransaction.mapping);
assert(mappedStep4, "Could not map step4");
originalTransaction.step(mappedStep4);

// Verify the transaction produces expected result
const originalTrState = editorState.apply(originalTransaction);
expect(originalTrState.doc.textContent).toEqual(
"WORKKKKKKK WORKKKKKKKWORKKKKKKK WORKKKKKKK",
);

// Transform to suggestion transaction
const suggestedTr = transformToSuggestionTransaction(
originalTransaction,
editorState,
);
const newState = editorState.apply(suggestedTr);

// Should pass without error - document should be modified
expect(newState.doc.toJSON()).not.toEqual(doc.toJSON());
});
Expand Down
1 change: 1 addition & 0 deletions src/rebasePos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { type Step } from "prosemirror-transform";
*/
export function rebasePos(pos: number, back: Step[], forth: Step[]) {
const reset = back
.slice()
.reverse()
.reduce((acc, step) => step.getMap().invert().map(acc), pos);
return forth.reduce((acc, step) => step.getMap().map(acc), reset);
Expand Down
1 change: 1 addition & 0 deletions src/withSuggestChanges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ function getStepHandler<S extends Step>(step: S): StepHandler<S> {
prevSteps: Step[],
) => {
const reset = prevSteps
.slice()
.reverse()
.reduce<Step | null>(
(acc, step) => acc?.map(step.getMap().invert()) ?? null,
Expand Down