Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to kill a CodeMirror instance?

I'm using Codemirror v3.16 and I'm trying to figure out how I can kill an instance of my codemirror? Basically codemirror fires when a textarea opens in a modal on my page. Closing this modal, I need to kill the instance, else when I reopen the modal, I get two textareas.

Can anyone help?

like image 312
Doidgey Avatar asked Sep 16 '13 12:09

Doidgey


People also ask

What is CodeMirror?

CodeMirror is a code editor component for the web. It can be used in websites to implement a text input field with support for many editing features, and has a rich programming interface to allow further extension. This is a CodeMirror field, configured for editing JavaScript code.

How do I use CodeMirror in textarea?

This could be used to, for example, replace a textarea with a real editor: var myCodeMirror = CodeMirror(function(elt) { myTextArea. parentNode. replaceChild(elt, myTextArea); }, {value: myTextArea.

How do you integrate CodeMirror?

Integrating with VuePut the <div> you will attach your CodeMirror to in your Vue template. Create the CodeMirror instance in the mounted lifecycle hook. Use $refs to get a reference to the <div> in the component's template. Listen for changes to your CodeMirror instance by listening to CodeMirror's 'changes' event.


2 Answers

When CodeMirror is removed from the DOM (and you kill all existing references to it that JavaScript might be holding), it'll be garbage collected. There is no explicit 'kill' method, you simply stop referring to it.

like image 23
Marijn Avatar answered Oct 07 '22 18:10

Marijn


If the CodeMirror instance was created with CodeMirror.fromTextArea, you can use its toTextArea method to copy the current contents to its "mirrored" text area and remove the instance.

Assuming your CM instance has the id of "CMEditor":

CM = document.getElementById('CMEditor');
CM.CodeMirror.toTextArea();

Alternately, you could instantiate the CM instance outside of the modal, and just hide and show it when the modal opens.

like image 160
Nick Tomlin Avatar answered Oct 07 '22 17:10

Nick Tomlin