From 4a113774bd7e161933aa33daa73f79e5e704999b Mon Sep 17 00:00:00 2001 From: Junyi Date: Sun, 27 Nov 2022 06:54:47 -0800 Subject: [PATCH] fix(plugin-workflow): fix trigger context getters (#1149) --- .../antd/filter/FilterItems.tsx | 2 +- .../workflow/src/client/calculators.tsx | 24 ++++++++--- .../workflow/src/client/locale/en-US.ts | 2 +- .../workflow/src/client/locale/ja-JP.ts | 2 +- .../workflow/src/client/locale/ru-RU.ts | 2 +- .../workflow/src/client/locale/tr-TR.ts | 2 +- .../workflow/src/client/locale/zh-CN.ts | 2 +- .../src/client/triggers/collection.tsx | 16 ++++--- .../workflow/src/client/triggers/index.tsx | 4 +- .../src/client/triggers/schedule/index.tsx | 42 +++++++++++-------- 10 files changed, 62 insertions(+), 36 deletions(-) diff --git a/packages/core/client/src/schema-component/antd/filter/FilterItems.tsx b/packages/core/client/src/schema-component/antd/filter/FilterItems.tsx index 2f5d4dba0..c6064b0a1 100644 --- a/packages/core/client/src/schema-component/antd/filter/FilterItems.tsx +++ b/packages/core/client/src/schema-component/antd/filter/FilterItems.tsx @@ -11,7 +11,7 @@ export const FilterItems = observer((props) => {
{field?.value?.map((item, index) => { return ( - field.remove(index)}> + field.remove(index)}> ); diff --git a/packages/plugins/workflow/src/client/calculators.tsx b/packages/plugins/workflow/src/client/calculators.tsx index 8d137ac84..8ac82ccd1 100644 --- a/packages/plugins/workflow/src/client/calculators.tsx +++ b/packages/plugins/workflow/src/client/calculators.tsx @@ -297,18 +297,32 @@ export const VariableTypes = { } }, $context: { - title: `{{t("Trigger context", { ns: "${NAMESPACE}" })}}`, + title: `{{t("Trigger variables", { ns: "${NAMESPACE}" })}}`, value: '$context', + options() { + const { workflow } = useFlowContext(); + const trigger = triggers.get(workflow.type); + return trigger?.getOptions?.(workflow.config) ?? null; + }, component() { const { workflow } = useFlowContext(); const trigger = triggers.get(workflow.type); return trigger?.getter ?? NullRender; }, - parse([prefix, ...path]) { - return { path: path.join('.') }; + appendTypeValue({ options }) { + return options.type ? [options.type] : []; + }, + onTypeChange(old, [type, optionType], onChange) { + onChange({ type, options: { type: optionType } }); + }, + parse([type, ...path]) { + return { type, ...( path?.length ? { path: path.join('.') } : {}) }; }, stringify({ options }) { const stack = ['$context']; + if (options?.type) { + stack.push(options.type); + } if (options?.path) { stack.push(options.path); } @@ -345,7 +359,7 @@ export function Operand({ const { type } = operand; const { component, appendTypeValue } = Types[type] || {}; - const VariableComponent = typeof component === 'function' ? component(operand) : NullRender; + const Variable = typeof component === 'function' ? component(operand) : NullRender; return (
- {children ?? onChange({ ...op })} />} + {children ?? onChange({ ...op })} />}
); } diff --git a/packages/plugins/workflow/src/client/locale/en-US.ts b/packages/plugins/workflow/src/client/locale/en-US.ts index 3f637f62b..475038b0c 100644 --- a/packages/plugins/workflow/src/client/locale/en-US.ts +++ b/packages/plugins/workflow/src/client/locale/en-US.ts @@ -46,7 +46,7 @@ export default { "Advanced": "Advanced", "Advanced type": "Advanced", "End": "End", - "Trigger context": "Trigger context", + "Trigger variables": "Trigger variables", "Node result": "Node result", "Constant": "Constant", "Boolean": "Boolean", diff --git a/packages/plugins/workflow/src/client/locale/ja-JP.ts b/packages/plugins/workflow/src/client/locale/ja-JP.ts index 2139d7ac3..534c6b5aa 100644 --- a/packages/plugins/workflow/src/client/locale/ja-JP.ts +++ b/packages/plugins/workflow/src/client/locale/ja-JP.ts @@ -46,7 +46,7 @@ export default { "Advanced": "アドバンスド", "Advanced type": "アドバンスタイプ", "End": "終了", - "Trigger context": "トリガーフィールド", + "Trigger variables": "トリガーフィールド", "Node result": "ノードの結果", "Constant": "定数", "Boolean": "論理値", diff --git a/packages/plugins/workflow/src/client/locale/ru-RU.ts b/packages/plugins/workflow/src/client/locale/ru-RU.ts index 17bc4d44b..e89d9819d 100644 --- a/packages/plugins/workflow/src/client/locale/ru-RU.ts +++ b/packages/plugins/workflow/src/client/locale/ru-RU.ts @@ -46,7 +46,7 @@ export default { "Advanced": "Продвинутый", "Advanced type": "Продвинутый", "End": "Окончание", - "Trigger context": "Контекст триггера", + "Trigger variables": "Контекст триггера", "Node result": "Результат Узла", "Constant": "Константа", "Boolean": "Логический", diff --git a/packages/plugins/workflow/src/client/locale/tr-TR.ts b/packages/plugins/workflow/src/client/locale/tr-TR.ts index e6aaf80c3..db5cfe111 100644 --- a/packages/plugins/workflow/src/client/locale/tr-TR.ts +++ b/packages/plugins/workflow/src/client/locale/tr-TR.ts @@ -46,7 +46,7 @@ export default { "Advanced": "Gelişmiş", "Advanced type": "Gelişmiş", "End": "Son", - "Trigger context": "Tetikleyici bağlamı", + "Trigger variables": "Tetikleyici bağlamı", "Node result": "Düğüm sonucu", "Constant": "Devamlı", "Boolean": "Boolean", diff --git a/packages/plugins/workflow/src/client/locale/zh-CN.ts b/packages/plugins/workflow/src/client/locale/zh-CN.ts index d9a47e00a..adcf7737c 100644 --- a/packages/plugins/workflow/src/client/locale/zh-CN.ts +++ b/packages/plugins/workflow/src/client/locale/zh-CN.ts @@ -12,7 +12,7 @@ export default { 'Load failed': '加载失败', 'Trigger': '触发器', - 'Trigger context': '触发数据', + 'Trigger variables': '触发器变量', 'Trigger data': '触发数据', 'Trigger time': '触发时间', 'Triggered at': '触发时间', diff --git a/packages/plugins/workflow/src/client/triggers/collection.tsx b/packages/plugins/workflow/src/client/triggers/collection.tsx index 5c1821a8b..9ba844bca 100644 --- a/packages/plugins/workflow/src/client/triggers/collection.tsx +++ b/packages/plugins/workflow/src/client/triggers/collection.tsx @@ -9,7 +9,7 @@ import { collection, filter } from '../schemas/collection'; import { css } from '@emotion/css'; import { onFieldValueChange } from '@formily/core'; import CollectionFieldSelect from '../components/CollectionFieldSelect'; -import { NAMESPACE } from '../locale'; +import { NAMESPACE, useWorkflowTranslation } from '../locale'; const FieldsSelect = observer((props) => { const compile = useCompile(); @@ -139,17 +139,23 @@ export default { components: { FieldsSelect }, + getOptions(config) { + const { t } = useWorkflowTranslation(); + const options: any[] = [ + { value: 'data', label: t('Trigger data') }, + ]; + return options; + }, getter(props) { const { type, options, onChange } = props; const { workflow } = useFlowContext(); - const value = options?.path?.replace(/^data\./, ''); return ( { - onChange({ type, options: { ...options, path: `data.${value}` } }); + value={options?.path} + onChange={(path) => { + onChange({ type, options: { ...options, path } }); }} /> ); diff --git a/packages/plugins/workflow/src/client/triggers/index.tsx b/packages/plugins/workflow/src/client/triggers/index.tsx index 489f1b8a8..0d73f9237 100644 --- a/packages/plugins/workflow/src/client/triggers/index.tsx +++ b/packages/plugins/workflow/src/client/triggers/index.tsx @@ -43,7 +43,7 @@ export interface Trigger { title: string; type: string; // group: string; - options?: { label: string; value: any; key: string }[]; + getOptions?(config: any): { label: string; value: any; key: string }[]; fieldset: { [key: string]: ISchema }; view?: ISchema; scope?: { [key: string]: any }; @@ -108,7 +108,7 @@ function TriggerExecution() { }, context: { type: 'object', - title: `{{t("Trigger context", { ns: "${NAMESPACE}" })}}`, + title: `{{t("Trigger variables", { ns: "${NAMESPACE}" })}}`, 'x-decorator': 'FormItem', 'x-component': 'Input.JSON', 'x-component-props': { diff --git a/packages/plugins/workflow/src/client/triggers/schedule/index.tsx b/packages/plugins/workflow/src/client/triggers/schedule/index.tsx index 115a0847e..379844049 100644 --- a/packages/plugins/workflow/src/client/triggers/schedule/index.tsx +++ b/packages/plugins/workflow/src/client/triggers/schedule/index.tsx @@ -27,33 +27,39 @@ export default { components: { ScheduleConfig }, + getOptions(config) { + const { t } = useWorkflowTranslation(); + const options: any[] = [ + { value: 'date', label: t('Trigger time') }, + ]; + if (config.mode === SCHEDULE_MODE.COLLECTION_FIELD) { + options.push({ + value: 'data', + label: t('Trigger data') + }); + } + return options; + }, getter({ type, options, onChange }) { const { t } = useWorkflowTranslation(); const compile = useCompile(); - const { workflow } = useFlowContext(); const { collections = [] } = useCollectionManager(); + const { workflow } = useFlowContext(); const path = options?.path ? options.path.split('.') : []; - const varOptions: any[] = [ - { value: 'date', label: t('Trigger time') }, - ]; - if (workflow.config.mode === SCHEDULE_MODE.COLLECTION_FIELD) { - const collection = collections.find(item => item.name === workflow.config.collection) ?? { fields: [] }; - varOptions.push({ - value: 'data', - label: t('Trigger data'), - children: collection.fields + if (!options.type || options.type === 'date') { + return null; + } + const collection = collections.find(item => item.name === workflow.config.collection) ?? { fields: [] }; + return ( + BaseTypeSet.has(field?.uiSchema?.type)) .map(field => ({ value: field.name, label: compile(field.uiSchema?.title), - })) - }); - } - return ( - { onChange({ type, options: { ...options, path: next.join('.') } }); }}