Skip to content

Commit

Permalink
feat: add state for workspace windows
Browse files Browse the repository at this point in the history
  • Loading branch information
liujuping authored and JackLian committed Jul 25, 2023
1 parent e875e33 commit d267fec
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
33 changes: 33 additions & 0 deletions packages/workspace/src/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,22 @@ export interface IEditorWindow extends Omit<IPublicModelWindow<IResource>, 'chan
sleep?: boolean;

init(): void;

updateState(state: WINDOW_STATE): void;
}

export enum WINDOW_STATE {
// 睡眠
sleep = 'sleep',

// 激活
active = 'active',

// 未激活
inactive = 'inactive',

// 销毁
destroyed = 'destroyed'
}

export class EditorWindow implements IEditorWindow {
Expand All @@ -51,6 +67,22 @@ export class EditorWindow implements IEditorWindow {
this.title = config.title;
this.icon = resource.icon;
this.sleep = config.sleep;
if (config.sleep) {
this.updateState(WINDOW_STATE.sleep);
}
}

updateState(state: WINDOW_STATE): void {
switch (state) {
case WINDOW_STATE.active:
this.editorView?.setActivate(true);
break;
case WINDOW_STATE.inactive:
this.editorView?.setActivate(false);
break;
case WINDOW_STATE.destroyed:
break;
}
}

async importSchema(schema: any) {
Expand Down Expand Up @@ -102,6 +134,7 @@ export class EditorWindow implements IEditorWindow {
this.initReady = true;
this.workspace.checkWindowQueue();
this.sleep = false;
this.updateState(WINDOW_STATE.active);
}

initViewTypes = async () => {
Expand Down
9 changes: 8 additions & 1 deletion packages/workspace/src/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { IDesigner, ILowCodePluginManager, LowCodePluginManager } from '@alilc/l
import { createModuleEventBus, Editor, IEditor, IEventBus, makeObservable, obx } from '@alilc/lowcode-editor-core';
import { IPublicApiPlugins, IPublicApiWorkspace, IPublicEnumPluginRegisterLevel, IPublicResourceList, IPublicTypeDisposable, IPublicTypeResourceType, IShellModelFactory } from '@alilc/lowcode-types';
import { BasicContext } from './context/base-context';
import { EditorWindow } from './window';
import { EditorWindow, WINDOW_STATE } from './window';
import type { IEditorWindow } from './window';
import { IResource, Resource } from './resource';
import { IResourceType, ResourceType } from './resource-type';
Expand Down Expand Up @@ -198,6 +198,7 @@ export class Workspace implements IWorkspace {
}
const window = this.windows[index];
this.windows.splice(index, 1);
this.window?.updateState(WINDOW_STATE.destroyed);
if (this.window === window) {
this.window = this.windows[index] || this.windows[index + 1] || this.windows[index - 1];
if (this.window?.sleep) {
Expand All @@ -206,6 +207,7 @@ export class Workspace implements IWorkspace {
this.emitChangeActiveWindow();
}
this.emitChangeWindow();
this.window?.updateState(WINDOW_STATE.active);
}

removeEditorWindow(resourceName: string, title: string) {
Expand All @@ -215,13 +217,15 @@ export class Workspace implements IWorkspace {

async openEditorWindowById(id: string) {
const window = this.editorWindowMap.get(id);
this.window?.updateState(WINDOW_STATE.inactive);
if (window) {
this.window = window;
if (window.sleep) {
await window.init();
}
this.emitChangeActiveWindow();
}
this.window?.updateState(WINDOW_STATE.active);
}

async openEditorWindow(name: string, title: string, options: Object, viewType?: string, sleep?: boolean) {
Expand All @@ -236,6 +240,7 @@ export class Workspace implements IWorkspace {
console.error(`${name} resourceType is not available`);
return;
}
this.window?.updateState(WINDOW_STATE.inactive);
const filterWindows = this.windows.filter(d => (d.resource?.name === name && d.resource.title == title));
if (filterWindows && filterWindows.length) {
this.window = filterWindows[0];
Expand All @@ -245,6 +250,7 @@ export class Workspace implements IWorkspace {
this.checkWindowQueue();
}
this.emitChangeActiveWindow();
this.window?.updateState(WINDOW_STATE.active);
return;
}
const resource = new Resource({
Expand All @@ -266,6 +272,7 @@ export class Workspace implements IWorkspace {
}
this.emitChangeWindow();
this.emitChangeActiveWindow();
this.window?.updateState(WINDOW_STATE.active);
}

onChangeWindows(fn: () => void) {
Expand Down

0 comments on commit d267fec

Please sign in to comment.