@nocobase/plugin-workflow-dynamic-calculation 0.17.0-alpha.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +661 -0
- package/README.md +9 -0
- package/README.zh-CN.md +9 -0
- package/client.d.ts +2 -0
- package/client.js +1 -0
- package/dist/client/DynamicCalculation.d.ts +72 -0
- package/dist/client/DynamicExpression.d.ts +6 -0
- package/dist/client/Provider.d.ts +2 -0
- package/dist/client/expression.d.ts +3 -0
- package/dist/client/index.d.ts +7 -0
- package/dist/client/index.js +1 -0
- package/dist/externalVersion.js +13 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +39 -0
- package/dist/locale/en-US.json +11 -0
- package/dist/locale/index.d.ts +3 -0
- package/dist/locale/index.js +39 -0
- package/dist/locale/zh-CN.json +11 -0
- package/dist/server/DynamicCalculation.d.ts +7 -0
- package/dist/server/DynamicCalculation.js +62 -0
- package/dist/server/Plugin.d.ts +6 -0
- package/dist/server/Plugin.js +36 -0
- package/dist/server/expression-field.d.ts +7 -0
- package/dist/server/expression-field.js +32 -0
- package/dist/server/index.d.ts +1 -0
- package/dist/server/index.js +33 -0
- package/package.json +33 -0
- package/server.d.ts +2 -0
- package/server.js +1 -0
- package/src/client/DynamicCalculation.tsx +98 -0
- package/src/client/DynamicExpression.tsx +57 -0
- package/src/client/Provider.tsx +22 -0
- package/src/client/expression.tsx +24 -0
- package/src/client/index.ts +28 -0
- package/src/index.ts +2 -0
- package/src/locale/en-US.json +11 -0
- package/src/locale/index.ts +12 -0
- package/src/locale/zh-CN.json +11 -0
- package/src/server/DynamicCalculation.ts +29 -0
- package/src/server/Plugin.ts +19 -0
- package/src/server/__tests__/collections/categories.ts +19 -0
- package/src/server/__tests__/instruction.test.ts +115 -0
- package/src/server/expression-field.ts +11 -0
- package/src/server/index.ts +1 -0
package/client.d.ts
ADDED
package/client.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = require('./dist/client/index.js');
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { SchemaInitializerItemType } from '@nocobase/client';
|
|
3
|
+
import { Instruction, WorkflowVariableInput } from '@nocobase/plugin-workflow/client';
|
|
4
|
+
declare function DynamicExpression({ value, onChange }: {
|
|
5
|
+
value: any;
|
|
6
|
+
onChange: any;
|
|
7
|
+
}): React.JSX.Element;
|
|
8
|
+
export default class extends Instruction {
|
|
9
|
+
title: string;
|
|
10
|
+
type: string;
|
|
11
|
+
group: string;
|
|
12
|
+
description: string;
|
|
13
|
+
fieldset: {
|
|
14
|
+
expression: {
|
|
15
|
+
type: string;
|
|
16
|
+
title: string;
|
|
17
|
+
'x-decorator': string;
|
|
18
|
+
'x-component': string;
|
|
19
|
+
required: boolean;
|
|
20
|
+
};
|
|
21
|
+
scope: {
|
|
22
|
+
type: string;
|
|
23
|
+
title: string;
|
|
24
|
+
'x-decorator': string;
|
|
25
|
+
'x-component': string;
|
|
26
|
+
'x-component-props': {
|
|
27
|
+
changeOnSelect: boolean;
|
|
28
|
+
variableOptions: {
|
|
29
|
+
types: {
|
|
30
|
+
type: string;
|
|
31
|
+
options: {
|
|
32
|
+
collection: string;
|
|
33
|
+
entity: boolean;
|
|
34
|
+
};
|
|
35
|
+
}[];
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
'x-reactions': {
|
|
39
|
+
dependencies: string[];
|
|
40
|
+
fulfill: {
|
|
41
|
+
state: {
|
|
42
|
+
visible: string;
|
|
43
|
+
};
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
};
|
|
48
|
+
components: {
|
|
49
|
+
DynamicExpression: typeof DynamicExpression;
|
|
50
|
+
WorkflowVariableInput: typeof WorkflowVariableInput;
|
|
51
|
+
ValueBlock: (() => JSX.Element) & {
|
|
52
|
+
Initializer: () => JSX.Element;
|
|
53
|
+
Result: (props: any) => JSX.Element;
|
|
54
|
+
};
|
|
55
|
+
};
|
|
56
|
+
useVariables({ key, title }: {
|
|
57
|
+
key: any;
|
|
58
|
+
title: any;
|
|
59
|
+
}, { types, fieldNames }: {
|
|
60
|
+
types: any;
|
|
61
|
+
fieldNames?: {
|
|
62
|
+
readonly label: "label";
|
|
63
|
+
readonly value: "value";
|
|
64
|
+
readonly children: "children";
|
|
65
|
+
};
|
|
66
|
+
}): {
|
|
67
|
+
value: any;
|
|
68
|
+
label: any;
|
|
69
|
+
};
|
|
70
|
+
useInitializers(node: any): SchemaInitializerItemType;
|
|
71
|
+
}
|
|
72
|
+
export {};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
export declare const DynamicExpression: React.ForwardRefExoticComponent<Omit<Partial<Omit<{
|
|
3
|
+
children?: React.ReactNode;
|
|
4
|
+
}, string | number | symbol> & Omit<any, "ref"> & {
|
|
5
|
+
children?: React.ReactNode;
|
|
6
|
+
}>, "ref"> & React.RefAttributes<unknown>>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
(function(n,e){typeof exports=="object"&&typeof module!="undefined"?e(exports,require("@nocobase/client"),require("react/jsx-runtime"),require("react-i18next"),require("@nocobase/plugin-workflow/client"),require("@formily/core"),require("@formily/react"),require("antd"),require("react")):typeof define=="function"&&define.amd?define(["exports","@nocobase/client","react/jsx-runtime","react-i18next","@nocobase/plugin-workflow/client","@formily/core","@formily/react","antd","react"],e):(n=typeof globalThis!="undefined"?globalThis:n||self,e(n["@nocobase/plugin-workflow-dynamic-calculation"]={},n["@nocobase/client"],n.jsxRuntime,n["react-i18next"],n["@nocobase/plugin-workflow"],n["@formily/core"],n["@formily/react"],n.antd,n.react))})(this,function(n,e,o,C,r,g,u,x,c){"use strict";var z=Object.defineProperty,L=Object.defineProperties;var _=Object.getOwnPropertyDescriptors;var F=Object.getOwnPropertySymbols;var U=Object.prototype.hasOwnProperty,G=Object.prototype.propertyIsEnumerable;var v=(n,e,o)=>e in n?z(n,e,{enumerable:!0,configurable:!0,writable:!0,value:o}):n[e]=o,m=(n,e)=>{for(var o in e||(e={}))U.call(e,o)&&v(n,o,e[o]);if(F)for(var o of F(e))G.call(e,o)&&v(n,o,e[o]);return n},y=(n,e)=>L(n,_(e));var f=(n,e,o)=>(v(n,typeof e!="symbol"?e+"":e,o),o);var w=(n,e,o)=>new Promise((C,r)=>{var g=c=>{try{x(o.next(c))}catch(a){r(a)}},u=c=>{try{x(o.throw(c))}catch(a){r(a)}},x=c=>c.done?C(c.value):Promise.resolve(c.value).then(g,u);x((o=o.apply(n,e)).next())});const a="workflow-dynamic-calculation";function k(s,i={}){const{t}=I(i);return t(s)}function I(s){return C.useTranslation(a,s)}const{defaultProps:T}=e.interfacesProperties,P={name:"expression",type:"string",group:"advanced",order:1,title:`{{t("Expression", { ns: "${a}" })}}`,description:`{{t("An expression for calculation in each rows", { ns: "${a}" })}}`,sortable:!0,default:{type:"text",uiSchema:{"x-component":"DynamicExpression"}},properties:m({},T)};function E(s){const i=e.useCollectionManager();return o.jsx(e.CollectionManagerContext.Provider,{value:y(m({},i),{interfaces:y(m({},i.interfaces),{expression:P})}),children:s.children})}function M(s){var t;return["belongsTo","hasOne"].includes(s.type)?((t=this.getCollection(s.collectionName))==null?void 0:t.template)==="expression"?!0:this.getCollectionFields(s.target).some(l=>l.interface==="expression"):!1}function q({value:s,onChange:i}){const{getCollectionFields:t,getCollection:l}=e.useCollectionManager(),d=r.useWorkflowVariableOptions({types:[M.bind({getCollectionFields:t,getCollection:l})]});return o.jsx(e.Variable.Input,{value:s,onChange:i,scope:d})}class j extends r.Instruction{constructor(){super(...arguments);f(this,"title",`{{t("Dynamic Calculation", { ns: "${a}" })}}`);f(this,"type","dynamic-calculation");f(this,"group","control");f(this,"description",`{{t("Calculate an expression based on a calculation engine and obtain a value as the result. Variables in the upstream nodes can be used in the expression. The expression is dynamic one from an expression collections.", { ns: "${a}" })}}`);f(this,"fieldset",{expression:{type:"string",title:`{{t("Calculation expression", { ns: "${a}" })}}`,"x-decorator":"FormItem","x-component":"DynamicExpression",required:!0},scope:{type:"string",title:`{{t("Variable datasource", { ns: "${a}" })}}`,"x-decorator":"FormItem","x-component":"WorkflowVariableInput","x-component-props":{changeOnSelect:!0,variableOptions:{types:[{type:"reference",options:{collection:"*",entity:!0}}]}},"x-reactions":{dependencies:["expression"],fulfill:{state:{visible:"{{$deps[0]}}"}}}}});f(this,"components",{DynamicExpression:q,WorkflowVariableInput:r.WorkflowVariableInput,ValueBlock:r.ValueBlock})}useVariables({key:t,title:l},{types:d,fieldNames:p=r.defaultFieldNames}){return d&&!d.some(h=>h in r.BaseTypeSets||Object.values(r.BaseTypeSets).some(b=>b.has(h)))?null:{[p.value]:t,[p.label]:l}}useInitializers(t){var l;return{name:`#${t.id}`,type:"item",title:(l=t.title)!=null?l:`#${t.id}`,Component:r.ValueBlock.Initializer,node:t,resultTitle:k("Calculation result")}}}const D=u.observer(s=>{const{onChange:i}=s,t=u.useField(),l=u.useForm(),p=[...t.path.segments.slice(0,-1),"sourceCollection"].join("."),[h,b]=c.useState(l.getValuesIn(p)),B=e.useCompile(),{getCollectionFields:W}=e.useCollectionManager();u.useFormEffects(()=>{g.onFormInitialValuesChange(V=>{b(V.getValuesIn(p))}),g.onFieldInputValueChange(p,V=>{b(V.value),i(null)})});const N=r.getCollectionFieldOptions({collection:h,compile:B,getCollectionFields:W});return o.jsx(e.Variable.TextArea,y(m({},s),{scope:N}))},{displayName:"InternalExpression"});function O(s){const{t:i}=C.useTranslation(),t=e.useRecord(),l=e.useCompile(),{getCollectionFields:d}=e.useCollectionManager(),p=c.useMemo(()=>r.getCollectionFieldOptions({collection:t.sourceCollection,compile:l,getCollectionFields:d}),[t.sourceCollection,t.sourceCollection]);return s.value?o.jsx(e.Variable.TextArea,y(m({},s),{scope:p})):o.jsx(x.Tag,{children:i("Unconfigured",{ns:a})})}const S=u.connect(D,u.mapReadPretty(O));class A extends e.Plugin{afterAdd(){return w(this,null,function*(){})}beforeLoad(){return w(this,null,function*(){})}load(){return w(this,null,function*(){this.app.addProvider(E);const i=this.app.pm.get("workflow"),t=new j;i.instructions.register(t.type,t)})}addComponents(){this.app.addComponents({DynamicExpression:S})}}n.default=A,Object.defineProperties(n,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
module.exports = {
|
|
2
|
+
"react": "18.2.0",
|
|
3
|
+
"@nocobase/client": "0.17.0-alpha.4",
|
|
4
|
+
"@nocobase/plugin-workflow": "0.17.0-alpha.4",
|
|
5
|
+
"@formily/core": "2.2.27",
|
|
6
|
+
"@formily/react": "2.2.27",
|
|
7
|
+
"antd": "5.8.6",
|
|
8
|
+
"react-i18next": "11.18.6",
|
|
9
|
+
"@nocobase/utils": "0.17.0-alpha.4",
|
|
10
|
+
"@nocobase/evaluators": "0.17.0-alpha.4",
|
|
11
|
+
"@nocobase/server": "0.17.0-alpha.4",
|
|
12
|
+
"@nocobase/database": "0.17.0-alpha.4"
|
|
13
|
+
};
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
var src_exports = {};
|
|
30
|
+
__export(src_exports, {
|
|
31
|
+
default: () => import_server.default
|
|
32
|
+
});
|
|
33
|
+
module.exports = __toCommonJS(src_exports);
|
|
34
|
+
__reExport(src_exports, require("./server"), module.exports);
|
|
35
|
+
var import_server = __toESM(require("./server"));
|
|
36
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
37
|
+
0 && (module.exports = {
|
|
38
|
+
...require("./server")
|
|
39
|
+
});
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
"Dynamic Calculation": "Dynamic Calculation",
|
|
3
|
+
"Calculate an expression based on a calculation engine and obtain a value as the result. Variables in the upstream nodes can be used in the expression. The expression is dynamic one from an expression collections.": "Calculate an expression based on a calculation engine and obtain a value as the result. Variables in the upstream nodes can be used in the expression. The expression is dynamic one from an expression collections.",
|
|
4
|
+
"Select dynamic expression": "Select dynamic expression",
|
|
5
|
+
"Select the dynamic expression queried from the upstream node. You need to query it from an expression collection.": "Select the dynamic expression queried from the upstream node. You need to query it from an expression collection.",
|
|
6
|
+
"Variable datasource": "Variable datasource",
|
|
7
|
+
"Dynamic expression": "Dynamic expression",
|
|
8
|
+
"An expression for calculation in each rows": "An expression for calculation in each rows",
|
|
9
|
+
"Unconfigured": "Unconfigured",
|
|
10
|
+
"Calculation result": "Calculation result"
|
|
11
|
+
}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export declare const NAMESPACE = "workflow-dynamic-calculation";
|
|
2
|
+
export declare function useLang(key: string, options?: {}): string;
|
|
3
|
+
export declare function usePluginTranslation(options: any): import("react-i18next").UseTranslationResponse<"workflow-dynamic-calculation", undefined>;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
var locale_exports = {};
|
|
19
|
+
__export(locale_exports, {
|
|
20
|
+
NAMESPACE: () => NAMESPACE,
|
|
21
|
+
useLang: () => useLang,
|
|
22
|
+
usePluginTranslation: () => usePluginTranslation
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(locale_exports);
|
|
25
|
+
var import_react_i18next = require("react-i18next");
|
|
26
|
+
const NAMESPACE = "workflow-dynamic-calculation";
|
|
27
|
+
function useLang(key, options = {}) {
|
|
28
|
+
const { t } = usePluginTranslation(options);
|
|
29
|
+
return t(key);
|
|
30
|
+
}
|
|
31
|
+
function usePluginTranslation(options) {
|
|
32
|
+
return (0, import_react_i18next.useTranslation)(NAMESPACE, options);
|
|
33
|
+
}
|
|
34
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
35
|
+
0 && (module.exports = {
|
|
36
|
+
NAMESPACE,
|
|
37
|
+
useLang,
|
|
38
|
+
usePluginTranslation
|
|
39
|
+
});
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
"Dynamic Calculation": "动态表达式计算",
|
|
3
|
+
"Calculate an expression based on a calculation engine and obtain a value as the result. Variables in the upstream nodes can be used in the expression. The expression is dynamic one from an expression collections.": "基于计算引擎计算表达式并获取值作为结果。可以在表达式中使用上游节点的变量。表达式是从表达式表中动态获取的。",
|
|
4
|
+
"Select dynamic expression": "选择动态表达式",
|
|
5
|
+
"Select the dynamic expression queried from the upstream node. You need to query it from an expression collection.": "从上游节点中选择查询出来的动态表达式。你需要从动态表达式类型的数据表中查询。",
|
|
6
|
+
"Variable datasource": "变量数据源",
|
|
7
|
+
"Dynamic expression": "动态表达式",
|
|
8
|
+
"An expression for calculation in each rows": "每行数据计算规则不同时使用",
|
|
9
|
+
"Unconfigured": "未配置",
|
|
10
|
+
"Calculation result": "运算结果"
|
|
11
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
24
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
25
|
+
mod
|
|
26
|
+
));
|
|
27
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
+
var DynamicCalculation_exports = {};
|
|
29
|
+
__export(DynamicCalculation_exports, {
|
|
30
|
+
DynamicCalculation: () => DynamicCalculation
|
|
31
|
+
});
|
|
32
|
+
module.exports = __toCommonJS(DynamicCalculation_exports);
|
|
33
|
+
var import_utils = require("@nocobase/utils");
|
|
34
|
+
var import_plugin_workflow = require("@nocobase/plugin-workflow");
|
|
35
|
+
var import_evaluators = __toESM(require("@nocobase/evaluators"));
|
|
36
|
+
class DynamicCalculation extends import_plugin_workflow.Instruction {
|
|
37
|
+
async run(node, prevJob, processor) {
|
|
38
|
+
let { engine = "math.js", expression = "" } = node.config;
|
|
39
|
+
let scope = processor.getScope(node.id);
|
|
40
|
+
const parsed = (0, import_utils.parse)(expression)(scope) ?? {};
|
|
41
|
+
engine = parsed.engine;
|
|
42
|
+
expression = parsed.expression;
|
|
43
|
+
scope = (0, import_utils.parse)(node.config.scope ?? "")(scope) ?? {};
|
|
44
|
+
const evaluator = import_evaluators.default.get(engine);
|
|
45
|
+
try {
|
|
46
|
+
const result = evaluator && expression ? evaluator(expression, scope) : null;
|
|
47
|
+
return {
|
|
48
|
+
result,
|
|
49
|
+
status: import_plugin_workflow.JOB_STATUS.RESOLVED
|
|
50
|
+
};
|
|
51
|
+
} catch (e) {
|
|
52
|
+
return {
|
|
53
|
+
result: e.toString(),
|
|
54
|
+
status: import_plugin_workflow.JOB_STATUS.ERROR
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
60
|
+
0 && (module.exports = {
|
|
61
|
+
DynamicCalculation
|
|
62
|
+
});
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
var Plugin_exports = {};
|
|
19
|
+
__export(Plugin_exports, {
|
|
20
|
+
default: () => Plugin_default
|
|
21
|
+
});
|
|
22
|
+
module.exports = __toCommonJS(Plugin_exports);
|
|
23
|
+
var import_server = require("@nocobase/server");
|
|
24
|
+
var import_expression_field = require("./expression-field");
|
|
25
|
+
var import_DynamicCalculation = require("./DynamicCalculation");
|
|
26
|
+
class Plugin_default extends import_server.Plugin {
|
|
27
|
+
workflow;
|
|
28
|
+
async load() {
|
|
29
|
+
this.db.registerFieldTypes({
|
|
30
|
+
expression: import_expression_field.ExpressionField
|
|
31
|
+
});
|
|
32
|
+
const workflowPlugin = this.app.getPlugin("workflow");
|
|
33
|
+
this.workflow = workflowPlugin;
|
|
34
|
+
workflowPlugin.instructions.register("dynamic-calculation", new import_DynamicCalculation.DynamicCalculation(workflowPlugin));
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { BaseFieldOptions, DataTypes, Field } from '@nocobase/database';
|
|
2
|
+
export interface ExpressionFieldOptions extends BaseFieldOptions {
|
|
3
|
+
type: 'expression';
|
|
4
|
+
}
|
|
5
|
+
export declare class ExpressionField extends Field {
|
|
6
|
+
get dataType(): DataTypes.TextDataTypeConstructor;
|
|
7
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
var expression_field_exports = {};
|
|
19
|
+
__export(expression_field_exports, {
|
|
20
|
+
ExpressionField: () => ExpressionField
|
|
21
|
+
});
|
|
22
|
+
module.exports = __toCommonJS(expression_field_exports);
|
|
23
|
+
var import_database = require("@nocobase/database");
|
|
24
|
+
class ExpressionField extends import_database.Field {
|
|
25
|
+
get dataType() {
|
|
26
|
+
return import_database.DataTypes.TEXT;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
30
|
+
0 && (module.exports = {
|
|
31
|
+
ExpressionField
|
|
32
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default } from './Plugin';
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
24
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
25
|
+
mod
|
|
26
|
+
));
|
|
27
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
+
var server_exports = {};
|
|
29
|
+
__export(server_exports, {
|
|
30
|
+
default: () => import_Plugin.default
|
|
31
|
+
});
|
|
32
|
+
module.exports = __toCommonJS(server_exports);
|
|
33
|
+
var import_Plugin = __toESM(require("./Plugin"));
|
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@nocobase/plugin-workflow-dynamic-calculation",
|
|
3
|
+
"displayName": "Workflow: Dynamic calculation node",
|
|
4
|
+
"displayName.zh-CN": "工作流:动态表达式计算节点",
|
|
5
|
+
"description": "Useful plugin for doing dynamic calculation based on expression collection records in workflow.",
|
|
6
|
+
"description.zh-CN": "用于在工作流中进行基于数据行的动态表达式计算。",
|
|
7
|
+
"version": "0.17.0-alpha.4",
|
|
8
|
+
"license": "AGPL-3.0",
|
|
9
|
+
"main": "./dist/server/index.js",
|
|
10
|
+
"devDependencies": {
|
|
11
|
+
"@ant-design/icons": "5.x",
|
|
12
|
+
"@formily/antd-v5": "1.x",
|
|
13
|
+
"@formily/core": "2.x",
|
|
14
|
+
"@formily/react": "2.x",
|
|
15
|
+
"antd": "5.x",
|
|
16
|
+
"lodash": "4.17.21",
|
|
17
|
+
"react": "18.x",
|
|
18
|
+
"react-i18next": "^11.15.1",
|
|
19
|
+
"react-router-dom": "^6.11.2"
|
|
20
|
+
},
|
|
21
|
+
"peerDependencies": {
|
|
22
|
+
"@nocobase/client": "0.x",
|
|
23
|
+
"@nocobase/database": "0.x",
|
|
24
|
+
"@nocobase/evaluators": "0.x",
|
|
25
|
+
"@nocobase/plugin-collection-manager": "0.x",
|
|
26
|
+
"@nocobase/plugin-workflow": ">=0.17.0-alpha.3",
|
|
27
|
+
"@nocobase/plugin-workflow-test": ">=0.17.0-alpha.3",
|
|
28
|
+
"@nocobase/server": "0.x",
|
|
29
|
+
"@nocobase/test": "0.x",
|
|
30
|
+
"@nocobase/utils": "0.x"
|
|
31
|
+
},
|
|
32
|
+
"gitHead": "663b03a3799a70ba1a2bc6a0d686e679331a50ad"
|
|
33
|
+
}
|
package/server.d.ts
ADDED
package/server.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = require('./dist/server/index.js');
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
|
|
3
|
+
import { SchemaInitializerItemType, Variable, useCollectionManager } from '@nocobase/client';
|
|
4
|
+
import {
|
|
5
|
+
BaseTypeSets,
|
|
6
|
+
Instruction,
|
|
7
|
+
ValueBlock,
|
|
8
|
+
WorkflowVariableInput,
|
|
9
|
+
defaultFieldNames,
|
|
10
|
+
useWorkflowVariableOptions,
|
|
11
|
+
} from '@nocobase/plugin-workflow/client';
|
|
12
|
+
import { NAMESPACE, useLang } from '../locale';
|
|
13
|
+
|
|
14
|
+
function useDynamicExpressionCollectionFieldMatcher(field): boolean {
|
|
15
|
+
if (!['belongsTo', 'hasOne'].includes(field.type)) {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
if (this.getCollection(field.collectionName)?.template === 'expression') {
|
|
20
|
+
return true;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const fields = this.getCollectionFields(field.target);
|
|
24
|
+
return fields.some((f) => f.interface === 'expression');
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function DynamicExpression({ value, onChange }) {
|
|
28
|
+
const { getCollectionFields, getCollection } = useCollectionManager();
|
|
29
|
+
const scope = useWorkflowVariableOptions({
|
|
30
|
+
types: [useDynamicExpressionCollectionFieldMatcher.bind({ getCollectionFields, getCollection })],
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
return <Variable.Input value={value} onChange={onChange} scope={scope} />;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export default class extends Instruction {
|
|
37
|
+
title = `{{t("Dynamic Calculation", { ns: "${NAMESPACE}" })}}`;
|
|
38
|
+
type = 'dynamic-calculation';
|
|
39
|
+
group = 'control';
|
|
40
|
+
description = `{{t("Calculate an expression based on a calculation engine and obtain a value as the result. Variables in the upstream nodes can be used in the expression. The expression is dynamic one from an expression collections.", { ns: "${NAMESPACE}" })}}`;
|
|
41
|
+
fieldset = {
|
|
42
|
+
expression: {
|
|
43
|
+
type: 'string',
|
|
44
|
+
title: `{{t("Calculation expression", { ns: "${NAMESPACE}" })}}`,
|
|
45
|
+
'x-decorator': 'FormItem',
|
|
46
|
+
'x-component': 'DynamicExpression',
|
|
47
|
+
required: true,
|
|
48
|
+
},
|
|
49
|
+
scope: {
|
|
50
|
+
type: 'string',
|
|
51
|
+
title: `{{t("Variable datasource", { ns: "${NAMESPACE}" })}}`,
|
|
52
|
+
'x-decorator': 'FormItem',
|
|
53
|
+
'x-component': 'WorkflowVariableInput',
|
|
54
|
+
'x-component-props': {
|
|
55
|
+
changeOnSelect: true,
|
|
56
|
+
variableOptions: {
|
|
57
|
+
types: [{ type: 'reference', options: { collection: '*', entity: true } }],
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
'x-reactions': {
|
|
61
|
+
dependencies: ['expression'],
|
|
62
|
+
fulfill: {
|
|
63
|
+
state: {
|
|
64
|
+
visible: '{{$deps[0]}}',
|
|
65
|
+
},
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
},
|
|
69
|
+
};
|
|
70
|
+
components = {
|
|
71
|
+
DynamicExpression,
|
|
72
|
+
WorkflowVariableInput,
|
|
73
|
+
ValueBlock,
|
|
74
|
+
};
|
|
75
|
+
useVariables({ key, title }, { types, fieldNames = defaultFieldNames }) {
|
|
76
|
+
if (
|
|
77
|
+
types &&
|
|
78
|
+
!types.some((type) => type in BaseTypeSets || Object.values(BaseTypeSets).some((set) => set.has(type)))
|
|
79
|
+
) {
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
return {
|
|
83
|
+
[fieldNames.value]: key,
|
|
84
|
+
[fieldNames.label]: title,
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
useInitializers(node): SchemaInitializerItemType {
|
|
88
|
+
return {
|
|
89
|
+
name: `#${node.id}`,
|
|
90
|
+
type: 'item',
|
|
91
|
+
title: node.title ?? `#${node.id}`,
|
|
92
|
+
Component: ValueBlock.Initializer,
|
|
93
|
+
node,
|
|
94
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
95
|
+
resultTitle: useLang('Calculation result'),
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { onFieldInputValueChange, onFormInitialValuesChange } from '@formily/core';
|
|
2
|
+
import { connect, mapReadPretty, observer, useField, useForm, useFormEffects } from '@formily/react';
|
|
3
|
+
import { Tag } from 'antd';
|
|
4
|
+
import React, { useMemo, useState } from 'react';
|
|
5
|
+
import { useTranslation } from 'react-i18next';
|
|
6
|
+
|
|
7
|
+
import { useCollectionManager, useCompile, useRecord, Variable } from '@nocobase/client';
|
|
8
|
+
import { getCollectionFieldOptions } from '@nocobase/plugin-workflow/client';
|
|
9
|
+
|
|
10
|
+
import { NAMESPACE } from '../locale';
|
|
11
|
+
|
|
12
|
+
const InternalExpression = observer(
|
|
13
|
+
(props: any) => {
|
|
14
|
+
const { onChange } = props;
|
|
15
|
+
const field = useField<any>();
|
|
16
|
+
// TODO(refactor): better to provide another context like useFieldset()
|
|
17
|
+
const form = useForm();
|
|
18
|
+
const basePath = field.path.segments.slice(0, -1);
|
|
19
|
+
const collectionPath = [...basePath, 'sourceCollection'].join('.');
|
|
20
|
+
const [collection, setCollection] = useState(form.getValuesIn(collectionPath));
|
|
21
|
+
const compile = useCompile();
|
|
22
|
+
const { getCollectionFields } = useCollectionManager();
|
|
23
|
+
|
|
24
|
+
useFormEffects(() => {
|
|
25
|
+
onFormInitialValuesChange((form) => {
|
|
26
|
+
setCollection(form.getValuesIn(collectionPath));
|
|
27
|
+
});
|
|
28
|
+
onFieldInputValueChange(collectionPath, (f) => {
|
|
29
|
+
setCollection(f.value);
|
|
30
|
+
onChange(null);
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
const options = getCollectionFieldOptions({ collection, compile, getCollectionFields });
|
|
35
|
+
|
|
36
|
+
return <Variable.TextArea {...props} scope={options} />;
|
|
37
|
+
},
|
|
38
|
+
{ displayName: 'InternalExpression' },
|
|
39
|
+
);
|
|
40
|
+
|
|
41
|
+
function Result(props) {
|
|
42
|
+
const { t } = useTranslation();
|
|
43
|
+
const values = useRecord();
|
|
44
|
+
const compile = useCompile();
|
|
45
|
+
const { getCollectionFields } = useCollectionManager();
|
|
46
|
+
const options = useMemo(
|
|
47
|
+
() => getCollectionFieldOptions({ collection: values.sourceCollection, compile, getCollectionFields }),
|
|
48
|
+
[values.sourceCollection, values.sourceCollection],
|
|
49
|
+
);
|
|
50
|
+
return props.value ? (
|
|
51
|
+
<Variable.TextArea {...props} scope={options} />
|
|
52
|
+
) : (
|
|
53
|
+
<Tag>{t('Unconfigured', { ns: NAMESPACE })}</Tag>
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export const DynamicExpression = connect(InternalExpression, mapReadPretty(Result));
|