GeezerVison is showing github.com — feat(skeleton): add registerConfigTransducer API · czfadmin/lowcode-engine@a3fef9c
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: docs/docs/api/skeleton.md +62Lines changed: 62 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -295,6 +295,68 @@ showArea(areaName: string): void; | 295 | 295 | */ | 296 | 296 | hideArea(areaName: string): void; | 297 | 297 | ``` | | 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 | + | 298 | 360 | ## 事件 | 299 | 361 | ### onShowPanel | 300 | 362 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/editor-skeleton/src/skeleton.ts +29-2Lines changed: 29 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -28,6 +28,7 @@ import { | 28 | 28 | IPublicTypeWidgetConfigArea, | 29 | 29 | IPublicTypeSkeletonConfig, | 30 | 30 | IPublicApiSkeleton, | | 31 | + IPublicTypeConfigTransducer, | 31 | 32 | } from '@alilc/lowcode-types'; | 32 | 33 |
| 33 | 34 | const logger = new Logger({ level: 'warn', bizName: 'skeleton' }); | @@ -111,6 +112,8 @@ export interface ISkeleton extends Omit<IPublicApiSkeleton, | 111 | 112 | export class Skeleton implements ISkeleton { | 112 | 113 | private panels = new Map<string, Panel>(); | 113 | 114 |
| | 115 | + private configTransducers: IPublicTypeConfigTransducer[] = []; | | 116 | + | 114 | 117 | private containers = new Map<string, WidgetContainer<any>>(); | 115 | 118 |
| 116 | 119 | readonly leftArea: Area<DockConfig | PanelDockConfig | DialogDockConfig>; | @@ -448,11 +451,35 @@ export class Skeleton implements ISkeleton { | 448 | 451 | return restConfig; | 449 | 452 | } | 450 | 453 |
| | 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 | + | 451 | 473 | 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 | + }, { | 453 | 479 | ...this.parseConfig(config), | 454 | 480 | ...extraConfig, | 455 | | - }; | | 481 | + }); | | 482 | + | 456 | 483 | let { area } = parsedConfig; | 457 | 484 | if (!area) { | 458 | 485 | if (parsedConfig.type === 'Panel') { | |
Collapse file Copy file name to clipboardExpand all lines: packages/shell/src/api/skeleton.ts +5-1Lines changed: 5 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 4 | 4 | SkeletonEvents, | 5 | 5 | } from '@alilc/lowcode-editor-skeleton'; | 6 | 6 | 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'; | 8 | 8 | import { getLogger } from '@alilc/lowcode-utils'; | 9 | 9 | import { SkeletonItem } from '../model/skeleton-item'; | 10 | 10 |
| @@ -208,6 +208,10 @@ export class Skeleton implements IPublicApiSkeleton { | 208 | 208 | }); | 209 | 209 | return () => editor.eventBus.off(SkeletonEvents.WIDGET_HIDE, listener); | 210 | 210 | } | | 211 | + | | 212 | + registerConfigTransducer(fn: IPublicTypeConfigTransducer, level: number, id?: string) { | | 213 | + this[skeletonSymbol].registerConfigTransducer(fn, level, id); | | 214 | + } | 211 | 215 | } | 212 | 216 |
| 213 | 217 | function normalizeArea(area: IPublicTypeWidgetConfigArea | undefined): 'leftArea' | 'rightArea' | 'topArea' | 'toolbar' | 'mainArea' | 'bottomArea' | 'leftFixedArea' | 'leftFloatArea' | 'stages' | 'subTopArea' { | |
Collapse file Copy file name to clipboardExpand all lines: packages/types/src/shell/api/skeleton.ts +18-1Lines changed: 18 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import { IPublicModelSkeletonItem } from '../model'; | 2 | | -import { IPublicTypeDisposable, IPublicTypeSkeletonConfig } from '../type'; | | 2 | +import { IPublicTypeConfigTransducer, IPublicTypeDisposable, IPublicTypeSkeletonConfig } from '../type'; | 3 | 3 |
| 4 | 4 | export interface IPublicApiSkeleton { | 5 | 5 |
| @@ -114,4 +114,21 @@ export interface IPublicApiSkeleton { | 114 | 114 | * @returns | 115 | 115 | */ | 116 | 116 | 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; | 117 | 134 | } |
Collapse file Copy file name to clipboard +9Lines changed: 9 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import { IPublicTypeSkeletonConfig } from '.'; | | 2 | + | | 3 | +export interface IPublicTypeConfigTransducer { | | 4 | + (prev: IPublicTypeSkeletonConfig): IPublicTypeSkeletonConfig; | | 5 | + | | 6 | + level?: number; | | 7 | + | | 8 | + id?: string; | | 9 | +} |
Collapse file Copy file name to clipboardExpand all lines: packages/types/src/shell/type/index.ts +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -91,3 +91,4 @@ export * from './hotkey-callback-config'; | 91 | 91 | export * from './hotkey-callbacks'; | 92 | 92 | export * from './scrollable'; | 93 | 93 | export * from './simulator-renderer'; | | 94 | +export * from './config-transducer'; |
|