@lobehub/ui 5.10.3 → 5.10.5

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.
Files changed (54) hide show
  1. package/es/Highlighter/style.mjs +1 -1
  2. package/es/HtmlPreview/HtmlPreview.d.mts +8 -0
  3. package/es/HtmlPreview/HtmlPreview.mjs +377 -0
  4. package/es/HtmlPreview/HtmlPreview.mjs.map +1 -0
  5. package/es/HtmlPreview/Iframe.d.mts +8 -0
  6. package/es/HtmlPreview/Iframe.mjs +159 -0
  7. package/es/HtmlPreview/Iframe.mjs.map +1 -0
  8. package/es/HtmlPreview/buildShellSrcDoc.mjs +235 -0
  9. package/es/HtmlPreview/buildShellSrcDoc.mjs.map +1 -0
  10. package/es/HtmlPreview/buildStaticSrcDoc.mjs +44 -0
  11. package/es/HtmlPreview/buildStaticSrcDoc.mjs.map +1 -0
  12. package/es/HtmlPreview/const.d.mts +42 -0
  13. package/es/HtmlPreview/const.mjs +63 -0
  14. package/es/HtmlPreview/const.mjs.map +1 -0
  15. package/es/HtmlPreview/index.d.mts +6 -0
  16. package/es/HtmlPreview/index.d.ts +1 -0
  17. package/es/HtmlPreview/index.js +1 -0
  18. package/es/HtmlPreview/index.mjs +5 -0
  19. package/es/HtmlPreview/injectAutoHeightScript.d.mts +5 -0
  20. package/es/HtmlPreview/injectAutoHeightScript.mjs +37 -0
  21. package/es/HtmlPreview/injectAutoHeightScript.mjs.map +1 -0
  22. package/es/HtmlPreview/injectStorageShim.mjs +62 -0
  23. package/es/HtmlPreview/injectStorageShim.mjs.map +1 -0
  24. package/es/HtmlPreview/type.d.mts +114 -0
  25. package/es/Markdown/Markdown.mjs +7 -3
  26. package/es/Markdown/Markdown.mjs.map +1 -1
  27. package/es/Markdown/SyntaxMarkdown/StreamdownRender.mjs +2 -17
  28. package/es/Markdown/SyntaxMarkdown/StreamdownRender.mjs.map +1 -1
  29. package/es/Markdown/SyntaxMarkdown/fenceState.mjs +40 -0
  30. package/es/Markdown/SyntaxMarkdown/fenceState.mjs.map +1 -0
  31. package/es/Markdown/SyntaxMarkdown/useSmoothStreamContent.mjs +13 -0
  32. package/es/Markdown/SyntaxMarkdown/useSmoothStreamContent.mjs.map +1 -1
  33. package/es/Markdown/components/CodeBlock.mjs +10 -2
  34. package/es/Markdown/components/CodeBlock.mjs.map +1 -1
  35. package/es/Markdown/type.d.mts +3 -0
  36. package/es/NeuralNetworkLoading/NeuralNetworkLoading.d.mts +8 -0
  37. package/es/NeuralNetworkLoading/NeuralNetworkLoading.mjs +142 -0
  38. package/es/NeuralNetworkLoading/NeuralNetworkLoading.mjs.map +1 -0
  39. package/es/NeuralNetworkLoading/index.d.mts +3 -0
  40. package/es/NeuralNetworkLoading/index.d.ts +1 -0
  41. package/es/NeuralNetworkLoading/index.js +1 -0
  42. package/es/NeuralNetworkLoading/index.mjs +2 -0
  43. package/es/NeuralNetworkLoading/type.d.mts +12 -0
  44. package/es/hooks/useMarkdown/useMarkdownComponents.mjs +9 -2
  45. package/es/hooks/useMarkdown/useMarkdownComponents.mjs.map +1 -1
  46. package/es/hooks/useStableValue.mjs +30 -0
  47. package/es/hooks/useStableValue.mjs.map +1 -0
  48. package/es/index.d.mts +8 -1
  49. package/es/index.mjs +7 -2
  50. package/es/mdx/mdxComponents/Pre.mjs +14 -1
  51. package/es/mdx/mdxComponents/Pre.mjs.map +1 -1
  52. package/es/utils/isDeepEqual.mjs +21 -0
  53. package/es/utils/isDeepEqual.mjs.map +1 -0
  54. package/package.json +1 -1
@@ -164,6 +164,6 @@ const bodyVariants = cva(styles.bodyRoot, {
164
164
  } }
165
165
  });
166
166
  //#endregion
167
- export { bodyVariants, headerVariants, styles, variants };
167
+ export { actionsHoverCls, bodyVariants, headerVariants, styles, variants };
168
168
 
169
169
  //# sourceMappingURL=style.mjs.map
@@ -0,0 +1,8 @@
1
+ import { HtmlPreviewProps } from "./type.mjs";
2
+ import * as _$react from "react";
3
+
4
+ //#region src/HtmlPreview/HtmlPreview.d.ts
5
+ declare const HtmlPreview: _$react.NamedExoticComponent<HtmlPreviewProps>;
6
+ //#endregion
7
+ export { HtmlPreview };
8
+ //# sourceMappingURL=HtmlPreview.d.mts.map
@@ -0,0 +1,377 @@
1
+ "use client";
2
+ import FlexBasic_default from "../Flex/FlexBasic.mjs";
3
+ import { stopPropagation } from "../utils/dom.mjs";
4
+ import ActionIcon from "../ActionIcon/ActionIcon.mjs";
5
+ import SyntaxHighlighter from "../Highlighter/SyntaxHighlighter/index.mjs";
6
+ import CopyButton from "../CopyButton/CopyButton.mjs";
7
+ import { downloadBlob } from "../utils/downloadBlob.mjs";
8
+ import { actionsHoverCls, variants } from "../Highlighter/style.mjs";
9
+ import { containsScript, isFullHtmlDocument, isHtmlContentClosed } from "./const.mjs";
10
+ import NeuralNetworkLoading from "../NeuralNetworkLoading/NeuralNetworkLoading.mjs";
11
+ import Segmented from "../Segmented/Segmented.mjs";
12
+ import HtmlPreviewIframe from "./Iframe.mjs";
13
+ import { memo, useCallback, useEffect, useMemo, useRef, useState } from "react";
14
+ import { Fragment as Fragment$1, jsx, jsxs } from "react/jsx-runtime";
15
+ import { createStyles, cx, keyframes } from "antd-style";
16
+ import { Download, Expand } from "lucide-react";
17
+ //#region src/HtmlPreview/HtmlPreview.tsx
18
+ const shimmer = keyframes`
19
+ 0% { background-position: 200% 0; }
20
+ 100% { background-position: -200% 0; }
21
+ `;
22
+ const useStyles = createStyles(({ css, cssVar, isDarkMode }) => ({
23
+ loadingBackdrop: css`
24
+ pointer-events: none;
25
+
26
+ position: absolute;
27
+ z-index: 1;
28
+ inset: 0;
29
+
30
+ /* Subtle moving sheen so it doesn't look frozen. */
31
+ background: linear-gradient(
32
+ 90deg,
33
+ transparent 0%,
34
+ ${isDarkMode ? "rgba(255, 255, 255, 0.04)" : "rgba(0, 0, 0, 0.04)"} 50%,
35
+ transparent 100%
36
+ );
37
+ background-repeat: no-repeat;
38
+ background-size: 200% 100%;
39
+
40
+ animation: ${shimmer} 1.6s ${cssVar.motionEaseInOut} infinite;
41
+ `,
42
+ loadingBadge: css`
43
+ position: absolute;
44
+ z-index: 2;
45
+ inset-block-start: 12px;
46
+ inset-inline-start: 12px;
47
+
48
+ display: inline-flex;
49
+ gap: 8px;
50
+ align-items: center;
51
+
52
+ padding-block: 4px;
53
+ padding-inline: 6px 10px;
54
+ border-radius: 999px;
55
+
56
+ font-size: 12px;
57
+ color: ${cssVar.colorTextDescription};
58
+
59
+ background: ${cssVar.colorBgContainer};
60
+ backdrop-filter: blur(8px);
61
+ box-shadow: 0 0 0 1px ${cssVar.colorBorderSecondary};
62
+ `,
63
+ loadingSource: css`
64
+ pointer-events: none;
65
+ overflow: hidden;
66
+ height: 100%;
67
+
68
+ /* Faded out so the iframe transition feels like content lighting up,
69
+ not like one document jump-cutting to another. */
70
+ opacity: 0.45;
71
+
72
+ /* SyntaxHighlighter sets its own background; flatten so the shimmer
73
+ overlay reads cleanly on top. */
74
+ & [data-code-type='highlighter'] {
75
+ background: transparent;
76
+ box-shadow: none;
77
+ }
78
+
79
+ /* Tail-follow is layout-only — we anchor the scrollable element to
80
+ its scrollHeight via the ref + effect; CSS just keeps the
81
+ overflow hidden. */
82
+ & pre,
83
+ & code {
84
+ background: transparent !important;
85
+ }
86
+ `,
87
+ loadingRoot: css`
88
+ position: relative;
89
+ overflow: hidden;
90
+ background: ${isDarkMode ? "#1f1f1f" : "#fafafa"};
91
+ `,
92
+ toolbar: cx(actionsHoverCls, css`
93
+ position: absolute;
94
+ z-index: 2;
95
+ inset-block-start: 8px;
96
+ inset-inline-end: 8px;
97
+
98
+ padding: 4px;
99
+ border-radius: ${cssVar.borderRadiusLG};
100
+
101
+ opacity: 0;
102
+ background: ${cssVar.colorBgContainer};
103
+ backdrop-filter: blur(8px);
104
+ box-shadow: 0 0 0 1px ${cssVar.colorBorderSecondary};
105
+
106
+ transition: opacity 0.2s ${cssVar.motionEaseOut};
107
+
108
+ &:focus-within {
109
+ opacity: 1;
110
+ }
111
+ `)
112
+ }));
113
+ const themeBackground = (theme) => {
114
+ if (theme === "dark") return "#1f1f1f";
115
+ if (theme === "light") return "#ffffff";
116
+ };
117
+ const downloadHtml = async (content, fileName) => {
118
+ const blob = new Blob([content], { type: "text/html;charset=utf-8" });
119
+ const url = URL.createObjectURL(blob);
120
+ try {
121
+ await downloadBlob(url, fileName);
122
+ } finally {
123
+ URL.revokeObjectURL(url);
124
+ }
125
+ };
126
+ const HtmlPreview = memo(({ actionIconSize, actionsRender, animated, bodyRender, children, className, classNames, copyable = true, defaultHeight, defaultMode = "preview", downloadable = true, fileName, language = "html", onExpand, sandbox, shadow, streamingMode = "auto", style, styles: customStyles, theme, variant = "filled", fullFeatured: _fullFeatured, showLanguage: _showLanguage, defaultExpand: _defaultExpand, ...rest }) => {
127
+ const trimmedChildren = useMemo(() => (children || "").trim(), [children]);
128
+ const isFragment = useMemo(() => !isFullHtmlDocument(trimmedChildren), [trimmedChildren]);
129
+ const [scriptLocked, setScriptLocked] = useState(false);
130
+ const [headClosed, setHeadClosed] = useState(false);
131
+ const [liveCommitted, setLiveCommitted] = useState(false);
132
+ const prevAnimatedRef = useRef(animated);
133
+ const lastCommitRef = useRef(0);
134
+ const pendingTimerRef = useRef(null);
135
+ const latestContentRef = useRef(trimmedChildren);
136
+ useEffect(() => {
137
+ latestContentRef.current = trimmedChildren;
138
+ }, [trimmedChildren]);
139
+ useEffect(() => {
140
+ if (animated && !prevAnimatedRef.current) {
141
+ setScriptLocked(false);
142
+ setHeadClosed(false);
143
+ setLiveCommitted(false);
144
+ lastCommitRef.current = 0;
145
+ if (pendingTimerRef.current) {
146
+ clearTimeout(pendingTimerRef.current);
147
+ pendingTimerRef.current = null;
148
+ }
149
+ }
150
+ prevAnimatedRef.current = animated;
151
+ }, [animated]);
152
+ useEffect(() => {
153
+ if (animated && !scriptLocked && containsScript(trimmedChildren)) setScriptLocked(true);
154
+ }, [
155
+ trimmedChildren,
156
+ animated,
157
+ scriptLocked
158
+ ]);
159
+ useEffect(() => {
160
+ if (animated && !headClosed) {
161
+ const lowered = trimmedChildren.toLowerCase();
162
+ if (lowered.includes("</head>") || lowered.includes("</style>")) setHeadClosed(true);
163
+ }
164
+ }, [
165
+ trimmedChildren,
166
+ animated,
167
+ headClosed
168
+ ]);
169
+ const [throttledContent, setThrottledContent] = useState(trimmedChildren);
170
+ useEffect(() => {
171
+ if (!animated) {
172
+ if (pendingTimerRef.current) {
173
+ clearTimeout(pendingTimerRef.current);
174
+ pendingTimerRef.current = null;
175
+ }
176
+ lastCommitRef.current = Date.now();
177
+ setThrottledContent(trimmedChildren);
178
+ return;
179
+ }
180
+ if (!headClosed) return;
181
+ const throttleMs = 250;
182
+ const now = Date.now();
183
+ const elapsed = now - lastCommitRef.current;
184
+ if (elapsed >= throttleMs) {
185
+ if (pendingTimerRef.current) {
186
+ clearTimeout(pendingTimerRef.current);
187
+ pendingTimerRef.current = null;
188
+ }
189
+ lastCommitRef.current = now;
190
+ setThrottledContent(trimmedChildren);
191
+ return;
192
+ }
193
+ if (pendingTimerRef.current === null) pendingTimerRef.current = setTimeout(() => {
194
+ lastCommitRef.current = Date.now();
195
+ pendingTimerRef.current = null;
196
+ setThrottledContent(latestContentRef.current);
197
+ }, throttleMs - elapsed);
198
+ }, [
199
+ trimmedChildren,
200
+ animated,
201
+ headClosed
202
+ ]);
203
+ useEffect(() => () => {
204
+ if (pendingTimerRef.current) clearTimeout(pendingTimerRef.current);
205
+ }, []);
206
+ useEffect(() => {
207
+ if (!animated || liveCommitted || !headClosed) return;
208
+ if (streamingMode === "live" || streamingMode === "auto" && !scriptLocked) setLiveCommitted(true);
209
+ }, [
210
+ animated,
211
+ headClosed,
212
+ liveCommitted,
213
+ scriptLocked,
214
+ streamingMode
215
+ ]);
216
+ const isStable = !animated || isHtmlContentClosed(trimmedChildren) || liveCommitted;
217
+ const [mode, setMode] = useState(defaultMode);
218
+ const effectiveMode = isFragment ? "source" : mode;
219
+ const contentRef = useRef(trimmedChildren);
220
+ useEffect(() => {
221
+ contentRef.current = trimmedChildren;
222
+ }, [trimmedChildren]);
223
+ const loadingSourceRef = useRef(null);
224
+ useEffect(() => {
225
+ if (isStable) return;
226
+ const node = loadingSourceRef.current;
227
+ if (!node) return;
228
+ node.scrollTop = node.scrollHeight;
229
+ }, [trimmedChildren, isStable]);
230
+ const getCopyContent = useCallback(() => contentRef.current, []);
231
+ const handleDownload = useCallback(() => {
232
+ downloadHtml(contentRef.current, fileName || "preview.html");
233
+ }, [fileName]);
234
+ const handleExpand = useCallback(() => {
235
+ onExpand?.(contentRef.current);
236
+ }, [onExpand]);
237
+ const background = themeBackground(theme);
238
+ const sourceBody = useMemo(() => /* @__PURE__ */ jsx(SyntaxHighlighter, {
239
+ animated,
240
+ className: classNames?.content,
241
+ language: "html",
242
+ style: {
243
+ height: "100%",
244
+ ...customStyles?.content
245
+ },
246
+ variant,
247
+ children: trimmedChildren
248
+ }), [
249
+ animated,
250
+ classNames?.content,
251
+ customStyles?.content,
252
+ trimmedChildren,
253
+ variant
254
+ ]);
255
+ const { styles } = useStyles();
256
+ const iframeBody = useMemo(() => /* @__PURE__ */ jsx(HtmlPreviewIframe, {
257
+ animated,
258
+ background,
259
+ className: classNames?.iframe,
260
+ content: throttledContent,
261
+ defaultHeight,
262
+ sandbox,
263
+ style: customStyles?.iframe
264
+ }), [
265
+ background,
266
+ classNames?.iframe,
267
+ customStyles?.iframe,
268
+ defaultHeight,
269
+ sandbox,
270
+ throttledContent
271
+ ]);
272
+ const loadingBody = useMemo(() => /* @__PURE__ */ jsxs("div", {
273
+ className: styles.loadingRoot,
274
+ style: { height: defaultHeight ?? 400 },
275
+ children: [
276
+ /* @__PURE__ */ jsx("div", {
277
+ className: styles.loadingSource,
278
+ ref: loadingSourceRef,
279
+ children: /* @__PURE__ */ jsx(SyntaxHighlighter, {
280
+ animated,
281
+ language: "html",
282
+ variant: "borderless",
283
+ children: trimmedChildren
284
+ })
285
+ }),
286
+ /* @__PURE__ */ jsx("div", { className: styles.loadingBackdrop }),
287
+ /* @__PURE__ */ jsxs("div", {
288
+ className: styles.loadingBadge,
289
+ children: [/* @__PURE__ */ jsx(NeuralNetworkLoading, { size: 16 }), /* @__PURE__ */ jsx("span", { children: "Preparing preview…" })]
290
+ })
291
+ ]
292
+ }), [
293
+ animated,
294
+ defaultHeight,
295
+ styles,
296
+ trimmedChildren
297
+ ]);
298
+ const defaultBody = effectiveMode === "preview" ? isStable ? iframeBody : loadingBody : sourceBody;
299
+ const body = useMemo(() => {
300
+ if (!bodyRender) return defaultBody;
301
+ return bodyRender({
302
+ content: trimmedChildren,
303
+ mode: effectiveMode,
304
+ originalNode: defaultBody
305
+ });
306
+ }, [
307
+ bodyRender,
308
+ defaultBody,
309
+ effectiveMode,
310
+ trimmedChildren
311
+ ]);
312
+ const segmentOptions = useMemo(() => [{
313
+ label: "Preview",
314
+ value: "preview"
315
+ }, {
316
+ label: "Source",
317
+ value: "source"
318
+ }], []);
319
+ const iconSize = actionIconSize || "small";
320
+ const originalActions = /* @__PURE__ */ jsxs(Fragment$1, { children: [
321
+ !isFragment && /* @__PURE__ */ jsx(Segmented, {
322
+ options: segmentOptions,
323
+ size: "small",
324
+ value: effectiveMode,
325
+ onChange: (v) => setMode(v)
326
+ }),
327
+ copyable && /* @__PURE__ */ jsx(CopyButton, {
328
+ content: getCopyContent,
329
+ size: iconSize
330
+ }),
331
+ downloadable && /* @__PURE__ */ jsx(ActionIcon, {
332
+ icon: Download,
333
+ size: iconSize,
334
+ title: "Download HTML",
335
+ onClick: handleDownload
336
+ }),
337
+ onExpand && /* @__PURE__ */ jsx(ActionIcon, {
338
+ icon: Expand,
339
+ size: iconSize,
340
+ title: "Open full preview",
341
+ onClick: handleExpand
342
+ })
343
+ ] });
344
+ const actions = actionsRender ? actionsRender({
345
+ actionIconSize: iconSize,
346
+ content: trimmedChildren,
347
+ getContent: getCopyContent,
348
+ mode: effectiveMode,
349
+ originalNode: originalActions,
350
+ setMode
351
+ }) : originalActions;
352
+ return /* @__PURE__ */ jsxs("div", {
353
+ className: cx(variants({
354
+ shadow,
355
+ variant
356
+ }), className),
357
+ "data-code-type": "html-preview",
358
+ "data-html-preview-language": language,
359
+ style,
360
+ ...rest,
361
+ children: [/* @__PURE__ */ jsx(FlexBasic_default, {
362
+ horizontal: true,
363
+ align: "center",
364
+ className: cx(styles.toolbar, classNames?.header),
365
+ flex: "none",
366
+ gap: 4,
367
+ style: customStyles?.header,
368
+ onClick: stopPropagation,
369
+ children: actions
370
+ }), body]
371
+ });
372
+ });
373
+ HtmlPreview.displayName = "HtmlPreview";
374
+ //#endregion
375
+ export { HtmlPreview as default };
376
+
377
+ //# sourceMappingURL=HtmlPreview.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"HtmlPreview.mjs","names":["Flexbox"],"sources":["../../src/HtmlPreview/HtmlPreview.tsx"],"sourcesContent":["'use client';\n\nimport { createStyles, cx, keyframes } from 'antd-style';\nimport { Download, Expand } from 'lucide-react';\nimport { memo, type ReactNode, useCallback, useEffect, useMemo, useRef, useState } from 'react';\n\nimport ActionIcon from '@/ActionIcon';\nimport CopyButton from '@/CopyButton';\nimport { Flexbox } from '@/Flex';\nimport { actionsHoverCls, variants } from '@/Highlighter/style';\nimport SyntaxHighlighter from '@/Highlighter/SyntaxHighlighter';\nimport NeuralNetworkLoading from '@/NeuralNetworkLoading';\nimport Segmented from '@/Segmented';\nimport { stopPropagation } from '@/utils/dom';\nimport { downloadBlob } from '@/utils/downloadBlob';\n\nimport { containsScript, DEFAULT_HEIGHT, isFullHtmlDocument, isHtmlContentClosed } from './const';\nimport HtmlPreviewIframe from './Iframe';\nimport type { HtmlPreviewMode, HtmlPreviewProps } from './type';\n\n// Sheen sweep direction: left → right.\n// `background-position` works inversely from \"where the image is drawn\":\n// at `200%` the over-sized gradient starts off to the left of the\n// container, at `-200%` it ends off to the right — so animating\n// 200% → -200% moves the visible bright spot from left to right.\nconst shimmer = keyframes`\n 0% { background-position: 200% 0; }\n 100% { background-position: -200% 0; }\n`;\n\nconst useStyles = createStyles(({ css, cssVar, isDarkMode }) => ({\n loadingBackdrop: css`\n pointer-events: none;\n\n position: absolute;\n z-index: 1;\n inset: 0;\n\n /* Subtle moving sheen so it doesn't look frozen. */\n background: linear-gradient(\n 90deg,\n transparent 0%,\n ${isDarkMode ? 'rgba(255, 255, 255, 0.04)' : 'rgba(0, 0, 0, 0.04)'} 50%,\n transparent 100%\n );\n background-repeat: no-repeat;\n background-size: 200% 100%;\n\n animation: ${shimmer} 1.6s ${cssVar.motionEaseInOut} infinite;\n `,\n loadingBadge: css`\n position: absolute;\n z-index: 2;\n inset-block-start: 12px;\n inset-inline-start: 12px;\n\n display: inline-flex;\n gap: 8px;\n align-items: center;\n\n padding-block: 4px;\n padding-inline: 6px 10px;\n border-radius: 999px;\n\n font-size: 12px;\n color: ${cssVar.colorTextDescription};\n\n background: ${cssVar.colorBgContainer};\n backdrop-filter: blur(8px);\n box-shadow: 0 0 0 1px ${cssVar.colorBorderSecondary};\n `,\n // The streaming source visible during Phase 1 — heavily faded so it\n // reads as \"this is preview-pending content\" rather than the finished\n // article. Auto-follows the tail so the user can see new tokens land\n // even on slow models.\n loadingSource: css`\n pointer-events: none;\n overflow: hidden;\n height: 100%;\n\n /* Faded out so the iframe transition feels like content lighting up,\n not like one document jump-cutting to another. */\n opacity: 0.45;\n\n /* SyntaxHighlighter sets its own background; flatten so the shimmer\n overlay reads cleanly on top. */\n & [data-code-type='highlighter'] {\n background: transparent;\n box-shadow: none;\n }\n\n /* Tail-follow is layout-only — we anchor the scrollable element to\n its scrollHeight via the ref + effect; CSS just keeps the\n overflow hidden. */\n & pre,\n & code {\n background: transparent !important;\n }\n `,\n loadingRoot: css`\n position: relative;\n overflow: hidden;\n background: ${isDarkMode ? '#1f1f1f' : '#fafafa'};\n `,\n // Inline top-right toolbar. Tagged with `actionsHoverCls` so the Highlighter\n // container's `&:hover .${actionsHoverCls} { opacity: 1 }` rule flips it\n // in/out as the user moves over the preview — same UX as the regular code\n // block actions.\n toolbar: cx(\n actionsHoverCls,\n css`\n position: absolute;\n z-index: 2;\n inset-block-start: 8px;\n inset-inline-end: 8px;\n\n padding: 4px;\n border-radius: ${cssVar.borderRadiusLG};\n\n opacity: 0;\n background: ${cssVar.colorBgContainer};\n backdrop-filter: blur(8px);\n box-shadow: 0 0 0 1px ${cssVar.colorBorderSecondary};\n\n transition: opacity 0.2s ${cssVar.motionEaseOut};\n\n &:focus-within {\n opacity: 1;\n }\n `,\n ),\n}));\n\nconst themeBackground = (theme?: 'light' | 'dark') => {\n if (theme === 'dark') return '#1f1f1f';\n if (theme === 'light') return '#ffffff';\n return undefined;\n};\n\nconst downloadHtml = async (content: string, fileName: string) => {\n const blob = new Blob([content], { type: 'text/html;charset=utf-8' });\n const url = URL.createObjectURL(blob);\n try {\n await downloadBlob(url, fileName);\n } finally {\n URL.revokeObjectURL(url);\n }\n};\n\nconst HtmlPreview = memo<HtmlPreviewProps>(\n ({\n actionIconSize,\n actionsRender,\n animated,\n bodyRender,\n children,\n className,\n classNames,\n copyable = true,\n defaultHeight,\n defaultMode = 'preview',\n downloadable = true,\n fileName,\n language = 'html',\n onExpand,\n sandbox,\n shadow,\n streamingMode = 'auto',\n style,\n styles: customStyles,\n theme,\n variant = 'filled',\n // `fullFeatured` / `showLanguage` / `defaultExpand` are accepted for API\n // compatibility with the rest of the Pre family but no longer drive a\n // separate header — the inline toolbar is always rendered.\n fullFeatured: _fullFeatured,\n showLanguage: _showLanguage,\n defaultExpand: _defaultExpand,\n ...rest\n }) => {\n const trimmedChildren = useMemo(() => (children || '').trim(), [children]);\n const isFragment = useMemo(() => !isFullHtmlDocument(trimmedChildren), [trimmedChildren]);\n\n // Per-session tracking. Reset on `animated` edge false → true.\n const [scriptLocked, setScriptLocked] = useState(false);\n const [headClosed, setHeadClosed] = useState(false);\n const [liveCommitted, setLiveCommitted] = useState(false);\n const prevAnimatedRef = useRef(animated);\n const lastCommitRef = useRef(0);\n const pendingTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);\n const latestContentRef = useRef(trimmedChildren);\n useEffect(() => {\n latestContentRef.current = trimmedChildren;\n }, [trimmedChildren]);\n useEffect(() => {\n if (animated && !prevAnimatedRef.current) {\n setScriptLocked(false);\n setHeadClosed(false);\n setLiveCommitted(false);\n lastCommitRef.current = 0;\n if (pendingTimerRef.current) {\n clearTimeout(pendingTimerRef.current);\n pendingTimerRef.current = null;\n }\n }\n prevAnimatedRef.current = animated;\n }, [animated]);\n\n // Sticky script detection: once a `<script>` appears in this session,\n // auto mode locks into defer.\n useEffect(() => {\n if (animated && !scriptLocked && containsScript(trimmedChildren)) {\n setScriptLocked(true);\n }\n }, [trimmedChildren, animated, scriptLocked]);\n\n // Track whether the head section has closed. Until it does, the iframe\n // would render either nothing or invalid HTML (style tag mid-stream).\n // After `</head>` (or `</style>` as a fallback for documents that skip\n // an explicit head close) we know the visual baseline is locked in.\n useEffect(() => {\n if (animated && !headClosed) {\n const lowered = trimmedChildren.toLowerCase();\n if (lowered.includes('</head>') || lowered.includes('</style>')) {\n setHeadClosed(true);\n }\n }\n }, [trimmedChildren, animated, headClosed]);\n\n // Two-phase streaming commit:\n // Phase 1 (head still streaming) — don't mount iframe at all. There's\n // nothing meaningful to render and styles aren't applied yet.\n // Phase 2 (head closed, body streaming) — commit at most once every\n // ~250ms. Important: this is a TRUE throttle, not a debounce. The\n // pending timer is held in a ref so it survives effect re-runs;\n // when it fires it commits the latest content (via another ref)\n // and clears itself, allowing a fresh schedule. If chunks come in\n // faster than the throttle window (which they do at ~50/sec) we\n // still get a commit every 250ms instead of waiting for streaming\n // to pause.\n // On streaming end → flush immediately, cancel any pending timer.\n const [throttledContent, setThrottledContent] = useState(trimmedChildren);\n useEffect(() => {\n if (!animated) {\n if (pendingTimerRef.current) {\n clearTimeout(pendingTimerRef.current);\n pendingTimerRef.current = null;\n }\n lastCommitRef.current = Date.now();\n setThrottledContent(trimmedChildren);\n return;\n }\n if (!headClosed) return;\n\n const throttleMs = 250;\n const now = Date.now();\n const elapsed = now - lastCommitRef.current;\n\n if (elapsed >= throttleMs) {\n if (pendingTimerRef.current) {\n clearTimeout(pendingTimerRef.current);\n pendingTimerRef.current = null;\n }\n lastCommitRef.current = now;\n setThrottledContent(trimmedChildren);\n return;\n }\n\n // Schedule a future commit, but only if one isn't already pending —\n // every chunk arrival re-runs this effect; we don't want to reset\n // the timer each time (that's debounce, and during continuous\n // streaming it would never fire).\n if (pendingTimerRef.current === null) {\n pendingTimerRef.current = setTimeout(() => {\n lastCommitRef.current = Date.now();\n pendingTimerRef.current = null;\n setThrottledContent(latestContentRef.current);\n }, throttleMs - elapsed);\n }\n }, [trimmedChildren, animated, headClosed]);\n useEffect(\n () => () => {\n if (pendingTimerRef.current) clearTimeout(pendingTimerRef.current);\n },\n [],\n );\n\n // Live-streaming commitment is sticky for the rest of the session. The\n // decision is made the moment the head seals:\n // • `live` → commit unconditionally\n // • `auto` → commit only if no `<script>` has appeared yet (script-\n // bearing docs go down the defer path to avoid running setup() on\n // partial source)\n // • `defer` → never commit; wait for `</html>`\n // Sticky-ness matters for `auto`: if a `<script>` arrives *after* the\n // head has already closed and we've started live-streaming, we keep\n // streaming rather than yanking the rendered content back into a\n // loading state mid-flight. The shell→static swap at end of streaming\n // re-runs the document cleanly anyway.\n useEffect(() => {\n if (!animated || liveCommitted || !headClosed) return;\n if (streamingMode === 'live' || (streamingMode === 'auto' && !scriptLocked)) {\n setLiveCommitted(true);\n }\n }, [animated, headClosed, liveCommitted, scriptLocked, streamingMode]);\n\n // Streaming gate. The iframe can mount in three situations:\n // 1. content is no longer animating\n // 2. `</html>` has arrived\n // 3. live streaming has been committed this session\n const isStable = !animated || isHtmlContentClosed(trimmedChildren) || liveCommitted;\n\n const [mode, setMode] = useState<HtmlPreviewMode>(defaultMode);\n\n // Fragments cannot meaningfully render in preview — force source view.\n // For streaming content that's not yet stable we keep the user's mode\n // choice and substitute a loading placeholder in the body instead, so\n // the toggle UI doesn't flip back and forth as content arrives.\n const effectiveMode: HtmlPreviewMode = isFragment ? 'source' : mode;\n\n const contentRef = useRef(trimmedChildren);\n useEffect(() => {\n contentRef.current = trimmedChildren;\n }, [trimmedChildren]);\n\n // Tail-follow the streaming source visible during Phase 1 — anchor\n // the scroll position to the latest tokens so a slow model's output\n // doesn't sit pinned to the document head while the user waits.\n const loadingSourceRef = useRef<HTMLDivElement | null>(null);\n useEffect(() => {\n if (isStable) return;\n const node = loadingSourceRef.current;\n if (!node) return;\n node.scrollTop = node.scrollHeight;\n }, [trimmedChildren, isStable]);\n\n const getCopyContent = useCallback(() => contentRef.current, []);\n\n const handleDownload = useCallback(() => {\n void downloadHtml(contentRef.current, fileName || 'preview.html');\n }, [fileName]);\n\n const handleExpand = useCallback(() => {\n onExpand?.(contentRef.current);\n }, [onExpand]);\n\n const background = themeBackground(theme);\n\n const sourceBody = useMemo(\n () => (\n <SyntaxHighlighter\n animated={animated}\n className={classNames?.content}\n language={'html'}\n style={{ height: '100%', ...customStyles?.content }}\n variant={variant}\n >\n {trimmedChildren}\n </SyntaxHighlighter>\n ),\n [animated, classNames?.content, customStyles?.content, trimmedChildren, variant],\n );\n\n const { styles } = useStyles();\n\n const iframeBody = useMemo(\n () => (\n <HtmlPreviewIframe\n animated={animated}\n background={background}\n className={classNames?.iframe}\n content={throttledContent}\n defaultHeight={defaultHeight}\n sandbox={sandbox}\n style={customStyles?.iframe}\n />\n ),\n [\n background,\n classNames?.iframe,\n customStyles?.iframe,\n defaultHeight,\n sandbox,\n throttledContent,\n ],\n );\n\n // Shown when the user is on preview mode but the iframe isn't ready\n // yet (Phase 1 of streaming: head still arriving). Holds the eventual\n // iframe height to avoid layout shift on mount.\n //\n // Stream the raw source through `SyntaxHighlighter` at low opacity so\n // the user sees real progress on slow models (a 30-tps DeepSeek\n // pumping a ~5 KB head can otherwise sit on a static spinner for\n // 20+ seconds). A small \"Preparing preview…\" badge keeps the loading\n // state unambiguous. Tail-follow keeps the visible region anchored\n // to the latest tokens — see the `useEffect` below.\n const loadingBody = useMemo(\n () => (\n <div className={styles.loadingRoot} style={{ height: defaultHeight ?? DEFAULT_HEIGHT }}>\n <div className={styles.loadingSource} ref={loadingSourceRef}>\n <SyntaxHighlighter animated={animated} language={'html'} variant={'borderless'}>\n {trimmedChildren}\n </SyntaxHighlighter>\n </div>\n <div className={styles.loadingBackdrop} />\n <div className={styles.loadingBadge}>\n <NeuralNetworkLoading size={16} />\n <span>Preparing preview…</span>\n </div>\n </div>\n ),\n [animated, defaultHeight, styles, trimmedChildren],\n );\n\n const previewBody = isStable ? iframeBody : loadingBody;\n\n const defaultBody = effectiveMode === 'preview' ? previewBody : sourceBody;\n\n const body = useMemo(() => {\n if (!bodyRender) return defaultBody;\n return bodyRender({\n content: trimmedChildren,\n mode: effectiveMode,\n originalNode: defaultBody,\n });\n }, [bodyRender, defaultBody, effectiveMode, trimmedChildren]);\n\n const segmentOptions = useMemo(\n () => [\n { label: 'Preview', value: 'preview' as const },\n { label: 'Source', value: 'source' as const },\n ],\n [],\n );\n\n const iconSize = actionIconSize || 'small';\n\n const originalActions: ReactNode = (\n <>\n {!isFragment && (\n <Segmented\n options={segmentOptions}\n size={'small'}\n value={effectiveMode}\n onChange={(v) => setMode(v as HtmlPreviewMode)}\n />\n )}\n {copyable && <CopyButton content={getCopyContent} size={iconSize} />}\n {downloadable && (\n <ActionIcon\n icon={Download}\n size={iconSize}\n title={'Download HTML'}\n onClick={handleDownload}\n />\n )}\n {onExpand && (\n <ActionIcon\n icon={Expand}\n size={iconSize}\n title={'Open full preview'}\n onClick={handleExpand}\n />\n )}\n </>\n );\n\n const actions = actionsRender\n ? actionsRender({\n actionIconSize: iconSize,\n content: trimmedChildren,\n getContent: getCopyContent,\n mode: effectiveMode,\n originalNode: originalActions,\n setMode,\n })\n : originalActions;\n\n return (\n <div\n className={cx(variants({ shadow, variant }), className)}\n data-code-type=\"html-preview\"\n data-html-preview-language={language}\n style={style}\n {...rest}\n >\n <Flexbox\n horizontal\n align={'center'}\n className={cx(styles.toolbar, classNames?.header)}\n flex={'none'}\n gap={4}\n style={customStyles?.header}\n onClick={stopPropagation}\n >\n {actions}\n </Flexbox>\n {body}\n </div>\n );\n },\n);\n\nHtmlPreview.displayName = 'HtmlPreview';\n\nexport default HtmlPreview;\n"],"mappings":";;;;;;;;;;;;;;;;;AAyBA,MAAM,UAAU,SAAS;;;;AAKzB,MAAM,YAAY,cAAc,EAAE,KAAK,QAAQ,kBAAkB;CAC/D,iBAAiB,GAAG;;;;;;;;;;;QAWd,aAAa,8BAA8B,sBAAsB;;;;;;iBAMxD,QAAQ,QAAQ,OAAO,gBAAgB;;CAEtD,cAAc,GAAG;;;;;;;;;;;;;;;aAeN,OAAO,qBAAqB;;kBAEvB,OAAO,iBAAiB;;4BAEd,OAAO,qBAAqB;;CAMtD,eAAe,GAAG;;;;;;;;;;;;;;;;;;;;;;;;CAwBlB,aAAa,GAAG;;;kBAGA,aAAa,YAAY,UAAU;;CAMnD,SAAS,GACP,iBACA,GAAG;;;;;;;uBAOgB,OAAO,eAAe;;;oBAGzB,OAAO,iBAAiB;;8BAEd,OAAO,qBAAqB;;iCAEzB,OAAO,cAAc;;;;;MAMnD;CACF,EAAE;AAEH,MAAM,mBAAmB,UAA6B;AACpD,KAAI,UAAU,OAAQ,QAAO;AAC7B,KAAI,UAAU,QAAS,QAAO;;AAIhC,MAAM,eAAe,OAAO,SAAiB,aAAqB;CAChE,MAAM,OAAO,IAAI,KAAK,CAAC,QAAQ,EAAE,EAAE,MAAM,2BAA2B,CAAC;CACrE,MAAM,MAAM,IAAI,gBAAgB,KAAK;AACrC,KAAI;AACF,QAAM,aAAa,KAAK,SAAS;WACzB;AACR,MAAI,gBAAgB,IAAI;;;AAI5B,MAAM,cAAc,MACjB,EACC,gBACA,eACA,UACA,YACA,UACA,WACA,YACA,WAAW,MACX,eACA,cAAc,WACd,eAAe,MACf,UACA,WAAW,QACX,UACA,SACA,QACA,gBAAgB,QAChB,OACA,QAAQ,cACR,OACA,UAAU,U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AO,WAAW;GAChB,SAAS;GACT,MAAM;GACN,cAAc;GACf,CAAC;IACD;EAAC;EAAY;EAAa;EAAe;EAAgB,CAAC;CAE7D,MAAM,iBAAiB,cACf,CACJ;EAAE,OAAO;EAAW,OAAO;EAAoB,EAC/C;EAAE,OAAO;EAAU,OAAO;EAAmB,CAC9C,EACD,EAAE,CACH;CAED,MAAM,WAAW,kBAAkB;CAEnC,MAAM,kBACJ,qBAAA,YAAA,EAAA,UAAA;EACG,CAAC,cACA,oBAAC,WAAD;GACE,SAAS;GACT,MAAM;GACN,OAAO;GACP,WAAW,MAAM,QAAQ,EAAqB;GAC9C,CAAA;EAEH,YAAY,oBAAC,YAAD;GAAY,SAAS;GAAgB,MAAM;GAAY,CAAA;EACnE,gBACC,oBAAC,YAAD;GACE,MAAM;GACN,MAAM;GACN,OAAO;GACP,SAAS;GACT,CAAA;EAEH,YACC,oBAAC,YAAD;GACE,MAAM;GACN,MAAM;GACN,OAAO;GACP,SAAS;GACT,CAAA;EAEH,EAAA,CAAA;CAGL,MAAM,UAAU,gBACZ,cAAc;EACZ,gBAAgB;EAChB,SAAS;EACT,YAAY;EACZ,MAAM;EACN,cAAc;EACd;EACD,CAAC,GACF;AAEJ,QACE,qBAAC,OAAD;EACE,WAAW,GAAG,SAAS;GAAE;GAAQ;GAAS,CAAC,EAAE,UAAU;EACvD,kBAAe;EACf,8BAA4B;EACrB;EACP,GAAI;YALN,CAOE,oBAACA,mBAAD;GACE,YAAA;GACA,OAAO;GACP,WAAW,GAAG,OAAO,SAAS,YAAY,OAAO;GACjD,MAAM;GACN,KAAK;GACL,OAAO,cAAc;GACrB,SAAS;aAER;GACO,CAAA,EACT,KACG;;EAGX;AAED,YAAY,cAAc"}
@@ -0,0 +1,8 @@
1
+ import { HtmlPreviewIframeProps } from "./type.mjs";
2
+ import * as _$react from "react";
3
+
4
+ //#region src/HtmlPreview/Iframe.d.ts
5
+ declare const HtmlPreviewIframe: _$react.NamedExoticComponent<HtmlPreviewIframeProps>;
6
+ //#endregion
7
+ export { HtmlPreviewIframe };
8
+ //# sourceMappingURL=Iframe.d.mts.map
@@ -0,0 +1,159 @@
1
+ "use client";
2
+ import { DEFAULT_SANDBOX, SRCDOC_MAX_LENGTH } from "./const.mjs";
3
+ import "./injectAutoHeightScript.mjs";
4
+ import { SHELL_UPDATE_MESSAGE_TYPE, buildShellSrcDoc } from "./buildShellSrcDoc.mjs";
5
+ import { buildStaticSrcDoc } from "./buildStaticSrcDoc.mjs";
6
+ import { memo, useCallback, useEffect, useId, useMemo, useRef, useState } from "react";
7
+ import { jsx } from "react/jsx-runtime";
8
+ import { createStyles, cx } from "antd-style";
9
+ //#region src/HtmlPreview/Iframe.tsx
10
+ const useStyles = createStyles(({ css, cssVar }) => ({
11
+ fallback: css`
12
+ padding: 16px;
13
+ font-size: 13px;
14
+ color: ${cssVar.colorTextDescription};
15
+ `,
16
+ iframe: css`
17
+ display: block;
18
+ width: 100%;
19
+ border: none;
20
+ background: transparent;
21
+ `
22
+ }));
23
+ const headSealedPattern = /<\/head\s*>|<body[\s>]/i;
24
+ const isHeadSealed = (raw) => headSealedPattern.test(raw);
25
+ const parseContent = (() => {
26
+ let parser = null;
27
+ return (content) => {
28
+ if (typeof window === "undefined") return null;
29
+ if (!content) return {
30
+ bodyHtml: "",
31
+ headExtrasHtml: "",
32
+ styleContent: ""
33
+ };
34
+ if (!parser) parser = new DOMParser();
35
+ const doc = parser.parseFromString(content, "text/html");
36
+ const styleParts = [];
37
+ const headExtras = [];
38
+ if (doc.head) for (const child of Array.from(doc.head.children)) if (child.tagName === "STYLE") styleParts.push(child.textContent || "");
39
+ else headExtras.push(child.outerHTML);
40
+ return {
41
+ bodyHtml: doc.body ? doc.body.innerHTML : "",
42
+ headExtrasHtml: isHeadSealed(content) ? headExtras.join("") : "",
43
+ styleContent: styleParts.join("\n")
44
+ };
45
+ };
46
+ })();
47
+ const HtmlPreviewIframe = memo(({ animated, background, content, className, defaultHeight = 400, ref, sandbox = DEFAULT_SANDBOX, style, title = "HTML preview" }) => {
48
+ const { styles } = useStyles();
49
+ const innerRef = useRef(null);
50
+ const frameId = useId();
51
+ const [height, setHeight] = useState(defaultHeight);
52
+ const defaultHeightRef = useRef(defaultHeight);
53
+ useEffect(() => {
54
+ defaultHeightRef.current = defaultHeight;
55
+ }, [defaultHeight]);
56
+ const tooLarge = content.length > SRCDOC_MAX_LENGTH;
57
+ const staticSrcDoc = useMemo(() => {
58
+ if (animated || tooLarge) return null;
59
+ return buildStaticSrcDoc({
60
+ background,
61
+ content,
62
+ frameId
63
+ });
64
+ }, [
65
+ animated,
66
+ background,
67
+ content,
68
+ frameId,
69
+ tooLarge
70
+ ]);
71
+ const shellSrcDoc = useMemo(() => {
72
+ if (!animated || tooLarge) return null;
73
+ return buildShellSrcDoc({
74
+ background,
75
+ frameId
76
+ });
77
+ }, [
78
+ animated,
79
+ background,
80
+ frameId,
81
+ tooLarge
82
+ ]);
83
+ const [shellReady, setShellReady] = useState(false);
84
+ useEffect(() => {
85
+ setShellReady(false);
86
+ }, [shellSrcDoc]);
87
+ const payload = useMemo(() => {
88
+ if (!animated || tooLarge) return null;
89
+ return parseContent(content);
90
+ }, [
91
+ animated,
92
+ content,
93
+ tooLarge
94
+ ]);
95
+ useEffect(() => {
96
+ if (!animated) return;
97
+ if (!shellReady || !payload) return;
98
+ const win = innerRef.current?.contentWindow;
99
+ if (!win) return;
100
+ win.postMessage({
101
+ frameId,
102
+ payload,
103
+ type: SHELL_UPDATE_MESSAGE_TYPE
104
+ }, "*");
105
+ }, [
106
+ animated,
107
+ payload,
108
+ shellReady,
109
+ frameId
110
+ ]);
111
+ useEffect(() => {
112
+ const handler = (event) => {
113
+ const data = event.data;
114
+ if (!data || typeof data !== "object") return;
115
+ if (data.frameId !== frameId) return;
116
+ if (event.source !== innerRef.current?.contentWindow) return;
117
+ if (data.type === `lobe-html-shell-update:ready`) {
118
+ setShellReady(true);
119
+ return;
120
+ }
121
+ if (data.type === "lobe-html-resize") {
122
+ const next = Number(data.height);
123
+ if (!Number.isFinite(next) || next <= 0) return;
124
+ const floored = Math.max(next, defaultHeightRef.current);
125
+ setHeight((prev) => Math.abs(prev - floored) < 1 ? prev : floored);
126
+ }
127
+ };
128
+ window.addEventListener("message", handler);
129
+ return () => window.removeEventListener("message", handler);
130
+ }, [frameId]);
131
+ const setRef = useCallback((node) => {
132
+ innerRef.current = node;
133
+ if (typeof ref === "function") ref(node);
134
+ else if (ref) ref.current = node;
135
+ }, [ref]);
136
+ if (tooLarge) return /* @__PURE__ */ jsx("div", {
137
+ className: cx(styles.fallback, className),
138
+ style,
139
+ children: "Content too large to preview inline."
140
+ });
141
+ const srcDoc = staticSrcDoc ?? shellSrcDoc ?? "";
142
+ const iframeKey = animated ? "shell" : "static";
143
+ return /* @__PURE__ */ jsx("iframe", {
144
+ className: cx(styles.iframe, className),
145
+ ref: setRef,
146
+ sandbox,
147
+ srcDoc,
148
+ style: {
149
+ height,
150
+ ...style
151
+ },
152
+ title
153
+ }, iframeKey);
154
+ });
155
+ HtmlPreviewIframe.displayName = "HtmlPreviewIframe";
156
+ //#endregion
157
+ export { HtmlPreviewIframe as default };
158
+
159
+ //# sourceMappingURL=Iframe.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Iframe.mjs","names":[],"sources":["../../src/HtmlPreview/Iframe.tsx"],"sourcesContent":["'use client';\n\nimport { createStyles, cx } from 'antd-style';\nimport { memo, useCallback, useEffect, useId, useMemo, useRef, useState } from 'react';\n\nimport { buildShellSrcDoc, SHELL_UPDATE_MESSAGE_TYPE } from './buildShellSrcDoc';\nimport { buildStaticSrcDoc } from './buildStaticSrcDoc';\nimport { DEFAULT_HEIGHT, DEFAULT_SANDBOX, SRCDOC_MAX_LENGTH } from './const';\nimport { AUTO_HEIGHT_MESSAGE_TYPE } from './injectAutoHeightScript';\nimport type { HtmlPreviewIframeProps } from './type';\n\nconst useStyles = createStyles(({ css, cssVar }) => ({\n fallback: css`\n padding: 16px;\n font-size: 13px;\n color: ${cssVar.colorTextDescription};\n `,\n iframe: css`\n display: block;\n width: 100%;\n border: none;\n background: transparent;\n `,\n}));\n\ninterface Payload {\n bodyHtml: string;\n /**\n * Non-inline-style children of `<head>` serialised in document order:\n * `<script src=…>`, `<script>…</script>`, `<link>`, `<meta>`, `<base>`,\n * `<title>` etc. The shell appends/dedupes these into its own head so\n * head-loaded resources (Tailwind CDN, p5.js, fonts, …) work for full\n * documents. Inline `<style>` is intentionally excluded — those flow\n * through `styleContent` so streaming partial CSS grows in place rather\n * than stacking duplicate `<style>` blocks.\n *\n * Empty until the user's `<head>` is *sealed* (a `</head>` close tag has\n * arrived, or `<body>` has opened — browsers auto-close head at that\n * point). Holding off prevents partial `src=\"https://cd\"` URLs from\n * being mounted and 404-ing while the model is still streaming.\n */\n headExtrasHtml: string;\n styleContent: string;\n}\n\n// Head is \"sealed\" as soon as we see a close tag or the body has begun;\n// after that point, additional chunks land in body and head extras won't\n// change.\nconst headSealedPattern = /<\\/head\\s*>|<body[\\s>]/i;\nconst isHeadSealed = (raw: string): boolean => headSealedPattern.test(raw);\n\nconst parseContent = (() => {\n // Lazy-init: only need one parser instance, and only in the browser.\n let parser: DOMParser | null = null;\n return (content: string): Payload | null => {\n if (typeof window === 'undefined') return null;\n if (!content) return { bodyHtml: '', headExtrasHtml: '', styleContent: '' };\n if (!parser) parser = new DOMParser();\n const doc = parser.parseFromString(content, 'text/html');\n\n const styleParts: string[] = [];\n const headExtras: string[] = [];\n\n if (doc.head) {\n for (const child of Array.from(doc.head.children)) {\n if (child.tagName === 'STYLE') {\n styleParts.push(child.textContent || '');\n } else {\n headExtras.push(child.outerHTML);\n }\n }\n }\n\n return {\n bodyHtml: doc.body ? doc.body.innerHTML : '',\n headExtrasHtml: isHeadSealed(content) ? headExtras.join('') : '',\n styleContent: styleParts.join('\\n'),\n };\n };\n})();\n\nexport const HtmlPreviewIframe = memo<HtmlPreviewIframeProps>(\n ({\n animated,\n background,\n content,\n className,\n defaultHeight = DEFAULT_HEIGHT,\n ref,\n sandbox = DEFAULT_SANDBOX,\n style,\n title = 'HTML preview',\n }) => {\n const { styles } = useStyles();\n const innerRef = useRef<HTMLIFrameElement | null>(null);\n const frameId = useId();\n const [height, setHeight] = useState<number>(defaultHeight);\n // Track caller-supplied `defaultHeight` in a ref so the (frameId-keyed)\n // message handler can floor auto-height updates without re-subscribing\n // every render.\n const defaultHeightRef = useRef(defaultHeight);\n useEffect(() => {\n defaultHeightRef.current = defaultHeight;\n }, [defaultHeight]);\n\n const tooLarge = content.length > SRCDOC_MAX_LENGTH;\n\n // ── Static mode ─────────────────────────────────────────────────────\n // When the content isn't being streamed we can hand the iframe the\n // user's HTML directly. The browser's normal HTML parser runs:\n // <script src=…> tags fetch and execute as if on a regular page,\n // inline <script> blocks run in DOM order, MutationObservers (like\n // Tailwind Play CDN's) get the document at its expected lifecycle\n // stage. Anything that a model can produce as a standalone web\n // page works without special handling on our side.\n const staticSrcDoc = useMemo(() => {\n if (animated || tooLarge) return null;\n return buildStaticSrcDoc({ background, content, frameId });\n }, [animated, background, content, frameId, tooLarge]);\n\n // ── Shell mode ─────────────────────────────────────────────────────\n // For streaming we keep one shell iframe loaded for the lifetime of\n // the session and pump content updates through postMessage. The\n // shell's morph script handles in-place DOM diffing + fade-in for\n // new nodes (see buildShellSrcDoc.ts). Tradeoff: external <script\n // src> tags appended this way don't always integrate cleanly with\n // class-engine CDNs, so static content is preferred when possible.\n const shellSrcDoc = useMemo(() => {\n if (!animated || tooLarge) return null;\n return buildShellSrcDoc({ background, frameId });\n }, [animated, background, frameId, tooLarge]);\n\n const [shellReady, setShellReady] = useState(false);\n useEffect(() => {\n // Each time we swap between shell and static modes (or rebuild the\n // shell because the theme changed) we need to wait for a fresh\n // ready ping before posting content.\n setShellReady(false);\n }, [shellSrcDoc]);\n\n const payload = useMemo<Payload | null>(() => {\n if (!animated || tooLarge) return null;\n return parseContent(content);\n }, [animated, content, tooLarge]);\n\n // Push content into the shell iframe whenever it changes — but only\n // after the shell has signalled ready, so its listener exists.\n useEffect(() => {\n if (!animated) return;\n if (!shellReady || !payload) return;\n const win = innerRef.current?.contentWindow;\n if (!win) return;\n win.postMessage(\n {\n frameId,\n payload,\n type: SHELL_UPDATE_MESSAGE_TYPE,\n },\n '*',\n );\n }, [animated, payload, shellReady, frameId]);\n\n useEffect(() => {\n const handler = (event: MessageEvent) => {\n const data = event.data;\n if (!data || typeof data !== 'object') return;\n if (data.frameId !== frameId) return;\n if (event.source !== innerRef.current?.contentWindow) return;\n\n if (data.type === `${SHELL_UPDATE_MESSAGE_TYPE}:ready`) {\n setShellReady(true);\n return;\n }\n\n if (data.type === AUTO_HEIGHT_MESSAGE_TYPE) {\n const next = Number(data.height);\n if (!Number.isFinite(next) || next <= 0) return;\n // Floor at `defaultHeight`. During streaming the shell body\n // briefly reports a small height between morph commits (empty\n // body just after head closes, then partial body, etc.) and on\n // every iframe remount the auto-height starts at body padding\n // before climbing back to content height. Letting the iframe\n // shrink to that interim height causes a visible up/down jitter\n // that reads as flicker — especially under a Markdown wrapper\n // that re-renders every chunk. Anchoring to the caller's stated\n // minimum height eliminates that without affecting the final\n // size: real content taller than `defaultHeight` grows the\n // iframe; content shorter than it stays at the floor.\n const floored = Math.max(next, defaultHeightRef.current);\n setHeight((prev) => (Math.abs(prev - floored) < 1 ? prev : floored));\n }\n };\n\n window.addEventListener('message', handler);\n return () => window.removeEventListener('message', handler);\n }, [frameId]);\n\n const setRef = useCallback(\n (node: HTMLIFrameElement | null) => {\n innerRef.current = node;\n if (typeof ref === 'function') ref(node);\n else if (ref) (ref as { current: HTMLIFrameElement | null }).current = node;\n },\n [ref],\n );\n\n if (tooLarge) {\n return (\n <div className={cx(styles.fallback, className)} style={style}>\n Content too large to preview inline.\n </div>\n );\n }\n\n const srcDoc = staticSrcDoc ?? shellSrcDoc ?? '';\n\n // Key the iframe by mode so React fully unmounts the previous DOM\n // element when we switch from shell (streaming) to static (finalised).\n // Setting iframe.srcdoc on an already-loaded element doesn't reliably\n // re-navigate in Chromium when the previous document was also srcdoc-\n // based — the new srcdoc attribute lands, but the document doesn't\n // reload, so the user sees stale (often empty) shell content. A fresh\n // element forces the browser to parse and load the new srcdoc.\n const iframeKey = animated ? 'shell' : 'static';\n\n return (\n <iframe\n className={cx(styles.iframe, className)}\n key={iframeKey}\n ref={setRef}\n sandbox={sandbox}\n srcDoc={srcDoc}\n style={{ height, ...style }}\n title={title}\n />\n );\n },\n);\n\nHtmlPreviewIframe.displayName = 'HtmlPreviewIframe';\n\nexport default HtmlPreviewIframe;\n"],"mappings":";;;;;;;;;AAWA,MAAM,YAAY,cAAc,EAAE,KAAK,cAAc;CACnD,UAAU,GAAG;;;aAGF,OAAO,qBAAqB;;CAEvC,QAAQ,GAAG;;;;;;CAMZ,EAAE;AAyBH,MAAM,oBAAoB;AAC1B,MAAM,gBAAgB,QAAyB,kBAAkB,KAAK,IAAI;AAE1E,MAAM,sBAAsB;CAE1B,IAAI,SAA2B;AAC/B,SAAQ,YAAoC;AAC1C,MAAI,OAAO,WAAW,YAAa,QAAO;AAC1C,MAAI,CAAC,QAAS,QAAO;GAAE,UAAU;GAAI,gBAAgB;GAAI,cAAc;GAAI;AAC3E,MAAI,CAAC,OAAQ,UAAS,IAAI,WAAW;EACrC,MAAM,MAAM,OAAO,gBAAgB,SAAS,YAAY;EAExD,MAAM,aAAuB,EAAE;EAC/B,MAAM,aAAuB,EAAE;AAE/B,MAAI,IAAI,KACN,MAAK,MAAM,SAAS,MAAM,KAAK,IAAI,KAAK,SAAS,CAC/C,KAAI,MAAM,YAAY,QACpB,YAAW,KAAK,MAAM,eAAe,GAAG;MAExC,YAAW,KAAK,MAAM,UAAU;AAKtC,SAAO;GACL,UAAU,IAAI,OAAO,IAAI,KAAK,YAAY;GAC1C,gBAAgB,aAAa,QAAQ,GAAG,WAAW,KAAK,GAAG,GAAG;GAC9D,cAAc,WAAW,KAAK,KAAK;GACpC;;IAED;AAEJ,MAAa,oBAAoB,MAC9B,EACC,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