gradio/frontend
1403k
1<script lang="ts">2 import { onDestroy } from "svelte";3 import { Download, Check } from "@gradio/icons";4 import { DownloadLink } from "@gradio/atoms";5 import { IconButton } from "@gradio/atoms";6 7 export let value: string;8 export let language: string;9 10 $: ext = get_ext_for_type(language);11 12 function get_ext_for_type(type: string): string {13 const exts: Record<string, string> = {14 py: "py",15 python: "py",16 md: "md",17 markdown: "md",18 json: "json",19 html: "html",20 css: "css",21 js: "js",22 javascript: "js",23 ts: "ts",24 typescript: "ts",25 yaml: "yaml",26 yml: "yml",27 dockerfile: "dockerfile",28 sh: "sh",29 shell: "sh",30 r: "r",31 c: "c",32 cpp: "cpp",33 latex: "tex"34 };35 36 return exts[type] || "txt";37 }38 39 let copied = false;40 let timer: NodeJS.Timeout;41 42 function copy_feedback(): void {43 copied = true;44 if (timer) clearTimeout(timer);45 timer = setTimeout(() => {46 copied = false;47 }, 2000);48 }49 50 $: download_value = URL.createObjectURL(new Blob([value]));51 52 onDestroy(() => {53 if (timer) clearTimeout(timer);54 });55</script>56 57<DownloadLink58 download="file.{ext}"59 href={download_value}60 on:click={copy_feedback}61>62 <IconButton Icon={copied ? Check : Download} />63</DownloadLink>64 