こんにちは。Pluginの作成について教えてください。
OneTwoSmallさんのPlugin-Enhanced-table-Blockを参考に、
AddBlockに追加されるように作成しましたが、AddBlockに表示されてきません。どのように変更をすれば、表示されてくるのでしょうか。わかるかたがいれば、よろしくお願いいたします。
//schema-viewer/src/client-v2/plugin.tsx
import { Plugin } from ‘@nocobase/client’;
import { SchemaViewer } from ‘./components/SchemaViewer’;
import { SchemaViewerBlockInitializer } from ‘./initializer/SchemaViewerBlockInitializer’;
export class SchemaViewerPlugin extends Plugin {
async load() {
// 1. システムに各Reactコンポーネントを名前付きで登録
this.app.addComponents({
SchemaViewer,
SchemaViewerBlockInitializer,
});
// 2. 有志プラグインと同じメソッド・引数構造でメニューへ相乗り登録
this.app.schemaInitializerManager.addItem('page:addBlock', 'dataBlocks.schemaViewer', {
title: '項目定義ビューワー (Schema Viewer)',
Component: 'SchemaViewerBlockInitializer', // 大文字始まり必須
});
this.app.schemaInitializerManager.addItem('RecordBlockInitializers', 'dataBlocks.schemaViewer', {
title: '項目定義ビューワー (Schema Viewer)',
Component: 'SchemaViewerBlockInitializer',
});
}
}
export default SchemaViewerPlugin;
//schema-viewer/src/client-v2/components/SchemaViewer.tsx
import React, { useEffect, useState } from ‘react’;
import { useAPIClient, useCollection } from ‘@nocobase/client’;
import { Card } from ‘antd’;
export const SchemaViewer = () => {
const api = useAPIClient();
const collection = useCollection();
const collectionName = collection?.name; // 安全に選択されたテーブル名を取得
const [fieldsJson, setFieldsJson] = useState(‘’);
useEffect(() => {
if (!collectionName) return;
// メタデータAPIからフィールド定義を叩く
api.request({ url: `/collections/${collectionName}/fields` })
.then(res => {
// 今回指定されたレベル(name, type, interface, title等)に綺麗にフィルタリング
const cleanFields = res.data?.data?.map((f: any) => ({
name: f.name,
type: f.type,
interface: f.interface,
...(f.primaryKey && { primaryKey: f.primaryKey }),
...(f.autoIncrement && { autoIncrement: f.autoIncrement }),
...(f.target && { target: f.target }),
uiSchema: { title: f.uiSchema?.title }
}));
setFieldsJson(JSON.stringify(cleanFields, null, 2));
})
.catch(err => {
console.error('定義の取得に失敗しました:', err);
});
}, [collectionName]);
if (!collectionName) {
return コレクションデータを読み込めません。データブロック内に配置してください。;
}
return (
<Card title={「${collectionName}」の簡易項目定義 (JSON)} style={{ marginBottom: 24 }}>
<pre style={{
maxHeight: 400,
overflow: ‘auto’,
backgroundColor: ‘#fafafa’,
padding: 12,
borderRadius: 4,
fontFamily: ‘monospace’
}}>
{fieldsJson}
);
};
//schema-viewer/src/client-v2/initializer/SchemaViewerBlockInitializer.tsx
import React from ‘react’;
import {
useSchemaInitializer,
DataBlockInitializer,
useSchemaInitializerItem,
} from ‘@nocobase/client’;
import { TableOutlined } from ‘@ant-design/icons’;
export const SchemaViewerBlockInitializer = (props: any) => {
const { insert } = useSchemaInitializer();
const itemConfig = useSchemaInitializerItem(); // 既存のコレクション一覧メタデータを同期
return (
<DataBlockInitializer
{…itemConfig} // これにより有志のコードと同じく、既存テーブルのリストがメニューに展開されます
icon={}
componentType={‘SchemaViewer’} // 内部のコンポーネントタイプ識別子
onCreateBlockSchema={async (options: any) => {
// テーブルが選択されたときに、ページに挿入するシンプルな設計図
const schema = {
type: ‘void’,
‘x-decorator’: ‘TableBlockProvider’,
‘x-decorator-props’: {
collection: options.item.name, // 選択されたコレクション(テーブル)の名前
dataSource: options.item.dataSource,
},
‘x-component’: ‘CardItem’, // 白枠カードのデザインを適用
properties: {
viewerBody: {
type: ‘void’,
‘x-component’: ‘SchemaViewer’, // 次のファイルで作るJSON表示コンポーネントを呼ぶ
},
},
};
// 組み立てた設計図を画面に反映(インサート)
insert(schema);
}}
/>
);
};