-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcs.js
56 lines (42 loc) · 1.39 KB
/
cs.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
const $ = require('jquery');
const ace = require('ace-builds');
require('ace-builds/webpack-resolver')
require('ace-builds/src-noconflict/theme-monokai')
const FeniaMode = require('./ace/mode-fenia').Mode;
var websock = require('./websock');
function fixindent(fn, str) {
var lines = str.replace(/\r/g,'').split('\n');
lines = lines.map(function(line) {
var parts = line.match(/^([ \t]*)(.*)$/);
return fn(parts[1]) + parts[2];
});
return lines.join('\n');
}
function tabsize8to4(str) {
return str.replace(/\t/g, ' ').replace(/ {4}/g, '\t');
}
function tabsize4to8(str) {
return str.replace(/\r/g,'').replace(/\t/g, ' ').replace(/ {8}/g, '\t');
}
$(document).ready(function() {
var editor = ace.edit($('#cs-modal .editor')[0], {
tabSize: 4
});
editor.setTheme('ace/theme/monokai');
editor.session.setMode(FeniaMode);
$('#cs-modal .run-button').click(function(e) {
var subj = $('#cs-subject').val(),
body = fixindent(tabsize4to8, editor.getValue());
e.preventDefault();
websock.rpccmd('cs_eval', subj, body);
});
$('#rpc-events').on('rpc-cs_edit', function(e, subj, body) {
if(subj) {
$('#cs-subject').val(subj);
}
if(body) {
editor.setValue(fixindent(tabsize8to4, body), -1);
}
$('#cs-modal').modal('show');
});
});