From b319286c4896e34844e6f21a552874f0f70d0742 Mon Sep 17 00:00:00 2001 From: JackLian Date: Tue, 21 Feb 2023 15:22:52 +0800 Subject: [PATCH] feat: add shell config model --- docs/docs/api/model/config.md | 113 ++++++++++++++++++ packages/editor-core/src/config.ts | 13 +- packages/engine/src/engine-core.ts | 3 +- packages/shell/src/index.ts | 4 +- packages/shell/src/model/config.ts | 39 ++++++ packages/shell/src/model/index.ts | 3 +- packages/shell/src/symbols.ts | 3 +- .../types/src/shell/model/engine-config.ts | 3 +- 8 files changed, 168 insertions(+), 13 deletions(-) create mode 100644 docs/docs/api/model/config.md create mode 100644 packages/shell/src/model/config.ts diff --git a/docs/docs/api/model/config.md b/docs/docs/api/model/config.md new file mode 100644 index 000000000..854aa1a04 --- /dev/null +++ b/docs/docs/api/model/config.md @@ -0,0 +1,113 @@ +--- +title: Config +sidebar_position: 16 +--- +> **@types** [IPublicModelEngineConfig](https://github.com/alibaba/lowcode-engine/blob/main/packages/types/src/shell/model/engine-config.ts)
+> **@since** v1.1.3 + +## 方法 +### has + +判断指定 key 是否有值 + +```typescript +/** + * 判断指定 key 是否有值 + * check if config has certain key configed + * @param key + * @returns + */ +has(key: string): boolean; +``` + +### get + +获取指定 key 的值 + +```typescript +/** + * 获取指定 key 的值 + * get value by key + * @param key + * @param defaultValue + * @returns + */ +get(key: string, defaultValue?: any): any; +``` + +### set + +设置指定 key 的值 + +```typescript +/** + * 设置指定 key 的值 + * set value for certain key + * @param key + * @param value + */ +set(key: string, value: any): void; +``` + +### setConfig +批量设值,set 的对象版本 + +```typescript +/** + * 批量设值,set 的对象版本 + * set multiple config key-values + * @param config + */ +setConfig(config: { [key: string]: any }): void; +``` + +### getPreference +获取全局 Preference, 用于管理全局浏览器侧用户 Preference,如 Panel 是否钉住 + +```typescript +/** + * 获取全局 Preference, 用于管理全局浏览器侧用户 Preference,如 Panel 是否钉住 + * get global user preference manager, which can be use to store + * user`s preference in user localstorage, such as a panel is pinned or not. + * @returns {IPublicModelPreference} + * @since v1.1.0 + */ +getPreference(): IPublicModelPreference; +``` + +相关类型:[IPublicModelPreference](https://github.com/alibaba/lowcode-engine/blob/main/packages/types/src/shell/model/preference.ts) + +## 事件 + +### onGot +获取指定 key 的值,函数回调模式,若多次被赋值,回调会被多次调用 + +```typescript +/** + * 获取指定 key 的值,函数回调模式,若多次被赋值,回调会被多次调用 + * set callback for event of value set for some key + * this will be called each time the value is set + * @param key + * @param fn + * @returns + */ +onGot(key: string, fn: (data: any) => void): IPublicTypeDisposable; +``` + +相关类型:[IPublicTypeDisposable](https://github.com/alibaba/lowcode-engine/blob/main/packages/types/src/shell/type/disposable.ts) + +### onceGot +获取指定 key 的值,若此时还未赋值,则等待,若已有值,则直接返回值 +> 注:此函数返回 Promise 实例,只会执行(fullfill)一次 + +```typescript +/** + * 获取指定 key 的值,若此时还未赋值,则等待,若已有值,则直接返回值 + * 注:此函数返回 Promise 实例,只会执行(fullfill)一次 + * wait until value of certain key is set, will only be + * triggered once. + * @param key + * @returns + */ +onceGot(key: string): Promise; +``` diff --git a/packages/editor-core/src/config.ts b/packages/editor-core/src/config.ts index 91ef33adb..12ef865ec 100644 --- a/packages/editor-core/src/config.ts +++ b/packages/editor-core/src/config.ts @@ -124,9 +124,7 @@ const VALID_ENGINE_OPTIONS = { type: 'array', description: '自定义 simulatorUrl 的地址', }, - /** - * 与 react-renderer 的 appHelper 一致,https://lowcode-engine.cn/site/docs/guide/expand/runtime/renderer#apphelper - */ + // 与 react-renderer 的 appHelper 一致,https://lowcode-engine.cn/site/docs/guide/expand/runtime/renderer#apphelper appHelper: { type: 'object', description: '定义 utils 和 constants 等对象', @@ -149,7 +147,6 @@ const VALID_ENGINE_OPTIONS = { }, }; - const getStrictModeValue = (engineOptions: IPublicTypeEngineOptions, defaultValue: boolean): boolean => { if (!engineOptions || !isPlainObject(engineOptions)) { return defaultValue; @@ -161,7 +158,8 @@ const getStrictModeValue = (engineOptions: IPublicTypeEngineOptions, defaultValu return engineOptions.enableStrictPluginMode; }; -export interface IEngineConfigPrivate { +export interface IEngineConfig extends IPublicModelEngineConfig { + /** * if engineOptions.strictPluginMode === true, only accept propertied predefined in EngineOptions. * @@ -176,8 +174,7 @@ export interface IEngineConfigPrivate { delWait(key: string, fn: any): void; } - -export class EngineConfig implements IPublicModelEngineConfig, IEngineConfigPrivate { +export class EngineConfig implements IEngineConfig { private config: { [key: string]: any } = {}; private waits = new Map< @@ -350,4 +347,4 @@ export class EngineConfig implements IPublicModelEngineConfig, IEngineConfigPriv } } -export const engineConfig = new EngineConfig(); \ No newline at end of file +export const engineConfig = new EngineConfig(); diff --git a/packages/engine/src/engine-core.ts b/packages/engine/src/engine-core.ts index abe378d64..ea0554cad 100644 --- a/packages/engine/src/engine-core.ts +++ b/packages/engine/src/engine-core.ts @@ -43,6 +43,7 @@ import { Logger, Canvas, Workspace, + Config, } from '@alilc/lowcode-shell'; import { isPlainObject } from '@alilc/lowcode-utils'; import './modules/live-editing'; @@ -96,7 +97,7 @@ editor.set('project', project); editor.set('setters' as any, setters); editor.set('material', material); editor.set('innerHotkey', innerHotkey); -const config = engineConfig; +const config = new Config(engineConfig); const event = new Event(commonEvent, { prefix: 'common' }); const logger = new Logger({ level: 'warn', bizName: 'common' }); const common = new Common(editor, innerSkeleton); diff --git a/packages/shell/src/index.ts b/packages/shell/src/index.ts index 74d11288e..aed56fb95 100644 --- a/packages/shell/src/index.ts +++ b/packages/shell/src/index.ts @@ -10,6 +10,7 @@ import { SettingPropEntry, SettingTopEntry, Clipboard, + Config, } from './model'; import { Project, @@ -61,4 +62,5 @@ export { Workspace, Clipboard, SimulatorHost, -}; \ No newline at end of file + Config, +}; diff --git a/packages/shell/src/model/config.ts b/packages/shell/src/model/config.ts new file mode 100644 index 000000000..d84120878 --- /dev/null +++ b/packages/shell/src/model/config.ts @@ -0,0 +1,39 @@ +import { IPublicModelEngineConfig, IPublicModelPreference, IPublicTypeDisposable } from '@alilc/lowcode-types'; +import { configSymbol } from '../symbols'; +import { IEngineConfig } from '@alilc/lowcode-editor-core'; + +export class Config implements IPublicModelEngineConfig { + private readonly [configSymbol]: IEngineConfig; + + constructor(innerEngineConfig: IEngineConfig) { + this[configSymbol] = innerEngineConfig; + } + + has(key: string): boolean { + return this[configSymbol].has(key); + } + + get(key: string, defaultValue?: any): any { + return this[configSymbol].get(key, defaultValue); + } + + set(key: string, value: any): void { + this[configSymbol].set(key, value); + } + + setConfig(config: { [key: string]: any }): void { + this[configSymbol].setConfig(config); + } + + onceGot(key: string): Promise { + return this[configSymbol].onceGot(key); + } + + onGot(key: string, fn: (data: any) => void): IPublicTypeDisposable { + return this[configSymbol].onGot(key, fn); + } + + getPreference(): IPublicModelPreference { + return this[configSymbol].getPreference(); + } +} diff --git a/packages/shell/src/model/index.ts b/packages/shell/src/model/index.ts index f2805342e..8e18c36bb 100644 --- a/packages/shell/src/model/index.ts +++ b/packages/shell/src/model/index.ts @@ -18,4 +18,5 @@ export * from './resource'; export * from './active-tracker'; export * from './plugin-instance'; export * from './window'; -export * from './clipboard'; \ No newline at end of file +export * from './clipboard'; +export * from './config'; diff --git a/packages/shell/src/symbols.ts b/packages/shell/src/symbols.ts index dac981e96..91ad609ac 100644 --- a/packages/shell/src/symbols.ts +++ b/packages/shell/src/symbols.ts @@ -31,4 +31,5 @@ export const windowSymbol = Symbol('window'); export const pluginInstanceSymbol = Symbol('plugin-instance'); export const resourceTypeSymbol = Symbol('resourceType'); export const resourceSymbol = Symbol('resource'); -export const clipboardSymbol = Symbol('clipboard'); \ No newline at end of file +export const clipboardSymbol = Symbol('clipboard'); +export const configSymbol = Symbol('configSymbol'); diff --git a/packages/types/src/shell/model/engine-config.ts b/packages/types/src/shell/model/engine-config.ts index 2b17d7e72..c9473cd12 100644 --- a/packages/types/src/shell/model/engine-config.ts +++ b/packages/types/src/shell/model/engine-config.ts @@ -1,3 +1,4 @@ +import { IPublicTypeDisposable } from '../type'; import { IPublicModelPreference } from './'; export interface IPublicModelEngineConfig { @@ -52,7 +53,7 @@ export interface IPublicModelEngineConfig { * @param fn * @returns */ - onGot(key: string, fn: (data: any) => void): () => void; + onGot(key: string, fn: (data: any) => void): IPublicTypeDisposable; /** * 获取全局 Preference, 用于管理全局浏览器侧用户 Preference,如 Panel 是否钉住