| import type { WorkerProxy } from "@gradio/wasm"; |
| import { is_self_origin } from "./url"; |
| import { mount_css as default_mount_css } from "../css"; |
|
|
| |
| |
| |
| const PREBUILT_CSS_URL = new URL("./theme.css", import.meta.url).href; |
| const DYNAMIC_THEME_CSS_URL_PATH = "/theme.css"; |
|
|
| export async function mount_prebuilt_css(target: HTMLElement): Promise<void> { |
| return default_mount_css(PREBUILT_CSS_URL, target); |
| } |
|
|
| export async function wasm_proxied_mount_css( |
| worker_proxy: WorkerProxy, |
| url_string: string, |
| target: HTMLElement |
| ): Promise<void> { |
| const request = new Request(url_string); |
| const url = new URL(request.url); |
|
|
| if (!is_self_origin(url)) { |
| |
| return default_mount_css(url_string, target); |
| } |
|
|
| const response = await worker_proxy.httpRequest({ |
| method: "GET", |
| path: url.pathname, |
| query_string: "", |
| headers: {} |
| }); |
| const css = new TextDecoder().decode(response.body); |
|
|
| const existing_link = document.querySelector( |
| `style[data-wasm-path='${url_string}']` |
| ); |
| if (existing_link) return; |
|
|
| if (url.pathname === DYNAMIC_THEME_CSS_URL_PATH) { |
| console.debug( |
| "Unmount the prebuilt theme.css before mounting the dynamic theme.css" |
| ); |
| const existing_prebuilt_css = document.querySelector( |
| `link[href='${PREBUILT_CSS_URL}']` |
| ); |
| existing_prebuilt_css?.remove(); |
| } |
|
|
| const style = document.createElement("style"); |
| style.setAttribute("data-wasm-path", url_string); |
| style.textContent = css; |
| |
| target.appendChild(style); |
| } |
|
|