-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditor.php
More file actions
208 lines (184 loc) · 6.51 KB
/
editor.php
File metadata and controls
208 lines (184 loc) · 6.51 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
<!DOCTYPE html>
<html lang="en">
<head>
<title>在线代码编辑器</title>
<style type="text/css" media="screen">
#editor {
/*position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;*/
width: 50%;
height: 750px;
float: left;
}
.result{
width: 50%;
height: 750px;
float: left;
overflow: scroll;
background: #8F908A;
}
.r-content{
margin: 10px;
color: #fff;
}
.header{
height: 45px;
width: 100%;
background: #438eb9;
}
.header .text{
padding: 6px 30px;
font-size:21px;
color: #fff;
}
.editor{
width: 100%;
}
</style>
</head>
<body style="margin: 0 0; background: #2F3129;">
<div class="row">
<div class="header">
<div class="text">在线代码编辑器</div>
</div>
<div class="editor">
<div id="editor"><?php
if($defaultCode){
echo htmlspecialchars("<?php".$defaultCode."\r?>");
}else {
?>
<?php
$aa=60;
echo 'hello '.'xxx'.$aa."\n";
echo date('Y-m-d H:i:s',time())."\n";
echo "PHP版本:".phpversion();
?>
<?php
}
?>
</div>
<div class="result">
<div class="r-content">
<div>
<button style="cursor: pointer;" class="run">运行</button>
<button style="cursor: pointer;" class="json-format">Json格式化</button>
<button style="cursor: pointer;" class="html-format">Html</button>
<hr/>
</div>
<div class="r-text">
</div>
</div>
</div>
<script src="<?php echo $dir_prefix; ?>/src-noconflict/jquery-2.0.3.min.js" type="text/javascript" charset="utf-8"></script>
<script src="<?php echo $dir_prefix; ?>/src-noconflict/ace.js" type="text/javascript" charset="utf-8"></script>
<script src="<?php echo $dir_prefix; ?>/src-noconflict/ext-language_tools.js" type="text/javascript" charset="utf-8"></script>
<script>
var editor = ace.edit("editor");
editor.setOptions({
enableBasicAutocompletion: true,
enableSnippets: true,
enableLiveAutocompletion: true
});
editor.setTheme("ace/theme/monokai");
editor.getSession().setMode("ace/mode/php");
function myGetCode() {
var arr = [];
$('#editor .ace_line').each(function () {
arr.push($(this).text());
})
return arr;
}
</script>
<script>
var timer = '';
var url = '';
var _DATA_ = '';
$('.run').click(function () {
var code = myGetCode();
loading();
$.ajax({
url : url,
type : 'post',
data : {"code" : code, 'from' : 'js'},
success : function (data) {
_DATA_ = data;
loading(true);
$('.r-text').html(data);
},
dataType : 'html',
error : function (data) {
_DATA_ = data.responseText;
loading(true);
$('.r-text').html(_DATA_);
}
});
});
$('.json-format').click(function () {
try {
var str = '';
str = _DATA_.replace(/.*?<br\/><hr\/>/, '')
.replace(/"/g, '"');
obj = eval('(' + str + ')');
}catch(e){
alert('返回结果为非Json字符串,无法转换');
return false;
}
$('.r-text').html(objtostr(obj));
});
$('.html-format').click(function () {
$('.r-text').html(_DATA_);
});
function loading(close) {
clearInterval(timer);
var str = '';
if(! close){
var t = 1;
str = '正在解析代码,请稍后';
$('.r-text').html(str);
timer = setInterval(function(){
var mod = t%4;
var prefix = '';
for(var i=0; i<mod; i++){
prefix += ' . ';
}
$('.r-text').html(str+prefix);
t++;
}, 1000)
}else{
$('.r-text').html('');
}
}
function objtostr(infoObj, level=1)
{
var
str = '',
next_level = 0;
next_level = level;
next_level++;
if(typeof(infoObj) == 'object'){
str += '<br/>'+getindent(next_level)+'-------level'+level+'-----------<br/>';
for(var key in infoObj){
str += getindent(next_level)+key + ' : ' + objtostr(infoObj[key], next_level) +'<br/>';
}
}else{
str += infoObj;
}
//获取缩进
function getindent(le)
{
var indent = '';
for(var i=0; i<le*6; i++){
indent += ' '
}
return indent;
}
return str;
}
</script>
</div>
</div>
</body>
</html>