mirror of
https://github.com/cudr/slate-collaborative.git
synced 2026-03-02 03:40:18 +00:00
feat: try to use inner annotations model to cursors
This commit is contained in:
@@ -3,7 +3,6 @@ import { Operation, Operations, SyncDoc } from '../model'
|
||||
import node from './node'
|
||||
import mark from './mark'
|
||||
import text from './text'
|
||||
import selection from './selection'
|
||||
import annotation from './annotation'
|
||||
|
||||
const setSelection = doc => doc
|
||||
@@ -14,20 +13,17 @@ const opType: any = {
|
||||
...annotation,
|
||||
...node,
|
||||
...mark,
|
||||
...selection
|
||||
set_selection: setSelection
|
||||
// set_value: setValue
|
||||
}
|
||||
|
||||
export const applyOperation = meta => (
|
||||
doc: SyncDoc,
|
||||
op: Operation
|
||||
): SyncDoc => {
|
||||
export const applyOperation = (doc: SyncDoc, op: Operation): SyncDoc => {
|
||||
try {
|
||||
const applyOp = opType[op.type]
|
||||
|
||||
if (!applyOp) throw new TypeError('Unsupported operation type!')
|
||||
|
||||
return applyOp(doc, op, meta)
|
||||
return applyOp(doc, op)
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
|
||||
@@ -35,5 +31,5 @@ export const applyOperation = meta => (
|
||||
}
|
||||
}
|
||||
|
||||
export const applySlateOps = (doc: SyncDoc, operations: Operations, meta) =>
|
||||
operations.reduce(applyOperation(meta), doc)
|
||||
export const applySlateOps = (doc: SyncDoc, operations: Operations) =>
|
||||
operations.reduce(applyOperation, doc)
|
||||
|
||||
@@ -1,37 +0,0 @@
|
||||
import { toJS } from '../utils'
|
||||
|
||||
const setSelection = (doc, op, { id, selection, annotationType }) => {
|
||||
if (!doc.cursors) {
|
||||
doc.cursors = {}
|
||||
}
|
||||
|
||||
const operation = op.toJS()
|
||||
|
||||
if (!doc.cursors[id]) {
|
||||
doc.cursors[id] = {
|
||||
key: id,
|
||||
type: annotationType,
|
||||
data: {}
|
||||
}
|
||||
}
|
||||
|
||||
const cursor = doc.cursors[id]
|
||||
const { focus, anchor } = operation.newProperties
|
||||
|
||||
if (focus) cursor.focus = focus
|
||||
if (anchor) cursor.anchor = anchor
|
||||
|
||||
const cursorPath = cursor.data.isBackward
|
||||
? anchor && anchor.path
|
||||
: focus && focus.path
|
||||
|
||||
if (cursorPath) cursor.data.cursorPath = toJS(cursorPath)
|
||||
|
||||
cursor.data.isBackward = selection.isBackward
|
||||
|
||||
return doc
|
||||
}
|
||||
|
||||
export default {
|
||||
set_selection: setSelection
|
||||
}
|
||||
Reference in New Issue
Block a user