From ce75ecdb2b1095cf9eaf6f82a6ee6edd0d8d2f2a Mon Sep 17 00:00:00 2001 From: Junyi Date: Wed, 7 Sep 2022 08:46:29 +0800 Subject: [PATCH] refactor(plugin-workflow): export client calculators registry (#816) --- .../workflow/src/client/calculators.tsx | 123 +++++++++++++----- .../plugins/workflow/src/client/index.tsx | 1 + 2 files changed, 92 insertions(+), 32 deletions(-) diff --git a/packages/plugins/workflow/src/client/calculators.tsx b/packages/plugins/workflow/src/client/calculators.tsx index 6b1f5b323..d6f73273e 100644 --- a/packages/plugins/workflow/src/client/calculators.tsx +++ b/packages/plugins/workflow/src/client/calculators.tsx @@ -10,54 +10,113 @@ import { instructions, useNodeContext } from "./nodes"; import { useFlowContext } from "./WorkflowCanvas"; import { triggers } from "./triggers"; import { useTranslation } from "react-i18next"; +import { Registry } from "@nocobase/utils/client"; function NullRender() { return null; } -export const calculators = [ +interface Calculator { + name: string; + group: string; +} + +export const calculators = new Registry(); + +calculators.register('equal', { + name: '=', + group: 'boolean', +}); +calculators.register('notEqual', { + name: '≠', + group: 'boolean', +}); +calculators.register('gt', { + name: '>', + group: 'boolean', +}); +calculators.register('gte', { + name: '≥', + group: 'boolean', +}); +calculators.register('lt', { + name: '<', + group: 'boolean', +}); +calculators.register('lte', { + name: '≤', + group: 'boolean', +}); + +calculators.register('add', { + name: '+', + group: 'number', +}); +calculators.register('minus', { + name: '-', + group: 'number', +}); +calculators.register('multiple', { + name: '*', + group: 'number', +}); +calculators.register('divide', { + name: '/', + group: 'number', +}); +calculators.register('mod', { + name: '%', + group: 'number', +}); + +calculators.register('includes', { + name: '{{t("contains")}}', + group: 'string' +}); +calculators.register('notIncludes', { + name: '{{t("does not contain")}}', + group: 'string' +}); +calculators.register('startsWith', { + name: '{{t("starts with")}}', + group: 'string' +}); +calculators.register('notStartsWith', { + name: '{{t("not starts with")}}', + group: 'string' +}); +calculators.register('endsWith', { + name: '{{t("ends with")}}', + group: 'string' +}); +calculators.register('notEndsWith', { + name: '{{t("not ends with")}}', + group: 'string' +}); + +const calculatorGroups = [ { value: 'boolean', - title: '{{t("Comparison")}}', - children: [ - { value: 'equal', name: '=' }, - { value: 'notEqual', name: '≠' }, - { value: 'gt', name: '>' }, - { value: 'gte', name: '≥' }, - { value: 'lt', name: '<' }, - { value: 'lte', name: '≤' } - ] + title: '{{t("Comparison")}}' }, { value: 'number', - title: '{{t("Arithmetic calculation")}}', - children: [ - { value: 'add', name: '+' }, - { value: 'minus', name: '-' }, - { value: 'multiple', name: '*' }, - { value: 'divide', name: '/' }, - { value: 'mod', name: '%' }, - ] + title: '{{t("Arithmetic calculation")}}' }, { value: 'string', - title: '{{t("String operation")}}', - children: [ - { value: 'includes', name: '{{t("contains")}}' }, - { value: 'notIncludes', name: '{{t("does not contain")}}' }, - { value: 'startsWith', name: '{{t("starts with")}}' }, - { value: 'notStartsWith', name: '{{t("not starts with")}}' }, - { value: 'endsWith', name: '{{t("ends with")}}' }, - { value: 'notEndsWith', name: '{{t("not ends with")}}' } - ] + title: '{{t("String operation")}}' }, { value: 'date', - title: '{{t("Date")}}', - children: [] + title: '{{t("Date")}}' } ]; +function getGroupCalculators(group) { + return Array.from(calculators.getEntities()).filter(([key, value]) => value.group === group); +} + const JT_VALUE_RE = /^\s*\{\{([\s\S]*)\}\}\s*$/; export function parseStringValue(value: string, Types) { @@ -320,10 +379,10 @@ export function Calculation({ calculator, operands = [], onChange }) { onChange={v => onChange({ operands, calculator: v })} placeholder={t('Calculator')} > - {calculators.map(group => ( + {calculatorGroups.map(group => ( - {group.children.map(item => ( - {compile(item.name)} + {getGroupCalculators(group.value).map(([value, { name }]) => ( + {compile(name)} ))} ))} diff --git a/packages/plugins/workflow/src/client/index.tsx b/packages/plugins/workflow/src/client/index.tsx index aefc7200d..a245465d9 100644 --- a/packages/plugins/workflow/src/client/index.tsx +++ b/packages/plugins/workflow/src/client/index.tsx @@ -1,3 +1,4 @@ export { triggers } from './triggers'; export * from './nodes'; +export { calculators } from './calculators'; export { WorkflowProvider as default } from './WorkflowProvider';