mirror of
https://github.com/cudr/slate-collaborative.git
synced 2026-03-02 03:40:18 +00:00
feat: add test annotations
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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]))
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user