Skip to content

Commit

Permalink
feat(skeleton): add registerConfigTransducer API
Browse files Browse the repository at this point in the history
  • Loading branch information
liujuping committed Nov 23, 2023
1 parent 394b56d commit 4874b2e
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 4 deletions.
62 changes: 62 additions & 0 deletions docs/docs/api/skeleton.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
31 changes: 29 additions & 2 deletions packages/editor-skeleton/src/skeleton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
IPublicTypeWidgetConfigArea,
IPublicTypeSkeletonConfig,
IPublicApiSkeleton,
IPublicTypeConfigTransducer,
} from '@alilc/lowcode-types';

const logger = new Logger({ level: 'warn', bizName: 'skeleton' });
Expand Down Expand Up @@ -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>;
Expand Down Expand Up @@ -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') {
Expand Down
6 changes: 5 additions & 1 deletion packages/shell/src/api/skeleton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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' {
Expand Down
19 changes: 18 additions & 1 deletion packages/types/src/shell/api/skeleton.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IPublicModelSkeletonItem } from '../model';
import { IPublicTypeDisposable, IPublicTypeSkeletonConfig } from '../type';
import { IPublicTypeConfigTransducer, IPublicTypeDisposable, IPublicTypeSkeletonConfig } from '../type';

export interface IPublicApiSkeleton {

Expand Down Expand Up @@ -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;
}
9 changes: 9 additions & 0 deletions packages/types/src/shell/type/config-transducer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { IPublicTypeSkeletonConfig } from '.';

export interface IPublicTypeConfigTransducer {
(prev: IPublicTypeSkeletonConfig): IPublicTypeSkeletonConfig;

level?: number;

id?: string;
}
1 change: 1 addition & 0 deletions packages/types/src/shell/type/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,4 @@ export * from './hotkey-callback-config';
export * from './hotkey-callbacks';
export * from './scrollable';
export * from './simulator-renderer';
export * from './config-transducer';

0 comments on commit 4874b2e

Please sign in to comment.