From 9881d69def27228b7c57c61954d549773b78ddb0 Mon Sep 17 00:00:00 2001 From: katherinehhh Date: Thu, 10 Aug 2023 14:52:29 +0800 Subject: [PATCH] refactor(schema-template): Inherit collection using blockTemplate (#2418) * refactor: blockTemplate * refactor: code imrove --- .../schema-initializer/items/RecordFormBlockInitializer.tsx | 5 +++-- .../items/RecordReadPrettyFormBlockInitializer.tsx | 5 +++-- .../src/schema-templates/SchemaTemplateManagerProvider.tsx | 6 +++++- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/packages/core/client/src/schema-initializer/items/RecordFormBlockInitializer.tsx b/packages/core/client/src/schema-initializer/items/RecordFormBlockInitializer.tsx index e4933f9d0..ec58f4795 100644 --- a/packages/core/client/src/schema-initializer/items/RecordFormBlockInitializer.tsx +++ b/packages/core/client/src/schema-initializer/items/RecordFormBlockInitializer.tsx @@ -9,7 +9,8 @@ import { createFormBlockSchema, useRecordCollectionDataSourceItems } from '../ut export const RecordFormBlockInitializer = (props) => { const { onCreateBlockSchema, componentType, createBlockSchema, insert, targetCollection, ...others } = props; const { getTemplateSchemaByMode } = useSchemaTemplateManager(); - const collection = targetCollection || useCollection(); + const currentCollection = useCollection(); + const collection = targetCollection || currentCollection; const association = useBlockAssociationContext(); return ( { ); } }} - items={useRecordCollectionDataSourceItems('FormItem')} + items={useRecordCollectionDataSourceItems('FormItem', null, collection?.name)} /> ); }; diff --git a/packages/core/client/src/schema-initializer/items/RecordReadPrettyFormBlockInitializer.tsx b/packages/core/client/src/schema-initializer/items/RecordReadPrettyFormBlockInitializer.tsx index 5643ea027..761a59759 100644 --- a/packages/core/client/src/schema-initializer/items/RecordReadPrettyFormBlockInitializer.tsx +++ b/packages/core/client/src/schema-initializer/items/RecordReadPrettyFormBlockInitializer.tsx @@ -17,7 +17,8 @@ export const RecordReadPrettyFormBlockInitializer = (props) => { ...others } = props; const { getTemplateSchemaByMode } = useSchemaTemplateManager(); - const collection = targetCollection || useCollection(); + const currentCollection = useCollection(); + const collection = targetCollection || currentCollection; const association = useBlockAssociationContext(); const { block } = useBlockRequestContext(); const actionInitializers = @@ -61,7 +62,7 @@ export const RecordReadPrettyFormBlockInitializer = (props) => { ); } }} - items={useRecordCollectionDataSourceItems('ReadPrettyFormItem')} + items={useRecordCollectionDataSourceItems('ReadPrettyFormItem', null, collection?.name)} /> ); }; diff --git a/packages/core/client/src/schema-templates/SchemaTemplateManagerProvider.tsx b/packages/core/client/src/schema-templates/SchemaTemplateManagerProvider.tsx index c89380445..cf09dc19c 100644 --- a/packages/core/client/src/schema-templates/SchemaTemplateManagerProvider.tsx +++ b/packages/core/client/src/schema-templates/SchemaTemplateManagerProvider.tsx @@ -5,6 +5,7 @@ import { cloneDeep } from 'lodash'; import React, { createContext, ReactNode, useContext, useMemo } from 'react'; import { useAPIClient, useRequest } from '../api-client'; import { Plugin } from '../application/Plugin'; +import { useCollectionManager } from '../collection-manager'; import { BlockTemplate } from './BlockTemplate'; export const SchemaTemplateManagerContext = createContext({}); @@ -35,6 +36,7 @@ export const useSchemaTemplate = () => { }; export const useSchemaTemplateManager = () => { + const { getInheritCollections } = useCollectionManager(); const { refresh, templates = [] } = useContext(SchemaTemplateManagerContext); const api = useAPIClient(); return { @@ -100,7 +102,9 @@ export const useSchemaTemplateManager = () => { return templates?.find((template) => template.key === key); }, getTemplatesByCollection(collectionName: string, resourceName: string = null) { - const items = templates?.filter?.((template) => template.collectionName === collectionName); + const parentCollections = getInheritCollections(collectionName); + const totalCollections = parentCollections.concat([collectionName]); + const items = templates?.filter?.((template) => totalCollections.includes(template.collectionName)); return items || []; }, getTemplatesByComponentName(componentName: string): Array {