feat: add setConfig method for project

This commit is contained in:
knight.chen 2023-03-25 13:16:39 +08:00 committed by 刘菊萍(絮黎)
parent 298ab0f62f
commit 3d51fe00bf
6 changed files with 81 additions and 48 deletions

View File

@ -252,6 +252,35 @@ setI18n(value: object): void;
**@since v1.0.17**
### setConfig
设置当前项目配置
```typescript
/**
* 设置当前项目配置
* set config for this project
* @param value object
* @since v1.1.4
*/
setConfig(value: IPublicTypeAppConfig): void;
setConfig<T extends keyof IPublicTypeAppConfig>(key: T, value: IPublicTypeAppConfig[T]): void;
```
**@since v1.1.4**
#### 如何扩展项目配置
```typescript
// shims.d.ts
declare module '@alilc/lowcode-types' {
export interface IPublicTypeAppConfig {
customProp: CustomPropType
}
}
export {};
```
## 事件

View File

@ -29,6 +29,7 @@ export interface IProject extends Omit<IBaseApiProject<
'onSimulatorHostReady' |
'onSimulatorRendererReady' |
'setI18n' |
'setConfig' |
'currentDocument' |
'selection' |
'documents' |
@ -75,21 +76,15 @@ export interface IProject extends Omit<IBaseApiProject<
/**
*
*/
set(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
key:
| 'version'
| 'componentsTree'
| 'componentsMap'
| 'utils'
| 'constants'
| 'i18n'
| 'css'
| 'dataSource'
| string,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
value: any,
): void;
set<T extends keyof IPublicTypeProjectSchema>(key: T, value: IPublicTypeProjectSchema[T]): void;
set(key: string, value: unknown): void;
/**
*
*/
get<T extends keyof IPublicTypeProjectSchema>(key: T): IPublicTypeProjectSchema[T];
get<T>(key: string): T;
get(key: string): unknown;
checkExclusive(activeDoc: DocumentModel): void;
}
@ -268,21 +263,9 @@ export class Project implements IProject {
/**
*
*/
set(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
key:
| 'version'
| 'componentsTree'
| 'componentsMap'
| 'utils'
| 'constants'
| 'i18n'
| 'css'
| 'dataSource'
| string,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
value: any,
): void {
set<T extends keyof IPublicTypeProjectSchema>(key: T, value: IPublicTypeProjectSchema[T]): void;
set(key: string, value: unknown): void;
set(key: string, value: unknown): void {
if (key === 'config') {
this.config = value;
}
@ -295,20 +278,10 @@ export class Project implements IProject {
/**
*
*/
get(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
key:
| 'version'
| 'componentsTree'
| 'componentsMap'
| 'utils'
| 'constants'
| 'i18n'
| 'css'
| 'dataSource'
| 'config'
| string,
): any {
get<T extends keyof IPublicTypeRootSchema>(key: T): IPublicTypeRootSchema[T];
get<T>(key: string): T;
get(key: string): unknown;
get(key: string): any {
if (key === 'config') {
return this.config;
}

View File

@ -13,6 +13,7 @@ import {
IPublicTypePropsTransducer,
IPublicEnumTransformStage,
IPublicTypeDisposable,
IPublicTypeAppConfig,
} from '@alilc/lowcode-types';
import { DocumentModel as ShellDocumentModel } from '../model';
import { SimulatorHost } from './simulator-host';
@ -216,4 +217,23 @@ export class Project implements IPublicApiProject {
setI18n(value: object): void {
this[projectSymbol].set('i18n', value);
}
/**
*
* @param value object
* @returns
*/
setConfig<T extends keyof IPublicTypeAppConfig>(key: T, value: IPublicTypeAppConfig[T]): void;
setConfig(value: IPublicTypeAppConfig): void;
setConfig(...params: any[]): void{
if(params.length === 2) {
const oldConfig = this[projectSymbol].get('config');
this[projectSymbol].set('config', {
...oldConfig,
[params[0]]: params[1],
})
} else {
this[projectSymbol].set('config', params[0])
}
}
}

View File

@ -1,4 +1,4 @@
import { IPublicTypeProjectSchema, IPublicTypeDisposable, IPublicTypeRootSchema, IPublicTypePropsTransducer } from '../type';
import { IPublicTypeProjectSchema, IPublicTypeDisposable, IPublicTypeRootSchema, IPublicTypePropsTransducer, IPublicTypeAppConfig } from '../type';
import { IPublicEnumTransformStage } from '../enum';
import { IPublicApiSimulatorHost } from './';
import { IPublicModelDocumentModel } from '../model';
@ -132,6 +132,16 @@ export interface IBaseApiProject<
* @since v1.0.17
*/
setI18n(value: object): void;
/**
*
*
* set config data for this project
* @param value object
* @since v1.1.4
*/
setConfig<T extends keyof IPublicTypeAppConfig>(key: T, value: IPublicTypeAppConfig[T]): void;
setConfig(value: IPublicTypeAppConfig): void;
}
export interface IPublicApiProject extends IBaseApiProject<IPublicModelDocumentModel> {}
export interface IPublicApiProject extends IBaseApiProject<IPublicModelDocumentModel> {}

View File

@ -4,7 +4,6 @@ export interface IPublicTypeAppConfig {
targetRootID?: string;
layout?: IPublicTypeLayout;
theme?: IPublicTypeTheme;
[key: string]: any;
}
interface IPublicTypeTheme {

View File

@ -57,8 +57,10 @@ export interface IPublicTypeProjectSchema<T = IPublicTypeRootSchema> {
dataSource?: DataSource;
/**
*
*
* TODO: 需要在后续版本中移除 `Record<string, unknown>`
*/
config?: IPublicTypeAppConfig | Record<string, any>;
config?: IPublicTypeAppConfig & Record<string, unknown>;
/**
*
*/