@@ -5,6 +5,35 @@ import CodeMirror from '../../../index.js';
5
5
let jBeautify = require ( 'js-beautify' ) . js ;
6
6
let hBeautify = require ( 'js-beautify' ) . html ;
7
7
8
+ // http://marijnhaverbeke.nl/blog/codemirror-mode-system.html
9
+ let sampleMode = ( ) => {
10
+ return {
11
+ startState : function ( ) {
12
+ return { inString : false } ;
13
+ } ,
14
+ token : function ( stream , state ) {
15
+ // If a string starts here
16
+ if ( ! state . inString && stream . peek ( ) == '"' ) {
17
+ stream . next ( ) ; // Skip quote
18
+ state . inString = true ; // Update state
19
+ }
20
+
21
+ if ( state . inString ) {
22
+ if ( stream . skipTo ( '"' ) ) { // Quote found on this line
23
+ stream . next ( ) ; // Skip quote
24
+ state . inString = false ; // Clear flag
25
+ } else {
26
+ stream . skipToEnd ( ) ; // Rest of line is string
27
+ }
28
+ return "string" ; // Token style
29
+ } else {
30
+ stream . skipTo ( '"' ) || stream . skipToEnd ( ) ;
31
+ return null ; // Unstyled token
32
+ }
33
+ }
34
+ } ;
35
+ }
36
+
8
37
class Editor extends React . Component {
9
38
10
39
constructor ( props ) {
@@ -15,13 +44,30 @@ class Editor extends React.Component {
15
44
16
45
let exampleJS = 'function StringStream(string) { this.pos = 0; this.string = string; } StringStream.prototype = { done: function() {return this.pos >= this.string.length;}, peek: function() {return this.string.charAt(this.pos);}, next: function() { if (this.pos < this.string.length) return this.string.charAt(this.pos++); }, eat: function(match) { var ch = this.string.charAt(this.pos); if (typeof match == "string") var ok = ch == match; else var ok = ch && match.test ? match.test(ch) : match(ch); if (ok) {this.pos++; return ch;} }, eatWhile: function(match) { var start = this.pos; while (this.eat(match)); if (this.pos > start) return this.string.slice(start, this.pos); }, backUp: function(n) {this.pos -= n;}, column: function() {return this.pos;}, eatSpace: function() { var start = this.pos; while (/s/.test(this.string.charAt(this.pos))) this.pos++; return this.pos - start; }, match: function(pattern, consume, caseInsensitive) { if (typeof pattern == "string") { function cased(str) {return caseInsensitive ? str.toLowerCase() : str;} if (cased(this.string).indexOf(cased(pattern), this.pos) == this.pos) { if (consume !== false) this.pos += str.length; return true; } } else { var match = this.string.slice(this.pos).match(pattern); if (match && consume !== false) this.pos += match[0].length; return match; } } };' ;
17
46
this . defaultJS = jBeautify ( exampleJS , { indent_size : 2 } ) ;
47
+
48
+ this . exampleCustomModeStrings = 'only "double quotes" will be tokenized\n\nsee http://marijnhaverbeke.nl/blog/codemirror-mode-system.html'
18
49
}
19
50
20
51
render ( ) {
21
52
53
+ let value = '' ;
54
+
55
+ switch ( this . props . mode ) {
56
+ case 'xml' :
57
+ value = this . defaultHTML ;
58
+ break ;
59
+ case 'javascript' :
60
+ value = this . defaultJS ;
61
+ break ;
62
+ case 'strings' :
63
+ value = this . exampleCustomModeStrings ;
64
+ break ;
65
+ }
66
+
22
67
return (
23
68
< CodeMirror
24
- value = { this . props . mode === 'xml' ? this . defaultHTML : this . defaultJS }
69
+ value = { value }
70
+ defineMode = { { name : 'strings' , fn : sampleMode } }
25
71
options = { {
26
72
mode : this . props . mode ,
27
73
theme : this . props . theme ,
0 commit comments