mirror of
https://github.com/cudr/slate-collaborative.git
synced 2026-03-02 03:40:18 +00:00
fix dependency update (#50)
* Always call external onChange. * Fix split_node missing properties bug. * fix: dependency update build Co-authored-by: Ulion <ulion2002@gmail.com>
This commit is contained in:
@@ -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
|
||||
|
||||
const slatePath = toSlatePath(path).slice(0, path?.length)
|
||||
|
||||
let node
|
||||
|
||||
try {
|
||||
node = getTarget(doc, slatePath) || map[obj]
|
||||
const { index, path, obj } = op
|
||||
|
||||
const slatePath = toSlatePath(path).slice(0, path?.length)
|
||||
|
||||
let node = map[obj]
|
||||
|
||||
try {
|
||||
node = getTarget(doc, slatePath)
|
||||
} catch (e) {
|
||||
console.error(e, slatePath, op, map, toJS(doc))
|
||||
}
|
||||
|
||||
if (typeof index !== 'number') return
|
||||
|
||||
const text = node?.text?.[index] || '*'
|
||||
|
||||
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: []
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e, op, doc)
|
||||
}
|
||||
|
||||
if (typeof index !== 'number') return
|
||||
|
||||
const text = node?.text[index] || '*'
|
||||
|
||||
if (node) {
|
||||
node.text = node.text?.slice(0, index) + node.text?.slice(index + 1)
|
||||
}
|
||||
|
||||
return {
|
||||
type: 'remove_text',
|
||||
path: slatePath,
|
||||
offset: index,
|
||||
text,
|
||||
marks: []
|
||||
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: [] }
|
||||
|
||||
return {
|
||||
type: 'remove_node',
|
||||
path: slatePath.length ? slatePath.concat(index) : [index],
|
||||
node: target
|
||||
if (!target) {
|
||||
throw new TypeError('Target is not found!')
|
||||
}
|
||||
|
||||
if (!map.hasOwnProperty(obj)) {
|
||||
map[obj] = 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
|
||||
|
||||
|
||||
@@ -10,10 +10,10 @@ const setDataOp = (
|
||||
type: 'set_node',
|
||||
path: toSlatePath(path),
|
||||
properties: {
|
||||
[key]: Automerge.getObjectById(doc, obj)?.[key]
|
||||
[key]: toJS(Automerge.getObjectById(doc, obj)?.[key])
|
||||
},
|
||||
newProperties: {
|
||||
[key]: value
|
||||
[key]: map?.[value] || value
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -22,7 +22,7 @@ const opSet = (op: Automerge.Diff, [map, ops]: any, doc: any) => {
|
||||
const { link, value, path, obj, key } = op
|
||||
|
||||
try {
|
||||
if (path && path[0] !== 'cursors') {
|
||||
if (path && path.length && path[0] !== 'cursors') {
|
||||
ops.push(setDataOp(op, doc))
|
||||
} else if (map[obj]) {
|
||||
map[obj][key as any] = link ? map[value] : value
|
||||
|
||||
Reference in New Issue
Block a user