Skip to content

Commit 44beb2a

Browse files
liujupingJackLian
authored andcommitted
fix(workspace): fix workspace editorView is undefined
1 parent 86d50e0 commit 44beb2a

File tree

3 files changed

+25
-12
lines changed

3 files changed

+25
-12
lines changed

packages/shell/src/api/material.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
IDesigner,
44
isComponentMeta,
55
} from '@alilc/lowcode-designer';
6-
import { IPublicTypeAssetsJson } from '@alilc/lowcode-utils';
6+
import { IPublicTypeAssetsJson, getLogger } from '@alilc/lowcode-utils';
77
import {
88
IPublicTypeComponentAction,
99
IPublicTypeComponentMetadata,
@@ -21,6 +21,8 @@ import { editorSymbol, designerSymbol } from '../symbols';
2121
import { ComponentMeta as ShellComponentMeta } from '../model';
2222
import { ComponentType } from 'react';
2323

24+
const logger = getLogger({ level: 'warn', bizName: 'shell-material' });
25+
2426
const innerEditorSymbol = Symbol('editor');
2527
export class Material implements IPublicApiMaterial {
2628
private readonly [innerEditorSymbol]: IPublicModelEditor;
@@ -31,6 +33,10 @@ export class Material implements IPublicApiMaterial {
3133
}
3234
const workspace: InnerWorkspace = globalContext.get('workspace');
3335
if (workspace.isActive) {
36+
if (!workspace.window.editor) {
37+
logger.error('Material api 调用时机出现问题,请检查');
38+
return this[innerEditorSymbol];
39+
}
3440
return workspace.window.editor;
3541
}
3642

packages/shell/src/model/window.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ export class Window implements IPublicModelWindow {
4848
}
4949

5050
get currentEditorView() {
51-
if (this[windowSymbol].editorView) {
52-
return new EditorView(this[windowSymbol].editorView).toProxy() as any;
51+
if (this[windowSymbol]._editorView) {
52+
return new EditorView(this[windowSymbol]._editorView).toProxy() as any;
5353
}
5454
return null;
5555
}

packages/workspace/src/window.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export interface IEditorWindow extends Omit<IPublicModelWindow<IResource>, 'chan
1717

1818
editorViews: Map<string, IViewContext>;
1919

20-
editorView: IViewContext;
20+
_editorView: IViewContext;
2121

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

@@ -54,14 +54,21 @@ export class EditorWindow implements IEditorWindow {
5454

5555
url: string | undefined;
5656

57-
@obx.ref editorView: Context;
57+
@obx.ref _editorView: Context;
5858

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

6161
@obx initReady = false;
6262

6363
sleep: boolean | undefined;
6464

65+
get editorView() {
66+
if (!this._editorView) {
67+
return this.editorViews.values().next().value;
68+
}
69+
return this._editorView;
70+
}
71+
6572
constructor(readonly resource: IResource, readonly workspace: IWorkspace, private config: IWindowCOnfig) {
6673
makeObservable(this);
6774
this.title = config.title;
@@ -75,10 +82,10 @@ export class EditorWindow implements IEditorWindow {
7582
updateState(state: WINDOW_STATE): void {
7683
switch (state) {
7784
case WINDOW_STATE.active:
78-
this.editorView?.setActivate(true);
85+
this._editorView?.setActivate(true);
7986
break;
8087
case WINDOW_STATE.inactive:
81-
this.editorView?.setActivate(false);
88+
this._editorView?.setActivate(false);
8289
break;
8390
case WINDOW_STATE.destroyed:
8491
break;
@@ -146,7 +153,7 @@ export class EditorWindow implements IEditorWindow {
146153
for (let i = 0; i < editorViews.length; i++) {
147154
const name = editorViews[i].viewName;
148155
await this.initViewType(name);
149-
if (!this.editorView) {
156+
if (!this._editorView) {
150157
this.changeViewName(name);
151158
}
152159
}
@@ -190,14 +197,14 @@ export class EditorWindow implements IEditorWindow {
190197
};
191198

192199
changeViewName = (name: string, ignoreEmit: boolean = true) => {
193-
this.editorView?.setActivate(false);
194-
this.editorView = this.editorViews.get(name)!;
200+
this._editorView?.setActivate(false);
201+
this._editorView = this.editorViews.get(name)!;
195202

196-
if (!this.editorView) {
203+
if (!this._editorView) {
197204
return;
198205
}
199206

200-
this.editorView.setActivate(true);
207+
this._editorView.setActivate(true);
201208

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

0 commit comments

Comments
 (0)