Merge pull request #7399 from lurunze1226/chore-editor-panel-async

chore(amis-editor): 编辑器配置面板支持异步加载
This commit is contained in:
hsm-lv 2023-07-07 09:47:47 +08:00 committed by GitHub
commit f5f5cac304
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 82 additions and 11 deletions

View File

@ -0,0 +1,45 @@
/**
* @file AsyncLayer.tsx
* @desc
*/
import React from 'react';
import {Spinner} from 'amis';
export interface asyncLayerOptions {
fallback?: React.ReactNode;
}
export const makeAsyncLayer = (
schemaBuilderFn: () => Promise<any>,
options?: asyncLayerOptions
) => {
const {fallback} = options || {};
const LazyComponent = React.lazy(async () => {
const schemaFormRender = await schemaBuilderFn();
return {
default: (...props: any[]) => <>{schemaFormRender(...props)}</>
};
});
return (props: any) => (
<React.Suspense
fallback={
fallback && React.isValidElement(fallback) ? (
fallback
) : (
<Spinner
show
overlay
size="sm"
tip="配置面板加载中"
tipPlacement="bottom"
className="flex"
/>
)
}
>
<LazyComponent {...props} />
</React.Suspense>
);
};

View File

@ -1,7 +1,10 @@
/**
* @file interface BasePlugin
*/
import omit from 'lodash/omit';
import {RegionWrapperProps} from './component/RegionWrapper';
import {makeAsyncLayer} from './component/AsyncLayer';
import {EditorManager} from './manager';
import {EditorStoreType} from './store/editor';
import {EditorNodeType} from './store/node';
@ -13,6 +16,7 @@ import find from 'lodash/find';
import type {RendererConfig} from 'amis-core';
import type {MenuDivider, MenuItem} from 'amis-ui/lib/components/ContextMenu';
import type {BaseSchema, SchemaCollection} from 'amis';
import type {asyncLayerOptions} from './component/AsyncLayer';
/**
*
@ -799,6 +803,11 @@ export interface PluginInterface
*/
panelJustify?: boolean;
/**
*
*/
async?: {enable: boolean} & asyncLayerOptions;
/**
*
*/
@ -1044,17 +1053,34 @@ export abstract class BasePlugin implements PluginInterface {
icon: plugin.panelIcon || plugin.icon || 'fa fa-cog',
pluginIcon: plugin.pluginIcon,
title: plugin.panelTitle || '设置',
render: this.manager.makeSchemaFormRender({
definitions: plugin.panelDefinitions,
submitOnChange: plugin.panelSubmitOnChange,
api: plugin.panelApi,
body: body,
controls: plugin.panelControlsCreator
? plugin.panelControlsCreator(context)
: plugin.panelControls!,
justify: plugin.panelJustify,
panelById: store.activeId
})
render:
typeof plugin.async === 'object' && plugin.async?.enable === true
? makeAsyncLayer(async () => {
const panelBody = await body;
return this.manager.makeSchemaFormRender({
definitions: plugin.panelDefinitions,
submitOnChange: plugin.panelSubmitOnChange,
api: plugin.panelApi,
body: panelBody,
controls: plugin.panelControlsCreator
? plugin.panelControlsCreator(context)
: plugin.panelControls!,
justify: plugin.panelJustify,
panelById: store.activeId
});
}, omit(plugin.async, 'enable'))
: this.manager.makeSchemaFormRender({
definitions: plugin.panelDefinitions,
submitOnChange: plugin.panelSubmitOnChange,
api: plugin.panelApi,
body: body,
controls: plugin.panelControlsCreator
? plugin.panelControlsCreator(context)
: plugin.panelControls!,
justify: plugin.panelJustify,
panelById: store.activeId
})
});
} else if (
context.info.plugin === this &&