feat: add test annotations

This commit is contained in:
cudr
2019-10-10 22:45:31 +03:00
parent ab3b8c4ee3
commit 2a4d64b074
10 changed files with 153 additions and 24 deletions

View File

@@ -1,14 +1,17 @@
import { Operation, SyncDoc } from '../model'
export const addAnnotation = (doc: SyncDoc, op: Operation): SyncDoc => {
console.log('addAnnotation!!!', op)
return doc
}
export const removeAnnotation = (doc: SyncDoc, op: Operation): SyncDoc => {
console.log('removeAnnotation!!!', op)
return doc
}
export const setAnnotation = (doc: SyncDoc, op: Operation): SyncDoc => {
console.log('setAnnotation!!!', op)
return doc
}

View File

@@ -5,7 +5,31 @@ import mark from './mark'
import text from './text'
import annotation from './annotation'
const setSelection = doc => doc
const setSelection = (doc, op, { id, selection }) => {
console.log('selection', selection.toJSON())
if (!doc.cursors) {
doc.cursors = {}
}
// console.log('setSelection', op.toJSON(), id)
const operation = op.toJSON()
if (!doc.cursors[id]) {
doc.cursors[id] = {
key: id,
type: 'collaborative_selection'
}
}
const cursor = doc.cursors[id]
const { focus, anchor } = operation.newProperties
if (focus) cursor.focus = focus
if (anchor) cursor.anchor = anchor
return doc
}
const setValue = (doc, op) => doc
const opType: any = {
@@ -18,13 +42,16 @@ const opType: any = {
set_value: setValue
}
export const applyOperation = (doc: SyncDoc, op: Operation): SyncDoc => {
export const applyOperation = meta => (
doc: SyncDoc,
op: Operation
): SyncDoc => {
try {
const applyOp = opType[op.type]
if (!applyOp) throw new TypeError('Invalid operation type!')
return applyOp(doc, op)
return applyOp(doc, op, meta)
} catch (e) {
console.error(e)
@@ -32,5 +59,5 @@ export const applyOperation = (doc: SyncDoc, op: Operation): SyncDoc => {
}
}
export const applySlateOps = (doc: SyncDoc, operations: Operations) =>
operations.reduce(applyOperation, doc)
export const applySlateOps = (doc: SyncDoc, operations: Operations, meta) =>
operations.reduce(applyOperation(meta), doc)

View File

@@ -15,18 +15,20 @@ const insertNodeOp = ({ value, obj, index, path }: Automerge.Diff) => map => {
const inserate = ({ nodes, ...json }: any, path) => {
const node = nodes ? { ...json, nodes: [] } : json
if (node.object === 'mark') {
ops.push({
type: 'add_mark',
path: path.slice(0, -1),
mark: node
})
} else {
ops.push({
type: 'insert_node',
path,
node
})
if (node.object) {
if (node.object === 'mark') {
ops.push({
type: 'add_mark',
path: path.slice(0, -1),
mark: node
})
} else {
ops.push({
type: 'insert_node',
path,
node
})
}
}
nodes && nodes.forEach((n, i) => inserate(n, [...path, i]))

View File

@@ -21,7 +21,7 @@ const opSet = (op: Automerge.Diff, [map, ops]) => {
if (set && path) {
ops.push(set(op))
} else {
} else if (map[obj]) {
map[obj][key] = link ? map[value] : value
}

View File

@@ -1,7 +1,14 @@
import toSync from './toSync'
import hexGen from './hexGen'
export const toJS = node => JSON.parse(JSON.stringify(node))
export const toJS = node => {
try {
return JSON.parse(JSON.stringify(node))
} catch (e) {
console.error(e)
return null
}
}
export const cloneNode = node => toSync(toJS(node))