-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
84 lines (79 loc) · 2.11 KB
/
index.js
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
const viz = new Viz();
const outputEl = document.getElementById("output");
let svg;
function center() {
if (!svg) {
return;
}
const vpWidth = outputEl.clientWidth;
const vpHeight = outputEl.clientHeight;
const svgWidth = svg.getSizes().width;
const svgHeight = svg.getSizes().height;
if (vpWidth > svgWidth && vpHeight > svgHeight) {
svg.resetZoom();
return;
}
let scaleDown = Math.min(vpHeight / svgHeight, vpWidth / svgWidth);
svg.zoom(scaleDown, true);
svg.pan({x: 0, y: 0});
svg.resize();
}
function render() {
if (svg) {
svg.destroy();
svg = undefined;
}
const content = document.getElementById('raw_graphviz').innerText;
const filterOut = document.getElementById('filter_out').value;
const filterIn = document.getElementById('filter_in').value;
const lines = content.split('\n');
const filteredLines = ["digraph G {"];
for (const line of lines) {
if (filterOut) {
const filterOutRegex = new RegExp(filterOut);
if (line.match(filterOutRegex)) {
continue;
}
}
if (filterIn) {
const filterInRegex = new RegExp(filterIn);
if (!line.match(filterInRegex)) {
continue;
}
}
filteredLines.push(line);
}
filteredLines.push("}");
const filteredGraphviz = filteredLines.join('\n');
viz.renderSVGElement(filteredGraphviz)
.then(function(element) {
outputEl.innerHTML = "";
outputEl.appendChild(element);
const panOptions = {
panEnabled: true,
controlIconsEnabled: true,
zoomScaleSensitivity: 0.4,
zoomEnabled: true,
minZoom: 0.3,
fit: false,
center: true,
};
svg = svgPanZoom(element, panOptions);
center();
})
.catch(error => {
// Possibly display the error
console.error(error);
console.log(filteredGraphviz);
});
}
function registerEventListeners() {
document.getElementById('filter_out').addEventListener("keydown", (event) => {
render();
});
document.getElementById('filter_in').addEventListener("keydown", (event) => {
render();
});
}
registerEventListeners();
render();