Access Existing Instance

CodeMirror 6 is used in a web based IDE that I use regularly.

Is there some way to access the editor instance? In the past, I could use the ‘CodeMirror’ class to find the DOM element and go from there but using ‘cm-editor’ doesn’t behave the same way.

I can see that there is a ‘findFromDom’ method on the EditorView class, which sounds like it might do what I need, but I have no idea how to access that.

Could somebody perhaps point me in the right direction?

Many thanks

Looking through the source for findFromDom, you can do:

let cmEditorElement = document.querySelector(".cm-editor") // Or whatever query you need
let editorView = cmEditorElement.querySelector(".cm-content").cmView.view

You can update the document and selection with this,
but to interact with the plugins/facets/fields, you’ll need references to their javascript values…
(Of course there might be ways to get those too, but then it gets very hacky)
Hope this helps.

2 Likes

Excellent! Thank you!

Hm, there’s no findFromDom on the editorView instance that gets returned for me

Did that method change?

It’s a static method. If you already have the instance, you don’t need to find it. Also DOM is capitalized: findFromDOM.