Skip to content

Commit

Permalink
feat: add types for engineConfig and put it to types
Browse files Browse the repository at this point in the history
  • Loading branch information
JackLian authored and liujuping committed Dec 8, 2022
1 parent 5a3ca97 commit 5de97c1
Show file tree
Hide file tree
Showing 5 changed files with 219 additions and 150 deletions.
170 changes: 27 additions & 143 deletions packages/editor-core/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { ComponentType } from 'react';
import { get as lodashGet } from 'lodash';
import { isPlainObject } from '@alilc/lowcode-utils';

import { RequestHandlersMap } from '@alilc/lowcode-datasource-types';

import { EngineOptions, IEngineConfig } from '@alilc/lowcode-types';
import { getLogger } from './utils/logger';

const logger = getLogger({ level: 'log', bizName: 'config' });
Expand Down Expand Up @@ -53,7 +50,7 @@ const VALID_ENGINE_OPTIONS = {
enableStrictPluginMode: {
type: 'boolean',
default: STRICT_PLUGIN_MODE_DEFAULT,
description: '开启严格插件模式,默认值: STRICT_PLUGIN_MODE_DEFAULT , 严格模式下,插件将无法通过engineOptions传递自定义配置项',
description: '开启严格插件模式,默认值STRICT_PLUGIN_MODE_DEFAULT , 严格模式下,插件将无法通过 engineOptions 传递自定义配置项',
},
enableReactiveContainer: {
type: 'boolean',
Expand Down Expand Up @@ -123,7 +120,7 @@ const VALID_ENGINE_OPTIONS = {
description: '自定义 simulatorUrl 的地址',
},
/**
* 与 react-renderer 的 appHelper 一致, https://www.yuque.com/lce/doc/nhilce#appHelper
* 与 react-renderer 的 appHelper 一致,https://lowcode-engine.cn/site/docs/guide/expand/runtime/renderer#apphelper
*/
appHelper: {
type: 'object',
Expand All @@ -146,138 +143,7 @@ const VALID_ENGINE_OPTIONS = {
description: '配置指定节点为根组件',
},
};
export interface EngineOptions {
/**
* 是否开启 condition 的能力,默认在设计器中不管 condition 是啥都正常展示
*/
enableCondition?: boolean;
/**
* @todo designMode 无法映射到文档渲染模块
*
* 设计模式,live 模式将会实时展示变量值,默认值:'design'
*/
designMode?: 'design' | 'live';
/**
* 设备类型,默认值:'default'
*/
device?: 'default' | 'mobile' | string;
/**
* 指定初始化的 deviceClassName,挂载到画布的顶层节点上
*/
deviceClassName?: string;
/**
* 语言,默认值:'zh-CN'
*/
locale?: string;
/**
* 渲染器类型,默认值:'react'
*/
renderEnv?: 'react' | 'rax' | string;
/**
* 设备类型映射器,处理设计器与渲染器中 device 的映射
*/
deviceMapper?: {
transform: (originalDevice: string) => string;
};
/**
* 开启严格插件模式,默认值: STRICT_PLUGIN_MODE_DEFAULT , 严格模式下,插件将无法通过engineOptions传递自定义配置项
* enable strict plugin mode, default value: false
* under strict mode, customed engineOption is not accepted.
*/
enableStrictPluginMode?: boolean;
/**
* 开启拖拽组件时,即将被放入的容器是否有视觉反馈,默认值:false
*/
enableReactiveContainer?: boolean;
/**
* 关闭画布自动渲染,在资产包多重异步加载的场景有效,默认值:false
*/
disableAutoRender?: boolean;
/**
* 关闭拖拽组件时的虚线响应,性能考虑,默认值:false
*/
disableDetecting?: boolean;
/**
* 定制画布中点击被忽略的 selectors,默认值:undefined
*/
customizeIgnoreSelectors?: (defaultIgnoreSelectors: string[], e: MouseEvent) => string[];
/**
* 禁止默认的设置面板,默认值:false
*/
disableDefaultSettingPanel?: boolean;
/**
* 禁止默认的设置器,默认值:false
*/
disableDefaultSetters?: boolean;
/**
* 打开画布的锁定操作,默认值:false
*/
enableCanvasLock?: boolean;
/**
* 容器锁定后,容器本身是否可以设置属性,仅当画布锁定特性开启时生效, 默认值为:false
*/
enableLockedNodeSetting?: boolean;
/**
* 当选中节点切换时,是否停留在相同的设置 tab 上,默认值:false
*/
stayOnTheSameSettingTab?: boolean;
/**
* 是否在只有一个 item 的时候隐藏设置 tabs,默认值:false
*/
hideSettingsTabsWhenOnlyOneItem?: boolean;
/**
* 自定义 loading 组件
*/
loadingComponent?: ComponentType;
/**
* 设置所有属性支持变量配置,默认值:false
*/
supportVariableGlobally?: boolean;
/**
* 设置 simulator 相关的 url,默认值:undefined
*/
simulatorUrl?: string[];
/**
* Vision-polyfill settings
*/
visionSettings?: {
// 是否禁用降级 reducer,默认值:false
disableCompatibleReducer?: boolean;
// 是否开启在 render 阶段开启 filter reducer,默认值:false
enableFilterReducerInRenderStage?: boolean;
};
/**
* 与 react-renderer 的 appHelper 一致, https://www.yuque.com/lce/doc/nhilce#appHelper
*/
appHelper?: {
/** 全局公共函数 */
utils?: Record<string, any>;
/** 全局常量 */
constants?: Record<string, any>;
};

/**
* 数据源引擎的请求处理器映射
*/
requestHandlersMap?: RequestHandlersMap;

/**
* @default true
* JSExpression 是否只支持使用 this 来访问上下文变量,假如需要兼容原来的 'state.xxx',则设置为 false
*/
thisRequiredInJSE?: boolean;

/**
* @default false
* 当开启组件未找到严格模式时,渲染模块不会默认给一个容器组件
*/
enableStrictNotFoundMode?: boolean;

/**
* 配置指定节点为根组件
*/
focusNodeSelector?: (rootNode: Node) => Node;
}

const getStrictModeValue = (engineOptions: EngineOptions, defaultValue: boolean): boolean => {
if (!engineOptions || !isPlainObject(engineOptions)) {
Expand All @@ -289,7 +155,25 @@ const getStrictModeValue = (engineOptions: EngineOptions, defaultValue: boolean)
}
return engineOptions.enableStrictPluginMode;
};
export class EngineConfig {

export interface IEngineConfigPrivate {
/**
* if engineOptions.strictPluginMode === true, only accept propertied predefined in EngineOptions.
*
* @param {EngineOptions} engineOptions
* @memberof EngineConfig
*/
setEngineOptions(engineOptions: EngineOptions): void;

notifyGot(key: string): void;

setWait(key: string, resolve: (data: any) => void, once?: boolean): void;

delWait(key: string, fn: any): void;
}


export class EngineConfig implements IEngineConfig, IEngineConfigPrivate {
private config: { [key: string]: any } = {};

private waits = new Map<
Expand Down Expand Up @@ -363,7 +247,7 @@ export class EngineConfig {
};
Object.keys(engineOptions).forEach((key) => {
if (isValidKey(key)) {
this.set(key, engineOptions[key]);
this.set(key, (engineOptions as any)[key]);
} else {
logger.warn(`failed to config ${key} to engineConfig, only predefined options can be set under strict mode, predefined options: `, VALID_ENGINE_OPTIONS);
}
Expand Down Expand Up @@ -408,7 +292,7 @@ export class EngineConfig {
}
}

private notifyGot(key: string) {
notifyGot(key: string): void {
let waits = this.waits.get(key);
if (!waits) {
return;
Expand All @@ -428,7 +312,7 @@ export class EngineConfig {
}
}

private setWait(key: string, resolve: (data: any) => void, once?: boolean) {
setWait(key: string, resolve: (data: any) => void, once?: boolean) {
const waits = this.waits.get(key);
if (waits) {
waits.push({ resolve, once });
Expand All @@ -437,7 +321,7 @@ export class EngineConfig {
}
}

private delWait(key: string, fn: any) {
delWait(key: string, fn: any) {
const waits = this.waits.get(key);
if (!waits) {
return;
Expand All @@ -454,4 +338,4 @@ export class EngineConfig {
}
}

export const engineConfig = new EngineConfig();
export const engineConfig = new EngineConfig();
15 changes: 9 additions & 6 deletions packages/editor-core/src/editor.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable no-console */
/* eslint-disable max-len */
import { StrictEventEmitter } from 'strict-event-emitter-types';
import { EventEmitter } from 'events';
import {
Expand All @@ -13,7 +15,6 @@ import {
} from '@alilc/lowcode-types';
import { engineConfig } from './config';
import { globalLocale } from './intl';
import * as utils from './utils';
import Preference from './utils/preference';
import { obx } from './utils';
import { AssetsJson, AssetLoader } from '@alilc/lowcode-utils';
Expand Down Expand Up @@ -68,15 +69,17 @@ export class Editor extends (EventEmitter as any) implements IEditor {

private hooks: HookConfig[] = [];

get<T = undefined, KeyOrType = any>(keyOrType: KeyOrType): GetReturnType<T, KeyOrType> | undefined {
get<T = undefined, KeyOrType = any>(
keyOrType: KeyOrType,
): GetReturnType<T, KeyOrType> | undefined {
return this.context.get(keyOrType as any);
}

has(keyOrType: KeyType): boolean {
return this.context.has(keyOrType);
}

set(key: KeyType, data: any): void | Promise<void> {
set(key: KeyType, data: any): void | Promise<void> {
if (key === 'assets') {
return this.setAssets(data);
}
Expand Down Expand Up @@ -113,8 +116,8 @@ export class Editor extends (EventEmitter as any) implements IEditor {
const { exportName, url } = component;
await (new AssetLoader()).load(url);
if (window[exportName]) {
assets.components = assets.components.concat(window[exportName].components || []);
assets.componentList = assets.componentList.concat(window[exportName].componentList || []);
assets.components = assets.components.concat((window[exportName] as any).components || []);
assets.componentList = assets.componentList?.concat((window[exportName] as any).componentList || []);
}
return window[exportName];
}),
Expand Down Expand Up @@ -215,7 +218,7 @@ export class Editor extends (EventEmitter as any) implements IEditor {
registerHooks = (hooks: HookConfig[]) => {
this.initHooks(hooks).forEach(({ message, type, handler }) => {
if (['on', 'once'].indexOf(type) !== -1) {
this[type](message, handler);
this[type]((message as any), handler);
}
});
};
Expand Down
2 changes: 1 addition & 1 deletion packages/types/src/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export interface AssetsJson {
*/
version: string;
/**
* 大包列表,external与package的概念相似,融合在一起
* 大包列表,external 与 package 的概念相似,融合在一起
*/
packages?: Package[];
/**
Expand Down
Loading

0 comments on commit 5de97c1

Please sign in to comment.