Files
eip4-web/src/pages/PageRenderPage.tsx
zhengwr 306f9ebbae feat(design-center): 集中式设计中心,替代每页设计按钮
用户反馈每个功能页右上角一颗「设计」按钮散落、不合理。改为:
- 新增 /design-center 页(DesignCenterPage):列全部功能页(按菜单分组+计数+
  搜索),每页一个「设计」入口开 editor.html;复用 fetchMenu/collectLeaves。
- Topbar 加 admin-only「设计中心」入口(brush 图标),useNavigate 直达。
- PageRenderPage 移除每页 DesignButton(及 getProfile import)。

验证:浏览器 /design-center 渲染 812 页/14 分组、顶栏按钮在、点行选中并存。

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-15 17:37:06 +08:00

126 lines
4.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 页面渲染路由 /page/:templateId —— 取转换后 schema 交 amis 渲染
// F1-4:加错误边界(schema 拉取失败/JSON 异常/amis 渲染抛错均显示页内错误卡片)
// F4-L1:管理员可见「设计」按钮,打开设计器(editor.html 多页入口)
import { Component, useEffect, useState } from 'react';
import type { ReactNode } from 'react';
import { useParams } from 'react-router-dom';
import { fetchSchema } from '../api/schema';
import { AmisRenderer } from '../components/AmisRenderer';
import { recordRecentPage } from '../utils/recentPages';
import { useTabTitle } from '../shell/TabTitleContext';
// ---- amis 渲染层错误边界(class component,捕获子树抛错)----
interface ErrorBoundaryState {
caught: boolean;
message: string;
}
class AmisErrorBoundary extends Component<
{ templateId: string; children: ReactNode },
ErrorBoundaryState
> {
constructor(props: { templateId: string; children: ReactNode }) {
super(props);
this.state = { caught: false, message: '' };
}
static getDerivedStateFromError(error: unknown): ErrorBoundaryState {
const msg = error instanceof Error ? error.message : String(error);
return { caught: true, message: msg };
}
override componentDidCatch() {
// 静默记录,不向上抛
}
override render() {
if (this.state.caught) {
return (
<SchemaErrorCard
templateId={this.props.templateId}
summary={'amis 渲染异常:' + this.state.message}
/>
);
}
return this.props.children;
}
}
// ---- 统一错误卡片 ----
function SchemaErrorCard({ templateId, summary }: { templateId: string; summary: string }) {
return (
<div
data-testid="schema-error-card"
style={{
margin: 24,
padding: '20px 24px',
border: '1px solid #f5c6cb',
borderRadius: 6,
background: '#fff5f5',
color: '#721c24',
maxWidth: 640,
}}
>
<div style={{ fontWeight: 600, marginBottom: 8, fontSize: 15 }}>页面加载失败</div>
<div style={{ marginBottom: 6, fontSize: 13, color: '#555' }}>
模板 ID:<code style={{ background: '#f1f1f1', padding: '1px 4px', borderRadius: 3 }}>{templateId}</code>
</div>
<div style={{ fontSize: 13, color: '#a94442', wordBreak: 'break-all' }}>{summary}</div>
</div>
);
}
// ---- 主组件 ----
export function PageRenderPage() {
const { templateId = '' } = useParams();
const [schema, setSchema] = useState<Record<string, unknown> | null>(null);
const [error, setError] = useState('');
const { updateTabTitle } = useTabTitle();
useEffect(() => {
let alive = true;
setSchema(null);
setError('');
fetchSchema(templateId)
.then((s) => {
if (!alive) return;
// 校验顶层结构:必须是对象,否则视为 JSON 异常
if (typeof s !== 'object' || s === null || Array.isArray(s)) {
setError('schema 格式异常:顶层不是 JSON 对象');
return;
}
setSchema(s);
// 渲染成功后写入最近打开(title 取 schema.title 或 templateId)
const title = (s['title'] as string) || templateId;
recordRecentPage({ templateId, name: title, path: title });
// F2-13:回填 Tab 标题为 schema title
updateTabTitle(`/page/${templateId}`, title);
})
.catch((e: Error) => {
if (alive) setError(e.message ?? '未知错误');
});
return () => {
alive = false;
};
}, [templateId]);
if (error) {
return <SchemaErrorCard templateId={templateId} summary={error} />;
}
if (!schema) {
return (
<div style={{ padding: 24, color: '#888' }} data-testid="schema-loading">
加载中…
</div>
);
}
return (
<div style={{ height: '100%', position: 'relative' }} data-testid="amis-host">
{/* 设计入口已收敛到顶栏「设计中心」(admin),不再每页一颗按钮 */}
<AmisErrorBoundary templateId={templateId}>
<AmisRenderer schema={schema} />
</AmisErrorBoundary>
</div>
);
}