Skip to content

Commit

Permalink
feat: add shell config model
Browse files Browse the repository at this point in the history
  • Loading branch information
JackLian authored and liujuping committed Feb 21, 2023
1 parent 7c16bb1 commit b319286
Showing 8 changed files with 168 additions and 13 deletions.
113 changes: 113 additions & 0 deletions docs/docs/api/model/config.md
Original file line number Diff line number Diff line change
@@ -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)<br/>
> **@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<any>;
```
13 changes: 5 additions & 8 deletions packages/editor-core/src/config.ts
Original file line number Diff line number Diff line change
@@ -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();
export const engineConfig = new EngineConfig();
3 changes: 2 additions & 1 deletion packages/engine/src/engine-core.ts
Original file line number Diff line number Diff line change
@@ -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);
4 changes: 3 additions & 1 deletion packages/shell/src/index.ts
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@ import {
SettingPropEntry,
SettingTopEntry,
Clipboard,
Config,
} from './model';
import {
Project,
@@ -61,4 +62,5 @@ export {
Workspace,
Clipboard,
SimulatorHost,
};
Config,
};
39 changes: 39 additions & 0 deletions packages/shell/src/model/config.ts
Original file line number Diff line number Diff line change
@@ -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<any> {
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();
}
}
3 changes: 2 additions & 1 deletion packages/shell/src/model/index.ts
Original file line number Diff line number Diff line change
@@ -18,4 +18,5 @@ export * from './resource';
export * from './active-tracker';
export * from './plugin-instance';
export * from './window';
export * from './clipboard';
export * from './clipboard';
export * from './config';
3 changes: 2 additions & 1 deletion packages/shell/src/symbols.ts
Original file line number Diff line number Diff line change
@@ -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');
export const clipboardSymbol = Symbol('clipboard');
export const configSymbol = Symbol('configSymbol');
3 changes: 2 additions & 1 deletion packages/types/src/shell/model/engine-config.ts
Original file line number Diff line number Diff line change
@@ -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 是否钉住

0 comments on commit b319286

Please sign in to comment.