Skip to content

Commit

Permalink
feat(workspace): update openEditorWindow api
Browse files Browse the repository at this point in the history
  • Loading branch information
liujuping authored and JackLian committed Aug 21, 2023
1 parent 9ddda1d commit 899ffa1
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 22 deletions.
11 changes: 9 additions & 2 deletions docs/docs/api/workspace.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,14 @@ setResourceList(resourceList: IPublicResourceList) {}
打开视图窗口

```typescript
openEditorWindow(resourceName: string, title: string, options: Object, viewName?: string): void;
/**
* 打开视图窗口
* @deprecated
*/
openEditorWindow(resourceName: string, id: string, extra: Object, viewName?: string, sleep?: boolean): Promise<void>;

/** 打开视图窗口 */
openEditorWindow(resource: Resource, sleep?: boolean): Promise<void>;
```

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

```typescript
removeEditorWindow(resourceName: string, title: string): void;
removeEditorWindow(resourceName: string, id: string): void;
```

### removeEditorWindowById
Expand Down
4 changes: 2 additions & 2 deletions packages/designer/src/plugin/plugin-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ export class LowCodePluginManager implements ILowCodePluginManager {
const ctx = this._getLowCodePluginContext({ pluginName, meta });
const customFilterValidOptions = engineConfig.get('customPluginFilterOptions', filterValidOptions);
const pluginTransducer = engineConfig.get('customPluginTransducer', null);
const newOptions = customFilterValidOptions(options, preferenceDeclaration!);
const newPluginModel = pluginTransducer ? await pluginTransducer(pluginModel, ctx, newOptions) : pluginModel;
const newPluginModel = pluginTransducer ? await pluginTransducer(pluginModel, ctx, options) : pluginModel;
const newOptions = customFilterValidOptions(options, newPluginModel.meta?.preferenceDeclaration);
const config = newPluginModel(ctx, newOptions);
// compat the legacy way to declare pluginName
// @ts-ignore
Expand Down
14 changes: 9 additions & 5 deletions packages/shell/src/api/workspace.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IPublicApiWorkspace, IPublicResourceList, IPublicTypeDisposable, IPublicTypeResourceType } from '@alilc/lowcode-types';
import { IWorkspace } from '@alilc/lowcode-workspace';
import { workspaceSymbol } from '../symbols';
import { resourceSymbol, workspaceSymbol } from '../symbols';
import { Resource as ShellResource, Window as ShellWindow } from '../model';
import { Plugins } from './plugins';

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

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

openEditorWindowById(id: string) {
this[workspaceSymbol].openEditorWindowById(id);
}

removeEditorWindow(resourceName: string, title: string) {
this[workspaceSymbol].removeEditorWindow(resourceName, title);
removeEditorWindow(resourceName: string, id: string) {
this[workspaceSymbol].removeEditorWindow(resourceName, id);
}

removeEditorWindowById(id: string) {
Expand Down
4 changes: 4 additions & 0 deletions packages/shell/src/model/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ export class Resource implements IPublicModelResource {
return this[resourceSymbol].title;
}

get id() {
return this[resourceSymbol].id;
}

get icon() {
return this[resourceSymbol].icon;
}
Expand Down
13 changes: 10 additions & 3 deletions packages/types/src/shell/api/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { IPublicApiPlugins, IPublicModelResource, IPublicResourceList, IPublicTy

export interface IPublicApiWorkspace<
Plugins = IPublicApiPlugins,
ModelWindow = IPublicModelWindow
ModelWindow = IPublicModelWindow,
Resource = IPublicModelResource,
> {

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

/**
* 打开视图窗口
* @deprecated
*/
openEditorWindow(resourceName: string, id: string, extra: Object, viewName?: string, sleep?: boolean): Promise<void>;

/** 打开视图窗口 */
openEditorWindow(resourceName: string, title: string, extra: Object, viewName?: string, sleep?: boolean): Promise<void>;
openEditorWindow(resource: Resource, sleep?: boolean): Promise<void>;

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

/** 移除视图窗口 */
removeEditorWindow(resourceName: string, title: string): void;
removeEditorWindow(resourceName: string, id: string): void;

/** 通过视图 id 移除窗口 */
removeEditorWindowById(id: string): void;
Expand Down
2 changes: 2 additions & 0 deletions packages/types/src/shell/model/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ export interface IBaseModelResource<
> {
get title(): string | undefined;

get id(): string | undefined;

get icon(): ReactElement | undefined;

get options(): Record<string, any>;
Expand Down
7 changes: 3 additions & 4 deletions packages/types/src/shell/type/resource-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ export interface IPublicResourceData {
/** 资源标题 */
title?: string;

/** 资源 Id */
id?: string;

/** 分类 */
category?: string;

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

/** 资源子元素 */
children?: IPublicResourceData[];

config?: {
disableBehaviors?: ('copy' | 'remove')[];
};
}

export type IPublicResourceList = IPublicResourceData[];
4 changes: 4 additions & 0 deletions packages/workspace/src/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ export class Resource implements IResource {
return this.resourceData.title || this.resourceTypeInstance.defaultTitle;
}

get id(): string | undefined {
return this.resourceData.id;
}

get options() {
return this.resourceData.options;
}
Expand Down
55 changes: 49 additions & 6 deletions packages/workspace/src/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ export interface IWorkspace extends Omit<IPublicApiWorkspace<
onChangeActiveEditorView(fn: () => void): IPublicTypeDisposable;

emitChangeActiveEditorView(): void;

openEditorWindowByResource(resource: IResource, sleep: boolean): Promise<void>;
}

export class Workspace implements IWorkspace {
Expand Down Expand Up @@ -91,12 +93,12 @@ export class Workspace implements IWorkspace {

@obx.ref window: IEditorWindow;

windowQueue: {
windowQueue: ({
name: string;
title: string;
options: Object;
viewName?: string;
}[] = [];
} | IResource)[] = [];

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

private remove(index: number) {
private async remove(index: number) {
if (index < 0) {
return;
}
Expand All @@ -202,16 +204,16 @@ export class Workspace implements IWorkspace {
if (this.window === window) {
this.window = this.windows[index] || this.windows[index + 1] || this.windows[index - 1];
if (this.window?.sleep) {
this.window.init();
await this.window.init();
}
this.emitChangeActiveWindow();
}
this.emitChangeWindow();
this.window?.updateState(WINDOW_STATE.active);
}

removeEditorWindow(resourceName: string, title: string) {
const index = this.windows.findIndex(d => (d.resource?.name === resourceName && d.title === title));
removeEditorWindow(resourceName: string, id: string) {
const index = this.windows.findIndex(d => (d.resource?.name === resourceName && d.title === id));
this.remove(index);
}

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

async openEditorWindowByResource(resource: IResource, sleep: boolean = false): Promise<void> {
if (this.window && !this.window?.initReady && !sleep) {
this.windowQueue.push(resource);
return;
}

this.window?.updateState(WINDOW_STATE.inactive);

const filterWindows = this.windows.filter(d => (d.resource?.id === resource.id));
if (filterWindows && filterWindows.length) {
this.window = filterWindows[0];
if (!sleep && this.window.sleep) {
await this.window.init();
} else {
this.checkWindowQueue();
}
this.emitChangeActiveWindow();
this.window?.updateState(WINDOW_STATE.active);
return;
}

const window = new EditorWindow(resource, this, {
title: resource.title,
options: resource.options,
viewName: resource.viewName,
sleep,
});

this.windows = [...this.windows, window];
this.editorWindowMap.set(window.id, window);
if (sleep) {
this.emitChangeWindow();
return;
}
this.window = window;
await this.window.init();
this.emitChangeWindow();
this.emitChangeActiveWindow();
this.window?.updateState(WINDOW_STATE.active);
}

async openEditorWindow(name: string, title: string, options: Object, viewName?: string, sleep?: boolean) {
if (this.window && !this.window?.initReady && !sleep) {
this.windowQueue.push({
Expand Down

0 comments on commit 899ffa1

Please sign in to comment.