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
|
<!-- This Source Code Form is subject to the terms of the Mozilla Public
- License, v. 2.0. If a copy of the MPL was not distributed with this
- file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
<html>
<head>
<link rel="stylesheet" href="./codemirror.css">
<link rel="stylesheet" href="./main.css">
<script src="./codemirror-compressed.js"></script>
<views>
<section class="task cm-s-default">
<pre class="request "></pre>
<pre class="response"><span class="one"></span><span class="two"></span><span class="three"></span></pre>
</section>
</views>
</head>
<body>
<pre class="input"></pre>
</body>
<script>
function debounce(fn, ms) {
var id;
return function(...args) {
clearTimeout(id);
id = setTimeout(fn, ms, ...args);
};
}
function Try(fn) {
return function(...args) {
try { return fn(...args); }
catch (error) { return null; }
};
}
var parse = Try(JSON.parse);
var CommandHistory = {
init: function() {
this._state = {};
this._state.els = document.querySelectorAll("body > section.task > .request");
this._state.idx = this._state.els.length;
},
get prev() {
if (!!this._state.els && this._state.idx > 0) {
this._state.idx--;
return this._state.els[this._state.idx].textContent;
}
return "";
},
get next() {
if (!!this._state.els && this._state.idx < this._state.els.length-1) {
this._state.idx++;
return this._state.els[this._state.idx].textContent;
}
return "";
}
}
function cmdHistory(fn, editor) {
editor.setValue(fn());
document.querySelector(".input").scrollIntoView();
}
var cmdHistoryNext = cmdHistory.bind(null, () => CommandHistory.next);
var cmdHistoryBack = cmdHistory.bind(null, () => CommandHistory.prev);
function send(editor) {
var input = editor.getWrapperElement().parentNode;
var code = editor.getValue().trim();
var packet = parse(code);
if (packet) {
var task = document.querySelector("views .task").cloneNode(true);
var request = task.querySelector(".request");
var response = task.querySelector(".response");
input.parentNode.insertBefore(task, input);
CodeMirror.runMode(JSON.stringify(packet, 2, 2),
"application/json",
request);
response.classList.add("pending");
editor.setValue("");
document.querySelector(".input").scrollIntoView();
port.postMessage(packet);
}
}
var editor = CodeMirror(document.querySelector(".input"), {
autofocus: true,
mode: "application/json",
matchBrackets: true,
value: '{"to": "root", "type": "requestTypes"}',
extraKeys: {"Cmd-Enter": send,
"Ctrl-Enter": send,
"Cmd-Down": cmdHistoryNext,
"Ctrl-Down": cmdHistoryNext,
"Cmd-Up": cmdHistoryBack,
"Ctrl-Up": cmdHistoryBack}
});
editor.on("change", debounce(function(editor) {
var input = editor.getWrapperElement().parentNode;
if (parse(editor.getValue().trim())) {
input.classList.remove("invalid");
} else {
input.classList.add("invalid");
}
}, 800));
</script>
<script>
window.addEventListener("message", event => {
window.port = event.ports[0];
port.onmessage = onMessage;
});
var onMessage = (event) => {
var packet = event.data;
var code = JSON.stringify(packet, 2, 2);
var input = document.querySelector(".input");
var response = document.querySelector(".task .response.pending");
if (!response) {
message = document.querySelector("views .task").cloneNode(true);
response = message.querySelector(".response");
response.classList.add("message");
input.parentNode.insertBefore(message, input);
}
if (packet.error) {
response.classList.add("error");
}
CodeMirror.runMode(code, "application/json", response);
response.classList.remove("pending");
document.querySelector(".input").scrollIntoView();
CommandHistory.init();
};
</script>
</html>
|