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)
|
||||
|
||||
Reference in New Issue
Block a user