feat: 分离模板演示数据并校验预览资源
This commit is contained in:
1 parent
6edde5a8d6
commit
b60174885e
31 files changed
+1785
-69
No files matched your search
@@ -3,12 +3,14 @@ import type { RuntimeWorkspace } from "@personal-brand/api-client";
|
||||
import { Alert } from "antd";
|
||||
import { resolveTemplateRenderer } from "./component-renderers.jsx";
|
||||
import type { PreviewSelection, PreviewTarget } from "./preview-selection.js";
|
||||
import { TemplatePreviewAssetsContext } from "./preview-assets.js";
|
||||
import "./ComponentTemplatePreview.css";
|
||||
|
||||
type PreviewProps = {
|
||||
workspace: RuntimeWorkspace;
|
||||
page: string;
|
||||
offeringId?: string;
|
||||
assetFiles?: Record<string, { sha256: string; dataUrl: string }>;
|
||||
selection?: PreviewSelection;
|
||||
targets: PreviewTarget[];
|
||||
onTargetSelected: (selection?: PreviewSelection) => void;
|
||||
@@ -21,6 +23,7 @@ export function ComponentTemplatePreview({
|
||||
workspace,
|
||||
page,
|
||||
offeringId,
|
||||
assetFiles,
|
||||
selection,
|
||||
targets,
|
||||
onTargetSelected,
|
||||
@@ -232,7 +235,9 @@ export function ComponentTemplatePreview({
|
||||
}}
|
||||
>
|
||||
<div ref={canvas} className="component-template-preview__canvas">
|
||||
<Renderer workspace={workspace} page={page} {...(offeringId ? { offeringId } : {})} />
|
||||
<TemplatePreviewAssetsContext.Provider value={assetFiles}>
|
||||
<Renderer workspace={workspace} page={page} {...(offeringId ? { offeringId } : {})} />
|
||||
</TemplatePreviewAssetsContext.Provider>
|
||||
<div
|
||||
ref={overlay}
|
||||
className={`component-template-preview__overlay-host ${
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
import { createContext } from "react";
|
||||
|
||||
export const TemplatePreviewAssetsContext = createContext<
|
||||
Record<string, { sha256: string; dataUrl: string }> | undefined
|
||||
>(undefined);
|
||||
@@ -1,4 +1,5 @@
|
||||
import React from "react";
|
||||
import { TemplatePreviewAssetsContext } from "./preview-assets.js";
|
||||
|
||||
export const TemplateAssetContext = React.createContext("");
|
||||
const templateAssets = import.meta.glob("../../../../../packages/template-*/miniapp/assets/**/*", {
|
||||
@@ -99,10 +100,14 @@ export function Checkbox({ value, checked }) {
|
||||
export const Label = element("label");
|
||||
export const Image = React.forwardRef(({ mode, src, style, ...props }, ref) => {
|
||||
const key = React.useContext(TemplateAssetContext);
|
||||
const assets = React.useContext(TemplatePreviewAssetsContext);
|
||||
return React.createElement("img", {
|
||||
...props,
|
||||
ref,
|
||||
src: resolveImageSource(src, key),
|
||||
src:
|
||||
assets && typeof src === "string" && src.startsWith("/assets/")
|
||||
? assets[src]?.dataUrl
|
||||
: resolveImageSource(src, key),
|
||||
style: { objectFit: mode === "aspectFit" ? "contain" : "cover", ...style },
|
||||
});
|
||||
});
|
||||
@@ -249,8 +249,12 @@ export function TemplateManagementPage() {
|
||||
renderItem={(item) => (
|
||||
<List.Item>
|
||||
<List.Item.Meta
|
||||
title={`${item.action} · ${item.actor.displayName}`}
|
||||
description={`${item.fromStatus ?? "-"} -> ${item.toStatus ?? "-"} · ${new Date(item.createdAt).toLocaleString()}`}
|
||||
title={`${item.action === "previewed" ? "预览模板" : item.action} · ${item.actor.displayName}`}
|
||||
description={
|
||||
item.action === "previewed"
|
||||
? `行业 ${typeof item.metadata?.industry === "string" ? item.metadata.industry : "-"} · ${new Date(item.createdAt).toLocaleString()}`
|
||||
: `${item.fromStatus ?? "-"} -> ${item.toStatus ?? "-"} · ${new Date(item.createdAt).toLocaleString()}`
|
||||
}
|
||||
/>
|
||||
</List.Item>
|
||||
)}
|
||||
@@ -260,7 +264,12 @@ export function TemplateManagementPage() {
|
||||
{previewVersion ? (
|
||||
<TemplatePackagePreviewModal
|
||||
version={previewVersion}
|
||||
onClose={() => setPreviewVersion(null)}
|
||||
onClose={() => {
|
||||
setPreviewVersion(null);
|
||||
void queryClient.invalidateQueries({
|
||||
queryKey: ["admin-template-package-audit", previewVersion.id],
|
||||
});
|
||||
}}
|
||||
/>
|
||||
) : null}
|
||||
</div>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { useState } from "react";
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
import { Alert, Modal, Select, Typography } from "antd";
|
||||
import { Alert, Collapse, List, Modal, Select, Tag, Typography } from "antd";
|
||||
import type { TemplatePackageVersion } from "@personal-brand/api-client";
|
||||
import { createApiClient } from "../../lib/api.js";
|
||||
import { QueryState } from "../../components/QueryState.js";
|
||||
@@ -76,6 +76,20 @@ export function TemplatePackagePreviewModal({
|
||||
>
|
||||
{data ? (
|
||||
<>
|
||||
{data.preview.diagnostics.map((diagnostic) => (
|
||||
<Alert
|
||||
key={diagnostic.code}
|
||||
type="warning"
|
||||
showIcon
|
||||
message={diagnostic.message}
|
||||
/>
|
||||
))}
|
||||
<Typography.Text type="secondary">
|
||||
演示数据:
|
||||
{data.preview.fixtureSource === "previewFixture"
|
||||
? "独立预览内容"
|
||||
: "兼容起始内容"}
|
||||
</Typography.Text>
|
||||
<Typography.Paragraph type="secondary">
|
||||
{data.industry.name} · Runtime {data.compatibility.runtimeContract} · renderer{" "}
|
||||
{data.compatibility.renderer.key}@{data.compatibility.renderer.version}
|
||||
@@ -103,9 +117,65 @@ export function TemplatePackagePreviewModal({
|
||||
onChange={setOfferingId}
|
||||
/>
|
||||
) : null}
|
||||
<Collapse
|
||||
items={[
|
||||
{
|
||||
key: "modules",
|
||||
label: "页面模块与字段来源",
|
||||
children: (
|
||||
<List
|
||||
dataSource={
|
||||
data.preview.pages.find((item) => item.key === page)?.modules ?? []
|
||||
}
|
||||
locale={{ emptyText: "此页面由 Runtime 提供,不包含模板编辑模块" }}
|
||||
renderItem={(item) => (
|
||||
<List.Item>
|
||||
<div>
|
||||
<Typography.Text strong>{item.title}</Typography.Text>
|
||||
<Typography.Paragraph type="secondary">
|
||||
{item.bindings.join("、")}
|
||||
</Typography.Paragraph>
|
||||
</div>
|
||||
</List.Item>
|
||||
)}
|
||||
/>
|
||||
),
|
||||
},
|
||||
{
|
||||
key: "assets",
|
||||
label: "资源诊断",
|
||||
children: (
|
||||
<List
|
||||
dataSource={data.preview.assets}
|
||||
locale={{ emptyText: "模板未声明资源槽位" }}
|
||||
renderItem={(item) => (
|
||||
<List.Item>
|
||||
<div>
|
||||
{item.label}{" "}
|
||||
<Tag color={item.status === "missing" ? "warning" : "default"}>
|
||||
{item.status === "missing"
|
||||
? "内容缺失"
|
||||
: item.status === "ready"
|
||||
? "包内资源已验证"
|
||||
: "可用性待验证"}
|
||||
</Tag>
|
||||
{item.required ? "必需" : "可选"} · 已配置 {item.populated}/
|
||||
{item.total}
|
||||
<Typography.Paragraph type="secondary">
|
||||
{item.binding}
|
||||
</Typography.Paragraph>
|
||||
</div>
|
||||
</List.Item>
|
||||
)}
|
||||
/>
|
||||
),
|
||||
},
|
||||
]}
|
||||
/>
|
||||
<ComponentTemplatePreview
|
||||
key={`${version.id}:${industry}:${page}`}
|
||||
workspace={data.workspace}
|
||||
assetFiles={data.preview.assetFiles}
|
||||
page={page}
|
||||
{...((offeringId ?? data.workspace.offerings[0]?.id)
|
||||
? { offeringId: offeringId ?? data.workspace.offerings[0]!.id }
|
||||
|
||||
Reference in new issue
Block a user