feat: cursors should works with annotations

This commit is contained in:
cudr
2019-10-13 19:37:56 +03:00
parent 242a836ce8
commit 0ceb38bbfd
19 changed files with 214 additions and 186 deletions

View File

@@ -14,7 +14,7 @@ const byAction = {
const rootKey = '00000000-0000-0000-0000-000000000000'
const toSlateOp = (ops: Automerge.Diff[], currentTree) => {
const toSlateOp = (ops: Automerge.Diff[], doc) => {
const iterate = (acc, op) => {
const action = byAction[op.action]
@@ -30,9 +30,7 @@ const toSlateOp = (ops: Automerge.Diff[], currentTree) => {
[]
])
const res = defer.flatMap(op => op(tempTree, currentTree))
console.log('toSlate@@@', ops, res)
const res = defer.flatMap(op => op(tempTree, doc)).filter(op => op)
return res
}

View File

@@ -41,9 +41,13 @@ 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 annotation = toJS(doc.annotations[key])
if (annotation) {
return {
type: 'remove_annotation',
annotation
}
}
}

View File

@@ -33,8 +33,6 @@ const AnnotationSetOp = ({ key, value }: Automerge.Diff) => (map, doc) => {
// }
// }
console.log('opSET!!', key, map[value], op)
return op
}

View File

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

View File

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

View File

@@ -1,3 +1,3 @@
import { Doc } from 'automerge'
export type CursorKey = string
export type SyncDoc = Doc<any>
export type SyncDoc = any

View File

@@ -5,7 +5,7 @@ export const toJS = node => {
try {
return JSON.parse(JSON.stringify(node))
} catch (e) {
console.error(e)
console.error('Convert to js failed!!! Return null')
return null
}
}