fully undo

Hello,

I try to build my first project with CodeMirror and at the moment it works on the first steps well. But I would like to add an undo function. In my structure CodeMirror save the data via Ajax call if the focus on the CM instance is lost. This works well, but the Ajax call can be failed so on this case CodeMirror should undo all changes after the last saved call.
In short

  1. CM is openend with source code (first undo checkpoint)
  2. user does some changes
  3. focus of the window is lost, CM data is saved by Ajax call, Ajax call success (second undo checkpoint)
  4. user add some new changes
  5. focus is lost, CM data will be saved with Ajax call, Ajax failes - undo changes until step 3

How can I create the CM undo calls? I need only to jump back to the previous checkpoint.
Also on this structure I would like to add also a Ajax call, which saves the data every line or every 5 seconds. How can I do this with CM and use also the undo checkpoint structure?

Thanks

Phil

On checkpoint, do

var savedUpto = cm.changeGeneration(true)

And then when rolling back:

while (!cm.isClean(savedUpto) && cm.historySize().undo > 0) cm.undo()

But there are some corner cases to take into account – if the user herself undoes past the save point, we’ll never reach it and the loop will just undo everything in the history. That’s bad, so you’d have to guard against that somehow (you could listen for "change" events and if their origin field is "undo" and they cause cm.isClean(savedUpto) to be true, disable rolling back to that save point). Also, if the history overflows (see the historyDepth option), you wouldn’t be able to undo the discarded changes anymore.

Thanks works well in my code.