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
}

View File

@@ -30,7 +30,11 @@ const toSlateOp = (ops: Automerge.Diff[], currentTree) => {
[]
])
return defer.flatMap(op => op(tempTree, currentTree))
const res = defer.flatMap(op => op(tempTree, currentTree))
console.log('toSlate@@@', ops, res)
return res
}
export { toSlateOp }

View File

@@ -52,7 +52,7 @@ const opInsert = (op: Automerge.Diff, [map, ops]) => {
if (link && map[obj]) {
map[obj].splice(index, 0, map[value] || value)
} else if (type === 'text' && !path) {
} else if ((type === 'text' || type === 'list') && !path) {
map[obj] = map[obj]
? map[obj]
.slice(0, index)

View File

@@ -40,10 +40,18 @@ const removeNodesOp = ({ index, obj, path }: Automerge.Diff) => (map, doc) => {
}
}
const removeAnnotationOp = ({ key }: Automerge.Diff) => (map, doc) => {
return {
type: 'remove_annotation',
annotation: toJS(doc.annotations[key])
}
}
const removeByType = {
text: removeTextOp,
nodes: removeNodesOp,
marks: removeMarkOp
marks: removeMarkOp,
annotations: removeAnnotationOp
}
const opRemove = (op: Automerge.Diff, [map, ops]) => {

View File

@@ -10,6 +10,31 @@ const setDataOp = ({ path, value }: Automerge.Diff) => map => ({
}
})
const AnnotationSetOp = ({ key, value }: Automerge.Diff) => (map, doc) => {
if (!doc.annotations) {
doc.annotations = {}
}
let op
if (!doc.annotations[key]) {
op = {
type: 'add_annotation',
annotation: map[value]
}
} else {
op = {
type: 'set_annotation',
properties: doc.annotations[key],
newProperties: map[value]
}
}
console.log('opSET!!', key, map[value], op)
return op
}
const setByType = {
data: setDataOp
}
@@ -25,6 +50,13 @@ const opSet = (op: Automerge.Diff, [map, ops]) => {
map[obj][key] = link ? map[value] : value
}
/**
* Annotation
*/
if (path && path.length === 1 && path[0] === 'annotations') {
ops.push(AnnotationSetOp(op))
}
return [map, ops]
} catch (e) {
console.error(e, op, toJS(map))

View File

@@ -0,0 +1,88 @@
import { toJS } from '../utils'
import { Operation } from 'slate'
import { List } from 'immutable'
export const setCursor = (
doc,
{ id, selection, selectionOps, annotationType }
) => {
if (!doc.annotations) {
doc.annotations = {}
}
if (!doc.annotations[id]) {
doc.annotations[id] = {
key: id,
type: annotationType,
data: {}
}
}
const annotation = toJS(doc.annotations[id])
if (selectionOps.size) {
selectionOps.forEach(op => {
const { newProperties } = op.toJSON()
if (newProperties.focus) annotation.focus = newProperties.focus
if (newProperties.anchor) annotation.anchor = newProperties.anchor
if (newProperties.data) annotation.data = newProperties.data
})
}
const cursorStart = annotation.anchor && annotation.anchor.offset
const cursorEnd = annotation.focus && annotation.focus.offset
console.log('cursor!!', cursorStart, cursorEnd)
console.log(
'selection!!',
annotation,
selection.start.offset,
selection.end.offset
)
if (selection.start.offset !== cursorStart) {
annotation.focus = selection.end.toJS() || {}
}
if (selection.end.offset !== cursorEnd) {
annotation.anchor = selection.start.toJS() || {}
}
annotation.data.isBackward = selection.isBackward
console.log('setted cursor', annotation, toJS(doc))
doc.annotations[id] = annotation
return doc
}
export const removeCursor = (doc, { id }) => {
console.log('!!!removeCursor', doc, id)
if (doc.annotations && doc.annotations[id]) {
delete doc.annotations[id]
}
return doc
}
export const cursorOpFilter = (doc, ops: List<Operation>) =>
ops.filter(op => {
const { annotations } = doc
if (op.type === 'set_annotation') {
return !(
(op.properties && annotations[op.properties.key]) ||
(op.newProperties && annotations[op.newProperties.key])
)
} else if (
op.type === 'add_annotation' ||
op.type === 'remove_annotation'
) {
return !annotations[op.annotation.key]
}
return true
})

View File

@@ -1,3 +1,4 @@
export * from './apply'
export * from './convert'
export * from './utils'
export * from './cursor'