【问题标题】:CodeMirror - insert text into editor when there are multiple editorsCodeMirror - 当有多个编辑器时将文本插入编辑器
【发布时间】:2018-04-25 12:35:52
【问题描述】:

我在一页上有两个 codemirror 编辑器。用于定位正确编辑器的项目和单选组的下拉列表。

我要做的是在更改下拉列表时将项目的值插入目标编辑器(由单选组删除)。

我的代码如下:但是该功能不起作用。当我提醒项目值和目标时,我得到了预期的结果,但是插入文本的功能失败了:

<script type="text/javascript"> 
    function editor(id) {
            var editor = CodeMirror.fromTextArea(id, {
                continuousScanning: 500,
                lineNumbers: true
            });
            editor.setSize(null, 550);
        }
    var config_id = document.getElementById('id_config')
    var config = editor(config_id);
    var remote_config_id = document.getElementById('id_remote_config')
    var remote_config = editor(remote_config_id);

    function insertStringInTemplate(str, target)    {
        if (target== "id_config") {
            var doc = config
        } else {
            var doc = remote_config
        }
            var cursor = doc.getCursor();
            var pos = {
                line: cursor.line,
                ch: cursor.ch
            }
            doc.replaceRange(str, pos);
        }

    $(function(){
        // bind change event to select
        $('#template_vars').on('change', function () {
            var var_data = $(this).val(); // get selected value
            var var_target = $('input[name=target]:checked').val();
            insertStringInTemplate(var_data, var_target)
            return false;
        });
    });
    $("#template_vars").chosen({no_results_text: "Oops, nothing found!"}); 
</script>

【问题讨论】:

    标签: javascript jquery codemirror


    【解决方案1】:

    但是插入文本的功能失败了

    function(即insertStringInTemplate())运行良好/正常;但是,问题在于 editor() 函数,您忘记返回 editor(即 CodeMirror 实例)。

    所以一个简单的解决方法是:

        function editor(id) {
                var editor = CodeMirror.fromTextArea(id, {
                    continuousScanning: 500,
                    lineNumbers: true
                });
                editor.setSize(null, 550);
                return editor; // <- here's the fix
            }
    

    CodePen 上的演示。

    但是在那个演示中,我在insertStringInTemplate() 函数中添加了一个if 块,如下代码所示:

        function insertStringInTemplate(str, target)    {
            if (target== "id_config") {
                var doc = config
            } else {
                var doc = remote_config
            }
    
            // If there's a selection, replace the selection.
            if ( doc.somethingSelected() ) {
              doc.replaceSelection( str );
              return;
            }
    
            // Otherwise, we insert at the cursor position.
                var cursor = doc.getCursor();
                var pos = {
                    line: cursor.line,
                    ch: cursor.ch
                }
                doc.replaceRange(str, pos);
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-11
      • 1970-01-01
      相关资源
      最近更新 更多