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

65 lines
1.2 KiB

5 years ago
import { toSlatePath, toJS } from '../utils/index'
import { getTarget } from '../path'
5 years ago
const removeTextOp = ({ index, path }) => () => ({
type: 'remove_text',
path: toSlatePath(path).slice(0, path.length),
offset: index,
text: '*',
marks: []
})
const removeMarkOp = ({ path, index }) => (map, doc) => {
const slatePath = toSlatePath(path)
const target = getTarget(doc, slatePath)
return {
type: 'remove_mark',
path: slatePath,
mark: {
type: target.marks[index].type
}
}
}
5 years ago
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,
marks: removeMarkOp
5 years ago
}
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