mirror of
https://github.com/cudr/slate-collaborative.git
synced 2024-10-27 20:34:06 +00:00
feat: garbage cursors on client
This commit is contained in:
parent
6ec91963c6
commit
8d7a10be1b
@ -16,7 +16,7 @@ class Connection {
|
|||||||
private options: ConnectionOptions
|
private options: ConnectionOptions
|
||||||
|
|
||||||
constructor(options: ConnectionOptions = defaultOptions) {
|
constructor(options: ConnectionOptions = defaultOptions) {
|
||||||
this.io = io(options.port, options.connectOpts)
|
this.io = io(options.entry, options.connectOpts)
|
||||||
this.docSet = new Automerge.DocSet()
|
this.docSet = new Automerge.DocSet()
|
||||||
this.connections = {}
|
this.connections = {}
|
||||||
this.options = merge(defaultOptions, options)
|
this.options = merge(defaultOptions, options)
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
import { ValueJSON } from 'slate'
|
import { ValueJSON } from 'slate'
|
||||||
|
import { Server } from 'http'
|
||||||
|
|
||||||
export interface ConnectionOptions {
|
export interface ConnectionOptions {
|
||||||
port: number
|
entry: number | Server
|
||||||
connectOpts?: SocketIO.ServerOptions
|
connectOpts?: SocketIO.ServerOptions
|
||||||
defaultValue?: ValueJSON
|
defaultValue?: ValueJSON
|
||||||
saveTreshold?: number
|
saveTreshold?: number
|
||||||
|
@ -6,7 +6,7 @@ export const getClients = (io, nsp) =>
|
|||||||
})
|
})
|
||||||
|
|
||||||
export const defaultOptions = {
|
export const defaultOptions = {
|
||||||
port: 9000,
|
entry: 9000,
|
||||||
saveTreshold: 2000,
|
saveTreshold: 2000,
|
||||||
cursorAnnotationType: 'collaborative_selection'
|
cursorAnnotationType: 'collaborative_selection'
|
||||||
}
|
}
|
||||||
|
@ -63,14 +63,14 @@ class Connection {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const currentDoc = this.docSet.getDoc(this.docId)
|
|
||||||
const docNew = this.connection.receiveMsg(data)
|
|
||||||
|
|
||||||
if (!docNew) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
const currentDoc = this.docSet.getDoc(this.docId)
|
||||||
|
const docNew = this.connection.receiveMsg(data)
|
||||||
|
|
||||||
|
if (!docNew) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
const operations = Automerge.diff(currentDoc, docNew)
|
const operations = Automerge.diff(currentDoc, docNew)
|
||||||
|
|
||||||
if (operations.length !== 0) {
|
if (operations.length !== 0) {
|
||||||
@ -87,12 +87,42 @@ class Connection {
|
|||||||
await Promise.resolve()
|
await Promise.resolve()
|
||||||
|
|
||||||
this.editor.remote = false
|
this.editor.remote = false
|
||||||
|
|
||||||
|
this.garbageCursors()
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(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>) => {
|
receiveSlateOps = (operations: Immutable.List<Operation>) => {
|
||||||
const doc = this.docSet.getDoc(this.docId)
|
const doc = this.docSet.getDoc(this.docId)
|
||||||
const message = `change from ${this.socket.id}`
|
const message = `change from ${this.socket.id}`
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
const Connection = require('@slate-collaborative/backend')
|
const Connection = require('@slate-collaborative/backend')
|
||||||
const defaultValue = require('./src/defaultValue')
|
const defaultValue = require('./src/defaultValue')
|
||||||
|
const server = require('http').createServer()
|
||||||
|
|
||||||
const config = {
|
const config = {
|
||||||
port: 9000,
|
entry: server, // or specify port to start io server
|
||||||
defaultValue,
|
defaultValue,
|
||||||
saveTreshold: 2000,
|
saveTreshold: 2000,
|
||||||
onAuthRequest: async (query, socket) => {
|
onAuthRequest: async (query, socket) => {
|
||||||
@ -19,3 +20,5 @@ const config = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const connection = new Connection(config)
|
const connection = new Connection(config)
|
||||||
|
|
||||||
|
server.listen(9000)
|
||||||
|
Loading…
Reference in New Issue
Block a user