-
Notifications
You must be signed in to change notification settings - Fork 5
/
jquery.codemirror.js
36 lines (31 loc) · 1.4 KB
/
jquery.codemirror.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
(function($){$.fn.codemirror = function(options) {
var result = this;
var settings = $.extend( {
'mode' : 'javascript',
'lineNumbers' : false,
'runmode' : false
}, options);
if (settings.runmode) this.each(function() {
var obj = $(this);
var accum = [], gutter = [], size = 0;
var callback = function(string, style) {
if (string == "\n") {
accum.push("<br>");
gutter.push('<pre>'+(++size)+'</pre>');
}
else if (style) {
accum.push("<span class=\"cm-" + CodeMirror.htmlEscape(style) + "\">" + CodeMirror.htmlEscape(string) + "</span>");
}
else {
accum.push(CodeMirror.htmlEscape(string));
}
}
CodeMirror.runMode(obj.val(), settings.mode, callback);
$('<div class="CodeMirror">'+(settings.lineNumbers?('<div class="CodeMirror-gutter"><div class="CodeMirror-gutter-text">'+gutter.join('')+'</div></div>'):'<!--gutter-->')+'<div class="CodeMirror-lines">'+(settings.lineNumbers?'<div style="position: relative; margin-left: '+size.toString().length+'em;">':'<div>')+'<pre class="cm-s-default">'+accum.join('')+'</pre></div></div></div>').insertAfter(obj);
obj.hide();
});
else this.each(function() {
result = CodeMirror.fromTextArea(this, settings);
});
return result;
};})( jQuery );