|
|
|
@ -5,52 +5,70 @@ import { toSlatePath, toJS } from '../utils'
|
|
|
|
|
import { getTarget } from '../path'
|
|
|
|
|
|
|
|
|
|
const removeTextOp = (op: Automerge.Diff) => (map: any, doc: Element) => {
|
|
|
|
|
const { index, path, obj } = op
|
|
|
|
|
try {
|
|
|
|
|
const { index, path, obj } = op
|
|
|
|
|
|
|
|
|
|
const slatePath = toSlatePath(path).slice(0, path?.length)
|
|
|
|
|
const slatePath = toSlatePath(path).slice(0, path?.length)
|
|
|
|
|
|
|
|
|
|
let node
|
|
|
|
|
let node = map[obj]
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
node = getTarget(doc, slatePath) || map[obj]
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error(e, op, doc)
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
node = getTarget(doc, slatePath)
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error(e, slatePath, op, map, toJS(doc))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (typeof index !== 'number') return
|
|
|
|
|
if (typeof index !== 'number') return
|
|
|
|
|
|
|
|
|
|
const text = node?.text[index] || '*'
|
|
|
|
|
const text = node?.text?.[index] || '*'
|
|
|
|
|
|
|
|
|
|
if (node) {
|
|
|
|
|
node.text = node.text?.slice(0, index) + node.text?.slice(index + 1)
|
|
|
|
|
}
|
|
|
|
|
if (node) {
|
|
|
|
|
node.text = node?.text ? (node.text.slice(0, index) + node.text.slice(index + 1)) : ''
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
type: 'remove_text',
|
|
|
|
|
path: slatePath,
|
|
|
|
|
offset: index,
|
|
|
|
|
text,
|
|
|
|
|
marks: []
|
|
|
|
|
return {
|
|
|
|
|
type: 'remove_text',
|
|
|
|
|
path: slatePath,
|
|
|
|
|
offset: index,
|
|
|
|
|
text,
|
|
|
|
|
marks: []
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error(e, op, map, toJS(doc))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const removeNodeOp = ({ index, obj, path }: Automerge.Diff) => (
|
|
|
|
|
const removeNodeOp = (op: Automerge.Diff) => (
|
|
|
|
|
map: any,
|
|
|
|
|
doc: Element
|
|
|
|
|
) => {
|
|
|
|
|
const slatePath = toSlatePath(path)
|
|
|
|
|
try {
|
|
|
|
|
const { index, obj, path } = op
|
|
|
|
|
|
|
|
|
|
const parent = getTarget(doc, slatePath)
|
|
|
|
|
const target = parent?.children[index as number] || { children: [] }
|
|
|
|
|
const slatePath = toSlatePath(path)
|
|
|
|
|
|
|
|
|
|
if (!map.hasOwnProperty(obj)) {
|
|
|
|
|
map[obj] = target
|
|
|
|
|
}
|
|
|
|
|
const parent = getTarget(doc, slatePath)
|
|
|
|
|
const target = parent?.children?.[index as number] || parent?.[index as number] || { children: [] }
|
|
|
|
|
|
|
|
|
|
if (!target) {
|
|
|
|
|
throw new TypeError('Target is not found!')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!map.hasOwnProperty(obj)) {
|
|
|
|
|
map[obj] = target
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
type: 'remove_node',
|
|
|
|
|
path: slatePath.length ? slatePath.concat(index) : [index],
|
|
|
|
|
node: target
|
|
|
|
|
if (!Number.isInteger(index)) {
|
|
|
|
|
throw new TypeError('Index is not a number')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
type: 'remove_node',
|
|
|
|
|
path: slatePath.length ? slatePath.concat(index) : [index],
|
|
|
|
|
node: target
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error(e, op, map, toJS(doc))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -61,7 +79,8 @@ const opRemove = (op: Automerge.Diff, [map, ops]: any) => {
|
|
|
|
|
if (
|
|
|
|
|
map.hasOwnProperty(obj) &&
|
|
|
|
|
typeof map[obj] !== 'string' &&
|
|
|
|
|
type !== 'text'
|
|
|
|
|
type !== 'text' &&
|
|
|
|
|
map?.obj?.length
|
|
|
|
|
) {
|
|
|
|
|
map[obj].splice(index, 1)
|
|
|
|
|
|
|
|
|
@ -72,7 +91,7 @@ const opRemove = (op: Automerge.Diff, [map, ops]: any) => {
|
|
|
|
|
|
|
|
|
|
const key = path[path.length - 1]
|
|
|
|
|
|
|
|
|
|
if (key === 'cursors') return [map, ops]
|
|
|
|
|
if (key === 'cursors' || op.key === 'cursors') return [map, ops]
|
|
|
|
|
|
|
|
|
|
const fn = key === 'text' ? removeTextOp : removeNodeOp
|
|
|
|
|
|
|
|
|
|