CodeMirror test whether value has changed

Viewed 202

This question has a JSFiddle: jsfiddle.net/redmeat/L6p85fjc.

I'm attempting to make a server call when CodeMirror loses focus, but I first want to check if the value has changed.

CodeMirror provides an API for testing whether a value has changed, including whether the value was undone:

  • doc.changeGeneration(?closeEvent: boolean) → integer
  • doc.isClean(?generation: integer) → boolean

From the documentation: changeGeneration returns a number that can later be passed to isClean to test whether any edits were made (and not undone) in the meantime.

However if changes are reverted using the backspace or delete keys rather than undo and redo shortcuts, the form is considered not clean even if a change generation number is provided to isClean and the value is back where it started.

This JSFidlde shows that the values can match, but unless undo or redo shortcuts (ctrl+z and ctrl+y) are used, the form is considered dirty.

  1. Open the JSFiddle.
  2. Add a character to the end of foo.

Next, either:

  • Use the backspace key to remove the character.
  • OR
  • Use the Ctrl+z shortcut to remove the character.

In both scenarios the final value is the same as the original value. But it is only in the second scenario where isClean is true.

Question: Is there any way to check if the value is different? I naively expected isClean + changeGeneration to help here :/

0 Answers
Related