Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 :

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

like image 235
Rad Avatar asked Nov 09 '13 02:11

Rad


People also ask

How do I set up CodeMirror?

The easiest way to set up CodeMirror is via <script> tag from a CDN. You can also bundle the CodeMirror npm module, but for the sake of example let's just use a CDN. Just make sure you include both the JavaScript and CSS files, otherwise your editor won't look right.

How do I reset CodeMirror editor?

If you don't want to kill the CodeMirror instance, just change the entire document holding the text, data on markers etc. This can be done by calling cm. swapDoc(doc: CodeMirror. Doc) .

What is CodeMirror CSS?

CodeMirror is a code editor component for the web. It can be used in websites to implement a text input field with support for many editing features, and has a rich programming interface to allow further extension.

What is CodeMirror modeinfo in C++?

Defines CodeMirror.modeInfo, an array of objects with {name, mime, mode} properties, where name is the human-readable name, mime the MIME type, and mode the name of the mode file that defines this MIME.

How to change the look of the CodeMirror editor?

Up to a certain extent, CodeMirror's look can be changed by modifying style sheet files. The style sheets supplied by modes simply provide the colors for that mode, and can be adapted in a very straightforward way. To style the editor itself, it is possible to alter or override the styles defined in codemirror.css.

How do I use CodeMirror with the distribution?

The distribution comes with a number of modes (see the mode/ directory), and it isn't hard to write new ones for other languages. The easiest way to use CodeMirror is to simply load the script and style sheet found under lib/ in the distribution, plus a mode script from one of the mode/ directories.

How do I create a new document using CodeMirror?

You can create new documents by calling the CodeMirror.Doc (text: string, mode: Object, firstLineNumber: ?number, lineSeparator: ?string) constructor. The last three arguments are optional and can be used to set a mode for the document, make it start at a line number other than 0, and set a specific line separator respectively.


1 Answers

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);
like image 88
helion3 Avatar answered Oct 16 '22 10:10

helion3