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'
|
2019-10-12 05:02:37 +00:00
|
|
|
import selection from './selection'
|
2019-10-05 08:44:49 +00:00
|
|
|
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,
|
2019-10-12 05:02:37 +00:00
|
|
|
...selection
|
|
|
|
// set_value: setValue
|
2019-10-05 08:44:49 +00:00
|
|
|
}
|
|
|
|
|
2019-10-10 19:45:31 +00:00
|
|
|
export const applyOperation = meta => (
|
|
|
|
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
|
|
|
|
2019-10-10 19:45:31 +00:00
|
|
|
return applyOp(doc, op, meta)
|
2019-10-05 08:44:49 +00:00
|
|
|
} catch (e) {
|
|
|
|
console.error(e)
|
|
|
|
|
|
|
|
return doc
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-10 19:45:31 +00:00
|
|
|
export const applySlateOps = (doc: SyncDoc, operations: Operations, meta) =>
|
|
|
|
operations.reduce(applyOperation(meta), doc)
|