feat: 建立组件化模板预览基线并规划可视化编辑

This commit is contained in:
que01 committed 2026-10-09 12:32:24 +08:00
1 parent c5af790554
commit 0b14b5a814
46 files changed
+1880 -427

No files matched your search

@@ -4,42 +4,42 @@ import type {
BrandProfile,
EditorPreviewFrameMessage,
EditorPreviewHostMessage,
RuntimeWorkspace,
} from "@personal-brand/api-client";
import { templatePreviewUrl } from "../../lib/template-preview.js";
import { ComponentTemplatePreview } from "./ComponentTemplatePreview.js";
import "./TemplatePreview.css";
export function TemplatePreview({
brand,
workspace,
page,
targetKey,
route,
onTargetSelected,
}: {
brand: BrandProfile;
workspace?: RuntimeWorkspace;
page: string;
targetKey?: string;
route: string;
onTargetSelected: (key: string) => void;
}) {
const frame = useRef<HTMLIFrameElement>(null);
const [ready, setReady] = useState(false);
const [error, setError] = useState("");
const [reloadKey, setReloadKey] = useState(0);
const config = brand.config ?? {};
const templateKey =
typeof config.visualTemplateKey === "string" ? config.visualTemplateKey : brand.templateKey;
const url = useMemo(
() =>
templatePreviewUrl(
templateKey,
brand.slug,
page,
import.meta.env.VITE_TEMPLATE_PREVIEW_URLS,
window.location.origin,
route,
),
[brand.slug, page, reloadKey, route, templateKey],
);
const url = useMemo(() => {
const value = templatePreviewUrl(
brand.slug,
page,
import.meta.env.VITE_TEMPLATE_PREVIEW_URL || "/template-preview/index.html",
window.location.origin,
`/pages/${encodeURIComponent(page)}/index`,
);
if (!value) return undefined;
const preview = new URL(value);
preview.searchParams.set("refresh", String(reloadKey));
return preview.toString();
}, [brand.slug, page, reloadKey]);
const origin = useMemo(() => (url ? new URL(url).origin : ""), [url]);
const brandDraft = useMemo(
() => ({
@@ -80,12 +80,21 @@ export function TemplatePreview({
if (!ready || !frame.current?.contentWindow || !origin) return;
const send = (message: EditorPreviewHostMessage) =>
frame.current?.contentWindow?.postMessage(message, origin);
send({
protocol: "personal-brand-editor-preview",
version: 1,
type: "brandDraft",
brand: brandDraft,
});
if (workspace) {
send({
protocol: "personal-brand-editor-preview",
version: 1,
type: "workspaceDraft",
workspace,
});
} else {
send({
protocol: "personal-brand-editor-preview",
version: 1,
type: "brandDraft",
brand: brandDraft,
});
}
if (targetKey)
send({
protocol: "personal-brand-editor-preview",
@@ -93,7 +102,7 @@ export function TemplatePreview({
type: "focusTarget",
targetKey,
});
}, [brandDraft, origin, ready, targetKey]);
}, [brandDraft, origin, ready, targetKey, workspace]);
useEffect(() => {
const refresh = () => setReloadKey((value) => value + 1);
@@ -106,30 +115,42 @@ export function TemplatePreview({
<Alert
type="warning"
showIcon
message="尚未配置模板预览服务"
description="请在 Admin 环境变量 VITE_TEMPLATE_PREVIEW_URLS 中为模板配置 H5 预览地址。"
message="页面预览暂不可用"
description="请确认模板预览资源已构建并发布,或本地预览服务正在运行。"
/>
);
return (
<div className="template-preview">
{!ready && !error ? (
<div className="template-preview__loading">
<Spin tip="正在加载页面预览" />
</div>
{workspace ? (
<ComponentTemplatePreview
workspace={workspace}
page={page}
{...(targetKey ? { targetKey } : {})}
onTargetSelected={onTargetSelected}
/>
) : null}
{error ? (
<Alert type="error" showIcon message="页面预览加载失败" description={error} />
{!workspace ? (
<>
{!ready && !error ? (
<div className="template-preview__loading">
<Spin tip="正在加载页面预览" />
</div>
) : null}
{error ? (
<Alert type="error" showIcon message="页面预览加载失败" description={error} />
) : null}
<iframe
key={url}
ref={frame}
title="小程序页面预览"
src={url}
referrerPolicy="origin"
onError={() => setError("请检查预览服务地址和网络连接。")}
style={{ opacity: ready ? 1 : 0 }}
/>
</>
) : null}
<iframe
key={url}
ref={frame}
title="小程序页面预览"
src={url}
referrerPolicy="origin"
onError={() => setError("请检查预览服务地址和网络连接。")}
style={{ opacity: ready ? 1 : 0 }}
/>
</div>
);
}