ネイティブプラグインは、カスタム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" }],
},
});
ディスクリプタには、EmDashがビルド時にコンポーネントを見つける場所を知るための一致するadminEntryが必要です:
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 | ダッシュボードの1/3幅 |
ウィジェットは画面の幅に基づいて自動的に折り返されます。
エクスポート構造
管理エントリーポイントは2つのオブジェクトをエクスポートします:
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" }, // 2番目
{ path: "/reports", label: "Reports", icon: "chart" }, // 3番目
],
}
ビルド設定
管理コンポーネントには別個のビルドエントリーポイントが必要です。バンドラーを設定します:
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,
};