-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
214 lines (190 loc) · 5.76 KB
/
index.html
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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>JaM -- A JavaScript Macro system</title>
<style type="text/css">
.header {
position: fixed;
top: 0;
left: 0;
width: 100%;
background: #99b;
border-bottom: 1px #77a solid;
}
#output {
margin-top: 3em;
}
#output pre {
margin: 0;
}
#input {
width: 100%;
height: 6em;
border: none;
border-top: 1px #ddd solid;
}
textarea.cmd {
background: #eee;
}
.user { font-weight: bold; }
.error { font-weight: bold; color: #b00; }
.return { color: #00b; }
</style>
<script type="text/javascript" src="jam.js"></script>
<script type="text/javascript">
var elem = {}
JaM.msg = function( str, className ) {
var pre = document.createElement('pre');
pre.className = className;
pre.appendChild( document.createTextNode( str ) );
elem.output.appendChild( pre );
var docelem = document.documentElement;
docelem.scrollTop = docelem.offsetHeight;
}
function doinclude() {
var file = elem.file.value;
JaM.msg( "JaM.include( '" + file + "' );", 'user' );
JaM.include( file );
}
function doview() {
var file = elem.file.value;
JaM.msg( 'JaM.getText( "' + file + '", JaM.msg );', 'user' );
JaM.getText( file, JaM.msg );
}
var History = {};
(function(){
var list = [];
var i = 0;
History.clear = function() {
list.push( elem.input.value );
i = list.length;
elem.input.value = '';
};
History.move = function( d ) {
if( i + d >= 0 && i + d <= list.length ) {
if( i < list.length && list[ i ] != elem.input.value ) {
list.push( elem.input.value );
}
i += d;
elem.input.value = i < list.length ? list[ i ] : '';
}
};
})();
function doeval() {
var code = elem.input.value;
JaM.msg( code, 'user' );
History.clear();
JaM.msg( JaM.eval( 'user input', code ), 'return' );
}
var mode = 'ins';
function keypress( e ) {
//console.log( '%d, %d: "%s"', e.keyCode, e.charCode, String.fromCharCode( e.charCode ) );
var code = e.keyCode;
var chr = String.fromCharCode( e.charCode );
if( mode == 'cmd' ) {
if( chr == 'i' ) {
mode = 'ins';
elem.input.className = mode;
e.preventDefault();
}
else if( chr == 'h' ) {
/*
var ne = document.createEvent("KeyboardEvent");
ne.initKeyEvent(
'keypress',true,true,null,false,false,false,false,37,0);
elem.input.dispatchEvent( ne );
*/
var sel = getSelection();
if( sel.rangeCount > 0 ) {
var range = sel.getRangeAt( 0 );
range.setStart( range.startContainer, range.startOffset - 1 );
range.collapse( true );
}
e.preventDefault();
}
else if( chr == 'k' || code == 38 ) {
History.move( -1 );
e.preventDefault();
}
else if( chr == 'j' || code == 40 ) {
History.move( +1 );
e.preventDefault();
}
else if( code in [ 33, 34, 35, 36, 37, 39 ] ) {
// arrow keys, let them thru
}
else if( code == 13 ) {
mode = 'ins';
elem.input.className = mode;
doeval();
e.preventDefault();
}
else {
e.preventDefault();
}
}
else {
// insert mode
if( code == 27 ) {
mode = 'cmd';
elem.input.className = mode;
e.preventDefault();
}
else if( code == 38 ) {
History.move( -1 );
e.preventDefault();
}
else if( code == 40 ) {
History.move( +1 );
e.preventDefault();
}
else if( code == 13 ) {
mode = 'ins';
elem.input.className = mode;
doeval();
e.preventDefault();
}
}
}
function doload() {
for( var id in { input:1, output:1, file:1 } ) {
elem[ id ] = document.getElementById( id );
}
elem.input.focus();
//elem.input.addEventListener( 'keypress', dokeypress, true );
}
</script>
</head>
<body onload="doload()">
<table class="header">
<tr>
<td>
<form onsubmit="doinclude(); return false;">
<select id="file">
<option>test01.js</option>
<option>test02.js</option>
<option>test03.js</option>
<option>test04.js</option>
<option>test05.js</option>
<option>test06.js</option>
<option>test07.js</option>
<option>test08.js</option>
<option>test09.js</option>
<option>test10.js</option>
<option>test11.js</option>
<option>test12.js</option>
</select>
<input type="submit" value="include" />
<input type="button" value="view" onclick="doview()" />
</form>
</td>
<td style="text-align: right; font-weight: bold; color: #eee;">
JaM
</td>
</tr>
</table>
<div id="output"></div>
<textarea id="input" onkeypress="keypress(event)"></textarea>
</body>
</html>