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/convert/set.ts

40 lines
793 B

import * as Automerge from 'automerge'
import { toSlatePath, toJS } from '../utils'
const setDataOp = (
{ key = '', obj, path, value }: Automerge.Diff,
doc: any
) => (map: any) => {
return {
type: 'set_node',
path: toSlatePath(path),
properties: {
[key]: Automerge.getObjectById(doc, obj)?.[key]
},
newProperties: {
[key]: value
}
}
}
const opSet = (op: Automerge.Diff, [map, ops]: any, doc: any) => {
const { link, value, path, obj, key } = op
try {
if (path && path[0] !== 'cursors') {
ops.push(setDataOp(op, doc))
} else if (map[obj]) {
map[obj][key as any] = link ? map[value] : value
}
return [map, ops]
} catch (e) {
console.error(e, op, toJS(map))
return [map, ops]
}
}
export default opSet