简体   繁体   English

Codemirror编辑器未加载textarea的内容

[英]Codemirror editor is not loading content of the textarea

I'm trying to apply CodeMirror to this mini web app that I'm using. 我正在尝试将CodeMirror应用于我正在使用的这个迷你Web应用程序。 It has two has 2 textareas. 它有两个有2 textareas。 I'd like to add CM for better visibility, so I can edit some stuff on the get to go. 我想添加CM以获得更好的可见性,因此我可以编辑一些内容。 So far, I managed to apply the Eclipse theme, but the tool doesn't work anymore. 到目前为止,我设法应用了Eclipse主题,但该工具不再适用。 It seems like CodeMirror is not copying the content to the textarea. 似乎CodeMirror没有将内容复制到textarea。

When I remove the Codemirror js the tool works again. 当我删除Codemirror js该工具再次工作。

Here's my JSFiddle 这是我的JSFiddle

HTML HTML

<textarea id="input" rows="10" cols="80"></textarea>
<textarea id="output" rows="10" cols="80" readonly></textarea>

JS JS

$('textarea').each(function(index, elem) {
  CodeMirror.fromTextArea(elem, {
    lineWrapping: true,
    mode: "javascript",
    theme: "eclipse",
    lineNumbers: true,

  });

});

It seems to me that the problem is in http://dean.edwards.name/packer/bindings.js , exactly, at the following code: 在我看来,问题出在http://dean.edwards.name/packer/bindings.js ,确切地说,在以下代码中:

"#pack-script": {
    disabled: false,

    onclick: function() {
        try {
            output.value = "";
            if (input.value) {
                var value = packer.pack(input.value, base62.checked, shrink.checked);
                output.value = value;
                message.update();
            }
        } catch (error) {
            message.error("error packing script", error);
        } finally {
            saveScript.disabled = !output.value;
            decodeScript.disabled = !output.value || !base62.checked;
        }
    }
},

CodeMirror uses internal formatting, and applies custom styling to the textareas. CodeMirror使用内部格式,并将自定义样式应用于textareas。 So, the direct methods for the textarea, such as input.value won't work. 因此,textarea的直接方法,例如input.value将不起作用。 You will need to tweak it so that it uses CodeMirror's methods to get/set the values as described in this guide under Content manipulation methods section. 您需要对其进行调整,以便它使用CodeMirror的方法来获取/设置值,如本指南中内容操作方法”部分所述。

Edit 1: 编辑1:

Apart from correcting some syntax errors, I got that working in this fiddle . 除了纠正一些语法错误,我得到了这个小提琴

Changes done: 完成的更改:

  1. Returned the CodeMirror object from the editor function, so that it can be assigned to a variable. editor函数返回CodeMirror对象,以便可以将其分配给变量。
  2. Changed the onclick method. 更改了onclick方法。 In finally block, there are undefined references to saveScript , and decodeScript , which I commented. finally块中,有未定义的saveScript引用和decodeScript ,我评论过。 And used CodeMirror's getValue() , and setValue() methods to get/set values respectively. 并使用CodeMirror的getValue()setValue()方法分别获取/设置值。

There still are some errors in the console, if observed, but that doesn't hamper the functionality of packer. 如果观察到,控制台中仍然存在一些错误,但这不会妨碍打包器的功能。

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

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