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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With