@kubuild/renderer 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs ADDED
@@ -0,0 +1,2084 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ ComponentErrorBoundary: () => ComponentErrorBoundary,
24
+ DEFAULT_BREAKPOINTS: () => DEFAULT_BREAKPOINTS,
25
+ DEFAULT_CSS_RESET: () => DEFAULT_CSS_RESET,
26
+ DEFAULT_RENDER_CONTEXT: () => DEFAULT_RENDER_CONTEXT,
27
+ DEFAULT_VIEWPORT_CONFIGS: () => DEFAULT_VIEWPORT_CONFIGS,
28
+ EditableText: () => EditableText,
29
+ HtmlEmbedView: () => HtmlEmbedView,
30
+ KubuildPreviewViewport: () => KubuildPreviewViewport,
31
+ KubuildRenderer: () => KubuildRenderer,
32
+ NodeRenderer: () => NodeRenderer,
33
+ PreviewViewportAdapter: () => PreviewViewportAdapter,
34
+ RenderContextProvider: () => RenderContextProvider,
35
+ collectStateStylesCss: () => collectStateStylesCss,
36
+ createMinimalRenderContext: () => createMinimalRenderContext,
37
+ createRenderContext: () => createRenderContext,
38
+ dispatchAction: () => dispatchAction,
39
+ isActionRegistered: () => isActionRegistered,
40
+ resolveActionPayload: () => resolveActionPayload,
41
+ resolveActionPayloadDetailed: () => resolveActionPayloadDetailed,
42
+ resolveAssetSync: () => resolveAssetSync,
43
+ resolveNodeStyles: () => resolveNodeStyles,
44
+ resolveVariable: () => resolveVariable,
45
+ resolveViewportContainerStyle: () => resolveViewportContainerStyle,
46
+ resolveViewportDimensions: () => resolveViewportDimensions,
47
+ resolveViewportFromWidth: () => resolveViewportFromWidth,
48
+ styleDefinitionToCssDeclarations: () => styleDefinitionToCssDeclarations,
49
+ transformEmbedHtml: () => transformEmbedHtml,
50
+ useRenderContext: () => useRenderContext
51
+ });
52
+ module.exports = __toCommonJS(index_exports);
53
+
54
+ // src/renderer.tsx
55
+ var import_react3 = require("react");
56
+ var import_schema3 = require("@kubuild/schema");
57
+ var import_components2 = require("@kubuild/components");
58
+
59
+ // src/render-context.tsx
60
+ var import_react = require("react");
61
+ var import_core = require("@kubuild/core");
62
+ var import_schema = require("@kubuild/schema");
63
+ var import_jsx_runtime = require("react/jsx-runtime");
64
+ var DEFAULT_RENDER_CONTEXT = Object.freeze({});
65
+ var RenderContextReact = (0, import_react.createContext)(DEFAULT_RENDER_CONTEXT);
66
+ function createRenderContext(options) {
67
+ if (!options) {
68
+ return DEFAULT_RENDER_CONTEXT;
69
+ }
70
+ const frozenVariables = options.variables ? Object.freeze({ ...options.variables }) : void 0;
71
+ return Object.freeze({
72
+ variables: frozenVariables,
73
+ ...options.assetProvider ? { assetProvider: options.assetProvider } : {},
74
+ ...options.actionRegistry ? { actionRegistry: options.actionRegistry } : {},
75
+ ...options.onDiagnostic ? { onDiagnostic: options.onDiagnostic } : {}
76
+ });
77
+ }
78
+ function createMinimalRenderContext(options) {
79
+ const assetsMap = new Map(Object.entries(options?.assets ?? {}));
80
+ const actionsMap = new Map(Object.entries(options?.actions ?? {}));
81
+ const assetProvider = {
82
+ resolve: (assetIdOrUri) => {
83
+ return assetsMap.get(assetIdOrUri) || assetIdOrUri;
84
+ }
85
+ };
86
+ const actionRegistry = {
87
+ get: (type) => actionsMap.get(type),
88
+ register: (type, handler) => {
89
+ actionsMap.set(type, handler);
90
+ },
91
+ unregister: (type) => {
92
+ actionsMap.delete(type);
93
+ }
94
+ };
95
+ return createRenderContext({
96
+ variables: options?.variables,
97
+ assetProvider,
98
+ actionRegistry,
99
+ onDiagnostic: options?.onDiagnostic
100
+ });
101
+ }
102
+ var RenderContextProvider = ({ value, children }) => {
103
+ const contextValue = (0, import_react.useMemo)(() => value || DEFAULT_RENDER_CONTEXT, [value]);
104
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(RenderContextReact.Provider, { value: contextValue, children });
105
+ };
106
+ function useRenderContext() {
107
+ return (0, import_react.useContext)(RenderContextReact);
108
+ }
109
+ function resolveAssetSync(assetProvider, assetIdOrUri) {
110
+ if (!assetProvider || !assetIdOrUri) {
111
+ return void 0;
112
+ }
113
+ try {
114
+ const result = assetProvider.resolve(assetIdOrUri);
115
+ return typeof result === "string" ? result : void 0;
116
+ } catch {
117
+ return void 0;
118
+ }
119
+ }
120
+ function resolveVariable(context, value) {
121
+ if (value === null || value === void 0) {
122
+ return value;
123
+ }
124
+ if ((0, import_schema.isVariableBinding)(value)) {
125
+ return (0, import_core.resolveBinding)(value, context).value;
126
+ }
127
+ if (typeof value === "string" && value.includes("{{")) {
128
+ return value.replace(/\{\{\s*([\w.-]+)\s*\}\}/g, (match, key) => {
129
+ const outcome = (0, import_core.resolveBinding)({ key }, context);
130
+ if (outcome.status === "resolved") {
131
+ return String(outcome.value);
132
+ }
133
+ return match;
134
+ });
135
+ }
136
+ return value;
137
+ }
138
+ function resolveActionPayloadDetailed(context, payload) {
139
+ if (!payload || typeof payload !== "object") {
140
+ return { value: payload, invalidPaths: [] };
141
+ }
142
+ const invalidPaths = [];
143
+ const resolveValueRecursively = (val, path) => {
144
+ if (val === null || val === void 0) {
145
+ return val;
146
+ }
147
+ if ((0, import_schema.isVariableBinding)(val)) {
148
+ const outcome = (0, import_core.resolveBinding)(val, context);
149
+ if (outcome.status === "empty") {
150
+ invalidPaths.push(path);
151
+ }
152
+ return outcome.value;
153
+ }
154
+ if (typeof val === "string") {
155
+ if (val.includes("{{")) {
156
+ return val.replace(/\{\{\s*([\w.-]+)\s*\}\}/g, (match, key) => {
157
+ const outcome = (0, import_core.resolveBinding)({ key }, context);
158
+ if (outcome.status !== "resolved") {
159
+ invalidPaths.push(path);
160
+ return match;
161
+ }
162
+ return String(outcome.value);
163
+ });
164
+ }
165
+ return val;
166
+ }
167
+ if (Array.isArray(val)) {
168
+ return val.map((item, index) => resolveValueRecursively(item, `${path}[${index}]`));
169
+ }
170
+ if (typeof val === "object") {
171
+ const copy = {};
172
+ for (const [k, v] of Object.entries(val)) {
173
+ copy[k] = resolveValueRecursively(v, path ? `${path}.${k}` : k);
174
+ }
175
+ return copy;
176
+ }
177
+ return val;
178
+ };
179
+ const resolved = {};
180
+ for (const [key, value] of Object.entries(payload)) {
181
+ resolved[key] = resolveValueRecursively(value, key);
182
+ }
183
+ return { value: resolved, invalidPaths };
184
+ }
185
+ function resolveActionPayload(context, payload) {
186
+ return resolveActionPayloadDetailed(context, payload).value;
187
+ }
188
+ function isActionRegistered(actionRegistry, actionType) {
189
+ if (!actionRegistry || !actionType) {
190
+ return false;
191
+ }
192
+ return Boolean(actionRegistry.get(actionType));
193
+ }
194
+ function dispatchAction(options) {
195
+ const { action, nodeId, document, context, onDiagnostic } = options;
196
+ if (!(0, import_schema.isActionBinding)(action)) {
197
+ const diagnostic = {
198
+ code: "INVALID_ACTION_PAYLOAD",
199
+ actionType: typeof action?.type === "string" ? action.type : "unknown",
200
+ nodeId,
201
+ message: `Invalid action binding on node ${nodeId || "unknown"}.`
202
+ };
203
+ onDiagnostic?.(diagnostic);
204
+ context?.onDiagnostic?.(diagnostic);
205
+ return false;
206
+ }
207
+ const handler = context?.actionRegistry?.get(action.type);
208
+ if (!handler) {
209
+ const diagnostic = {
210
+ code: "UNKNOWN_ACTION",
211
+ actionType: action.type,
212
+ nodeId,
213
+ message: `No action handler registered for action type "${action.type}".`
214
+ };
215
+ onDiagnostic?.(diagnostic);
216
+ context?.onDiagnostic?.(diagnostic);
217
+ return false;
218
+ }
219
+ const { value: resolvedPayload, invalidPaths } = resolveActionPayloadDetailed(context, action.payload);
220
+ if (invalidPaths.length > 0) {
221
+ const diagnostic = {
222
+ code: "INVALID_ACTION_BINDING",
223
+ actionType: action.type,
224
+ nodeId,
225
+ message: `Action "${action.type}" payload has unresolved binding path(s) [${invalidPaths.join(
226
+ ", "
227
+ )}] on node ${nodeId || "unknown"}; handler was not invoked.`,
228
+ invalidPaths
229
+ };
230
+ onDiagnostic?.(diagnostic);
231
+ context?.onDiagnostic?.(diagnostic);
232
+ return false;
233
+ }
234
+ const executionContext = {
235
+ nodeId,
236
+ document,
237
+ variables: context?.variables
238
+ };
239
+ try {
240
+ const result = handler(resolvedPayload, executionContext);
241
+ if (result && typeof result.catch === "function") {
242
+ result.catch((error) => {
243
+ const diagnostic = {
244
+ code: "ACTION_EXECUTION_ERROR",
245
+ actionType: action.type,
246
+ nodeId,
247
+ message: `Action "${action.type}" handler threw an asynchronous error: ${error instanceof Error ? error.message : String(error)}`,
248
+ error
249
+ };
250
+ onDiagnostic?.(diagnostic);
251
+ context?.onDiagnostic?.(diagnostic);
252
+ });
253
+ }
254
+ return true;
255
+ } catch (error) {
256
+ const diagnostic = {
257
+ code: "ACTION_EXECUTION_ERROR",
258
+ actionType: action.type,
259
+ nodeId,
260
+ message: `Action "${action.type}" handler threw a synchronous error: ${error instanceof Error ? error.message : String(error)}`,
261
+ error
262
+ };
263
+ onDiagnostic?.(diagnostic);
264
+ context?.onDiagnostic?.(diagnostic);
265
+ return false;
266
+ }
267
+ }
268
+
269
+ // src/renderer.tsx
270
+ var import_core3 = require("@kubuild/core");
271
+ var import_lucide_react = require("lucide-react");
272
+
273
+ // src/styles.ts
274
+ function resolveNodeStyles(styles, viewport = "desktop") {
275
+ if (!styles) return {};
276
+ const base = styles.base || {};
277
+ const override = styles[viewport] || {};
278
+ return { ...base, ...override };
279
+ }
280
+ function escapeCssValue(value) {
281
+ return String(value).replace(/[{};]+/g, "");
282
+ }
283
+ function escapeCssIdent(value) {
284
+ return value.replace(/["\\\]]/g, "\\$&");
285
+ }
286
+ function styleDefinitionToCssDeclarations(styleDefinition) {
287
+ if (!styleDefinition || typeof styleDefinition !== "object") return "";
288
+ const declarations = [];
289
+ for (const [key, value] of Object.entries(styleDefinition)) {
290
+ if (value === null || value === void 0 || value === "") continue;
291
+ const property = key.replace(/[A-Z]/g, (c) => `-${c.toLowerCase()}`);
292
+ declarations.push(`${property}: ${escapeCssValue(value)};`);
293
+ }
294
+ return declarations.join(" ");
295
+ }
296
+ function collectStateStylesCss(document) {
297
+ if (!document?.document) return "";
298
+ const rules = [];
299
+ const walk = (node) => {
300
+ const states = node.styles?.states;
301
+ if (states && typeof states === "object") {
302
+ for (const [state, styleDefinition] of Object.entries(states)) {
303
+ const declarations = styleDefinitionToCssDeclarations(
304
+ styleDefinition
305
+ );
306
+ if (!declarations) continue;
307
+ const safeState = /^::?[a-zA-Z-]+$/.test(state) ? state : null;
308
+ if (!safeState) continue;
309
+ rules.push(`[data-kubuild-node="${escapeCssIdent(node.id)}"]${safeState} { ${declarations} }`);
310
+ }
311
+ }
312
+ node.children?.forEach(walk);
313
+ };
314
+ walk(document.document);
315
+ return rules.join("\n");
316
+ }
317
+ var DEFAULT_CSS_RESET = `
318
+ *, *::before, *::after {
319
+ box-sizing: border-box;
320
+ }
321
+
322
+ * {
323
+ margin: 0;
324
+ }
325
+
326
+ body {
327
+ line-height: 1.5;
328
+ -webkit-font-smoothing: antialiased;
329
+ }
330
+
331
+ img, picture, video, canvas, svg {
332
+ display: block;
333
+ max-width: 100%;
334
+ }
335
+
336
+ input, button, textarea, select {
337
+ font: inherit;
338
+ }
339
+
340
+ p, h1, h2, h3, h4, h5, h6 {
341
+ overflow-wrap: break-word;
342
+ }
343
+ `.trim();
344
+
345
+ // src/error-boundary.tsx
346
+ var import_react2 = require("react");
347
+ var import_jsx_runtime2 = require("react/jsx-runtime");
348
+ var ComponentErrorBoundary = class extends import_react2.Component {
349
+ constructor(props) {
350
+ super(props);
351
+ this.state = { hasError: false };
352
+ }
353
+ static getDerivedStateFromError(error) {
354
+ return { hasError: true, error };
355
+ }
356
+ componentDidCatch(error, errorInfo) {
357
+ if (this.props.onError) {
358
+ this.props.onError(error, errorInfo);
359
+ }
360
+ }
361
+ render() {
362
+ if (this.state.hasError) {
363
+ const { nodeId, componentType, mode = "runtime" } = this.props;
364
+ const errorMessage = this.state.error?.message || "Unknown render error";
365
+ if (mode === "editor") {
366
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(
367
+ "div",
368
+ {
369
+ "data-kubuild-node": nodeId,
370
+ "data-kubuild-error": componentType,
371
+ style: {
372
+ padding: "12px 16px",
373
+ margin: "4px 0",
374
+ backgroundColor: "#fef2f2",
375
+ border: "1px solid #ef4444",
376
+ borderRadius: "6px",
377
+ color: "#b91c1c",
378
+ fontFamily: "system-ui, -apple-system, sans-serif",
379
+ fontSize: "13px",
380
+ lineHeight: "1.4"
381
+ },
382
+ children: [
383
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { style: { fontWeight: 600, marginBottom: "4px" }, children: [
384
+ "\u26A0\uFE0F Component Render Error: <",
385
+ componentType,
386
+ ">"
387
+ ] }),
388
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { style: { fontSize: "11px", color: "#7f1d1d", wordBreak: "break-all" }, children: [
389
+ "Node ID: ",
390
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("code", { children: nodeId }),
391
+ " \u2014 ",
392
+ errorMessage
393
+ ] })
394
+ ]
395
+ }
396
+ );
397
+ }
398
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
399
+ "div",
400
+ {
401
+ "data-kubuild-node": nodeId,
402
+ "data-kubuild-error": componentType,
403
+ style: { display: "none" },
404
+ "aria-hidden": "true"
405
+ }
406
+ );
407
+ }
408
+ return this.props.children;
409
+ }
410
+ };
411
+
412
+ // src/prop-resolution.ts
413
+ var import_schema2 = require("@kubuild/schema");
414
+ var import_components = require("@kubuild/components");
415
+ var import_core2 = require("@kubuild/core");
416
+ function emptyValueFor(primitiveType) {
417
+ switch (primitiveType) {
418
+ case "string":
419
+ return "";
420
+ case "number":
421
+ return 0;
422
+ case "boolean":
423
+ return false;
424
+ }
425
+ }
426
+ function resolveBindableField(node, field, definition, context, diagnostics) {
427
+ const rawValue = node.props?.[field.name];
428
+ const expectedType = (0, import_components.primitiveTypeForField)(field);
429
+ if (expectedType === void 0 || rawValue === void 0) {
430
+ return rawValue;
431
+ }
432
+ const fallbackValue = definition.defaultProps?.[field.name] ?? field.defaultValue ?? emptyValueFor(expectedType);
433
+ if ((0, import_schema2.isVariableBinding)(rawValue)) {
434
+ const outcome = (0, import_core2.resolveBinding)(rawValue, context);
435
+ if (typeof outcome.value === expectedType) {
436
+ return outcome.value;
437
+ }
438
+ diagnostics.push({
439
+ code: "INCOMPATIBLE_BINDING_TYPE",
440
+ nodeId: node.id,
441
+ propName: field.name,
442
+ expectedType,
443
+ actualType: typeof outcome.value,
444
+ message: `Prop "${field.name}" on node "${node.id}" expected a ${expectedType} but resolved binding "${rawValue.key}" produced a ${typeof outcome.value}.`
445
+ });
446
+ return fallbackValue;
447
+ }
448
+ if (expectedType === "string" && typeof rawValue === "string" && rawValue.includes("{{")) {
449
+ return resolveVariable(context, rawValue);
450
+ }
451
+ return rawValue;
452
+ }
453
+ function resolvePropsForNode(node, definition, context) {
454
+ const rawProps = node.props || {};
455
+ if (!definition || !definition.propFields || definition.propFields.length === 0) {
456
+ return { props: rawProps, diagnostics: [] };
457
+ }
458
+ const diagnostics = [];
459
+ const resolved = { ...rawProps };
460
+ for (const field of definition.propFields) {
461
+ if ((0, import_components.primitiveTypeForField)(field) === void 0) {
462
+ continue;
463
+ }
464
+ resolved[field.name] = resolveBindableField(node, field, definition, context, diagnostics);
465
+ }
466
+ return { props: resolved, diagnostics };
467
+ }
468
+
469
+ // src/renderer.tsx
470
+ var import_jsx_runtime3 = require("react/jsx-runtime");
471
+ function toPascalCase(str) {
472
+ if (!str) return "";
473
+ return str.replace(/[-_](\w)/g, (_, c) => c.toUpperCase()).replace(/^\w/, (c) => c.toUpperCase());
474
+ }
475
+ function getYouTubeId(url) {
476
+ if (!url || typeof url !== "string") return null;
477
+ const match = url.match(
478
+ /(?:youtube\.com\/(?:[^\/]+\/.+\/|(?:v|e(?:mbed)?)\/|.*[?&]v=)|youtu\.be\/)([^"&?\/\s]{11})/i
479
+ );
480
+ return match ? match[1] : null;
481
+ }
482
+ function getVimeoId(url) {
483
+ if (!url || typeof url !== "string") return null;
484
+ const match = url.match(
485
+ /(?:vimeo\.com\/(?:channels\/(?:\w+\/)?|groups\/([^\/]*)\/videos\/|album\/(\d+)\/video\/|video\/|))(\d+)/i
486
+ );
487
+ return match ? match[3] : null;
488
+ }
489
+ function aspectRatioToCss(ratio) {
490
+ if (ratio === "16:9") return "16 / 9";
491
+ if (ratio === "4:3") return "4 / 3";
492
+ if (ratio === "1:1") return "1 / 1";
493
+ if (ratio === "9:16") return "9 / 16";
494
+ if (typeof ratio === "string" && ratio !== "auto") return ratio.replace(":", " / ");
495
+ return void 0;
496
+ }
497
+ var EditableText = ({
498
+ as = "p",
499
+ id,
500
+ className,
501
+ style,
502
+ value,
503
+ isEditable,
504
+ nodeId,
505
+ onClick,
506
+ onChange,
507
+ ...rest
508
+ }) => {
509
+ const isEditingRef = (0, import_react3.useRef)(false);
510
+ const Tag = as;
511
+ if (!isEditable) {
512
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
513
+ Tag,
514
+ {
515
+ id,
516
+ className,
517
+ style,
518
+ onClick,
519
+ "data-kubuild-node": nodeId,
520
+ ...rest,
521
+ children: value
522
+ }
523
+ );
524
+ }
525
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
526
+ Tag,
527
+ {
528
+ id,
529
+ className,
530
+ style: {
531
+ ...style,
532
+ outline: "none",
533
+ cursor: "text"
534
+ },
535
+ contentEditable: true,
536
+ suppressContentEditableWarning: true,
537
+ "data-kubuild-node": nodeId,
538
+ onClick: (e) => {
539
+ onClick?.(e);
540
+ },
541
+ onFocus: () => {
542
+ isEditingRef.current = true;
543
+ },
544
+ onInput: (e) => {
545
+ const text = e.currentTarget.textContent ?? "";
546
+ onChange?.(text, false);
547
+ },
548
+ onBlur: (e) => {
549
+ isEditingRef.current = false;
550
+ const text = e.currentTarget.textContent ?? "";
551
+ onChange?.(text, true);
552
+ },
553
+ onKeyDown: (e) => {
554
+ if (e.key === "Escape") {
555
+ e.currentTarget.blur();
556
+ }
557
+ },
558
+ ...rest,
559
+ children: value
560
+ }
561
+ );
562
+ };
563
+ var useIsomorphicLayoutEffect = typeof window !== "undefined" ? import_react3.useLayoutEffect : import_react3.useEffect;
564
+ function transformEmbedHtml(rawHtml) {
565
+ if (!rawHtml) return "";
566
+ return rawHtml.replace(/<style\b([^>]*)>([\s\S]*?)<\/style>/gi, (_match, attrs, cssContent) => {
567
+ let transformedCss = cssContent;
568
+ transformedCss = transformedCss.replace(/(^|[\s,{}])body(?=[\s,{])/g, "$1:host, body");
569
+ transformedCss = transformedCss.replace(/(^|[\s,{}])html(?=[\s,{])/g, "$1:host, html");
570
+ return `<style${attrs}>
571
+ :host { display: block; }
572
+ ${transformedCss}</style>`;
573
+ });
574
+ }
575
+ var HtmlEmbedView = ({
576
+ id,
577
+ style,
578
+ onClick,
579
+ dataKubuildNode,
580
+ html,
581
+ role
582
+ }) => {
583
+ const hostRef = (0, import_react3.useRef)(null);
584
+ const shadowRootRef = (0, import_react3.useRef)(null);
585
+ const scopedHtml = (0, import_react3.useMemo)(() => transformEmbedHtml(html), [html]);
586
+ useIsomorphicLayoutEffect(() => {
587
+ const host = hostRef.current;
588
+ if (!host) return;
589
+ if (typeof host.attachShadow === "function") {
590
+ if (!shadowRootRef.current) {
591
+ if (host.shadowRoot) {
592
+ shadowRootRef.current = host.shadowRoot;
593
+ } else {
594
+ try {
595
+ shadowRootRef.current = host.attachShadow({ mode: "open" });
596
+ } catch {
597
+ shadowRootRef.current = host.shadowRoot;
598
+ }
599
+ }
600
+ }
601
+ if (shadowRootRef.current) {
602
+ shadowRootRef.current.innerHTML = scopedHtml;
603
+ return;
604
+ }
605
+ }
606
+ host.innerHTML = scopedHtml;
607
+ }, [scopedHtml]);
608
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
609
+ "div",
610
+ {
611
+ ref: hostRef,
612
+ id,
613
+ style,
614
+ onClick,
615
+ "data-kubuild-node": dataKubuildNode,
616
+ role,
617
+ children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
618
+ "template",
619
+ {
620
+ shadowrootmode: "open",
621
+ dangerouslySetInnerHTML: { __html: scopedHtml }
622
+ }
623
+ )
624
+ }
625
+ );
626
+ };
627
+ function NodeRenderer({
628
+ node,
629
+ document,
630
+ registry,
631
+ context: propContext,
632
+ viewport = "desktop",
633
+ mode = "runtime",
634
+ onNodeClick,
635
+ onDiagnostic,
636
+ onActionDispatch,
637
+ onNodePropChange,
638
+ instanceSuffix = ""
639
+ }) {
640
+ const context = propContext || DEFAULT_RENDER_CONTEXT;
641
+ const styles = resolveNodeStyles(node.styles, viewport);
642
+ const props = node.props || {};
643
+ const definition = registry.get(node.type);
644
+ const domId = instanceSuffix ? `${node.id}${instanceSuffix}` : node.id;
645
+ const { props: resolvedProps, diagnostics } = resolvePropsForNode(node, definition, context);
646
+ diagnostics.forEach((diagnostic) => {
647
+ onDiagnostic?.(diagnostic);
648
+ context?.onDiagnostic?.(diagnostic);
649
+ });
650
+ const handleClick = (e) => {
651
+ e.stopPropagation();
652
+ if (onNodeClick) {
653
+ onNodeClick(node.id, e);
654
+ }
655
+ if (props.action && !props.disabled) {
656
+ dispatchAction({
657
+ action: props.action,
658
+ nodeId: node.id,
659
+ document,
660
+ context,
661
+ onDiagnostic
662
+ });
663
+ if (onActionDispatch && (0, import_schema3.isActionBinding)(props.action)) {
664
+ onActionDispatch(props.action.type, resolveActionPayload(context, props.action.payload), node.id);
665
+ }
666
+ }
667
+ };
668
+ const childrenElements = node.children?.map((child) => /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
669
+ NodeRenderer,
670
+ {
671
+ node: child,
672
+ document,
673
+ registry,
674
+ context,
675
+ viewport,
676
+ mode,
677
+ onNodeClick,
678
+ onDiagnostic,
679
+ onActionDispatch,
680
+ onNodePropChange,
681
+ instanceSuffix
682
+ },
683
+ `${child.id}${instanceSuffix}`
684
+ ));
685
+ const renderNodeContent = () => {
686
+ if (definition?.renderer && typeof definition.renderer === "function") {
687
+ const CustomRenderer = definition.renderer;
688
+ try {
689
+ if (typeof CustomRenderer === "function" && !CustomRenderer.prototype?.isReactComponent) {
690
+ return CustomRenderer({
691
+ node,
692
+ document,
693
+ props: resolvedProps,
694
+ styles,
695
+ context,
696
+ children: childrenElements,
697
+ onClick: handleClick
698
+ });
699
+ }
700
+ } catch (err) {
701
+ throw err;
702
+ }
703
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
704
+ CustomRenderer,
705
+ {
706
+ node,
707
+ document,
708
+ props: resolvedProps,
709
+ styles,
710
+ context,
711
+ onClick: handleClick,
712
+ children: childrenElements
713
+ }
714
+ );
715
+ }
716
+ switch (node.type) {
717
+ case "page":
718
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { id: domId, style: styles, onClick: handleClick, "data-kubuild-node": node.id, children: childrenElements });
719
+ case "section":
720
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
721
+ "section",
722
+ {
723
+ id: domId,
724
+ style: styles,
725
+ onClick: handleClick,
726
+ "data-kubuild-node": node.id,
727
+ "aria-label": typeof resolvedProps.ariaLabel === "string" ? resolvedProps.ariaLabel : void 0,
728
+ role: typeof resolvedProps.role === "string" ? resolvedProps.role : void 0,
729
+ children: childrenElements
730
+ }
731
+ );
732
+ case "container":
733
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
734
+ "div",
735
+ {
736
+ id: domId,
737
+ style: styles,
738
+ onClick: handleClick,
739
+ "data-kubuild-node": node.id,
740
+ role: typeof resolvedProps.role === "string" ? resolvedProps.role : void 0,
741
+ children: childrenElements
742
+ }
743
+ );
744
+ case "columns":
745
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
746
+ "div",
747
+ {
748
+ id: domId,
749
+ style: styles,
750
+ onClick: handleClick,
751
+ "data-kubuild-node": node.id,
752
+ role: typeof resolvedProps.role === "string" ? resolvedProps.role : void 0,
753
+ children: childrenElements
754
+ }
755
+ );
756
+ case "heading": {
757
+ const rawLevel = typeof resolvedProps.level === "number" ? resolvedProps.level : typeof props.level === "number" ? props.level : 2;
758
+ const clampedLevel = Math.min(Math.max(rawLevel, 1), 6);
759
+ const text = String(resolvedProps.text ?? "");
760
+ const Tag = `h${clampedLevel}`;
761
+ const isEditable = mode === "editor" && !(0, import_schema3.isVariableBinding)(props.text);
762
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
763
+ EditableText,
764
+ {
765
+ as: Tag,
766
+ id: domId,
767
+ style: styles,
768
+ value: text,
769
+ isEditable,
770
+ nodeId: node.id,
771
+ onClick: handleClick,
772
+ onChange: (val, isBlur) => onNodePropChange?.(node.id, "text", val, isBlur),
773
+ "aria-label": typeof resolvedProps.ariaLabel === "string" ? resolvedProps.ariaLabel : void 0
774
+ }
775
+ );
776
+ }
777
+ case "text": {
778
+ const content2 = String(resolvedProps.content ?? "");
779
+ const isEditable = mode === "editor" && !(0, import_schema3.isVariableBinding)(props.content);
780
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
781
+ EditableText,
782
+ {
783
+ as: "p",
784
+ id: domId,
785
+ style: styles,
786
+ value: content2,
787
+ isEditable,
788
+ nodeId: node.id,
789
+ onClick: handleClick,
790
+ onChange: (val, isBlur) => onNodePropChange?.(node.id, "content", val, isBlur),
791
+ role: typeof resolvedProps.role === "string" ? resolvedProps.role : void 0
792
+ }
793
+ );
794
+ }
795
+ case "paragraph": {
796
+ const text = String(resolvedProps.text ?? resolvedProps.content ?? props.text ?? props.content ?? "");
797
+ const isEditable = mode === "editor" && !(0, import_schema3.isVariableBinding)(props.text) && !(0, import_schema3.isVariableBinding)(props.content);
798
+ const propName = props.content !== void 0 ? "content" : "text";
799
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
800
+ EditableText,
801
+ {
802
+ as: "p",
803
+ id: domId,
804
+ style: styles,
805
+ value: text,
806
+ isEditable,
807
+ nodeId: node.id,
808
+ onClick: handleClick,
809
+ onChange: (val, isBlur) => onNodePropChange?.(node.id, propName, val, isBlur),
810
+ role: typeof resolvedProps.role === "string" ? resolvedProps.role : void 0
811
+ }
812
+ );
813
+ }
814
+ case "link": {
815
+ const text = String(resolvedProps.text ?? props.text ?? "");
816
+ const rawHref = typeof resolvedProps.href === "string" ? resolvedProps.href : typeof props.href === "string" ? props.href : void 0;
817
+ const href = rawHref ? (0, import_core3.sanitizeUrl)(rawHref, "#") : "#";
818
+ const rawTarget = typeof resolvedProps.target === "string" ? resolvedProps.target : void 0;
819
+ const rawRel = typeof resolvedProps.rel === "string" ? resolvedProps.rel : void 0;
820
+ const rel = rawTarget === "_blank" && !rawRel ? "noopener noreferrer" : rawRel;
821
+ const isEditable = mode === "editor" && !(0, import_schema3.isVariableBinding)(props.text);
822
+ if (isEditable) {
823
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
824
+ EditableText,
825
+ {
826
+ as: "a",
827
+ id: domId,
828
+ href: void 0,
829
+ target: rawTarget,
830
+ rel,
831
+ style: styles,
832
+ value: text,
833
+ isEditable,
834
+ nodeId: node.id,
835
+ onClick: handleClick,
836
+ onChange: (val, isBlur) => onNodePropChange?.(node.id, "text", val, isBlur),
837
+ role: typeof resolvedProps.role === "string" ? resolvedProps.role : void 0
838
+ }
839
+ );
840
+ }
841
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
842
+ "a",
843
+ {
844
+ id: domId,
845
+ href,
846
+ target: rawTarget,
847
+ rel,
848
+ style: styles,
849
+ onClick: handleClick,
850
+ "data-kubuild-node": node.id,
851
+ role: typeof resolvedProps.role === "string" ? resolvedProps.role : void 0,
852
+ children: text
853
+ }
854
+ );
855
+ }
856
+ case "blockquote": {
857
+ const text = resolvedProps.text !== void 0 ? String(resolvedProps.text) : props.text !== void 0 ? String(props.text) : "";
858
+ const rawCite = typeof resolvedProps.cite === "string" ? resolvedProps.cite : typeof props.cite === "string" ? props.cite : void 0;
859
+ const safeCite = rawCite ? (0, import_core3.sanitizeUrl)(rawCite) : void 0;
860
+ const hasChildren = Boolean(childrenElements && childrenElements.length > 0);
861
+ const isEditable = mode === "editor" && !(0, import_schema3.isVariableBinding)(props.text);
862
+ if (hasChildren) {
863
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
864
+ "blockquote",
865
+ {
866
+ id: domId,
867
+ cite: safeCite,
868
+ style: styles,
869
+ onClick: handleClick,
870
+ "data-kubuild-node": node.id,
871
+ role: typeof resolvedProps.role === "string" ? resolvedProps.role : void 0,
872
+ children: [
873
+ text ? isEditable ? /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
874
+ EditableText,
875
+ {
876
+ as: "p",
877
+ value: text,
878
+ isEditable,
879
+ nodeId: node.id,
880
+ onClick: handleClick,
881
+ onChange: (val, isBlur) => onNodePropChange?.(node.id, "text", val, isBlur)
882
+ }
883
+ ) : /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("p", { children: text }) : null,
884
+ childrenElements
885
+ ]
886
+ }
887
+ );
888
+ }
889
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
890
+ EditableText,
891
+ {
892
+ as: "blockquote",
893
+ id: domId,
894
+ cite: safeCite,
895
+ style: styles,
896
+ value: text,
897
+ isEditable,
898
+ nodeId: node.id,
899
+ onClick: handleClick,
900
+ onChange: (val, isBlur) => onNodePropChange?.(node.id, "text", val, isBlur),
901
+ role: typeof resolvedProps.role === "string" ? resolvedProps.role : void 0
902
+ }
903
+ );
904
+ }
905
+ case "badge": {
906
+ const text = String(resolvedProps.text ?? props.text ?? "Badge");
907
+ const variant = typeof resolvedProps.variant === "string" ? resolvedProps.variant : "default";
908
+ const isEditable = mode === "editor" && !(0, import_schema3.isVariableBinding)(props.text);
909
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
910
+ EditableText,
911
+ {
912
+ as: "span",
913
+ id: domId,
914
+ style: styles,
915
+ value: text,
916
+ isEditable,
917
+ nodeId: node.id,
918
+ "data-variant": variant,
919
+ onClick: handleClick,
920
+ onChange: (val, isBlur) => onNodePropChange?.(node.id, "text", val, isBlur),
921
+ role: typeof resolvedProps.role === "string" ? resolvedProps.role : void 0
922
+ }
923
+ );
924
+ }
925
+ case "code-block": {
926
+ const code = String(resolvedProps.code ?? props.code ?? "");
927
+ const language = typeof resolvedProps.language === "string" ? resolvedProps.language : typeof props.language === "string" ? props.language : void 0;
928
+ const isEditable = mode === "editor" && !(0, import_schema3.isVariableBinding)(props.code);
929
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
930
+ "pre",
931
+ {
932
+ id: domId,
933
+ style: styles,
934
+ onClick: handleClick,
935
+ "data-kubuild-node": node.id,
936
+ "data-language": language,
937
+ role: typeof resolvedProps.role === "string" ? resolvedProps.role : void 0,
938
+ children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
939
+ EditableText,
940
+ {
941
+ as: "code",
942
+ className: language ? `language-${language}` : void 0,
943
+ style: { fontFamily: "inherit", color: "inherit", display: "block", whiteSpace: "pre" },
944
+ value: code,
945
+ isEditable,
946
+ nodeId: node.id,
947
+ onClick: handleClick,
948
+ onChange: (val, isBlur) => onNodePropChange?.(node.id, "code", val, isBlur)
949
+ }
950
+ )
951
+ }
952
+ );
953
+ }
954
+ case "list": {
955
+ const rawTag = resolvedProps.tag;
956
+ const Tag = rawTag === "ol" ? "ol" : "ul";
957
+ const rawListStyle = resolvedProps.listStyleType;
958
+ const listStyleType = rawListStyle === "custom-icon" ? "none" : rawListStyle;
959
+ const listStyles = {
960
+ ...styles,
961
+ ...listStyleType ? { listStyleType } : {}
962
+ };
963
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
964
+ Tag,
965
+ {
966
+ id: domId,
967
+ style: listStyles,
968
+ onClick: handleClick,
969
+ "data-kubuild-node": node.id,
970
+ "data-list-style": rawListStyle,
971
+ role: typeof resolvedProps.role === "string" ? resolvedProps.role : void 0,
972
+ children: childrenElements
973
+ }
974
+ );
975
+ }
976
+ case "list-item": {
977
+ const text = resolvedProps.text !== void 0 ? String(resolvedProps.text) : "";
978
+ const hasChildren = Boolean(childrenElements && childrenElements.length > 0);
979
+ const isEditable = mode === "editor" && !(0, import_schema3.isVariableBinding)(props.text);
980
+ if (hasChildren) {
981
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
982
+ "li",
983
+ {
984
+ id: domId,
985
+ style: styles,
986
+ onClick: handleClick,
987
+ "data-kubuild-node": node.id,
988
+ role: typeof resolvedProps.role === "string" ? resolvedProps.role : void 0,
989
+ children: [
990
+ text ? isEditable ? /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
991
+ EditableText,
992
+ {
993
+ as: "span",
994
+ value: text,
995
+ isEditable,
996
+ nodeId: node.id,
997
+ onClick: handleClick,
998
+ onChange: (val, isBlur) => onNodePropChange?.(node.id, "text", val, isBlur)
999
+ }
1000
+ ) : /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { children: text }) : null,
1001
+ childrenElements
1002
+ ]
1003
+ }
1004
+ );
1005
+ }
1006
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
1007
+ EditableText,
1008
+ {
1009
+ as: "li",
1010
+ id: domId,
1011
+ style: styles,
1012
+ value: text,
1013
+ isEditable,
1014
+ nodeId: node.id,
1015
+ onClick: handleClick,
1016
+ onChange: (val, isBlur) => onNodePropChange?.(node.id, "text", val, isBlur),
1017
+ role: typeof resolvedProps.role === "string" ? resolvedProps.role : void 0
1018
+ }
1019
+ );
1020
+ }
1021
+ case "table": {
1022
+ const isStriped = resolvedProps.striped === true;
1023
+ const isBordered = resolvedProps.bordered !== false;
1024
+ const isCompact = resolvedProps.compact === true;
1025
+ const tableStyles = {
1026
+ width: "100%",
1027
+ borderCollapse: "collapse",
1028
+ ...styles,
1029
+ ...isBordered ? { border: styles.border || "1px solid #e2e8f0" } : {}
1030
+ };
1031
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
1032
+ "table",
1033
+ {
1034
+ id: domId,
1035
+ style: tableStyles,
1036
+ onClick: handleClick,
1037
+ "data-kubuild-node": node.id,
1038
+ "data-striped": isStriped ? "true" : void 0,
1039
+ "data-bordered": isBordered ? "true" : void 0,
1040
+ "data-compact": isCompact ? "true" : void 0,
1041
+ role: typeof resolvedProps.role === "string" ? resolvedProps.role : void 0,
1042
+ children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("tbody", { children: childrenElements })
1043
+ }
1044
+ );
1045
+ }
1046
+ case "table-row": {
1047
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
1048
+ "tr",
1049
+ {
1050
+ id: domId,
1051
+ style: styles,
1052
+ onClick: handleClick,
1053
+ "data-kubuild-node": node.id,
1054
+ role: typeof resolvedProps.role === "string" ? resolvedProps.role : void 0,
1055
+ children: childrenElements
1056
+ }
1057
+ );
1058
+ }
1059
+ case "table-cell": {
1060
+ const rawTag = resolvedProps.tag;
1061
+ const Tag = rawTag === "th" ? "th" : "td";
1062
+ const colSpan = typeof resolvedProps.colSpan === "number" && resolvedProps.colSpan > 1 ? resolvedProps.colSpan : void 0;
1063
+ const rowSpan = typeof resolvedProps.rowSpan === "number" && resolvedProps.rowSpan > 1 ? resolvedProps.rowSpan : void 0;
1064
+ const text = resolvedProps.text !== void 0 ? String(resolvedProps.text) : "";
1065
+ const hasChildren = Boolean(childrenElements && childrenElements.length > 0);
1066
+ const isEditable = mode === "editor" && !(0, import_schema3.isVariableBinding)(props.text);
1067
+ const cellStyles = {
1068
+ padding: "8px 12px",
1069
+ border: "1px solid #e2e8f0",
1070
+ textAlign: "left",
1071
+ ...styles,
1072
+ ...Tag === "th" ? {
1073
+ fontWeight: styles.fontWeight || "600",
1074
+ backgroundColor: styles.backgroundColor || "#f8fafc"
1075
+ } : {}
1076
+ };
1077
+ if (hasChildren) {
1078
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
1079
+ Tag,
1080
+ {
1081
+ id: domId,
1082
+ colSpan,
1083
+ rowSpan,
1084
+ style: cellStyles,
1085
+ onClick: handleClick,
1086
+ "data-kubuild-node": node.id,
1087
+ role: typeof resolvedProps.role === "string" ? resolvedProps.role : void 0,
1088
+ children: [
1089
+ text ? isEditable ? /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
1090
+ EditableText,
1091
+ {
1092
+ as: "span",
1093
+ value: text,
1094
+ isEditable,
1095
+ nodeId: node.id,
1096
+ onClick: handleClick,
1097
+ onChange: (val, isBlur) => onNodePropChange?.(node.id, "text", val, isBlur)
1098
+ }
1099
+ ) : /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { children: text }) : null,
1100
+ childrenElements
1101
+ ]
1102
+ }
1103
+ );
1104
+ }
1105
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
1106
+ EditableText,
1107
+ {
1108
+ as: Tag,
1109
+ id: domId,
1110
+ colSpan,
1111
+ rowSpan,
1112
+ style: cellStyles,
1113
+ value: text,
1114
+ isEditable,
1115
+ nodeId: node.id,
1116
+ onClick: handleClick,
1117
+ onChange: (val, isBlur) => onNodePropChange?.(node.id, "text", val, isBlur),
1118
+ role: typeof resolvedProps.role === "string" ? resolvedProps.role : void 0
1119
+ }
1120
+ );
1121
+ }
1122
+ case "image": {
1123
+ const rawSrc = typeof resolvedProps.src === "string" && resolvedProps.src.length > 0 ? resolvedProps.src : void 0;
1124
+ const directSrc = rawSrc ?? void 0;
1125
+ const asset = (0, import_schema3.isAssetReference)(props.asset) ? props.asset : void 0;
1126
+ const resolvedSrc = !directSrc && asset && context?.assetProvider ? resolveAssetSync(context.assetProvider, asset.assetId) : void 0;
1127
+ const fallbackSrc = asset?.fallbackUrl;
1128
+ const rawUrl = directSrc || resolvedSrc || fallbackSrc;
1129
+ const safeSrc = rawUrl ? (0, import_core3.sanitizeUrl)(rawUrl, "", { allowBlobMedia: true }) : void 0;
1130
+ const alt = typeof resolvedProps.alt === "string" ? resolvedProps.alt : "";
1131
+ const loading = resolvedProps.loading === "eager" ? "eager" : "lazy";
1132
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
1133
+ "img",
1134
+ {
1135
+ id: domId,
1136
+ src: safeSrc || void 0,
1137
+ alt,
1138
+ role: alt.length === 0 ? "presentation" : void 0,
1139
+ loading,
1140
+ width: resolvedProps.width,
1141
+ height: resolvedProps.height,
1142
+ style: styles,
1143
+ onClick: handleClick,
1144
+ "data-kubuild-node": node.id
1145
+ }
1146
+ );
1147
+ }
1148
+ case "video": {
1149
+ const rawSrc = typeof resolvedProps.src === "string" ? resolvedProps.src : typeof props.src === "string" ? props.src : "";
1150
+ const provider = typeof resolvedProps.provider === "string" ? resolvedProps.provider : typeof props.provider === "string" ? props.provider : "auto";
1151
+ const rawPoster = typeof resolvedProps.poster === "string" ? resolvedProps.poster : typeof props.poster === "string" ? props.poster : void 0;
1152
+ const poster = rawPoster ? (0, import_core3.sanitizeUrl)(rawPoster) : void 0;
1153
+ const controls = resolvedProps.controls !== false && props.controls !== false;
1154
+ const autoplay = resolvedProps.autoplay === true || props.autoplay === true;
1155
+ const loop = resolvedProps.loop === true || props.loop === true;
1156
+ const muted = resolvedProps.muted === true || props.muted === true;
1157
+ const aspectRatio = aspectRatioToCss(resolvedProps.aspectRatio ?? props.aspectRatio ?? "16:9");
1158
+ const ytId = provider === "youtube" ? getYouTubeId(rawSrc) || rawSrc : provider === "auto" ? getYouTubeId(rawSrc) : null;
1159
+ const vimeoId = provider === "vimeo" ? getVimeoId(rawSrc) || rawSrc : provider === "auto" ? getVimeoId(rawSrc) : null;
1160
+ const videoStyles = {
1161
+ width: "100%",
1162
+ maxWidth: "100%",
1163
+ display: "block",
1164
+ ...aspectRatio ? { aspectRatio } : {},
1165
+ ...styles
1166
+ };
1167
+ if (ytId) {
1168
+ const autoplayParam = autoplay ? "1" : "0";
1169
+ const loopParam = loop ? `1&playlist=${ytId}` : "0";
1170
+ const muteParam = muted ? "1" : "0";
1171
+ const controlsParam = controls ? "1" : "0";
1172
+ const embedUrl = `https://www.youtube.com/embed/${ytId}?autoplay=${autoplayParam}&loop=${loopParam}&mute=${muteParam}&controls=${controlsParam}`;
1173
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
1174
+ "div",
1175
+ {
1176
+ id: domId,
1177
+ style: { ...videoStyles, position: "relative", overflow: "hidden" },
1178
+ onClick: handleClick,
1179
+ "data-kubuild-node": node.id,
1180
+ "data-video-provider": "youtube",
1181
+ role: typeof resolvedProps.role === "string" ? resolvedProps.role : void 0,
1182
+ children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
1183
+ "iframe",
1184
+ {
1185
+ src: embedUrl,
1186
+ title: "YouTube video",
1187
+ style: { width: "100%", height: "100%", border: "none", minHeight: "240px" },
1188
+ allow: "accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share",
1189
+ allowFullScreen: true
1190
+ }
1191
+ )
1192
+ }
1193
+ );
1194
+ }
1195
+ if (vimeoId) {
1196
+ const autoplayParam = autoplay ? "1" : "0";
1197
+ const loopParam = loop ? "1" : "0";
1198
+ const muteParam = muted ? "1" : "0";
1199
+ const embedUrl = `https://player.vimeo.com/video/${vimeoId}?autoplay=${autoplayParam}&loop=${loopParam}&muted=${muteParam}`;
1200
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
1201
+ "div",
1202
+ {
1203
+ id: domId,
1204
+ style: { ...videoStyles, position: "relative", overflow: "hidden" },
1205
+ onClick: handleClick,
1206
+ "data-kubuild-node": node.id,
1207
+ "data-video-provider": "vimeo",
1208
+ role: typeof resolvedProps.role === "string" ? resolvedProps.role : void 0,
1209
+ children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
1210
+ "iframe",
1211
+ {
1212
+ src: embedUrl,
1213
+ title: "Vimeo video",
1214
+ style: { width: "100%", height: "100%", border: "none", minHeight: "240px" },
1215
+ allow: "autoplay; fullscreen; picture-in-picture",
1216
+ allowFullScreen: true
1217
+ }
1218
+ )
1219
+ }
1220
+ );
1221
+ }
1222
+ const safeSrc = (0, import_core3.sanitizeUrl)(rawSrc, "", { allowBlobMedia: true });
1223
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
1224
+ "video",
1225
+ {
1226
+ id: domId,
1227
+ src: safeSrc || void 0,
1228
+ poster: poster || void 0,
1229
+ controls,
1230
+ autoPlay: autoplay,
1231
+ loop,
1232
+ muted,
1233
+ style: videoStyles,
1234
+ onClick: handleClick,
1235
+ "data-kubuild-node": node.id,
1236
+ "data-video-provider": "html5",
1237
+ playsInline: true,
1238
+ role: typeof resolvedProps.role === "string" ? resolvedProps.role : void 0
1239
+ }
1240
+ );
1241
+ }
1242
+ case "icon": {
1243
+ const name = typeof resolvedProps.name === "string" && resolvedProps.name.trim().length > 0 ? resolvedProps.name.trim() : typeof props.name === "string" && props.name.trim().length > 0 ? props.name.trim() : "star";
1244
+ const size = typeof resolvedProps.size === "number" ? resolvedProps.size : typeof props.size === "number" ? props.size : 24;
1245
+ const color = typeof resolvedProps.color === "string" ? resolvedProps.color : typeof props.color === "string" ? props.color : "currentColor";
1246
+ const strokeWidth = typeof resolvedProps.strokeWidth === "number" ? resolvedProps.strokeWidth : typeof props.strokeWidth === "number" ? props.strokeWidth : 2;
1247
+ const pascalName = toPascalCase(name);
1248
+ const IconComponent = import_lucide_react.icons[pascalName];
1249
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
1250
+ "span",
1251
+ {
1252
+ id: domId,
1253
+ style: { display: "inline-flex", alignItems: "center", justifyContent: "center", color, ...styles },
1254
+ onClick: handleClick,
1255
+ "data-kubuild-node": node.id,
1256
+ "data-icon-name": name,
1257
+ role: typeof resolvedProps.role === "string" ? resolvedProps.role : "img",
1258
+ "aria-label": typeof resolvedProps.ariaLabel === "string" ? resolvedProps.ariaLabel : name,
1259
+ children: IconComponent ? /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(IconComponent, { size, color, strokeWidth }) : /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("svg", { width: size, height: size, viewBox: "0 0 24 24", fill: "none", stroke: color, strokeWidth, strokeLinecap: "round", strokeLinejoin: "round", children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("polygon", { points: "12 2 15.09 8.26 22 9.27 17 14.14 18.18 21.02 12 17.77 5.82 21.02 7 14.14 2 9.27 8.91 8.26 12 2" }) })
1260
+ }
1261
+ );
1262
+ }
1263
+ case "html-embed": {
1264
+ const rawHtml = typeof resolvedProps.html === "string" ? resolvedProps.html : typeof props.html === "string" ? props.html : "";
1265
+ const sanitized = (0, import_core3.sanitizeHtml)(rawHtml);
1266
+ if (mode === "editor" && !rawHtml.trim()) {
1267
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
1268
+ "div",
1269
+ {
1270
+ id: domId,
1271
+ style: {
1272
+ padding: "16px",
1273
+ border: "2px dashed #94a3b8",
1274
+ borderRadius: "8px",
1275
+ backgroundColor: "#f8fafc",
1276
+ color: "#64748b",
1277
+ textAlign: "center",
1278
+ fontSize: "13px",
1279
+ fontFamily: "sans-serif",
1280
+ ...styles
1281
+ },
1282
+ onClick: handleClick,
1283
+ "data-kubuild-node": node.id,
1284
+ children: [
1285
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { style: { fontWeight: 600 }, children: "</> HTML Embed" }),
1286
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { style: { fontSize: "11px", marginTop: "4px" }, children: "Click to configure HTML code in Inspector Panel" })
1287
+ ]
1288
+ }
1289
+ );
1290
+ }
1291
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
1292
+ HtmlEmbedView,
1293
+ {
1294
+ id: domId,
1295
+ style: styles,
1296
+ onClick: handleClick,
1297
+ dataKubuildNode: node.id,
1298
+ html: sanitized,
1299
+ role: typeof resolvedProps.role === "string" ? resolvedProps.role : void 0
1300
+ }
1301
+ );
1302
+ }
1303
+ case "button": {
1304
+ const label = String(resolvedProps.label ?? "Button");
1305
+ const disabled = resolvedProps.disabled === true;
1306
+ const rawHref = typeof resolvedProps.href === "string" ? resolvedProps.href : void 0;
1307
+ const href = rawHref ? (0, import_core3.sanitizeUrl)(rawHref, "#") : void 0;
1308
+ const rawTarget = typeof resolvedProps.target === "string" ? resolvedProps.target : void 0;
1309
+ const rawRel = typeof resolvedProps.rel === "string" ? resolvedProps.rel : void 0;
1310
+ const rel = rawTarget === "_blank" && !rawRel ? "noopener noreferrer" : rawRel;
1311
+ const action = (0, import_schema3.isActionBinding)(props.action) ? props.action : void 0;
1312
+ const actionResolved = action ? isActionRegistered(context?.actionRegistry, action.type) : void 0;
1313
+ const actionAttrs = action ? { "data-kubuild-action": action.type, "data-kubuild-action-resolved": actionResolved } : {};
1314
+ const ariaLabel = typeof resolvedProps.ariaLabel === "string" ? resolvedProps.ariaLabel : void 0;
1315
+ const isEditable = mode === "editor" && !(0, import_schema3.isVariableBinding)(props.label);
1316
+ const rawButtonType = typeof resolvedProps.buttonType === "string" ? resolvedProps.buttonType : "button";
1317
+ const buttonType = rawButtonType === "submit" || rawButtonType === "reset" ? rawButtonType : "button";
1318
+ if (href && !disabled) {
1319
+ if (isEditable) {
1320
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
1321
+ EditableText,
1322
+ {
1323
+ as: "a",
1324
+ id: domId,
1325
+ href: mode === "editor" ? void 0 : href,
1326
+ target: rawTarget,
1327
+ rel,
1328
+ style: styles,
1329
+ value: label,
1330
+ isEditable,
1331
+ nodeId: node.id,
1332
+ onClick: handleClick,
1333
+ onChange: (val, isBlur) => onNodePropChange?.(node.id, "label", val, isBlur),
1334
+ "aria-label": ariaLabel,
1335
+ tabIndex: 0,
1336
+ ...actionAttrs
1337
+ }
1338
+ );
1339
+ }
1340
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
1341
+ "a",
1342
+ {
1343
+ id: domId,
1344
+ href,
1345
+ target: rawTarget,
1346
+ rel,
1347
+ style: styles,
1348
+ onClick: handleClick,
1349
+ "data-kubuild-node": node.id,
1350
+ "aria-label": ariaLabel,
1351
+ tabIndex: 0,
1352
+ ...actionAttrs,
1353
+ children: label
1354
+ }
1355
+ );
1356
+ }
1357
+ if (isEditable) {
1358
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
1359
+ EditableText,
1360
+ {
1361
+ as: "button",
1362
+ id: domId,
1363
+ type: mode === "editor" ? "button" : buttonType,
1364
+ disabled,
1365
+ "aria-disabled": disabled ? true : void 0,
1366
+ "aria-label": ariaLabel,
1367
+ tabIndex: disabled ? -1 : 0,
1368
+ style: styles,
1369
+ value: label,
1370
+ isEditable,
1371
+ nodeId: node.id,
1372
+ onClick: disabled ? void 0 : handleClick,
1373
+ onChange: (val, isBlur) => onNodePropChange?.(node.id, "label", val, isBlur),
1374
+ ...actionAttrs
1375
+ }
1376
+ );
1377
+ }
1378
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
1379
+ "button",
1380
+ {
1381
+ id: domId,
1382
+ type: mode === "editor" ? "button" : buttonType,
1383
+ disabled,
1384
+ "aria-disabled": disabled ? true : void 0,
1385
+ "aria-label": ariaLabel,
1386
+ tabIndex: disabled ? -1 : 0,
1387
+ style: styles,
1388
+ onClick: disabled ? void 0 : handleClick,
1389
+ "data-kubuild-node": node.id,
1390
+ ...actionAttrs,
1391
+ children: label
1392
+ }
1393
+ );
1394
+ }
1395
+ case "form": {
1396
+ const action = typeof resolvedProps.action === "string" ? resolvedProps.action : void 0;
1397
+ const method = typeof resolvedProps.method === "string" ? resolvedProps.method : "POST";
1398
+ const target = typeof resolvedProps.target === "string" ? resolvedProps.target : void 0;
1399
+ const autoComplete = typeof resolvedProps.autoComplete === "string" ? resolvedProps.autoComplete : void 0;
1400
+ const name = typeof resolvedProps.name === "string" ? resolvedProps.name : void 0;
1401
+ const handleSubmit = (e) => {
1402
+ if (mode === "editor") {
1403
+ e.preventDefault();
1404
+ }
1405
+ };
1406
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
1407
+ "form",
1408
+ {
1409
+ id: domId,
1410
+ name,
1411
+ action: action && mode !== "editor" ? (0, import_core3.sanitizeUrl)(action, "") : void 0,
1412
+ method,
1413
+ target,
1414
+ autoComplete,
1415
+ style: styles,
1416
+ onClick: handleClick,
1417
+ onSubmit: handleSubmit,
1418
+ "data-kubuild-node": node.id,
1419
+ role: "form",
1420
+ "aria-label": name,
1421
+ children: childrenElements
1422
+ }
1423
+ );
1424
+ }
1425
+ case "input": {
1426
+ const name = typeof resolvedProps.name === "string" ? resolvedProps.name : void 0;
1427
+ const inputType = typeof resolvedProps.type === "string" ? resolvedProps.type : "text";
1428
+ const placeholder = typeof resolvedProps.placeholder === "string" ? resolvedProps.placeholder : void 0;
1429
+ const defaultValue = resolvedProps.defaultValue !== void 0 ? String(resolvedProps.defaultValue) : void 0;
1430
+ const required = resolvedProps.required === true;
1431
+ const disabled = resolvedProps.disabled === true;
1432
+ const readOnly = resolvedProps.readOnly === true;
1433
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
1434
+ "input",
1435
+ {
1436
+ id: domId,
1437
+ type: inputType,
1438
+ name,
1439
+ placeholder,
1440
+ defaultValue,
1441
+ required,
1442
+ disabled,
1443
+ readOnly,
1444
+ style: styles,
1445
+ onClick: handleClick,
1446
+ "data-kubuild-node": node.id
1447
+ }
1448
+ );
1449
+ }
1450
+ case "textarea": {
1451
+ const name = typeof resolvedProps.name === "string" ? resolvedProps.name : void 0;
1452
+ const placeholder = typeof resolvedProps.placeholder === "string" ? resolvedProps.placeholder : void 0;
1453
+ const defaultValue = resolvedProps.defaultValue !== void 0 ? String(resolvedProps.defaultValue) : void 0;
1454
+ const rows = typeof resolvedProps.rows === "number" ? resolvedProps.rows : 4;
1455
+ const required = resolvedProps.required === true;
1456
+ const disabled = resolvedProps.disabled === true;
1457
+ const readOnly = resolvedProps.readOnly === true;
1458
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
1459
+ "textarea",
1460
+ {
1461
+ id: domId,
1462
+ name,
1463
+ placeholder,
1464
+ defaultValue,
1465
+ rows,
1466
+ required,
1467
+ disabled,
1468
+ readOnly,
1469
+ style: styles,
1470
+ onClick: handleClick,
1471
+ "data-kubuild-node": node.id
1472
+ }
1473
+ );
1474
+ }
1475
+ case "select": {
1476
+ const name = typeof resolvedProps.name === "string" ? resolvedProps.name : void 0;
1477
+ const placeholder = typeof resolvedProps.placeholder === "string" ? resolvedProps.placeholder : void 0;
1478
+ const defaultValue = resolvedProps.defaultValue !== void 0 ? String(resolvedProps.defaultValue) : void 0;
1479
+ const required = resolvedProps.required === true;
1480
+ const disabled = resolvedProps.disabled === true;
1481
+ let optionsList = [];
1482
+ const rawOptions = resolvedProps.options ?? props.options;
1483
+ if (Array.isArray(rawOptions)) {
1484
+ optionsList = rawOptions.map((opt) => {
1485
+ if (typeof opt === "object" && opt !== null) {
1486
+ const record = opt;
1487
+ return {
1488
+ label: String(record.label ?? record.value ?? ""),
1489
+ value: String(record.value ?? record.label ?? "")
1490
+ };
1491
+ }
1492
+ return { label: String(opt), value: String(opt) };
1493
+ });
1494
+ } else if (typeof rawOptions === "string") {
1495
+ try {
1496
+ const parsed = JSON.parse(rawOptions);
1497
+ if (Array.isArray(parsed)) {
1498
+ optionsList = parsed.map((opt) => {
1499
+ if (typeof opt === "object" && opt !== null) {
1500
+ const record = opt;
1501
+ return {
1502
+ label: String(record.label ?? record.value ?? ""),
1503
+ value: String(record.value ?? record.label ?? "")
1504
+ };
1505
+ }
1506
+ return { label: String(opt), value: String(opt) };
1507
+ });
1508
+ }
1509
+ } catch {
1510
+ }
1511
+ }
1512
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
1513
+ "select",
1514
+ {
1515
+ id: domId,
1516
+ name,
1517
+ defaultValue,
1518
+ required,
1519
+ disabled,
1520
+ style: styles,
1521
+ onClick: handleClick,
1522
+ "data-kubuild-node": node.id,
1523
+ children: [
1524
+ placeholder && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("option", { value: "", disabled: required, children: placeholder }),
1525
+ optionsList.map((opt, idx) => /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("option", { value: opt.value, children: opt.label }, `${opt.value}-${idx}`))
1526
+ ]
1527
+ }
1528
+ );
1529
+ }
1530
+ case "checkbox": {
1531
+ const name = typeof resolvedProps.name === "string" ? resolvedProps.name : void 0;
1532
+ const label = String(resolvedProps.label ?? "Checkbox");
1533
+ const value = resolvedProps.value !== void 0 ? String(resolvedProps.value) : "yes";
1534
+ const defaultChecked = resolvedProps.defaultChecked === true;
1535
+ const required = resolvedProps.required === true;
1536
+ const disabled = resolvedProps.disabled === true;
1537
+ const isEditable = mode === "editor" && !(0, import_schema3.isVariableBinding)(props.label);
1538
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
1539
+ "label",
1540
+ {
1541
+ id: domId,
1542
+ style: styles,
1543
+ onClick: handleClick,
1544
+ "data-kubuild-node": node.id,
1545
+ children: [
1546
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
1547
+ "input",
1548
+ {
1549
+ type: "checkbox",
1550
+ name,
1551
+ value,
1552
+ defaultChecked,
1553
+ required,
1554
+ disabled,
1555
+ style: { cursor: disabled ? "not-allowed" : "pointer" }
1556
+ }
1557
+ ),
1558
+ isEditable ? /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
1559
+ EditableText,
1560
+ {
1561
+ as: "span",
1562
+ value: label,
1563
+ isEditable,
1564
+ nodeId: node.id,
1565
+ onChange: (val, isBlur) => onNodePropChange?.(node.id, "label", val, isBlur)
1566
+ }
1567
+ ) : /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { children: label })
1568
+ ]
1569
+ }
1570
+ );
1571
+ }
1572
+ case "radio": {
1573
+ const name = typeof resolvedProps.name === "string" ? resolvedProps.name : void 0;
1574
+ const label = String(resolvedProps.label ?? "Radio");
1575
+ const value = resolvedProps.value !== void 0 ? String(resolvedProps.value) : "option";
1576
+ const defaultChecked = resolvedProps.defaultChecked === true;
1577
+ const required = resolvedProps.required === true;
1578
+ const disabled = resolvedProps.disabled === true;
1579
+ const isEditable = mode === "editor" && !(0, import_schema3.isVariableBinding)(props.label);
1580
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
1581
+ "label",
1582
+ {
1583
+ id: domId,
1584
+ style: styles,
1585
+ onClick: handleClick,
1586
+ "data-kubuild-node": node.id,
1587
+ children: [
1588
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
1589
+ "input",
1590
+ {
1591
+ type: "radio",
1592
+ name,
1593
+ value,
1594
+ defaultChecked,
1595
+ required,
1596
+ disabled,
1597
+ style: { cursor: disabled ? "not-allowed" : "pointer" }
1598
+ }
1599
+ ),
1600
+ isEditable ? /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
1601
+ EditableText,
1602
+ {
1603
+ as: "span",
1604
+ value: label,
1605
+ isEditable,
1606
+ nodeId: node.id,
1607
+ onChange: (val, isBlur) => onNodePropChange?.(node.id, "label", val, isBlur)
1608
+ }
1609
+ ) : /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { children: label })
1610
+ ]
1611
+ }
1612
+ );
1613
+ }
1614
+ case "collection": {
1615
+ const sourceKey = typeof props.sourceKey === "string" ? props.sourceKey : void 0;
1616
+ const itemAlias = typeof props.itemAlias === "string" && props.itemAlias.length > 0 ? props.itemAlias : "item";
1617
+ const indexKey = `${itemAlias}Index`;
1618
+ const sourceValue = sourceKey ? (0, import_core3.resolveBinding)({ key: sourceKey }, context).value : void 0;
1619
+ if (!Array.isArray(sourceValue)) {
1620
+ const collectionDiagnostic = {
1621
+ code: "INVALID_COLLECTION_SOURCE",
1622
+ nodeId: node.id,
1623
+ propName: "sourceKey",
1624
+ message: `Collection node "${node.id}" expected an array at variable path "${sourceKey ?? "(missing sourceKey)"}" but found ${sourceValue === void 0 ? "nothing" : typeof sourceValue}.`
1625
+ };
1626
+ onDiagnostic?.(collectionDiagnostic);
1627
+ context?.onDiagnostic?.(collectionDiagnostic);
1628
+ if (mode === "editor") {
1629
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
1630
+ "div",
1631
+ {
1632
+ id: domId,
1633
+ "data-kubuild-node": node.id,
1634
+ "data-kubuild-collection-invalid": node.type,
1635
+ style: {
1636
+ ...styles,
1637
+ border: "2px dashed #f59e0b",
1638
+ backgroundColor: "#fffbeb",
1639
+ color: "#92400e",
1640
+ padding: "12px",
1641
+ borderRadius: "6px",
1642
+ fontFamily: "system-ui, -apple-system, sans-serif"
1643
+ },
1644
+ onClick: handleClick,
1645
+ children: /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("div", { style: { fontWeight: 600, fontSize: "13px" }, children: [
1646
+ "\u{1F4E6} Collection: expected an array at ",
1647
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("code", { children: sourceKey ?? "(missing sourceKey)" })
1648
+ ] })
1649
+ }
1650
+ );
1651
+ }
1652
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { id: domId, "data-kubuild-node": node.id, style: styles, onClick: handleClick, "aria-hidden": "true" });
1653
+ }
1654
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { id: domId, "data-kubuild-node": node.id, style: styles, onClick: handleClick, children: sourceValue.map((item, index) => {
1655
+ const childContext = {
1656
+ ...context,
1657
+ variables: { ...context?.variables ?? {}, [itemAlias]: item, [indexKey]: index }
1658
+ };
1659
+ const itemSuffix = `${instanceSuffix}--${index}`;
1660
+ return node.children?.map((child) => /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
1661
+ NodeRenderer,
1662
+ {
1663
+ node: child,
1664
+ document,
1665
+ registry,
1666
+ context: childContext,
1667
+ viewport,
1668
+ mode,
1669
+ onNodeClick,
1670
+ onDiagnostic,
1671
+ onActionDispatch,
1672
+ onNodePropChange,
1673
+ instanceSuffix: itemSuffix
1674
+ },
1675
+ `${child.id}${itemSuffix}`
1676
+ ));
1677
+ }) });
1678
+ }
1679
+ default:
1680
+ if (mode === "editor") {
1681
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
1682
+ "div",
1683
+ {
1684
+ id: domId,
1685
+ "data-kubuild-node": node.id,
1686
+ "data-kubuild-unknown": node.type,
1687
+ style: {
1688
+ ...styles,
1689
+ border: "2px dashed #f59e0b",
1690
+ backgroundColor: "#fffbeb",
1691
+ color: "#92400e",
1692
+ padding: "12px",
1693
+ borderRadius: "6px",
1694
+ fontFamily: "system-ui, -apple-system, sans-serif"
1695
+ },
1696
+ onClick: handleClick,
1697
+ children: [
1698
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("div", { style: { fontWeight: 600, fontSize: "13px", marginBottom: "4px" }, children: [
1699
+ "\u{1F9E9} Unknown Component: ",
1700
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("code", { children: node.type })
1701
+ ] }),
1702
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("div", { style: { fontSize: "11px", color: "#b45309", marginBottom: childrenElements ? "8px" : 0 }, children: [
1703
+ "Node ID: ",
1704
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("code", { children: node.id })
1705
+ ] }),
1706
+ childrenElements
1707
+ ]
1708
+ }
1709
+ );
1710
+ }
1711
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
1712
+ "div",
1713
+ {
1714
+ id: domId,
1715
+ "data-kubuild-node": node.id,
1716
+ "data-kubuild-unknown": node.type,
1717
+ style: styles,
1718
+ onClick: handleClick,
1719
+ children: childrenElements
1720
+ }
1721
+ );
1722
+ }
1723
+ };
1724
+ let content;
1725
+ try {
1726
+ content = renderNodeContent();
1727
+ } catch (error) {
1728
+ if (mode === "editor") {
1729
+ content = /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
1730
+ "div",
1731
+ {
1732
+ "data-kubuild-node": node.id,
1733
+ "data-kubuild-error": node.type,
1734
+ style: {
1735
+ padding: "12px 16px",
1736
+ margin: "4px 0",
1737
+ backgroundColor: "#fef2f2",
1738
+ border: "1px solid #ef4444",
1739
+ borderRadius: "6px",
1740
+ color: "#b91c1c",
1741
+ fontFamily: "system-ui, -apple-system, sans-serif",
1742
+ fontSize: "13px",
1743
+ lineHeight: "1.4"
1744
+ },
1745
+ children: [
1746
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("div", { style: { fontWeight: 600, marginBottom: "4px" }, children: [
1747
+ "\u26A0\uFE0F Component Render Error: <",
1748
+ node.type,
1749
+ ">"
1750
+ ] }),
1751
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("div", { style: { fontSize: "11px", color: "#7f1d1d", wordBreak: "break-all" }, children: [
1752
+ "Node ID: ",
1753
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("code", { children: node.id }),
1754
+ " \u2014 ",
1755
+ error instanceof Error ? error.message : String(error)
1756
+ ] })
1757
+ ]
1758
+ }
1759
+ );
1760
+ } else {
1761
+ content = /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
1762
+ "div",
1763
+ {
1764
+ "data-kubuild-node": node.id,
1765
+ "data-kubuild-error": node.type,
1766
+ style: { display: "none" },
1767
+ "aria-hidden": "true"
1768
+ }
1769
+ );
1770
+ }
1771
+ }
1772
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(ComponentErrorBoundary, { nodeId: node.id, componentType: node.type, mode, children: content });
1773
+ }
1774
+ var KubuildRenderer = ({
1775
+ document,
1776
+ registry = (0, import_components2.createDefaultComponentRegistry)(),
1777
+ context,
1778
+ viewport = "desktop",
1779
+ mode = "runtime",
1780
+ className,
1781
+ onNodeClick,
1782
+ onDiagnostic,
1783
+ onActionDispatch,
1784
+ onNodePropChange
1785
+ }) => {
1786
+ if (!document || !document.document) {
1787
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { className, children: "Empty Document" });
1788
+ }
1789
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(RenderContextProvider, { value: context, children: /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("div", { className: `kubuild-canvas-root ${className || ""}`, children: [
1790
+ (() => {
1791
+ const css = collectStateStylesCss(document);
1792
+ return css ? /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("style", { "data-kubuild-state-styles": true, children: css }) : null;
1793
+ })(),
1794
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
1795
+ NodeRenderer,
1796
+ {
1797
+ node: document.document,
1798
+ document,
1799
+ registry,
1800
+ context,
1801
+ viewport,
1802
+ mode,
1803
+ onNodeClick,
1804
+ onDiagnostic,
1805
+ onActionDispatch,
1806
+ onNodePropChange
1807
+ }
1808
+ )
1809
+ ] }) });
1810
+ };
1811
+
1812
+ // src/preview-adapter.tsx
1813
+ var import_react4 = require("react");
1814
+ var import_jsx_runtime4 = require("react/jsx-runtime");
1815
+ var DEFAULT_VIEWPORT_CONFIGS = Object.freeze({
1816
+ desktop: {
1817
+ width: "100%",
1818
+ maxWidth: "1280px",
1819
+ minHeight: "600px",
1820
+ label: "Desktop (1280px)",
1821
+ isFluid: true
1822
+ },
1823
+ tablet: {
1824
+ width: "768px",
1825
+ height: "1024px",
1826
+ minHeight: "600px",
1827
+ label: "Tablet (768 \xD7 1024)",
1828
+ isFluid: false
1829
+ },
1830
+ mobile: {
1831
+ width: "375px",
1832
+ height: "667px",
1833
+ minHeight: "500px",
1834
+ label: "Mobile (375 \xD7 667)",
1835
+ isFluid: false
1836
+ }
1837
+ });
1838
+ var DEFAULT_BREAKPOINTS = Object.freeze({
1839
+ mobile: 480,
1840
+ tablet: 768,
1841
+ desktop: 1024
1842
+ });
1843
+ function resolveViewportFromWidth(width, breakpoints) {
1844
+ const resolved = { ...DEFAULT_BREAKPOINTS, ...breakpoints };
1845
+ if (width <= resolved.mobile) {
1846
+ return "mobile";
1847
+ }
1848
+ if (width <= resolved.tablet) {
1849
+ return "tablet";
1850
+ }
1851
+ return "desktop";
1852
+ }
1853
+ function resolveViewportDimensions(viewport, customConfigs) {
1854
+ const baseConfig = DEFAULT_VIEWPORT_CONFIGS[viewport] || DEFAULT_VIEWPORT_CONFIGS.desktop;
1855
+ const custom = customConfigs?.[viewport];
1856
+ return { ...baseConfig, ...custom };
1857
+ }
1858
+ function resolveViewportContainerStyle(viewport, customConfigs, customScale) {
1859
+ const config = resolveViewportDimensions(viewport, customConfigs);
1860
+ const scale = customScale ?? config.scale ?? 1;
1861
+ const toCssVal = (v) => typeof v === "number" ? `${v}px` : v;
1862
+ const style = {
1863
+ width: toCssVal(config.width),
1864
+ maxWidth: toCssVal(config.maxWidth),
1865
+ minWidth: toCssVal(config.minWidth),
1866
+ height: toCssVal(config.height),
1867
+ minHeight: toCssVal(config.minHeight),
1868
+ maxHeight: toCssVal(config.maxHeight),
1869
+ aspectRatio: config.aspectRatio,
1870
+ transition: "width 0.2s ease, max-width 0.2s ease, height 0.2s ease"
1871
+ };
1872
+ if (scale !== 1) {
1873
+ style.transform = `scale(${scale})`;
1874
+ style.transformOrigin = "top center";
1875
+ }
1876
+ return style;
1877
+ }
1878
+ var PreviewViewportAdapter = ({
1879
+ document,
1880
+ viewport = "desktop",
1881
+ onViewportChange,
1882
+ viewportConfigs,
1883
+ breakpoints,
1884
+ registry,
1885
+ context,
1886
+ mode = "runtime",
1887
+ showChrome = false,
1888
+ chromeTitle,
1889
+ editorOverlay,
1890
+ scale,
1891
+ className,
1892
+ style,
1893
+ canvasClassName,
1894
+ canvasStyle,
1895
+ onNodeClick,
1896
+ onDiagnostic,
1897
+ onActionDispatch
1898
+ }) => {
1899
+ const currentConfig = (0, import_react4.useMemo)(
1900
+ () => resolveViewportDimensions(viewport, viewportConfigs),
1901
+ [viewport, viewportConfigs]
1902
+ );
1903
+ const containerStyle = (0, import_react4.useMemo)(
1904
+ () => resolveViewportContainerStyle(viewport, viewportConfigs, scale),
1905
+ [viewport, viewportConfigs, scale]
1906
+ );
1907
+ const mergedCanvasStyle = (0, import_react4.useMemo)(
1908
+ () => ({
1909
+ ...containerStyle,
1910
+ ...canvasStyle,
1911
+ position: "relative",
1912
+ boxSizing: "border-box"
1913
+ }),
1914
+ [containerStyle, canvasStyle]
1915
+ );
1916
+ return /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(
1917
+ "div",
1918
+ {
1919
+ "data-kubuild-preview-container": true,
1920
+ "data-viewport": viewport,
1921
+ className: `kubuild-preview-viewport-adapter ${className || ""}`,
1922
+ style: {
1923
+ display: "flex",
1924
+ flexDirection: "column",
1925
+ alignItems: "center",
1926
+ justifyContent: "flex-start",
1927
+ width: "100%",
1928
+ height: "100%",
1929
+ boxSizing: "border-box",
1930
+ ...style
1931
+ },
1932
+ children: [
1933
+ showChrome && /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(
1934
+ "div",
1935
+ {
1936
+ "data-kubuild-preview-chrome": true,
1937
+ style: {
1938
+ display: "flex",
1939
+ alignItems: "center",
1940
+ justifyContent: "space-between",
1941
+ width: "100%",
1942
+ maxWidth: containerStyle.maxWidth || containerStyle.width,
1943
+ padding: "8px 12px",
1944
+ marginBottom: "8px",
1945
+ backgroundColor: "#1e293b",
1946
+ color: "#f8fafc",
1947
+ borderRadius: "8px",
1948
+ fontSize: "12px",
1949
+ fontFamily: "system-ui, -apple-system, sans-serif",
1950
+ boxSizing: "border-box"
1951
+ },
1952
+ children: [
1953
+ /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("div", { style: { display: "flex", alignItems: "center", gap: "8px" }, children: [
1954
+ /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("span", { style: { fontWeight: 600 }, children: chromeTitle || document.metadata?.title || "Preview" }),
1955
+ /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
1956
+ "span",
1957
+ {
1958
+ "data-testid": "viewport-badge",
1959
+ style: {
1960
+ fontSize: "10px",
1961
+ padding: "2px 6px",
1962
+ borderRadius: "4px",
1963
+ backgroundColor: "#334155",
1964
+ color: "#94a3b8",
1965
+ textTransform: "uppercase",
1966
+ fontWeight: 700
1967
+ },
1968
+ children: viewport
1969
+ }
1970
+ ),
1971
+ /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("span", { style: { fontSize: "11px", color: "#64748b" }, children: currentConfig.label || `${currentConfig.width} \xD7 ${currentConfig.height || "auto"}` })
1972
+ ] }),
1973
+ onViewportChange && /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
1974
+ "div",
1975
+ {
1976
+ "data-testid": "viewport-switcher",
1977
+ style: { display: "flex", gap: "4px", backgroundColor: "#0f172a", padding: "2px", borderRadius: "6px" },
1978
+ children: ["desktop", "tablet", "mobile"].map((device) => {
1979
+ const isActive = viewport === device;
1980
+ return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
1981
+ "button",
1982
+ {
1983
+ type: "button",
1984
+ "data-testid": `viewport-btn-${device}`,
1985
+ onClick: () => onViewportChange(device),
1986
+ style: {
1987
+ padding: "4px 10px",
1988
+ fontSize: "11px",
1989
+ fontWeight: 500,
1990
+ borderRadius: "4px",
1991
+ border: "none",
1992
+ cursor: "pointer",
1993
+ textTransform: "capitalize",
1994
+ backgroundColor: isActive ? "#3b82f6" : "transparent",
1995
+ color: isActive ? "#ffffff" : "#94a3b8",
1996
+ transition: "all 0.15s ease"
1997
+ },
1998
+ children: device
1999
+ },
2000
+ device
2001
+ );
2002
+ })
2003
+ }
2004
+ )
2005
+ ]
2006
+ }
2007
+ ),
2008
+ /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(
2009
+ "div",
2010
+ {
2011
+ "data-kubuild-preview-canvas": true,
2012
+ "data-viewport": viewport,
2013
+ className: `kubuild-preview-canvas ${canvasClassName || ""}`,
2014
+ style: mergedCanvasStyle,
2015
+ children: [
2016
+ /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
2017
+ KubuildRenderer,
2018
+ {
2019
+ document,
2020
+ registry,
2021
+ context,
2022
+ viewport,
2023
+ mode,
2024
+ onNodeClick,
2025
+ onDiagnostic,
2026
+ onActionDispatch
2027
+ }
2028
+ ),
2029
+ editorOverlay && /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
2030
+ "div",
2031
+ {
2032
+ "data-kubuild-preview-overlay": true,
2033
+ style: {
2034
+ position: "absolute",
2035
+ top: 0,
2036
+ left: 0,
2037
+ right: 0,
2038
+ bottom: 0,
2039
+ pointerEvents: "none",
2040
+ zIndex: 10
2041
+ },
2042
+ children: editorOverlay
2043
+ }
2044
+ )
2045
+ ]
2046
+ }
2047
+ )
2048
+ ]
2049
+ }
2050
+ );
2051
+ };
2052
+ var KubuildPreviewViewport = PreviewViewportAdapter;
2053
+ // Annotate the CommonJS export names for ESM import in node:
2054
+ 0 && (module.exports = {
2055
+ ComponentErrorBoundary,
2056
+ DEFAULT_BREAKPOINTS,
2057
+ DEFAULT_CSS_RESET,
2058
+ DEFAULT_RENDER_CONTEXT,
2059
+ DEFAULT_VIEWPORT_CONFIGS,
2060
+ EditableText,
2061
+ HtmlEmbedView,
2062
+ KubuildPreviewViewport,
2063
+ KubuildRenderer,
2064
+ NodeRenderer,
2065
+ PreviewViewportAdapter,
2066
+ RenderContextProvider,
2067
+ collectStateStylesCss,
2068
+ createMinimalRenderContext,
2069
+ createRenderContext,
2070
+ dispatchAction,
2071
+ isActionRegistered,
2072
+ resolveActionPayload,
2073
+ resolveActionPayloadDetailed,
2074
+ resolveAssetSync,
2075
+ resolveNodeStyles,
2076
+ resolveVariable,
2077
+ resolveViewportContainerStyle,
2078
+ resolveViewportDimensions,
2079
+ resolveViewportFromWidth,
2080
+ styleDefinitionToCssDeclarations,
2081
+ transformEmbedHtml,
2082
+ useRenderContext
2083
+ });
2084
+ //# sourceMappingURL=index.cjs.map