简体   繁体   English

codemirror.setOption('mode',val) 不工作

[英]codemirror.setOption('mode',val) isnt working

I am trying to make a text editor with firepad and codemirror, everything works perfectly on page loading, but when I try to change the mode on button click, the function gets called but我正在尝试使用 firepad 和 codemirror 制作文本编辑器,页面加载时一切正常,但是当我尝试更改按钮单击模式时,function 被调用但是

codemirror.setOption('mode',val);//should change mode
CodeMirror.autoLoadMode(editor, mode);//should reload codemirror

doesn't seem to work.似乎不起作用。 I am calling the following code on the onload event of the body (works perfectly):我在主体的 onload 事件上调用以下代码(完美运行):

var firepadRef = getExampleRef();
codeMirror = CodeMirror(document.getElementById('firepad-container'), {
    lineNumbers: true,
    mode: 'javascript'
});
var firepad = Firepad.fromCodeMirror(firepadRef, codeMirror, {
    defaultText: '// Welcome'
});

function getExampleRef() {
  var ref = database.ref('Firepad');
  var hash = window.location.hash.replace(/#/g, '');
  if (hash) {
    ref = ref.child(hash);
  } else {
    ref = ref.push(); // generate unique location.
    window.location = window.location + '#' + ref.key;
  }
  if (typeof console !== 'undefined') {
    console.log('Firebase data: ', ref.toString());
  }
  return ref;
}

on button click event I am calling the following code (problem here:):在按钮单击事件上,我调用以下代码(这里有问题:):

console.log('Button Clicked');
CodeMirror.modeURL = "https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.40.0/mode/%N/%N.js";
var editor = CodeMirror(document.getElementById('firepad-container'), {
  lineNumbers: true
});
console.log("started changing");
var val ="xml",mode,spec;
mode=spec=val;
editor.setOption("mode", spec);//isn't changing mode!!
CodeMirror.autoLoadMode(editor, mode);//isn't reloading editor!!

This is just a miniaturized sample of the code (the relevant one).这只是代码(相关代码)的一个小型示例。

I am using as a resources for the mode reloading and for firebase with codemirror the following:我正在使用以下资源作为模式重新加载和 firebase 的代码镜像:

https://firepad.io/docs/ https://firepad.io/docs/

https://codemirror.net/demo/loadmode.html# https://codemirror.net/demo/loadmode.html#

Scripts and stylesheets being called are the following (the relevant ones only):被调用的脚本和样式表如下(仅相关的):

<link rel="stylesheet" href="codemirror-dark.css">
<link rel="stylesheet" href="https://cdn.firebase.com/libs/firepad/1.4.0/firepad.css">

<script src="https://www.gstatic.com/firebasejs/5.0.4/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/5.2.0/firebase-database.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.40.0/codemirror.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.40.0/addon/mode/loadmode.js"></script>
<script src="https://cdn.firebase.com/libs/firepad/1.4.0/firepad.min.js"></script>

I am using Firefox and there is no errors on the developper console(it is just neither changing nor reloading).我正在使用 Firefox 并且开发人员控制台上没有错误(它既没有更改也没有重新加载)。 So what am I missing??那我错过了什么?? Thanks in advance.提前致谢。

codemirror doesn't accept "xml" as a mode type. codemirror 不接受“xml”作为模式类型。 What you're looking for is "application/xml" or "text/xml".您正在寻找的是“application/xml”或“text/xml”。

according to the documentation on CodeMirrors site, you need to call codemirror.findModeByExtension on your filename string's extension to get an info object. You call CM_OBJ.setOption("mode",info.mime) first, then CodeMirror.autoLoadMode(editor,info.mode) if you want to use simpler and less specific typing strings.根据 CodeMirrors 站点上的文档,您需要在文件名字符串的扩展名上调用 codemirror.findModeByExtension 以获取信息 object。您首先调用 CM_OBJ.setOption("mode",info.mime),然后调用 CodeMirror.autoLoadMode(editor,info .mode) 如果你想使用更简单和更不具体的输入字符串。

this works for me on firefox and chrome from the inspect page.这对我适用于 firefox 和检查页面上的 chrome。 source: https://codemirror.net/demo/loadmode.html来源: https://codemirror.net/demo/loadmode.html

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

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