javascript 获取 CodeMirror 实例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11581516/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 13:44:34  来源:igfitidea点击:

Get CodeMirror instance

javascriptcodemirror

提问by Ben Fransen

I want to get an instance of CodeMirror (it is binded to a textarea '#code'). From an onclick-event I want to add a value to the current value of the CodeMirror instance. 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.

我想获得一个 CodeMirror 的实例(它绑定到一个文本区域“#code”)。从 onclick 事件中,我想向 CodeMirror 实例的当前值添加一个值。如何做到这一点?从文档中,我似乎找不到任何东西来获取实例并将其绑定到 javascript 中的 loca var。

采纳答案by Ben Fransen

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.

回答by ashatte

Another method I have found elsewhereis 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 元素时,这很有效。

回答by Alireza Mirian

There is a getWrapperElementon code mirror editor objects which gives you the root DOM element of the code mirror instance:

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

var codemirrorDomElem = editor.getWrapperElement();

回答by Mr. Polywhirl

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'),
    
  • 功能性

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

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

jQuery

jQuery

  • Functional

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

    $('#code + .CodeMirror').get(0)
    
  • 功能性

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

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

Extra:A more advanced solution involving clipboard.js-> JSFiddle Demo

额外:一个更高级的解决方案,涉及到clipboard.js->JSFiddle Demo



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>

回答by ArifMustafa

I am using with Vue CLI 3, vue-codemirrorcomponent like this:

我正在使用 with Vue CLI 3,vue-codemirror组件是这样的:

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

import codemirrorto use within page:

导入codemirror以在页面内使用:

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

and simply add codemirrorinside componentsobject, thereafter configuration in datasection is:

并简单地添加codemirror内部components对象,然后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 mountedlifecycle section of Vue:

并在mounted生命周期部分初始化对象Vue

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.

希望这会帮助很多人。

回答by Enigma Plus

You can simply drop the var: instead of having

您可以简单地删除var: 而不是

var editor = CodeMirror.fromTextArea...

Just have

只要有

editor = CodeMirror.fromTextArea...

Then editoris directly available to use in other functions

然后editor就可以直接在其他功能中使用了