feat: garbage cursors on client

This commit is contained in:
cudr
2019-10-27 15:43:12 +03:00
parent 6ec91963c6
commit 8d7a10be1b
5 changed files with 45 additions and 11 deletions

View File

@@ -63,14 +63,14 @@ class Connection {
return
}
const currentDoc = this.docSet.getDoc(this.docId)
const docNew = this.connection.receiveMsg(data)
if (!docNew) {
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) {
@@ -87,12 +87,42 @@ class Connection {
await Promise.resolve()
this.editor.remote = false
this.garbageCursors()
}
} catch (e) {
console.error(e)
}
}
garbageCursors = async () => {
const doc = this.docSet.getDoc(this.docId)
const { value } = this.editor
if (value.annotations.size === Object.keys(doc.annotations).length) {
return
}
const garbage = []
value.annotations.forEach(annotation => {
if (
annotation.type === this.cursorAnnotationType &&
!doc.annotations[annotation.key]
) {
garbage.push(annotation)
}
})
if (garbage.length) {
this.editor.withoutSaving(() => {
garbage.forEach(annotation => {
this.editor.removeAnnotation(annotation)
})
})
}
}
receiveSlateOps = (operations: Immutable.List<Operation>) => {
const doc = this.docSet.getDoc(this.docId)
const message = `change from ${this.socket.id}`