CodeMirror simple mode - Javascript CodeMirror

Javascript examples for CodeMirror:Configuration

Description

CodeMirror simple mode

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <title>Using CodeMirror mode</title> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript" src="https://codemirror.net/lib/codemirror.js"></script> 
      <link rel="stylesheet" type="text/css" href="https://codemirror.net/lib/codemirror.css"> 
      <style id="compiled-css" type="text/css">

.cm-style1 {//from   w  w  w.  j a va2s.  c om
   color: red;
}
.cm-style2 {
   color: blue;
}
.cm-style3 {
   color: purple;
}


      </style> 
   </head> 
   <body> 
      <textarea rows="4" cols="30" id="cm" name="cm">aaa bbb ccc</textarea> 
      <script type="text/javascript">
var keywords = ["timer", "counter", "version"];
CodeMirror.defineMode("mymode", function() {
  return {
    token: function(stream, state) {
      stream.eatWhile(/\w/);
      if (arrayContains(stream.current(), keywords)) {
        return "style1";
      }
      stream.next();
    }
  };
});
var editor = CodeMirror.fromTextArea(document.getElementById('cm'), {
  mode: "mymode",
  lineNumbers: true
});
function arrayContains(needle, arrhaystack) {
  var lower = needle.toLowerCase();
  return (arrhaystack.indexOf(lower) > -1);
}

      </script>  
   </body>
</html>

Related Tutorials