Skip to content

Commit 0184dcd

Browse files
liujupingJackLian
authored andcommitted
feat: added onChangeViewType event to window model
1 parent 3dc402b commit 0184dcd

File tree

4 files changed

+38
-5
lines changed

4 files changed

+38
-5
lines changed

docs/docs/api/model/window.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,15 @@ function changeViewType(viewName: string): void
6262
```typescript
6363
function save(): Promise(void)
6464
```
65+
66+
## 事件
67+
68+
### onChangeViewType
69+
70+
窗口视图变更事件
71+
72+
```
73+
onChangeViewType(fn: (viewName: string) => void): IPublicTypeDisposable;
74+
```
75+
76+
相关类型:[IPublicTypeDisposable](https://github.com/alibaba/lowcode-engine/blob/main/packages/types/src/shell/type/disposable.ts)

packages/shell/src/model/window.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { windowSymbol } from '../symbols';
2-
import { IPublicModelResource, IPublicModelWindow } from '@alilc/lowcode-types';
2+
import { IPublicModelResource, IPublicModelWindow, IPublicTypeDisposable } from '@alilc/lowcode-types';
33
import { EditorWindow } from '@alilc/lowcode-workspace';
44
import { Resource as ShellResource } from './resource';
55

@@ -31,7 +31,11 @@ export class Window implements IPublicModelWindow {
3131
}
3232

3333
changeViewType(viewName: string) {
34-
this[windowSymbol].changeViewType(viewName);
34+
this[windowSymbol].changeViewType(viewName, false);
35+
}
36+
37+
onChangeViewType(fun: (viewName: string) => void): IPublicTypeDisposable {
38+
return this[windowSymbol].onChangeViewType(fun);
3539
}
3640

3741
async save() {

packages/types/src/shell/model/window.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ReactElement } from 'react';
2-
import { IPublicTypeNodeSchema } from '../type';
2+
import { IPublicTypeDisposable, IPublicTypeNodeSchema } from '../type';
33
import { IPublicModelResource } from './resource';
44

55
export interface IPublicModelWindow {
@@ -24,4 +24,7 @@ export interface IPublicModelWindow {
2424

2525
/** 调用当前窗口视图保存钩子 */
2626
save(): Promise<any>;
27+
28+
/** 窗口视图变更事件 */
29+
onChangeViewType(fn: (viewName: string) => void): IPublicTypeDisposable;
2730
}

packages/workspace/src/window.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import { uniqueId } from '@alilc/lowcode-utils';
2-
import { makeObservable, obx } from '@alilc/lowcode-editor-core';
2+
import { createModuleEventBus, IEventBus, makeObservable, obx } from '@alilc/lowcode-editor-core';
33
import { Context } from './context/view-context';
44
import { Workspace } from './workspace';
55
import { Resource } from './resource';
6+
import { IPublicTypeDisposable } from '../../types/es/shell/type/disposable';
67

78
export class EditorWindow {
89
id: string = uniqueId('window');
910
icon: React.ReactElement | undefined;
1011

12+
private emitter: IEventBus = createModuleEventBus('Project');
13+
1114
@obx.ref editorView: Context;
1215

1316
@obx editorViews: Map<string, Context> = new Map<string, Context>();
@@ -59,6 +62,14 @@ export class EditorWindow {
5962
}
6063
};
6164

65+
onChangeViewType(fn: (viewName: string) => void): IPublicTypeDisposable {
66+
this.emitter.on('window.change.view.type', fn);
67+
68+
return () => {
69+
this.emitter.off('window.change.view.type', fn);
70+
};
71+
}
72+
6273
execViewTypesInit = async () => {
6374
const editorViews = this.resource.editorViews;
6475
for (let i = 0; i < editorViews.length; i++) {
@@ -81,10 +92,13 @@ export class EditorWindow {
8192
this.editorViews.set(name, editorView);
8293
};
8394

84-
changeViewType = (name: string) => {
95+
changeViewType = (name: string, ignoreEmit: boolean = true) => {
8596
this.editorView?.setActivate(false);
8697
this.editorView = this.editorViews.get(name)!;
8798

99+
if (!ignoreEmit) {
100+
this.emitter.emit('window.change.view.type', name);
101+
}
88102
this.editorView.setActivate(true);
89103
};
90104

0 commit comments

Comments
 (0)