Skip to content

Commit a3fef9c

Browse files
liujupingJackLian
authored andcommitted
feat(skeleton): add registerConfigTransducer API
1 parent 029fd1c commit a3fef9c

File tree

6 files changed

+124
-4
lines changed

6 files changed

+124
-4
lines changed

docs/docs/api/skeleton.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,68 @@ showArea(areaName: string): void;
295295
*/
296296
hideArea(areaName: string): void;
297297
```
298+
299+
### registerConfigTransducer
300+
注册一个面板的配置转换器(transducer)。
301+
302+
```typescript
303+
/**
304+
* 注册一个面板的配置转换器(transducer)。
305+
* Registers a configuration transducer for a panel.
306+
* @param {IPublicTypeConfigTransducer} transducer
307+
* - 要注册的转换器函数。该函数接受一个配置对象(类型为 IPublicTypeSkeletonConfig)作为输入,并返回修改后的配置对象。
308+
* - The transducer function to be registered. This function takes a configuration object
309+
*
310+
* @param {number} level
311+
* - 转换器的优先级。优先级较高的转换器会先执行。
312+
* - The priority level of the transducer. Transducers with higher priority levels are executed first.
313+
*
314+
* @param {string} [id]
315+
* - (可选)转换器的唯一标识符。用于在需要时引用或操作特定的转换器。
316+
* - (Optional) A unique identifier for the transducer. Used for referencing or manipulating a specific transducer when needed.
317+
*/
318+
registerConfigTransducer(transducer: IPublicTypeConfigTransducer, level: number, id?: string): void;
319+
```
320+
321+
使用示例
322+
323+
```typescript
324+
import { IPublicModelPluginContext, IPublicTypeSkeletonConfig } from '@alilc/lowcode-types';
325+
326+
function updatePanelWidth(config: IPublicTypeSkeletonConfig) {
327+
if (config.type === 'PanelDock') {
328+
return {
329+
...config,
330+
panelProps: {
331+
...(config.panelProps || {}),
332+
width: 240,
333+
},
334+
}
335+
}
336+
337+
return config;
338+
}
339+
340+
const controlPanelWidthPlugin = (ctx: IPublicModelPluginContext) => {
341+
const { skeleton } = ctx;
342+
(skeleton as any).registerConfigTransducer?.(updatePanelWidth, 1, 'update-panel-width');
343+
344+
return {
345+
init() {},
346+
};
347+
};
348+
349+
controlPanelWidthPlugin.pluginName = 'controlPanelWidthPlugin';
350+
controlPanelWidthPlugin.meta = {
351+
dependencies: [],
352+
engines: {
353+
lowcodeEngine: '^1.2.3', // 插件需要配合 ^1.0.0 的引擎才可运行
354+
},
355+
};
356+
357+
export default controlPanelWidthPlugin;
358+
```
359+
298360
## 事件
299361
### onShowPanel
300362

packages/editor-skeleton/src/skeleton.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
IPublicTypeWidgetConfigArea,
2929
IPublicTypeSkeletonConfig,
3030
IPublicApiSkeleton,
31+
IPublicTypeConfigTransducer,
3132
} from '@alilc/lowcode-types';
3233

3334
const logger = new Logger({ level: 'warn', bizName: 'skeleton' });
@@ -111,6 +112,8 @@ export interface ISkeleton extends Omit<IPublicApiSkeleton,
111112
export class Skeleton implements ISkeleton {
112113
private panels = new Map<string, Panel>();
113114

115+
private configTransducers: IPublicTypeConfigTransducer[] = [];
116+
114117
private containers = new Map<string, WidgetContainer<any>>();
115118

116119
readonly leftArea: Area<DockConfig | PanelDockConfig | DialogDockConfig>;
@@ -448,11 +451,35 @@ export class Skeleton implements ISkeleton {
448451
return restConfig;
449452
}
450453

454+
registerConfigTransducer(
455+
transducer: IPublicTypeConfigTransducer,
456+
level = 100,
457+
id?: string,
458+
) {
459+
transducer.level = level;
460+
transducer.id = id;
461+
const i = this.configTransducers.findIndex((item) => item.level != null && item.level > level);
462+
if (i < 0) {
463+
this.configTransducers.push(transducer);
464+
} else {
465+
this.configTransducers.splice(i, 0, transducer);
466+
}
467+
}
468+
469+
getRegisteredConfigTransducers(): IPublicTypeConfigTransducer[] {
470+
return this.configTransducers;
471+
}
472+
451473
add(config: IPublicTypeSkeletonConfig, extraConfig?: Record<string, any>): IWidget | Widget | Panel | Stage | Dock | PanelDock | undefined {
452-
const parsedConfig = {
474+
const registeredTransducers = this.getRegisteredConfigTransducers();
475+
476+
const parsedConfig = registeredTransducers.reduce((prevConfig, current) => {
477+
return current(prevConfig);
478+
}, {
453479
...this.parseConfig(config),
454480
...extraConfig,
455-
};
481+
});
482+
456483
let { area } = parsedConfig;
457484
if (!area) {
458485
if (parsedConfig.type === 'Panel') {

packages/shell/src/api/skeleton.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
SkeletonEvents,
55
} from '@alilc/lowcode-editor-skeleton';
66
import { skeletonSymbol } from '../symbols';
7-
import { IPublicApiSkeleton, IPublicModelSkeletonItem, IPublicTypeDisposable, IPublicTypeSkeletonConfig, IPublicTypeWidgetConfigArea } from '@alilc/lowcode-types';
7+
import { IPublicApiSkeleton, IPublicModelSkeletonItem, IPublicTypeConfigTransducer, IPublicTypeDisposable, IPublicTypeSkeletonConfig, IPublicTypeWidgetConfigArea } from '@alilc/lowcode-types';
88
import { getLogger } from '@alilc/lowcode-utils';
99
import { SkeletonItem } from '../model/skeleton-item';
1010

@@ -208,6 +208,10 @@ export class Skeleton implements IPublicApiSkeleton {
208208
});
209209
return () => editor.eventBus.off(SkeletonEvents.WIDGET_HIDE, listener);
210210
}
211+
212+
registerConfigTransducer(fn: IPublicTypeConfigTransducer, level: number, id?: string) {
213+
this[skeletonSymbol].registerConfigTransducer(fn, level, id);
214+
}
211215
}
212216

213217
function normalizeArea(area: IPublicTypeWidgetConfigArea | undefined): 'leftArea' | 'rightArea' | 'topArea' | 'toolbar' | 'mainArea' | 'bottomArea' | 'leftFixedArea' | 'leftFloatArea' | 'stages' | 'subTopArea' {

packages/types/src/shell/api/skeleton.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { IPublicModelSkeletonItem } from '../model';
2-
import { IPublicTypeDisposable, IPublicTypeSkeletonConfig } from '../type';
2+
import { IPublicTypeConfigTransducer, IPublicTypeDisposable, IPublicTypeSkeletonConfig } from '../type';
33

44
export interface IPublicApiSkeleton {
55

@@ -114,4 +114,21 @@ export interface IPublicApiSkeleton {
114114
* @returns
115115
*/
116116
onHideWidget(listener: (...args: any[]) => void): IPublicTypeDisposable;
117+
118+
/**
119+
* 注册一个面板的配置转换器(transducer)。
120+
* Registers a configuration transducer for a panel.
121+
* @param {IPublicTypeConfigTransducer} transducer
122+
* - 要注册的转换器函数。该函数接受一个配置对象(类型为 IPublicTypeSkeletonConfig)作为输入,并返回修改后的配置对象。
123+
* - The transducer function to be registered. This function takes a configuration object (of type IPublicTypeSkeletonConfig) as input and returns a modified configuration object.
124+
*
125+
* @param {number} level
126+
* - 转换器的优先级。优先级较高的转换器会先执行。
127+
* - The priority level of the transducer. Transducers with higher priority levels are executed first.
128+
*
129+
* @param {string} [id]
130+
* - (可选)转换器的唯一标识符。用于在需要时引用或操作特定的转换器。
131+
* - (Optional) A unique identifier for the transducer. Used for referencing or manipulating a specific transducer when needed.
132+
*/
133+
registerConfigTransducer(transducer: IPublicTypeConfigTransducer, level: number, id?: string): void;
117134
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { IPublicTypeSkeletonConfig } from '.';
2+
3+
export interface IPublicTypeConfigTransducer {
4+
(prev: IPublicTypeSkeletonConfig): IPublicTypeSkeletonConfig;
5+
6+
level?: number;
7+
8+
id?: string;
9+
}

packages/types/src/shell/type/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,4 @@ export * from './hotkey-callback-config';
9191
export * from './hotkey-callbacks';
9292
export * from './scrollable';
9393
export * from './simulator-renderer';
94+
export * from './config-transducer';

0 commit comments

Comments
 (0)