Skip to content

Commit

Permalink
fix(workspace): fix workspace editorView is undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
liujuping committed Feb 7, 2024
1 parent 86d50e0 commit 1953691
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
8 changes: 7 additions & 1 deletion packages/shell/src/api/material.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
IDesigner,
isComponentMeta,
} from '@alilc/lowcode-designer';
import { IPublicTypeAssetsJson } from '@alilc/lowcode-utils';
import { IPublicTypeAssetsJson, getLogger } from '@alilc/lowcode-utils';
import {
IPublicTypeComponentAction,
IPublicTypeComponentMetadata,
Expand All @@ -21,6 +21,8 @@ import { editorSymbol, designerSymbol } from '../symbols';
import { ComponentMeta as ShellComponentMeta } from '../model';
import { ComponentType } from 'react';

const logger = getLogger({ level: 'warn', bizName: 'shell-material' });

const innerEditorSymbol = Symbol('editor');
export class Material implements IPublicApiMaterial {
private readonly [innerEditorSymbol]: IPublicModelEditor;
Expand All @@ -31,6 +33,10 @@ export class Material implements IPublicApiMaterial {
}
const workspace: InnerWorkspace = globalContext.get('workspace');
if (workspace.isActive) {
if (!workspace.window.editor) {
logger.error('Material api 调用时机出现问题,请检查');
return this[innerEditorSymbol];
}
return workspace.window.editor;
}

Expand Down
4 changes: 2 additions & 2 deletions packages/shell/src/model/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ export class Window implements IPublicModelWindow {
}

get currentEditorView() {
if (this[windowSymbol].editorView) {
return new EditorView(this[windowSymbol].editorView).toProxy() as any;
if (this[windowSymbol]._editorView) {
return new EditorView(this[windowSymbol]._editorView).toProxy() as any;
}
return null;
}
Expand Down
25 changes: 16 additions & 9 deletions packages/workspace/src/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export interface IEditorWindow extends Omit<IPublicModelWindow<IResource>, 'chan

editorViews: Map<string, IViewContext>;

editorView: IViewContext;
_editorView: IViewContext;

changeViewName: (name: string, ignoreEmit?: boolean) => void;

Expand Down Expand Up @@ -54,14 +54,21 @@ export class EditorWindow implements IEditorWindow {

url: string | undefined;

@obx.ref editorView: Context;
@obx.ref _editorView: Context;

@obx editorViews: Map<string, Context> = new Map<string, Context>();

@obx initReady = false;

sleep: boolean | undefined;

get editorView() {
if (!this._editorView) {
return this.editorViews.values().next().value;
}
return this._editorView;
}

constructor(readonly resource: IResource, readonly workspace: IWorkspace, private config: IWindowCOnfig) {
makeObservable(this);
this.title = config.title;
Expand All @@ -75,10 +82,10 @@ export class EditorWindow implements IEditorWindow {
updateState(state: WINDOW_STATE): void {
switch (state) {
case WINDOW_STATE.active:
this.editorView?.setActivate(true);
this._editorView?.setActivate(true);
break;
case WINDOW_STATE.inactive:
this.editorView?.setActivate(false);
this._editorView?.setActivate(false);
break;
case WINDOW_STATE.destroyed:
break;
Expand Down Expand Up @@ -146,7 +153,7 @@ export class EditorWindow implements IEditorWindow {
for (let i = 0; i < editorViews.length; i++) {
const name = editorViews[i].viewName;
await this.initViewType(name);
if (!this.editorView) {
if (!this._editorView) {
this.changeViewName(name);
}
}
Expand Down Expand Up @@ -190,14 +197,14 @@ export class EditorWindow implements IEditorWindow {
};

changeViewName = (name: string, ignoreEmit: boolean = true) => {
this.editorView?.setActivate(false);
this.editorView = this.editorViews.get(name)!;
this._editorView?.setActivate(false);
this._editorView = this.editorViews.get(name)!;

if (!this.editorView) {
if (!this._editorView) {
return;
}

this.editorView.setActivate(true);
this._editorView.setActivate(true);

if (!ignoreEmit) {
this.emitter.emit('window.change.view.type', name);
Expand Down

0 comments on commit 1953691

Please sign in to comment.