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/remove.ts

50 lines
947 B

import { toSlatePath, toJS } from '../utils/index'
const removeTextOp = ({ index, path }) => () => ({
type: 'remove_text',
path: toSlatePath(path).slice(0, path.length),
offset: index,
text: '*',
marks: []
})
const removeNodesOp = ({ index, path }) => () => {
const nPath = toSlatePath(path)
return {
type: 'remove_node',
path: nPath.length ? nPath.concat(index) : [index],
node: {
object: 'text'
}
}
}
const removeByType = {
text: removeTextOp,
nodes: removeNodesOp
}
const opRemove = (op, [map, ops]) => {
try {
const { index, path, obj } = op
if (map.hasOwnProperty(obj) && op.type !== 'text') {
map[obj].splice(index, 1)
return [map, ops]
}
if (!path) return [map, ops]
const fn = removeByType[path[path.length - 1]]
return [map, [...ops, fn(op)]]
} catch (e) {
console.error(e, op, toJS(map))
return [map, ops]
}
}
export default opRemove