mirror of
https://github.com/alibaba/lowcode-engine.git
synced 2025-12-12 19:52:51 +00:00
feat(skeleton): add registerConfigTransducer API
This commit is contained in:
parent
029fd1ce67
commit
a3fef9c13d
@ -295,6 +295,68 @@ showArea(areaName: string): void;
|
|||||||
*/
|
*/
|
||||||
hideArea(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
|
### onShowPanel
|
||||||
|
|
||||||
|
|||||||
@ -28,6 +28,7 @@ import {
|
|||||||
IPublicTypeWidgetConfigArea,
|
IPublicTypeWidgetConfigArea,
|
||||||
IPublicTypeSkeletonConfig,
|
IPublicTypeSkeletonConfig,
|
||||||
IPublicApiSkeleton,
|
IPublicApiSkeleton,
|
||||||
|
IPublicTypeConfigTransducer,
|
||||||
} from '@alilc/lowcode-types';
|
} from '@alilc/lowcode-types';
|
||||||
|
|
||||||
const logger = new Logger({ level: 'warn', bizName: 'skeleton' });
|
const logger = new Logger({ level: 'warn', bizName: 'skeleton' });
|
||||||
@ -111,6 +112,8 @@ export interface ISkeleton extends Omit<IPublicApiSkeleton,
|
|||||||
export class Skeleton implements ISkeleton {
|
export class Skeleton implements ISkeleton {
|
||||||
private panels = new Map<string, Panel>();
|
private panels = new Map<string, Panel>();
|
||||||
|
|
||||||
|
private configTransducers: IPublicTypeConfigTransducer[] = [];
|
||||||
|
|
||||||
private containers = new Map<string, WidgetContainer<any>>();
|
private containers = new Map<string, WidgetContainer<any>>();
|
||||||
|
|
||||||
readonly leftArea: Area<DockConfig | PanelDockConfig | DialogDockConfig>;
|
readonly leftArea: Area<DockConfig | PanelDockConfig | DialogDockConfig>;
|
||||||
@ -448,11 +451,35 @@ export class Skeleton implements ISkeleton {
|
|||||||
return restConfig;
|
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 {
|
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),
|
...this.parseConfig(config),
|
||||||
...extraConfig,
|
...extraConfig,
|
||||||
};
|
});
|
||||||
|
|
||||||
let { area } = parsedConfig;
|
let { area } = parsedConfig;
|
||||||
if (!area) {
|
if (!area) {
|
||||||
if (parsedConfig.type === 'Panel') {
|
if (parsedConfig.type === 'Panel') {
|
||||||
|
|||||||
@ -4,7 +4,7 @@ import {
|
|||||||
SkeletonEvents,
|
SkeletonEvents,
|
||||||
} from '@alilc/lowcode-editor-skeleton';
|
} from '@alilc/lowcode-editor-skeleton';
|
||||||
import { skeletonSymbol } from '../symbols';
|
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 { getLogger } from '@alilc/lowcode-utils';
|
||||||
import { SkeletonItem } from '../model/skeleton-item';
|
import { SkeletonItem } from '../model/skeleton-item';
|
||||||
|
|
||||||
@ -208,6 +208,10 @@ export class Skeleton implements IPublicApiSkeleton {
|
|||||||
});
|
});
|
||||||
return () => editor.eventBus.off(SkeletonEvents.WIDGET_HIDE, listener);
|
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' {
|
function normalizeArea(area: IPublicTypeWidgetConfigArea | undefined): 'leftArea' | 'rightArea' | 'topArea' | 'toolbar' | 'mainArea' | 'bottomArea' | 'leftFixedArea' | 'leftFloatArea' | 'stages' | 'subTopArea' {
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
import { IPublicModelSkeletonItem } from '../model';
|
import { IPublicModelSkeletonItem } from '../model';
|
||||||
import { IPublicTypeDisposable, IPublicTypeSkeletonConfig } from '../type';
|
import { IPublicTypeConfigTransducer, IPublicTypeDisposable, IPublicTypeSkeletonConfig } from '../type';
|
||||||
|
|
||||||
export interface IPublicApiSkeleton {
|
export interface IPublicApiSkeleton {
|
||||||
|
|
||||||
@ -114,4 +114,21 @@ export interface IPublicApiSkeleton {
|
|||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
onHideWidget(listener: (...args: any[]) => void): IPublicTypeDisposable;
|
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;
|
||||||
}
|
}
|
||||||
|
|||||||
9
packages/types/src/shell/type/config-transducer.ts
Normal file
9
packages/types/src/shell/type/config-transducer.ts
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
import { IPublicTypeSkeletonConfig } from '.';
|
||||||
|
|
||||||
|
export interface IPublicTypeConfigTransducer {
|
||||||
|
(prev: IPublicTypeSkeletonConfig): IPublicTypeSkeletonConfig;
|
||||||
|
|
||||||
|
level?: number;
|
||||||
|
|
||||||
|
id?: string;
|
||||||
|
}
|
||||||
@ -91,3 +91,4 @@ export * from './hotkey-callback-config';
|
|||||||
export * from './hotkey-callbacks';
|
export * from './hotkey-callbacks';
|
||||||
export * from './scrollable';
|
export * from './scrollable';
|
||||||
export * from './simulator-renderer';
|
export * from './simulator-renderer';
|
||||||
|
export * from './config-transducer';
|
||||||
Loading…
x
Reference in New Issue
Block a user