I want to calculate the changed ranges for transactions (even with multiple steps).
I’m aware of findDiffStart
and findDiffEnd
but that’s not enough if a change something at the start and end of the document within one transaction.
let diffStart = oldState.doc.content.findDiffStart(newState.doc.content)
let diffEnd = oldState.doc.content.findDiffEnd(newState.doc.content)
With step.getMap()
I can get the changed ranges of each step but these positions depend on the current doc of the step (transaction.docs[stepIndex]
).
That’s why I thought I could map these positions forwards and backwards based on its index so that I get the correct positions for the oldState
and newState
of the whole transaction.
transaction.steps.forEach((step, index) => {
const upcomingMapping = transaction.mapping
.slice(index, transaction.mapping.maps.length)
const pastMapping = transaction.mapping
.slice(0, index)
.invert()
step.getMap().forEach((oldStart, oldEnd, newStart, newEnd) => {
const oldStart = pastMapping.map(oldStart, -1)
const oldEnd = pastMapping.map(oldEnd)
const newStart = upcomingMapping.map(oldStart, -1)
const newEnd = upcomingMapping.map(oldEnd)
})
})
That’s working fine for “normal” changes but breaks (out of range) if I undo changes with the history plugin. So I guess it’s not that easy to map the positions
Does anyone have any ideas what I can do differently or better?