Merge pull request #26209 from overleaf/em-multiple-edit-ops

Support multiple ops in the history OT ShareJS type

GitOrigin-RevId: fad1e9081ed1978de414c5130692d3b23fcd13d8
This commit is contained in:
Eric Mc Sween
2025-06-10 08:06:13 +00:00
committed by Copybot
parent 5b08adc4ff
commit 6d202432ff
2 changed files with 169 additions and 4 deletions
@@ -30,18 +30,49 @@ export const historyOTType = {
api,
transformX(ops1: EditOperation[], ops2: EditOperation[]) {
const [a, b] = EditOperationTransformer.transform(ops1[0], ops2[0])
return [[a], [b]]
// Dynamic programming algorithm: gradually transform both sides in a nested
// loop.
const left = [...ops1]
const right = [...ops2]
for (let i = 0; i < left.length; i++) {
for (let j = 0; j < right.length; j++) {
// At this point:
// left[0..i] is ops1[0..i] rebased over ops2[0..j-1]
// right[0..j] is ops2[0..j] rebased over ops1[0..i-1]
const [a, b] = EditOperationTransformer.transform(left[i], right[j])
left[i] = a
right[j] = b
}
}
return [left, right]
},
apply(snapshot: StringFileData, ops: EditOperation[]) {
const afterFile = StringFileData.fromRaw(snapshot.toRaw())
afterFile.edit(ops[0])
for (const op of ops) {
afterFile.edit(op)
}
return afterFile
},
compose(ops1: EditOperation[], ops2: EditOperation[]) {
return [ops1[0].compose(ops2[0])]
const ops = [...ops1, ...ops2]
let currentOp = ops.shift()
if (currentOp === undefined) {
// No ops to process
return []
}
const result = []
for (const op of ops) {
if (currentOp.canBeComposedWith(op)) {
currentOp = currentOp.compose(op)
} else {
result.push(currentOp)
currentOp = op
}
}
result.push(currentOp)
return result
},
// Do not provide normalize, used by submitOp to fixup bad input.