From 899ffa15541164c31e2e3688344639046df282ec Mon Sep 17 00:00:00 2001 From: liujuping Date: Mon, 21 Aug 2023 10:56:03 +0800 Subject: [PATCH] feat(workspace): update openEditorWindow api --- docs/docs/api/workspace.md | 11 +++- .../designer/src/plugin/plugin-manager.ts | 4 +- packages/shell/src/api/workspace.ts | 14 +++-- packages/shell/src/model/resource.ts | 4 ++ packages/types/src/shell/api/workspace.ts | 13 ++++- packages/types/src/shell/model/resource.ts | 2 + .../types/src/shell/type/resource-list.ts | 7 +-- packages/workspace/src/resource.ts | 4 ++ packages/workspace/src/workspace.ts | 55 +++++++++++++++++-- 9 files changed, 92 insertions(+), 22 deletions(-) diff --git a/docs/docs/api/workspace.md b/docs/docs/api/workspace.md index 9d9960927..5e0f7acd2 100644 --- a/docs/docs/api/workspace.md +++ b/docs/docs/api/workspace.md @@ -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; + +/** 打开视图窗口 */ +openEditorWindow(resource: Resource, sleep?: boolean): Promise; ``` ### openEditorWindowById @@ -100,7 +107,7 @@ openEditorWindowById(id: string): void; 移除视图窗口 ```typescript -removeEditorWindow(resourceName: string, title: string): void; +removeEditorWindow(resourceName: string, id: string): void; ``` ### removeEditorWindowById diff --git a/packages/designer/src/plugin/plugin-manager.ts b/packages/designer/src/plugin/plugin-manager.ts index f30ccdc8f..f6b325dc0 100644 --- a/packages/designer/src/plugin/plugin-manager.ts +++ b/packages/designer/src/plugin/plugin-manager.ts @@ -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 diff --git a/packages/shell/src/api/workspace.ts b/packages/shell/src/api/workspace.ts index 1a16d31ce..b19638ec7 100644 --- a/packages/shell/src/api/workspace.ts +++ b/packages/shell/src/api/workspace.ts @@ -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'; @@ -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 { - await this[workspaceSymbol].openEditorWindow(resourceName, title, extra, viewName, sleep); + async openEditorWindow(): Promise { + 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) { diff --git a/packages/shell/src/model/resource.ts b/packages/shell/src/model/resource.ts index 6a1a07e49..29a385b99 100644 --- a/packages/shell/src/model/resource.ts +++ b/packages/shell/src/model/resource.ts @@ -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; } diff --git a/packages/types/src/shell/api/workspace.ts b/packages/types/src/shell/api/workspace.ts index 8ae2434bc..d4232d9a1 100644 --- a/packages/types/src/shell/api/workspace.ts +++ b/packages/types/src/shell/api/workspace.ts @@ -3,7 +3,8 @@ import { IPublicApiPlugins, IPublicModelResource, IPublicResourceList, IPublicTy export interface IPublicApiWorkspace< Plugins = IPublicApiPlugins, - ModelWindow = IPublicModelWindow + ModelWindow = IPublicModelWindow, + Resource = IPublicModelResource, > { /** 是否启用 workspace 模式 */ @@ -29,14 +30,20 @@ export interface IPublicApiWorkspace< /** 注册资源 */ registerResourceType(resourceTypeModel: IPublicTypeResourceType): void; + /** + * 打开视图窗口 + * @deprecated + */ + openEditorWindow(resourceName: string, id: string, extra: Object, viewName?: string, sleep?: boolean): Promise; + /** 打开视图窗口 */ - openEditorWindow(resourceName: string, title: string, extra: Object, viewName?: string, sleep?: boolean): Promise; + openEditorWindow(resource: Resource, sleep?: boolean): Promise; /** 通过视图 id 打开窗口 */ openEditorWindowById(id: string): void; /** 移除视图窗口 */ - removeEditorWindow(resourceName: string, title: string): void; + removeEditorWindow(resourceName: string, id: string): void; /** 通过视图 id 移除窗口 */ removeEditorWindowById(id: string): void; diff --git a/packages/types/src/shell/model/resource.ts b/packages/types/src/shell/model/resource.ts index 0d791412b..b79b6acfc 100644 --- a/packages/types/src/shell/model/resource.ts +++ b/packages/types/src/shell/model/resource.ts @@ -5,6 +5,8 @@ export interface IBaseModelResource< > { get title(): string | undefined; + get id(): string | undefined; + get icon(): ReactElement | undefined; get options(): Record; diff --git a/packages/types/src/shell/type/resource-list.ts b/packages/types/src/shell/type/resource-list.ts index 7abdcc7b1..ec998a95b 100644 --- a/packages/types/src/shell/type/resource-list.ts +++ b/packages/types/src/shell/type/resource-list.ts @@ -8,6 +8,9 @@ export interface IPublicResourceData { /** 资源标题 */ title?: string; + /** 资源 Id */ + id?: string; + /** 分类 */ category?: string; @@ -24,10 +27,6 @@ export interface IPublicResourceData { /** 资源子元素 */ children?: IPublicResourceData[]; - - config?: { - disableBehaviors?: ('copy' | 'remove')[]; - }; } export type IPublicResourceList = IPublicResourceData[]; \ No newline at end of file diff --git a/packages/workspace/src/resource.ts b/packages/workspace/src/resource.ts index a7ecc59b2..6e8518385 100644 --- a/packages/workspace/src/resource.ts +++ b/packages/workspace/src/resource.ts @@ -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; } diff --git a/packages/workspace/src/workspace.ts b/packages/workspace/src/workspace.ts index b12fbb65b..50255b7db 100644 --- a/packages/workspace/src/workspace.ts +++ b/packages/workspace/src/workspace.ts @@ -50,6 +50,8 @@ export interface IWorkspace extends Omit void): IPublicTypeDisposable; emitChangeActiveEditorView(): void; + + openEditorWindowByResource(resource: IResource, sleep: boolean): Promise; } export class Workspace implements IWorkspace { @@ -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, @@ -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; } @@ -202,7 +204,7 @@ 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(); } @@ -210,8 +212,8 @@ export class Workspace implements IWorkspace { 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); } @@ -228,6 +230,47 @@ export class Workspace implements IWorkspace { this.window?.updateState(WINDOW_STATE.active); } + async openEditorWindowByResource(resource: IResource, sleep: boolean = false): Promise { + 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({