You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cudr_slate-collaborative/packages/bridge/src/apply/index.ts

38 lines
764 B

import { Operation } from 'slate'
import node from './node'
import text from './text'
import { SyncValue } from '../model'
import { toJS } from '../utils'
const setSelection = (doc: any) => doc
const opType = {
...text,
...node,
set_selection: setSelection
}
const applyOperation = (doc: SyncValue, op: Operation): SyncValue => {
try {
const applyOp = opType[op.type]
if (!applyOp) {
throw new TypeError(`Unsupported operation type: ${op.type}!`)
}
return applyOp(doc, op as any)
} catch (e) {
console.error(e, op, toJS(doc))
return doc
}
}
const applySlateOps = (doc: SyncValue, operations: Operation[]): SyncValue => {
return operations.reduce(applyOperation, doc)
}
export { applyOperation, applySlateOps }