|
5 | 5 | import { python } from "@codemirror/lang-python"; |
6 | 6 | import Graph from "./Graph.svelte"; |
7 | 7 | import type { Language } from "../../../control-flow/cfg"; |
8 | | -
|
| 8 | + import * as LZString from "lz-string"; |
9 | 9 | export let codeGo = "func Example() {\n\tif x {\n\t\treturn\n\t}\n}"; |
10 | 10 | export let codeC = "void main() {\n\tif (x) {\n\t\treturn;\n\t}\n}"; |
11 | 11 | export let codePython = "def example():\n if x:\n return"; |
|
18 | 18 | { language: "C" as Language, text: "C" }, |
19 | 19 | { language: "Python" as Language, text: "Python (experimental)" }, |
20 | 20 | ]; |
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]; |
22 | 36 | let code = codeGo; |
23 | 37 | $: { |
24 | 38 | switch (selection.language) { |
|
36 | 50 |
|
37 | 51 | let simplify = true; |
38 | 52 | 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 | + } |
39 | 68 | </script> |
40 | 69 |
|
41 | 70 | <main> |
|
58 | 87 | </option> |
59 | 88 | {/each} |
60 | 89 | </select> |
| 90 | + <button on:click={share}>Share (experimental)</button> |
61 | 91 | </div> |
62 | 92 | <div class="codemirror"> |
63 | 93 | {#if selection.language === "Go"} |
|
0 commit comments