-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.html
425 lines (358 loc) · 18.6 KB
/
app.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Simplified Syntax Trees</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-9ndCyUaIbzAi2FUVXJi0CjmCapSmO7SnpJef0486qhLnuZ2cdeRhO02iuK6FUUVM" crossorigin="anonymous">
<style>
body {
overflow: hidden;
}
#visualization {
width: 100%;
height: calc(100vh - 100px);
}
.phrase {
cursor: pointer;
}
.cursor-pointer {
cursor: pointer;
}
</style>
<script src="https://d3js.org/d3.v5.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/canvg/dist/browser/canvg.min.js"></script>
</head>
<body>
<form class="m-2" id="input_form" method="get">
<div class="input-group">
<input type="text" class="form-control" id="input_text" name="i" type="text" placeholder="Syntax Tree Input"
aria-label="Syntax Tree Input">
<button class="btn btn-outline-secondary" type="submit" id="button-addon2">Submit</button>
</div>
</form>
<div class="dropdown m-2">
<button class="btn btn-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">
Export
</button>
<ul class="dropdown-menu">
<li><a class="dropdown-item cursor-pointer" onclick="downloadSvg()">Export as SVG</a></li>
<li><a class="dropdown-item cursor-pointer" onclick="downloadPng()">Export as PNG</a></li>
</ul>
</div>
<script>
// enable undo and redo through browser history
window.onpopstate = function (e) {
if (!e.state.navigation) {
return; // on safari popstate gets fired on page load
}
window.location = location.href;
};
// parse input
let input_text = new URLSearchParams(window.location.search).get('i');
if (input_text == null) { input_text = ""; }
input_text = input_text.replace(/ /g, " ");
input_text = input_text.replace(/ +/g, " ");
input_text = input_text.trim();
document.forms['input_form']['input_text'].value = input_text;
let dataset = input_text.split(" ");
const line_draw_offset = 24 // draw the line above the text, should be >= font-size
const snap_size = 80
// initialize variables
let selected = 0;
let selected_id = 0;
let drag_start_x = 0;
let drag_start_y = 0;
let drag_end_x = 0;
let drag_end_y = 0;
let drag_distance_x = 0;
let drag_distance_y = 0;
let connections = {};
for (let i = 0; i <= dataset.length; i++) {
connections["n" + i] = new Array();
}
function handle_mouseover(d) {
d3.select(this).attr("font-weight", "bolder");
}
function handle_mouseout(d) {
d3.select(this).attr("font-weight", "normal");
}
function handle_click(d) {
// select first phrase, remove connection line and return
if (selected == 0) {
selected = 1
selected_id = d3.select(this).attr("id");
d3.select(this).attr("fill", "red")
svg.select("line#" + selected_id).attr("stroke", "none")
for (let i = 0; i <= dataset.length; i++) {
if (connections["n" + i].includes(selected_id) == true) {
connections["n" + i] = connections["n" + i].filter(item => item !== selected_id);
}
}
dataset[selected_id.slice(1)] = dataset[selected_id.slice(1)].split(":")[0];
// update input text form and url
document.forms['input_form']['input_text'].value = dataset.join(" ");
window.history.pushState({ navigation: true }, '', '/app.html?i=' + dataset.join(" "));
return;
}
// deselect and return if same phrase is selected again
if (d3.select(this).attr("id") == selected_id) {
selected = 0
svg.select("text#" + selected_id).attr("fill", "black");
return;
}
// create connection line if different phrase is selected
svg.select("line#" + selected_id)
.attr("x2", d3.select(this).attr("x"))
.attr("y2", parseInt(d3.select(this).attr("y")) + parseInt(line_draw_offset / 3))
.attr("stroke", "black")
selected = 0
connections[d3.select(this).attr("id")].push(selected_id);
svg.select("text#" + selected_id)
.attr("fill", "black");
// remove : on deselect
let child_id = selected_id.slice(1);
let parent_id = d3.select(this).attr("id").slice(1);
dataset[child_id] = dataset[child_id] + ":" + parent_id;
console.log(dataset.join(' '));
// update input text form and url
document.forms['input_form']['input_text'].value = dataset.join(" ");
window.history.pushState({ navigation: true }, '', '/app.html?i=' + dataset.join(" "));
}
function drag_started(d) {
d3.event.sourceEvent.stopPropagation();
drag_distance_x = d3.select(this).attr("x") - d3.event.x;
drag_distance_y = d3.select(this).attr("y") - d3.event.y;
// for recursive movement
drag_start_x = d3.select(this).attr("x");
drag_start_y = d3.select(this).attr("y");
}
function drag_move(d) {
// move text
d3.select(this)
.attr("x", d3.event.x + drag_distance_x)
.attr("y", d3.event.y + drag_distance_y)
.raise();
// move connection line
svg.select("line#" + d3.select(this).attr("id"))
.attr("x1", d3.select(this).attr("x"))
.attr("y1", d3.select(this).attr("y") - line_draw_offset);
// move connected lines of connected phrases
for (let i = 0; i <= connections[d3.select(this).attr("id")].length; i++) {
svg.select("line#" + connections[d3.select(this).attr("id")][i])
.attr("x2", (d3.event.x + drag_distance_x))
.attr("y2", parseInt(d3.select(this).attr("y")) + parseInt(line_draw_offset / 3));
}
}
function drag_ended(d) {
d3.select(this)
.attr("x", Math.round((d3.event.x + drag_distance_x) / snap_size) * snap_size)
.attr("y", Math.round((d3.event.y + drag_distance_y) / snap_size) * snap_size)
// move connection line
svg.select("line#" + d3.select(this).attr("id"))
.attr("x1", d3.select(this).attr("x"))
.attr("y1", d3.select(this).attr("y") - line_draw_offset);
// move connected lines of connected phrases
for (let b = 0; b <= connections[d3.select(this).attr("id")].length; b++) {
svg.select("line#" + connections[d3.select(this).attr("id")][b])
.attr("x2", d3.select(this).attr("x"))
.attr("y2", parseInt(d3.select(this).attr("y")) + parseInt(line_draw_offset / 3));
}
// recursive movement
drag_end_x = d3.select(this).attr("x");
drag_end_y = d3.select(this).attr("y");
if (d3.event.sourceEvent.altKey) {
move_recursively(d3.select(this), parseInt(drag_end_x) - parseInt(drag_start_x), parseInt(drag_end_y) - parseInt(drag_start_y));
}
}
function move_recursively(phrase, x, y) {
for (let b = 0; b < connections[phrase.attr("id")].length; b++) {
let subphrase = "text#" + connections[phrase.attr("id")][b];
let subphrase_x_moved = parseInt(d3.select(subphrase).attr("x")) + x;
let subphrase_y_moved = parseInt(d3.select(subphrase).attr("y")) + y;
d3.select(subphrase)
.attr("x", subphrase_x_moved)
.attr("y", subphrase_y_moved);
d3.select("line#" + connections[phrase.attr("id")][b])
.attr("x1", subphrase_x_moved)
.attr("y1", subphrase_y_moved - parseInt(line_draw_offset))
.attr("x2", parseInt(phrase.attr("x")))
.attr("y2", parseInt(phrase.attr("y")) + parseInt(line_draw_offset / 3));
move_recursively(d3.select(subphrase), x, y);
}
}
let zoom = d3.zoom()
.scaleExtent([0.5, 4])
.on("zoom", function () {
svg.attr("transform", d3.event.transform)
});
// create svg
let svg = d3.select("body").append("svg")
.attr("xmlns", "http://www.w3.org/2000/svg")
.attr("id", "visualization")
.call(zoom)
.on("dblclick.zoom", null)
.append("g");
// drag handlers
let drag = d3.drag()
.on("start", drag_started)
.on("drag", drag_move)
.on("end", drag_ended);
// create phrases
svg.selectAll("text")
.data(dataset)
.enter()
.append("text")
.attr("x", function (d, i) { return snap_size + i * snap_size * 2; })
.attr("y", snap_size)
.attr("id", function (d, i) { return "n" + i; })
.text(function (d, i) { return d; })
.attr("class", "phrase")
.call(drag)
.on("mouseover", handle_mouseover)
.on("mouseout", handle_mouseout)
.on("click", handle_click)
.attr("font-family", "sans-serif")
.attr("font-size", "24px")
.attr("text-anchor", "middle");
// create connection lines
svg.selectAll("line")
.data(dataset)
.enter()
.append("line")
.attr("stroke-width", 2)
.attr("id", function (d, i) { return "n" + i; });
// connect and move phrases appropriately
{
// iterate over dataset and connect phrases to their parents
for (let j = 0; j < dataset.length; j++) {
let j_split = dataset[j].split(":")
let parent_id = j_split[j_split.length - 1]
if (parseInt(parent_id) || parent_id == "0") {
// place child below parent
// count number of children of same parent
let nth_child = 0;
let this_child = 0;
for (let i = 0; i < dataset.length; i++) {
if (i == j) { this_child = nth_child; }
if (dataset[i].split(":")[dataset[i].split(":").length - 1] == parent_id) {
nth_child++;
}
}
//console.log(j_split[0] + " is child " + this_child + "/" + nth_child);
let movement = (snap_size * 2) * -(this_child + 1 - nth_child);
// calculate movement of children for recursive movement (before moving the phrase)
let min_x = -(parseInt(d3.select("text#n" + j).attr("x")) - parseInt(d3.select("text#n" + parent_id).attr("x")) + movement);
let move_y = parseInt(d3.select("text#n" + parent_id).attr("y")) + snap_size - parseInt(d3.select("text#n" + j).attr("y"));
// move phrase
d3.select("text#n" + j)
.attr("x", parseInt(d3.select("text#n" + parent_id).attr("x")) - movement)
.attr("y", parseInt(d3.select("text#n" + parent_id).attr("y")) + snap_size);
// move connection line
svg.select("line#n" + j)
.attr("x1", d3.select("text#n" + j).attr("x"))
.attr("y1", parseInt(d3.select("text#n" + j).attr("y")) - line_draw_offset)
.attr("x2", d3.select("text#n" + parent_id).attr("x"))
.attr("y2", parseInt(d3.select("text#n" + parent_id).attr("y")) + parseInt(line_draw_offset / 3))
.attr("stroke", "blue");
connections[d3.select("text#n" + parent_id).attr("id")].push("n" + j);
// move children recursively
move_recursively(d3.select("text#n" + j), min_x, move_y);
// remove : from text
d3.select("text#n" + j).text(d3.select("text#n" + j).text().split(":")[0]);
}
}
// move all phrases to the left by min_x (initial positioning)
let initial_min_x = 9000;
for (let i = 0; i < dataset.length; i++) {
if (parseInt(d3.select("text#n" + i).attr("x")) < initial_min_x) {
initial_min_x = parseInt(d3.select("text#n" + i).attr("x"));
}
}
//console.log("min_x: " + initial_min_x);
let initial_move_x = initial_min_x - snap_size;
svg.selectAll("text").attr("x", function (d, i) { return parseInt(d3.select(this).attr("x")) - initial_move_x; });
svg.selectAll("line").attr("x1", function (d, i) { return parseInt(d3.select(this).attr("x1")) - initial_move_x; })
.attr("x2", function (d, i) { return parseInt(d3.select(this).attr("x2")) - initial_move_x; });
}
let export_min_x = Number.MAX_VALUE;
let export_min_y = Number.MAX_VALUE;
let export_transform = svg.attr("transform");
function prepareExport() {
// get movement values
for (let i = 0; i < dataset.length; i++) {
if (parseInt(d3.select("text#n" + i).attr("x")) < export_min_x) {
export_min_x = parseInt(d3.select("text#n" + i).attr("x"));
}
if (parseInt(d3.select("text#n" + i).attr("y")) < export_min_y) {
export_min_y = parseInt(d3.select("text#n" + i).attr("y"));
}
}
// move all elements
svg.selectAll("text").attr("x", function (d, i) { return parseInt(d3.select(this).attr("x")) - export_min_x + snap_size; });
svg.selectAll("line").attr("x1", function (d, i) { return parseInt(d3.select(this).attr("x1")) - export_min_x + snap_size; })
.attr("x2", function (d, i) { return parseInt(d3.select(this).attr("x2")) - export_min_x + snap_size; });
svg.selectAll("text").attr("y", function (d, i) { return parseInt(d3.select(this).attr("y")) - export_min_y + snap_size; });
svg.selectAll("line").attr("y1", function (d, i) { return parseInt(d3.select(this).attr("y1")) - export_min_y + snap_size; })
.attr("y2", function (d, i) { return parseInt(d3.select(this).attr("y2")) - export_min_y + snap_size; });
// save transform and remove it
export_transform = svg.attr("transform");
svg.attr("transform", null);
}
function restoreAfterExport() {
// move elements back
svg.selectAll("text").attr("x", function (d, i) { return parseInt(d3.select(this).attr("x")) + export_min_x - snap_size; });
svg.selectAll("line").attr("x1", function (d, i) { return parseInt(d3.select(this).attr("x1")) + export_min_x - snap_size; })
.attr("x2", function (d, i) { return parseInt(d3.select(this).attr("x2")) + export_min_x - snap_size; });
svg.selectAll("text").attr("y", function (d, i) { return parseInt(d3.select(this).attr("y")) + export_min_y - snap_size; });
svg.selectAll("line").attr("y1", function (d, i) { return parseInt(d3.select(this).attr("y1")) + export_min_y - snap_size; })
.attr("y2", function (d, i) { return parseInt(d3.select(this).attr("y2")) + export_min_y - snap_size; });
// reapply transform
svg.attr("transform", export_transform);
// reset move values
export_min_x = Number.MAX_VALUE;
export_min_y = Number.MAX_VALUE;
}
function downloadSvg() {
prepareExport();
d3.select("svg").attr("width", d3.select("g").node().getBBox().width + snap_size)
.attr("height", d3.select("g").node().getBBox().height + snap_size);
const svgElement = document.getElementById('visualization');
let preface = '<?xml version="1.0" encoding="UTF-8"?>\r\n';
let svgBlob = new Blob([preface, svgElement.outerHTML], { type: "image/svg+xml;charset=utf-8" });
let dataUrl = URL.createObjectURL(svgBlob);
const link = document.createElement('a');
link.href = dataUrl;
link.download = 'export.svg';
link.click();
restoreAfterExport();
}
function downloadPng() {
prepareExport();
d3.select("svg").attr("width", null)
.attr("height", null);
const scaleFactor = 6;
const svgElement = document.getElementById('visualization');
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
canvas.width = (d3.select("g").node().getBBox().width + snap_size) * scaleFactor;
canvas.height = (d3.select("g").node().getBBox().height + snap_size) * scaleFactor;
// Convert the SVG to canvas
ctx.scale(scaleFactor, scaleFactor);
canvg(canvas, svgElement.outerHTML);
// Set the canvas background color to white
ctx.globalCompositeOperation = 'destination-over'
ctx.fillStyle = 'white';
ctx.fillRect(0, 0, canvas.width, canvas.height);
const dataUrl = canvas.toDataURL('image/png');
const link = document.createElement('a');
link.href = dataUrl;
link.download = 'export.png';
link.click();
restoreAfterExport();
}
</script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-geWF76RCwLtnZ8qwWowPQNguL3RmwHVBC9FhGdlKrxdiJJigb/j/68SIy3Te4Bkz" crossorigin="anonymous"></script>
<script async src="https://umami.nils.moe/script.js" data-website-id="a434cd0c-3035-40cb-abf5-351e2f1b7fc4"></script>
</body>
</html>