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

View File

@ -13,6 +13,7 @@ import {
IPublicTypePropsTransducer, IPublicTypePropsTransducer,
IPublicEnumTransformStage, IPublicEnumTransformStage,
IPublicTypeDisposable, IPublicTypeDisposable,
IPublicTypeAppConfig,
} from '@alilc/lowcode-types'; } from '@alilc/lowcode-types';
import { DocumentModel as ShellDocumentModel } from '../model'; import { DocumentModel as ShellDocumentModel } from '../model';
import { SimulatorHost } from './simulator-host'; import { SimulatorHost } from './simulator-host';
@ -216,4 +217,23 @@ export class Project implements IPublicApiProject {
setI18n(value: object): void { setI18n(value: object): void {
this[projectSymbol].set('i18n', value); 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 { IPublicEnumTransformStage } from '../enum';
import { IPublicApiSimulatorHost } from './'; import { IPublicApiSimulatorHost } from './';
import { IPublicModelDocumentModel } from '../model'; import { IPublicModelDocumentModel } from '../model';
@ -132,6 +132,16 @@ export interface IBaseApiProject<
* @since v1.0.17 * @since v1.0.17
*/ */
setI18n(value: object): void; 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; targetRootID?: string;
layout?: IPublicTypeLayout; layout?: IPublicTypeLayout;
theme?: IPublicTypeTheme; theme?: IPublicTypeTheme;
[key: string]: any;
} }
interface IPublicTypeTheme { interface IPublicTypeTheme {

View File

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