Skip to content

Commit

Permalink
Fix bug where deletions' end position wasn't mapped forward
Browse files Browse the repository at this point in the history
  • Loading branch information
marijnh committed Sep 19, 2017
1 parent 723d7f9 commit 7d24824
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/changeset.js
Expand Up @@ -56,9 +56,9 @@ export class ChangeSet {
if (maps.length == 0) return this

maps = this.maps.concat(maps)
let inserted = [], deleted = this.deleted.concat()
let inserted = [], deleted = []

// Map existing inserted spans forward
// Map existing inserted and deleted spans forward
for (let i = 0; i < this.inserted.length; i++) {
let span = this.inserted[i], {from, to} = span
for (let j = this.maps.length; j < maps.length && to > from; j++) {
Expand All @@ -67,6 +67,11 @@ export class ChangeSet {
}
if (to > from) inserted.push(from != span.from || to != span.to ? new Span(from, to, span.data) : span)
}
for (let i = 0; i < this.deleted.length; i++) {
let span = this.deleted[i], pos = span.pos
for (let j = this.maps.length; j < maps.length; j++) pos = maps[j].map(pos, -1)
deleted.push(pos == span.pos ? span : new DeletedSpan(span.from, span.to, span.data, pos, span.slice))
}

// Add spans for new steps.
let newBoundaries = [] // Used to make sure new insertions are checked for merging
Expand Down
5 changes: 5 additions & 0 deletions test/test-changes.js
Expand Up @@ -63,6 +63,11 @@ describe("ChangeSet", () => {
(tr, p) => tr.insert(p("a"), schema.text("--")),
(tr, p) => tr.delete(p("a"), p("a") + 1)
], {a: 1}))

it("maps deletions forward", find(doc(p("f<a>ooba<b>r<c>")), [
(tr, p) => tr.delete(p("b"), p("c")),
(tr, p) => tr.insert(p("a"), schema.text("OKAY"))
], {a: 4}, {b: "r"}))
})

function find(doc, build, insertions, deletions, sep) {
Expand Down

0 comments on commit 7d24824

Please sign in to comment.