|
import type { WorkerProxy } from "@gradio/wasm";
|
|
import { is_self_host } from "@gradio/wasm/network";
|
|
import { mount_css as default_mount_css } from "@gradio/core";
|
|
|
|
|
|
|
|
|
|
const PREBUILT_CSS_URL = new URL("./theme.css", import.meta.url).href;
|
|
const DYNAMIC_THEME_CSS_URL_PATH = "/theme.css";
|
|
|
|
export function mount_prebuilt_css(
|
|
target: Parameters<typeof default_mount_css>[1]
|
|
): 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_host(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_style = document.querySelector(
|
|
`style[data-wasm-path='${url_string}']`
|
|
);
|
|
existing_style?.remove();
|
|
|
|
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);
|
|
}
|
|
|