Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BugFix: Handling multiple changes in a singular transaction #10

Merged
merged 1 commit into from
Jan 9, 2024
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
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