diff --git a/packages/backend/src/Connection.ts b/packages/backend/src/Connection.ts index 3b6b8e2..cd72149 100644 --- a/packages/backend/src/Connection.ts +++ b/packages/backend/src/Connection.ts @@ -124,6 +124,7 @@ class Connection { socket.leave(id) + this.garbageCursor(socket.nsp.name, id) 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 => { const doc = this.docSet.getDoc(nsp) diff --git a/packages/bridge/src/apply/selection.ts b/packages/bridge/src/apply/selection.ts index 82e2359..55283bd 100644 --- a/packages/bridge/src/apply/selection.ts +++ b/packages/bridge/src/apply/selection.ts @@ -21,7 +21,9 @@ const setSelection = (doc, op, { id, selection, annotationType }) => { if (focus) cursor.focus = focus 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) diff --git a/packages/client/src/Connection.ts b/packages/client/src/Connection.ts index 427efba..63b7496 100644 --- a/packages/client/src/Connection.ts +++ b/packages/client/src/Connection.ts @@ -115,9 +115,7 @@ class Connection { if (!doc) return - const selectionOps = operations.filter(op => op.type === 'set_selection') - - console.log('hasSelectionOps', selectionOps.size) + operations = operations.filter(op => op.type !== 'set_selection') const { value } = this.editor @@ -130,12 +128,24 @@ class Connection { } 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 = { type: 'set_selection', - properties: {}, + properties: cursor || {}, newProperties: { anchor: selection.start, focus: selection.end @@ -147,8 +157,6 @@ class Connection { operations = operations.push(op) } - console.log('operations', operations.toJSON()) - const changed = Automerge.change(doc, message, (d: any) => applySlateOps(d, operations, meta) )