Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update setter types #1934

Merged
merged 1 commit into from
Apr 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions packages/editor-core/src/di/setter.ts
Original file line number Diff line number Diff line change
@@ -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;
}>();
Expand Down Expand Up @@ -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;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -40,7 +39,7 @@ class SettingFieldView extends Component<SettingFieldViewProps, SettingFieldView

stageName: string | undefined;

setters?: Setters;
setters?: IPublicApiSetters;

constructor(props: SettingFieldViewProps) {
super(props);
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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,
Expand Down
23 changes: 14 additions & 9 deletions packages/shell/src/api/setters.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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;
}

Expand Down Expand Up @@ -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);
};
Expand Down
8 changes: 8 additions & 0 deletions packages/types/src/shell/api/setters.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { ReactNode } from 'react';

import { IPublicTypeRegisteredSetter, IPublicTypeCustomView } from '../type';

export interface IPublicApiSetters {

/**
* 获取指定 setter
* get setter by type
Expand Down Expand Up @@ -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;
}