File size: 15,103 Bytes
6b4e089 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 | <script module lang="ts">
// Shared across instances so a component whose head script was already
// added by another instance can await that in-flight load instead of
// running js_on_load before the script has executed.
const head_script_loads = new Map<string, Promise<void>>();
</script>
<script lang="ts">
import { onDestroy } from "svelte";
import Handlebars from "handlebars";
import type { Snippet } from "svelte";
let {
elem_classes = [],
props = {},
html_template = "${value}",
css_template = "",
js_on_load = null,
head = null,
visible = true,
autoscroll = false,
apply_default_css = true,
component_class_name = "HTML",
upload = null,
server = {},
onevent,
onupdate_value,
children
}: {
// every one of these has a default in the destructure above
elem_classes?: string[];
props?: Record<string, any>;
html_template?: string;
css_template?: string;
js_on_load?: string | null;
head?: string | null;
visible?: boolean;
autoscroll?: boolean;
apply_default_css?: boolean;
component_class_name?: string;
upload?: ((file: File) => Promise<{ path: string; url: string }>) | null;
server?: Record<string, (...args: any[]) => Promise<any>>;
onevent?: (detail: { type: "click" | "submit"; data: any }) => void;
onupdate_value?: (detail: {
data: any;
property: "value" | "label" | "visible";
}) => void;
children?: Snippet;
} = $props();
let [has_children, pre_html_template, post_html_template] = $derived.by(
() => {
if (html_template.includes("@children") && children) {
const parts = html_template.split("@children");
return [true, parts[0] || "", parts.slice(1).join("@children") || ""];
}
return [false, html_template, ""];
}
);
let old_props = $state($state.snapshot(props));
type WatchEntry = { props: string[]; callback: () => void };
let watch_entries: WatchEntry[] = [];
function watch(propOrProps: string | string[], callback: () => void): void {
const prop_list = Array.isArray(propOrProps) ? propOrProps : [propOrProps];
watch_entries.push({ props: prop_list, callback });
}
function fire_watchers(changed_keys: string[]): void {
const seen = new Set<WatchEntry>();
for (const entry of watch_entries) {
if (entry.props.some((k) => changed_keys.includes(k))) {
seen.add(entry);
}
}
for (const entry of seen) {
try {
entry.callback();
} catch (e) {
console.error("Error in watch callback:", e);
}
}
}
const trigger = (
event_type: "click" | "submit",
event_data: any = {}
): void => {
onevent?.({ type: event_type, data: event_data });
};
let element: HTMLDivElement;
let pre_element: HTMLDivElement;
let post_element: HTMLDivElement;
let scrollable_parent: HTMLElement | null = null;
let random_id = `html-${Math.random().toString(36).substring(2, 11)}`;
let style_element: HTMLStyleElement | null = null;
let reactiveProps: Record<string, any> = {};
let currentHtml = $state("");
let currentPreHtml = $state("");
let currentPostHtml = $state("");
let currentCss = $state("");
let renderScheduled = $state(false);
let mounted = $state(false);
let html_error_message: string | null = $state(null);
let css_error_message: string | null = $state(null);
function get_scrollable_parent(element: HTMLElement): HTMLElement | null {
let parent = element.parentElement;
while (parent) {
const style = window.getComputedStyle(parent);
if (
style.overflow === "auto" ||
style.overflow === "scroll" ||
style.overflowY === "auto" ||
style.overflowY === "scroll"
) {
return parent;
}
parent = parent.parentElement;
}
return null;
}
function is_at_bottom(): boolean {
if (!element) return true;
if (!scrollable_parent) {
return (
window.innerHeight + window.scrollY >=
document.documentElement.scrollHeight - 100
);
}
return (
scrollable_parent.offsetHeight + scrollable_parent.scrollTop >=
scrollable_parent.scrollHeight - 100
);
}
function scroll_to_bottom(): void {
if (!element) return;
if (scrollable_parent) {
scrollable_parent.scrollTo(0, scrollable_parent.scrollHeight);
} else {
window.scrollTo(0, document.documentElement.scrollHeight);
}
}
async function scroll_on_html_update(): Promise<void> {
if (!autoscroll || !element) return;
if (!scrollable_parent) {
scrollable_parent = get_scrollable_parent(element);
}
if (is_at_bottom()) {
await new Promise((resolve) => setTimeout(resolve, 300));
scroll_to_bottom();
}
}
function render_template(
template: string,
props: Record<string, any>,
language: "html" | "css" = "html"
): string {
try {
const handlebarsTemplate = Handlebars.compile(template);
const handlebarsRendered = handlebarsTemplate(props);
const propKeys = Object.keys(props);
const propValues = Object.values(props);
const templateFunc = new Function(
...propKeys,
`return \`${handlebarsRendered}\`;`
);
if (language === "html") {
html_error_message = null;
} else if (language === "css") {
css_error_message = null;
}
return templateFunc(...propValues);
} catch (e) {
console.error("Error evaluating template:", e);
if (language === "html") {
html_error_message = e instanceof Error ? e.message : String(e);
} else if (language === "css") {
css_error_message = e instanceof Error ? e.message : String(e);
}
return "";
}
}
function update_css(): void {
if (typeof document === "undefined") return;
if (!style_element) {
style_element = document.createElement("style");
document.head.appendChild(style_element);
}
currentCss = render_template(css_template, reactiveProps, "css");
if (currentCss) {
style_element.textContent = `#${random_id} { ${currentCss} }`;
} else {
style_element.textContent = "";
}
}
onDestroy(() => style_element?.remove());
function updateDOM(
_element: HTMLElement | undefined,
oldHtml: string,
newHtml: string
): void {
if (!_element || oldHtml === newHtml) return;
const tempContainer = document.createElement("div");
tempContainer.innerHTML = newHtml;
const oldNodes = Array.from(_element.childNodes);
const newNodes = Array.from(tempContainer.childNodes);
const maxLength = Math.max(oldNodes.length, newNodes.length);
for (let i = 0; i < maxLength; i++) {
const oldNode = oldNodes[i];
const newNode = newNodes[i];
if (!oldNode && newNode) {
_element.appendChild(newNode.cloneNode(true));
} else if (oldNode && !newNode) {
_element.removeChild(oldNode);
} else if (oldNode && newNode) {
updateNode(oldNode, newNode);
}
}
}
function updateNode(oldNode: Node, newNode: Node): void {
if (
oldNode.nodeType === Node.TEXT_NODE &&
newNode.nodeType === Node.TEXT_NODE
) {
if (oldNode.textContent !== newNode.textContent) {
oldNode.textContent = newNode.textContent;
}
return;
}
if (
oldNode.nodeType === Node.ELEMENT_NODE &&
newNode.nodeType === Node.ELEMENT_NODE
) {
const oldElement = oldNode as Element;
const newElement = newNode as Element;
if (oldElement.tagName !== newElement.tagName) {
oldNode.parentNode?.replaceChild(newNode.cloneNode(true), oldNode);
return;
}
const oldAttrs = Array.from(oldElement.attributes);
const newAttrs = Array.from(newElement.attributes);
for (const attr of oldAttrs) {
if (!newElement.hasAttribute(attr.name)) {
oldElement.removeAttribute(attr.name);
}
}
for (const attr of newAttrs) {
if (oldElement.getAttribute(attr.name) !== attr.value) {
oldElement.setAttribute(attr.name, attr.value);
if (
attr.name === "value" &&
(oldElement.tagName === "INPUT" ||
oldElement.tagName === "TEXTAREA" ||
oldElement.tagName === "SELECT")
) {
(
oldElement as
| HTMLInputElement
| HTMLTextAreaElement
| HTMLSelectElement
).value = attr.value;
}
}
}
const oldChildren = Array.from(oldElement.childNodes);
const newChildren = Array.from(newElement.childNodes);
const maxChildren = Math.max(oldChildren.length, newChildren.length);
for (let i = 0; i < maxChildren; i++) {
const oldChild = oldChildren[i];
const newChild = newChildren[i];
if (!oldChild && newChild) {
oldElement.appendChild(newChild.cloneNode(true));
} else if (oldChild && !newChild) {
oldElement.removeChild(oldChild);
} else if (oldChild && newChild) {
updateNode(oldChild, newChild);
}
}
} else {
oldNode.parentNode?.replaceChild(newNode.cloneNode(true), oldNode);
}
}
function renderHTML(): void {
if (has_children) {
const newPreHtml = render_template(
pre_html_template,
reactiveProps,
"html"
);
updateDOM(pre_element, currentPreHtml, newPreHtml);
currentPreHtml = newPreHtml;
const newPostHtml = render_template(
post_html_template,
reactiveProps,
"html"
);
updateDOM(post_element, currentPostHtml, newPostHtml);
currentPostHtml = newPostHtml;
} else {
const newHtml = render_template(html_template, reactiveProps, "html");
updateDOM(element, currentHtml, newHtml);
currentHtml = newHtml;
}
if (autoscroll) {
scroll_on_html_update();
}
}
function scheduleRender(): void {
if (!renderScheduled) {
renderScheduled = true;
queueMicrotask(() => {
renderScheduled = false;
renderHTML();
update_css();
});
}
}
async function loadHead(headHtml: string): Promise<void> {
if (!headHtml) return;
const parser = new DOMParser();
const doc = parser.parseFromString(`<head>${headHtml}</head>`, "text/html");
const promises: Promise<void>[] = [];
for (const el of Array.from(doc.head.children)) {
if (el.tagName === "SCRIPT") {
const parsed_script = el as HTMLScriptElement;
const src = parsed_script.src;
if (src) {
const in_flight = head_script_loads.get(src);
if (in_flight) {
promises.push(in_flight);
continue;
}
// selector-free dedupe: author-provided src may contain `]` etc.
// that would break a querySelector string.
if (Array.from(document.scripts).some((s) => s.src === src)) continue;
const script = document.createElement("script");
// Created scripts default to force-async; copy the author's intent
// so a plain <script src> keeps document order.
script.async = (el as HTMLScriptElement).async;
script.src = src;
const load = new Promise<void>((resolve, reject) => {
script.onload = () => resolve();
script.onerror = () =>
reject(new Error(`Failed to load script: ${src}`));
});
head_script_loads.set(src, load);
promises.push(load);
document.head.appendChild(script);
} else {
if (
Array.from(document.scripts).some(
(s) => !s.src && s.textContent === parsed_script.textContent
)
) {
continue;
}
const script = document.createElement("script");
script.textContent = parsed_script.textContent;
document.head.appendChild(script);
}
} else {
// selector-free dedupe, same reason as the script[src] check above.
const href = el.tagName === "LINK" ? (el as HTMLLinkElement).href : "";
const existing = href
? Array.from(document.querySelectorAll("link")).some(
(l) => (l as HTMLLinkElement).href === href
)
: false;
if (!existing) {
document.head.appendChild(el.cloneNode(true));
}
}
}
await Promise.all(promises);
}
// Mount effect
$effect(() => {
if (!element || mounted) return;
mounted = true;
reactiveProps = new Proxy($state.snapshot(props), {
set(target, property, value) {
const oldValue = target[property as string];
target[property as string] = value;
if (oldValue !== value) {
scheduleRender();
if (
property === "value" ||
property === "label" ||
property === "visible"
) {
props[property] = value;
old_props[property] = value;
onupdate_value?.({ data: value, property });
}
}
return true;
}
});
if (has_children) {
currentPreHtml = render_template(
pre_html_template,
reactiveProps,
"html"
);
pre_element.innerHTML = currentPreHtml;
currentPostHtml = render_template(
post_html_template,
reactiveProps,
"html"
);
post_element.innerHTML = currentPostHtml;
} else {
currentHtml = render_template(html_template, reactiveProps, "html");
element.innerHTML = currentHtml;
}
update_css();
if (autoscroll) {
scroll_to_bottom();
}
scroll_on_html_update();
(async () => {
if (head) {
await loadHead(head);
}
if (js_on_load && element) {
try {
const upload_func =
upload ??
(async (_file: File): Promise<{ path: string; url: string }> => {
throw new Error("upload is not available in this context");
});
const func = new Function(
"element",
"trigger",
"props",
"server",
"upload",
"watch",
js_on_load
);
func(element, trigger, reactiveProps, server, upload_func, watch);
} catch (error) {
console.error("Error executing js_on_load:", error);
}
}
})();
});
$effect(() => {
if (
reactiveProps &&
props &&
JSON.stringify(old_props) !== JSON.stringify(props)
) {
const changedKeys: string[] = [];
for (const key in props) {
if (JSON.stringify(reactiveProps[key]) !== JSON.stringify(props[key])) {
changedKeys.push(key);
}
reactiveProps[key] = $state.snapshot(props[key]);
}
old_props = props;
if (changedKeys.length > 0) {
queueMicrotask(() => fire_watchers(changedKeys));
}
}
});
</script>
{#if html_error_message || css_error_message}
<div class="error-container">
<strong class="error-title"
>Error rendering <code class="error-component-name"
>{component_class_name}</code
>:</strong
>
<code class="error-message">{html_error_message || css_error_message}</code>
</div>
{:else}
<div
bind:this={element}
id={random_id}
class="{apply_default_css && !has_children
? 'prose gradio-style'
: ''} {elem_classes.join(' ')}"
class:hide={!visible}
class:has_children
>
{#if has_children}
<div
class={apply_default_css ? "prose gradio-style" : ""}
bind:this={pre_element}
></div>
{@render children?.()}
<div
class={apply_default_css ? "prose gradio-style" : ""}
bind:this={post_element}
></div>
{/if}
</div>
{/if}
<style>
.hide {
display: none;
}
.has_children {
display: flex;
flex-direction: column;
gap: var(--layout-gap);
}
.error-container {
padding: 12px;
background-color: #fee;
border: 1px solid #fcc;
border-radius: 4px;
color: #c33;
font-family: monospace;
font-size: 13px;
}
.error-title {
display: block;
margin-bottom: 8px;
}
.error-component-name {
background-color: #fdd;
padding: 2px 4px;
border-radius: 2px;
}
.error-message {
white-space: pre-wrap;
word-break: break-word;
}
</style>
|