-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
232 lines (208 loc) · 4.97 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
<!DOCTYPE html>
<!-- Author: Özgür Kesim <[email protected]> 2017 -->
<html>
<head>
{{ $name := .Cur.Name }}
<title>playdot - {{$name}} online</title>
<link rel="stylesheet" href="/static/codemirror.css">
<script src="/static/codemirror.js"></script>
<script src="/static/{{$name}}.js"></script>
<style>
body {
height: 100vh;
width: 100%;
margin:0;
overflow:hidden;
font-family:Calibri,Verdana,Sans-Serif;
}
#top {
padding:10px;
margin:0px;
font-size:20px;
font-family:Sans-serif;
background: {{ .Cur.BgColor }};
}
#source {
height: 40vh;
width: 100%;
overflow:auto;
}
#run {
margin: 10pt;
}
#doc {
margin: 20px;
font-size: 9pt;
}
#output {
width: 100%;
height: 50vh;
overflow:auto;
}
.on {
background:white;
color:{{.Cur.BgColor}};
padding: 5px;
}
svg {
padding: 5px;
// border: 2px solid lime;
}
#link {
margin-left: 20px;
}
.button {
background-color: {{ .Cur.BgColor }};
border: none;
color: white;
padding: 7px 13px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
cursor: pointer;
}
.error {
background-color: lightsalmon;
}
</style>
</head>
<body>
<div id="top">Tools: {{ range .Tools }}
{{ if eq .Name $name }} <span class="on">{{$name}}</span> {{ else }} <a href="/{{.Name}}">{{.Name}}</a> {{ end }} |
{{ end }}
</div>
<!--
TODO:
- add download-button for output in other formats
-->
<textarea name="source" id="source">
{{ .Cur.Example }}
</textarea>
<div id="mid">
<button id="run" class="button" onclick="run()" title="or ctrl-enter in editor">run</button>
<button id="save" class="button" onclick="share()" title="or ctrl-s in editor">share</button><span id="link"></span>
<input id="file" type="file" name="file" class="button"/>
<input id="scale" type ="range" min ="0.5" max="3" step ="0.1" value ="1"/>
<span id="doc">
Documentation:
{{ range $link, $text := .Cur.Documentation }}
<a href="{{$link}}" target="_blank">{{$text}}</a> /
{{ end }}
</span>
</div>
<div id="output"></div>
<script>
var output = document.getElementById("output");
var source = document.getElementById("source");
var link = document.getElementById("link");
var filein = document.getElementById("file");
var scale = document.getElementById("scale");
var editor = CodeMirror.fromTextArea(source, {
lineNumbers: true,
smartIndent: true,
});
window.onkeydown=function(e) {
if (e.ctrlKey && e.key == "Enter") {
e.preventDefault();
run();
} else if (e.ctrlKey && e.key == "s") {
e.preventDefault();
share();
}
}
filein.onchange=function() {
var file = filein.files[0];
var reader = new FileReader();
reader.onload = function (e) {
editor.setValue(e.target.result);
};
reader.readAsText(file);
filein.value="";
window.location.hash="";
}
scale.onchange=function() {
var svg=output.querySelector("svg");
var sc = scale.valueAsNumber;
if (svg) {
svg.height.baseVal.value = svg.viewBox.baseVal.height*sc;
svg.width.baseVal.value = svg.viewBox.baseVal.width*sc;
console.log("scale:", sc, "svg:", svg.height.baseVal.value, svg.width.baseVal.value);
}
}
window.onhashchange = hashloc;
function run() {
var data = editor.getValue().trim();
if (!data) return;
link.innerHTML = "";
var req = new XMLHttpRequest();
req.open("POST", "/{{$name}}/c", false);
req.onreadystatechange = function() {
switch (req.status) {
case 200:
if (req.getResponseHeader("Content-Type") === "image/png") {
output.innerHTML = '<img src="data:image/png;base64,' + req.responseText+ '"/>';
} else {
output.innerHTML = req.responseText;
}
break;
case 400:
output.innerHTML = '<pre class="error">'+req.responseText+'</pre>';
break;
}
}
req.send(data);
}
function share() {
var data = editor.getValue();
var req = new XMLHttpRequest();
req.open("POST", "/{{$name}}/s", false);
req.onreadystatechange = function() {
switch (req.status) {
case 200:
link.innerHTML = "<i>saved as</i> <a href=\"/{{$name}}/#"+req.responseText+"!\">#"+req.responseText+"!</a>" +
" or <a href=\"/{{$name}}/d/"+req.responseText+"\">download</a>";
document.title= "{{$name}}#"+req.responseText;
document.location.hash=req.responseText+"!";
break;
case 400:
output.innerHTML = '<pre class="error">'+req.responseText+'</pre>';
break;
}
}
req.send(data);
}
function load(id) {
link.innerHtml = "";
var req = new XMLHttpRequest();
req.open("GET", "/{{$name}}/l/"+id, false);
req.onreadystatechange = function() {
switch (req.status) {
case 200:
editor.setValue(req.responseText);
break;
case 400:
editor.setValue("");
output.innerHTML = '<pre class="error">'+req.responseText+'</pre>';
break;
}
}
req.send();
}
function hashloc() {
var hash = window.location.hash;
if (hash) {
var parts = (hash.split('#')[1]).split('!');
var id = parts[0];
if (id) {
load(id);
document.title="{{$name}}#"+id;
if (parts.length > 1) run();
}
}
}
hashloc();
</script>
</body>
</html>