Skip to content

Commit

Permalink
BugFix: Handling multiple changes in a singular transaction (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
KsanStone authored Jan 9, 2024
1 parent 50223e2 commit c54592e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/codeMirrorToAm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@ export default function (
(
fromA: number,
toA: number,
_fromB: number,
fromB: number,
_toB: number,
inserted: Text
) => {
am.splice(doc, path, fromA, toA - fromA, inserted.toString())
// We are cloning the path as `am.splice` calls `.unshift` on it, modifying it in place,
// causing the path to be broken on subsequent changes
am.splice(doc, path.slice(), fromB, toA - fromA, inserted.toString())
}
)
}
Expand Down
32 changes: 32 additions & 0 deletions test/Editor.cy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,38 @@ describe("<Editor />", () => {
assert.equal(doc.text, "Hello")
})
})

it("handles moving lines", () => {
const { handle } = makeHandle("Hello\nWorld")
mount(<Editor handle={handle} path={["text"]} />)
cy.get("div.cm-content").type("{ctrl+end}{alt+upArrow}")
cy.get("div.cm-content").should(
"have.html",
expectedHtml(["World", "Hello"], 1)
)
cy.wait(100).then(async () => {
const doc = await handle.doc()
assert.equal(doc.text, "World\nHello")
})
})

it("handles multiple cursors", () => {
const { handle } = makeHandle("Hello\nWorld\nThere!")
mount(<Editor handle={handle} path={["text"]} />)
cy.get("div.cm-content>.cm-line").eq(0).click()
cy.get("div.cm-content>.cm-line").eq(1).click({ ctrlKey: true })
cy.get("div.cm-content>.cm-line").eq(2).click({ ctrlKey: true })
cy.get("div.cm-content").type(" Lines{home}{shift+rightArrow}{del}")
cy.get("div.cm-content>.cm-line").eq(0).click()
cy.get("div.cm-content").should(
"have.html",
expectedHtml(["ello Lines", "orld Lines", "here! Lines"], 0)
)
cy.wait(100).then(async () => {
const doc = await handle.doc()
assert.equal(doc.text, "ello Lines\norld Lines\nhere! Lines")
})
})
})

describe("remote changes", () => {
Expand Down

0 comments on commit c54592e

Please sign in to comment.