mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-21 20:47:08 -05:00
Merge pull request #21643 from overleaf/em-tracked-deletes-at-same-position
Improve handling of tracked delete rejections GitOrigin-RevId: 79308539f5e7af70f6292a0fd6352e882eac84dd
This commit is contained in:
parent
c9a6724790
commit
bc1e3dacda
2 changed files with 66 additions and 7 deletions
|
@ -313,10 +313,11 @@ class RangesTracker {
|
||||||
|
|
||||||
let alreadyMerged = false
|
let alreadyMerged = false
|
||||||
let previousChange = null
|
let previousChange = null
|
||||||
|
let trackedDeleteRejected = false
|
||||||
const movedChanges = []
|
const movedChanges = []
|
||||||
const removeChanges = []
|
const removeChanges = []
|
||||||
const newChanges = []
|
const newChanges = []
|
||||||
|
const trackedDeletesAtOpPosition = []
|
||||||
for (let i = 0; i < this.changes.length; i++) {
|
for (let i = 0; i < this.changes.length; i++) {
|
||||||
change = this.changes[i]
|
change = this.changes[i]
|
||||||
const changeStart = change.op.p
|
const changeStart = change.op.p
|
||||||
|
@ -327,13 +328,22 @@ class RangesTracker {
|
||||||
change.op.p += opLength
|
change.op.p += opLength
|
||||||
movedChanges.push(change)
|
movedChanges.push(change)
|
||||||
} else if (opStart === changeStart) {
|
} else if (opStart === changeStart) {
|
||||||
// If we are undoing, then we want to cancel any existing delete ranges if we can.
|
// Keep track of tracked deletes that are at the same position as the
|
||||||
// Check if the insert matches the start of the delete, and just remove it from the delete instead if so.
|
// insert. At the end of the loop, If we didn't find a tracked delete
|
||||||
if (
|
// to reject, we'll move these deletes after the insert.
|
||||||
|
trackedDeletesAtOpPosition.push(change)
|
||||||
|
|
||||||
|
if (trackedDeleteRejected) {
|
||||||
|
// We rejected a tracked delete. Move the remaining tracked deletes after the insert
|
||||||
|
change.op.p += opLength
|
||||||
|
movedChanges.push(change)
|
||||||
|
} else if (
|
||||||
undoing &&
|
undoing &&
|
||||||
change.op.d.length >= op.i.length &&
|
change.op.d.length >= op.i.length &&
|
||||||
change.op.d.slice(0, op.i.length) === op.i
|
change.op.d.slice(0, op.i.length) === op.i
|
||||||
) {
|
) {
|
||||||
|
// If we are undoing, then we want to reject any existing tracked delete if we can.
|
||||||
|
// Check if the insert matches the start of the delete, and just remove it from the delete instead if so.
|
||||||
change.op.d = change.op.d.slice(op.i.length)
|
change.op.d = change.op.d.slice(op.i.length)
|
||||||
change.op.p += op.i.length
|
change.op.p += op.i.length
|
||||||
if (change.op.d === '') {
|
if (change.op.d === '') {
|
||||||
|
@ -342,9 +352,7 @@ class RangesTracker {
|
||||||
movedChanges.push(change)
|
movedChanges.push(change)
|
||||||
}
|
}
|
||||||
alreadyMerged = true
|
alreadyMerged = true
|
||||||
} else {
|
trackedDeleteRejected = true
|
||||||
change.op.p += opLength
|
|
||||||
movedChanges.push(change)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (change.op.i != null) {
|
} else if (change.op.i != null) {
|
||||||
|
@ -449,6 +457,15 @@ class RangesTracker {
|
||||||
previousChange = change
|
previousChange = change
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!trackedDeleteRejected) {
|
||||||
|
// We didn't reject any tracked delete. Move all existing tracked deletes
|
||||||
|
// at the insert position after the insert
|
||||||
|
for (const change of trackedDeletesAtOpPosition) {
|
||||||
|
change.op.p += opLength
|
||||||
|
movedChanges.push(change)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (this.track_changes && !alreadyMerged) {
|
if (this.track_changes && !alreadyMerged) {
|
||||||
this._addOp(op, metadata)
|
this._addOp(op, metadata)
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ const RangesTracker = require('../..')
|
||||||
describe('RangesTracker', function () {
|
describe('RangesTracker', function () {
|
||||||
describe('with duplicate change ids', function () {
|
describe('with duplicate change ids', function () {
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
|
this.comments = []
|
||||||
this.changes = [
|
this.changes = [
|
||||||
{ id: 'id1', op: { p: 1, i: 'hello' } },
|
{ id: 'id1', op: { p: 1, i: 'hello' } },
|
||||||
{ id: 'id2', op: { p: 10, i: 'world' } },
|
{ id: 'id2', op: { p: 10, i: 'world' } },
|
||||||
|
@ -26,4 +27,45 @@ describe('RangesTracker', function () {
|
||||||
expect(this.rangesTracker.changes).to.deep.equal([this.changes[2]])
|
expect(this.rangesTracker.changes).to.deep.equal([this.changes[2]])
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('with multiple tracked deletes at the same position', function () {
|
||||||
|
beforeEach(function () {
|
||||||
|
this.comments = []
|
||||||
|
this.changes = [
|
||||||
|
{ id: 'id1', op: { p: 33, d: 'before' } },
|
||||||
|
{ id: 'id2', op: { p: 50, d: 'right before' } },
|
||||||
|
{ id: 'id3', op: { p: 50, d: 'this one' } },
|
||||||
|
{ id: 'id4', op: { p: 50, d: 'right after' } },
|
||||||
|
{ id: 'id5', op: { p: 75, d: 'long after' } },
|
||||||
|
]
|
||||||
|
this.rangesTracker = new RangesTracker(this.changes, this.comments)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('preserves the text order when rejecting changes', function () {
|
||||||
|
this.rangesTracker.applyOp(
|
||||||
|
{ p: 50, i: 'this one', u: true },
|
||||||
|
{ user_id: 'user-id' }
|
||||||
|
)
|
||||||
|
expect(this.rangesTracker.changes).to.deep.equal([
|
||||||
|
{ id: 'id1', op: { p: 33, d: 'before' } },
|
||||||
|
{ id: 'id2', op: { p: 50, d: 'right before' } },
|
||||||
|
{ id: 'id4', op: { p: 58, d: 'right after' } },
|
||||||
|
{ id: 'id5', op: { p: 83, d: 'long after' } },
|
||||||
|
])
|
||||||
|
})
|
||||||
|
|
||||||
|
it('moves all tracked deletes after the insert if not rejecting changes', function () {
|
||||||
|
this.rangesTracker.applyOp(
|
||||||
|
{ p: 50, i: 'some other text', u: true },
|
||||||
|
{ user_id: 'user-id' }
|
||||||
|
)
|
||||||
|
expect(this.rangesTracker.changes).to.deep.equal([
|
||||||
|
{ id: 'id1', op: { p: 33, d: 'before' } },
|
||||||
|
{ id: 'id2', op: { p: 65, d: 'right before' } },
|
||||||
|
{ id: 'id3', op: { p: 65, d: 'this one' } },
|
||||||
|
{ id: 'id4', op: { p: 65, d: 'right after' } },
|
||||||
|
{ id: 'id5', op: { p: 90, d: 'long after' } },
|
||||||
|
])
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in a new issue