feat: 搭建设计生成工作台骨架

This commit is contained in:
que01 committed 2026-10-15 15:54:21 +08:00
1 parent bf4bbf81c7
commit 083a323c1f
20 files changed
+587 -1

No files matched your search

+27
View File
@@ -0,0 +1,27 @@
# Personal Brand Design Lab
Design Lab 是内部设计生产工具,用 `DesignSpec` 生成可预览、可校验的移动端 HTML 设计稿。
## 开发
```bash
pnpm --filter @personal-brand/design-lab dev
```
默认端口是 `3010`,可通过 `DESIGN_LAB_PORT` 覆盖。
## 验证
```bash
pnpm --filter @personal-brand/design-spec test
pnpm --filter @personal-brand/design-lab test
pnpm --filter @personal-brand/design-spec typecheck
pnpm --filter @personal-brand/design-lab typecheck
pnpm --filter @personal-brand/design-lab build
```
## 边界
- DesignSpec 是唯一源头。
- HTML 是设计稿和转换依据,不直接进入线上项目。
- Design Lab 不连接业务数据库,也不直接写入生产模板包。
+12
View File
@@ -0,0 +1,12 @@
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Personal Brand Design Lab</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>
+24
View File
@@ -0,0 +1,24 @@
{
"name": "@personal-brand/design-lab",
"private": true,
"type": "module",
"dependencies": {
"@personal-brand/design-spec": "workspace:*",
"react": "^19.3.0",
"react-dom": "^19.3.0"
},
"scripts": {
"dev": "vite",
"build": "vite build",
"test": "node --experimental-strip-types --test test/*.test.mjs",
"lint": "eslint .",
"typecheck": "tsc -p tsconfig.json --noEmit",
"clean": "rimraf dist"
},
"devDependencies": {
"@types/react": "^19.3.0",
"@types/react-dom": "^19.3.0",
"@vitejs/plugin-react": "^6.1.1",
"vite": "^8.3.0"
}
}
+74
View File
@@ -0,0 +1,74 @@
:root {
font-family: system-ui, sans-serif;
color: #1f2328;
background: #f3f4f6;
}
body {
margin: 0;
}
.app {
display: grid;
grid-template-columns: minmax(360px, 34%) 1fr;
min-height: 100vh;
}
.app aside {
display: flex;
flex-direction: column;
gap: 16px;
padding: 20px;
background: #ffffff;
border-right: 1px solid #e5e7eb;
}
.app header {
display: flex;
align-items: center;
justify-content: space-between;
}
textarea {
min-height: 420px;
resize: vertical;
border: 1px solid #d1d5db;
border-radius: 8px;
padding: 12px;
font-family: ui-monospace, monospace;
font-size: 13px;
}
button {
border: 0;
border-radius: 8px;
background: #111827;
color: #ffffff;
font-size: 14px;
padding: 10px 16px;
}
button:disabled {
background: #9ca3af;
}
.errors {
margin: 0;
padding-left: 20px;
color: #dc2626;
font-size: 13px;
}
main {
display: grid;
place-items: center;
padding: 32px;
}
iframe {
width: 375px;
height: 720px;
border: 1px solid #d1d5db;
border-radius: 24px;
background: #ffffff;
}
+57
View File
@@ -0,0 +1,57 @@
import { useMemo, useState } from "react";
import {
DESIGN_SPEC_VERSION,
renderDesignSpec,
validateDesignSpec,
type DesignSpec,
} from "@personal-brand/design-spec";
import { sampleSpec } from "./samples";
import "./App.css";
function App() {
const [specText, setSpecText] = useState(JSON.stringify(sampleSpec, null, 2));
const [previewSrcDoc, setPreviewSrcDoc] = useState(() => renderDesignSpec(sampleSpec));
const parsed = useMemo(() => {
try {
return { value: JSON.parse(specText) as DesignSpec };
} catch (error) {
return { error: error instanceof Error ? error.message : "JSON 解析失败" };
}
}, [specText]);
const errors = "value" in parsed ? validateDesignSpec(parsed.value) : [parsed.error];
function handleRender() {
if (!("value" in parsed) || validateDesignSpec(parsed.value).length > 0) return;
setPreviewSrcDoc(renderDesignSpec(parsed.value));
}
return (
<div className="app">
<aside>
<header>
<strong>Design Lab</strong>
<span>DesignSpec v{DESIGN_SPEC_VERSION}</span>
</header>
<textarea
value={specText}
spellCheck={false}
onChange={(event) => setSpecText(event.target.value)}
/>
<button type="button" onClick={handleRender} disabled={errors.length > 0}>
生成设计稿
</button>
<ul className="errors">
{errors.map((error) => (
<li key={error}>{error}</li>
))}
</ul>
</aside>
<main>
<iframe title="设计稿预览" srcDoc={previewSrcDoc} />
</main>
</div>
);
}
export default App;
+9
View File
@@ -0,0 +1,9 @@
import { StrictMode } from "react";
import { createRoot } from "react-dom/client";
import App from "./App";
createRoot(document.getElementById("root")!).render(
<StrictMode>
<App />
</StrictMode>,
);
+35
View File
@@ -0,0 +1,35 @@
import { DESIGN_SPEC_VERSION, type DesignSpec } from "@personal-brand/design-spec";
export const sampleSpec: DesignSpec = {
version: DESIGN_SPEC_VERSION,
templateKey: "acheng-campus-food",
displayName: "阿成校园餐",
tone: "干净、亲切、可信",
theme: {
primaryColor: "#f2683c",
backgroundColor: "#f7f7f4",
surfaceColor: "#ffffff",
textColor: "#1f2328",
mutedTextColor: "#6b7280",
borderRadius: 16,
},
sections: [
{
type: "hero",
title: "每天现做的校园餐",
content: ["周三到周日供应", "下单后统一配送"],
action: "查看菜单",
},
{
type: "services",
title: "供应方式",
content: ["单餐预订", "周餐预订", "宿舍团购"],
},
{
type: "contact",
title: "联系阿成",
content: ["微信预约", "当天 16:00 截单"],
action: "提交预约",
},
],
};
+11
View File
@@ -0,0 +1,11 @@
import assert from "node:assert/strict";
import test from "node:test";
import { renderDesignSpec } from "@personal-brand/design-spec";
import { sampleSpec } from "../src/samples.ts";
test("sample renders a design preview", () => {
const html = renderDesignSpec(sampleSpec);
assert.match(html, /data-template-section="contact"/);
assert.match(html, /data-action="contact"/);
});
+13
View File
@@ -0,0 +1,13 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"jsx": "react-jsx",
"lib": ["ES2022", "DOM", "DOM.Iterable"],
"module": "ESNext",
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"noEmit": true,
"types": ["vite/client"]
},
"include": ["src", "test", "vite.config.ts"]
}
+10
View File
@@ -0,0 +1,10 @@
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
export default defineConfig({
plugins: [react()],
server: {
host: "0.0.0.0",
port: Number(process.env.DESIGN_LAB_PORT ?? 3010),
},
});