@nocobase/plugin-field-formula 1.0.0-alpha.1
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/FormulaComponentFieldSettings.d.ts +2 -0
- package/dist/client/components/Formula/Expression.d.ts +3 -0
- package/dist/client/components/Formula/Result.d.ts +4 -0
- package/dist/client/components/Formula/index.d.ts +7 -0
- package/dist/client/components/index.d.ts +1 -0
- package/dist/client/index.d.ts +5 -0
- package/dist/client/index.js +8 -0
- package/dist/client/interfaces/formula.d.ts +127 -0
- package/dist/client/locale/index.d.ts +3 -0
- package/dist/client/scopes.d.ts +2 -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 +8 -0
- package/dist/locale/ko_KR.json +8 -0
- package/dist/locale/zh-CN.json +8 -0
- package/dist/server/formula-field.d.ts +19 -0
- package/dist/server/formula-field.js +118 -0
- package/dist/server/index.d.ts +1 -0
- package/dist/server/index.js +33 -0
- package/dist/server/migrations/20230213161904-alter-field.d.ts +5 -0
- package/dist/server/migrations/20230213161904-alter-field.js +62 -0
- package/dist/server/migrations/20240425222058-change-locale-module.d.ts +6 -0
- package/dist/server/migrations/20240425222058-change-locale-module.js +42 -0
- package/dist/server/plugin.d.ts +11 -0
- package/dist/server/plugin.js +57 -0
- package/dist/utils/index.d.ts +45 -0
- package/dist/utils/index.js +188 -0
- package/package.json +32 -0
- package/server.d.ts +2 -0
- package/server.js +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,7 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
export declare const Formula: {
|
|
3
|
+
(): any;
|
|
4
|
+
Expression: (props: any) => import("react").JSX.Element;
|
|
5
|
+
Result: import("react").ForwardRefExoticComponent<Omit<Partial<any>, "ref"> & import("react").RefAttributes<unknown>>;
|
|
6
|
+
};
|
|
7
|
+
export default Formula;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { Formula } from './Formula';
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
(function(n,r){typeof exports=="object"&&typeof module!="undefined"?r(exports,require("@nocobase/client"),require("@formily/react"),require("react/jsx-runtime"),require("@formily/core"),require("@formily/reactive"),require("@nocobase/evaluators/client"),require("@nocobase/utils/client"),require("react"),require("react-i18next")):typeof define=="function"&&define.amd?define(["exports","@nocobase/client","@formily/react","react/jsx-runtime","@formily/core","@formily/reactive","@nocobase/evaluators/client","@nocobase/utils/client","react","react-i18next"],r):(n=typeof globalThis!="undefined"?globalThis:n||self,r(n["@nocobase/plugin-field-formula"]={},n["@nocobase/client"],n["@formily/react"],n.jsxRuntime,n["@formily/core"],n["@formily/reactive"],n["@nocobase/evaluators"],n["@nocobase/utils"],n.react))})(this,function(n,r,o,s,I,E,x,b,c){"use strict";var ce=Object.defineProperty,me=Object.defineProperties;var de=Object.getOwnPropertyDescriptors;var D=Object.getOwnPropertySymbols;var J=Object.prototype.hasOwnProperty,U=Object.prototype.propertyIsEnumerable;var R=(n,r,o)=>r in n?ce(n,r,{enumerable:!0,configurable:!0,writable:!0,value:o}):n[r]=o,y=(n,r)=>{for(var o in r||(r={}))J.call(r,o)&&R(n,o,r[o]);if(D)for(var o of D(r))U.call(r,o)&&R(n,o,r[o]);return n},v=(n,r)=>me(n,de(r));var z=(n,r)=>{var o={};for(var s in n)J.call(n,s)&&r.indexOf(s)<0&&(o[s]=n[s]);if(n!=null&&D)for(var s of D(n))r.indexOf(s)<0&&U.call(n,s)&&(o[s]=n[s]);return o};var l=(n,r,o)=>(R(n,typeof r!="symbol"?r+"":r,o),o);var H=(n,r,o)=>new Promise((s,I)=>{var E=c=>{try{b(o.next(c))}catch(C){I(C)}},x=c=>{try{b(o.throw(c))}catch(C){I(C)}},b=c=>c.done?s(c.value):Promise.resolve(c.value).then(E,x);b((o=o.apply(n,r)).next())});const C=e=>{var S;const{value:t="",supports:i=[],useCurrentFields:a,onChange:u}=e,h=r.useCompile(),{interfaces:d}=r.useCollectionManager_deprecated(),f=((S=a==null?void 0:a())!=null?S:[]).filter(m=>i.includes(m.interface)).map(m=>{var q,F;return{label:h(m.uiSchema.title),value:m.name,children:(F=(q=d[m.interface]).usePathOptions)==null?void 0:F.call(q,m)}});return s.jsx(r.Variable.TextArea,{value:t,onChange:u,scope:f})},K={boolean:Boolean,integer:{boolean(e){return Number(e)},number(e){return e>=0?Math.floor(e):Math.ceil(e)},bigint(e){return Number(e)},string(e){const t=Number.parseInt(e,10);return Number.isNaN(t)||!Number.isFinite(t)?null:t},date(e){const t=e.valueOf();return Number.isNaN(t)?null:t}},bigInt:{boolean(e){return Number(e)},number(e){return Math.floor(e>=0?Math.floor(e):Math.ceil(e))},bigint(e){return Number(e)},string(e){const t=Number.parseInt(e,10);return Number.isNaN(t)||!Number.isFinite(t)?null:t},date(e){const t=e.valueOf();return Number.isNaN(t)?null:t}},double:{boolean(e){return Number(e)},number(e){return e},bigint(e){return Number(e)},string(e){const t=Number.parseFloat(e);return Number.isNaN(t)||!Number.isFinite(t)?null:t},date(e){const t=e.valueOf();return Number.isNaN(t)?null:t}},decimal:{boolean(e){return Number(e)},number(e){return e},bigint(e){return e},date(e){const t=e.valueOf();return Number.isNaN(t)?null:t}},string:{boolean(e){return e.toString()},number(e){return e.toString()},bigint(e){return e.toString()},string(e){return e},date(e){return e.toISOString()}},date:{boolean(e){return null},number(e){const t=new Date(e);return Number.isNaN(t.valueOf())?null:t},bigint(e){const t=new Date(Number(e));return Number.isNaN(t.valueOf())?null:t},string(e){const t=Date.parse(e);return Number.isNaN(t)?null:new Date(t)},date(e){return new Date(e)}}};function L(e,t){if(e==null)return null;let i=typeof e;i=="object"&&e instanceof Date&&(i="date");const a=K[t];if(!a)return null;if(typeof a=="function")return a(e);const u=a[i];return u?u(e):null}const Q={boolean:r.Checkbox.ReadPretty,integer:r.InputNumber.ReadPretty,bigInt:r.InputNumber.ReadPretty,double:r.InputNumber.ReadPretty,decimal:r.InputNumber.ReadPretty,date:r.DatePicker.ReadPretty,string:r.Input.ReadPretty};function j(e){const t=o.useFieldSchema(),i=e||t,a=r.useCollection_deprecated(),{getCollection:u,getCollectionField:h}=r.useCollectionManager_deprecated(),d=i.name.split(".");let p=a;for(let f=0;f<d.length-1;f++){const S=p.getField(d[f]);p=u(S.target)}return h(`${p.name}.${d[d.length-1]}`)}function W(e,t,i){const a=e[t];return Array.isArray(a)?a[i]:a&&typeof a=="object"?a:e}function X(e){var k,_,B;const V=e,{value:t}=V,i=z(V,["value"]),a=o.useFieldSchema(),{dataType:u,expression:h,engine:d="math.js"}=(k=j())!=null?k:{},[p,f]=c.useState(t),{evaluate:S}=x.evaluators.get(d),m=r.useFormBlockContext(),F=o.useField().path.entire,A=F==null?void 0:F.replace(`.${a.name}`,""),ae=A.split(".")[0],se=parseInt((_=A.split("."))==null?void 0:_[1]),P=c.useContext(r.ActionContext);c.useEffect(()=>{f(t)},[t]),o.useFormEffects(()=>{I.onFormValuesChange(ue=>{var G,$;if(a.name.indexOf(".")>=0||!(m!=null&&m.form)||(G=m.form)!=null&&G.readPretty)return;const le=E.toJS(W(ue.values,ae,se));let g;try{g=S(h,le),g=L(g,u)}catch(pe){g=null}g==null&&p==null&&f(g),f(g),e.onChange(g),($=P==null?void 0:P.setFormValueChanged)==null||$.call(P,!1)})});const ie=(B=Q[u])!=null?B:r.Input;return s.jsx(ie,v(y({},i),{value:u==="double"?b.toFixedByStep(p,e.step):p}))}const M=()=>null;M.Expression=C,M.Result=o.connect(X);const N="field-formula";function Y(e){const t=x.evaluators.get(e);return t!=null&&t.link?s.jsxs(s.Fragment,{children:[s.jsx("span",{className:r.css`
|
|
2
|
+
&:after {
|
|
3
|
+
content: ':';
|
|
4
|
+
}
|
|
5
|
+
& + a {
|
|
6
|
+
margin-left: 0.25em;
|
|
7
|
+
}
|
|
8
|
+
`,children:r.i18n.t("Syntax references",{ns:N})}),s.jsx("a",{href:t.link,target:"_blank",rel:"noreferrer",children:t.label})]}):null}const Z=[{dependencies:["dataType"],fulfill:{state:{display:'{{["double", "decimal"].includes($deps[0]) ? "visible" : "none"}}'}}}],O=[{dependencies:["dataType"],fulfill:{state:{display:'{{$deps[0] === "date" ? "visible" : "none"}}'}}}],{defaultProps:ee,dateTimeProps:T,operators:te}=r.interfacesProperties,re={"uiSchema.x-component-props.dateFormat":v(y({},b.lodash.cloneDeep(T["uiSchema.x-component-props.dateFormat"])),{"x-reactions":O}),"uiSchema.x-component-props.showTime":v(y({},b.lodash.cloneDeep(T["uiSchema.x-component-props.showTime"])),{"x-reactions":[...T["uiSchema.x-component-props.showTime"]["x-reactions"],...O]}),"uiSchema.x-component-props.timeFormat":y({},b.lodash.cloneDeep(T["uiSchema.x-component-props.timeFormat"]))};class ne extends r.CollectionFieldInterface{constructor(){super(...arguments);l(this,"name","formula");l(this,"type","object");l(this,"group","advanced");l(this,"order",1);l(this,"title",`{{t("Formula", { ns: "${N}" })}}`);l(this,"description",`{{t("Configure and store the results of calculations between multiple field values in the same record, supporting both Math.js and Excel formula functions.", { ns: "${N}" })}}`);l(this,"sortable",!0);l(this,"default",{type:"formula",uiSchema:{type:"string","x-component":"Formula.Result","x-component-props":{stringMode:!0,step:"1"},"x-read-pretty":!0}});l(this,"properties",v(y(v(y({},ee),{dataType:{type:"string",title:'{{t("Storage type")}}',"x-decorator":"FormItem","x-component":"Select","x-disabled":"{{ !createOnly }}",enum:[{value:"boolean",label:"Boolean"},{value:"integer",label:"Integer"},{value:"bigInt",label:"Big integer"},{value:"double",label:"Double"},{value:"string",label:"String"},{value:"date",label:"Datetime"}],required:!0,default:"double"},"uiSchema.x-component-props.step":{type:"string",title:'{{t("Precision")}}',"x-component":"Select","x-decorator":"FormItem",required:!0,default:"0",enum:[{value:"0",label:"1"},{value:"0.1",label:"1.0"},{value:"0.01",label:"1.00"},{value:"0.001",label:"1.000"},{value:"0.0001",label:"1.0000"},{value:"0.00001",label:"1.00000"}],"x-reactions":Z}}),re),{engine:{type:"string",title:`{{t("Calculation engine", { ns: "${N}" })}}`,"x-decorator":"FormItem","x-component":"Radio.Group",enum:Array.from(x.evaluators.getEntities()).reduce((i,[a,u])=>i.concat(y({value:a},u)),[]),required:!0,default:"math.js"},expression:{type:"string",title:`{{t("Expression", { ns: "${N}" })}}`,required:!0,"x-component":"Formula.Expression","x-decorator":"FormItem","x-component-props":{supports:["checkbox","number","percent","integer","number","percent","input","textarea","email","phone","datetime","createdAt","updatedAt","radioGroup","checkboxGroup","select","multipleSelect"],useCurrentFields:"{{ useCurrentFields }}"},"x-reactions":{dependencies:["engine"],fulfill:{schema:{description:"{{renderExpressionDescription($deps[0])}}"}}},"x-validator"(i,a,{form:u}){const{values:h}=u,{evaluate:d}=x.evaluators.get(h.engine),p=i.trim().replace(/{{\s*([^{}]+)\s*}}/g,"1");try{return d(p),""}catch(f){return r.i18n.t("Expression syntax error",{ns:N})}}}}));l(this,"filterable",{operators:te.number});l(this,"titleUsable",!0)}}const oe=new r.SchemaSettings({name:"fieldSettings:component:Formula.Result",items:[{name:"displayFormat",Component:r.SchemaSettingsNumberFormat,useComponentProps(){const e=o.useFieldSchema(),{fieldSchema:t}=r.useColumnSchema();return{fieldSchema:t||e}},useVisible(){const e=o.useFieldSchema(),{fieldSchema:t}=r.useColumnSchema(),i=t||e,{dataType:a}=j(i)||{},u=["integer","bigInt","double","decimal"].includes(a);return r.useIsFieldReadPretty()&&u}}]});class w extends r.Plugin{load(){return H(this,null,function*(){this.app.addComponents({Formula:M}),this.app.addScopes({renderExpressionDescription:Y}),this.app.dataSourceManager.addFieldInterfaces([ne]),this.app.schemaSettingsManager.add(oe)})}}n.PluginFieldFormulaClient=w,n.default=w,Object.defineProperties(n,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { CollectionFieldInterface } from '@nocobase/client';
|
|
2
|
+
export declare class FormulaFieldInterface extends CollectionFieldInterface {
|
|
3
|
+
name: string;
|
|
4
|
+
type: string;
|
|
5
|
+
group: string;
|
|
6
|
+
order: number;
|
|
7
|
+
title: string;
|
|
8
|
+
description: string;
|
|
9
|
+
sortable: boolean;
|
|
10
|
+
default: {
|
|
11
|
+
type: string;
|
|
12
|
+
uiSchema: {
|
|
13
|
+
type: string;
|
|
14
|
+
'x-component': string;
|
|
15
|
+
'x-component-props': {
|
|
16
|
+
stringMode: boolean;
|
|
17
|
+
step: string;
|
|
18
|
+
};
|
|
19
|
+
'x-read-pretty': boolean;
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
properties: {
|
|
23
|
+
engine: {
|
|
24
|
+
type: string;
|
|
25
|
+
title: string;
|
|
26
|
+
'x-decorator': string;
|
|
27
|
+
'x-component': string;
|
|
28
|
+
enum: unknown;
|
|
29
|
+
required: boolean;
|
|
30
|
+
default: string;
|
|
31
|
+
};
|
|
32
|
+
expression: {
|
|
33
|
+
type: string;
|
|
34
|
+
title: string;
|
|
35
|
+
required: boolean;
|
|
36
|
+
'x-component': string;
|
|
37
|
+
'x-decorator': string;
|
|
38
|
+
'x-component-props': {
|
|
39
|
+
supports: string[];
|
|
40
|
+
useCurrentFields: string;
|
|
41
|
+
};
|
|
42
|
+
'x-reactions': {
|
|
43
|
+
dependencies: string[];
|
|
44
|
+
fulfill: {
|
|
45
|
+
schema: {
|
|
46
|
+
description: string;
|
|
47
|
+
};
|
|
48
|
+
};
|
|
49
|
+
};
|
|
50
|
+
"x-validator"(value: any, rules: any, { form }: {
|
|
51
|
+
form: any;
|
|
52
|
+
}): string;
|
|
53
|
+
};
|
|
54
|
+
'uiSchema.x-component-props.dateFormat': any;
|
|
55
|
+
'uiSchema.x-component-props.showTime': any;
|
|
56
|
+
'uiSchema.x-component-props.timeFormat': any;
|
|
57
|
+
dataType: {
|
|
58
|
+
type: string;
|
|
59
|
+
title: string;
|
|
60
|
+
'x-decorator': string;
|
|
61
|
+
'x-component': string;
|
|
62
|
+
'x-disabled': string;
|
|
63
|
+
enum: {
|
|
64
|
+
value: string;
|
|
65
|
+
label: string;
|
|
66
|
+
}[];
|
|
67
|
+
required: boolean;
|
|
68
|
+
default: string;
|
|
69
|
+
};
|
|
70
|
+
'uiSchema.x-component-props.step': {
|
|
71
|
+
type: string;
|
|
72
|
+
title: string;
|
|
73
|
+
'x-component': string;
|
|
74
|
+
'x-decorator': string;
|
|
75
|
+
required: boolean;
|
|
76
|
+
default: string;
|
|
77
|
+
enum: {
|
|
78
|
+
value: string;
|
|
79
|
+
label: string;
|
|
80
|
+
}[];
|
|
81
|
+
'x-reactions': {
|
|
82
|
+
dependencies: string[];
|
|
83
|
+
fulfill: {
|
|
84
|
+
state: {
|
|
85
|
+
display: string;
|
|
86
|
+
};
|
|
87
|
+
};
|
|
88
|
+
}[];
|
|
89
|
+
};
|
|
90
|
+
'uiSchema.title': {
|
|
91
|
+
type: string;
|
|
92
|
+
title: string;
|
|
93
|
+
required: boolean;
|
|
94
|
+
'x-decorator': string;
|
|
95
|
+
'x-component': string;
|
|
96
|
+
};
|
|
97
|
+
name: {
|
|
98
|
+
type: string;
|
|
99
|
+
title: string;
|
|
100
|
+
required: boolean;
|
|
101
|
+
'x-disabled': string;
|
|
102
|
+
'x-decorator': string;
|
|
103
|
+
'x-component': string;
|
|
104
|
+
'x-validator': string;
|
|
105
|
+
description: string;
|
|
106
|
+
};
|
|
107
|
+
};
|
|
108
|
+
filterable: {
|
|
109
|
+
operators: ({
|
|
110
|
+
label: string;
|
|
111
|
+
value: string;
|
|
112
|
+
selected: boolean;
|
|
113
|
+
noValue?: undefined;
|
|
114
|
+
} | {
|
|
115
|
+
label: string;
|
|
116
|
+
value: string;
|
|
117
|
+
selected?: undefined;
|
|
118
|
+
noValue?: undefined;
|
|
119
|
+
} | {
|
|
120
|
+
label: string;
|
|
121
|
+
value: string;
|
|
122
|
+
noValue: boolean;
|
|
123
|
+
selected?: undefined;
|
|
124
|
+
})[];
|
|
125
|
+
};
|
|
126
|
+
titleUsable: boolean;
|
|
127
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
module.exports = {
|
|
2
|
+
"@formily/react": "2.3.0",
|
|
3
|
+
"@nocobase/client": "1.0.0-alpha.1",
|
|
4
|
+
"@nocobase/evaluators": "1.0.0-alpha.1",
|
|
5
|
+
"@nocobase/utils": "1.0.0-alpha.1",
|
|
6
|
+
"react": "18.2.0",
|
|
7
|
+
"@nocobase/database": "1.0.0-alpha.1",
|
|
8
|
+
"@nocobase/server": "1.0.0-alpha.1",
|
|
9
|
+
"@nocobase/test": "1.0.0-alpha.1",
|
|
10
|
+
"react-i18next": "11.18.6",
|
|
11
|
+
"@formily/core": "2.3.0",
|
|
12
|
+
"@formily/reactive": "2.3.0"
|
|
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,8 @@
|
|
|
1
|
+
{
|
|
2
|
+
"Formula": "Formula",
|
|
3
|
+
"Calculation engine": "Calculation engine",
|
|
4
|
+
"Expression": "Expression",
|
|
5
|
+
"Expression syntax error": "Expression syntax error",
|
|
6
|
+
"Syntax references": "Syntax references",
|
|
7
|
+
"Compute a value based on the other fields": "Compute a value based on the other fields"
|
|
8
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
{
|
|
2
|
+
"Formula": "公式",
|
|
3
|
+
"Calculation engine": "计算引擎",
|
|
4
|
+
"Expression": "表达式",
|
|
5
|
+
"Expression syntax error": "表达式语法错误",
|
|
6
|
+
"Syntax references": "语法参考",
|
|
7
|
+
"Configure and store the results of calculations between multiple field values in the same record, supporting both Math.js and Excel formula functions.": "可以配置并存储同一条记录的多字段值之间的计算结果,支持 Math.js 和 Excel formula functions 两种引擎。"
|
|
8
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { BaseFieldOptions, Field } from '@nocobase/database';
|
|
2
|
+
export interface FormulaFieldOptions extends BaseFieldOptions {
|
|
3
|
+
type: 'formula';
|
|
4
|
+
engine: string;
|
|
5
|
+
expression: string;
|
|
6
|
+
}
|
|
7
|
+
export declare class FormulaField extends Field {
|
|
8
|
+
get dataType(): any;
|
|
9
|
+
calculate(scope: any): any;
|
|
10
|
+
initFieldData: ({ transaction }: {
|
|
11
|
+
transaction: any;
|
|
12
|
+
}) => Promise<void>;
|
|
13
|
+
calculateField: (instance: any) => Promise<void>;
|
|
14
|
+
updateFieldData: (instance: any, { transaction }: {
|
|
15
|
+
transaction: any;
|
|
16
|
+
}) => Promise<void>;
|
|
17
|
+
bind(): void;
|
|
18
|
+
unbind(): void;
|
|
19
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
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 formula_field_exports = {};
|
|
19
|
+
__export(formula_field_exports, {
|
|
20
|
+
FormulaField: () => FormulaField
|
|
21
|
+
});
|
|
22
|
+
module.exports = __toCommonJS(formula_field_exports);
|
|
23
|
+
var import_database = require("@nocobase/database");
|
|
24
|
+
var import_evaluators = require("@nocobase/evaluators");
|
|
25
|
+
var import_utils = require("../utils");
|
|
26
|
+
const DataTypeMap = {
|
|
27
|
+
boolean: import_database.DataTypes.BOOLEAN,
|
|
28
|
+
integer: import_database.DataTypes.INTEGER,
|
|
29
|
+
bigInt: import_database.DataTypes.BIGINT,
|
|
30
|
+
double: import_database.DataTypes.DOUBLE,
|
|
31
|
+
decimal: import_database.DataTypes.DECIMAL,
|
|
32
|
+
string: import_database.DataTypes.STRING,
|
|
33
|
+
date: import_database.DataTypes.DATE(3)
|
|
34
|
+
};
|
|
35
|
+
class FormulaField extends import_database.Field {
|
|
36
|
+
get dataType() {
|
|
37
|
+
const { dataType } = this.options;
|
|
38
|
+
return DataTypeMap[dataType] ?? import_database.DataTypes.DOUBLE;
|
|
39
|
+
}
|
|
40
|
+
calculate(scope) {
|
|
41
|
+
const { expression, engine = "math.js", dataType = "double" } = this.options;
|
|
42
|
+
const evaluate = import_evaluators.evaluators.get(engine);
|
|
43
|
+
try {
|
|
44
|
+
const result = evaluate(expression, scope);
|
|
45
|
+
return (0, import_utils.toDbType)(result, dataType);
|
|
46
|
+
} catch (e) {
|
|
47
|
+
console.error(e);
|
|
48
|
+
}
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
initFieldData = async ({ transaction }) => {
|
|
52
|
+
const { name } = this.options;
|
|
53
|
+
const records = await this.collection.repository.find({
|
|
54
|
+
order: [this.collection.model.primaryKeyAttribute],
|
|
55
|
+
transaction
|
|
56
|
+
});
|
|
57
|
+
for (const record of records) {
|
|
58
|
+
const scope = record.toJSON();
|
|
59
|
+
const result = this.calculate(scope);
|
|
60
|
+
if (result != null) {
|
|
61
|
+
await record.update(
|
|
62
|
+
{
|
|
63
|
+
[name]: result
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
transaction,
|
|
67
|
+
silent: true,
|
|
68
|
+
hooks: false
|
|
69
|
+
}
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
calculateField = async (instance) => {
|
|
75
|
+
const { name } = this.options;
|
|
76
|
+
const result = this.calculate(instance.toJSON());
|
|
77
|
+
instance.set(name, result);
|
|
78
|
+
};
|
|
79
|
+
updateFieldData = async (instance, { transaction }) => {
|
|
80
|
+
if (this.collection.name !== instance.collectionName || instance.name !== this.options.name) {
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
this.options = Object.assign(this.options, instance.options);
|
|
84
|
+
const { name } = this.options;
|
|
85
|
+
const records = await this.collection.repository.find({
|
|
86
|
+
order: [this.collection.model.primaryKeyAttribute],
|
|
87
|
+
transaction
|
|
88
|
+
});
|
|
89
|
+
for (const record of records) {
|
|
90
|
+
const scope = record.toJSON();
|
|
91
|
+
const result = this.calculate(scope);
|
|
92
|
+
await record.update(
|
|
93
|
+
{
|
|
94
|
+
[name]: result
|
|
95
|
+
},
|
|
96
|
+
{
|
|
97
|
+
transaction,
|
|
98
|
+
silent: true,
|
|
99
|
+
hooks: false
|
|
100
|
+
}
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
bind() {
|
|
105
|
+
super.bind();
|
|
106
|
+
this.database.on("fields.afterUpdate", this.updateFieldData);
|
|
107
|
+
this.on("beforeSave", this.calculateField);
|
|
108
|
+
}
|
|
109
|
+
unbind() {
|
|
110
|
+
super.unbind();
|
|
111
|
+
this.off("beforeSave", this.calculateField);
|
|
112
|
+
this.database.off("fields.afterUpdate", this.updateFieldData);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
116
|
+
0 && (module.exports = {
|
|
117
|
+
FormulaField
|
|
118
|
+
});
|
|
@@ -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"));
|
|
@@ -0,0 +1,62 @@
|
|
|
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 alter_field_exports = {};
|
|
19
|
+
__export(alter_field_exports, {
|
|
20
|
+
default: () => alter_field_default
|
|
21
|
+
});
|
|
22
|
+
module.exports = __toCommonJS(alter_field_exports);
|
|
23
|
+
var import_server = require("@nocobase/server");
|
|
24
|
+
class alter_field_default extends import_server.Migration {
|
|
25
|
+
appVersion = "<=0.9.0-alpha.3";
|
|
26
|
+
async up() {
|
|
27
|
+
const result = await this.app.version.satisfies("<=0.9.0-alpha.3");
|
|
28
|
+
if (!result) {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
const { db } = this.context;
|
|
32
|
+
await db.sequelize.transaction(async (transaction) => {
|
|
33
|
+
const Field = db.getRepository("fields");
|
|
34
|
+
const fields = await Field.find({ transaction });
|
|
35
|
+
for (const field of fields) {
|
|
36
|
+
if (["mathFormula", "excelFormula"].includes(field.get("type"))) {
|
|
37
|
+
const { options } = field;
|
|
38
|
+
field.set({
|
|
39
|
+
type: "formula",
|
|
40
|
+
interface: "formula",
|
|
41
|
+
options: {
|
|
42
|
+
...options,
|
|
43
|
+
engine: field.get("type") === "mathFormula" ? "math.js" : "formula.js",
|
|
44
|
+
dataType: options.dataType === "number" ? "double" : "string"
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
await field.save({ transaction });
|
|
48
|
+
const schema = await field.getUiSchema({ transaction });
|
|
49
|
+
schema.set("x-component", "Formula.Result");
|
|
50
|
+
await schema.save({ transaction });
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
const repository = db.getRepository("applicationPlugins");
|
|
54
|
+
await repository.destroy({
|
|
55
|
+
filter: {
|
|
56
|
+
name: ["math-formula-field", "excel-formula-field"]
|
|
57
|
+
},
|
|
58
|
+
transaction
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
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 change_locale_module_exports = {};
|
|
19
|
+
__export(change_locale_module_exports, {
|
|
20
|
+
default: () => change_locale_module_default
|
|
21
|
+
});
|
|
22
|
+
module.exports = __toCommonJS(change_locale_module_exports);
|
|
23
|
+
var import_server = require("@nocobase/server");
|
|
24
|
+
class change_locale_module_default extends import_server.Migration {
|
|
25
|
+
on = "afterLoad";
|
|
26
|
+
// 'beforeLoad' or 'afterLoad'
|
|
27
|
+
appVersion = "<1.0.0-alpha.1";
|
|
28
|
+
async up() {
|
|
29
|
+
const repo = this.db.getRepository("localizationTexts");
|
|
30
|
+
if (!repo) {
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
await repo.update({
|
|
34
|
+
filter: {
|
|
35
|
+
module: "resources.formula-field"
|
|
36
|
+
},
|
|
37
|
+
values: {
|
|
38
|
+
module: "resources.field-formula"
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { InstallOptions, Plugin } from '@nocobase/server';
|
|
2
|
+
export declare class PluginFieldFormulaServer extends Plugin {
|
|
3
|
+
afterAdd(): void;
|
|
4
|
+
beforeLoad(): void;
|
|
5
|
+
load(): Promise<void>;
|
|
6
|
+
install(options?: InstallOptions): Promise<void>;
|
|
7
|
+
afterEnable(): Promise<void>;
|
|
8
|
+
afterDisable(): Promise<void>;
|
|
9
|
+
remove(): Promise<void>;
|
|
10
|
+
}
|
|
11
|
+
export default PluginFieldFormulaServer;
|
|
@@ -0,0 +1,57 @@
|
|
|
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
|
+
PluginFieldFormulaServer: () => PluginFieldFormulaServer,
|
|
21
|
+
default: () => plugin_default
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(plugin_exports);
|
|
24
|
+
var import_server = require("@nocobase/server");
|
|
25
|
+
var import_path = require("path");
|
|
26
|
+
var import_formula_field = require("./formula-field");
|
|
27
|
+
class PluginFieldFormulaServer extends import_server.Plugin {
|
|
28
|
+
afterAdd() {
|
|
29
|
+
}
|
|
30
|
+
beforeLoad() {
|
|
31
|
+
this.db.registerFieldTypes({
|
|
32
|
+
formula: import_formula_field.FormulaField
|
|
33
|
+
});
|
|
34
|
+
this.db.addMigrations({
|
|
35
|
+
namespace: this.name,
|
|
36
|
+
directory: (0, import_path.resolve)(__dirname, "./migrations"),
|
|
37
|
+
context: {
|
|
38
|
+
plugin: this
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
async load() {
|
|
43
|
+
}
|
|
44
|
+
async install(options) {
|
|
45
|
+
}
|
|
46
|
+
async afterEnable() {
|
|
47
|
+
}
|
|
48
|
+
async afterDisable() {
|
|
49
|
+
}
|
|
50
|
+
async remove() {
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
var plugin_default = PluginFieldFormulaServer;
|
|
54
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
55
|
+
0 && (module.exports = {
|
|
56
|
+
PluginFieldFormulaServer
|
|
57
|
+
});
|