最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Change Codemirror mode on the fly through a select menu - Stack Overflow

programmeradmin0浏览0评论

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 wele

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 :

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

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

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

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 wele

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 :

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

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

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

Share Improve this question edited Nov 9, 2013 at 5:30 Rad asked Nov 9, 2013 at 2:04 RadRad 1,0293 gold badges15 silver badges33 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

Update I just remembered that my mode changer would never work unless I provided the mime type code mirror expects, not the mode name. i.e. pass it text/x-markdown and not markdown

I'm using the latest release of codemirror on my site http://pste.me.

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.

I'm not a codemirror expert but I believe the addition mode/autoload methods are no longer used. 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.

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

<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

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();
}
发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>