2023-05-18 23:55:46 +08:00
|
|
|
<script lang="ts">
|
|
|
|
import { onDestroy } from "svelte";
|
|
|
|
import { Copy, Check } from "@gradio/icons";
|
|
|
|
|
|
|
|
let copied = false;
|
|
|
|
export let value: string;
|
|
|
|
let timer: NodeJS.Timeout;
|
|
|
|
|
2023-08-04 06:01:18 +08:00
|
|
|
function copy_feedback(): void {
|
2023-05-18 23:55:46 +08:00
|
|
|
copied = true;
|
|
|
|
if (timer) clearTimeout(timer);
|
|
|
|
timer = setTimeout(() => {
|
|
|
|
copied = false;
|
|
|
|
}, 2000);
|
|
|
|
}
|
|
|
|
|
2023-08-04 06:01:18 +08:00
|
|
|
async function handle_copy(): Promise<void> {
|
2023-05-18 23:55:46 +08:00
|
|
|
if ("clipboard" in navigator) {
|
|
|
|
await navigator.clipboard.writeText(value);
|
|
|
|
copy_feedback();
|
2023-06-01 21:51:36 +08:00
|
|
|
} else {
|
|
|
|
const textArea = document.createElement("textarea");
|
|
|
|
textArea.value = value;
|
|
|
|
|
|
|
|
textArea.style.position = "absolute";
|
|
|
|
textArea.style.left = "-999999px";
|
|
|
|
|
|
|
|
document.body.prepend(textArea);
|
|
|
|
textArea.select();
|
|
|
|
|
|
|
|
try {
|
|
|
|
document.execCommand("copy");
|
|
|
|
copy_feedback();
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
} finally {
|
|
|
|
textArea.remove();
|
|
|
|
}
|
2023-05-18 23:55:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onDestroy(() => {
|
|
|
|
if (timer) clearTimeout(timer);
|
|
|
|
});
|
|
|
|
</script>
|
|
|
|
|
2023-09-22 20:12:26 +08:00
|
|
|
<button
|
|
|
|
on:click={handle_copy}
|
|
|
|
title="copy"
|
2023-10-03 01:29:20 +08:00
|
|
|
aria-label={copied ? "Copied message" : "Copy message"}
|
2023-09-22 20:12:26 +08:00
|
|
|
>
|
2023-08-10 22:11:26 +08:00
|
|
|
{#if !copied}
|
2023-09-22 20:12:26 +08:00
|
|
|
<Copy />
|
2023-08-10 22:11:26 +08:00
|
|
|
{/if}
|
2023-05-18 23:55:46 +08:00
|
|
|
{#if copied}
|
2023-09-22 20:12:26 +08:00
|
|
|
<Check />
|
2023-05-18 23:55:46 +08:00
|
|
|
{/if}
|
|
|
|
</button>
|
|
|
|
|
|
|
|
<style>
|
|
|
|
button {
|
|
|
|
position: relative;
|
2023-08-10 22:11:26 +08:00
|
|
|
top: 0;
|
|
|
|
right: 0;
|
|
|
|
cursor: pointer;
|
2023-10-03 01:29:20 +08:00
|
|
|
color: var(--body-text-color-subdued);
|
|
|
|
margin-right: 5px;
|
|
|
|
}
|
|
|
|
|
|
|
|
button:hover {
|
|
|
|
color: var(--body-text-color);
|
2023-05-18 23:55:46 +08:00
|
|
|
}
|
|
|
|
</style>
|