feat: 细化 Area 类型,优化 IWidgetBaseConfig 及相关的类型代码

This commit is contained in:
humphry.hy 2021-11-11 17:06:48 +08:00
parent f160282441
commit b07436d444
4 changed files with 43 additions and 25 deletions

View File

@ -11,6 +11,7 @@ import {
isPanelConfig, isPanelConfig,
DividerConfig, DividerConfig,
isDividerConfig, isDividerConfig,
IWidgetConfigArea,
} from './types'; } from './types';
import Panel, { isPanel } from './widget/panel'; import Panel, { isPanel } from './widget/panel';
import WidgetContainer from './widget/widget-container'; import WidgetContainer from './widget/widget-container';
@ -221,8 +222,8 @@ export class Skeleton {
Object.keys(plugins).forEach((area) => { Object.keys(plugins).forEach((area) => {
plugins[area].forEach((item) => { plugins[area].forEach((item) => {
const { pluginKey, type, props = {}, pluginProps } = item; const { pluginKey, type, props = {}, pluginProps } = item;
const config: any = { const config: Partial<IWidgetBaseConfig> = {
area, area: area as IWidgetConfigArea,
type: 'Widget', type: 'Widget',
name: pluginKey, name: pluginKey,
contentProps: pluginProps, contentProps: pluginProps,
@ -249,7 +250,7 @@ export class Skeleton {
if (pluginKey in components) { if (pluginKey in components) {
config.content = components[pluginKey]; config.content = components[pluginKey];
} }
this.add(config); this.add(config as IWidgetBaseConfig);
}); });
}); });
} }
@ -296,8 +297,8 @@ export class Skeleton {
} }
createPanel(config: PanelConfig) { createPanel(config: PanelConfig) {
config = this.parseConfig(config); const parsedConfig = this.parseConfig(config);
const panel = new Panel(this, config); const panel = new Panel(this, parsedConfig as PanelConfig);
this.panels.set(panel.name, panel); this.panels.set(panel.name, panel);
return panel; return panel;
} }
@ -316,7 +317,7 @@ export class Skeleton {
area: 'stages', area: 'stages',
...config, ...config,
}); });
return stage?.getName(); return stage?.getName?.();
} }
createContainer( createContainer(
@ -331,8 +332,8 @@ export class Skeleton {
return container; return container;
} }
private parseConfig(config: IWidgetBaseConfig): any { private parseConfig(config: IWidgetBaseConfig) {
if ((config as any).parsed) { if (config.parsed) {
return config; return config;
} }
const { content, ...restConfig } = config; const { content, ...restConfig } = config;
@ -357,8 +358,8 @@ export class Skeleton {
return restConfig; return restConfig;
} }
add(config: IWidgetBaseConfig & { area?: string }, extraConfig?: object) { add(config: IWidgetBaseConfig, extraConfig?: Record<string, any>) {
const parsedConfig: any = { const parsedConfig = {
...this.parseConfig(config), ...this.parseConfig(config),
...extraConfig, ...extraConfig,
}; };
@ -375,29 +376,29 @@ export class Skeleton {
switch (area) { switch (area) {
case 'leftArea': case 'leftArea':
case 'left': case 'left':
return this.leftArea.add(parsedConfig); return this.leftArea.add(parsedConfig as PanelDockConfig);
case 'rightArea': case 'rightArea':
case 'right': case 'right':
return this.rightArea.add(parsedConfig); return this.rightArea.add(parsedConfig as PanelConfig);
case 'topArea': case 'topArea':
case 'top': case 'top':
return this.topArea.add(parsedConfig); return this.topArea.add(parsedConfig as PanelDockConfig);
case 'toolbar': case 'toolbar':
return this.toolbar.add(parsedConfig); return this.toolbar.add(parsedConfig as PanelDockConfig);
case 'mainArea': case 'mainArea':
case 'main': case 'main':
case 'center': case 'center':
case 'centerArea': case 'centerArea':
return this.mainArea.add(parsedConfig); return this.mainArea.add(parsedConfig as PanelConfig);
case 'bottomArea': case 'bottomArea':
case 'bottom': case 'bottom':
return this.bottomArea.add(parsedConfig); return this.bottomArea.add(parsedConfig as PanelConfig);
case 'leftFixedArea': case 'leftFixedArea':
return this.leftFixedArea.add(parsedConfig); return this.leftFixedArea.add(parsedConfig as PanelConfig);
case 'leftFloatArea': case 'leftFloatArea':
return this.leftFloatArea.add(parsedConfig); return this.leftFloatArea.add(parsedConfig as PanelConfig);
case 'stages': case 'stages':
return this.stages.add(parsedConfig); return this.stages.add(parsedConfig as StageConfig);
default: default:
// do nothing // do nothing
} }

View File

@ -2,13 +2,30 @@ import { ReactElement, ComponentType } from 'react';
import { TitleContent, IconType, I18nData, TipContent } from '@ali/lowcode-types'; import { TitleContent, IconType, I18nData, TipContent } from '@ali/lowcode-types';
import { IWidget } from './widget/widget'; import { IWidget } from './widget/widget';
/**
*
*/
export type IWidgetConfigArea =
| 'leftArea' | 'left' | 'rightArea'
| 'right' | 'topArea' | 'top'
| 'toolbar' | 'mainArea' | 'main'
| 'center' | 'centerArea' | 'bottomArea'
| 'bottom' | 'leftFixedArea'
| 'leftFloatArea' | 'stages';
export interface IWidgetBaseConfig { export interface IWidgetBaseConfig {
type: string; type: string;
name: string; name: string;
area?: string; // 停靠位置, 默认 float, 如果添加非固定区, /**
props?: object; *
* type 'Panel' 'leftFloatArea'
* type 'Widget' 'mainArea'
* 'leftArea'
*/
area?: IWidgetConfigArea;
props?: Record<string, any>;
content?: any; content?: any;
contentProps?: object; contentProps?: Record<string, any>;
// index?: number; // index?: number;
[extra: string]: any; [extra: string]: any;
} }
@ -111,7 +128,7 @@ export interface PanelDockConfig extends IDockBaseConfig {
type: 'PanelDock'; type: 'PanelDock';
panelName?: string; panelName?: string;
panelProps?: PanelProps & { panelProps?: PanelProps & {
area?: string; area?: IWidgetConfigArea;
}; };
content?: string | ReactElement | ComponentType<any> | PanelConfig[]; // content for pane content?: string | ReactElement | ComponentType<any> | PanelConfig[]; // content for pane
} }

View File

@ -78,7 +78,7 @@ export default class Panel implements IWidget {
private container?: WidgetContainer<Panel, PanelConfig>; private container?: WidgetContainer<Panel, PanelConfig>;
@obx.ref private parent?: WidgetContainer; @obx.ref public parent?: WidgetContainer;
constructor(readonly skeleton: Skeleton, readonly config: PanelConfig) { constructor(readonly skeleton: Skeleton, readonly config: PanelConfig) {
makeObservable(this); makeObservable(this);

View File

@ -12,7 +12,7 @@ export interface IWidget {
readonly align?: string; readonly align?: string;
readonly isWidget: true; readonly isWidget: true;
readonly visible: boolean; readonly visible: boolean;
readonly disabled: boolean; readonly disabled?: boolean;
readonly body: ReactNode; readonly body: ReactNode;
readonly skeleton: Skeleton; readonly skeleton: Skeleton;
readonly config: IWidgetBaseConfig; readonly config: IWidgetBaseConfig;