-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditor.js
More file actions
341 lines (308 loc) · 10.9 KB
/
editor.js
File metadata and controls
341 lines (308 loc) · 10.9 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/* global Log, Diff */
/* global CodeMirror */
/* global ace */
/*mock Log object */
var doNothing = function() {};
var Log = {
error: doNothing,
debug: doNothing
};
function Editor() {
var self = {
template: null
};
self._updateTaskHeight = function(h) {
$('#task_description').css({
'height': h + 'px'
});
};
self._updateEditorHeight = function(h) {
$('#solution').css({
minHeight: h - 15
});
$('#solution').css({
height: h - 15
});
};
self.updatePageLayout = function() {
var wh = $(window).height();
var hdr_h = $('#header').height();
var console_h = $('#console').height();
var buttons_h = $('#buttons').height();
var ver_h = $('#verification_results').height();
var eb_h = $('#editor_bar').height();
var editor_h = Math.max(250, wh - hdr_h - console_h - buttons_h - 70 - eb_h);
var task_h = Math.max(200, wh - hdr_h - ver_h - 50);
self._updateEditorHeight(editor_h);
self._updateTaskHeight(task_h);
Log.debug("candidate update page", "editor_h=" + editor_h + " wh=" + wh + " hdr_h=" +
hdr_h + " console_h=" + console_h + " buttons_h=" + buttons_h + ' ver_h=' + ver_h);
};
self.getValue = function() {
return $('#solution').val();
};
self.setValue = function(value) {
$('#solution').val(value);
};
self.setTemplate = function(template) {
self.template = template;
};
self.setPrgLang = function(prg_lang) {};
self.setEditable = function(editable) {};
self.clearHistory = function() {};
self.onCopyEvent = function(f) {};
self.onPasteEvent = function(f) {};
self.onChangeEvent = function(f) {
$('#solution').on('change', f);
};
return self;
}
function AceEditor() {
var self = Editor();
try {
$('#solution').after('<pre class="ace" style="display: none;"></pre>');
self.ace = ace.edit($('.ace').get(0));
$('#solution').hide();
$('.ace').show();
} catch (err) {
Log.error("Candidate interface", "Error setting up Ace", err);
window.alert(err);
// Fall back to normal textarea-based Editor
return self;
}
self.diffshower = ace.require('diffshower').DiffShower(self);
self.diffshower.enable(true);
self.Range = ace.require('ace/range').Range;
self.Anchor = ace.require('ace/anchor').Anchor;
self.HashHandler = ace.require("ace/keyboard/hash_handler").HashHandler;
var session = self.ace.getSession();
session.setTabSize(4);
session.setUseSoftTabs(true);
session.setUseWrapMode(true);
self._updateEditorHeight = function(h) {
$('.ace').css({
minHeight: h
});
$('.ace').css({
height: h
});
self.ace.resize();
};
self.clean = function() {
self.ace.replaceAll('-', {
needle: '\u2212'
}); // convert unicode minus to hyphen-minus (#1668)
};
self.getValue = function() {
self.clean();
return self.ace.getValue();
};
self.setValue = function(value) {
if(self.readOnlyRanges){
//detach document
$.each(self.readOnlyRanges, function(idx, value){
value.start.detach();
value.end.detach();
});
}
self.ace.getSession().setValue(value);
self.ace.clearSelection();
//self.highlightDiff();
//self.showDiff();
if (self.handleChange)
self.handleChange();
if(self.readOnlyRanges){
//atach document
var doc = self.ace.getSession().getDocument();
$.each(self.readOnlyRanges, function(idx, value){
value.start.attach(doc);
value.end.attach(doc);
});
}
};
self.prgLangToEditorMode = function(prg_lang) {
var lang_dict = {
'c': 'c_cpp',
'cpp': 'c_cpp',
'cs': 'csharp',
'java': 'java',
'js': 'javascript',
'lua': 'lua',
'm': 'objectivec',
'py': 'python',
'pas': 'pascal',
'php': 'php',
'pl': 'perl',
'rb': 'ruby',
'scala': 'scala',
'sql': 'sql',
'vb': 'vbscript'
};
return lang_dict[prg_lang] || 'plain_text';
};
self.setPrgLang = function(prg_lang) {
var mode = 'ace/mode/' + self.prgLangToEditorMode(prg_lang);
if (prg_lang == 'php') // Highlight PHP without <?php tag
self.ace.getSession().setMode({
path: mode,
inline: true
});
else
self.ace.getSession().setMode({
path: mode
});
};
self.setEditable = function(editable) {
self.ace.setReadOnly(!editable);
};
self.clearHistory = function() {
self.ace.getSession().setUndoManager(new ace.UndoManager());
};
self.onCopyEvent = function(f) {
self.ace.on('copy', f);
};
self.onPasteEvent = function(f) {
self.ace.on('paste', f);
};
self.onChangeEvent = function(f) {
self.handleChange = f;
self.ace.on('change', f);
};
self.diff = null;
self.highlightDiff = function() {
if (self.diff) {
$(self.diff.highlightChanged).each(function(i, line_no) {
self.ace.getSession().removeGutterDecoration(line_no, 'diff-changed');
});
$(self.diff.highlightRemoved).each(function(i, line_no) {
self.ace.getSession().removeGutterDecoration(line_no, 'diff-removed');
});
}
if (self.template === null)
self.diff = null;
else {
try {
self.diff = Diff.analyze(self.template, self.getValue());
} catch (err) {
Log.error('Error computing diff', err);
// fail gracefully
self.diff = null;
return;
}
$(self.diff.highlightChanged).each(function(i, line_no) {
self.ace.getSession().addGutterDecoration(line_no, 'diff-changed');
});
$(self.diff.highlightRemoved).each(function(i, line_no) {
self.ace.getSession().addGutterDecoration(line_no, 'diff-removed');
});
}
};
self.markers = [];
self.readOnlyRanges = [];
self.noNewLines = false;
self.setReadOnlyRegions = function(regions) {
//regions is a list of ranges
//if only the start position is specified, the range terminates at end-of-line
//this makes the assumpiton that the line is not empty
$.each(regions, function(index, value) {
if (value.start) {
var start = value.start;
var end = {};
if (!value.end) {
end.row = start.row;
end.column = self.ace.getSession().getDocument().getLine(start.row).length;
} else {
end = value.end;
}
var doc = self.ace.getSession().getDocument();
var start_anchor = new self.Anchor(doc, start.row, start.column);
start_anchor.setPosition(start.row, start.column, true);
var end_anchor = new self.Anchor(doc, end.row, end.column);
end_anchor.setPosition(end.row, end.column, true);
self.readOnlyRanges.push({"start": start_anchor, "end": end_anchor});
}
});
};
self.setNoNewLines = function(bool){
self.noNewLines = Boolean(bool);
if (self.noNewLines){
self.ace.keyBinding.addKeyboardHandler(self.keyHandlers.enterHandler);
}
else{
self.ace.keyBinding.removeKeyboardHandler(self.keyHandlers.enterHandler);
}
};
self.keyHandlers = {
enterHandler : new self.HashHandler([{// to assign a name to return key
name: "return",
bindKey: "Return|Shift-Return",
descr: "Always block Return",
exec: function(ed){
return false;
}
}])
};
self.enforceReadOnlyRegions = function(e) {
//check that ranges have been sent
var command = e.command;
var contains = false;
var multiLineSelect = false;
if (self.readOnlyRanges) {
$.each(self.readOnlyRanges, function(index, val){
contains = self.inRange(val);
if (contains){
return false;
}
});
}
if (self.noNewLines){
multiLineSelect = self.ace.getSession().getSelection().isMultiLine();
}
if (contains || multiLineSelect){
if (!command.readOnly){//commad.readOnly is true if the command doesn't cause a write
e.preventDefault();
e.stopPropagation();
}
}
else if(self.noNewLines){
var currentCursor = self.ace.getCursorPosition();
var rightEdge = currentCursor.column == self.ace.getSession().getLine(currentCursor.row).length;
var leftEdge = currentCursor.column === 0;
var stop = self.isPrevented(command)||self.isReturn(command) || (self.isDelete(command) && rightEdge) || (self.isBackspace(command) && leftEdge);
if (stop){
//console.log(command);
e.preventDefault();
e.stopPropagation();
}
}
};
self.inRange = function(range){ //check if the current range intersects with selection
var sel = self.ace.getSession().getSelection().getRange();
var start = range.start;
var end = range.end;
range = new self.Range(start.row, start.column, end.row, end.column);
return range.intersects(sel);
};
self.isDelete = function(command){
return command.name in {"del" : true, "removetolineend" : true, "removewordright": true};
};
self.isBackspace = function(command){
return command.name in {"backspace":true, "cut-or-delete":true, "removetolinestart":true, "removewordleft": true};
};
self.isReturn = function(command){
return command.name in {"return": true};
};
self.isPrevented = function(command){
return command.name in {"cut":true,
"removeline":true,
"duplicateSelection":true,
"copylinesup":true,
"copylinesdown": true,
"movelinesup": true,
"movelinesdown" : true,
"splitline" : true};
};
//self.ace.on('change', self.highlightDiff);
self.ace.commands.on("exec", self.enforceReadOnlyRegions);
return self;
}