cudr_slate-collaborative/packages/bridge/src/apply/index.ts

36 lines
756 B
TypeScript
Raw Normal View History

2019-10-05 08:44:49 +00:00
import { Operation, Operations, SyncDoc } from '../model'
import node from './node'
import mark from './mark'
import text from './text'
import annotation from './annotation'
2019-10-12 05:02:37 +00:00
const setSelection = doc => doc
const setValue = doc => doc
2019-10-05 08:44:49 +00:00
const opType: any = {
...text,
...annotation,
...node,
...mark,
set_selection: setSelection
2019-10-12 05:02:37 +00:00
// set_value: setValue
2019-10-05 08:44:49 +00:00
}
export const applyOperation = (doc: SyncDoc, op: Operation): SyncDoc => {
2019-10-05 08:44:49 +00:00
try {
const applyOp = opType[op.type]
2019-10-12 05:02:37 +00:00
if (!applyOp) throw new TypeError('Unsupported operation type!')
2019-10-05 08:44:49 +00:00
return applyOp(doc, op)
2019-10-05 08:44:49 +00:00
} catch (e) {
console.error(e)
return doc
}
}
export const applySlateOps = (doc: SyncDoc, operations: Operations) =>
operations.reduce(applyOperation, doc)