Skip to content

Commit 899ffa1

Browse files
liujupingJackLian
authored andcommitted
feat(workspace): update openEditorWindow api
1 parent 9ddda1d commit 899ffa1

File tree

9 files changed

+92
-22
lines changed

9 files changed

+92
-22
lines changed

docs/docs/api/workspace.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,14 @@ setResourceList(resourceList: IPublicResourceList) {}
8484
打开视图窗口
8585

8686
```typescript
87-
openEditorWindow(resourceName: string, title: string, options: Object, viewName?: string): void;
87+
/**
88+
* 打开视图窗口
89+
* @deprecated
90+
*/
91+
openEditorWindow(resourceName: string, id: string, extra: Object, viewName?: string, sleep?: boolean): Promise<void>;
92+
93+
/** 打开视图窗口 */
94+
openEditorWindow(resource: Resource, sleep?: boolean): Promise<void>;
8895
```
8996

9097
### openEditorWindowById
@@ -100,7 +107,7 @@ openEditorWindowById(id: string): void;
100107
移除视图窗口
101108

102109
```typescript
103-
removeEditorWindow(resourceName: string, title: string): void;
110+
removeEditorWindow(resourceName: string, id: string): void;
104111
```
105112

106113
### removeEditorWindowById

packages/designer/src/plugin/plugin-manager.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ export class LowCodePluginManager implements ILowCodePluginManager {
8484
const ctx = this._getLowCodePluginContext({ pluginName, meta });
8585
const customFilterValidOptions = engineConfig.get('customPluginFilterOptions', filterValidOptions);
8686
const pluginTransducer = engineConfig.get('customPluginTransducer', null);
87-
const newOptions = customFilterValidOptions(options, preferenceDeclaration!);
88-
const newPluginModel = pluginTransducer ? await pluginTransducer(pluginModel, ctx, newOptions) : pluginModel;
87+
const newPluginModel = pluginTransducer ? await pluginTransducer(pluginModel, ctx, options) : pluginModel;
88+
const newOptions = customFilterValidOptions(options, newPluginModel.meta?.preferenceDeclaration);
8989
const config = newPluginModel(ctx, newOptions);
9090
// compat the legacy way to declare pluginName
9191
// @ts-ignore

packages/shell/src/api/workspace.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { IPublicApiWorkspace, IPublicResourceList, IPublicTypeDisposable, IPublicTypeResourceType } from '@alilc/lowcode-types';
22
import { IWorkspace } from '@alilc/lowcode-workspace';
3-
import { workspaceSymbol } from '../symbols';
3+
import { resourceSymbol, workspaceSymbol } from '../symbols';
44
import { Resource as ShellResource, Window as ShellWindow } from '../model';
55
import { Plugins } from './plugins';
66

@@ -64,16 +64,20 @@ export class Workspace implements IPublicApiWorkspace {
6464
this[workspaceSymbol].registerResourceType(resourceTypeModel);
6565
}
6666

67-
async openEditorWindow(resourceName: string, title: string, extra: object, viewName?: string, sleep?: boolean): Promise<void> {
68-
await this[workspaceSymbol].openEditorWindow(resourceName, title, extra, viewName, sleep);
67+
async openEditorWindow(): Promise<void> {
68+
if (typeof arguments[0] === 'string') {
69+
await this[workspaceSymbol].openEditorWindow(arguments[0], arguments[1], arguments[2], arguments[3], arguments[4]);
70+
} else {
71+
await this[workspaceSymbol].openEditorWindowByResource(arguments[0]?.[resourceSymbol], arguments[1]);
72+
}
6973
}
7074

7175
openEditorWindowById(id: string) {
7276
this[workspaceSymbol].openEditorWindowById(id);
7377
}
7478

75-
removeEditorWindow(resourceName: string, title: string) {
76-
this[workspaceSymbol].removeEditorWindow(resourceName, title);
79+
removeEditorWindow(resourceName: string, id: string) {
80+
this[workspaceSymbol].removeEditorWindow(resourceName, id);
7781
}
7882

7983
removeEditorWindowById(id: string) {

packages/shell/src/model/resource.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ export class Resource implements IPublicModelResource {
1313
return this[resourceSymbol].title;
1414
}
1515

16+
get id() {
17+
return this[resourceSymbol].id;
18+
}
19+
1620
get icon() {
1721
return this[resourceSymbol].icon;
1822
}

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import { IPublicApiPlugins, IPublicModelResource, IPublicResourceList, IPublicTy
33

44
export interface IPublicApiWorkspace<
55
Plugins = IPublicApiPlugins,
6-
ModelWindow = IPublicModelWindow
6+
ModelWindow = IPublicModelWindow,
7+
Resource = IPublicModelResource,
78
> {
89

910
/** 是否启用 workspace 模式 */
@@ -29,14 +30,20 @@ export interface IPublicApiWorkspace<
2930
/** 注册资源 */
3031
registerResourceType(resourceTypeModel: IPublicTypeResourceType): void;
3132

33+
/**
34+
* 打开视图窗口
35+
* @deprecated
36+
*/
37+
openEditorWindow(resourceName: string, id: string, extra: Object, viewName?: string, sleep?: boolean): Promise<void>;
38+
3239
/** 打开视图窗口 */
33-
openEditorWindow(resourceName: string, title: string, extra: Object, viewName?: string, sleep?: boolean): Promise<void>;
40+
openEditorWindow(resource: Resource, sleep?: boolean): Promise<void>;
3441

3542
/** 通过视图 id 打开窗口 */
3643
openEditorWindowById(id: string): void;
3744

3845
/** 移除视图窗口 */
39-
removeEditorWindow(resourceName: string, title: string): void;
46+
removeEditorWindow(resourceName: string, id: string): void;
4047

4148
/** 通过视图 id 移除窗口 */
4249
removeEditorWindowById(id: string): void;

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ export interface IBaseModelResource<
55
> {
66
get title(): string | undefined;
77

8+
get id(): string | undefined;
9+
810
get icon(): ReactElement | undefined;
911

1012
get options(): Record<string, any>;

packages/types/src/shell/type/resource-list.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ export interface IPublicResourceData {
88
/** 资源标题 */
99
title?: string;
1010

11+
/** 资源 Id */
12+
id?: string;
13+
1114
/** 分类 */
1215
category?: string;
1316

@@ -24,10 +27,6 @@ export interface IPublicResourceData {
2427

2528
/** 资源子元素 */
2629
children?: IPublicResourceData[];
27-
28-
config?: {
29-
disableBehaviors?: ('copy' | 'remove')[];
30-
};
3130
}
3231

3332
export type IPublicResourceList = IPublicResourceData[];

packages/workspace/src/resource.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ export class Resource implements IResource {
6060
return this.resourceData.title || this.resourceTypeInstance.defaultTitle;
6161
}
6262

63+
get id(): string | undefined {
64+
return this.resourceData.id;
65+
}
66+
6367
get options() {
6468
return this.resourceData.options;
6569
}

packages/workspace/src/workspace.ts

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ export interface IWorkspace extends Omit<IPublicApiWorkspace<
5050
onChangeActiveEditorView(fn: () => void): IPublicTypeDisposable;
5151

5252
emitChangeActiveEditorView(): void;
53+
54+
openEditorWindowByResource(resource: IResource, sleep: boolean): Promise<void>;
5355
}
5456

5557
export class Workspace implements IWorkspace {
@@ -91,12 +93,12 @@ export class Workspace implements IWorkspace {
9193

9294
@obx.ref window: IEditorWindow;
9395

94-
windowQueue: {
96+
windowQueue: ({
9597
name: string;
9698
title: string;
9799
options: Object;
98100
viewName?: string;
99-
}[] = [];
101+
} | IResource)[] = [];
100102

101103
constructor(
102104
readonly registryInnerPlugin: (designer: IDesigner, editor: IEditor, plugins: IPublicApiPlugins) => Promise<IPublicTypeDisposable>,
@@ -192,7 +194,7 @@ export class Workspace implements IWorkspace {
192194
this.remove(index);
193195
}
194196

195-
private remove(index: number) {
197+
private async remove(index: number) {
196198
if (index < 0) {
197199
return;
198200
}
@@ -202,16 +204,16 @@ export class Workspace implements IWorkspace {
202204
if (this.window === window) {
203205
this.window = this.windows[index] || this.windows[index + 1] || this.windows[index - 1];
204206
if (this.window?.sleep) {
205-
this.window.init();
207+
await this.window.init();
206208
}
207209
this.emitChangeActiveWindow();
208210
}
209211
this.emitChangeWindow();
210212
this.window?.updateState(WINDOW_STATE.active);
211213
}
212214

213-
removeEditorWindow(resourceName: string, title: string) {
214-
const index = this.windows.findIndex(d => (d.resource?.name === resourceName && d.title === title));
215+
removeEditorWindow(resourceName: string, id: string) {
216+
const index = this.windows.findIndex(d => (d.resource?.name === resourceName && d.title === id));
215217
this.remove(index);
216218
}
217219

@@ -228,6 +230,47 @@ export class Workspace implements IWorkspace {
228230
this.window?.updateState(WINDOW_STATE.active);
229231
}
230232

233+
async openEditorWindowByResource(resource: IResource, sleep: boolean = false): Promise<void> {
234+
if (this.window && !this.window?.initReady && !sleep) {
235+
this.windowQueue.push(resource);
236+
return;
237+
}
238+
239+
this.window?.updateState(WINDOW_STATE.inactive);
240+
241+
const filterWindows = this.windows.filter(d => (d.resource?.id === resource.id));
242+
if (filterWindows && filterWindows.length) {
243+
this.window = filterWindows[0];
244+
if (!sleep && this.window.sleep) {
245+
await this.window.init();
246+
} else {
247+
this.checkWindowQueue();
248+
}
249+
this.emitChangeActiveWindow();
250+
this.window?.updateState(WINDOW_STATE.active);
251+
return;
252+
}
253+
254+
const window = new EditorWindow(resource, this, {
255+
title: resource.title,
256+
options: resource.options,
257+
viewName: resource.viewName,
258+
sleep,
259+
});
260+
261+
this.windows = [...this.windows, window];
262+
this.editorWindowMap.set(window.id, window);
263+
if (sleep) {
264+
this.emitChangeWindow();
265+
return;
266+
}
267+
this.window = window;
268+
await this.window.init();
269+
this.emitChangeWindow();
270+
this.emitChangeActiveWindow();
271+
this.window?.updateState(WINDOW_STATE.active);
272+
}
273+
231274
async openEditorWindow(name: string, title: string, options: Object, viewName?: string, sleep?: boolean) {
232275
if (this.window && !this.window?.initReady && !sleep) {
233276
this.windowQueue.push({

0 commit comments

Comments
 (0)