feat: add types for engineConfig and put it to types

This commit is contained in:
JackLian 2022-12-08 13:21:33 +08:00 committed by 刘菊萍(絮黎)
parent 5a3ca97187
commit 5de97c10c8
5 changed files with 219 additions and 150 deletions

View File

@ -1,9 +1,6 @@
import { ComponentType } from 'react';
import { get as lodashGet } from 'lodash'; import { get as lodashGet } from 'lodash';
import { isPlainObject } from '@alilc/lowcode-utils'; import { isPlainObject } from '@alilc/lowcode-utils';
import { EngineOptions, IEngineConfig } from '@alilc/lowcode-types';
import { RequestHandlersMap } from '@alilc/lowcode-datasource-types';
import { getLogger } from './utils/logger'; import { getLogger } from './utils/logger';
const logger = getLogger({ level: 'log', bizName: 'config' }); const logger = getLogger({ level: 'log', bizName: 'config' });
@ -53,7 +50,7 @@ const VALID_ENGINE_OPTIONS = {
enableStrictPluginMode: { enableStrictPluginMode: {
type: 'boolean', type: 'boolean',
default: STRICT_PLUGIN_MODE_DEFAULT, default: STRICT_PLUGIN_MODE_DEFAULT,
description: '开启严格插件模式,默认值: STRICT_PLUGIN_MODE_DEFAULT , 严格模式下插件将无法通过engineOptions传递自定义配置项', description: '开启严格插件模式,默认值STRICT_PLUGIN_MODE_DEFAULT , 严格模式下,插件将无法通过 engineOptions 传递自定义配置项',
}, },
enableReactiveContainer: { enableReactiveContainer: {
type: 'boolean', type: 'boolean',
@ -123,7 +120,7 @@ const VALID_ENGINE_OPTIONS = {
description: '自定义 simulatorUrl 的地址', 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: { appHelper: {
type: 'object', type: 'object',
@ -146,138 +143,7 @@ const VALID_ENGINE_OPTIONS = {
description: '配置指定节点为根组件', 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;
/**
* selectorsundefined
*/
customizeIgnoreSelectors?: (defaultIgnoreSelectors: string[], e: MouseEvent) => string[];
/**
* false
*/
disableDefaultSettingPanel?: boolean;
/**
* false
*/
disableDefaultSetters?: boolean;
/**
* false
*/
enableCanvasLock?: boolean;
/**
* false
*/
enableLockedNodeSetting?: boolean;
/**
* tab false
*/
stayOnTheSameSettingTab?: boolean;
/**
* item tabsfalse
*/
hideSettingsTabsWhenOnlyOneItem?: boolean;
/**
* loading
*/
loadingComponent?: ComponentType;
/**
* false
*/
supportVariableGlobally?: boolean;
/**
* simulator urlundefined
*/
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 => { const getStrictModeValue = (engineOptions: EngineOptions, defaultValue: boolean): boolean => {
if (!engineOptions || !isPlainObject(engineOptions)) { if (!engineOptions || !isPlainObject(engineOptions)) {
@ -289,7 +155,25 @@ const getStrictModeValue = (engineOptions: EngineOptions, defaultValue: boolean)
} }
return engineOptions.enableStrictPluginMode; 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 config: { [key: string]: any } = {};
private waits = new Map< private waits = new Map<
@ -363,7 +247,7 @@ export class EngineConfig {
}; };
Object.keys(engineOptions).forEach((key) => { Object.keys(engineOptions).forEach((key) => {
if (isValidKey(key)) { if (isValidKey(key)) {
this.set(key, engineOptions[key]); this.set(key, (engineOptions as any)[key]);
} else { } else {
logger.warn(`failed to config ${key} to engineConfig, only predefined options can be set under strict mode, predefined options: `, VALID_ENGINE_OPTIONS); logger.warn(`failed to config ${key} to engineConfig, only predefined options can be set under strict mode, predefined options: `, VALID_ENGINE_OPTIONS);
} }
@ -408,7 +292,7 @@ export class EngineConfig {
} }
} }
private notifyGot(key: string) { notifyGot(key: string): void {
let waits = this.waits.get(key); let waits = this.waits.get(key);
if (!waits) { if (!waits) {
return; return;
@ -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); const waits = this.waits.get(key);
if (waits) { if (waits) {
waits.push({ resolve, once }); waits.push({ resolve, once });
@ -437,7 +321,7 @@ export class EngineConfig {
} }
} }
private delWait(key: string, fn: any) { delWait(key: string, fn: any) {
const waits = this.waits.get(key); const waits = this.waits.get(key);
if (!waits) { if (!waits) {
return; return;

View File

@ -1,3 +1,5 @@
/* eslint-disable no-console */
/* eslint-disable max-len */
import { StrictEventEmitter } from 'strict-event-emitter-types'; import { StrictEventEmitter } from 'strict-event-emitter-types';
import { EventEmitter } from 'events'; import { EventEmitter } from 'events';
import { import {
@ -13,7 +15,6 @@ import {
} from '@alilc/lowcode-types'; } from '@alilc/lowcode-types';
import { engineConfig } from './config'; import { engineConfig } from './config';
import { globalLocale } from './intl'; import { globalLocale } from './intl';
import * as utils from './utils';
import Preference from './utils/preference'; import Preference from './utils/preference';
import { obx } from './utils'; import { obx } from './utils';
import { AssetsJson, AssetLoader } from '@alilc/lowcode-utils'; import { AssetsJson, AssetLoader } from '@alilc/lowcode-utils';
@ -68,7 +69,9 @@ export class Editor extends (EventEmitter as any) implements IEditor {
private hooks: HookConfig[] = []; 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); return this.context.get(keyOrType as any);
} }
@ -113,8 +116,8 @@ export class Editor extends (EventEmitter as any) implements IEditor {
const { exportName, url } = component; const { exportName, url } = component;
await (new AssetLoader()).load(url); await (new AssetLoader()).load(url);
if (window[exportName]) { if (window[exportName]) {
assets.components = assets.components.concat(window[exportName].components || []); assets.components = assets.components.concat((window[exportName] as any).components || []);
assets.componentList = assets.componentList.concat(window[exportName].componentList || []); assets.componentList = assets.componentList?.concat((window[exportName] as any).componentList || []);
} }
return window[exportName]; return window[exportName];
}), }),
@ -215,7 +218,7 @@ export class Editor extends (EventEmitter as any) implements IEditor {
registerHooks = (hooks: HookConfig[]) => { registerHooks = (hooks: HookConfig[]) => {
this.initHooks(hooks).forEach(({ message, type, handler }) => { this.initHooks(hooks).forEach(({ message, type, handler }) => {
if (['on', 'once'].indexOf(type) !== -1) { if (['on', 'once'].indexOf(type) !== -1) {
this[type](message, handler); this[type]((message as any), handler);
} }
}); });
}; };

View File

@ -0,0 +1,181 @@
import { RequestHandlersMap } from '@alilc/lowcode-datasource-types';
import { ComponentType } from 'react';
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;
/**
* selectorsundefined
*/
customizeIgnoreSelectors?: (defaultIgnoreSelectors: string[], e: MouseEvent) => string[];
/**
* false
*/
disableDefaultSettingPanel?: boolean;
/**
* false
*/
disableDefaultSetters?: boolean;
/**
* false
*/
enableCanvasLock?: boolean;
/**
* false
*/
enableLockedNodeSetting?: boolean;
/**
* tab false
*/
stayOnTheSameSettingTab?: boolean;
/**
* item tabsfalse
*/
hideSettingsTabsWhenOnlyOneItem?: boolean;
/**
* loading
*/
loadingComponent?: ComponentType;
/**
* false
*/
supportVariableGlobally?: boolean;
/**
* simulator urlundefined
*/
simulatorUrl?: string[];
/**
* Vision-polyfill settings
*/
visionSettings?: {
// 是否禁用降级 reducer默认值false
disableCompatibleReducer?: boolean;
// 是否开启在 render 阶段开启 filter reducer默认值false
enableFilterReducerInRenderStage?: boolean;
};
/**
* react-renderer appHelper https://lowcode-engine.cn/site/docs/guide/expand/runtime/renderer#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;
}
export interface IEngineConfig {
/**
* key
* @param key
* @returns
*/
has(key: string): boolean;
/**
* key
* @param key
* @param defaultValue
* @returns
*/
get(key: string, defaultValue?: any): any;
/**
* key
* @param key
* @param value
*/
set(key: string, value: any): void;
/**
* set
* @param config
*/
setConfig(config: { [key: string]: any }): void;
/**
* key
* Promise fullfill
* @param key
* @returns
*/
onceGot(key: string): Promise<any>;
/**
* key
* @param key
* @param fn
* @returns
*/
onGot(key: string, fn: (data: any) => void): () => void;
}

View File

@ -28,5 +28,6 @@ export * from './designer';
export * from './dragon'; export * from './dragon';
export * from './shell'; export * from './shell';
export * from './shell-model-factory'; export * from './shell-model-factory';
export * from './engine-config';
// TODO: remove this in future versions // TODO: remove this in future versions
export * from './deprecated'; export * from './deprecated';