feat: try to use inner annotations model to cursors

This commit is contained in:
cudr
2019-10-12 20:58:23 +03:00
parent ad2780b936
commit 55def80703
11 changed files with 196 additions and 111 deletions

View File

@@ -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)

View File

@@ -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
}