-
Notifications
You must be signed in to change notification settings - Fork 0
/
cppedit.html
348 lines (319 loc) · 9.71 KB
/
cppedit.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
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
<!DOCTYPE html>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>C++输入法</title>
<style>
* {
box-sizing: border-box;
}
body {
margin: 0;
padding: 0;
display: flex;
flex-direction: column;
height: 100vh;
color: var(--base00);
font-size: 12px;
overflow: hidden;
--yellow: #b58900;
--orange: #cb4b16;
--red: #dc322f;
--magenta: #d33682;
--violet: #6c71c4;
--blue: #268bd2;
--cyan: #2aa198;
--green: #859900;
--base03: silver;
--base02: silver;
--base01: silver;
--base00: silver;
--base0: silver;
--base1: silver;
--base2: silver;
--base3: silver;
}
body.light {
--base03: #002b36;
--base02: #073642;
--base01: #586e75;
--base00: #657b83;
--base0: #839496;
--base1: #93a1a1;
--base2: #eee8d5;
--base3: #fdf6e3;
}
body.dark {
--base03: #fdf6e3;
--base02: #eee8d5;
--base01: #93a1a1;
--base00: #839496;
--base0: #657b83;
--base1: #586e75;
--base2: #073642;
--base3: #002b36;
}
#cpp {
border-style: none;
resize: none;
flex: 1;
font-family: monospace;
font-size: 1.2rem;
background-color: var(--base3);
color: var(--base00);
border-style: none;
}
#tools {
flex: 0 11rem;
overflow-y: auto;
display: grid;
grid-template-columns: repeat(auto-fill, minmax(7rem, 1fr));
background-color: var(--base2);
font-size: 1rem;
}
#tools > button {
border-style: none;
flex: 1;
font-size: 1rem;
background-color: var(--base2);
color: var(--base00);
height: 2.5rem;
}
#tools > button:active {
background-color: var(--base00);
color: var(--base2);
font-weight: bold;
}
#tools > fieldset {
grid-column: 1 / -1;
border-color: var(--base1);
border-style: solid;
}
legend {
font-size: 0.8em;
color: var(--base1);
}
.radio-group {
display: flex;
flex-direction: row;
}
.radio-group > * {
flex: 1;
}
.radio-group > :active {
background-color: var(--base00);
color: var(--base2);
font-weight: bold;
}
.radio-group input[type="radio"] {
display: none;
}
.radio-group input[type="radio"] + * {
display: block;
width: 100%;
text-align: center;
}
.radio-group input[type="radio"]:checked + * {
font-weight: bold;
color: var(--base01);
border-bottom: thin solid var(--base00);
}
.icon-adjust, .icon-play {
display: inline-block;
vertical-align: -0.5ex;
width: 1em;
height: 1em;
}
.icon-adjust::before {
content: "";
display: inline-block;
width: 0.5em;
height: 1em;
border-radius: 0.5em 0 0 0.5em;
background-color: currentcolor;
}
.icon-adjust::after {
content: "";
display: inline-block;
box-sizing: border-box;
width: 0.5em;
height: 1em;
border-radius: 0 0.5em 0.5em 0;
border: 0.05em solid currentcolor;
border-left-style: none;
}
.icon-play::before {
content: "";
display: inline-block;
box-sizing: content-box;
width: 0;
height: 0;
margin: 0 0.25em;
border-style: solid;
border-width: 0.5em 0 0.5em 0.5em;
border-color: transparent transparent transparent currentcolor;
}
</style>
<textarea id="cpp"></textarea>
<div id="tools">
<button onclick="cpp.select();document.execCommand('copy');">全部复制</button>
<button onclick="prev();">上一占位符</button>
<button onclick="next();">下一占位符</button>
<button onclick="cpp.focus();document.execCommand('cut');">随缘剪切</button>
<button onclick="cpp.focus();document.execCommand('copy');">随缘复制</button>
<button onclick="cpp.focus();document.execCommand('paste');">随缘粘贴</button>
<button onclick="ins('\t');">制表符</button>
<button onclick="ins(';');"><code>;</code></button>
<button onclick="ins('[□]');"><code>[ ]</code></button>
<button onclick="ins('{□}');"><code>{ }</code></button>
<button onclick="ins('<');"><code><</code></button>
<button onclick="ins('>');"><code>></code></button>
<button onclick="ins('&&');"><code>&&</code></button>
<button onclick="ins('||');"><code>||</code></button>
<button onclick="ins('\\n');"><code>\n</code></button>
<fieldset>
<legend>循环变量</legend>
<div class="radio-group">
<label><input type="radio" name="loopvar" checked><var>i</var></label>
<label><input type="radio" name="loopvar"><var>j</var></label>
<label><input type="radio" name="loopvar"><var>k</var></label>
<label><input type="radio" name="loopvar"><var>a</var></label>
<label><input type="radio" name="loopvar"><var>b</var></label>
<label><input type="radio" name="loopvar"><var>c</var></label>
<label><input type="radio" name="loopvar"><var>d</var></label>
<label><input type="radio" name="loopvar"><var>m</var></label>
<label><input type="radio" name="loopvar"><var>n</var></label>
<label><input type="radio" name="loopvar"><var>x</var></label>
<label><input type="radio" name="loopvar"><var>y</var></label>
</div>
</fieldset>
<button onclick="insline('#include <cstdio>\n#include <iostream>\n#include <iomanip>\nusing namespace std;\n\nint main(int argc, char *argv[]) {\n□\nreturn 0;\n}');">主程序</button>
<button onclick="insline('#include <□>');">包含头</button>
<button onclick="insline('cin >> □;');">流输入</button>
<button onclick="insline('scanf("%d", &□);');">C风格输入</button>
<button onclick="insline('cout << □ << endl;');">流输出</button>
<button onclick="insline('ios::sync_with_stdio(false);');">禁用流同步</button>
<button onclick="insline('printf("%d", □);');">C风格输出</button>
<button onclick="insline('if (□) {\n□;\n} else {\n}');">判断</button>
<button onclick="insline('for (;;) {\n□;\n}');">循环</button>
<button onclick="insline('for (int ★ = 0; ★ < □; ++★) {\n□;\n}');">计次循环</button>
<button onclick="insline('while (□) {\n□;\n}');">判断循环</button>
<button onclick="insline('struct □ {\n□;\n};');">结构体</button>
<button onclick="insline('class □ {\nprivate:\n□;\npublic:\n□;\n};');">类</button>
<button onclick="alert('运行个鬼');"><span class="icon-play"></span> 没法运行</button>
<button onclick="autoindent();">重算缩进</button>
<fieldset>
<legend>制表位大小</legend>
<input type="range" min="2" max="8" value="8" oninput="cpp.style.tabSize = this.value; cpp.style.MozTabSize = this.value; this.nextSibling.nextSibling.innerHTML = this.value;">
<span>8</span>
</fieldset>
<button onclick="togglelightdark();"><span class="icon-adjust"></span> 主题</button>
<button onclick="alert('【cppedit】\nFrog Chen制作的非常简陋的C++程序输入法,主要用于在手机上写数据结构课的作业。\n作者是个连关于框都懒得仔细做的屑。\n原始发布地址:https://salenzo.github.io/Utilities/cppedit.html\n使用了Solarized色彩方案,地址:https://ethanschoonover.com/solarized/');">关于</button>
</div>
<script>
if (window.location.hash == "#light") {
document.body.classList.add("light");
} else if (window.location.hash == "#dark") {
document.body.classList.add("dark");
} else {
if (window.matchMedia("prefers-color-scheme: light").matches) {
window.location.hash = "#light";
document.body.classList.add("light");
} else {
window.location.hash = "#dark";
document.body.classList.add("dark");
}
}
function togglelightdark() {
if (document.body.classList.contains("dark")) {
document.body.classList.remove("dark");
document.body.classList.add("light");
window.location.hash = "#light";
} else {
document.body.classList.remove("light");
document.body.classList.add("dark");
window.location.hash = "#dark";
}
}
window.onhashchange = function () {
if (window.location.hash == "#light") {
document.body.classList.remove("dark");
document.body.classList.add("light");
} else {
window.location.hash = "#dark";
document.body.classList.remove("light");
document.body.classList.add("dark");
}
}
window.onbeforeunload = function (e) {
if (cpp.value.trim().length) {
e.preventDefault();
e.returnValue = "";
} else {
delete e["returnValue"];
}
};
function preprocess(text) {
text = text.replace(/★/g, document.querySelector('input[name="loopvar"]:checked').parentNode.innerText);
return text;
}
function ins(text) {
text = preprocess(text);
cpp.focus();
cpp.setRangeText(text, cpp.selectionStart, cpp.selectionEnd, "end");
if (text.indexOf("□") >= 0) {
let index = cpp.value.lastIndexOf("□", cpp.selectionStart);
cpp.setSelectionRange(index, index + 1, "forward");
}
}
function insline(text) {
text = preprocess(text);
let str = "\n" + cpp.value + "\n";
let str_before = str.slice(0, str.lastIndexOf("\n", cpp.selectionStart + 1) + 1).trimStart();
let str_after = str.slice(str.lastIndexOf("\n", cpp.selectionEnd + 1)).trimEnd();
str = "\n" + str_before + text + str_after + "\n";
cpp.value = str;
if (text.indexOf("□") >= 0) {
let index = 1 + str_before.length + text.indexOf("□");
cpp.setSelectionRange(index, index + 1, "forward");
cpp.focus();
}
}
function prev() {
let index = cpp.value.lastIndexOf("□", cpp.selectionStart - 1);
if (index < 0) {
index = cpp.value.lastIndexOf("□");
if (index < 0) {
return;
}
}
cpp.setSelectionRange(index, index + 1, "forward");
cpp.focus();
}
function next() {
let index = cpp.value.indexOf("□", cpp.selectionStart + 1);
if (index < 0) {
index = cpp.value.indexOf("□");
if (index < 0) {
return;
}
}
cpp.setSelectionRange(index, index + 1, "forward");
cpp.focus();
}
function autoindent() {
let indent = 0;
cpp.value = cpp.value.split("\n").map(line => {
line = line.trim();
if (!line.length) return line;
if (line[0] == "}") {
indent--;
}
line = "\t".repeat(line[line.length - 1] == ":" ? indent - 1 : indent) + line;
if (line[line.length - 1] == "{") {
indent++;
}
return line;
}).join("\n");
}
</script>