student.html
11 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>学生端</title>
<link href="../css/reset.css" rel="stylesheet">
<link rel="stylesheet" href="../css/responsive/css/bootstrap.min.css">
<link rel="stylesheet" href="../js/codemirror-5.25.0/lib/codemirror.css">
<link rel="stylesheet" href="../js/ladda/ladda-themeless.min.css">
<link rel="stylesheet" href="style/main.css">
<style>
.teacher-live, .student-work{
position: absolute;
width: 50%;
height: 100%;
}
.teacher-live{
left: 0;
border-right: 1px solid #999;
}
.student-work{
right: 0;
}
</style>
<script>
var gxb_api = "https://restful.gaoxiaobang.com";
</script>
</head>
<body>
<div class="student">
<div class="teacher-live">
<div class="live-wrap">
<!-- 头部信息 -->
<div class="header">
<h1>老师端</h1>
</div>
<!-- 录制区域 -->
<div class="live-code">
<textarea id="teacher" title="HTML"></textarea>
</div>
<!-- 编译操作区 -->
<!-- 运行结果区 -->
<div class="live-console-wrap">
<div id="toolbar">
<!-- Nav tabs -->
<ul class="nav nav-tabs result-tabs need-compile" role="tablist" id="cmprun-tabs">
<li class="active"><a href="#stdin" role="tab" data-toggle="tab" class="text">标准</a></li>
<li><a href="#cmpinfo" role="tab" data-toggle="tab" class="text">编译</a></li>
<li><a href="#stderr" role="tab" data-toggle="tab" class="text">错误</a></li>
<li><a href="#output" role="tab" data-toggle="tab" class="text">结果</a></li>
</ul>
<div class="pull-right func">
<button class="btn compile-btn" id="CompileBtn">
<span class="ladda-label">编译运行</span>
</button>
<a id="full-screen" class="fb">
<img src="../img/full-screen-icon.svg" alt="">
</a>
<a id="back-screen" class="fb" style="display: none;">
<img src="../img/full-screen-icon.svg" alt="">
</a>
</div>
</div>
<!-- Tab panes -->
<div id="resultoutput" class="tab-content need-compile">
<div class="tab-pane active padall15" id="stdin">
<div class="stdin-wrap">
<input class="effect-2" id="runtimeArgus" type="text" placeholder="标准输入">
<span class="focus-border"></span>
</div>
</div>
<div class="tab-pane padall15" id="cmpinfo"></div>
<div class="tab-pane padall15" id="stderr"></div>
<div class="tab-pane padall15" id="output"></div>
</div>
</div>
<!-- 录制控制区 -->
<div class="live-ctrl">
</div>
</div>
</div>
<div class="student-work">
<div class="live-wrap">
<!-- 头部信息 -->
<div class="header">
<h1>学生端</h1>
</div>
<!-- 录制区域 -->
<div class="live-code">
<textarea id="student" title="HTML"></textarea>
</div>
<!-- 编译操作区 -->
<!-- 运行结果区 -->
<!-- 录制控制区 -->
<div class="live-ctrl">
</div>
</div>
</div>
</div>
<script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
<script src="https://code.jquery.com/jquery-1.11.1.js"></script>
<script src="../js/codemirror-5.25.0/lib/codemirror.js"></script>
<script src="../js/bootstrap.js"></script>
<script src="../js/ladda/spin.js"></script>
<script src="../js/ladda/ladda.js"></script>
<script src="./js/main.js"></script>
<script>
$(function() {
var teacherId = document.getElementById('teacher');
var studentId = document.getElementById('student');
var socket = io('http://localhost:3000');
var teacher = CodeMirror.fromTextArea(teacherId, {
value: '输入HTML代码',
mode: 'text/javascript',
lineNumbers: true,
smartIndent: false
});
var student = CodeMirror.fromTextArea(studentId, {
value: '输入HTML代码',
mode: 'text/javascript',
lineNumbers: true,
smartIndent: false
});
socket.on('teacher.programming', function(msg) {
console.log(msg);
var msgObj = JSON.parse(msg)
// student.setValue(msg)
processChangeObject(teacher, msgObj);
$('#messages').append($('<li>').text(msg));
window.scrollTo(0, document.body.scrollHeight);
});
socket.on('programming.compile', function(msg) {
console.log('-------------');
console.log(msg);
// $('#CompileBtn').trigger('click');
console.log('编译' + teacher.getValue());
var el = document.querySelector('#CompileBtn')
var l = Ladda.create(el);
l.start();
var input = $('#input').val() || '';
var code = teacher.getValue();
var compileData = {
code: code,
langid: 10,
input: input
}
$.ajax({
type: "post",
url: gxb_api + "/submit/submitCode/api",
data: JSON.stringify(compileData),
dataType: "json",
contentType: "application/json",
success: function(data) {
console.log(data);
l.remove();
compileResult(data);
},
error: function(){
l.stop();
alert('错误');
}
});
});
function processChangeObject(playbackcm, obj) {
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);
}
}
$('#CompileBtn').on('click', function(){
console.log('编译' + teacher.getValue());
var el = document.querySelector('#CompileBtn')
var l = Ladda.create(el);
l.start();
var input = $('#input').val() || '';
var code = teacher.getValue();
var compileData = {
code: code,
langid: 10,
input: input
}
$.ajax({
type: "post",
url: gxb_api + "/submit/submitCode/api",
data: JSON.stringify(compileData),
dataType: "json",
contentType: "application/json",
success: function(data) {
console.log(data);
l.remove();
compileResult(data);
},
error: function(){
l.stop();
alert('错误');
}
});
})
// 编译结果
function compileResult(data){
if(data.result){
$('#cmpinfo').html('<p class="text-warning">' + data.cmpinfo.replace(/\n/g, "<br />") + "</p>");
$('#stderr').html('<p class="text-danger">' + data.stderr.replace(/\n/g, "<br />") + "</p>");
$('#output').html('<p class="text-success">' + data.output.replace(/\n/g, "<br />") + "</p>");
if(data.cmpinfo.length){
$('#cmprun-tabs a[href="#cmpinfo"]').tab('show');
}else if(data.stderr.length){
$('#cmprun-tabs a[href="#stderr"]').tab('show');
}else if(data.output.length){
$('#cmprun-tabs a[href="#output"]').tab('show');
}
if(!data.cmpinfo.length){
$('#cmpinfo').prepend('<p class="alert alert-success"><i class="glyphicon glyphicon-ok-sign"></i> 编译已成功</p>');
}
if(!data.stderr.length){
$('#stderr').prepend('<p class="alert alert-success"><i class="glyphicon glyphicon-ok-sign"></i> 无输出错误</p>');
}
if(!data.output.length){
$('#output').prepend('<p class="alert alert-warning"><i class="glyphicon glyphicon-exclamation-sign"></i> 代码编译错误或者运行相关功能受限制</p>');
}
}else{
$('#cmpinfo').html('<p class="alert alert-danger"><i class="glyphicon glyphicon-exclamation-sign"></i> 云端编译超时,请稍后重新尝试</p>');
$('#cmprun-tabs a[href="#cmpinfo"]').tab('show');
}
}
});
</script>
</body>
</html>