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

41 lines
852 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
}
2019-10-19 20:29:09 +00:00
const applyOperation = (doc: SyncDoc, op: Operation): SyncDoc => {
2019-10-05 08:44:49 +00:00
try {
const applyOp = opType[op.type]
2019-10-13 21:49:28 +00:00
if (!applyOp) {
console.log('operation', op.toJS())
throw new TypeError(`Unsupported operation type: ${op.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
}
}
2019-10-19 20:29:09 +00:00
const applySlateOps = (doc: SyncDoc, operations: Operations) =>
operations.reduce(applyOperation, doc)
2019-10-19 20:29:09 +00:00
export { applyOperation, applySlateOps }