Skip to content

Commit b319286

Browse files
JackLianliujuping
authored andcommitted
feat: add shell config model
1 parent 7c16bb1 commit b319286

File tree

8 files changed

+168
-13
lines changed

8 files changed

+168
-13
lines changed

docs/docs/api/model/config.md

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
---
2+
title: Config
3+
sidebar_position: 16
4+
---
5+
> **@types** [IPublicModelEngineConfig](https://github.com/alibaba/lowcode-engine/blob/main/packages/types/src/shell/model/engine-config.ts)<br/>
6+
> **@since** v1.1.3
7+
8+
## 方法
9+
### has
10+
11+
判断指定 key 是否有值
12+
13+
```typescript
14+
/**
15+
* 判断指定 key 是否有值
16+
* check if config has certain key configed
17+
* @param key
18+
* @returns
19+
*/
20+
has(key: string): boolean;
21+
```
22+
23+
### get
24+
25+
获取指定 key 的值
26+
27+
```typescript
28+
/**
29+
* 获取指定 key 的值
30+
* get value by key
31+
* @param key
32+
* @param defaultValue
33+
* @returns
34+
*/
35+
get(key: string, defaultValue?: any): any;
36+
```
37+
38+
### set
39+
40+
设置指定 key 的值
41+
42+
```typescript
43+
/**
44+
* 设置指定 key 的值
45+
* set value for certain key
46+
* @param key
47+
* @param value
48+
*/
49+
set(key: string, value: any): void;
50+
```
51+
52+
### setConfig
53+
批量设值,set 的对象版本
54+
55+
```typescript
56+
/**
57+
* 批量设值,set 的对象版本
58+
* set multiple config key-values
59+
* @param config
60+
*/
61+
setConfig(config: { [key: string]: any }): void;
62+
```
63+
64+
### getPreference
65+
获取全局 Preference, 用于管理全局浏览器侧用户 Preference,如 Panel 是否钉住
66+
67+
```typescript
68+
/**
69+
* 获取全局 Preference, 用于管理全局浏览器侧用户 Preference,如 Panel 是否钉住
70+
* get global user preference manager, which can be use to store
71+
* user`s preference in user localstorage, such as a panel is pinned or not.
72+
* @returns {IPublicModelPreference}
73+
* @since v1.1.0
74+
*/
75+
getPreference(): IPublicModelPreference;
76+
```
77+
78+
相关类型:[IPublicModelPreference](https://github.com/alibaba/lowcode-engine/blob/main/packages/types/src/shell/model/preference.ts)
79+
80+
## 事件
81+
82+
### onGot
83+
获取指定 key 的值,函数回调模式,若多次被赋值,回调会被多次调用
84+
85+
```typescript
86+
/**
87+
* 获取指定 key 的值,函数回调模式,若多次被赋值,回调会被多次调用
88+
* set callback for event of value set for some key
89+
* this will be called each time the value is set
90+
* @param key
91+
* @param fn
92+
* @returns
93+
*/
94+
onGot(key: string, fn: (data: any) => void): IPublicTypeDisposable;
95+
```
96+
97+
相关类型:[IPublicTypeDisposable](https://github.com/alibaba/lowcode-engine/blob/main/packages/types/src/shell/type/disposable.ts)
98+
99+
### onceGot
100+
获取指定 key 的值,若此时还未赋值,则等待,若已有值,则直接返回值
101+
> 注:此函数返回 Promise 实例,只会执行(fullfill)一次
102+
103+
```typescript
104+
/**
105+
* 获取指定 key 的值,若此时还未赋值,则等待,若已有值,则直接返回值
106+
* 注:此函数返回 Promise 实例,只会执行(fullfill)一次
107+
* wait until value of certain key is set, will only be
108+
* triggered once.
109+
* @param key
110+
* @returns
111+
*/
112+
onceGot(key: string): Promise<any>;
113+
```

packages/editor-core/src/config.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,7 @@ const VALID_ENGINE_OPTIONS = {
124124
type: 'array',
125125
description: '自定义 simulatorUrl 的地址',
126126
},
127-
/**
128-
* 与 react-renderer 的 appHelper 一致,https://lowcode-engine.cn/site/docs/guide/expand/runtime/renderer#apphelper
129-
*/
127+
// 与 react-renderer 的 appHelper 一致,https://lowcode-engine.cn/site/docs/guide/expand/runtime/renderer#apphelper
130128
appHelper: {
131129
type: 'object',
132130
description: '定义 utils 和 constants 等对象',
@@ -149,7 +147,6 @@ const VALID_ENGINE_OPTIONS = {
149147
},
150148
};
151149

152-
153150
const getStrictModeValue = (engineOptions: IPublicTypeEngineOptions, defaultValue: boolean): boolean => {
154151
if (!engineOptions || !isPlainObject(engineOptions)) {
155152
return defaultValue;
@@ -161,7 +158,8 @@ const getStrictModeValue = (engineOptions: IPublicTypeEngineOptions, defaultValu
161158
return engineOptions.enableStrictPluginMode;
162159
};
163160

164-
export interface IEngineConfigPrivate {
161+
export interface IEngineConfig extends IPublicModelEngineConfig {
162+
165163
/**
166164
* if engineOptions.strictPluginMode === true, only accept propertied predefined in EngineOptions.
167165
*
@@ -176,8 +174,7 @@ export interface IEngineConfigPrivate {
176174
delWait(key: string, fn: any): void;
177175
}
178176

179-
180-
export class EngineConfig implements IPublicModelEngineConfig, IEngineConfigPrivate {
177+
export class EngineConfig implements IEngineConfig {
181178
private config: { [key: string]: any } = {};
182179

183180
private waits = new Map<
@@ -350,4 +347,4 @@ export class EngineConfig implements IPublicModelEngineConfig, IEngineConfigPriv
350347
}
351348
}
352349

353-
export const engineConfig = new EngineConfig();
350+
export const engineConfig = new EngineConfig();

packages/engine/src/engine-core.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import {
4343
Logger,
4444
Canvas,
4545
Workspace,
46+
Config,
4647
} from '@alilc/lowcode-shell';
4748
import { isPlainObject } from '@alilc/lowcode-utils';
4849
import './modules/live-editing';
@@ -96,7 +97,7 @@ editor.set('project', project);
9697
editor.set('setters' as any, setters);
9798
editor.set('material', material);
9899
editor.set('innerHotkey', innerHotkey);
99-
const config = engineConfig;
100+
const config = new Config(engineConfig);
100101
const event = new Event(commonEvent, { prefix: 'common' });
101102
const logger = new Logger({ level: 'warn', bizName: 'common' });
102103
const common = new Common(editor, innerSkeleton);

packages/shell/src/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
SettingPropEntry,
1111
SettingTopEntry,
1212
Clipboard,
13+
Config,
1314
} from './model';
1415
import {
1516
Project,
@@ -61,4 +62,5 @@ export {
6162
Workspace,
6263
Clipboard,
6364
SimulatorHost,
64-
};
65+
Config,
66+
};

packages/shell/src/model/config.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { IPublicModelEngineConfig, IPublicModelPreference, IPublicTypeDisposable } from '@alilc/lowcode-types';
2+
import { configSymbol } from '../symbols';
3+
import { IEngineConfig } from '@alilc/lowcode-editor-core';
4+
5+
export class Config implements IPublicModelEngineConfig {
6+
private readonly [configSymbol]: IEngineConfig;
7+
8+
constructor(innerEngineConfig: IEngineConfig) {
9+
this[configSymbol] = innerEngineConfig;
10+
}
11+
12+
has(key: string): boolean {
13+
return this[configSymbol].has(key);
14+
}
15+
16+
get(key: string, defaultValue?: any): any {
17+
return this[configSymbol].get(key, defaultValue);
18+
}
19+
20+
set(key: string, value: any): void {
21+
this[configSymbol].set(key, value);
22+
}
23+
24+
setConfig(config: { [key: string]: any }): void {
25+
this[configSymbol].setConfig(config);
26+
}
27+
28+
onceGot(key: string): Promise<any> {
29+
return this[configSymbol].onceGot(key);
30+
}
31+
32+
onGot(key: string, fn: (data: any) => void): IPublicTypeDisposable {
33+
return this[configSymbol].onGot(key, fn);
34+
}
35+
36+
getPreference(): IPublicModelPreference {
37+
return this[configSymbol].getPreference();
38+
}
39+
}

packages/shell/src/model/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ export * from './resource';
1818
export * from './active-tracker';
1919
export * from './plugin-instance';
2020
export * from './window';
21-
export * from './clipboard';
21+
export * from './clipboard';
22+
export * from './config';

packages/shell/src/symbols.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,5 @@ export const windowSymbol = Symbol('window');
3131
export const pluginInstanceSymbol = Symbol('plugin-instance');
3232
export const resourceTypeSymbol = Symbol('resourceType');
3333
export const resourceSymbol = Symbol('resource');
34-
export const clipboardSymbol = Symbol('clipboard');
34+
export const clipboardSymbol = Symbol('clipboard');
35+
export const configSymbol = Symbol('configSymbol');

packages/types/src/shell/model/engine-config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { IPublicTypeDisposable } from '../type';
12
import { IPublicModelPreference } from './';
23

34
export interface IPublicModelEngineConfig {
@@ -52,7 +53,7 @@ export interface IPublicModelEngineConfig {
5253
* @param fn
5354
* @returns
5455
*/
55-
onGot(key: string, fn: (data: any) => void): () => void;
56+
onGot(key: string, fn: (data: any) => void): IPublicTypeDisposable;
5657

5758
/**
5859
* 获取全局 Preference, 用于管理全局浏览器侧用户 Preference,如 Panel 是否钉住

0 commit comments

Comments
 (0)