-
Notifications
You must be signed in to change notification settings - Fork 28
/
code-mirror-editor.js
146 lines (125 loc) · 4.24 KB
/
code-mirror-editor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
var React = require("react");
var CodeMirror = require('codemirror');
require('codemirror/mode/javascript/javascript');
require('codemirror/addon/fold/foldcode');
import CodeMirrorHighlight from './code-mirror-highlight';
var IS_MOBILE = (
navigator.userAgent.match(/Android/i)
|| navigator.userAgent.match(/webOS/i)
|| navigator.userAgent.match(/iPhone/i)
|| navigator.userAgent.match(/iPad/i)
|| navigator.userAgent.match(/iPod/i)
|| navigator.userAgent.match(/BlackBerry/i)
|| navigator.userAgent.match(/Windows Phone/i)
);
var OPEN_MARK = /{{{/;
var CLOSE_MARK = /}}}/;
CodeMirror.registerGlobalHelper('fold', 'marked',
function(mode, mirror) {
return mode.name === 'javascript';
},
function(mirror, start) {
var lineNo = start.line;
var lineText = mirror.getLine(lineNo);
var lineCount = mirror.lineCount();
var openMatch = OPEN_MARK.exec(lineText);
var closeMatch = CLOSE_MARK.exec(lineText);
if (openMatch) {
// search forwards
for (var i = lineNo; i < lineCount; i++) {
closeMatch = CLOSE_MARK.exec(mirror.getLine(i));
if (closeMatch) {
return {
from: CodeMirror.Pos(lineNo, openMatch.index),
to: CodeMirror.Pos(i, closeMatch.index + 3)
};
}
}
} else if (closeMatch) {
// search backwards
for (var i = lineNo; i >= 0; i--) {
openMatch = OPEN_MARK.exec(mirror.getLine(i));
if (openMatch) {
return {
from: CodeMirror.Pos(i, openMatch.index),
to: CodeMirror.Pos(lineNo, closeMatch.index + 3)
};
}
}
}
}
);
var CodeMirrorEditor = React.createClass({
getDefaultProps() {
return {
renderType: IS_MOBILE ? 'pre' : 'textarea',
}
},
componentDidMount: function() {
if (this.props.renderType === 'textarea') {
this.instantiateTextarea();
}
},
instantiateTextarea() {
this.editor = CodeMirror.fromTextArea(this.refs.editor, {
mode: 'javascript',
lineNumbers: false,
lineWrapping: true,
smartIndent: false, // javascript mode does bad things with jsx indents
matchBrackets: true,
theme: 'solarized-light',
readOnly: this.props.readOnly
});
this.editor.foldCode(0, { widget: '...' });
this.editor.on('change', this.handleChange);
this.editor.on('beforeSelectionChange', (instance, obj) => {
// why is ranges plural?
var selection = obj.ranges ?
obj.ranges[0] :
obj;
var noRange = selection.anchor.ch === selection.head.ch &&
selection.anchor.line === selection.head.line;
if (!noRange) {
return;
}
var cursor = selection.anchor;
var line = instance.getLine(cursor.line);
var match = OPEN_MARK.exec(line) || CLOSE_MARK.exec(line);
// the opening or closing mark appears on this line
if (match &&
// and the cursor is on it
// (this is buggy if both occur on the same line)
cursor.ch >= match.index &&
cursor.ch < match.index + 3) {
// TODO(joel) - figure out why this doesn't fold although it
// seems like it should work.
instance.foldCode(cursor, { widget: '...' });
}
});
},
componentDidUpdate: function() {
if (this.props.readOnly) {
this.editor.setValue(this.props.codeText);
}
},
handleChange: function() {
if (!this.props.readOnly && this.props.onChange) {
this.props.onChange(this.editor.getValue());
}
},
render: function() {
// wrap in a div to fully contain CodeMirror
var editor;
if (this.props.renderType === 'pre') {
editor = <CodeMirrorHighlight codeText={this.props.codeText} />;
} else {
editor = <textarea ref="editor" defaultValue={this.props.codeText} />;
}
return (
<div style={this.props.style} className={this.props.className}>
{editor}
</div>
);
}
});
module.exports = CodeMirrorEditor;