Hi community,
I’m trying to compute delta between two Ysnapshots with the help of prosemiror demo & binding I was to be able to generate a first draft like this one:
const getDelta = (sharedType, prevSnapshot = Y.emptySnapshot) => {
if (sharedType.doc) {
const nextSnapshot = Y.snapshot(sharedType.doc)
if (Y.equalSnapshots(prevSnapshot, nextSnapshot)) {
return undefined
}
const computeTextChanges = (text: Y.Text) => text.toDelta(
nextSnapshot,
prevSnapshot,
(type, { client }) => ({ type, client })
)
const computeNodeChanges = (node: Y.Map<any>) => {
if (node.has('type')) {
console.info('Type', node.get('type').constructor.name)
const children = node.get('children')
if (children && children.constructor === Y.Array) {
const computedChildren = computeArrayChanges(children)
if (Object.keys(computedChildren).length > 0) {
return {
children: computedChildren,
// How to have attribute diff?
}
}
}
}
if (node.has('text')) {
const text = node.get('text')
if (text && text.constructor === Y.Text) {
const delta = computeTextChanges(text)
if (delta.length > 0) {
return delta
}
}
}
return {}
}
const computeArrayChanges = (array: Y.Array<any>) => {
const sparseTree = {}
Y.typeListToArraySnapshot(array, new Y.Snapshot(prevSnapshot.ds, nextSnapshot.sv)).forEach((elm, index) => {
const tmp = computeNodeChanges(elm)
if (Object.keys(tmp).length > 0) {
sparseTree[index] = tmp
}
})
return sparseTree
}
return computeArrayChanges(sharedType)
}
return undefined
}
I’m wondering how I could know that one element is new or removed in an array? I’ve seen some Y.isVisible from Snapshot (btw not exported) but not sure how I can get to know elements got removed or inserted.
Then it is nearly the same question on a Ymap how I get to know attribute got added/deleted/replaced?
Thanks in advance for your help