如何從 DESIGN.md 建立設計系統

使用 SDK 上傳 DESIGN.md 檔案,並以程式化方式從中建立設計系統。

DESIGN.md 檔案以純文字定義專案的視覺識別——顏色、字體、間距與元件模式。SDK 可讓你上傳該檔案,並將其轉換為可實際套用的設計系統,由 Stitch 套用到產生的畫面上。

這是兩步驟流程:先上傳檔案,再依據該檔案建立設計系統。

上傳 DESIGN.md

import { stitch } from "@google/stitch-sdk";
import { readFileSync } from "node:fs";

const project = stitch.project("your-project-id");
const markdown = readFileSync("DESIGN.md", "utf-8");
const instance = await project.uploadDesignMd(Buffer.from(markdown, "utf-8").toString("base64"));

檔案內容必須使用 base64 編碼。Buffer.from(str, 'utf-8').toString('base64') 能正確處理所有 UTF-8 字串,包括非 ASCII 字元。回應是一個畫面執行個體,你需要將它傳給下一步。

建立設計系統

const ds = await project.createDesignSystemFromDesignMd(
  { id: instance.id, sourceScreen: instance.sourceScreen },
  "DESKTOP",
);
console.log(`Created design system: ${ds.id}`);

selectedScreenInstance 需要上傳回應中的 id 與 sourceScreen。deviceType 為選填,可使用 "DESKTOP"、"MOBILE"、"TABLET" 或 "AGNOSTIC"。

套用到畫面

建立完成後,像使用其他設計系統一樣將其套用到畫面:

const projectData = await stitch.callTool("get_project", {
  name: `projects/${project.id}`,
});

const instances = projectData.screenInstances.map((si: any) => ({
  id: si.id,
  sourceScreen: si.sourceScreen,
}));

await ds.apply(instances);

完整流程

import { stitch } from "@google/stitch-sdk";
import { readFileSync } from "node:fs";

const project = await stitch.createProject("Branded App");

// Generate screens first
const home = await project.generate("A landing page for a SaaS product");
const pricing = await project.generate("A pricing page with three tiers");

// Upload and create design system from DESIGN.md
const markdown = readFileSync("DESIGN.md", "utf-8");
const instance = await project.uploadDesignMd(Buffer.from(markdown, "utf-8").toString("base64"));
const ds = await project.createDesignSystemFromDesignMd(
  { id: instance.id, sourceScreen: instance.sourceScreen },
  "DESKTOP",
);

// Apply to all screens
const projectData = await stitch.callTool("get_project", {
  name: `projects/${project.id}`,
});
const screenInstances = projectData.screenInstances.map((si: any) => ({
  id: si.id,
  sourceScreen: si.sourceScreen,
}));
await ds.apply(screenInstances);
console.log("Design system from DESIGN.md applied to all screens");

直接使用 callTool

如果你偏好使用底層的 callTool API(適合代理程式工作流程):

const instance = await stitch.callTool("upload_design_md", {
  projectId: "your-project-id",
  designMdBase64: Buffer.from(markdown, "utf-8").toString("base64"),
});

const ds = await stitch.callTool("create_design_system_from_design_md", {
  projectId: "your-project-id",
  selectedScreenInstance: {
    id: instance.id,
    sourceScreen: instance.sourceScreen,
  },
  deviceType: "DESKTOP",
});

下一步

原文: https://stitch.withgoogle.com/docs/sdk/design-md/

本頁翻譯自 Google Stitch 官方文件,原文版權歸 Google LLC 所有。本站為獨立第三方翻譯,與 Google 無關聯。