|
| 1 | +/** |
| 2 | + * Example: Writing a Custom Studio Plugin |
| 3 | + * |
| 4 | + * This file demonstrates how to create an external plugin for ObjectStack Studio. |
| 5 | + * A Studio plugin follows the VS Code extension model: |
| 6 | + * |
| 7 | + * 1. **Manifest (Declarative)** — Declare what you contribute (viewers, icons, groups) |
| 8 | + * 2. **Activate (Imperative)** — Register React components and handlers at runtime |
| 9 | + * |
| 10 | + * ## Quick Start |
| 11 | + * |
| 12 | + * ```tsx |
| 13 | + * import { myCustomPlugin } from './my-plugin'; |
| 14 | + * import { builtInPlugins } from './plugins/built-in'; |
| 15 | + * |
| 16 | + * // Add your plugin alongside built-ins |
| 17 | + * const allPlugins = [...builtInPlugins, myCustomPlugin]; |
| 18 | + * |
| 19 | + * <PluginRegistryProvider plugins={allPlugins}> |
| 20 | + * <App /> |
| 21 | + * </PluginRegistryProvider> |
| 22 | + * ``` |
| 23 | + */ |
| 24 | + |
| 25 | +import { defineStudioPlugin } from '@objectstack/spec/studio'; |
| 26 | +import type { StudioPlugin, MetadataViewerProps } from '../types'; |
| 27 | +import { Workflow } from 'lucide-react'; |
| 28 | + |
| 29 | +// ─── Step 1: Create your viewer component ──────────────────────────── |
| 30 | + |
| 31 | +/** |
| 32 | + * A custom viewer component for Flow metadata. |
| 33 | + * |
| 34 | + * This receives standard props from the plugin host: |
| 35 | + * - `metadataType` — The type of metadata (e.g., "flows") |
| 36 | + * - `metadataName` — The item name (e.g., "approval_flow") |
| 37 | + * - `data` — The raw metadata payload (loaded from API) |
| 38 | + * - `mode` — Current view mode ("preview" | "design" | "code" | "data") |
| 39 | + */ |
| 40 | +function FlowDesignerComponent({ metadataType, metadataName, data, mode }: MetadataViewerProps) { |
| 41 | + return ( |
| 42 | + <div className="p-6 space-y-4"> |
| 43 | + <div className="flex items-center gap-2"> |
| 44 | + <Workflow className="h-5 w-5 text-primary" /> |
| 45 | + <h2 className="text-lg font-semibold">Flow Designer</h2> |
| 46 | + <span className="text-xs text-muted-foreground">({mode} mode)</span> |
| 47 | + </div> |
| 48 | + |
| 49 | + <div className="rounded-lg border bg-card p-4"> |
| 50 | + <h3 className="font-medium">{metadataName}</h3> |
| 51 | + <p className="text-sm text-muted-foreground mt-1"> |
| 52 | + Type: {metadataType} |
| 53 | + </p> |
| 54 | + </div> |
| 55 | + |
| 56 | + {mode === 'design' && ( |
| 57 | + <div className="rounded-lg border-2 border-dashed border-primary/20 bg-primary/5 p-8 text-center"> |
| 58 | + <p className="text-sm text-muted-foreground"> |
| 59 | + 🎨 Visual flow designer canvas would go here |
| 60 | + </p> |
| 61 | + <p className="text-xs text-muted-foreground mt-1"> |
| 62 | + Drag and drop flow nodes, connect with edges, etc. |
| 63 | + </p> |
| 64 | + </div> |
| 65 | + )} |
| 66 | + |
| 67 | + {mode === 'preview' && data && ( |
| 68 | + <pre className="rounded-lg bg-muted p-4 text-xs overflow-auto"> |
| 69 | + {JSON.stringify(data, null, 2)} |
| 70 | + </pre> |
| 71 | + )} |
| 72 | + </div> |
| 73 | + ); |
| 74 | +} |
| 75 | + |
| 76 | +// ─── Step 2: Define the plugin ─────────────────────────────────────── |
| 77 | + |
| 78 | +export const flowDesignerPlugin: StudioPlugin = { |
| 79 | + /** |
| 80 | + * The manifest declares what this plugin contributes. |
| 81 | + * This is purely declarative — no React components here. |
| 82 | + */ |
| 83 | + manifest: defineStudioPlugin({ |
| 84 | + id: 'example.flow-designer', |
| 85 | + name: 'Flow Designer', |
| 86 | + version: '0.1.0', |
| 87 | + description: 'Visual flow designer for automation flows.', |
| 88 | + author: 'Example', |
| 89 | + |
| 90 | + contributes: { |
| 91 | + // Register a viewer for the "flows" metadata type |
| 92 | + metadataViewers: [ |
| 93 | + { |
| 94 | + id: 'flow-canvas', |
| 95 | + metadataTypes: ['flows'], |
| 96 | + label: 'Flow Designer', |
| 97 | + priority: 50, // Higher than default inspector (-1) |
| 98 | + modes: ['preview', 'design'], // Supports both preview and design modes |
| 99 | + }, |
| 100 | + ], |
| 101 | + |
| 102 | + // Optionally add custom actions |
| 103 | + actions: [ |
| 104 | + { |
| 105 | + id: 'validate-flow', |
| 106 | + label: 'Validate Flow', |
| 107 | + icon: 'check-circle', |
| 108 | + location: 'toolbar', |
| 109 | + metadataTypes: ['flows'], |
| 110 | + }, |
| 111 | + ], |
| 112 | + |
| 113 | + // Optionally add commands |
| 114 | + commands: [ |
| 115 | + { |
| 116 | + id: 'example.flow-designer.create', |
| 117 | + label: 'Create New Flow', |
| 118 | + shortcut: 'Ctrl+Shift+F', |
| 119 | + icon: 'plus', |
| 120 | + }, |
| 121 | + ], |
| 122 | + }, |
| 123 | + }), |
| 124 | + |
| 125 | + /** |
| 126 | + * The activate function registers runtime components and handlers. |
| 127 | + * It receives the `StudioPluginAPI` — similar to VS Code's `vscode` module. |
| 128 | + */ |
| 129 | + activate(api) { |
| 130 | + // Register the React component for our declared viewer |
| 131 | + api.registerViewer('flow-canvas', FlowDesignerComponent); |
| 132 | + |
| 133 | + // Register action handler |
| 134 | + api.registerAction('validate-flow', async (ctx) => { |
| 135 | + console.log(`Validating flow: ${ctx.metadataName}`, ctx.data); |
| 136 | + // In a real plugin, this would validate the flow structure |
| 137 | + alert(`Flow "${ctx.metadataName}" is valid! ✅`); |
| 138 | + }); |
| 139 | + |
| 140 | + // Register command handler |
| 141 | + api.registerCommand('example.flow-designer.create', () => { |
| 142 | + console.log('Creating new flow...'); |
| 143 | + // In a real plugin, this would open a creation dialog |
| 144 | + }); |
| 145 | + |
| 146 | + // Register a custom icon |
| 147 | + api.registerMetadataIcon('flows', Workflow, 'Flows'); |
| 148 | + }, |
| 149 | + |
| 150 | + /** |
| 151 | + * Optional: cleanup when the plugin is deactivated. |
| 152 | + */ |
| 153 | + deactivate() { |
| 154 | + console.log('[FlowDesigner] Plugin deactivated'); |
| 155 | + }, |
| 156 | +}; |
0 commit comments