{"version":3,"file":"utils-c3e3db58.js","sources":["../../../../js/utils/src/utils.ts"],"sourcesContent":["import { type ActionReturn } from \"svelte/action\";\nexport interface SelectData {\n\tindex: number | [number, number];\n\tvalue: any;\n\tselected?: boolean;\n}\n\nexport interface LikeData {\n\tindex: number | [number, number];\n\tvalue: any;\n\tliked?: boolean;\n}\n\nexport interface ShareData {\n\tdescription: string;\n\ttitle?: string;\n}\n\nexport class ShareError extends Error {\n\tconstructor(message: string) {\n\t\tsuper(message);\n\t\tthis.name = \"ShareError\";\n\t}\n}\n\nexport async function uploadToHuggingFace(\n\tdata: string,\n\ttype: \"base64\" | \"url\"\n): Promise {\n\tif (window.__gradio_space__ == null) {\n\t\tthrow new ShareError(\"Must be on Spaces to share.\");\n\t}\n\tlet blob: Blob;\n\tlet contentType: string;\n\tlet filename: string;\n\tif (type === \"url\") {\n\t\tconst response = await fetch(data);\n\t\tblob = await response.blob();\n\t\tcontentType = response.headers.get(\"content-type\") || \"\";\n\t\tfilename = response.headers.get(\"content-disposition\") || \"\";\n\t} else {\n\t\tblob = dataURLtoBlob(data);\n\t\tcontentType = data.split(\";\")[0].split(\":\")[1];\n\t\tfilename = \"file\" + contentType.split(\"/\")[1];\n\t}\n\n\tconst file = new File([blob], filename, { type: contentType });\n\n\t// Send file to endpoint\n\tconst uploadResponse = await fetch(\"https://huggingface.co/uploads\", {\n\t\tmethod: \"POST\",\n\t\tbody: file,\n\t\theaders: {\n\t\t\t\"Content-Type\": file.type,\n\t\t\t\"X-Requested-With\": \"XMLHttpRequest\"\n\t\t}\n\t});\n\n\t// Check status of response\n\tif (!uploadResponse.ok) {\n\t\tif (\n\t\t\tuploadResponse.headers.get(\"content-type\")?.includes(\"application/json\")\n\t\t) {\n\t\t\tconst error = await uploadResponse.json();\n\t\t\tthrow new ShareError(`Upload failed: ${error.error}`);\n\t\t}\n\t\tthrow new ShareError(`Upload failed.`);\n\t}\n\n\t// Return response if needed\n\tconst result = await uploadResponse.text();\n\treturn result;\n}\n\nfunction dataURLtoBlob(dataurl: string): Blob {\n\tvar arr = dataurl.split(\",\"),\n\t\tmime = (arr[0].match(/:(.*?);/) as RegExpMatchArray)[1],\n\t\tbstr = atob(arr[1]),\n\t\tn = bstr.length,\n\t\tu8arr = new Uint8Array(n);\n\twhile (n--) {\n\t\tu8arr[n] = bstr.charCodeAt(n);\n\t}\n\treturn new Blob([u8arr], { type: mime });\n}\n\nexport function copy(node: HTMLDivElement): ActionReturn {\n\tnode.addEventListener(\"click\", handle_copy);\n\n\tasync function handle_copy(event: MouseEvent): Promise {\n\t\tconst path = event.composedPath() as HTMLButtonElement[];\n\n\t\tconst [copy_button] = path.filter(\n\t\t\t(e) => e?.tagName === \"BUTTON\" && e.classList.contains(\"copy_code_button\")\n\t\t);\n\n\t\tif (copy_button) {\n\t\t\tevent.stopImmediatePropagation();\n\n\t\t\tconst copy_text = copy_button.parentElement!.innerText.trim();\n\t\t\tconst copy_sucess_button = Array.from(\n\t\t\t\tcopy_button.children\n\t\t\t)[1] as HTMLDivElement;\n\n\t\t\tconst copied = await copy_to_clipboard(copy_text);\n\n\t\t\tif (copied) copy_feedback(copy_sucess_button);\n\n\t\t\tfunction copy_feedback(_copy_sucess_button: HTMLDivElement): void {\n\t\t\t\t_copy_sucess_button.style.opacity = \"1\";\n\t\t\t\tsetTimeout(() => {\n\t\t\t\t\t_copy_sucess_button.style.opacity = \"0\";\n\t\t\t\t}, 2000);\n\t\t\t}\n\t\t}\n\t}\n\n\treturn {\n\t\tdestroy(): void {\n\t\t\tnode.removeEventListener(\"click\", handle_copy);\n\t\t}\n\t};\n}\n\nasync function copy_to_clipboard(value: string): Promise {\n\tlet copied = false;\n\tif (\"clipboard\" in navigator) {\n\t\tawait navigator.clipboard.writeText(value);\n\t\tcopied = true;\n\t} else {\n\t\tconst textArea = document.createElement(\"textarea\");\n\t\ttextArea.value = value;\n\n\t\ttextArea.style.position = \"absolute\";\n\t\ttextArea.style.left = \"-999999px\";\n\n\t\tdocument.body.prepend(textArea);\n\t\ttextArea.select();\n\n\t\ttry {\n\t\t\tdocument.execCommand(\"copy\");\n\t\t\tcopied = true;\n\t\t} catch (error) {\n\t\t\tconsole.error(error);\n\t\t\tcopied = false;\n\t\t} finally {\n\t\t\ttextArea.remove();\n\t\t}\n\t}\n\n\treturn copied;\n}\n"],"names":["ShareError","message","uploadToHuggingFace","data","type","blob","contentType","filename","response","dataURLtoBlob","file","uploadResponse","error","dataurl","arr","mime","bstr","u8arr","copy","node","handle_copy","event","path","copy_button","e","copy_feedback","_copy_sucess_button","copy_text","copy_sucess_button","copy_to_clipboard","value","copied","textArea"],"mappings":"AAkBO,MAAMA,UAAmB,KAAM,CACrC,YAAYC,EAAiB,CAC5B,MAAMA,CAAO,EACb,KAAK,KAAO,YACb,CACD,CAEsB,eAAAC,EACrBC,EACAC,EACkB,CACd,GAAA,OAAO,kBAAoB,KACxB,MAAA,IAAIJ,EAAW,6BAA6B,EAE/C,IAAAK,EACAC,EACAC,EACJ,GAAIH,IAAS,MAAO,CACb,MAAAI,EAAW,MAAM,MAAML,CAAI,EAC1BE,EAAA,MAAMG,EAAS,OACtBF,EAAcE,EAAS,QAAQ,IAAI,cAAc,GAAK,GACtDD,EAAWC,EAAS,QAAQ,IAAI,qBAAqB,GAAK,QAE1DH,EAAOI,EAAcN,CAAI,EACXG,EAAAH,EAAK,MAAM,GAAG,EAAE,CAAC,EAAE,MAAM,GAAG,EAAE,CAAC,EAC7CI,EAAW,OAASD,EAAY,MAAM,GAAG,EAAE,CAAC,EAGvC,MAAAI,EAAO,IAAI,KAAK,CAACL,CAAI,EAAGE,EAAU,CAAE,KAAMD,CAAA,CAAa,EAGvDK,EAAiB,MAAM,MAAM,iCAAkC,CACpE,OAAQ,OACR,KAAMD,EACN,QAAS,CACR,eAAgBA,EAAK,KACrB,mBAAoB,gBACrB,CAAA,CACA,EAGG,GAAA,CAACC,EAAe,GAAI,CACvB,GACCA,EAAe,QAAQ,IAAI,cAAc,GAAG,SAAS,kBAAkB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