Workspace.on('editor-change')

Vault.read reads the file’s content saved in the disk at the moment.
And this is not the same as the previous state of the editor.
It’s important to distinguish between the Editor interface and the Vault interface.

  • Editor: based on the CodeMirror editor engine. Can handle edit history
  • Vault: simply interacts (read & write) with the file system

If you are not familiar with CodeMirror, I strongly recommend reading their system guide.

https://codemirror.net/docs/guide/

This page from the Obsidian developer docs is also helpful for understanding how CodeMirror (and hence Obsidian) handles the edit history.


I’m not sure it’s a typical way, but you can define an editor extension (StateField or ViewPlugin) to access the previous state. Which one to choose depends on what you want to do.

StateField:

The second parameter transaction: Transaction of the update() method (CodeMirror Reference Manual) has both the current & previous EditorState:

  • transaction.startState: previous
  • transaction.state: current

ViewPlugin:

The only parameter update: ViewUpdate of the update() method (CodeMirror Reference Manual) has both the current & previous EditorState:

  • update.startState: previous
  • update.state: current

P.S. Let me know if anyone knows a more handy way!

1 Like