Skip to content

Commit 69ab81a

Browse files
liujupingJackLian
authored andcommitted
feat: the event supplements the dispose function as the return value
1 parent 8356298 commit 69ab81a

13 files changed

Lines changed: 117 additions & 87 deletions

File tree

docs/docs/api/material.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,8 +358,11 @@ material.getRegisteredMetadataTransducers();
358358
* add callback for assets changed event
359359
* @param fn
360360
*/
361-
onChangeAssets(fn: () => void): void;
361+
onChangeAssets(fn: () => void): IPublicTypeDisposable;
362362
```
363+
364+
相关类型:[IPublicTypeDisposable](https://github.com/alibaba/lowcode-engine/blob/main/packages/types/src/shell/type/disposable.ts)
365+
363366
##### 示例
364367
```typescript
365368
import { material } from '@alilc/lowcode-engine';

docs/docs/api/model/document-model.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -327,27 +327,31 @@ onChangeSelection(fn: (ids: string[]) => void): IPublicTypeDisposable;
327327
* set callback for event on visibility changed for certain node
328328
* @param fn
329329
*/
330-
onChangeNodeVisible(fn: (node: IPublicModelNode, visible: boolean) => void): void;
330+
onChangeNodeVisible(fn: (node: IPublicModelNode, visible: boolean) => void): IPublicTypeDisposable;
331331
```
332332

333-
相关类型:[IPublicModelNode](https://github.com/alibaba/lowcode-engine/blob/main/packages/types/src/shell/model/node.ts)
333+
- 相关类型:[IPublicModelNode](https://github.com/alibaba/lowcode-engine/blob/main/packages/types/src/shell/model/node.ts)
334+
- 相关类型:[IPublicTypeDisposable](https://github.com/alibaba/lowcode-engine/blob/main/packages/types/src/shell/type/disposable.ts)
334335

335336
### onChangeNodeChildren
336337

337-
onChangeNodeChildren(fn: (info?: IPublicTypeOnChangeOptions) => void)
338-
339338
当前 document 的节点 children 变更事件
340339

341340
```typescript
341+
onChangeNodeChildren(fn: (info?: IPublicTypeOnChangeOptions) => void): IPublicTypeDisposable;
342342
```
343343

344+
相关类型:[IPublicTypeDisposable](https://github.com/alibaba/lowcode-engine/blob/main/packages/types/src/shell/type/disposable.ts)
345+
344346
### onChangeNodeProp
345347
当前 document 节点属性修改事件
346348

347349
```typescript
348-
onChangeNodeProp(fn: (info: IPublicTypePropChangeOptions) => void)
350+
onChangeNodeProp(fn: (info: IPublicTypePropChangeOptions) => void): IPublicTypeDisposable;
349351
```
350352

353+
相关类型:[IPublicTypeDisposable](https://github.com/alibaba/lowcode-engine/blob/main/packages/types/src/shell/type/disposable.ts)
354+
351355
### onImportSchema
352356
当前 document 导入新的 schema 事件
353357
```typescript

docs/docs/api/skeleton.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -295,9 +295,11 @@ hideArea(areaName: string): void;
295295
* @param listener
296296
* @returns
297297
*/
298-
onShowPanel(listener: (...args: any[]) => void): () => void;
298+
onShowPanel(listener: (...args: any[]) => void): IPublicTypeDisposable;
299299
```
300300

301+
相关类型:[IPublicTypeDisposable](https://github.com/alibaba/lowcode-engine/blob/main/packages/types/src/shell/type/disposable.ts)
302+
301303
### onHidePanel
302304

303305
监听 Panel 实例隐藏事件
@@ -309,9 +311,11 @@ onShowPanel(listener: (...args: any[]) => void): () => void;
309311
* @param listener
310312
* @returns
311313
*/
312-
onHidePanel(listener: (...args: any[]) => void): () => void;
314+
onHidePanel(listener: (...args: any[]) => void): IPublicTypeDisposable;
313315
```
314316

317+
相关类型:[IPublicTypeDisposable](https://github.com/alibaba/lowcode-engine/blob/main/packages/types/src/shell/type/disposable.ts)
318+
315319

316320
### onShowWidget
317321

@@ -324,9 +328,10 @@ onHidePanel(listener: (...args: any[]) => void): () => void;
324328
* @param listener
325329
* @returns
326330
*/
327-
onShowWidget(listener: (...args: any[]) => void): () => void;
331+
onShowWidget(listener: (...args: any[]) => void): IPublicTypeDisposable;
328332
```
329333

334+
相关类型:[IPublicTypeDisposable](https://github.com/alibaba/lowcode-engine/blob/main/packages/types/src/shell/type/disposable.ts)
330335

331336
### onHideWidget
332337

@@ -339,9 +344,11 @@ onShowWidget(listener: (...args: any[]) => void): () => void;
339344
* @param listener
340345
* @returns
341346
*/
342-
onHideWidget(listener: (...args: any[]) => void): () => void;
347+
onHideWidget(listener: (...args: any[]) => void): IPublicTypeDisposable;
343348
```
344349

350+
相关类型:[IPublicTypeDisposable](https://github.com/alibaba/lowcode-engine/blob/main/packages/types/src/shell/type/disposable.ts)
351+
345352
## 使用示例
346353

347354
```typescript

docs/docs/api/workspace.md

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -69,22 +69,6 @@ registerResourceType(resourceTypeModel: IPublicTypeResourceType): void;
6969

7070
相关类型:[IPublicTypeResourceType](https://github.com/alibaba/lowcode-engine/blob/main/packages/types/src/shell/type/resource-type.ts)
7171

72-
### onChangeWindows
73-
74-
窗口新增/删除的事件
75-
76-
```typescript
77-
function onChangeWindows(fn: () => void): void;
78-
```
79-
80-
### onChangeActiveWindow
81-
82-
active 窗口变更事件
83-
84-
```typescript
85-
function onChangeActiveWindow(fn: () => void): void;
86-
```
87-
8872
### setResourceList
8973

9074
设置设计器资源列表数据
@@ -95,16 +79,6 @@ setResourceList(resourceList: IPublicResourceList) {}
9579

9680
相关类型:[IPublicResourceOptions](https://github.com/alibaba/lowcode-engine/blob/main/packages/types/src/shell/type/resource-options.ts)
9781

98-
### onResourceListChange
99-
100-
设计器资源列表数据变更事件
101-
102-
```typescript
103-
onResourceListChange(fn: (resourceList: IPublicResourceList): void): (): void;
104-
```
105-
106-
相关类型:[IPublicResourceOptions](https://github.com/alibaba/lowcode-engine/blob/main/packages/types/src/shell/type/resource-options.ts)
107-
10882
### openEditorWindow
10983

11084
打开视图窗口
@@ -135,4 +109,38 @@ removeEditorWindow(resourceName: string, title: string): void;
135109

136110
```typescript
137111
removeEditorWindowById(id: string): void;
138-
```
112+
```
113+
114+
## 事件
115+
116+
### onChangeWindows
117+
118+
窗口新增/删除的事件
119+
120+
```typescript
121+
function onChangeWindows(fn: () => void): IPublicTypeDisposable;
122+
```
123+
124+
相关类型:[IPublicTypeDisposable](https://github.com/alibaba/lowcode-engine/blob/main/packages/types/src/shell/type/disposable.ts)
125+
126+
### onChangeActiveWindow
127+
128+
active 窗口变更事件
129+
130+
```typescript
131+
function onChangeActiveWindow(fn: () => void): IPublicTypeDisposable;
132+
```
133+
134+
相关类型:[IPublicTypeDisposable](https://github.com/alibaba/lowcode-engine/blob/main/packages/types/src/shell/type/disposable.ts)
135+
136+
137+
### onResourceListChange
138+
139+
设计器资源列表数据变更事件
140+
141+
```typescript
142+
onResourceListChange(fn: (resourceList: IPublicResourceList): void): (): IPublicTypeDisposable;
143+
```
144+
145+
- 相关类型:[IPublicResourceOptions](https://github.com/alibaba/lowcode-engine/blob/main/packages/types/src/shell/type/resource-options.ts)
146+
- 相关类型:[IPublicTypeDisposable](https://github.com/alibaba/lowcode-engine/blob/main/packages/types/src/shell/type/disposable.ts)

packages/designer/src/project/project.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,6 @@ export class Project implements IProject {
8585
return this._simulator || null;
8686
}
8787

88-
key = Math.random();
89-
9088
@computed get currentDocument(): IDocumentModel | null {
9189
return this.documents.find((doc) => doc.active);
9290
}

packages/shell/src/api/material.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
IPublicModelComponentMeta,
1313
IPublicTypeNpmInfo,
1414
IPublicModelEditor,
15+
IPublicTypeDisposable,
1516
} from '@alilc/lowcode-types';
1617
import { Workspace as InnerWorkspace } from '@alilc/lowcode-workspace';
1718
import { editorSymbol, designerSymbol } from '../symbols';
@@ -170,7 +171,7 @@ export class Material implements IPublicApiMaterial {
170171
* 监听 assets 变化的事件
171172
* @param fn
172173
*/
173-
onChangeAssets(fn: () => void): Function {
174+
onChangeAssets(fn: () => void): IPublicTypeDisposable {
174175
const dispose = [
175176
// 设置 assets,经过 setAssets 赋值
176177
this[editorSymbol].onGot('assets', fn),

packages/shell/src/api/skeleton.ts

Lines changed: 5 additions & 5 deletions
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, IPublicTypeWidgetBaseConfig, IPublicTypeWidgetConfigArea } from '@alilc/lowcode-types';
7+
import { IPublicApiSkeleton, IPublicTypeDisposable, IPublicTypeWidgetBaseConfig, IPublicTypeWidgetConfigArea } from '@alilc/lowcode-types';
88

99
const innerSkeletonSymbol = Symbol('skeleton');
1010
export class Skeleton implements IPublicApiSkeleton {
@@ -129,7 +129,7 @@ export class Skeleton implements IPublicApiSkeleton {
129129
* @param listener
130130
* @returns
131131
*/
132-
onShowPanel(listener: (...args: any[]) => void) {
132+
onShowPanel(listener: (...args: any[]) => void): IPublicTypeDisposable {
133133
const { editor } = this[skeletonSymbol];
134134
editor.eventBus.on(SkeletonEvents.PANEL_SHOW, (name: any, panel: any) => {
135135
// 不泄漏 skeleton
@@ -144,7 +144,7 @@ export class Skeleton implements IPublicApiSkeleton {
144144
* @param listener
145145
* @returns
146146
*/
147-
onHidePanel(listener: (...args: any[]) => void) {
147+
onHidePanel(listener: (...args: any[]) => void): IPublicTypeDisposable {
148148
const { editor } = this[skeletonSymbol];
149149
editor.eventBus.on(SkeletonEvents.PANEL_HIDE, (name: any, panel: any) => {
150150
// 不泄漏 skeleton
@@ -159,7 +159,7 @@ export class Skeleton implements IPublicApiSkeleton {
159159
* @param listener
160160
* @returns
161161
*/
162-
onShowWidget(listener: (...args: any[]) => void) {
162+
onShowWidget(listener: (...args: any[]) => void): IPublicTypeDisposable {
163163
const { editor } = this[skeletonSymbol];
164164
editor.eventBus.on(SkeletonEvents.WIDGET_SHOW, (name: any, panel: any) => {
165165
// 不泄漏 skeleton
@@ -174,7 +174,7 @@ export class Skeleton implements IPublicApiSkeleton {
174174
* @param listener
175175
* @returns
176176
*/
177-
onHideWidget(listener: (...args: any[]) => void) {
177+
onHideWidget(listener: (...args: any[]) => void): IPublicTypeDisposable {
178178
const { editor } = this[skeletonSymbol];
179179
editor.eventBus.on(SkeletonEvents.WIDGET_HIDE, (name: any, panel: any) => {
180180
// 不泄漏 skeleton

packages/shell/src/api/workspace.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { IPublicApiWorkspace, IPublicResourceList, IPublicTypeResourceType } from '@alilc/lowcode-types';
1+
import { IPublicApiWorkspace, IPublicResourceList, IPublicTypeDisposable, IPublicTypeResourceType } from '@alilc/lowcode-types';
22
import { Workspace as InnerWorkSpace } from '@alilc/lowcode-workspace';
33
import { Plugins } from '@alilc/lowcode-shell';
44
import { workspaceSymbol } from '../symbols';
@@ -19,7 +19,7 @@ export class Workspace implements IPublicApiWorkspace {
1919
this[workspaceSymbol].setResourceList(resourceList);
2020
}
2121

22-
onResourceListChange(fn: (resourceList: IPublicResourceList) => void): () => void {
22+
onResourceListChange(fn: (resourceList: IPublicResourceList) => void): IPublicTypeDisposable {
2323
return this[workspaceSymbol].onResourceListChange(fn);
2424
}
2525

@@ -59,11 +59,11 @@ export class Workspace implements IPublicApiWorkspace {
5959
return this[workspaceSymbol].windows.map((d) => new ShellWindow(d));
6060
}
6161

62-
onChangeWindows(fn: () => void) {
62+
onChangeWindows(fn: () => void): IPublicTypeDisposable {
6363
return this[workspaceSymbol].onChangeWindows(fn);
6464
}
6565

66-
onChangeActiveWindow(fn: () => void) {
66+
onChangeActiveWindow(fn: () => void): IPublicTypeDisposable {
6767
return this[workspaceSymbol].onChangeActiveWindow(fn);
6868
}
6969
}

packages/shell/src/model/document-model.ts

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,8 @@ export class DocumentModel implements IPublicModelDocumentModel {
288288
* 当前 document 的节点显隐状态变更事件
289289
* @param fn
290290
*/
291-
onChangeNodeVisible(fn: (node: IPublicModelNode, visible: boolean) => void): void {
292-
this[documentSymbol].onChangeNodeVisible((node: IPublicModelNode, visible: boolean) => {
291+
onChangeNodeVisible(fn: (node: IPublicModelNode, visible: boolean) => void): IPublicTypeDisposable {
292+
return this[documentSymbol].onChangeNodeVisible((node: IPublicModelNode, visible: boolean) => {
293293
fn(ShellNode.create(node)!, visible);
294294
});
295295
}
@@ -298,8 +298,8 @@ export class DocumentModel implements IPublicModelDocumentModel {
298298
* 当前 document 的节点 children 变更事件
299299
* @param fn
300300
*/
301-
onChangeNodeChildren(fn: (info: IPublicTypeOnChangeOptions) => void): void {
302-
this[documentSymbol].onChangeNodeChildren((info?: IPublicTypeOnChangeOptions) => {
301+
onChangeNodeChildren(fn: (info: IPublicTypeOnChangeOptions) => void): IPublicTypeDisposable {
302+
return this[documentSymbol].onChangeNodeChildren((info?: IPublicTypeOnChangeOptions) => {
303303
if (!info) {
304304
return;
305305
}
@@ -314,19 +314,27 @@ export class DocumentModel implements IPublicModelDocumentModel {
314314
* 当前 document 节点属性修改事件
315315
* @param fn
316316
*/
317-
onChangeNodeProp(fn: (info: IPublicTypePropChangeOptions) => void): void {
317+
onChangeNodeProp(fn: (info: IPublicTypePropChangeOptions) => void): IPublicTypeDisposable {
318+
const callback = (info: GlobalEvent.Node.Prop.ChangeOptions) => {
319+
fn({
320+
key: info.key,
321+
oldValue: info.oldValue,
322+
newValue: info.newValue,
323+
prop: ShellProp.create(info.prop)!,
324+
node: ShellNode.create(info.node as any)!,
325+
});
326+
};
318327
this[editorSymbol].on(
319328
GlobalEvent.Node.Prop.InnerChange,
320-
(info: GlobalEvent.Node.Prop.ChangeOptions) => {
321-
fn({
322-
key: info.key,
323-
oldValue: info.oldValue,
324-
newValue: info.newValue,
325-
prop: ShellProp.create(info.prop)!,
326-
node: ShellNode.create(info.node as any)!,
327-
});
328-
},
329+
callback,
329330
);
331+
332+
return () => {
333+
this[editorSymbol].off(
334+
GlobalEvent.Node.Prop.InnerChange,
335+
callback,
336+
);
337+
};
330338
}
331339

332340
/**

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { IPublicTypeAssetsJson, IPublicTypeMetadataTransducer, IPublicTypeComponentAction, IPublicTypeNpmInfo } from '../type';
1+
import { IPublicTypeAssetsJson, IPublicTypeMetadataTransducer, IPublicTypeComponentAction, IPublicTypeNpmInfo, IPublicTypeDisposable } from '../type';
22
import { IPublicModelComponentMeta } from '../model';
33
import { ComponentType } from 'react';
44

@@ -104,5 +104,5 @@ export interface IPublicApiMaterial {
104104
* add callback for assets changed event
105105
* @param fn
106106
*/
107-
onChangeAssets(fn: () => void): Function;
107+
onChangeAssets(fn: () => void): IPublicTypeDisposable;
108108
}

0 commit comments

Comments
 (0)