fix: LiveCanvas now always renders when user toggles to Preview mode
This commit is contained in:
@@ -38,11 +38,10 @@ const LiveCanvas = memo(({ data, type, isStreaming }: { data: string, type: stri
|
|||||||
const iframeRef = useRef<HTMLIFrameElement>(null);
|
const iframeRef = useRef<HTMLIFrameElement>(null);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!iframeRef.current) return;
|
if (!iframeRef.current || !data) return;
|
||||||
|
|
||||||
const isHtml = data.includes("<") && data.includes(">");
|
// Decode HTML entities if present
|
||||||
const isEncodedHtml = data.includes("<") && data.includes(">");
|
const isEncodedHtml = data.includes("<") && data.includes(">");
|
||||||
const shouldRender = isHtml || isEncodedHtml || ["web", "app", "design", "html", "ui"].includes(type);
|
|
||||||
const normalized = isEncodedHtml
|
const normalized = isEncodedHtml
|
||||||
? data
|
? data
|
||||||
.replace(/</g, "<")
|
.replace(/</g, "<")
|
||||||
@@ -51,8 +50,20 @@ const LiveCanvas = memo(({ data, type, isStreaming }: { data: string, type: stri
|
|||||||
.replace(/"/g, "\"")
|
.replace(/"/g, "\"")
|
||||||
.replace(/'/g, "'")
|
.replace(/'/g, "'")
|
||||||
: data;
|
: data;
|
||||||
if (shouldRender) {
|
|
||||||
const doc = `
|
// Check if the content is a full HTML document or a fragment
|
||||||
|
const isFullDocument = normalized.trim().startsWith("<!DOCTYPE") || normalized.trim().startsWith("<html");
|
||||||
|
|
||||||
|
let doc: string;
|
||||||
|
if (isFullDocument) {
|
||||||
|
// If it's a full document, inject Tailwind CSS but keep the structure
|
||||||
|
doc = normalized.replace(/<head>/i, `<head>
|
||||||
|
<script src="https://cdn.tailwindcss.com"></script>
|
||||||
|
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Space+Grotesk:wght@400;600;700;800&display=swap">
|
||||||
|
`);
|
||||||
|
} else {
|
||||||
|
// Wrap fragments in a styled container
|
||||||
|
doc = `
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html class="dark">
|
<html class="dark">
|
||||||
<head>
|
<head>
|
||||||
@@ -81,19 +92,20 @@ const LiveCanvas = memo(({ data, type, isStreaming }: { data: string, type: stri
|
|||||||
margin: 0;
|
margin: 0;
|
||||||
padding: 24px;
|
padding: 24px;
|
||||||
font-family: "Space Grotesk", "IBM Plex Sans", system-ui, sans-serif;
|
font-family: "Space Grotesk", "IBM Plex Sans", system-ui, sans-serif;
|
||||||
background: #0b1414;
|
background: #f8fafc;
|
||||||
color: #ecfdf3;
|
color: #1e293b;
|
||||||
min-height: 100vh;
|
min-height: 100vh;
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
<body class="bg-[#0b1414] text-emerald-50">
|
<body>
|
||||||
${normalized}
|
${normalized}
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
`;
|
`;
|
||||||
iframeRef.current.srcdoc = doc;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
iframeRef.current.srcdoc = doc;
|
||||||
}, [data, type]);
|
}, [data, type]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -624,7 +636,7 @@ export default function AIAssist() {
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="flex-1 overflow-hidden relative">
|
<div className="flex-1 overflow-hidden relative">
|
||||||
{viewMode === "preview" && previewData && canRenderPreview ? (
|
{viewMode === "preview" && previewData ? (
|
||||||
<LiveCanvas
|
<LiveCanvas
|
||||||
data={previewData.data}
|
data={previewData.data}
|
||||||
type={previewData.type}
|
type={previewData.type}
|
||||||
|
|||||||
Reference in New Issue
Block a user