feat: update setter types

This commit is contained in:
liujuping 2023-04-21 16:29:25 +08:00 committed by 林熠
parent 4a18f71ebc
commit aab8a3a10e
4 changed files with 36 additions and 19 deletions

View File

@ -1,8 +1,7 @@
import { ReactNode } from 'react';
import { IPublicTypeCustomView, IPublicTypeRegisteredSetter } from '@alilc/lowcode-types';
import { IPublicApiSetters, IPublicTypeCustomView, IPublicTypeRegisteredSetter } from '@alilc/lowcode-types';
import { createContent, isCustomView } from '@alilc/lowcode-utils';
const settersMap = new Map<string, IPublicTypeRegisteredSetter & {
type: string;
}>();
@ -44,13 +43,17 @@ function getInitialFromSetter(setter: any) {
) || null; // eslint-disable-line
}
export class Setters {
constructor(readonly viewName: string = 'global') {}
export interface ISetters extends IPublicApiSetters {
}
export class Setters implements ISetters {
settersMap = new Map<string, IPublicTypeRegisteredSetter & {
type: string;
}>();
constructor(readonly viewName: string = 'global') {}
getSetter = (type: string): IPublicTypeRegisteredSetter | null => {
return this.settersMap.get(type) || null;
};

View File

@ -1,14 +1,13 @@
import { Component, MouseEvent, Fragment } from 'react';
import { Component, MouseEvent, Fragment, ReactNode } from 'react';
import { shallowIntl, observer, obx, engineConfig, runInAction } from '@alilc/lowcode-editor-core';
import { createContent, isJSSlot, isSetterConfig } from '@alilc/lowcode-utils';
import { Skeleton, Stage } from '@alilc/lowcode-editor-skeleton';
import { IPublicTypeCustomView } from '@alilc/lowcode-types';
import { IPublicApiSetters, IPublicTypeCustomView, IPublicTypeDynamicProps } from '@alilc/lowcode-types';
import { ISettingEntry, IComponentMeta, ISettingField, isSettingField, ISettingTopEntry } from '@alilc/lowcode-designer';
import { createField } from '../field';
import PopupService, { PopupPipe } from '../popup';
import { SkeletonContext } from '../../context';
import { intl } from '../../locale';
import { Setters } from '@alilc/lowcode-shell';
function isStandardComponent(componentMeta: IComponentMeta | null) {
if (!componentMeta) return false;
@ -40,7 +39,7 @@ class SettingFieldView extends Component<SettingFieldViewProps, SettingFieldView
stageName: string | undefined;
setters?: Setters;
setters?: IPublicApiSetters;
constructor(props: SettingFieldViewProps) {
super(props);
@ -112,7 +111,9 @@ class SettingFieldView extends Component<SettingFieldViewProps, SettingFieldView
const { defaultValue } = extraProps;
const { setter } = this.field;
let setterProps: any = {};
let setterProps: {
setters?: (ReactNode | string)[];
} & Record<string, unknown> | IPublicTypeDynamicProps = {};
let setterType: any;
let initialValue: any = null;
@ -236,7 +237,7 @@ class SettingFieldView extends Component<SettingFieldViewProps, SettingFieldView
...extraProps,
},
!stageName &&
this.setters.createSetterContent(setterType, {
this.setters?.createSetterContent(setterType, {
...shallowIntl(setterProps),
forceInline: extraProps.forceInline,
key: field.id,

View File

@ -1,5 +1,5 @@
import { IPublicTypeCustomView, IPublicApiSetters, IPublicTypeRegisteredSetter } from '@alilc/lowcode-types';
import { Setters as InnerSetters, globalContext } from '@alilc/lowcode-editor-core';
import { ISetters, globalContext, untracked } from '@alilc/lowcode-editor-core';
import { ReactNode } from 'react';
import { getLogger } from '@alilc/lowcode-utils';
@ -9,26 +9,28 @@ const settersSymbol = Symbol('setters');
const logger = getLogger({ level: 'warn', bizName: 'shell-setters' });
export class Setters implements IPublicApiSetters {
readonly [innerSettersSymbol]: InnerSetters;
readonly [innerSettersSymbol]: ISetters;
get [settersSymbol](): InnerSetters {
get [settersSymbol](): ISetters {
if (this.workspaceMode) {
return this[innerSettersSymbol];
}
const workspace = globalContext.get('workspace');
if (workspace.isActive) {
if (!workspace.window.innerSetters) {
logger.error('setter api 调用时机出现问题,请检查');
return this[innerSettersSymbol];
}
return workspace.window.innerSetters;
return untracked(() => {
if (!workspace.window.innerSetters) {
logger.error('setter api 调用时机出现问题,请检查');
return this[innerSettersSymbol];
}
return workspace.window.innerSetters;
});
}
return this[innerSettersSymbol];
}
constructor(innerSetters: InnerSetters, readonly workspaceMode = false) {
constructor(innerSetters: ISetters, readonly workspaceMode = false) {
this[innerSettersSymbol] = innerSetters;
}
@ -64,6 +66,9 @@ export class Setters implements IPublicApiSetters {
return this[settersSymbol].registerSetter(typeOrMaps, setter);
};
/**
* @deprecated
*/
createSetterContent = (setter: any, props: Record<string, any>): ReactNode => {
return this[settersSymbol].createSetterContent(setter, props);
};

View File

@ -1,6 +1,9 @@
import { ReactNode } from 'react';
import { IPublicTypeRegisteredSetter, IPublicTypeCustomView } from '../type';
export interface IPublicApiSetters {
/**
* setter
* get setter by type
@ -29,4 +32,9 @@ export interface IPublicApiSetters {
typeOrMaps: string | { [key: string]: IPublicTypeCustomView | IPublicTypeRegisteredSetter },
setter?: IPublicTypeCustomView | IPublicTypeRegisteredSetter | undefined
): void;
/**
* @deprecated
*/
createSetterContent (setter: any, props: Record<string, any>): ReactNode;
}