diff --git a/packages/core/client/src/application/schema-initializer/hooks/useSchemaInitializerRender.tsx b/packages/core/client/src/application/schema-initializer/hooks/useSchemaInitializerRender.tsx index 3d42430a76..968aa3f01d 100644 --- a/packages/core/client/src/application/schema-initializer/hooks/useSchemaInitializerRender.tsx +++ b/packages/core/client/src/application/schema-initializer/hooks/useSchemaInitializerRender.tsx @@ -14,6 +14,7 @@ import { SchemaInitializerItems } from '../components'; import { SchemaInitializerButton } from '../components/SchemaInitializerButton'; import { withInitializer } from '../withInitializer'; import { SchemaInitializerOptions } from '../types'; +import { SchemaInitializer } from '../SchemaInitializer'; const InitializerComponent: FC> = React.memo((options) => { const Component: any = options.Component || SchemaInitializerButton; @@ -33,12 +34,12 @@ const InitializerComponent: FC> = React.memo( InitializerComponent.displayName = 'InitializerComponent'; export function useSchemaInitializerRender( - name: string, + name: string | SchemaInitializer, options?: Omit, 'name'>, ) { const app = useApp(); const initializer = useMemo( - () => app.schemaInitializerManager.get(name), + () => (typeof name === 'object' ? name : app.schemaInitializerManager.get(name)), [app.schemaInitializerManager, name], ); const res = useMemo(() => { diff --git a/packages/core/client/src/application/schema-settings/hooks/useSchemaSettingsRender.tsx b/packages/core/client/src/application/schema-settings/hooks/useSchemaSettingsRender.tsx index e52a64b0f0..eb5df07a43 100644 --- a/packages/core/client/src/application/schema-settings/hooks/useSchemaSettingsRender.tsx +++ b/packages/core/client/src/application/schema-settings/hooks/useSchemaSettingsRender.tsx @@ -16,6 +16,7 @@ import { SchemaSettingsProps } from '../../../schema-settings'; import { Schema } from '@formily/json-schema'; import { GeneralField } from '@formily/core'; import { Designable } from '../../../schema-component'; +import { SchemaSettings } from '../SchemaSettings'; type UseSchemaSettingsRenderOptions = Omit, 'name' | 'items'> & Omit & { @@ -24,9 +25,15 @@ type UseSchemaSettingsRenderOptions = Omit, 'nam dn?: Designable; }; -export function useSchemaSettingsRender(name: string, options?: UseSchemaSettingsRenderOptions) { +export function useSchemaSettingsRender( + name: string | SchemaSettings, + options?: UseSchemaSettingsRenderOptions, +) { const app = useApp(); - const schemaSetting = useMemo(() => app.schemaSettingsManager.get(name), [app.schemaSettingsManager, name]); + const schemaSetting = useMemo( + () => (typeof name === 'object' ? name : app.schemaSettingsManager.get(name)), + [app.schemaSettingsManager, name], + ); if (!name) { return { exists: false, diff --git a/packages/core/client/src/schema-component/antd/block-item/BlockItemError.tsx b/packages/core/client/src/schema-component/antd/block-item/BlockItemError.tsx index 842328cff1..2ef1031fae 100644 --- a/packages/core/client/src/schema-component/antd/block-item/BlockItemError.tsx +++ b/packages/core/client/src/schema-component/antd/block-item/BlockItemError.tsx @@ -12,10 +12,29 @@ import { FC } from 'react'; import { ErrorBoundary, FallbackProps } from 'react-error-boundary'; import { BlockItemCard } from './BlockItemCard'; import { ErrorFallback } from '../error-fallback'; +import { SchemaSettings } from '../../../application/schema-settings/SchemaSettings'; +import { SchemaToolbar } from '../../../schema-settings/GeneralSchemaDesigner'; + +const blockDeleteSettings = new SchemaSettings({ + name: 'blockDeleteSettings', + items: [ + { + name: 'remove', + type: 'remove', + componentProps: { + removeParentsIfNoChildren: true, + breakRemoveOn(s) { + return s['x-component'] === 'Grid'; // 其顶级是 Grid,这一层级不能删 + }, + }, + }, + ], +}); const FallbackComponent: FC = (props) => { return ( + ); diff --git a/packages/core/client/src/schema-settings/GeneralSchemaDesigner.tsx b/packages/core/client/src/schema-settings/GeneralSchemaDesigner.tsx index 6e39251c4c..c87e5a5e7b 100644 --- a/packages/core/client/src/schema-settings/GeneralSchemaDesigner.tsx +++ b/packages/core/client/src/schema-settings/GeneralSchemaDesigner.tsx @@ -14,7 +14,13 @@ import { Space } from 'antd'; import classNames from 'classnames'; import React, { FC, useEffect, useMemo, useRef } from 'react'; import { useTranslation } from 'react-i18next'; -import { SchemaToolbarProvider, useSchemaInitializerRender, useSchemaSettingsRender } from '../application'; +import { + SchemaInitializer, + SchemaSettings, + SchemaToolbarProvider, + useSchemaInitializerRender, + useSchemaSettingsRender, +} from '../application'; import { useDataSourceManager } from '../data-source/data-source/DataSourceManagerProvider'; import { useDataSource } from '../data-source/data-source/DataSourceProvider'; import { DragHandler, useCompile, useDesignable, useGridContext, useGridRowContext } from '../schema-component'; @@ -181,8 +187,8 @@ export const GeneralSchemaDesigner: FC = (props: any export interface SchemaToolbarProps { title?: string | string[]; draggable?: boolean; - initializer?: string | false; - settings?: string | false; + initializer?: string | SchemaInitializer | false; + settings?: string | SchemaSettings | false; /** * @default true */ @@ -215,11 +221,11 @@ const InternalSchemaToolbar: FC = (props) => { if (Array.isArray(title)) return title.map((item) => compile(item)); }, [compile, title]); const { render: schemaSettingsRender, exists: schemaSettingsExists } = useSchemaSettingsRender( - fieldSchema['x-settings'] || settings, + settings || fieldSchema['x-settings'], fieldSchema['x-settings-props'], ); const { render: schemaInitializerRender, exists: schemaInitializerExists } = useSchemaInitializerRender( - fieldSchema['x-initializer'] || initializer, + initializer || fieldSchema['x-initializer'], fieldSchema['x-initializer-props'], ); const rowCtx = useGridRowContext();