Skip to content

Commit 32f16d8

Browse files
authored
Added option to download SVG from demo
Added option to download SVG from demo
2 parents ec3b3c4 + d9876d7 commit 32f16d8

File tree

2 files changed

+55
-7
lines changed

2 files changed

+55
-7
lines changed

src/frontend/src/lib/Demo.svelte

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,29 @@
6565
navigator.clipboard.writeText(newUrl);
6666
window.open(newUrl, "_blank").focus();
6767
}
68+
69+
let graph: Graph;
70+
71+
function downloadString(text: string, fileType: string, fileName: string) {
72+
var blob = new Blob([text], { type: fileType });
73+
74+
var a = document.createElement("a");
75+
a.download = fileName;
76+
a.href = URL.createObjectURL(blob);
77+
a.dataset.downloadurl = [fileType, a.download, a.href].join(":");
78+
a.style.display = "none";
79+
document.body.appendChild(a);
80+
a.click();
81+
document.body.removeChild(a);
82+
setTimeout(function () {
83+
URL.revokeObjectURL(a.href);
84+
}, 1500);
85+
}
86+
87+
function saveSVG() {
88+
const svg = graph.getSVG();
89+
downloadString(svg, "image/svg+xml", "function-graph-overview.svg");
90+
}
6891
</script>
6992

7093
<main>
@@ -115,14 +138,25 @@
115138
</div>
116139
</div>
117140
<div class="graph">
118-
<div class="controls">
119-
<input type="checkbox" id="simplify" bind:checked={simplify} />
120-
<label for="simplify">Simplify</label>
141+
<div class="graph-controls">
142+
<div class="settings">
143+
<input type="checkbox" id="simplify" bind:checked={simplify} />
144+
<label for="simplify">Simplify</label>
121145

122-
<input type="checkbox" id="flatSwitch" bind:checked={flatSwitch} />
123-
<label for="flatSwitch">Flat Switch</label>
146+
<input type="checkbox" id="flatSwitch" bind:checked={flatSwitch} />
147+
<label for="flatSwitch">Flat Switch</label>
148+
</div>
149+
<div class="download">
150+
<button on:click={saveSVG}>Save SVG</button>
151+
</div>
124152
</div>
125-
<Graph {code} language={selection.language} {simplify} {flatSwitch} />
153+
<Graph
154+
{code}
155+
language={selection.language}
156+
{simplify}
157+
{flatSwitch}
158+
bind:this={graph}
159+
/>
126160
</div>
127161
</main>
128162

@@ -150,6 +184,15 @@
150184
margin-top: 1rem;
151185
margin-left: 1rem;
152186
}
187+
.graph-controls {
188+
font-family: Arial, Helvetica, sans-serif;
189+
margin-top: 1rem;
190+
margin-left: 1rem;
191+
margin-right: 1rem;
192+
193+
display: flex;
194+
justify-content: space-between;
195+
}
153196
154197
.codemirror {
155198
padding-top: 1rem;

src/frontend/src/lib/Graph.svelte

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
1414
let parsers: Parsers;
1515
let graphviz: Graphviz;
16+
let dot: string;
1617
export let code: string;
1718
export let language: Language;
1819
export let verbose: boolean = false;
@@ -49,7 +50,7 @@
4950
if (trim) cfg = trimFor(cfg);
5051
if (simplify) cfg = simplifyCFG(cfg, mergeNodeAttrs);
5152
52-
const dot = graphToDot(cfg, verbose);
53+
dot = graphToDot(cfg, verbose);
5354
5455
return graphviz.dot(dot);
5556
}
@@ -65,6 +66,10 @@
6566
return `<p style='border: 2px red solid;'>${error.toString()}</p>`;
6667
}
6768
}
69+
70+
export function getSVG() {
71+
return graphviz.dot(dot);
72+
}
6873
</script>
6974

7075
<div class="results">

0 commit comments

Comments
 (0)