mirror of
https://github.com/yu-i-i/overleaf-cep.git
synced 2026-11-12 21:23:45 +01:00
* [history-v1] add setContent endpoint * [history-v1] refactor setContent to return a change+v * [history-v1] make typescript happy GitOrigin-RevId: e2cb0281367c01aa9cd842f250f2016d591ec0fc
40 lines
884 B
JavaScript
40 lines
884 B
JavaScript
const DMP = require('diff-match-patch')
|
|
const dmp = new DMP()
|
|
|
|
// Do not attempt to produce a diff for more than 100ms
|
|
dmp.Diff_Timeout = 0.1
|
|
|
|
module.exports = {
|
|
ADDED: 1,
|
|
REMOVED: -1,
|
|
UNCHANGED: 0,
|
|
|
|
diffAsShareJsOp(before, after) {
|
|
const diffs = dmp.diff_main(before.join('\n'), after.join('\n'))
|
|
dmp.diff_cleanupSemantic(diffs)
|
|
|
|
const ops = []
|
|
let position = 0
|
|
for (const diff of diffs) {
|
|
const [type, content] = diff
|
|
if (type === this.ADDED) {
|
|
ops.push({
|
|
i: content,
|
|
p: position,
|
|
})
|
|
position += content.length
|
|
} else if (type === this.REMOVED) {
|
|
ops.push({
|
|
d: content,
|
|
p: position,
|
|
})
|
|
} else if (type === this.UNCHANGED) {
|
|
position += content.length
|
|
} else {
|
|
throw new Error('Unknown type')
|
|
}
|
|
}
|
|
return ops
|
|
},
|
|
}
|