refactor(plugin-workflow): refactor nodes variable option api (#2192)

This commit is contained in:
Junyi 2023-07-06 11:27:34 +07:00 committed by GitHub
parent 6e0471bd46
commit dc91d44ce6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 69 additions and 39 deletions

View File

@ -30,7 +30,6 @@ function AssociatedConfig({ value, onChange, ...props }): JSX.Element {
const { setValuesIn } = useForm(); const { setValuesIn } = useForm();
const compile = useCompile(); const compile = useCompile();
const { getCollection } = useCollectionManager(); const { getCollection } = useCollectionManager();
const current = useNodeContext();
const options = [nodesOptions, triggerOptions].map((item) => { const options = [nodesOptions, triggerOptions].map((item) => {
const children = item.useOptions({ types: [matchToManyField] })?.filter(Boolean); const children = item.useOptions({ types: [matchToManyField] })?.filter(Boolean);
return { return {
@ -300,16 +299,17 @@ export default {
ValueBlock, ValueBlock,
AssociatedConfig, AssociatedConfig,
}, },
useVariables(current, { types }) { useVariables({ id, title }, { types }) {
if ( if (
types && types &&
!types.some((type) => type in BaseTypeSets || Object.values(BaseTypeSets).some((set) => set.has(type))) !types.some((type) => type in BaseTypeSets || Object.values(BaseTypeSets).some((set) => set.has(type)))
) { ) {
return null; return null;
} }
return [ return {
// { key: '', value: '', label: lang('Calculation result') } value: `${id}`,
]; label: title,
};
}, },
useInitializers(node): SchemaInitializerItemOptions | null { useInitializers(node): SchemaInitializerItemOptions | null {
if (!node.config.collection) { if (!node.config.collection) {

View File

@ -18,15 +18,19 @@ function useDynamicExpressionCollectionFieldMatcher(field): boolean {
return false; return false;
} }
if (this.getCollection(field.collectionName)?.template === 'expression') {
return true;
}
const fields = this.getCollectionFields(field.target); const fields = this.getCollectionFields(field.target);
return fields.some((f) => f.interface === 'expression'); return fields.some((f) => f.interface === 'expression');
} }
const DynamicConfig = ({ value, onChange }) => { const DynamicConfig = ({ value, onChange }) => {
const { t } = useTranslation(); const { t } = useTranslation();
const { getCollectionFields } = useCollectionManager(); const { getCollectionFields, getCollection } = useCollectionManager();
const scope = useWorkflowVariableOptions({ const scope = useWorkflowVariableOptions({
types: [useDynamicExpressionCollectionFieldMatcher.bind({ getCollectionFields })], types: [useDynamicExpressionCollectionFieldMatcher.bind({ getCollectionFields, getCollection })],
}); });
return ( return (
@ -189,7 +193,7 @@ export default {
RadioWithTooltip, RadioWithTooltip,
DynamicConfig, DynamicConfig,
}, },
useVariables(current, options) { useVariables({ id, title }, options) {
const { types } = options ?? {}; const { types } = options ?? {};
if ( if (
types && types &&
@ -197,9 +201,10 @@ export default {
) { ) {
return null; return null;
} }
return [ return {
// { key: '', value: '', label: lang('Calculation result') } value: id,
]; label: title,
};
}, },
useInitializers(node): SchemaInitializerItemOptions { useInitializers(node): SchemaInitializerItemOptions {
return { return {

View File

@ -40,22 +40,34 @@ export default {
CollectionFieldset, CollectionFieldset,
FieldsSelect, FieldsSelect,
}, },
useVariables({ config }, options) { useVariables({ id, title, config }, options) {
const compile = useCompile(); const compile = useCompile();
const { getCollectionFields } = useCollectionManager(); const { getCollectionFields } = useCollectionManager();
// const depth = config?.params?.appends?.length // const depth = config?.params?.appends?.length
// ? config?.params?.appends.reduce((max, item) => Math.max(max, item.split('.').length), 1) // ? config?.params?.appends.reduce((max, item) => Math.max(max, item.split('.').length), 1)
// : 0; // : 0;
const result = getCollectionFieldOptions({ const name = `${id}`;
collection: config.collection, const [result] = getCollectionFieldOptions({
// collection: config.collection,
...options, ...options,
fields: [
{
collectionName: config.collection,
name,
type: 'hasOne',
target: config.collection,
uiSchema: {
title,
},
},
],
// depth: options?.depth ?? depth, // depth: options?.depth ?? depth,
appends: config.params?.appends, appends: [name, ...(config.params?.appends?.map((item) => `${name}.${item}`) || [])],
compile, compile,
getCollectionFields, getCollectionFields,
}); });
return result?.length ? result : null; return result;
}, },
useInitializers(node): SchemaInitializerItemOptions | null { useInitializers(node): SchemaInitializerItemOptions | null {
if (!node.config.collection) { if (!node.config.collection) {

View File

@ -38,7 +38,7 @@ import aggregate from './aggregate';
import { JobStatusOptionsMap } from '../constants'; import { JobStatusOptionsMap } from '../constants';
import { NAMESPACE, lang } from '../locale'; import { NAMESPACE, lang } from '../locale';
import request from './request'; import request from './request';
import { VariableOptions } from '../variable'; import { VariableOption, VariableOptions } from '../variable';
import { NodeDescription } from '../components/NodeDescription'; import { NodeDescription } from '../components/NodeDescription';
export interface Instruction { export interface Instruction {
@ -53,7 +53,7 @@ export interface Instruction {
components?: { [key: string]: any }; components?: { [key: string]: any };
component?(props): JSX.Element; component?(props): JSX.Element;
endding?: boolean; endding?: boolean;
useVariables?(node, options?): VariableOptions; useVariables?(node, options?): VariableOption;
useScopeVariables?(node, options?): VariableOptions; useScopeVariables?(node, options?): VariableOptions;
useInitializers?(node): SchemaInitializerItemOptions | null; useInitializers?(node): SchemaInitializerItemOptions | null;
initializers?: { [key: string]: any }; initializers?: { [key: string]: any };

View File

@ -85,7 +85,7 @@ export default {
ModeConfig, ModeConfig,
AssigneesSelect, AssigneesSelect,
}, },
useVariables({ config }, { types }) { useVariables({ id, title, config }, { types }) {
const compile = useCompile(); const compile = useCompile();
const { getCollectionFields } = useCollectionManager(); const { getCollectionFields } = useCollectionManager();
const formKeys = Object.keys(config.forms ?? {}); const formKeys = Object.keys(config.forms ?? {});
@ -97,27 +97,33 @@ export default {
.map((formKey) => { .map((formKey) => {
const form = config.forms[formKey]; const form = config.forms[formKey];
// eslint-disable-next-line react-hooks/rules-of-hooks const fieldsOptions = getCollectionFieldOptions({
const options = getCollectionFieldOptions({
fields: form.collection?.fields, fields: form.collection?.fields,
collection: form.collection, collection: form.collection,
types, types,
compile, compile,
getCollectionFields, getCollectionFields,
}); });
return options.length const label = compile(form.title) || formKey;
return fieldsOptions.length
? { ? {
key: formKey, key: formKey,
value: formKey, value: formKey,
label: form.title || formKey, label,
title: form.title || formKey, title: label,
children: options, children: fieldsOptions,
} }
: null; : null;
}) })
.filter(Boolean); .filter(Boolean);
return options.length ? options : null; return options.length
? {
value: `${id}`,
label: title,
children: options,
}
: null;
}, },
useInitializers(node): SchemaInitializerItemOptions | null { useInitializers(node): SchemaInitializerItemOptions | null {
const { getCollection } = useCollectionManager(); const { getCollection } = useCollectionManager();

View File

@ -43,22 +43,34 @@ export default {
FilterDynamicComponent, FilterDynamicComponent,
FieldsSelect, FieldsSelect,
}, },
useVariables({ config }, options) { useVariables({ id, title, config }, options) {
const compile = useCompile(); const compile = useCompile();
const { getCollectionFields } = useCollectionManager(); const { getCollectionFields } = useCollectionManager();
// const depth = config?.params?.appends?.length // const depth = config?.params?.appends?.length
// ? config?.params?.appends.reduce((max, item) => Math.max(max, item.split('.').length), 1) // ? config?.params?.appends.reduce((max, item) => Math.max(max, item.split('.').length), 1)
// : 0; // : 0;
const result = getCollectionFieldOptions({ const name = `${id}`;
collection: config.collection, const [result] = getCollectionFieldOptions({
// collection: config.collection,
...options, ...options,
fields: [
{
collectionName: config.collection,
name,
type: 'hasOne',
target: config.collection,
uiSchema: {
title,
},
},
],
// depth: options?.depth ?? depth, // depth: options?.depth ?? depth,
appends: config.params?.appends, appends: [name, ...(config.params?.appends?.map((item) => `${name}.${item}`) || [])],
compile, compile,
getCollectionFields, getCollectionFields,
}); });
return result?.length ? result : null; return result;
}, },
useInitializers(node): SchemaInitializerItemOptions | null { useInitializers(node): SchemaInitializerItemOptions | null {
if (!node.config.collection || node.config.multiple) { if (!node.config.collection || node.config.multiple) {

View File

@ -22,14 +22,9 @@ export const nodesOptions = {
const result: VariableOption[] = []; const result: VariableOption[] = [];
upstreams.forEach((node) => { upstreams.forEach((node) => {
const instruction = instructions.get(node.type); const instruction = instructions.get(node.type);
const subOptions = instruction.useVariables?.(node, options); const subOption = instruction.useVariables?.(node, options);
if (subOptions) { if (subOption) {
result.push({ result.push(subOption);
key: node.id.toString(),
value: node.id.toString(),
label: node.title ?? `#${node.id}`,
children: subOptions,
});
} }
}); });
return result; return result;