|
1 | 1 | import { render, VNode } from "@commontools/html"; |
2 | | -import { Cell, effect, UI } from "@commontools/runner"; |
3 | | -import { inspectCharm, loadManager } from "./charm.ts"; |
| 2 | +import { Cell, UI } from "@commontools/runner"; |
| 3 | +import { loadManager } from "./charm.ts"; |
4 | 4 | import { CharmsController } from "@commontools/charm/ops"; |
5 | 5 | import type { CharmConfig } from "./charm.ts"; |
6 | 6 | import { getLogger } from "@commontools/utils/logger"; |
| 7 | +import { MockDoc } from "@commontools/html/utils"; |
7 | 8 |
|
8 | | -const logger = getLogger("charm-render", { level: "info", enabled: true }); |
| 9 | +const logger = getLogger("charm-render", { level: "info", enabled: false }); |
9 | 10 |
|
10 | 11 | export interface RenderOptions { |
11 | 12 | watch?: boolean; |
12 | 13 | onUpdate?: (html: string) => void; |
13 | 14 | } |
14 | 15 |
|
15 | 16 | /** |
16 | | - * Renders a charm's UI to HTML using JSDOM. |
| 17 | + * Renders a charm's UI to HTML using htmlparser2. |
17 | 18 | * Supports both static and reactive rendering with --watch mode. |
18 | 19 | */ |
19 | 20 | export async function renderCharm( |
20 | 21 | config: CharmConfig, |
21 | 22 | options: RenderOptions = {}, |
22 | 23 | ): Promise<string | (() => void)> { |
23 | | - // Dynamically import JSDOM to avoid top-level import issues |
24 | | - const { JSDOM } = await import("npm:jsdom"); |
25 | | - |
26 | | - // 1. Setup JSDOM environment |
27 | | - const dom = new JSDOM( |
| 24 | + const mock = new MockDoc( |
28 | 25 | '<!DOCTYPE html><html><body><div id="root"></div></body></html>', |
29 | 26 | ); |
30 | | - const { window } = dom; |
31 | | - |
32 | | - // Set up global DOM objects needed by the render system |
33 | | - globalThis.document = window.document; |
34 | | - globalThis.Element = window.Element; |
35 | | - globalThis.Node = window.Node; |
36 | | - globalThis.Text = window.Text; |
37 | | - globalThis.HTMLElement = window.HTMLElement; |
38 | | - globalThis.Event = window.Event; |
39 | | - globalThis.CustomEvent = window.CustomEvent; |
40 | | - globalThis.MutationObserver = window.MutationObserver; |
| 27 | + const { document, renderOptions } = mock; |
41 | 28 |
|
42 | | - try { |
43 | | - // 2. Get charm controller to access the Cell |
44 | | - const manager = await loadManager(config); |
45 | | - const charms = new CharmsController(manager); |
46 | | - const charm = await charms.get(config.charm); |
47 | | - const cell = charm.getCell(); |
| 29 | + // 2. Get charm controller to access the Cell |
| 30 | + const manager = await loadManager(config); |
| 31 | + const charms = new CharmsController(manager); |
| 32 | + const charm = await charms.get(config.charm); |
| 33 | + const cell = charm.getCell(); |
48 | 34 |
|
49 | | - // Check if charm has UI |
50 | | - const staticValue = cell.get(); |
51 | | - if (!staticValue?.[UI]) { |
52 | | - throw new Error(`Charm ${config.charm} has no UI`); |
53 | | - } |
| 35 | + // Check if charm has UI |
| 36 | + const staticValue = cell.get(); |
| 37 | + if (!staticValue?.[UI]) { |
| 38 | + throw new Error(`Charm ${config.charm} has no UI`); |
| 39 | + } |
54 | 40 |
|
55 | | - // 3. Get the root container |
56 | | - const container = window.document.getElementById("root"); |
57 | | - if (!container) { |
58 | | - throw new Error("Could not find root container"); |
59 | | - } |
| 41 | + // 3. Get the root container |
| 42 | + const container = document.getElementById("root"); |
| 43 | + if (!container) { |
| 44 | + throw new Error("Could not find root container"); |
| 45 | + } |
60 | 46 |
|
61 | | - if (options.watch) { |
62 | | - // 4a. Reactive rendering - pass the Cell directly |
63 | | - const uiCell = cell.key(UI); |
64 | | - const cancel = render(container, uiCell as Cell<VNode>); // FIXME: types |
| 47 | + if (options.watch) { |
| 48 | + // 4a. Reactive rendering - pass the Cell directly |
| 49 | + const uiCell = cell.key(UI); |
| 50 | + const cancel = render(container, uiCell as Cell<VNode>, renderOptions); // FIXME: types |
65 | 51 |
|
66 | | - // 5a. Set up monitoring for changes |
67 | | - let updateCount = 0; |
68 | | - const unsubscribe = cell.sink((value) => { |
69 | | - if (value?.[UI]) { |
70 | | - updateCount++; |
71 | | - // Wait for all runtime computations to complete |
72 | | - manager.runtime.idle().then(() => { |
73 | | - const html = container.innerHTML; |
74 | | - logger.info(() => `[Update ${updateCount}] UI changed`); |
75 | | - if (options.onUpdate) { |
76 | | - options.onUpdate(html); |
77 | | - } |
78 | | - }); |
79 | | - } |
80 | | - }); |
| 52 | + // 5a. Set up monitoring for changes |
| 53 | + let updateCount = 0; |
| 54 | + const unsubscribe = cell.sink((value) => { |
| 55 | + if (value?.[UI]) { |
| 56 | + updateCount++; |
| 57 | + // Wait for all runtime computations to complete |
| 58 | + manager.runtime.idle().then(() => { |
| 59 | + const html = container.innerHTML; |
| 60 | + logger.info(() => `[Update ${updateCount}] UI changed`); |
| 61 | + if (options.onUpdate) { |
| 62 | + options.onUpdate(html); |
| 63 | + } |
| 64 | + }); |
| 65 | + } |
| 66 | + }); |
81 | 67 |
|
82 | | - // Return cleanup function |
83 | | - return () => { |
84 | | - cancel(); |
85 | | - unsubscribe(); |
86 | | - window.close(); |
87 | | - }; |
88 | | - } else { |
89 | | - // 4b. Static rendering - render once with current value |
90 | | - const vnode = staticValue[UI]; |
91 | | - render(container, vnode as VNode); // FIXME: types |
| 68 | + // Return cleanup function |
| 69 | + return () => { |
| 70 | + cancel(); |
| 71 | + unsubscribe(); |
| 72 | + }; |
| 73 | + } else { |
| 74 | + // 4b. Static rendering - render once with current value |
| 75 | + const vnode = staticValue[UI]; |
| 76 | + render(container, vnode as VNode, renderOptions); // FIXME: types |
92 | 77 |
|
93 | | - // 5b. Return the rendered HTML |
94 | | - return container.innerHTML; |
95 | | - } |
96 | | - } finally { |
97 | | - // Clean up JSDOM only in static mode |
98 | | - if (!options.watch) { |
99 | | - window.close(); |
100 | | - } |
| 78 | + // 5b. Return the rendered HTML |
| 79 | + return container.innerHTML; |
101 | 80 | } |
102 | 81 | } |
0 commit comments