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

52 lines
1.0 KiB

import { toSlatePath, toJS } from '../utils/index'
const insertTextOp = ({ index, path, value }) => () => ({
type: 'insert_text',
path: toSlatePath(path),
offset: index,
text: value,
marks: []
})
const insertNodeOp = ({ value, index, path }) => map => ({
type: 'insert_node',
path: [...toSlatePath(path), index],
node: map[value]
})
const insertByType = {
text: insertTextOp,
list: insertNodeOp
}
const opInsert = (op, [map, ops]) => {
try {
const { link, obj, path, index, type, value } = op
if (link && map[obj]) {
map[obj].splice(index, 0, map[value] || value)
} else if (type === 'text' && !path) {
map[obj] = map[obj]
? map[obj]
.slice(0, index)
.concat(value)
.concat(map[obj].slice(index))
: value
} else {
const insert = insertByType[type]
const operation = insert && insert(op, map)
ops.push(operation)
}
return [map, ops]
} catch (e) {
console.error(e, op, toJS(map))
return [map, ops]
}
}
export default opInsert