I’m working with ProseMirror’s ChangeSet to track document changes across time.
I don’t just have an initial snapshot and a final document. Instead, I have:
An initial document snapshot D0
Multiple intermediate snapshots D1, D2, ..., Dn
A current document Dc
Each snapshot is associated with a revision id
What I want is one consolidated ChangeSet that represents all changes from D0 → Dc, with revision metadata preserved (so I can later render decorations or inspect which revision introduced which ranges).
The following approach does not work.
const base = ChangeSet.create(D0)
let current = base
for (let i = 1; i <= n; i++) {
const tr = new Transform(prevSnapshot).replace(
0,
prevSnapshot.content.size,
snapshots[i].slice(0, snapshots[i].content.size),
)
current = current.addSteps(
snapshots[i],
tr.mapping.maps,
[revisionIds[i]],
)
prevSnapshot = snapshots[i]
}
No. Change sets are intended to be built up with the actual steps produced by editing. Overwriting the entire document repeatedly will indeed not produce a useful change set.
Using ChangeSet, yes. It is probably possible to run some kind of custom diffing algorithm to find a set of ranges each snapshot changes, and then use those to do what you want to do it.
With the extensive help of Claude.ai I create a very comprehensive change tracking system, with multiple users and revision sets, the ability to turn tracking on and off, to clear revisions, to show all changes or just some, which works with additions, deletions, undo etc. Get hold of me if you want to see what I created, happy to share it with you if the changeset module is not what you need.
No, it is not a diff type plugin for multiple changed copies of a document, it is a change tracker that can be turned on or off, and can also support multiple revision sets and users (so sort of like revisions as Tip Tap mentions), although they would have to be editing the same document (and not at the same time). To be honest, it is a fairly complex plugin and there are still edge cases that are probably not handled. Work in progress. It keeps an array of changes (additions and deletions) and uses decorations to display them (or hide them) in the document as you write.
PM me if you want to take a look at it, understanding it is evolving as I spend time testing and finding new edge cases that need fixes. Still, pretty cool so far even if it might not be useful once I get Yjs or HocusPocus collaboration figured out.