import type { ActionReturn } from "svelte/action"; import type { Client } from "@gradio/client"; import type { ComponentType, SvelteComponent } from "svelte"; import { tick, untrack } from "svelte"; import type { Component } from "svelte"; import { fromStore } from "svelte/store"; import type { Readable } from "svelte/store"; export const I18N_MARKER = "__i18n__"; const TRANSLATABLE_PROPS = [ "label", "info", "placeholder", "description", "title", "value" ]; export interface SharedProps { elem_id?: string; elem_classes: string[]; components?: string[]; server_fns?: string[]; interactive: boolean; visible: boolean | "hidden"; id: number; container: boolean; target: HTMLElement; theme_mode: "light" | "dark" | "system"; version: string; root: string; autoscroll: boolean; max_file_size: number | null; formatter: any; //I18nFormatter; client: Client; scale: number; min_width: number; padding: boolean; load_component: load_component; loading_status?: any; label: string; show_label: boolean; validation_error?: string | null; theme?: "light" | "dark"; show_progress: boolean; api_prefix: string; server: ServerFunctions; attached_events?: string[]; register_component: ( id: number, set_data: (data: Record & SharedProps) => void, get_data: Function ) => void; unregister_component: ( id: number, set_data?: (data: Record & SharedProps) => void ) => void; dispatcher: Function; } export type LoadingComponent = Promise<{ default: Component; }>; export const GRADIO_ROOT = "GRADIO_ROOT"; export interface ValueData { value: any; is_value_data: boolean; } export interface SelectData { id?: string | number; component_id?: string | number; row_value?: any[]; col_value?: any[]; index: number | [number, number]; value: any; selected?: boolean; } export interface LikeData { index: number | [number, number]; value: any; liked?: boolean | string; } export interface KeyUpData { key: string; input_value: string; } export interface ShareData { description: string; title?: string; } export interface CopyData { value: string; } export class ShareError extends Error { constructor(message: string) { super(message); this.name = "ShareError"; } } export async function uploadToHuggingFace( data: string | { url?: string; path?: string }, type: "base64" | "url" ): Promise { 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") { let url: string; if (typeof data === "object" && data.url) { url = data.url; } else if (typeof data === "string") { url = data; } else { throw new Error("Invalid data format for URL type"); } const response = await fetch(url); blob = await response.blob(); contentType = response.headers.get("content-type") || ""; filename = response.headers.get("content-disposition") || ""; } else { let dataurl: string; if (typeof data === "object" && data.path) { dataurl = data.path; } else if (typeof data === "string") { dataurl = data; } else { throw new Error("Invalid data format for base64 type"); } blob = dataURLtoBlob(dataurl); contentType = dataurl.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 { 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 { 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; } export const play_media = (media: HTMLMediaElement): void => { Promise.resolve(media.play()).catch((e: DOMException) => { if (e?.name === "AbortError" || e?.name === "NotAllowedError") return; console.warn("play() was rejected:", e); }); }; export const format_time = (seconds: number): string => { const hours = Math.floor(seconds / 3600); const minutes = Math.floor((seconds % 3600) / 60); const seconds_remainder = Math.round(seconds) % 60; const padded_minutes = `${minutes < 10 ? "0" : ""}${minutes}`; const padded_seconds = `${ seconds_remainder < 10 ? "0" : "" }${seconds_remainder}`; if (hours > 0) { return `${hours}:${padded_minutes}:${padded_seconds}`; } return `${minutes}:${padded_seconds}`; }; interface Args { api_url: string; name: string; id?: string; variant: "component" | "example" | "base"; } export type component_loader = (args: Args) => { component: { default: ComponentType; }; }; export type LoadedComponentWithRuntime = { component: LoadingComponent; runtime: false | typeof import("svelte"); }; export type load_component = ( name: string, variant: "component" | "example" | "base", component_class_id?: string ) => LoadedComponentWithRuntime; const is_browser = typeof window !== "undefined"; export type ServerFunctions = Record Promise>; export const allowed_shared_props: (keyof SharedProps)[] = [ "elem_id", "elem_classes", "visible", "interactive", "server_fns", "server", "id", "target", "theme_mode", "version", "root", "autoscroll", "max_file_size", "formatter", "client", "load_component", "scale", "min_width", "theme", "padding", "loading_status", "label", "show_label", "validation_error", "show_progress", "api_prefix", "container", "attached_events", "register_component", "unregister_component", "dispatcher" ] as const; export type I18nFormatter = any; export function has_i18n_marker(value: unknown): value is string { return typeof value === "string" && value.includes(I18N_MARKER); } export function translate_i18n_marker( value: string, translate: (key: string) => string ): string { const start = value.indexOf(I18N_MARKER); if (start === -1) return value; const json_start = start + I18N_MARKER.length; const json_end = value.indexOf("}", json_start) + 1; if (json_end === 0) return value; try { const metadata = JSON.parse(value.slice(json_start, json_end)); if (metadata?.key) { const translated = translate(metadata.key); const result = translated !== metadata.key ? translated : metadata.key; return value.slice(0, start) + result + value.slice(json_end); } } catch {} return value; } export class Gradio { load_component: load_component; shared: SharedProps = $state({} as SharedProps) as SharedProps; props = $state({} as U) as U; i18n: I18nFormatter = $state((v: string) => v) as any; // Live formatter store injected by @gradio/core (its canonical svelte-i18n // instance). Used purely as a reactivity trigger to re-translate props when // the locale changes. Kept off `props`/`$state` so it isn't proxied or // serialized. See Blocks.svelte where this is set. // NOTE: the underlying duplicate-svelte-i18n issue is also addressed by the // workspace resolving to a single Svelte version (pinned to 5.48.0, since // 5.56 regressed lazy-rendering); this injection remains as defense-in-depth. i18n_store: Readable | undefined; _i18n_from_store: { readonly current: unknown } | undefined; translatable_props: Record = {}; dispatcher!: Function; last_update: ReturnType | null = null; shared_props: (keyof SharedProps)[] = allowed_shared_props; mounted = false; old_value: any; register_component!: ( id: number, set_data: (data: Record & SharedProps) => void, get_data: Function ) => void; unregister_component!: ( id: number, set_data?: (data: Record & SharedProps) => void ) => void; set_data_callback!: (data: Record & SharedProps) => void; get_data_callback!: Function; registered_id: number | null = null; last_shared_props!: SharedProps; last_props!: U; constructor( _props: { shared_props: SharedProps; props: U }, default_values?: Partial ) { for (const key in _props.shared_props) { // @ts-ignore i'm not doing pointless typescript gymanstics this.shared[key] = _props.shared_props[key]; } for (const key in _props.props) { // i18n_store is a store reference kept off `props`/`$state` (see below) if (key === "i18n_store") continue; // @ts-ignore same here this.props[key] = _props.props[key]; } if (default_values) { for (const key in default_values) { if (this.props[key as keyof U] === undefined) { // @ts-ignore this.props[key] = default_values[key as keyof U]; } } } // @ts-ignore same here this.i18n = this.props.i18n ?? ((v: string) => v); // @ts-ignore - read the raw store reference before it's wrapped in $state this.i18n_store = _props.props.i18n_store; this._i18n_from_store = this.i18n_store ? fromStore(this.i18n_store) : undefined; for (const key of TRANSLATABLE_PROPS) { // @ts-ignore this.shared[key] = this._translate_and_store( "shared", key, // @ts-ignore _props.shared_props[key] ); // @ts-ignore this.props[key] = this._translate_and_store( "props", key, // @ts-ignore _props.props[key] ); } this.load_component = this.shared.load_component; this.register_component = this.shared.register_component || (() => {}); this.unregister_component = this.shared.unregister_component || (() => {}); this.dispatcher = this.shared.dispatcher || (() => {}); this.set_data_callback = this.set_data.bind(this); this.get_data_callback = this.get_data.bind(this); this.register_component( this.shared.id, // @ts-ignore this.set_data_callback, this.get_data_callback ); this.registered_id = this.shared.id; this.last_shared_props = _props.shared_props; this.last_props = _props.props; // @gr.render may reuse a Svelte component instance for a node with // a new Gradio id. Register callbacks under that id, while leaving // prop updates to set_data/app-tree sync. $effect(() => { const current_id = _props.shared_props.id; if (this.last_shared_props !== _props.shared_props) { for (const key in _props.shared_props) { // @ts-ignore const value = _props.shared_props[key]; if (value === undefined) continue; if (this._is_i18n_managed(`shared.${key}`, value)) continue; // @ts-ignore i'm not doing pointless typescript gymanstics this.shared[key] = value; } this.last_shared_props = _props.shared_props; } if (this.last_props !== _props.props) { for (const key in _props.props) { // i18n_store is a store reference kept off `props`/`$state` if (key === "i18n_store") continue; const value = _props.props[key]; if (value === undefined) continue; if (this._is_i18n_managed(`props.${key}`, value)) continue; // @ts-ignore same here this.props[key] = value; } this.last_props = _props.props; } if (this.registered_id !== null && this.registered_id !== current_id) { this.unregister_component(this.registered_id, this.set_data_callback); } this.register_component( current_id, // @ts-ignore this.set_data_callback, this.get_data_callback ); this.registered_id = current_id; untrack(() => { this.shared.id = current_id; }); }); $effect(() => { return () => { if (this.registered_id !== null) { this.unregister_component(this.registered_id, this.set_data_callback); } }; }); // Re-translate props when the locale changes at runtime. The store is // injected by @gradio/core so we subscribe to the same svelte-i18n // instance that actually receives locale updates. $effect(() => { if (!this.i18n_store) return; const unsubscribe = this.i18n_store.subscribe(() => { for (const [full_key, original] of Object.entries( this.translatable_props )) { const [target, key] = full_key.split("."); const translated = this.i18n(original); // @ts-ignore if (target === "shared") this.shared[key] = translated; // @ts-ignore else this.props[key] = translated; } }); return unsubscribe; }); } // check if props are translatable _is_i18n_managed(key: string, new_value: unknown): boolean { const original_marker = this.translatable_props[key]; if (!original_marker) return false; if (new_value === original_marker) return true; // if value has changed then remove key delete this.translatable_props[key]; return false; } _translate_and_store( target: "shared" | "props", key: string, value: unknown ): unknown { if (typeof value !== "string") return value; const translated = this.i18n(value); if (translated !== value) { this.translatable_props[`${target}.${key}`] = value; } return translated; } // Reactive variant of `i18n`. Reading it inside a derived or template // subscribes to the live locale store (via Svelte's createSubscriber), so // the caller re-runs when the locale changes at runtime. The store is only // the reactivity trigger; translation always goes through `this.i18n` so a // custom formatter passed via props keeps taking effect. live_i18n = (value: string): string => { void this._i18n_from_store?.current; return this.i18n(value); }; dispatch(event_name: E, data?: T[E]): void { this.dispatcher(this.shared.id, event_name, data); } async get_data(): Promise { return $state.snapshot(this.props); } update(data: Partial): void { this.set_data(data as U & SharedProps); } set_data(data: Partial): void { for (const key in data) { // @ts-ignore const value = data[key]; const translated = has_i18n_marker(value) ? this._translate_and_store( this.shared_props.includes(key as keyof SharedProps) ? "shared" : "props", key, value ) : value; if (this.shared_props.includes(key as keyof SharedProps)) { const _key = key as keyof SharedProps; // @ts-ignore i'm not doing pointless typescript gymanstics this.shared[_key] = translated; continue; } // @ts-ignore this.props[key] = translated; } } watch_for_change() { $effect(() => { if (!this.mounted) { // @ts-ignore this.old_value = this.props.value; this.mounted = true; } // @ts-ignore if (this.old_value != this.props.value) { // @ts-ignore this.old_value = this.props.value; // @ts-ignore this.dispatch("change"); } }); } } // function _load_component( // this: Gradio, // name: string, // variant: "component" | "example" | "base" = "component" // ): ReturnType { // return this._load_component!({ // name, // api_url: this.shared.client.config?.root!, // variant // }); // } export const css_units = (dimension_value: string | number): string => { return typeof dimension_value === "number" ? dimension_value + "px" : dimension_value; }; export function should_show_scroll_fade( container: HTMLElement | null ): boolean { if (!container) return false; const has_overflow = container.scrollHeight > container.clientHeight; const at_bottom = container.scrollTop >= container.scrollHeight - container.clientHeight - 1; return has_overflow && !at_bottom; } type MappedProps = { [K in keyof T]: T[K] }; type MappedProp = { [P in K]: T[P] };