简体   繁体   English

获取CodeMirror实例

[英]Get CodeMirror instance

I want to get an instance of CodeMirror (it is binded to a textarea '#code'). 我想获得CodeMirror的一个实例(它绑定到textarea'#code')。 From an onclick-event I want to add a value to the current value of the CodeMirror instance. 从onclick-event我想要为CodeMirror实例的当前值添加一个值。 How can this be achieved? 怎么能实现这一目标? From the docs I can't seem to find anything to get an instance and bind it to a loca var in javascript. 从文档中我似乎找不到任何东西来获取实例并将其绑定到javascript中的loca var。

Another method I have found elsewhere is as follows: 我在别处找到的另一种方法如下:

//Get a reference to the CodeMirror editor
var editor = document.querySelector('.CodeMirror').CodeMirror;

This works well when you are creating the CodeMirror instance dynamically or replacing an existing DOM element with a CodeMirror instance. 当您动态创建CodeMirror实例或用CodeMirror实例替换现有DOM元素时,这很有效。

Someone just posted an answer but removed it. 有人刚发布了一个答案,但删除了它。 Nevertheless, it was a working solution. 然而,这是一个有效的解决方案。 Thanks! 谢谢!

-- Basically this was his solution: - 基本上这是他的解决方案:

// create an instance
var editor = CodeMirror.fromTextArea('code');
// store it
$('#code').data('CodeMirrorInstance', editor);
// get it
var myInstance = $('code').data('CodeMirrorInstance');
// from here on the API functions are available to 'myInstance' again.

代码镜像编辑器对象上有一个getWrapperElement ,它为您提供代码镜像实例的根DOM元素:

var codemirrorDomElem = editor.getWrapperElement();

You can find the instance starting with the <textarea> and moving to the next sibling. 您可以找到以<textarea>开头并移动到下一个兄弟的实例。

Native 本地人

  • Functional 实用

     document.querySelector('#code').nextSibling, 
  • Selector 选择

     document.querySelector('#code + .CodeMirror'), 

jQuery jQuery的

  • Functional 实用

     $('#code').next('.CodeMirror').get(0), 
  • Selector 选择

     $('#code + .CodeMirror').get(0) 

Extra: A more advanced solution involving clipboard.js -> JSFiddle Demo 额外:一个更高级的解决方案涉及clipboard.js - > JSFiddle演示


Example

 // Selector for textarea var selector = '#code'; $(function() { var editor = CodeMirror.fromTextArea($(selector).get(0), { mode: 'javascript', theme: 'paraiso-dark', lineNumbers : true }); editor.setSize(320, 240); editor.getDoc().setValue(JSON.stringify(getSampleData(), null, 4)); $('#response').text(allEqual([ document.querySelector(selector).nextSibling, // Native - Functional document.querySelector(selector + ' + .CodeMirror'), // Native - Selector $(selector).next('.CodeMirror').get(0), // jQuery - Functional $(selector + ' + .CodeMirror').get(0) // jQuery - Selector ])); }); function allEqual(arr) { return arr.every(function(current, index, all) { return current === all[(index + 1) % all.length]; }); }; // Return sample JSON data. function getSampleData() { return [ { color: "red", value: "#f00" }, { color: "green", value: "#0f0" }, { color: "blue", value: "#00f" } ]; } 
 #response { font-weight: bold; } 
 <link href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.7.0/codemirror.min.css" rel="stylesheet"/> <link href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.7.0/theme/paraiso-dark.min.css" rel="stylesheet"/> <script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.7.0/codemirror.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div>All equal?: <span id="response"></span></div> <textarea rows="10" cols="60" id="code"></textarea> 

I am using with Vue CLI 3 , vue-codemirror component like this: 我正在使用Vue CLI 3vue-codemirror组件,如下所示:

<codemirror id="textarea_editor" v-model="textarea_input" :options="cm_editor_config"></codemirror>

import codemirror to use within page: 导入要在页面内使用的codemirror

import { codemirror } from 'vue-codemirror'
import 'codemirror/lib/codemirror.css'

and simply add codemirror inside components object, thereafter configuration in data section is: 并简单地在components对象中添加codemirror ,之后在data部分中的配置是:

codemirror_editor: undefined,
cm_editor_config: {
    tabSize: 4,
    mode: 'application/json',
    theme: 'base16-dark',
    lineNumbers: true,
    // lineWrapping: true,
    styleActiveSelected: true,
    line: true,
}

and initialized the object in mounted lifecycle section of Vue : 并初始化Vue mounted生命周期部分中的对象:

mounted () {
    if ( !this.codemirror_editor ) {
        this.codemirror_editor = $('#textarea_editor').find('.CodeMirror').get(0).CodeMirror;
    }
    // do something with this.codemirror_editor
}

Hope this would help many one. 希望这会对很多人有所帮助。

You can simply drop the var : instead of having 您可以简单地删除var :而不是

var editor = CodeMirror.fromTextArea...

Just have 只是有

editor = CodeMirror.fromTextArea...

Then editor is directly available to use in other functions 然后编辑器可以直接用于其他功能

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM