Skip to content

Commit 9f12ed5

Browse files
committed
The demo learned to share links
1 parent 49de287 commit 9f12ed5

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

src/frontend/src/lib/Demo.svelte

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import { python } from "@codemirror/lang-python";
66
import Graph from "./Graph.svelte";
77
import type { Language } from "../../../control-flow/cfg";
8-
8+
import * as LZString from "lz-string";
99
export let codeGo = "func Example() {\n\tif x {\n\t\treturn\n\t}\n}";
1010
export let codeC = "void main() {\n\tif (x) {\n\t\treturn;\n\t}\n}";
1111
export let codePython = "def example():\n if x:\n return";
@@ -18,7 +18,21 @@
1818
{ language: "C" as Language, text: "C" },
1919
{ language: "Python" as Language, text: "Python (experimental)" },
2020
];
21-
let selection = languages[0];
21+
22+
const urlParams = new URLSearchParams(window.location.search);
23+
if (urlParams.has("go")) {
24+
codeGo = LZString.decompressFromEncodedURIComponent(urlParams.get("go"));
25+
}
26+
if (urlParams.has("c")) {
27+
codeC = LZString.decompressFromEncodedURIComponent(urlParams.get("c"));
28+
}
29+
if (urlParams.has("python")) {
30+
codePython = LZString.decompressFromEncodedURIComponent(
31+
urlParams.get("python"),
32+
);
33+
}
34+
35+
let selection = languages[parseInt(urlParams.get("language")) || 0];
2236
let code = codeGo;
2337
$: {
2438
switch (selection.language) {
@@ -36,6 +50,21 @@
3650
3751
let simplify = true;
3852
let flatSwitch = false;
53+
54+
function share() {
55+
const compressedCode = LZString.compressToEncodedURIComponent(code);
56+
const codeName = selection.language.toLowerCase();
57+
const language = languages.findIndex((lang) => lang == selection);
58+
const query = `?language=${language}&${codeName}=${compressedCode}`;
59+
const newUrl =
60+
window.location.protocol +
61+
"//" +
62+
window.location.host +
63+
window.location.pathname +
64+
query;
65+
navigator.clipboard.writeText(newUrl);
66+
window.open(newUrl, "_blank").focus();
67+
}
3968
</script>
4069

4170
<main>
@@ -58,6 +87,7 @@
5887
</option>
5988
{/each}
6089
</select>
90+
<button on:click={share}>Share (experimental)</button>
6191
</div>
6292
<div class="codemirror">
6393
{#if selection.language === "Go"}

0 commit comments

Comments
 (0)