tachybase_todo/packages/plugins/@nocobase/plugin-calendar/src/client/index.tsx
Zeke Zhang 74051ff0a5
refactor(DataBlock): kanban and gantt and map and calendar (#3792)
* refactor: kanban

* refactor: gantt

* refactor: map

* refactor: calendar

* refactor: compat

* refactor: rename to createKanbanBlockUISchema

* refactor(kanban): use x-use-component-props instead of useProps

* refactor(Gantt): rename to createGanttBlockUISchema

* refactor: use x-use-component-props instead of useProps

* refactor: rename

* refactor(Map): use x-use-component-props instead of useProps

* refactor(Calendar): rename

* refactor(Calendar): should not get collection on getting association in UISchema

* refactor(Calendar): use x-use-component-props instead of useProps

* chore: add comment

* chore: fix unit test

* fix: add scopes to fix e2e

* fix(Calendar): add association property to CalendarBlockProvider decorator

* test: add e2e for Calenndar
2024-03-27 18:06:28 +08:00

76 lines
3.0 KiB
TypeScript

import { Plugin, canMakeAssociationBlock, useCollection } from '@nocobase/client';
import { generateNTemplate } from '../locale';
import { CalendarV2 } from './calendar';
import { calendarBlockSettings } from './calendar/Calender.Settings';
import { CalendarCollectionTemplate } from './collection-templates/calendar';
import { CalendarBlockProvider, useCalendarBlockProps } from './schema-initializer/CalendarBlockProvider';
import {
CalendarActionInitializers_deprecated,
CalendarFormActionInitializers,
calendarActionInitializers,
deleteEventActionInitializer,
} from './schema-initializer/initializers';
import {
CalendarBlockInitializer,
RecordAssociationCalendarBlockInitializer,
useCreateAssociationCalendarBlock,
} from './schema-initializer/items';
import { useMemo } from 'react';
import { useCalendarBlockDecoratorProps } from './hooks/useCalendarBlockDecoratorProps';
export class PluginCalendarClient extends Plugin {
async load() {
this.app.dataSourceManager.addCollectionTemplates([CalendarCollectionTemplate]);
this.app.schemaInitializerManager.addItem('page:addBlock', 'dataBlocks.calendar', {
title: generateNTemplate('Calendar'),
Component: 'CalendarBlockInitializer',
});
this.app.schemaInitializerManager.addItem('popup:common:addBlock', 'dataBlocks.calendar', {
title: generateNTemplate('Calendar'),
Component: 'CalendarBlockInitializer',
useVisible() {
const collection = useCollection();
return useMemo(
() =>
collection.fields.some(
(field) => canMakeAssociationBlock(field) && ['hasMany', 'belongsToMany'].includes(field.type),
),
[collection.fields],
);
},
useComponentProps() {
const { createAssociationCalendarBlock } = useCreateAssociationCalendarBlock();
return {
onlyCurrentDataSource: true,
filterCollections({ associationField }) {
if (associationField) {
return ['hasMany', 'belongsToMany'].includes(associationField.type);
}
return false;
},
createBlockSchema: createAssociationCalendarBlock,
showAssociationFields: true,
};
},
});
this.app.addComponents({
CalendarBlockProvider,
CalendarBlockInitializer: CalendarBlockInitializer as any,
RecordAssociationCalendarBlockInitializer,
CalendarV2,
});
this.app.addScopes({ useCalendarBlockProps, useCalendarBlockDecoratorProps });
this.schemaSettingsManager.add(calendarBlockSettings);
this.app.schemaInitializerManager.add(CalendarActionInitializers_deprecated);
this.app.schemaInitializerManager.add(calendarActionInitializers);
this.app.schemaInitializerManager.add(CalendarFormActionInitializers);
this.app.schemaInitializerManager
.get('details:configureActions')
.add('enableActions.deleteEvent', deleteEventActionInitializer);
}
}
export default PluginCalendarClient;