Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(workspace): fix workspace editorView is undefined #2921

Merged
merged 1 commit into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading