CodingEditer.js
22.5 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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
;(function(window){
var _role = getQuery('role');
var CodingEditer = {
records: [], // 存储录入的操作
recording_status: false, // 当前是否在录制 true: 正在录制,false: 没有录制
// isWeb: 0, // 是否是前端 1:是,0:否
triggertotal: 0, // 已播放总数
speed: 1, // 播放速度
isPause: true, // 是否暂停 true:不暂停,flase:暂停
recordtimeoutcontrolls: [],
isRcording: false,
firstPlay: 1, // 第一次播放
pauseValue: [],
isRecordPage: 1,
flag: true, // 解决多次点击
init: function(){
console.log('CodingEditer');
},
webResult: function(html, css, js){
$('#ifrcontainer').html($('<iframe />', { 'id': 'resultiframe', 'frameBorder': '0', 'style': 'width:100%', 'z-index': '-1000' }));
console.log(js);
var iframe = $('#resultiframe')[0], doc;
if (iframe.contentDocument) {
doc = iframe.contentDocument;
} else if (iframe.contentWindow) {
doc = iframe.contentWindow.document;
} else {
doc = iframe.document;
}
/* 判断用户输入HTML片段还是完成的HTML文档 */
// var completedflag = 0,
// lowhtml = html.toLowerCase();
// if (lowhtml.toLowerCase().indexOf('</body>') >= 0 && lowhtml.toLowerCase().indexOf('<body>') >= 0) {
// completedflag = 1;
// }
var _css = "<style>" + css + "</style>";
var _script = '<script type="text/javascript">' + js + "</script>";
var result = '<!DOCTYPE HTML><html><head>' + _css + _script + '</head><body>' + html + '</body></html>';
// result = result + _script;
doc.open();
doc.writeln(result);
doc.close();
},
initCodeMirror: function(langid){
var treeLinkEditor = Dir.treeLinkEditor;
var langid = getQuery('languageid') || Dir.languageid;
initProgramPage(treeLinkEditor, langid);
},
/**
* 获取各个文件中的代码
* Created by Kira on 2017-02-08
*/
getCodeMirrorValue: function(){
var pauseValue = [];
var treeLinkEditor = Dir.treeLinkEditor;
for (var i = 0; i < treeLinkEditor.length; i++) {
// Dir.showEdite({'treeNode': {'name': treeLinkEditor[i].file}});
pauseValue.push(treeLinkEditor[i].CodeMirrorRecord.getValue());
}
console.log(pauseValue);
return pauseValue;
},
getPlayCodeMirrorValue: function(){
CodingEditer.pauseValue = [];
var treeLinkEditor = Dir.treeLinkEditor;
for (var i = 0; i < treeLinkEditor.length; i++) {
console.log(treeLinkEditor[i].CodeMirrorReplay.getValue())
// ZTREE.showEdite({'treeNode': {'name': treeLinkEditor[i].file}});
CodingEditer.pauseValue.push(treeLinkEditor[i].CodeMirrorReplay.getValue());
}
},
setCodeMirrorValue: function(){
CodingEditer.resetCodeMirrorValue(1);
var codeLength = CodingEditer.pauseValue;
var treeLinkEditor = Dir.treeLinkEditor;
console.log(treeLinkEditor);
for (var i = 0; i < treeLinkEditor.length; i++) {
Dir.showEdite({'treeNode': {'name': treeLinkEditor[i].file}});
treeLinkEditor[i].CodeMirrorReplay.setValue(codeLength[i]);
}
},
/**
* 播放代码
* Created by Kira on 2017-02-08
*/
playCoding: function(){
CodingEditer.editable(1);
CodingEditer.flag = false;
if (CodingEditer.firstPlay == 0) {
CodingEditer.setCodeMirrorValue();
Dir.showEdite({'treeNode': CodingEditer.treeSelectNode});
}else{
CodingEditer.resetCodeMirrorValue(1);
// alert('new Iterm()------' + CodingEditer.firstPlay)
// var iterm = new Iterm();
// alert('iterm ============' + CodingEditer.firstPlay)
// iterm.compileResult()
}
// CodingEditer.resetCodeMirrorValue(1);
// var iterm = new Iterm();
// iterm.compileResult()
var playbackrecord = CodingEditer.records;
var starttime = 0;
var playTotalTime = 0;
$('#recordertab a:eq(1)').tab('show');
for (var i = 0; i < playbackrecord.length; i++) {
if (playbackrecord[i].trigger === 1) {
playTotalTime += parseInt(playbackrecord[i].intervaltime);
}else if (playbackrecord[i].trigger === 0) {
var timeoutcontroller = null;
var replayinterval = playbackrecord[i].intervaltime;
if (!CodingEditer.isPause) {
starttime = replayinterval - (CodingEditer.audioTime - playTotalTime);
CodingEditer.isPause = true;
}else{
// if (i == 0) {
// starttime = replayinterval;
// }else{
// starttime = parseInt(starttime) + parseInt(replayinterval);
// }
starttime = parseInt(starttime) + parseInt(replayinterval);
}
timeoutcontroller = setTimeout(changeValueCallback(playbackrecord[i]), starttime/CodingEditer.speed);
CodingEditer.recordtimeoutcontrolls.push(timeoutcontroller);
}
}
console.log(CodingEditer.recordtimeoutcontrolls)
},
pauseCode: function(){
CodingEditer.getPlayCodeMirrorValue();
console.log('---------------------------------');
console.log(CodingEditer.pauseValue)
CodingEditer.isPause = 0;
CodingEditer.firstPlay = 0;
CodingEditer.editable(0);
CodingEditer.treeSelectNode = Dir.treeObj.getSelectedNodes()[0];
var recordtimeoutcontrolls = CodingEditer.recordtimeoutcontrolls;
for (var i = 0; i < recordtimeoutcontrolls.length; i++) {
clearTimeout(recordtimeoutcontrolls[i]);
}
CodingEditer.recordtimeoutcontrolls = [];
CodingEditer.recordtimeouts = [];
},
/**
* 记录动作
* Created by Kira on 2017-02-08
*/
addRecord: function(treeNode, check_tree_time){
console.log(check_tree_time)
console.log(CodingEditer.record_startime)
var recordobj = {
intervaltime: check_tree_time - CodingEditer.record_startime,
editor: 'tab',
changeobj: {
from: {
ch: 0,
line: 0,
},
text: '',
to: {
ch: 0,
line: 0,
}
},
treefile: treeNode,
trigger: 0,
playTime: 0
};
CodingEditer.records.push(recordobj);
console.log(CodingEditer.records);
CodingEditer.record_startime = check_tree_time;
},
initRecodesValue: function(){
var treeLinkEditor = Dir.treeLinkEditor;
CodingEditer.resetCodeMirrorValue();
CodingEditer.record_startime = new Date().getTime(); // 设置开始录制的时间
CodingEditer.recording_status = true;
CodingEditer.records = [];
console.log(CodingEditer.records);
for (var i = treeLinkEditor.length-1; i >= 0; i--) {
console.log(treeLinkEditor[i].CodeMirrorRecord.getValue());
var recordobj = {
intervaltime: 0,
editor: treeLinkEditor[i].name,
changeobj: {
from: {
ch: 0,
line: 0,
},
text: [treeLinkEditor[i].CodeMirrorRecord.getValue()],
to: {
ch: 0,
line: 0,
}
},
trigger: 0,
playTime: 0
};
CodingEditer.records.push(recordobj);
}
console.log(CodingEditer.records);
},
resetCodeMirrorValue: function(num) {
var _this = this;
var _thisNum = num;
var treeLinkEditor = Dir.treeLinkEditor;
if (_thisNum == 0) {
for (var i = 0; i < treeLinkEditor.length; i++) {
var _path = treeLinkEditor[i].filePath;
console.log(treeLinkEditor)
Dir.showEdite({'treeNode': {'name': treeLinkEditor[i].file}});
treeLinkEditor[i].CodeMirrorRecord.setValue("");
}
} else if (_thisNum == 1) {
for (var i = 0; i < treeLinkEditor.length; i++) {
if (treeLinkEditor[i].CodeMirrorReplay.getValue()) {
Dir.showEdite({'treeNode': {'name': treeLinkEditor[i].file}, 'replay': true});
treeLinkEditor[i].CodeMirrorReplay.setValue("");
}
}
} else {
console.log('------------------------------------------------')
console.log(treeLinkEditor)
console.log('------------------------------------------------')
for (var i = 0; i < treeLinkEditor.length; i++) {
if(treeLinkEditor[i].CodeMirrorRecord){
Dir.showEdite({'treeNode': {'name': treeLinkEditor[i].file}});
treeLinkEditor[i].CodeMirrorRecord.setValue('');
}else if(treeLinkEditor[i].CodeMirrorReplay.getValue()){
Dir.showEdite({'treeNode': {'name': treeLinkEditor[i].file}, 'replay': true});
treeLinkEditor[i].CodeMirrorReplay.setValue("");
}
}
}
Dir.showEdite({'treeNode': {'name': treeLinkEditor[0].file}});
},
playEnd: function(){
Hourglass.stopTimer()
for (var i = 0; i < CodingEditer.records.length; i++) {
CodingEditer.records[i].trigger = 0;
}
},
/*
* 修改代码状态
*/
editable: function(status){
// status: 1 禁止修改,0 允许修改
var treeLinkEditor = Dir.treeLinkEditor;
console.log(status + '##########################')
if (status == 0 || status == 1) {
for (var i = 0; i < treeLinkEditor.length; i++) {
if(!Dir.isReplayPage){
treeLinkEditor[i].CodeMirrorRecord.setOption('readOnly', status);
}
treeLinkEditor[i].CodeMirrorReplay.setOption('readOnly', status);
}
}else{
console.log('status undefind');
}
}
}
// 初始化编程页面
function initProgramPage(treeLinkEditor, langid){
console.log(treeLinkEditor)
var modeName = getMode(langid);
console.log(modeName)
// 生成 CodeMirror
for (var i = 0; i < treeLinkEditor.length; i++) {
var modeNameItem = modeName[i] || modeName[0];
// 判断是否是播放页面,如果是则不显示录制区相关dom
if(!Dir.isReplayPage){
treeLinkEditor[i].CodeMirrorRecord = CodeMirror.fromTextArea($('#' + treeLinkEditor[i].record)[0], {
value: '',
mode: modeNameItem,
styleActiveLine: true,
lineNumbers: true,
smartIndent: false,
// theme: "erlang-dark",
onChange: function(em, changeobj) {
console.log('----------em---------')
console.log(em.getWrapperElement().getAttribute('id'))
console.log(Dir.getCurrentEditor(em.getWrapperElement().getAttribute('id')))
var _pauseValue = CodingEditer.getCodeMirrorValue();
if (CodingEditer.recording_status) {
buildRecords(Dir.getCurrentEditor(em.getWrapperElement().getAttribute('id')), changeobj);
}
if (CodingEditer.isWeb) {
console.log(_pauseValue[2])
// CodingEditer.webResult(_pauseValue[0], _pauseValue[1], _pauseValue[2])
}
},
onFocus: function(em) {
},
onCursorActivity: function(em) {
}
});
treeLinkEditor[i].CodeMirrorRecordId = treeLinkEditor[i].record + 'CodeMirror';
$('#' + treeLinkEditor[i].record).next().attr('id', treeLinkEditor[i].CodeMirrorRecordId).hide();
}
treeLinkEditor[i].CodeMirrorReplay = CodeMirror.fromTextArea($('#' + treeLinkEditor[i].replay)[0], {
value: '',
mode: modeNameItem,
lineNumbers: true,
smartIndent: false,
onChange: function(em, changeobj) {
},
onFocus: function(em) {
},
onCursorActivity: function(em) {
}
});
treeLinkEditor[i].CodeMirrorReplayId = treeLinkEditor[i].replay + 'CodeMirror';
$('#' + treeLinkEditor[i].replay).next().attr('id', treeLinkEditor[i].CodeMirrorReplayId).hide();
}
// 默认显示第一个文件夹的第一个文件
Dir.showEdite();
}
function getMode(langid){
var langid = parseInt(langid);
var mode = [];
switch (langid) {
case 4:
mode = ["text/x-python"];
break;
case 10:
mode = ["text/x-java"];
break;
case 11:
mode = ["text/x-csrc"];
break;
case 29:
mode = ["application/x-httpd-php"];
break;
case 35:
mode = ["text/html", "text/css", "text/javascript"];
break;
case 40:
mode = ["text/x-sql"];
break;
case 43:
mode = ["text/x-objectivec"];
break;
case 82:
mode = ["text/x-c++src"];
break;
case 117:
mode = ["text/x-rsrc"];
break;
default:
break;
}
return mode;
}
/**
* 组合录入代码
* by Kira 2017-3-9
*/
function buildRecords(editor, changeobj){
var intervaltime,
currenttime = new Date().getTime();
var _role = getQuery('role');
// 根据不同的身份设置录制代码的时间间隔,当学生端输入的时候,重置间隔为100毫秒
if (_role == 0) {
intervaltime = currenttime - CodingEditer.record_startime;
}else if (_role == 1){
intervaltime = 100;
}else{
intervaltime = 0;
}
// intervaltime = currenttime - CodingEditer.record_startime;
var recordobj = {
intervaltime: intervaltime,
editor: editor,
changeobj: changeobj,
trigger: 0,
playTime: 0,
currenttime: currenttime
};
CodingEditer.records.push(recordobj); // 添加每次输入对象
console.log(CodingEditer.records)
CodingEditer.record_startime = currenttime; //设定起始时间
}
function changeValueCallback(record, replay_type, offset) {
var obj = record.changeobj,
editor = record.editor,
treeLinkEditor = Dir.treeLinkEditor;
return function() {
// 播放
for (var i = 0; i < treeLinkEditor.length; i++) {
if(editor == treeLinkEditor[i].name){ // 播放代码
processChangeObject(treeLinkEditor[i].CodeMirrorReplay, obj);
}else if(record.treefile){ // 切换tree目录
Dir.showEdite({'treeNode': record.treefile, 'replay': true});
}
}
record.trigger = 1; //标记已运行
record.playTime = new Date().getTime(); // 播放开始时间
if (record.code == 0 || record.code == 1) {
// var iterm = new Iterm();
var resultData = {
status: record.status || 200,
compilerInfo: record.compilerInfo || '',
content: record.content || '',
error: record.error || '',
code: record.code || ''
};
Iterm.compileResult(resultData, record.runtimeArgus)
}else if (record.runtimeArgus){
// var iterm = new Iterm();
Iterm.compileResult(resultData, record.runtimeArgus)
}
CodingEditer.triggertotal += 1;
if (CodingEditer.triggertotal === CodingEditer.records.length && !replay_type) {
CodingEditer.editable(0);
CodingEditer.flag = true
for (var i = 0; i < CodingEditer.records.length; i++) {
CodingEditer.records[i].trigger = 0;
}
CodingEditer.triggertotal = 0;
CodingEditer.firstPlay = 1;
if (_role == 1) {
$('.play-code').show();
$('.pause-play-code').hide();
}
}
}
}
function processChangeObject(playbackcm, obj) {
// 菜单选中
Dir.showEdite({'treeNode': {'name': getEditor(playbackcm, 'name')},'replay': true});
for (var i = 0; i < obj.text.length; i++) {
/* 设置鼠标行标示 */
if (playbackcm.getTextArea().id === "HTMLplayer") {
if (htmlhlLine !== null) {
playbackcm.clearMarker(htmlhlLine);
}
htmlhlLine = playbackcm.setMarker(obj.from.line + i, '<span class="text-primary glyphicon glyphicon-pencil"></span>');
} else if (playbackcm.getTextArea().id === "CSSplayer") {
if (csshlLine !== null) {
playbackcm.clearMarker(csshlLine);
}
csshlLine = playbackcm.setMarker(obj.from.line + i, '<span class="text-primary glyphicon glyphicon-pencil"></span>');
} else if (playbackcm.getTextArea().id === "JSplayer") {
if (jshlLine !== null) {
playbackcm.clearMarker(jshlLine);
}
jshlLine = playbackcm.setMarker(obj.from.line + i, '<span class="text-primary glyphicon glyphicon-pencil"></span>');
}
/* 设置鼠标输入指针 */
playbackcm.setCursor({ line: obj.from.line + i, ch: obj.from.ch });
//以下代码处理其他
if (obj.text.length === 3 && obj.text[1] !== '' && i === 1) {
playbackcm.setSelection({ line: obj.from.line + i, ch: 0 });
} else if (obj.text.length === 3 && obj.text[2] !== '' && i === 2) {
playbackcm.setSelection({ line: obj.from.line + i, ch: 0 });
//以下代码处理文字块选择并回车的问题
} else if (obj.text.length === 2 && obj.text[i] === '' && i === 1) {
playbackcm.setSelection({ line: obj.from.line + i, ch: 0 });
//以下处理块选择并且粘贴多行问题
} else {
if (i == 0) {
playbackcm.setSelection({ line: obj.from.line + i, ch: obj.from.ch }, { line: obj.to.line + i, ch: obj.to.ch });
} else {
playbackcm.setSelection({ line: obj.from.line + i, ch: 0 }, { line: obj.from.line + i, ch: 0 });
}
}
if (i !== obj.text.length - 1) {
playbackcm.replaceSelection(obj.text[i] + '\n');
} else {
playbackcm.replaceSelection(obj.text[i]);
}
}
if (obj.next) {
processChangeObject(playbackcm, obj.next);
}
if (CodingEditer.autorun) {
CodingEditer.generateResult(CodingEditer.htmlplayereditor.getValue(),
CodingEditer.cssplayereditor.getValue(),
CodingEditer.jsplayereditor.getValue()
);
}
}
function getEditor(obj, key){
var currentObj,
treeLinkEditor = Dir.treeLinkEditor;
for (var i = 0; i < treeLinkEditor.length; i++) {
if(treeLinkEditor[i].CodeMirrorReplay === obj){
currentObj = treeLinkEditor[i];
}
}
switch (key) {
case 'name':
return currentObj.name;
break;
case 'file':
return currentObj.file;
break;
case 'filePath':
return currentObj.file;
break;
case 'record':
return currentObj.record;
break;
case 'replay':
return currentObj.replay;
break;
case 'CodeMirrorRecord':
return currentObj.CodeMirrorRecord;
break;
case 'CodeMirrorReplay':
return currentObj.CodeMirrorReplay;
break;
case 'CodeMirrorRecordId':
return currentObj.CodeMirrorRecordId;
break;
case 'CodeMirrorReplayId':
return currentObj.CodeMirrorReplayId;
break;
default:
break;
}
}
window.CodingEditer = CodingEditer;
})(window)