feat(skeleton): add registerConfigTransducer API

This commit is contained in:
liujuping 2023-11-23 15:32:54 +08:00 committed by 林熠
parent 029fd1ce67
commit a3fef9c13d
6 changed files with 124 additions and 4 deletions

View File

@ -295,6 +295,68 @@ showArea(areaName: string): void;
*/
hideArea(areaName: string): void;
```
### registerConfigTransducer
注册一个面板的配置转换器transducer
```typescript
/**
* 注册一个面板的配置转换器transducer
* Registers a configuration transducer for a panel.
* @param {IPublicTypeConfigTransducer} transducer
* - 要注册的转换器函数。该函数接受一个配置对象(类型为 IPublicTypeSkeletonConfig作为输入并返回修改后的配置对象。
* - The transducer function to be registered. This function takes a configuration object
*
* @param {number} level
* - 转换器的优先级。优先级较高的转换器会先执行。
* - The priority level of the transducer. Transducers with higher priority levels are executed first.
*
* @param {string} [id]
* - (可选)转换器的唯一标识符。用于在需要时引用或操作特定的转换器。
* - (Optional) A unique identifier for the transducer. Used for referencing or manipulating a specific transducer when needed.
*/
registerConfigTransducer(transducer: IPublicTypeConfigTransducer, level: number, id?: string): void;
```
使用示例
```typescript
import { IPublicModelPluginContext, IPublicTypeSkeletonConfig } from '@alilc/lowcode-types';
function updatePanelWidth(config: IPublicTypeSkeletonConfig) {
if (config.type === 'PanelDock') {
return {
...config,
panelProps: {
...(config.panelProps || {}),
width: 240,
},
}
}
return config;
}
const controlPanelWidthPlugin = (ctx: IPublicModelPluginContext) => {
const { skeleton } = ctx;
(skeleton as any).registerConfigTransducer?.(updatePanelWidth, 1, 'update-panel-width');
return {
init() {},
};
};
controlPanelWidthPlugin.pluginName = 'controlPanelWidthPlugin';
controlPanelWidthPlugin.meta = {
dependencies: [],
engines: {
lowcodeEngine: '^1.2.3', // 插件需要配合 ^1.0.0 的引擎才可运行
},
};
export default controlPanelWidthPlugin;
```
## 事件
### onShowPanel

View File

@ -28,6 +28,7 @@ import {
IPublicTypeWidgetConfigArea,
IPublicTypeSkeletonConfig,
IPublicApiSkeleton,
IPublicTypeConfigTransducer,
} from '@alilc/lowcode-types';
const logger = new Logger({ level: 'warn', bizName: 'skeleton' });
@ -111,6 +112,8 @@ export interface ISkeleton extends Omit<IPublicApiSkeleton,
export class Skeleton implements ISkeleton {
private panels = new Map<string, Panel>();
private configTransducers: IPublicTypeConfigTransducer[] = [];
private containers = new Map<string, WidgetContainer<any>>();
readonly leftArea: Area<DockConfig | PanelDockConfig | DialogDockConfig>;
@ -448,11 +451,35 @@ export class Skeleton implements ISkeleton {
return restConfig;
}
registerConfigTransducer(
transducer: IPublicTypeConfigTransducer,
level = 100,
id?: string,
) {
transducer.level = level;
transducer.id = id;
const i = this.configTransducers.findIndex((item) => item.level != null && item.level > level);
if (i < 0) {
this.configTransducers.push(transducer);
} else {
this.configTransducers.splice(i, 0, transducer);
}
}
getRegisteredConfigTransducers(): IPublicTypeConfigTransducer[] {
return this.configTransducers;
}
add(config: IPublicTypeSkeletonConfig, extraConfig?: Record<string, any>): IWidget | Widget | Panel | Stage | Dock | PanelDock | undefined {
const parsedConfig = {
const registeredTransducers = this.getRegisteredConfigTransducers();
const parsedConfig = registeredTransducers.reduce((prevConfig, current) => {
return current(prevConfig);
}, {
...this.parseConfig(config),
...extraConfig,
};
});
let { area } = parsedConfig;
if (!area) {
if (parsedConfig.type === 'Panel') {

View File

@ -4,7 +4,7 @@ import {
SkeletonEvents,
} from '@alilc/lowcode-editor-skeleton';
import { skeletonSymbol } from '../symbols';
import { IPublicApiSkeleton, IPublicModelSkeletonItem, IPublicTypeDisposable, IPublicTypeSkeletonConfig, IPublicTypeWidgetConfigArea } from '@alilc/lowcode-types';
import { IPublicApiSkeleton, IPublicModelSkeletonItem, IPublicTypeConfigTransducer, IPublicTypeDisposable, IPublicTypeSkeletonConfig, IPublicTypeWidgetConfigArea } from '@alilc/lowcode-types';
import { getLogger } from '@alilc/lowcode-utils';
import { SkeletonItem } from '../model/skeleton-item';
@ -208,6 +208,10 @@ export class Skeleton implements IPublicApiSkeleton {
});
return () => editor.eventBus.off(SkeletonEvents.WIDGET_HIDE, listener);
}
registerConfigTransducer(fn: IPublicTypeConfigTransducer, level: number, id?: string) {
this[skeletonSymbol].registerConfigTransducer(fn, level, id);
}
}
function normalizeArea(area: IPublicTypeWidgetConfigArea | undefined): 'leftArea' | 'rightArea' | 'topArea' | 'toolbar' | 'mainArea' | 'bottomArea' | 'leftFixedArea' | 'leftFloatArea' | 'stages' | 'subTopArea' {

View File

@ -1,5 +1,5 @@
import { IPublicModelSkeletonItem } from '../model';
import { IPublicTypeDisposable, IPublicTypeSkeletonConfig } from '../type';
import { IPublicTypeConfigTransducer, IPublicTypeDisposable, IPublicTypeSkeletonConfig } from '../type';
export interface IPublicApiSkeleton {
@ -114,4 +114,21 @@ export interface IPublicApiSkeleton {
* @returns
*/
onHideWidget(listener: (...args: any[]) => void): IPublicTypeDisposable;
/**
* transducer
* Registers a configuration transducer for a panel.
* @param {IPublicTypeConfigTransducer} transducer
* - IPublicTypeSkeletonConfig
* - The transducer function to be registered. This function takes a configuration object (of type IPublicTypeSkeletonConfig) as input and returns a modified configuration object.
*
* @param {number} level
* -
* - The priority level of the transducer. Transducers with higher priority levels are executed first.
*
* @param {string} [id]
* -
* - (Optional) A unique identifier for the transducer. Used for referencing or manipulating a specific transducer when needed.
*/
registerConfigTransducer(transducer: IPublicTypeConfigTransducer, level: number, id?: string): void;
}

View File

@ -0,0 +1,9 @@
import { IPublicTypeSkeletonConfig } from '.';
export interface IPublicTypeConfigTransducer {
(prev: IPublicTypeSkeletonConfig): IPublicTypeSkeletonConfig;
level?: number;
id?: string;
}

View File

@ -91,3 +91,4 @@ export * from './hotkey-callback-config';
export * from './hotkey-callbacks';
export * from './scrollable';
export * from './simulator-renderer';
export * from './config-transducer';