Is dirty flag available in codemirror?

We are using code mirror version 5.49.2. Does code mirror have an api or a flag to check if the code in the editor is dirty or in other words, if the user has edited the code in the editor? I am not looking at the ‘change’ event listener, which I understand will launch everytime there is a change, while what i am looking at is, if there is an api which can be called when required, to find out if the code is dirty.
Thanks in advance.

Look at the changeGeneration and isClean methods.

Thanks for the reply. Will check

@marijn is there something similar in v6?

No, but documents are structure-sharing persistent values, and comparing them is optimized to make use of this, so if you just want to check whether a the current document is the same as some previous one, keep a reference to that previous state.doc, and compare with cleanDoc.eq(state.doc) later.

2 Likes

Thanks for your reply. I just want to be able to decide whether to save changes on blur event (via rest API)

Will cleanDoc.eq(state.doc) return true if all changes were undid since the editor was instantiated? If not, how can I check whether the document is clean in this sense?

Yes, eq on Text instances will be very fast when the documents share structure (aren’t created separately, but produced by modifying a shared root document), and will tell you whether a given document matches your reference document.