Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// We'll replace the entire editor value on the first automerge update
const fromPos = codeMirror.posFromIndex(0)
const toPos = codeMirror.posFromIndex(codeMirror.getValue().length)
codeMirror.replaceRange(
getText(newDoc).join(''),
fromPos,
toPos,
'automerge'
)
return
}
// After the first automerge update we update the doc using a diff
const textObjectId = Automerge.getObjectId(getText(doc))
const diff = Automerge.diff(doc, newDoc)
for (const d of diff) {
if (d.obj !== textObjectId) continue
switch (d.action) {
case 'insert': {
const fromPos = codeMirror.posFromIndex(d.index)
codeMirror.replaceRange(d.value, fromPos, null, 'automerge')
break
}
case 'remove': {
const fromPos = codeMirror.posFromIndex(d.index)
const toPos = codeMirror.posFromIndex(d.index + 1)
codeMirror.replaceRange('', fromPos, toPos, 'automerge')
break
}
}
}
_onUpdate(docId, doc, prevDoc) {
if (this.id == docId) {
let diff = Automerge.diff(prevDoc, doc);
this.lastDiffs = diff.filter((d) => d.type === 'text');
this.doc = doc;
this.emit('updated', this);
}
}
updateWithRemoteChanges = (msg) => {
console.debug(`Client ${this.props.clientId} received message:`)
console.debug(msg)
const currentDoc = this.docSet.getDoc(this.props.docId)
const docNew = this.connection.receiveMsg(msg)
const opSetDiff = Automerge.diff(currentDoc, docNew)
if (opSetDiff.length !== 0) {
let change = this.state.value.change()
change = applyAutomergeOperations(opSetDiff, change, () => { this.updateSlateFromAutomerge() });
if (change) {
this.setState({ value: change.value })
}
}
}
it('convert remove operations', () => {
const doc1 = Automerge.change(createDoc(), 'change', d => {
d.document.nodes.push(createBlockJSON('paragraph', 'hello!'))
d.document.nodes.push(createBlockJSON('paragraph', 'hello twice!'))
d.document.nodes[1].nodes[0].text = 'hello!'
})
const doc2 = cloneDoc(doc1)
const change = Automerge.change(doc1, 'change', d => {
delete d.document.nodes[1]
delete d.document.nodes[0].nodes[0]
})
const operations = Automerge.diff(doc2, change)
const slateOps = toSlateOp(operations, change)
const expectedOps = [
{
type: 'remove_node',
path: [1],
node: {
object: 'text'
}
},
{
type: 'remove_node',
path: [0, 0],
node: {
object: 'text'
updateWithRemoteChanges = (msg) => {
console.debug(`Client ${this.clientId} received message:`)
console.debug(msg)
const currentDoc = this.docSet.getDoc(msg.docId)
const docNew = this.connection.receiveMsg(msg)
if (msg.docId !== this.state.docId) return
if (!currentDoc && docNew) {
const newValue = automergeJsonToSlate({"document": {...docNew.note}})
const value = Value.fromJSON(newValue)
this.setState({ value: value })
return
}
if (!currentDoc && !docNew) return
const opSetDiff = Automerge.diff(currentDoc, docNew)
if (opSetDiff.length !== 0) {
let change = this.state.value.change()
change = applyAutomergeOperations(opSetDiff, change, () => { this.updateSlateFromAutomerge() });
if (change) {
this.setState({ value: change.value })
}
}
}
recieveData = async (data: any) => {
if (this.docId !== data.docId || !this.connection) {
return
}
try {
const currentDoc = this.docSet.getDoc(this.docId)
const docNew = this.connection.receiveMsg(data)
if (!docNew) {
return
}
const operations = Automerge.diff(currentDoc, docNew)
if (operations.length !== 0) {
const slateOps = toSlateOp(operations, currentDoc)
this.editor.remote = true
this.editor.withoutSaving(() => {
slateOps.forEach(o => {
this.editor.applyOperation(o)
})
})
await Promise.resolve()
this.editor.remote = false
function updateCodeMirrorDocs(oldDoc, newDoc, links, mutex) {
var e_1, _a
if (mutex.locked || !oldDoc) {
return newDoc
}
var diffs = automerge_1.diff(oldDoc, newDoc)
try {
for (
var diffs_1 = __values(diffs), diffs_1_1 = diffs_1.next();
!diffs_1_1.done;
diffs_1_1 = diffs_1.next()
) {
var d = diffs_1_1.value
if (d.type !== 'text') continue
var link = findLink(newDoc, links, d)
if (!link) continue
var codeMirrorDoc = link.codeMirror.getDoc()
switch (d.action) {
case 'insert': {
var fromPos = codeMirrorDoc.posFromIndex(d.index)
codeMirrorDoc.replaceRange(d.value, fromPos, undefined, 'automerge')
break
export default function updateCodeMirrorDocs(
oldDoc: T,
newDoc: T,
links: Set>,
mutex: Mutex
): T {
if (mutex.locked || !oldDoc) {
return newDoc
}
const diffs = diff(oldDoc, newDoc)
for (const d of diffs) {
if (d.type !== 'text') continue
const link = findLink(newDoc, links, d)
if (!link) continue
const codeMirrorDoc = link.codeMirror.getDoc()
switch (d.action) {
case 'insert': {
const fromPos = codeMirrorDoc.posFromIndex(d.index!)
codeMirrorDoc.replaceRange(d.value, fromPos, undefined, 'automerge')
break
}
case 'remove': {
const fromPos = codeMirrorDoc.posFromIndex(d.index!)
const toPos = codeMirrorDoc.posFromIndex(d.index! + 1)