原生外掛程式可以使用自訂 React 頁面和儀表板小工具擴充管理面板 — 沙箱外掛程式則將其 UI 描述為 Block Kit,因為將外掛程式 JavaScript 傳送到管理介面會破壞沙箱隔離。
如果您的外掛程式只需要一個設定表單,自動產生的 admin.settingsSchema 表單(參見您的第一個原生外掛程式)無需編寫任何 React 即可涵蓋大多數情況。當您需要比 settingsSchema 提供的更豐富的 UI 時,請使用自訂元件。
管理進入點
具有管理 UI 的外掛程式從 admin 進入點匯出 pages 和 widgets 物件:
import { SEOSettingsPage } from "./components/SEOSettingsPage";
import { SEODashboardWidget } from "./components/SEODashboardWidget";
export const widgets = {
"seo-overview": SEODashboardWidget,
};
export const pages = {
"/settings": SEOSettingsPage,
};
在 package.json 中設定進入點:
{
"exports": {
".": "./dist/index.js",
"./admin": "./dist/admin.js"
}
}
從 definePlugin() 引用它:
definePlugin({
id: "seo",
version: "1.0.0",
admin: {
entry: "@my-org/plugin-seo/admin",
pages: [{ path: "/settings", label: "SEO Settings", icon: "settings" }],
widgets: [{ id: "seo-overview", title: "SEO Overview", size: "half" }],
},
});
描述器需要一個匹配的 adminEntry,以便 EmDash 知道在建置時在哪裡找到元件:
adminEntry: "@my-org/plugin-seo/admin",
管理頁面
管理頁面是掛載在 /_emdash/admin/plugins/<plugin-id>/<path> 下的 React 元件。
頁面定義
admin: {
pages: [
{
path: "/settings",
label: "Settings",
icon: "settings",
},
{
path: "/reports",
label: "Reports",
icon: "chart",
},
],
}
頁面元件
import { useState, useEffect } from "react";
import { usePluginAPI } from "@emdash-cms/admin";
export function SettingsPage() {
const api = usePluginAPI();
const [settings, setSettings] = useState<Record<string, unknown>>({});
const [saving, setSaving] = useState(false);
useEffect(() => {
api.get("settings").then(setSettings);
}, []);
const handleSave = async () => {
setSaving(true);
await api.post("settings/save", settings);
setSaving(false);
};
return (
<div>
<h1>Plugin Settings</h1>
<label>
Site Title
<input
type="text"
value={(settings.siteTitle as string) || ""}
onChange={(e) => setSettings({ ...settings, siteTitle: e.target.value })}
/>
</label>
<button onClick={handleSave} disabled={saving}>
{saving ? "Saving..." : "Save Settings"}
</button>
</div>
);
}
外掛程式 API 掛鉤
usePluginAPI() 呼叫您的外掛程式路由,自動新增外掛程式 ID 前綴和 X-EmDash-Request: 1 CSRF 標頭:
import { usePluginAPI } from "@emdash-cms/admin";
function MyComponent() {
const api = usePluginAPI();
const data = await api.get("status"); // GET /_emdash/api/plugins/<id>/status
await api.post("settings/save", { enabled: true }); // 帶 JSON 本文的 POST
const result = await api.get("history?limit=50"); // 支援查詢參數
}
儀表板小工具
小工具出現在管理儀表板上,提供一目了然的資訊。
小工具定義
admin: {
widgets: [
{
id: "seo-overview",
title: "SEO Overview",
size: "half", // "full" | "half" | "third"
},
],
}
小工具元件
import { useState, useEffect } from "react";
import { usePluginAPI } from "@emdash-cms/admin";
export function SEOWidget() {
const api = usePluginAPI();
const [data, setData] = useState({ score: 0, issues: [] });
useEffect(() => {
api.get("analyze").then(setData);
}, []);
return (
<div className="widget-content">
<div className="score">{data.score}%</div>
<ul>
{data.issues.map((issue, i) => (
<li key={i}>{(issue as { message: string }).message}</li>
))}
</ul>
</div>
);
}
小工具大小
| Size | Description |
|---|---|
full | 儀表板全寬 |
half | 儀表板半寬 |
third | 儀表板三分之一寬度 |
小工具根據螢幕寬度自動換行。
匯出結構
管理進入點匯出兩個物件:
import { SettingsPage } from "./components/SettingsPage";
import { ReportsPage } from "./components/ReportsPage";
import { StatusWidget } from "./components/StatusWidget";
import { OverviewWidget } from "./components/OverviewWidget";
export const pages = {
"/settings": SettingsPage,
"/reports": ReportsPage,
};
export const widgets = {
status: StatusWidget,
overview: OverviewWidget,
};
使用管理元件
EmDash 為常見模式提供預建的元件:
import {
Card,
Button,
Input,
Select,
Toggle,
Table,
Pagination,
Alert,
Loading,
} from "@emdash-cms/admin";
function SettingsPage() {
return (
<Card title="Settings">
<Input label="API Key" type="password" />
<Toggle label="Enabled" defaultChecked />
<Button variant="primary">Save</Button>
</Card>
);
}
自動產生的設定 UI
如果您的外掛程式只需要一個設定表單,請使用 admin.settingsSchema 而無需自訂元件:
admin: {
settingsSchema: {
apiKey: { type: "secret", label: "API Key" },
enabled: { type: "boolean", label: "Enabled", default: true },
},
},
EmDash 會自動產生設定頁面。僅當您需要超出基本表單的行為時,才使用自訂 React 頁面。
導覽
外掛程式頁面出現在外掛程式名稱下的管理側邊欄中。順序與 admin.pages 陣列匹配。
admin: {
pages: [
{ path: "/settings", label: "Settings", icon: "settings" }, // 第一個
{ path: "/history", label: "History", icon: "history" }, // 第二個
{ path: "/reports", label: "Reports", icon: "chart" }, // 第三個
],
}
建置設定
管理元件需要單獨的建置進入點。設定您的打包器:
tsdown
export default {
entry: {
index: "src/index.ts",
admin: "src/admin.tsx",
},
format: "esm",
dts: true,
external: ["react", "react-dom", "emdash", "@emdash-cms/admin"],
}; tsup
export default {
entry: ["src/index.ts", "src/admin.tsx"],
format: "esm",
dts: true,
external: ["react", "react-dom", "emdash", "@emdash-cms/admin"],
}; 將 React 和 EmDash Admin 作為外部相依性保留,以避免打包重複項。
外掛程式啟用/停用
當外掛程式在管理介面中被停用時:
- 側邊欄連結被隱藏。
- 儀表板小工具不會被轉譯。
- 管理頁面傳回 404。
- 後端掛鉤仍然執行(為了資料安全)。
外掛程式可以檢查其啟用狀態:
const enabled = await ctx.kv.get<boolean>("_emdash:enabled");
完整範例
import { definePlugin } from "emdash";
import type { PluginDescriptor } from "emdash";
export function analyticsPlugin(): PluginDescriptor {
return {
id: "analytics",
version: "1.0.0",
format: "native",
entrypoint: "@my-org/plugin-analytics",
adminEntry: "@my-org/plugin-analytics/admin",
adminPages: [
{ path: "/dashboard", label: "Dashboard", icon: "chart" },
{ path: "/settings", label: "Settings", icon: "settings" },
],
adminWidgets: [{ id: "events-today", title: "Events Today", size: "third" }],
};
}
export function createPlugin() {
return definePlugin({
id: "analytics",
version: "1.0.0",
capabilities: ["network:request"],
allowedHosts: ["api.analytics.example.com"],
storage: {
events: { indexes: ["type", "createdAt"] },
},
admin: {
entry: "@my-org/plugin-analytics/admin",
settingsSchema: {
trackingId: { type: "string", label: "Tracking ID" },
enabled: { type: "boolean", label: "Enabled", default: true },
},
pages: [
{ path: "/dashboard", label: "Dashboard", icon: "chart" },
{ path: "/settings", label: "Settings", icon: "settings" },
],
widgets: [{ id: "events-today", title: "Events Today", size: "third" }],
},
routes: {
stats: {
handler: async (ctx) => {
const today = new Date().toISOString().split("T")[0];
const count = await ctx.storage.events.count({
createdAt: { gte: today },
});
return { today: count };
},
},
},
});
}
export default createPlugin;
import { EventsWidget } from "./components/EventsWidget";
import { DashboardPage } from "./components/DashboardPage";
import { SettingsPage } from "./components/SettingsPage";
export const widgets = {
"events-today": EventsWidget,
};
export const pages = {
"/dashboard": DashboardPage,
"/settings": SettingsPage,
};