Merge pull request #22471 from overleaf/em-tracked-deletes-at-same-position
Improve handling of tracked delete rejections GitOrigin-RevId: 35857d0a3f739c0531223737b2b649c9e8033157
This commit is contained in:
@@ -313,10 +313,11 @@ class RangesTracker {
|
||||
|
||||
let alreadyMerged = false
|
||||
let previousChange = null
|
||||
let trackedDeleteRejected = false
|
||||
const movedChanges = []
|
||||
const removeChanges = []
|
||||
const newChanges = []
|
||||
|
||||
const trackedDeletesAtOpPosition = []
|
||||
for (let i = 0; i < this.changes.length; i++) {
|
||||
change = this.changes[i]
|
||||
const changeStart = change.op.p
|
||||
@@ -327,13 +328,22 @@ class RangesTracker {
|
||||
change.op.p += opLength
|
||||
movedChanges.push(change)
|
||||
} else if (opStart === changeStart) {
|
||||
// If we are undoing, then we want to cancel any existing delete ranges if we can.
|
||||
// Check if the insert matches the start of the delete, and just remove it from the delete instead if so.
|
||||
if (
|
||||
// Keep track of tracked deletes that are at the same position as the
|
||||
// insert. At the end of the loop, if we didn't find a tracked delete
|
||||
// to reject, we'll move these deletes after the insert.
|
||||
trackedDeletesAtOpPosition.push(change)
|
||||
|
||||
if (trackedDeleteRejected && op.orderedRejections) {
|
||||
// We rejected a tracked delete. Move the remaining tracked deletes after the insert
|
||||
change.op.p += opLength
|
||||
movedChanges.push(change)
|
||||
} else if (
|
||||
undoing &&
|
||||
change.op.d.length >= op.i.length &&
|
||||
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.p += op.i.length
|
||||
if (change.op.d === '') {
|
||||
@@ -342,9 +352,7 @@ class RangesTracker {
|
||||
movedChanges.push(change)
|
||||
}
|
||||
alreadyMerged = true
|
||||
} else {
|
||||
change.op.p += opLength
|
||||
movedChanges.push(change)
|
||||
trackedDeleteRejected = true
|
||||
}
|
||||
}
|
||||
} else if (change.op.i != null) {
|
||||
@@ -449,6 +457,15 @@ class RangesTracker {
|
||||
previousChange = change
|
||||
}
|
||||
|
||||
if (!trackedDeleteRejected || !op.orderedRejections) {
|
||||
// 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) {
|
||||
this._addOp(op, metadata)
|
||||
}
|
||||
@@ -624,9 +641,13 @@ class RangesTracker {
|
||||
}
|
||||
|
||||
_addOp(op, metadata) {
|
||||
// Don't take a reference to the existing op since we'll modify this in place with future changes
|
||||
op = this._clone(op)
|
||||
// TODO: Remove this when the orderedRejections transition is over
|
||||
delete op.orderedRejections
|
||||
const change = {
|
||||
id: this.newId(),
|
||||
op: this._clone(op), // Don't take a reference to the existing op since we'll modify this in place with future changes
|
||||
op,
|
||||
metadata: this._clone(metadata),
|
||||
}
|
||||
this.changes.push(change)
|
||||
|
||||
Reference in New Issue
Block a user