feat: 增加可视化模板页面编辑器

This commit is contained in:
que01 committed 2026-10-08 18:00:36 +08:00
1 parent 9d2038cc93
commit c5af790554
24 files changed
+773 -129

No files matched your search

@@ -0,0 +1,135 @@
import { useEffect, useMemo, useRef, useState } from "react";
import { Alert, Spin } from "antd";
import type {
BrandProfile,
EditorPreviewFrameMessage,
EditorPreviewHostMessage,
} from "@personal-brand/api-client";
import { templatePreviewUrl } from "../../lib/template-preview.js";
import "./TemplatePreview.css";
export function TemplatePreview({
brand,
page,
targetKey,
route,
onTargetSelected,
}: {
brand: BrandProfile;
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 origin = useMemo(() => (url ? new URL(url).origin : ""), [url]);
const brandDraft = useMemo(
() => ({
name: brand.name,
tagline: brand.tagline,
description: brand.description,
avatarUrl: brand.avatarUrl,
coverUrl: brand.coverUrl,
hours: brand.hours,
address: brand.address,
deliveryNote: brand.deliveryNote,
contactPhone: brand.contactPhone,
contactWechat: brand.contactWechat,
config: brand.config,
}),
[brand],
);
useEffect(() => {
setReady(false);
setError("");
}, [url]);
useEffect(() => {
const onMessage = (event: MessageEvent<EditorPreviewFrameMessage>) => {
if (event.source !== frame.current?.contentWindow || event.origin !== origin) return;
const message = event.data;
if (message?.protocol !== "personal-brand-editor-preview" || message.version !== 1) return;
if (message.type === "ready") setReady(true);
if (message.type === "targetSelected" && message.page === page)
onTargetSelected(message.targetKey);
};
window.addEventListener("message", onMessage);
return () => window.removeEventListener("message", onMessage);
}, [onTargetSelected, origin, page]);
useEffect(() => {
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 (targetKey)
send({
protocol: "personal-brand-editor-preview",
version: 1,
type: "focusTarget",
targetKey,
});
}, [brandDraft, origin, ready, targetKey]);
useEffect(() => {
const refresh = () => setReloadKey((value) => value + 1);
window.addEventListener("template-preview-refresh", refresh);
return () => window.removeEventListener("template-preview-refresh", refresh);
}, []);
if (!url)
return (
<Alert
type="warning"
showIcon
message="尚未配置模板预览服务"
description="请在 Admin 环境变量 VITE_TEMPLATE_PREVIEW_URLS 中为模板配置 H5 预览地址。"
/>
);
return (
<div className="template-preview">
{!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 }}
/>
</div>
);
}