Record/playback complete edit history.

I’d like to implement such a record/playback of complete edit history feature based on CM. This consists 2 parts: record and playback:

1 on record time, as user edits the text, save the operations into an object; in additional to regular edit operations like insert, delete, etc., there are custom, application-specific operations as well, save these as well;

2 later when the user wants to play back the whole edit history, he loops through the events, and performs them one by one, including both CM-built in events and custom events, so that the whole history is played back.

Is this possible with CM’s history API? I am new to CM, and although I have examined CM’s sample history JSON via getHistory(), I don’t quite understand what some of the fields mean yet.

Thanks a lot in advance!
GC

Not with the history API, but you can record the changes as reported by the "changes" event and replace those.

Thanks for your quick response.

So my understanding is that the history API has a granularity coarser than required by the record/playback feature – one can set the history as a whole, but can’t play back the events in the history from beginning to end, event by event, right?

Therefore, in order to implement record/playback, one could:

1 implement listeners for changes/change evets, and add them to an events object; also, when custom, application-specific events (like execute) happen, save them to the events object as well. I suppose CM can handle undo/redo in such a scenario;

2 during playback, loop through these events, and for each one of them:

  • if it’s a CM event like insert, call a CM API like doc.replaceRange to insert text;
  • if it’s a custom event like execute, call the application’s interface to do some execution stuff;

Of course, since this events is custom, one can add more info like time so that playback is synced up with record.

Any chance you could comment if the above proposed implementation is a viable one?

Thanks again!

Sounds like it should work, yes.

Fantastic. I will attempt such an implementation then. Thanks.

BTW, CodeMirror is such a cool piece of software. Kudos!