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

feat(workspace): add enableAutoOpenFirstWindow config and onWindowRendererReady function #1910

Merged
merged 1 commit into from
Apr 19, 2023
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
5 changes: 5 additions & 0 deletions packages/editor-core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ const VALID_ENGINE_OPTIONS = {
type: 'function',
description: '配置指定节点为根组件',
},
enableAutoOpenFirstWindow: {
type: 'boolean',
description: '应用级设计模式下,自动打开第一个窗口',
default: true,
},
};

const getStrictModeValue = (engineOptions: IPublicTypeEngineOptions, defaultValue: boolean): boolean => {
Expand Down
13 changes: 10 additions & 3 deletions packages/engine/src/engine-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,23 @@ import {
engineConfig,
Setters as InnerSetters,
Hotkey as InnerHotkey,
IEditor,
} from '@alilc/lowcode-editor-core';
import {
IPublicTypeEngineOptions,
IPublicModelDocumentModel,
IPublicTypePluginMeta,
IPublicTypeDisposable,
IPublicApiPlugins,
IPublicApiWorkspace,
} from '@alilc/lowcode-types';
import {
Designer,
LowCodePluginManager,
ILowCodePluginContextPrivate,
ILowCodePluginContextApiAssembler,
PluginPreference,
IDesigner,
} from '@alilc/lowcode-designer';
import {
Skeleton as InnerSkeleton,
Expand All @@ -30,6 +34,7 @@ import {
import {
Workspace as InnerWorkspace,
Workbench as WorkSpaceWorkbench,
IWorkspace,
} from '@alilc/lowcode-workspace';

import {
Expand Down Expand Up @@ -61,7 +66,7 @@ export * from './modules/skeleton-types';
export * from './modules/designer-types';
export * from './modules/lowcode-types';

async function registryInnerPlugin(designer: Designer, editor: Editor, plugins: Plugins): Promise<IPublicTypeDisposable> {
async function registryInnerPlugin(designer: IDesigner, editor: IEditor, plugins: IPublicApiPlugins): Promise<IPublicTypeDisposable> {
// 注册一批内置插件
const componentMetaParserPlugin = componentMetaParser(designer);
const defaultPanelRegistryPlugin = defaultPanelRegistry(editor);
Expand All @@ -83,8 +88,8 @@ async function registryInnerPlugin(designer: Designer, editor: Editor, plugins:
};
}

const innerWorkspace = new InnerWorkspace(registryInnerPlugin, shellModelFactory);
const workspace = new Workspace(innerWorkspace);
const innerWorkspace: IWorkspace = new InnerWorkspace(registryInnerPlugin, shellModelFactory);
const workspace: IPublicApiWorkspace = new Workspace(innerWorkspace);
const editor = new Editor();
globalContext.register(editor, Editor);
globalContext.register(editor, 'editor');
Expand Down Expand Up @@ -207,7 +212,9 @@ export async function init(
}),
engineContainer,
);
innerWorkspace.enableAutoOpenFirstWindow = engineConfig.get('enableAutoOpenFirstWindow', true);
innerWorkspace.setActive(true);
innerWorkspace.initWindow();
innerHotkey.activate(false);
await innerWorkspace.plugins.init(pluginPreference);
return;
Expand Down
2 changes: 1 addition & 1 deletion packages/shell/src/api/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class Project implements IPublicApiProject {
}
const workspace = globalContext.get('workspace');
if (workspace.isActive) {
if (!workspace.window.innerProject) {
if (!workspace.window?.innerProject) {
logger.error('project api 调用时机出现问题,请检查');
return this[innerProjectSymbol];
}
Expand Down
7 changes: 7 additions & 0 deletions packages/shell/src/api/skeleton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ import {
} from '@alilc/lowcode-editor-skeleton';
import { skeletonSymbol } from '../symbols';
import { IPublicApiSkeleton, IPublicTypeDisposable, IPublicTypeSkeletonConfig, IPublicTypeWidgetConfigArea } from '@alilc/lowcode-types';
import { getLogger } from '@alilc/lowcode-utils';

const innerSkeletonSymbol = Symbol('skeleton');

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

export class Skeleton implements IPublicApiSkeleton {
private readonly [innerSkeletonSymbol]: ISkeleton;
private readonly pluginName: string;
Expand All @@ -18,6 +21,10 @@ export class Skeleton implements IPublicApiSkeleton {
}
const workspace = globalContext.get('workspace');
if (workspace.isActive) {
if (!workspace.window.innerSkeleton) {
logger.error('skeleton api 调用时机出现问题,请检查');
return this[innerSkeletonSymbol];
}
return workspace.window.innerSkeleton;
}

Expand Down
7 changes: 7 additions & 0 deletions packages/shell/src/api/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,16 @@ export class Workspace implements IPublicApiWorkspace {
}

get window() {
if (!this[workspaceSymbol].window) {
return null;
}
return new ShellWindow(this[workspaceSymbol].window);
}

onWindowRendererReady(fn: () => void): IPublicTypeDisposable {
return this[workspaceSymbol].onWindowRendererReady(fn);
}

registerResourceType(resourceTypeModel: IPublicTypeResourceType): void {
this[workspaceSymbol].registerResourceType(resourceTypeModel);
}
Expand Down
4 changes: 4 additions & 0 deletions packages/shell/src/model/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,8 @@ export class Window implements IPublicModelWindow {
async save() {
return await this[windowSymbol].save();
}

get plugins() {
return this[windowSymbol].plugins;
}
}
8 changes: 7 additions & 1 deletion packages/types/src/shell/api/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface IPublicApiWorkspace<
isActive: boolean;

/** 当前设计器窗口 */
window: ModelWindow;
window: ModelWindow | null;

plugins: Plugins;

Expand Down Expand Up @@ -46,4 +46,10 @@ export interface IPublicApiWorkspace<

/** active 窗口变更事件 */
onChangeActiveWindow(fn: () => void): IPublicTypeDisposable;

/**
* window 下的所有视图 renderer ready 事件
* @since v1.1.7
*/
onWindowRendererReady(fn: () => void): IPublicTypeDisposable;
}
9 changes: 9 additions & 0 deletions packages/types/src/shell/type/engine-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { RequestHandlersMap } from '@alilc/lowcode-datasource-types';
import { ComponentType } from 'react';

export interface IPublicTypeEngineOptions {

/**
* 是否开启 condition 的能力,默认在设计器中不管 condition 是啥都正常展示
* when this is true, node that configured as conditional not renderring
Expand Down Expand Up @@ -136,8 +137,10 @@ export interface IPublicTypeEngineOptions {
* 与 react-renderer 的 appHelper 一致,https://lowcode-engine.cn/site/docs/guide/expand/runtime/renderer#apphelper
*/
appHelper?: {

/** 全局公共函数 */
utils?: Record<string, any>;

/** 全局常量 */
constants?: Record<string, any>;
};
Expand Down Expand Up @@ -168,6 +171,12 @@ export interface IPublicTypeEngineOptions {
* 开启应用级设计模式
*/
enableWorkspaceMode?: boolean;

/**
* @default true
* 应用级设计模式下,自动打开第一个窗口
*/
enableAutoOpenFirstWindow?: boolean;
}

/**
Expand Down
16 changes: 12 additions & 4 deletions packages/workspace/src/context/view-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ export class Context extends BasicContext {

@obx isInit: boolean = false;

@computed get active() {
return this._activate;
}

init = flow(function* (this: Context) {
if (this.viewType === 'webview') {
const url = yield this.instance?.url?.();
Expand All @@ -43,6 +39,18 @@ export class Context extends BasicContext {
makeObservable(this);
}

@computed get active() {
return this._activate;
}

onSimulatorRendererReady = (): Promise<void> => {
return new Promise((resolve) => {
this.project.onSimulatorRendererReady(() => {
resolve();
});
});
};

setActivate = (_activate: boolean) => {
this._activate = _activate;
this.innerHotkey.activate(this._activate);
Expand Down
2 changes: 1 addition & 1 deletion packages/workspace/src/layouts/workbench.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class Workbench extends Component<{
{
workspace.windows.map(d => (
<WindowView
active={d.id === workspace.window.id}
active={d.id === workspace.window?.id}
window={d}
key={d.id}
/>
Expand Down
7 changes: 7 additions & 0 deletions packages/workspace/src/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ export class EditorWindow implements IEditorWindow {
async init() {
await this.initViewTypes();
await this.execViewTypesInit();
Promise.all(Array.from(this.editorViews.values()).map((d) => d.onSimulatorRendererReady)).then(() => {
this.workspace.emitWindowRendererReady();
});
this.url = await this.resource.url();
this.setDefaultViewType();
this.initReady = true;
Expand Down Expand Up @@ -182,6 +185,10 @@ export class EditorWindow implements IEditorWindow {
return this.editorView?.designer;
}

get plugins() {
return this.editorView?.plugins;
}

get innerPlugins() {
return this.editorView?.innerPlugins;
}
Expand Down
44 changes: 31 additions & 13 deletions packages/workspace/src/workspace.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IDesigner, ILowCodePluginManager, LowCodePluginManager } from '@alilc/lowcode-designer';
import { createModuleEventBus, Editor, IEventBus, makeObservable, obx } from '@alilc/lowcode-editor-core';
import { IPublicApiPlugins, IPublicApiWorkspace, IPublicResourceList, IPublicTypeResourceType, IShellModelFactory } from '@alilc/lowcode-types';
import { createModuleEventBus, Editor, IEditor, IEventBus, makeObservable, obx } from '@alilc/lowcode-editor-core';
import { IPublicApiPlugins, IPublicApiWorkspace, IPublicResourceList, IPublicTypeDisposable, IPublicTypeResourceType, IShellModelFactory } from '@alilc/lowcode-types';
import { BasicContext } from './context/base-context';
import { EditorWindow } from './window';
import type { IEditorWindow } from './window';
Expand All @@ -11,6 +11,8 @@ enum EVENT {
CHANGE_WINDOW = 'change_window',

CHANGE_ACTIVE_WINDOW = 'change_active_window',

WINDOW_RENDER_READY = 'window_render_ready',
}

const CHANGE_EVENT = 'resource.list.change';
Expand All @@ -19,10 +21,12 @@ export interface IWorkspace extends Omit<IPublicApiWorkspace<
LowCodePluginManager,
IEditorWindow
>, 'resourceList' | 'plugins'> {
readonly registryInnerPlugin: (designer: IDesigner, editor: Editor, plugins: IPublicApiPlugins) => Promise<void>;
readonly registryInnerPlugin: (designer: IDesigner, editor: Editor, plugins: IPublicApiPlugins) => Promise<IPublicTypeDisposable>;

readonly shellModelFactory: IShellModelFactory;

enableAutoOpenFirstWindow: boolean;

window: IEditorWindow;

plugins: ILowCodePluginManager;
Expand All @@ -32,11 +36,19 @@ export interface IWorkspace extends Omit<IPublicApiWorkspace<
getResourceType(resourceName: string): IResourceType;

checkWindowQueue(): void;

emitWindowRendererReady(): void;

initWindow(): void;

setActive(active: boolean): void;
}

export class Workspace implements IWorkspace {
context: BasicContext;

enableAutoOpenFirstWindow: boolean;

private emitter: IEventBus = createModuleEventBus('workspace');

private _isActive = false;
Expand Down Expand Up @@ -79,10 +91,10 @@ export class Workspace implements IWorkspace {
}[] = [];

constructor(
readonly registryInnerPlugin: (designer: IDesigner, editor: Editor, plugins: IPublicApiPlugins) => Promise<void>,
readonly registryInnerPlugin: (designer: IDesigner, editor: IEditor, plugins: IPublicApiPlugins) => Promise<IPublicTypeDisposable>,
readonly shellModelFactory: any,
) {
this.init();
this.context = new BasicContext(this, '');
makeObservable(this);
}

Expand All @@ -97,13 +109,8 @@ export class Workspace implements IWorkspace {
}
}

init() {
this.initWindow();
this.context = new BasicContext(this, '');
}

initWindow() {
if (!this.defaultResourceType) {
if (!this.defaultResourceType || this.enableAutoOpenFirstWindow === false) {
return;
}
const resourceName = this.defaultResourceType.name;
Expand All @@ -128,7 +135,7 @@ export class Workspace implements IWorkspace {
const resourceType = new ResourceType(resourceTypeModel);
this.resourceTypeMap.set(resourceTypeModel.resourceName, resourceType);

if (!this.window && this.defaultResourceType) {
if (!this.window && this.defaultResourceType && this._isActive) {
this.initWindow();
}
}
Expand All @@ -149,6 +156,17 @@ export class Workspace implements IWorkspace {
};
}

onWindowRendererReady(fn: () => void): IPublicTypeDisposable {
this.emitter.on(EVENT.WINDOW_RENDER_READY, fn);
return () => {
this.emitter.off(EVENT.WINDOW_RENDER_READY, fn);
};
}

emitWindowRendererReady() {
this.emitter.emit(EVENT.WINDOW_RENDER_READY);
}

getResourceType(resourceName: string): IResourceType {
return this.resourceTypeMap.get(resourceName)!;
}
Expand Down Expand Up @@ -188,7 +206,7 @@ export class Workspace implements IWorkspace {
}

openEditorWindow(name: string, title: string, options: Object, viewType?: string, sleep?: boolean) {
if (!this.window?.initReady && !sleep) {
if (this.window && !this.window?.initReady && !sleep) {
this.windowQueue.push({
name, title, options, viewType,
});
Expand Down