File size: 3,639 Bytes
2b06d1d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | import { type ActionReturn } from "svelte/action";
export interface SelectData {
index: number | [number, number];
value: any;
selected?: boolean;
}
export interface LikeData {
index: number | [number, number];
value: any;
liked?: boolean;
}
export interface ShareData {
description: string;
title?: string;
}
export class ShareError extends Error {
constructor(message: string) {
super(message);
this.name = "ShareError";
}
}
export async function uploadToHuggingFace(
data: string,
type: "base64" | "url"
): Promise<string> {
if (window.__gradio_space__ == null) {
throw new ShareError("Must be on Spaces to share.");
}
let blob: Blob;
let contentType: string;
let filename: string;
if (type === "url") {
const response = await fetch(data);
blob = await response.blob();
contentType = response.headers.get("content-type") || "";
filename = response.headers.get("content-disposition") || "";
} else {
blob = dataURLtoBlob(data);
contentType = data.split(";")[0].split(":")[1];
filename = "file" + contentType.split("/")[1];
}
const file = new File([blob], filename, { type: contentType });
// Send file to endpoint
const uploadResponse = await fetch("https://huggingface.co/uploads", {
method: "POST",
body: file,
headers: {
"Content-Type": file.type,
"X-Requested-With": "XMLHttpRequest"
}
});
// Check status of response
if (!uploadResponse.ok) {
if (
uploadResponse.headers.get("content-type")?.includes("application/json")
) {
const error = await uploadResponse.json();
throw new ShareError(`Upload failed: ${error.error}`);
}
throw new ShareError(`Upload failed.`);
}
// Return response if needed
const result = await uploadResponse.text();
return result;
}
function dataURLtoBlob(dataurl: string): Blob {
var arr = dataurl.split(","),
mime = (arr[0].match(/:(.*?);/) as RegExpMatchArray)[1],
bstr = atob(arr[1]),
n = bstr.length,
u8arr = new Uint8Array(n);
while (n--) {
u8arr[n] = bstr.charCodeAt(n);
}
return new Blob([u8arr], { type: mime });
}
export function copy(node: HTMLDivElement): ActionReturn {
node.addEventListener("click", handle_copy);
async function handle_copy(event: MouseEvent): Promise<void> {
const path = event.composedPath() as HTMLButtonElement[];
const [copy_button] = path.filter(
(e) => e?.tagName === "BUTTON" && e.classList.contains("copy_code_button")
);
if (copy_button) {
event.stopImmediatePropagation();
const copy_text = copy_button.parentElement!.innerText.trim();
const copy_sucess_button = Array.from(
copy_button.children
)[1] as HTMLDivElement;
const copied = await copy_to_clipboard(copy_text);
if (copied) copy_feedback(copy_sucess_button);
function copy_feedback(_copy_sucess_button: HTMLDivElement): void {
_copy_sucess_button.style.opacity = "1";
setTimeout(() => {
_copy_sucess_button.style.opacity = "0";
}, 2000);
}
}
}
return {
destroy(): void {
node.removeEventListener("click", handle_copy);
}
};
}
async function copy_to_clipboard(value: string): Promise<boolean> {
let copied = false;
if ("clipboard" in navigator) {
await navigator.clipboard.writeText(value);
copied = true;
} 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");
copied = true;
} catch (error) {
console.error(error);
copied = false;
} finally {
textArea.remove();
}
}
return copied;
}
|