forked from gradia-lang/grab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
283 lines (253 loc) · 10.6 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
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
<!DOCTYPE html>
<html lang="ja">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://gradia-lang.github.io/gradia.js" type="module"></script>
<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lz-string/1.4.4/lz-string.min.js"></script>
<title>Graboratory</title>
</head>
<body>
<header>
<span>Graboratory</span>
<button id="new-code" onclick="document.getElementById('notearea').appendChild(createCell(isCode=true))">New Code</button>
<button id="new-text" onclick="document.getElementById('notearea').appendChild(createCell(isCode=false))">New Text</button>
<button id="copy-raw" onclick="saveState()">Copy Raw</button>
</header>
<div id="notearea"></div>
</body>
<script type='module'>
// Gradia instance
let gradia;
let lang;
function createCell(isCode = false) {
let cell = document.createElement("div");
cell.className = "cell";
if (isCode) {
let title = document.createElement("span");
if (lang == "ja") {
title.innerHTML = "Gradiaコード"
} else {
title.innerHTML = "Gradia code"
}
cell.appendChild(title);
let button = document.createElement("button");
if (lang == "ja") {
button.innerHTML = "実行"
} else {
button.innerHTML = "Run";
}
button.onclick = function() {
this.parentElement.childNodes[2].style.display = "none";
let code = this.parentElement.childNodes[3].value;
if (this.parentElement.lastChild.className == "result") {
this.parentElement.lastChild.innerHTML = gradia.eval(code);
} else {
let result = document.createElement("pre");
result.className = "result";
result.innerHTML = gradia.eval(code);
this.parentElement.appendChild(result);
}
};
cell.appendChild(button);
} else {
let title = document.createElement("span");
if (lang == "ja") {
title.innerHTML = "文章"
} else {
title.innerHTML = "Text"
}
cell.appendChild(title);
let button = document.createElement("button");
if (lang == "ja") {
button.innerHTML = "表示";
} else {
button.innerHTML = "Show";
}
button.onclick = function() {
let code = this.parentElement.childNodes[3].value;
this.parentElement.childNodes[0].style.display = "none";
this.parentElement.childNodes[1].style.display = "none";
this.parentElement.childNodes[2].style.display = "none";
this.parentElement.childNodes[3].style.display = "none";
if (this.parentElement.lastChild.className == "result") {
this.parentElement.lastChild.style.display = "block";
this.parentElement.lastChild.innerHTML = marked.marked(code);
} else {
let result = document.createElement("div");
result.className = "result";
result.innerHTML = marked.marked(code);
result.onclick = function() {
this.parentElement.childNodes[0].style.display = "inline";
this.parentElement.childNodes[1].style.display = "inline";
this.parentElement.childNodes[2].style.display = "none";
this.parentElement.childNodes[3].style.display = "block";
this.parentElement.lastChild.style.display = "none";
};
this.parentElement.appendChild(result);
}
};
cell.appendChild(button);
}
let deleteButton = document.createElement("button");
if (lang == "ja") {
deleteButton.innerHTML = "削除";
} else {
deleteButton.innerHTML = "Delete";
}
deleteButton.onclick = function() {
this.parentElement.remove()
};
deleteButton.style.display = "none";
cell.append(deleteButton);
let editor = document.createElement("textarea");
editor.onfocus = function() {
this.parentElement.childNodes[2].style.display = "inline";
}
editor.addEventListener("input", function() {
let len = (this.value.match(/\n/g) || []).length;
this.style.height = `${len+2}lh`;
});
if (isCode) {
editor.className = "code";
} else {
editor.className = "text";
}
cell.appendChild(editor);
return cell;
}
function changeLangMenu() {
if (lang == "ja") {
document.getElementById("new-code").innerHTML = "新しいコード";
document.getElementById("new-text").innerHTML = "新しい文章";
document.getElementById("copy-raw").innerHTML = "生データをコピー"
}
};
function saveState() {
let result = [];
for (let node of document.getElementById('notearea').children) {
node = node.childNodes[3]
console.log(node);
result.push([node.className, node.value]);
}
result = JSON.stringify(result)
window.navigator.clipboard.writeText(result);
}
function loadStateFromWeb() {
var xhr = new XMLHttpRequest();
let url = decodeURIComponent(new URL(window.location.href).searchParams.get("link"));
if (url == 'null') {
return
}
lang = decodeURIComponent(new URL(window.location.href).searchParams.get("lang"));
changeLangMenu();
xhr.open("GET", url, true);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
let state = JSON.parse(xhr.responseText);
for (let node of state) {
if (node[0] == "code") {
let cell = createCell(isCode=true);
let len = (node[1].match(/\n/g) || []).length;
cell.childNodes[3].value = node[1];
cell.childNodes[3].style.height = `${len+2}lh`;
document.getElementById('notearea').appendChild(cell);
} else {
let cell = createCell(isCode=false);
let len = (node[1].match(/\n/g) || []).length;
cell.childNodes[3].value = node[1];
cell.childNodes[3].style.height = `${len+2}lh`;
document.getElementById('notearea').appendChild(cell);
cell.childNodes[1].click();
}
}
}
};
xhr.send();
}
document.addEventListener("DOMContentLoaded", loadStateFromWeb);
window.addEventListener("load", () => {
const GET = new URLSearchParams(location.search);
if (GET.get('raw') !== null) {
console.log(GET.get('raw'))
console.log(decodeURIComponent(GET.get('raw')))
let state = JSON.parse(decodeURIComponent(GET.get('raw')));
for (let node of state) {
if (node[0] == "code") {
let cell = createCell(true);
let len = (node[1].match(/\n/g) || []).length;
cell.childNodes[3].value = node[1];
cell.childNodes[3].style.height = `${len+2}lh`;
document.getElementById('notearea').appendChild(cell);
} else {
let cell = createCell(false);
let len = (node[1].match(/\n/g) || []).length;
cell.childNodes[3].value = node[1];
cell.childNodes[3].style.height = `${len+2}lh`;
document.getElementById('notearea').appendChild(cell);
cell.childNodes[1].click();
}
}
} else {
console.info('[GET] raw data is not found.')
}
})
setTimeout(function() {
gradia = new Gradia();
}, 1500);
</script>
<style>
body {
background-color: rgba(240, 240, 240, 0.3);
}
span {
padding: 1vh;
}
header {
padding: 1vh;
background-color: whitesmoke;
border: 1px solid lightgray;
border-radius: 5px;
}
#notearea {
background-color: transparent;
}
.cell {
padding: 1vh;
background-color: transparent;
border-bottom: 1px solid lightgray;
margin-bottom: 2vh;
}
.cell textarea {
resize: vertical;
height: 2lh;
display: block;
border-style: double;
border-width: 0.5px;
border-radius: 5px;
width: 100%;
line-height: 150%;
box-sizing: border-box;
padding: 1vh;
}
.cell textarea:focus {
outline-style: solid;
outline-width: 1px;
outline-color:cornflowerblue;
}
button {
padding: 1vh;
border-radius: 5px;
border: 1px solid lightgray;
background-color: whitesmoke;
margin-bottom: 0.5vh;
margin-right: 0.5vh;
}
.cell .result {
padding: 1vh;
overflow: scroll;
background-color: transparent;
}
</style>
</html>