2023-03-11 01:52:17 +08:00
|
|
|
<script lang="ts">
|
|
|
|
import { onDestroy } from "svelte";
|
|
|
|
import { fade } from "svelte/transition";
|
|
|
|
import { Download, Check } from "@gradio/icons";
|
|
|
|
|
|
|
|
export let value: string;
|
|
|
|
export let language: string;
|
|
|
|
|
|
|
|
$: ext = get_ext_for_type(language);
|
|
|
|
|
2023-08-04 06:01:18 +08:00
|
|
|
function get_ext_for_type(type: string): string {
|
2023-03-11 01:52:17 +08:00
|
|
|
const exts: Record<string, string> = {
|
|
|
|
py: "py",
|
|
|
|
python: "py",
|
|
|
|
md: "md",
|
|
|
|
markdown: "md",
|
|
|
|
json: "json",
|
|
|
|
html: "html",
|
|
|
|
css: "css",
|
|
|
|
js: "js",
|
|
|
|
javascript: "js",
|
|
|
|
ts: "ts",
|
|
|
|
typescript: "ts",
|
|
|
|
yaml: "yaml",
|
|
|
|
yml: "yml",
|
|
|
|
dockerfile: "dockerfile",
|
|
|
|
sh: "sh",
|
|
|
|
shell: "sh",
|
|
|
|
r: "r"
|
|
|
|
};
|
|
|
|
|
|
|
|
return exts[type] || "txt";
|
|
|
|
}
|
|
|
|
|
|
|
|
let copied = false;
|
|
|
|
let timer: NodeJS.Timeout;
|
|
|
|
|
2023-08-04 06:01:18 +08:00
|
|
|
function copy_feedback(): void {
|
2023-03-11 01:52:17 +08:00
|
|
|
copied = true;
|
|
|
|
if (timer) clearTimeout(timer);
|
|
|
|
timer = setTimeout(() => {
|
|
|
|
copied = false;
|
|
|
|
}, 2000);
|
|
|
|
}
|
|
|
|
|
|
|
|
$: download_value = URL.createObjectURL(new Blob([value]));
|
|
|
|
|
|
|
|
onDestroy(() => {
|
|
|
|
if (timer) clearTimeout(timer);
|
|
|
|
});
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<a
|
|
|
|
download="file.{ext}"
|
|
|
|
href={download_value}
|
|
|
|
class:copied
|
|
|
|
on:click={copy_feedback}
|
|
|
|
>
|
|
|
|
<Download />
|
|
|
|
{#if copied}
|
|
|
|
<span class="check" transition:fade><Check /></span>
|
|
|
|
{/if}
|
|
|
|
</a>
|
|
|
|
|
|
|
|
<style>
|
|
|
|
a {
|
|
|
|
position: relative;
|
|
|
|
cursor: pointer;
|
|
|
|
padding: 5px;
|
|
|
|
|
|
|
|
width: 22px;
|
|
|
|
height: 22px;
|
|
|
|
}
|
|
|
|
|
|
|
|
.copied {
|
|
|
|
color: var(--color-green-500);
|
|
|
|
}
|
|
|
|
|
|
|
|
.check {
|
|
|
|
position: absolute;
|
|
|
|
top: 0;
|
|
|
|
right: 0;
|
|
|
|
z-index: var(--layer-top);
|
2023-03-18 08:20:55 +08:00
|
|
|
background: var(--background-fill-primary);
|
2023-03-11 01:52:17 +08:00
|
|
|
padding: var(--size-1);
|
|
|
|
width: 100%;
|
|
|
|
height: 100%;
|
|
|
|
color: var(--body-text-color);
|
|
|
|
}
|
|
|
|
</style>
|