feat: success garbage cursor

This commit is contained in:
cudr 2019-10-12 12:19:06 +03:00
parent 4efc8db60e
commit ad2780b936
3 changed files with 34 additions and 9 deletions

View File

@ -124,6 +124,7 @@ class Connection {
socket.leave(id) socket.leave(id)
this.garbageCursor(socket.nsp.name, id)
this.garbageNsp() this.garbageNsp()
} }
@ -137,6 +138,20 @@ class Connection {
}) })
} }
garbageCursor = (nsp, id) => {
const doc = this.docSet.getDoc(nsp)
if (!doc.cursors) return
const change = Automerge.change(
doc,
`remove cursor ${id}`,
(d: any) => delete d.cursors[id]
)
this.docSet.setDoc(nsp, change)
}
removeDoc = async nsp => { removeDoc = async nsp => {
const doc = this.docSet.getDoc(nsp) const doc = this.docSet.getDoc(nsp)

View File

@ -21,7 +21,9 @@ const setSelection = (doc, op, { id, selection, annotationType }) => {
if (focus) cursor.focus = focus if (focus) cursor.focus = focus
if (anchor) cursor.anchor = anchor if (anchor) cursor.anchor = anchor
const cursorPath = (anchor && anchor.path) || (focus && focus.path) const cursorPath = cursor.data.isBackward
? anchor && anchor.path
: focus && focus.path
if (cursorPath) cursor.data.cursorPath = toJS(cursorPath) if (cursorPath) cursor.data.cursorPath = toJS(cursorPath)

View File

@ -115,9 +115,7 @@ class Connection {
if (!doc) return if (!doc) return
const selectionOps = operations.filter(op => op.type === 'set_selection') operations = operations.filter(op => op.type !== 'set_selection')
console.log('hasSelectionOps', selectionOps.size)
const { value } = this.editor const { value } = this.editor
@ -130,12 +128,24 @@ class Connection {
} }
const cursor = doc.cursors[meta.id] const cursor = doc.cursors[meta.id]
const cursorOffset = cursor && cursor.anchor && cursor.anchor.offset const cursorStart = cursor && cursor.anchor && cursor.anchor.offset
const cursorEnd = cursor && cursor.focus && cursor.focus.offset
if (!selectionOps.size && selection.start.offset !== cursorOffset) { console.log('cursor!!', cursorStart, cursorEnd)
console.log('selection!!', selection.start.offset, selection.end.offset)
if (
selection.start.offset !== cursorStart ||
selection.end.offset !== cursorEnd
) {
console.log(
'!!!!!! append selection op!!!',
selection.start.toJS(),
selection.end.toJS()
)
const opData = { const opData = {
type: 'set_selection', type: 'set_selection',
properties: {}, properties: cursor || {},
newProperties: { newProperties: {
anchor: selection.start, anchor: selection.start,
focus: selection.end focus: selection.end
@ -147,8 +157,6 @@ class Connection {
operations = operations.push(op) operations = operations.push(op)
} }
console.log('operations', operations.toJSON())
const changed = Automerge.change(doc, message, (d: any) => const changed = Automerge.change(doc, message, (d: any) =>
applySlateOps(d, operations, meta) applySlateOps(d, operations, meta)
) )