automerge/edit-trace/automerge-js.js
2022-07-07 09:25:45 +01:00

27 lines
875 B
JavaScript

// Apply the paper editing trace to an Automerge.Text object, one char at a time
const { edits, finalText } = require('./editing-trace')
const Automerge = require('../automerge-js')
const wasm_api = require('../automerge-wasm')
Automerge.use(wasm_api)
const start = new Date()
let state = Automerge.from({text: new Automerge.Text()})
state = Automerge.change(state, doc => {
for (let i = 0; i < edits.length; i++) {
if (i % 10000 === 0) {
console.log(`Processed ${i} edits in ${new Date() - start} ms`)
}
let edit = edits[i]
if (edit[1] > 0) doc.text.deleteAt(edit[0], edit[1])
if (edit.length > 2) doc.text.insertAt(edit[0], ...edit.slice(2))
}
})
let _ = Automerge.save(state)
console.log(`Done in ${new Date() - start} ms`)
if (state.text.join('') !== finalText) {
throw new RangeError('ERROR: final text did not match expectation')
}