Monaco-editor: Support dirty state?

Created on 8 Feb 2017  路  1Comment  路  Source: microsoft/monaco-editor

It would be nice to have access to the dirty state of the editor.

I see there is code for this in vscode but not in monaco, yet it is a useful feature.

The "monarch" page of the website approximates dirty state by just marking dirty = true when a change is made in the editor, but if changes are made then undone, there is no built-in way to have a correct dirty status.

Most helpful comment

There is model.getAlternativeVersionId():

Get the alternative version id of the model. This alternative version id is not always incremented, it will return the same values in the case of undo-redo.

So to implement a dirty indicator for example:

  • initialize lastSavedVersionId = model.getAlternativeVersionId()
  • when the user saves, save the file and record lastSavedVersionId = model.getAlternativeVersionId()
  • when the content changes (model.onDidChangeContent(...)), you can determine fileIsDirty = (lastSavedVersionId !== model.getAlternativeVersionId())

>All comments

There is model.getAlternativeVersionId():

Get the alternative version id of the model. This alternative version id is not always incremented, it will return the same values in the case of undo-redo.

So to implement a dirty indicator for example:

  • initialize lastSavedVersionId = model.getAlternativeVersionId()
  • when the user saves, save the file and record lastSavedVersionId = model.getAlternativeVersionId()
  • when the content changes (model.onDidChangeContent(...)), you can determine fileIsDirty = (lastSavedVersionId !== model.getAlternativeVersionId())
Was this page helpful?
0 / 5 - 0 ratings