1import { u as state, v as proxy, w as get, x as set, y as user_effect, z as untrack } from './index-BvBk1Iap.js'; 2import { s as snapshot } from './clone-DaB-S2nH.js'; 3import { $ as $locale } from './i18n-CMo5lpzy.js'; 4 5/* utils.svelte.ts generated by Svelte v5.48.0 */ 6 7const I18N_MARKER = "__i18n__"; 8 9const TRANSLATABLE_PROPS = [ 10 "label", 11 "info", 12 "placeholder", 13 "description", 14 "title", 15 "value" 16]; 17 18class ShareError extends Error { 19 constructor(message) { 20 super(message); 21 this.name = "ShareError"; 22 } 23} 24 25async function uploadToHuggingFace(data, type) { 26 if (window.__gradio_space__ == null) { 27 throw new ShareError("Must be on Spaces to share."); 28 } 29 30 let blob; 31 let contentType; 32 let filename; 33 34 { 35 let url; 36 37 if (typeof data === "object" && data.url) { 38 url = data.url; 39 } else if (typeof data === "string") { 40 url = data; 41 } else { 42 throw new Error("Invalid data format for URL type"); 43 } 44 45 const response = await fetch(url); 46 47 blob = await response.blob(); 48 contentType = response.headers.get("content-type") || ""; 49 filename = response.headers.get("content-disposition") || ""; 50 } 51 52 const file = new File([blob], filename, { type: contentType }); 53 54 const uploadResponse = await fetch("https://huggingface.co/uploads", { 55 method: "POST", 56 body: file, 57 headers: { 58 "Content-Type": file.type, 59 "X-Requested-With": "XMLHttpRequest" 60 } 61 }); 62 63 if (!uploadResponse.ok) { 64 if (uploadResponse.headers.get("content-type")?.includes("application/json")) { 65 const error = await uploadResponse.json(); 66 67 throw new ShareError(`Upload failed: ${error.error}`); 68 } 69 70 throw new ShareError(`Upload failed.`); 71 } 72 73 const result = await uploadResponse.text(); 74 75 return result; 76} 77 78function copy(node) { 79 node.addEventListener("click", handle_copy); 80 81 async function handle_copy(event) { 82 const path = event.composedPath(); 83 const [copy_button] = path.filter((e) => e?.tagName === "BUTTON" && e.classList.contains("copy_code_button")); 84 85 if (copy_button) { 86 let copy_feedback = function (_copy_sucess_button) { 87 _copy_sucess_button.style.opacity = "1"; 88 89 setTimeout( 90 () => { 91 _copy_sucess_button.style.opacity = "0"; 92 }, 93 2e3 94 ); 95 }; 96 97 event.stopImmediatePropagation(); 98 99 const copy_text = copy_button.parentElement.innerText.trim(); 100 const copy_sucess_button = Array.from(copy_button.children)[1]; 101 const copied = await copy_to_clipboard(copy_text); 102 103 if (copied) copy_feedback(copy_sucess_button); 104 } 105 } 106 107 return { 108 destroy() { 109 node.removeEventListener("click", handle_copy); 110 } 111 }; 112} 113 114async function copy_to_clipboard(value) { 115 let copied = false; 116 117 if ("clipboard" in navigator) { 118 await navigator.clipboard.writeText(value); 119 copied = true; 120 } else { 121 const textArea = document.createElement("textarea"); 122 123 textArea.value = value; 124 textArea.style.position = "absolute"; 125 textArea.style.left = "-999999px"; 126 document.body.prepend(textArea); 127 textArea.select(); 128 129 try { 130 document.execCommand("copy"); 131 copied = true; 132 } catch(error) { 133 console.error(error); 134 copied = false; 135 } finally { 136 textArea.remove(); 137 } 138 } 139 140 return copied; 141} 142 143const format_time = (seconds) => { 144 const hours = Math.floor(seconds / 3600); 145 const minutes = Math.floor(seconds % 3600 / 60); 146 const seconds_remainder = Math.round(seconds) % 60; 147 const padded_minutes = `${minutes < 10 ? "0" : ""}${minutes}`; 148 const padded_seconds = `${seconds_remainder < 10 ? "0" : ""}${seconds_remainder}`; 149 150 if (hours > 0) { 151 return `${hours}:${padded_minutes}:${padded_seconds}`; 152 } 153 154 return `${minutes}:${padded_seconds}`; 155}; 156 157const allowed_shared_props = [ 158 "elem_id", 159 "elem_classes", 160 "visible", 161 "interactive", 162 "server_fns", 163 "server", 164 "id", 165 "target", 166 "theme_mode", 167 "version", 168 "root", 169 "autoscroll", 170 "max_file_size", 171 "formatter", 172 "client", 173 "load_component", 174 "scale", 175 "min_width", 176 "theme", 177 "padding", 178 "loading_status", 179 "label", 180 "show_label", 181 "validation_error", 182 "show_progress", 183 "api_prefix", 184 "container", 185 "attached_events", 186 "register_component", 187 "dispatcher" 188]; 189 190function has_i18n_marker(value) { 191 return typeof value === "string" && value.includes(I18N_MARKER); 192} 193 194function translate_i18n_marker(value, translate) { 195 const start = value.indexOf(I18N_MARKER); 196 197 if (start === -1) return value; 198 199 const json_start = start + I18N_MARKER.length; 200 const json_end = value.indexOf("}", json_start) + 1; 201 202 if (json_end === 0) return value; 203 204 try { 205 const metadata = JSON.parse(value.slice(json_start, json_end)); 206 207 if (metadata?.key) { 208 const translated = translate(metadata.key); 209 const result = translated !== metadata.key ? translated : metadata.key; 210 211 return value.slice(0, start) + result + value.slice(json_end); 212 } 213 } catch {} 214 215 return value; 216} 217 218class Gradio { 219 load_component; 220 #shared = state(proxy({})); 221 222 get shared() { 223 return get(this.#shared); 224 } 225 226 set shared(value) { 227 set(this.#shared, value, true); 228 } 229 230 #props = state(proxy({})); 231 232 get props() { 233 return get(this.#props); 234 } 235 236 set props(value) { 237 set(this.#props, value, true); 238 } 239
240 #i18n = state((v) => v); 241 242 get i18n() { 243 return get(this.#i18n); 244 } 245 246 set i18n(value) { 247 set(this.#i18n, value, true); 248 } 249 250 translatable_props = {}; 251 dispatcher; 252 last_update = null; 253 shared_props = allowed_shared_props; 254 mounted = false; 255 old_value; 256 register_component; 257 258 constructor(_props, default_values) { 259 for (const key in _props.shared_props) { 260 this.shared[key] = _props.shared_props[key]; 261 } 262 263 for (const key in _props.props) { 264 this.props[key] = _props.props[key]; 265 } 266 267 if (default_values) { 268 for (const key in default_values) { 269 if (this.props[key] === void 0) { 270 this.props[key] = default_values[key]; 271 } 272 } 273 } 274 275 this.i18n = this.props.i18n ?? ((v) => v); 276 277 for (const key of TRANSLATABLE_PROPS) { 278 this.shared[key] = this._translate_and_store( 279 "shared", 280 key, 281 // @ts-ignore 282 _props.shared_props[key] 283 ); 284 285 this.props[key] = this._translate_and_store( 286 "props", 287 key, 288 // @ts-ignore 289 _props.props[key] 290 ); 291 } 292 293 this.load_component = this.shared.load_component; 294 this.register_component = this.shared.register_component || (() => {}); 295 this.dispatcher = this.shared.dispatcher || (() => {}); 296 297 this.register_component( 298 _props.shared_props.id, 299 // @ts-ignore 300 this.set_data.bind(this), 301 this.get_data.bind(this) 302 ); 303 304 user_effect(() => { 305 for (const key in _props.shared_props) { 306 if (this._is_i18n_managed(`shared.${key}`, _props.shared_props[key])) continue; 307 308 this.shared[key] = _props.shared_props[key]; 309 } 310 311 for (const key in _props.props) { 312 if (this._is_i18n_managed(`props.${key}`, _props.props[key])) continue; 313 314 this.props[key] = _props.props[key]; 315 } 316 317 this.register_component( 318 _props.shared_props.id, 319 // @ts-ignore 320 this.set_data.bind(this), 321 this.get_data.bind(this) 322 ); 323 324 untrack(() => { 325 this.shared.id = _props.shared_props.id; 326 }); 327 }); 328 329 if (Object.keys(this.translatable_props).length > 0) { 330 $locale.subscribe(() => { 331 for (const [full_key, original] of Object.entries(this.translatable_props)) { 332 const [target, key] = full_key.split("."); 333 const translated = this.i18n(original); 334 335 if (target === "shared") this.shared[key] = translated; else this.props[key] = translated; 336 } 337 }); 338 } 339 } 340 341 // check if props are translatable 342 _is_i18n_managed(key, new_value) { 343 const original_marker = this.translatable_props[key]; 344 345 if (!original_marker) return false; 346 if (new_value === original_marker) return true; 347 348 delete this.translatable_props[key]; 349 350 return false; 351 } 352 353 _translate_and_store(target, key, value) { 354 if (typeof value !== "string") return value; 355 356 const translated = this.i18n(value); 357 358 if (translated !== value) { 359 this.translatable_props[`${target}.${key}`] = value; 360 } 361 362 return translated; 363 } 364 365 dispatch(event_name, data) { 366 this.dispatcher(this.shared.id, event_name, data); 367 } 368 369 async get_data() { 370 return snapshot(this.props); 371 } 372 373 update(data) { 374 this.set_data(data); 375 } 376 377 set_data(data) { 378 for (const key in data) { 379 const value = data[key]; 380 381 const translated = has_i18n_marker(value) 382 ? this._translate_and_store(this.shared_props.includes(key) ? "shared" : "props", key, value) 383 : value; 384 385 if (this.shared_props.includes(key)) { 386 const _key = key; 387 388 this.shared[_key] = translated; 389 390 continue; 391 } 392 393 this.props[key] = translated; 394 } 395 } 396 397 watch_for_change() { 398 user_effect(() => { 399 if (!this.mounted) { 400 this.old_value = this.props.value; 401 this.mounted = true; 402 } 403 404 if (this.old_value != this.props.value) { 405 this.old_value = this.props.value; 406 this.dispatch("change"); 407 } 408 }); 409 } 410} 411 412const css_units = (dimension_value) => { 413 return typeof dimension_value === "number" ? dimension_value + "px" : dimension_value; 414}; 415 416function should_show_scroll_fade(container) { 417 if (!container) return false; 418 419 const has_overflow = container.scrollHeight > container.clientHeight; 420 const at_bottom = container.scrollTop >= container.scrollHeight - container.clientHeight - 1; 421 422 return has_overflow && !at_bottom; 423} 424 425export { Gradio as G, I18N_MARKER as I, ShareError as S, copy as a, allowed_shared_props as b, css_units as c, format_time as f, should_show_scroll_fade as s, translate_i18n_marker as t, uploadToHuggingFace as u }; 426//# sourceMappingURL=utils.svelte-CYLB_jIA.js.map
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.