Skip to content

Commit 3d51fe0

Browse files
knight.chenliujuping
authored andcommitted
feat: add setConfig method for project
1 parent 298ab0f commit 3d51fe0

File tree

6 files changed

+81
-48
lines changed

6 files changed

+81
-48
lines changed

docs/docs/api/project.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,35 @@ setI18n(value: object): void;
252252

253253
**@since v1.0.17**
254254

255+
### setConfig
256+
设置当前项目配置
257+
258+
```typescript
259+
/**
260+
* 设置当前项目配置
261+
* set config for this project
262+
* @param value object
263+
* @since v1.1.4
264+
*/
265+
setConfig(value: IPublicTypeAppConfig): void;
266+
setConfig<T extends keyof IPublicTypeAppConfig>(key: T, value: IPublicTypeAppConfig[T]): void;
267+
```
268+
269+
**@since v1.1.4**
270+
271+
#### 如何扩展项目配置
272+
273+
```typescript
274+
// shims.d.ts
275+
declare module '@alilc/lowcode-types' {
276+
export interface IPublicTypeAppConfig {
277+
customProp: CustomPropType
278+
}
279+
}
280+
281+
export {};
282+
```
283+
255284

256285
## 事件
257286

packages/designer/src/project/project.ts

Lines changed: 17 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export interface IProject extends Omit<IBaseApiProject<
2929
'onSimulatorHostReady' |
3030
'onSimulatorRendererReady' |
3131
'setI18n' |
32+
'setConfig' |
3233
'currentDocument' |
3334
'selection' |
3435
'documents' |
@@ -75,21 +76,15 @@ export interface IProject extends Omit<IBaseApiProject<
7576
/**
7677
* 分字段设置储存数据,不记录操作记录
7778
*/
78-
set(
79-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
80-
key:
81-
| 'version'
82-
| 'componentsTree'
83-
| 'componentsMap'
84-
| 'utils'
85-
| 'constants'
86-
| 'i18n'
87-
| 'css'
88-
| 'dataSource'
89-
| string,
90-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
91-
value: any,
92-
): void;
79+
set<T extends keyof IPublicTypeProjectSchema>(key: T, value: IPublicTypeProjectSchema[T]): void;
80+
set(key: string, value: unknown): void;
81+
82+
/**
83+
* 分字段获取储存数据
84+
*/
85+
get<T extends keyof IPublicTypeProjectSchema>(key: T): IPublicTypeProjectSchema[T];
86+
get<T>(key: string): T;
87+
get(key: string): unknown;
9388

9489
checkExclusive(activeDoc: DocumentModel): void;
9590
}
@@ -268,21 +263,9 @@ export class Project implements IProject {
268263
/**
269264
* 分字段设置储存数据,不记录操作记录
270265
*/
271-
set(
272-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
273-
key:
274-
| 'version'
275-
| 'componentsTree'
276-
| 'componentsMap'
277-
| 'utils'
278-
| 'constants'
279-
| 'i18n'
280-
| 'css'
281-
| 'dataSource'
282-
| string,
283-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
284-
value: any,
285-
): void {
266+
set<T extends keyof IPublicTypeProjectSchema>(key: T, value: IPublicTypeProjectSchema[T]): void;
267+
set(key: string, value: unknown): void;
268+
set(key: string, value: unknown): void {
286269
if (key === 'config') {
287270
this.config = value;
288271
}
@@ -295,20 +278,10 @@ export class Project implements IProject {
295278
/**
296279
* 分字段设置储存数据
297280
*/
298-
get(
299-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
300-
key:
301-
| 'version'
302-
| 'componentsTree'
303-
| 'componentsMap'
304-
| 'utils'
305-
| 'constants'
306-
| 'i18n'
307-
| 'css'
308-
| 'dataSource'
309-
| 'config'
310-
| string,
311-
): any {
281+
get<T extends keyof IPublicTypeRootSchema>(key: T): IPublicTypeRootSchema[T];
282+
get<T>(key: string): T;
283+
get(key: string): unknown;
284+
get(key: string): any {
312285
if (key === 'config') {
313286
return this.config;
314287
}

packages/shell/src/api/project.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
IPublicTypePropsTransducer,
1414
IPublicEnumTransformStage,
1515
IPublicTypeDisposable,
16+
IPublicTypeAppConfig,
1617
} from '@alilc/lowcode-types';
1718
import { DocumentModel as ShellDocumentModel } from '../model';
1819
import { SimulatorHost } from './simulator-host';
@@ -216,4 +217,23 @@ export class Project implements IPublicApiProject {
216217
setI18n(value: object): void {
217218
this[projectSymbol].set('i18n', value);
218219
}
220+
221+
/**
222+
* 设置项目配置
223+
* @param value object
224+
* @returns
225+
*/
226+
setConfig<T extends keyof IPublicTypeAppConfig>(key: T, value: IPublicTypeAppConfig[T]): void;
227+
setConfig(value: IPublicTypeAppConfig): void;
228+
setConfig(...params: any[]): void{
229+
if(params.length === 2) {
230+
const oldConfig = this[projectSymbol].get('config');
231+
this[projectSymbol].set('config', {
232+
...oldConfig,
233+
[params[0]]: params[1],
234+
})
235+
} else {
236+
this[projectSymbol].set('config', params[0])
237+
}
238+
}
219239
}

packages/types/src/shell/api/project.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { IPublicTypeProjectSchema, IPublicTypeDisposable, IPublicTypeRootSchema, IPublicTypePropsTransducer } from '../type';
1+
import { IPublicTypeProjectSchema, IPublicTypeDisposable, IPublicTypeRootSchema, IPublicTypePropsTransducer, IPublicTypeAppConfig } from '../type';
22
import { IPublicEnumTransformStage } from '../enum';
33
import { IPublicApiSimulatorHost } from './';
44
import { IPublicModelDocumentModel } from '../model';
@@ -132,6 +132,16 @@ export interface IBaseApiProject<
132132
* @since v1.0.17
133133
*/
134134
setI18n(value: object): void;
135+
136+
/**
137+
* 设置当前项目配置
138+
*
139+
* set config data for this project
140+
* @param value object
141+
* @since v1.1.4
142+
*/
143+
setConfig<T extends keyof IPublicTypeAppConfig>(key: T, value: IPublicTypeAppConfig[T]): void;
144+
setConfig(value: IPublicTypeAppConfig): void;
135145
}
136146

137-
export interface IPublicApiProject extends IBaseApiProject<IPublicModelDocumentModel> {}
147+
export interface IPublicApiProject extends IBaseApiProject<IPublicModelDocumentModel> {}

packages/types/src/shell/type/app-config.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ export interface IPublicTypeAppConfig {
44
targetRootID?: string;
55
layout?: IPublicTypeLayout;
66
theme?: IPublicTypeTheme;
7-
[key: string]: any;
87
}
98

109
interface IPublicTypeTheme {

packages/types/src/shell/type/project-schema.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,10 @@ export interface IPublicTypeProjectSchema<T = IPublicTypeRootSchema> {
5757
dataSource?: DataSource;
5858
/**
5959
* 当前应用配置信息
60+
*
61+
* TODO: 需要在后续版本中移除 `Record<string, unknown>` 类型签名
6062
*/
61-
config?: IPublicTypeAppConfig | Record<string, any>;
63+
config?: IPublicTypeAppConfig & Record<string, unknown>;
6264
/**
6365
* 当前应用元数据信息
6466
*/

0 commit comments

Comments
 (0)