From d0e524a1a07bd3b35a2c08ba8e56b28464819830 Mon Sep 17 00:00:00 2001 From: Junyi Date: Mon, 11 Apr 2022 22:06:41 +0800 Subject: [PATCH] feat(plugin-workflow): support context variables from model trigger (#284) --- .../client/src/workflow/WorkflowCanvas.tsx | 3 +- packages/client/src/workflow/calculators.tsx | 42 +++++++++++++++---- .../client/src/workflow/nodes/condition.tsx | 2 +- packages/client/src/workflow/nodes/query.tsx | 13 +++--- .../client/src/workflow/triggers/index.tsx | 23 +++++++--- .../client/src/workflow/triggers/model.tsx | 23 ++++++++++ 6 files changed, 83 insertions(+), 23 deletions(-) diff --git a/packages/client/src/workflow/WorkflowCanvas.tsx b/packages/client/src/workflow/WorkflowCanvas.tsx index 9afca0990..b1c50edde 100644 --- a/packages/client/src/workflow/WorkflowCanvas.tsx +++ b/packages/client/src/workflow/WorkflowCanvas.tsx @@ -40,7 +40,7 @@ export function WorkflowCanvas() { return
加载失败
; } - const { nodes = [] } = data?.data ?? {}; + const { nodes = [], ...workflow } = data?.data ?? {}; makeNodes(nodes); @@ -48,6 +48,7 @@ export function WorkflowCanvas() { return ( { - const children = typeof item.options === 'function' ? item.options() : item.options; + const options = typeof item.options === 'function' ? item.options() : item.options; return { label: item.title, value: item.value, - children, - disabled: children && !children.length, - isLeaf: !children + children: options, + disabled: options && !options.length, + isLeaf: !options }; })} onChange={(next: Array) => { @@ -246,7 +272,7 @@ export function Operand({ onChange, value: operand = { type: 'constant', value: } }} /> - onChange({ ...op })} /> + {children ?? onChange({ ...op })} />} ); } diff --git a/packages/client/src/workflow/nodes/condition.tsx b/packages/client/src/workflow/nodes/condition.tsx index a438af99d..bc3b53d65 100644 --- a/packages/client/src/workflow/nodes/condition.tsx +++ b/packages/client/src/workflow/nodes/condition.tsx @@ -161,7 +161,7 @@ export default { { label: '分支模式', key: 'branch', value: { rejectOnFalse: false } } ], render(data) { - const { id, config: { calculation, rejectOnFalse } } = data; + const { id, config: { rejectOnFalse } } = data; const { nodes } = useFlowContext(); const trueEntry = nodes.find(item => item.upstreamId === id && item.branchIndex === 1); const falseEntry = nodes.find(item => item.upstreamId === id && item.branchIndex === 0); diff --git a/packages/client/src/workflow/nodes/query.tsx b/packages/client/src/workflow/nodes/query.tsx index 7c754c3ef..33c066307 100644 --- a/packages/client/src/workflow/nodes/query.tsx +++ b/packages/client/src/workflow/nodes/query.tsx @@ -8,9 +8,7 @@ import { css } from '@emotion/css'; import { useCollectionManager } from '../..'; import { useCollectionFilterOptions } from '../../collection-manager/action-hooks'; import { useFlowContext } from '../WorkflowCanvas'; -import { Operand, parseStringValue, VariableTypes, VariableTypesContext } from '../calculators'; - -const BaseTypeSet = new Set(['boolean', 'number', 'string', 'date']); +import { Operand, parseStringValue, VariableTypes, VariableTypesContext, BaseTypeSet } from '../calculators'; export default { title: '数据查询', @@ -89,10 +87,7 @@ export default { constant: { title: '常量', value: 'constant', - options: undefined, - component() { - return renderSchemaComponent; - } + options: undefined } }; @@ -112,7 +107,9 @@ export default { onChange(stringify(next)); } }} - /> + > + {operand.type === 'constant' ? renderSchemaComponent() : null} + ); } diff --git a/packages/client/src/workflow/triggers/index.tsx b/packages/client/src/workflow/triggers/index.tsx index 2991c53de..50fb45205 100644 --- a/packages/client/src/workflow/triggers/index.tsx +++ b/packages/client/src/workflow/triggers/index.tsx @@ -1,6 +1,7 @@ import React from "react"; -import { useForm } from "@formily/react"; +import { ISchema, useForm } from "@formily/react"; import { cx } from "@emotion/css"; +import { Registry } from "@nocobase/utils"; import { SchemaComponent, useActionContext, useAPIClient, useRecord, useResourceActionContext } from '../../'; import model from './model'; @@ -30,18 +31,30 @@ function useUpdateConfigAction() { }; }; - -const triggerTypes = { - model +export interface Trigger { + title: string; + type: string; + // group: string; + options?: { label: string; value: any; key: string }[]; + fieldset: { [key: string]: ISchema }; + view?: ISchema; + scope?: { [key: string]: any }; + components?: { [key: string]: any }; + render?(props): React.ReactElement; + getter?(node: any): React.ReactElement; }; +export const triggers = new Registry(); + +triggers.register('model', model); + export const TriggerConfig = () => { const { data } = useResourceActionContext(); if (!data) { return null; } const { type, config } = data.data; - const { title, fieldset, scope } = triggerTypes[type]; + const { title, fieldset, scope } = triggers.get(type); return (

{title}

diff --git a/packages/client/src/workflow/triggers/model.tsx b/packages/client/src/workflow/triggers/model.tsx index a8905362a..8ed33e1c2 100644 --- a/packages/client/src/workflow/triggers/model.tsx +++ b/packages/client/src/workflow/triggers/model.tsx @@ -1,9 +1,15 @@ +import React from 'react'; import { action } from '@formily/reactive'; import { t } from 'i18next'; +import { Select } from 'antd'; + import { useCollectionManager } from '../../collection-manager'; +import { useFlowContext } from '../WorkflowCanvas'; +import { BaseTypeSet } from '../calculators'; export default { title: '数据表事件', + type: 'model', fieldset: { collection: { type: 'string', @@ -32,5 +38,22 @@ export default { })(collections); } } + }, + getter({ type, options, onChange }) { + const { collections = [] } = useCollectionManager(); + const { workflow } = useFlowContext(); + const collection = collections.find(item => item.name === workflow.config.collection) ?? { fields: [] }; + + return ( + + ); } };