简体   繁体   English

通过选择菜单即时更改 Codemirror 模式

[英]Change Codemirror mode on the fly through a select menu

I am trying to change a codemirror mode on the fly using the methode below, but it is unfortunately not working and your help is welcome我正在尝试使用下面的方法动态更改代码镜像模式,但不幸的是它不起作用,欢迎您的帮助

I have a select menu such :我有一个选择菜单,例如:

<select name="idLanguage" id="select" onChange="selectMode()">
<option value="1">Python</option>
<option value="10">JavaScript</option>
<option value="33">Asterisk dialplan</option>
<option value="34">Clojure</option>
<option value="35">Common Lisp</option>
<option value="36">D</option>
<option value="37">ECL</option>
<option value="38">Go</option>
<option value="39">Haskell</option>
<option value="40">HTML</option>
<option value="41">Jinja2</option>
<option value="42">LiveScript</option>
<option value="43">mIRC</option>
</select>

and then I use this javascript method :然后我使用这个javascript方法:

var modeInput = document.getElementById("select");
function selectMode() {
  var myindex  = modeInput.selectedIndex;
  var modefly = modeInput.options[myindex].text.toLowerCase();
  alert(modefly); // This is giving me the exact mode on the screen
  editor.setOption("mode", modefly);// no change in the mode
  CodeMirror.autoLoadMode(editor, modefly);//no change in the mode
  //editor.refresh();
   }

Although the alert() is giving the right answer, the mode is not changed尽管alert()给出了正确的答案,但模式没有改变

Any idea ?任何想法 ?

Thank you谢谢

UPDATE :更新 :

I am loading all the modes in the header (python, javascript etc ..) I changed the structure of codemirror assets, I have a single directory called assets that contain a folder js with all the javascripts including codemirror modes so I am thinking that this is no longuer valid我正在加载标头中的所有模式(python、javascript 等..)我更改了 codemirror 资产的结构,我有一个名为assets的目录,其中包含一个文件夹js ,其中包含所有 javascript,包括 codemirror 模式,所以我在想这个不再有效

CodeMirror.modeURL = "../mode/%N/%N.js";

how should I fix it ?我应该如何解决? with the configuration of folders I have right now, even the lazy mode example is not working使用我现在拥有的文件夹配置,即使是惰性模式示例也无法正常工作

Update I just remembered that my mode changer would never work unless I provided the mime type code mirror expects, not the mode name.更新我只记得我的模式更改器永远不会工作,除非我提供镜像期望的 mime 类型代码,而不是模式名称。 ie pass it text/x-markdown and not markdown即传递它text/x-markdown而不是markdown

I'm using the latest release of codemirror on my site http://pste.me .我在我的网站http://pste.me上使用了最新版本的 codemirror。

Via a select menu, the mode can be changed using:通过选择菜单,可以使用以下方式更改模式:

$('#mode').change(function(){
   editor.setOption("mode", $(this).val() );
});

Where editor is a reference to an CodeMirror.fromTextArea object.其中editor是对CodeMirror.fromTextArea对象的引用。

I'm not a codemirror expert but I believe the addition mode/autoload methods are no longer used.我不是 codemirror 专家,但我相信不再使用添加模式/自动加载方法。 I'm not having any problem with it auto-loading the needed files, but you could always dynamically build a new script tag and append it to the document head before setting the mode.我对它自动加载所需文件没有任何问题,但您始终可以在设置模式之前动态构建一个新的脚本标签并将其附加到文档head

That's the method we use for the editor themes:这就是我们用于编辑器主题的方法:

// Append the theme styles
var link = document.createElement('link');
link.onload = function(){
    pste.editor.setOption("theme", theme);
};
link.rel = "stylesheet";
link.media = "all";
link.href = INTERFACE_URL+"/codemirror/theme/"+theme+".css";
document.getElementsByTagName('head')[0].appendChild(link);

This solution is working for me, hope you find this useful.这个解决方案对我有用,希望你觉得这很有用。

In html file load these scripts在 html 文件中加载这些脚本

<script
  type="text/javascript"
  src="/node_modules/codemirror/lib/codemirror.js"
></script>
<script
  src="/node_modules/codemirror/addon/mode/loadmode.js"
  defer
></script>

And int your script.js并且 int 你的 script.js

CodeMirror.modeURL = "/node_modules/codemirror/mode/%N/%N.js";
let editor = CodeMirror.fromTextArea(document.querySelector("textarea"), {
  lineNumbers: true,
  value: "",
  matchBrackets: true,
  autoCloseBrackets: true,
});

var modeInput = document.getElementById("select");
function selectMode() {
  var myindex  = modeInput.selectedIndex;
  var modefly = modeInput.options[myindex].text.toLowerCase();
  alert(modefly); 
  //Make sure to use valid MIME type according to the selected input
  editor.setOption("mode", modefly);
  CodeMirror.autoLoadMode(editor, modefly);
  //editor.refresh();
}

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

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