2019-10-06 20:56:27 +00:00
|
|
|
import * as Automerge from 'automerge'
|
2020-07-01 21:56:48 +00:00
|
|
|
import { Element } from 'slate'
|
2020-05-10 13:50:12 +00:00
|
|
|
|
|
|
|
import { toSlatePath, toJS } from '../utils'
|
2019-10-05 21:24:52 +00:00
|
|
|
import { getTarget } from '../path'
|
2021-02-07 04:55:23 +00:00
|
|
|
import { setDataOp } from './set'
|
2019-10-05 08:44:49 +00:00
|
|
|
|
2020-07-01 21:56:48 +00:00
|
|
|
const removeTextOp = (op: Automerge.Diff) => (map: any, doc: Element) => {
|
2021-01-18 16:56:00 +00:00
|
|
|
try {
|
|
|
|
const { index, path, obj } = op
|
2020-07-01 21:56:48 +00:00
|
|
|
|
2021-01-18 16:56:00 +00:00
|
|
|
const slatePath = toSlatePath(path).slice(0, path?.length)
|
2020-07-01 21:56:48 +00:00
|
|
|
|
2021-01-18 16:56:00 +00:00
|
|
|
let node = map[obj]
|
2020-07-01 21:56:48 +00:00
|
|
|
|
2021-01-18 16:56:00 +00:00
|
|
|
try {
|
|
|
|
node = getTarget(doc, slatePath)
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e, slatePath, op, map, toJS(doc))
|
|
|
|
}
|
2020-07-01 21:56:48 +00:00
|
|
|
|
2021-01-18 16:56:00 +00:00
|
|
|
if (typeof index !== 'number') return
|
2020-07-01 21:56:48 +00:00
|
|
|
|
2021-01-18 16:56:00 +00:00
|
|
|
const text = node?.text?.[index] || '*'
|
2020-07-01 21:56:48 +00:00
|
|
|
|
2021-01-18 16:56:00 +00:00
|
|
|
if (node) {
|
2021-02-04 07:16:35 +00:00
|
|
|
node.text = node?.text
|
|
|
|
? node.text.slice(0, index) + node.text.slice(index + 1)
|
|
|
|
: ''
|
2021-01-18 16:56:00 +00:00
|
|
|
}
|
2020-07-01 21:56:48 +00:00
|
|
|
|
2021-01-18 16:56:00 +00:00
|
|
|
return {
|
|
|
|
type: 'remove_text',
|
|
|
|
path: slatePath,
|
|
|
|
offset: index,
|
|
|
|
text,
|
|
|
|
marks: []
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e, op, map, toJS(doc))
|
2020-07-01 21:56:48 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-05 08:44:49 +00:00
|
|
|
|
2021-02-04 07:16:35 +00:00
|
|
|
const removeNodeOp = (op: Automerge.Diff) => (map: any, doc: Element) => {
|
2021-01-18 16:56:00 +00:00
|
|
|
try {
|
|
|
|
const { index, obj, path } = op
|
2019-10-06 10:30:35 +00:00
|
|
|
|
2021-01-18 16:56:00 +00:00
|
|
|
const slatePath = toSlatePath(path)
|
2020-07-01 21:56:48 +00:00
|
|
|
|
2021-01-18 16:56:00 +00:00
|
|
|
const parent = getTarget(doc, slatePath)
|
2021-02-04 07:16:35 +00:00
|
|
|
const target = parent?.children?.[index as number] ||
|
|
|
|
parent?.[index as number] || { children: [] }
|
2021-01-18 16:56:00 +00:00
|
|
|
|
|
|
|
if (!target) {
|
|
|
|
throw new TypeError('Target is not found!')
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!map.hasOwnProperty(obj)) {
|
|
|
|
map[obj] = target
|
|
|
|
}
|
2019-10-06 10:30:35 +00:00
|
|
|
|
2021-01-18 16:56:00 +00:00
|
|
|
if (!Number.isInteger(index)) {
|
|
|
|
throw new TypeError('Index is not a number')
|
|
|
|
}
|
|
|
|
|
2021-02-04 07:16:35 +00:00
|
|
|
if (parent?.children?.[index as number]) {
|
|
|
|
parent.children.splice(index, 1)
|
|
|
|
} else if (parent?.[index as number]) {
|
|
|
|
parent.splice(index, 1)
|
|
|
|
}
|
|
|
|
|
2021-01-18 16:56:00 +00:00
|
|
|
return {
|
|
|
|
type: 'remove_node',
|
|
|
|
path: slatePath.length ? slatePath.concat(index) : [index],
|
|
|
|
node: target
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e, op, map, toJS(doc))
|
2019-10-05 08:44:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-04 07:16:35 +00:00
|
|
|
const opRemove = (
|
|
|
|
op: Automerge.Diff,
|
|
|
|
[map, ops]: any,
|
|
|
|
doc: any,
|
|
|
|
tmpDoc: Element
|
|
|
|
) => {
|
2019-10-05 08:44:49 +00:00
|
|
|
try {
|
2020-05-10 13:50:12 +00:00
|
|
|
const { index, path, obj, type } = op
|
2019-10-05 08:44:49 +00:00
|
|
|
|
2021-02-07 04:55:23 +00:00
|
|
|
if (type === 'map' && path) {
|
|
|
|
// remove a key from map, mapping to slate set a key's value to undefined.
|
|
|
|
ops.push(setDataOp(op, doc)(map, tmpDoc))
|
|
|
|
return [map, ops]
|
|
|
|
}
|
|
|
|
|
2020-05-10 13:50:12 +00:00
|
|
|
if (
|
|
|
|
map.hasOwnProperty(obj) &&
|
|
|
|
typeof map[obj] !== 'string' &&
|
2021-01-18 16:56:00 +00:00
|
|
|
type !== 'text' &&
|
|
|
|
map?.obj?.length
|
2020-05-10 13:50:12 +00:00
|
|
|
) {
|
2019-10-05 08:44:49 +00:00
|
|
|
map[obj].splice(index, 1)
|
|
|
|
|
|
|
|
return [map, ops]
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!path) return [map, ops]
|
|
|
|
|
2020-05-10 13:50:12 +00:00
|
|
|
const key = path[path.length - 1]
|
|
|
|
|
2021-01-18 16:56:00 +00:00
|
|
|
if (key === 'cursors' || op.key === 'cursors') return [map, ops]
|
2020-05-10 13:50:12 +00:00
|
|
|
|
|
|
|
const fn = key === 'text' ? removeTextOp : removeNodeOp
|
2019-10-05 08:44:49 +00:00
|
|
|
|
2021-02-04 07:16:35 +00:00
|
|
|
return [map, [...ops, fn(op)(map, tmpDoc)]]
|
2019-10-05 08:44:49 +00:00
|
|
|
} catch (e) {
|
|
|
|
console.error(e, op, toJS(map))
|
|
|
|
|
|
|
|
return [map, ops]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default opRemove
|