Get CodeMirror instance from wrapper element?

Is there any way to get a CodeMirror instance from its wrapper element?

And is there an internal instance index you can access?

Yes, the .CodeMirror property on the DOM node.

No. There is one, but it’s private.

I don’t see what you mean. There’s a CodeMirror CSS class but how do I get myCodeMirror (as you named it in CodeMirror.fromTextArea(myTextArea) from the DOM element?

document.querySelector(".CodeMirror").CodeMirror

Got it. Thanks.

Another question: I’m getting a CodeMirror instance from the original element (textarea, pre, whatever) using element.nextSibling then getting the instance as you explained above. Is there a cleaner more reliable way to do this?

Nope, nothing apart from saving the original value you got when you called fromTextarea somewhere.

Okay. The reason I’m doing this is the case where you’re adding my toolbars to a third party page where that value is buried in some gob of compressed code.

Thanks.

I take it that the above discussion (about .CodeMirror on a DOM node) is about CodeMirror 5, right? Is there an analogous way of getting hold of a CodeMirror instance in CodeMirror 6? (Thanks!)

For Codemirror 6 it is

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

but there is a limited ability to interact with this instance as you’ll need the interact with it using the same classes that made the CM6 instance (importing plugins and trying to apply them most likely won’t work)

1 Like