@nocobase/flow-engine
Version:
A standalone flow engine for NocoBase, managing workflows, models, and actions.
208 lines (206 loc) • 9.71 kB
JavaScript
/**
* This file is part of the NocoBase (R) project.
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
* Authors: NocoBase Team.
*
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
* For more information, please refer to: https://www.nocobase.com/agreement.
*/
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var FlowSettings_exports = {};
__export(FlowSettings_exports, {
FlowSettings: () => FlowSettings
});
module.exports = __toCommonJS(FlowSettings_exports);
var import_antd = require("antd");
var import_react = __toESM(require("react"));
var import_react2 = require("@formily/react");
var import_flowContext = require("../../../../flowContext");
var import_hooks = require("../../../../hooks");
var import_utils = require("../../../../utils");
const { Item: FormItem } = import_antd.Form;
const isModelByIdProps = /* @__PURE__ */ __name((props) => {
return "uid" in props && "modelClassName" in props && Boolean(props.uid) && Boolean(props.modelClassName);
}, "isModelByIdProps");
const FlowSettings = /* @__PURE__ */ __name((props) => {
if (isModelByIdProps(props)) {
return /* @__PURE__ */ import_react.default.createElement(FlowSettingsWithModelById, { ...props });
} else {
return /* @__PURE__ */ import_react.default.createElement(FlowSettingsWithModel, { ...props });
}
}, "FlowSettings");
const FlowSettingsWithModel = (0, import_react2.observer)(({ model, flowKey }) => {
if (!model) {
return /* @__PURE__ */ import_react.default.createElement(import_antd.Alert, { message: "\u63D0\u4F9B\u7684\u6A21\u578B\u65E0\u6548", type: "error" });
}
return /* @__PURE__ */ import_react.default.createElement(FlowSettingsContent, { model, flowKey });
});
const FlowSettingsWithModelById = (0, import_react2.observer)(({ uid, flowKey, modelClassName }) => {
const model = (0, import_hooks.useFlowModelById)(uid, modelClassName);
if (!model) {
return /* @__PURE__ */ import_react.default.createElement(import_antd.Alert, { message: `\u672A\u627E\u5230ID\u4E3A ${uid} \u7684\u6A21\u578B`, type: "error" });
}
return /* @__PURE__ */ import_react.default.createElement(FlowSettingsContent, { model, flowKey });
});
const FlowSettingsContent = (0, import_react2.observer)(({ model, flowKey }) => {
const [form] = import_antd.Form.useForm();
const flows = model.getFlows();
const flow = flows.get(flowKey);
const configurableSteps = Object.entries((flow == null ? void 0 : flow.steps) || {}).map(([stepKey, actionStep]) => {
var _a, _b;
if (actionStep.hideInSettings) {
return null;
}
const stepUiSchema = actionStep.uiSchema || {};
let actionUiSchema = {};
if (actionStep.use) {
const action = (_b = (_a = model.flowEngine) == null ? void 0 : _a.getAction) == null ? void 0 : _b.call(_a, actionStep.use);
if (action && action.uiSchema) {
actionUiSchema = action.uiSchema;
}
}
const mergedUiSchema = { ...actionUiSchema };
Object.entries(stepUiSchema).forEach(([fieldKey, schema]) => {
if (mergedUiSchema[fieldKey]) {
mergedUiSchema[fieldKey] = { ...mergedUiSchema[fieldKey], ...schema };
} else {
mergedUiSchema[fieldKey] = schema;
}
});
if (Object.keys(mergedUiSchema).length === 0) {
return null;
}
return { stepKey, step: actionStep, uiSchema: mergedUiSchema };
}).filter(Boolean);
const getCurrentParams = (0, import_react.useCallback)(async () => {
const params = {};
for (const { stepKey, step } of configurableSteps) {
const stepParams = model.getStepParams(flowKey, stepKey) || {};
const flowRuntimeContext = new import_flowContext.FlowRuntimeContext(model, flowKey, "settings");
const flow2 = model.getFlow(flowKey);
(0, import_utils.setupRuntimeContextSteps)(flowRuntimeContext, (flow2 == null ? void 0 : flow2.steps) || {}, model, flowKey);
const resolvedDefaultParams = await (0, import_utils.resolveDefaultParams)(step.defaultParams, flowRuntimeContext);
const mergedParams = { ...resolvedDefaultParams, ...stepParams };
if (Object.keys(mergedParams).length > 0) {
params[stepKey] = mergedParams;
}
}
return params;
}, [model, flowKey, configurableSteps]);
(0, import_react.useEffect)(() => {
const loadParams = /* @__PURE__ */ __name(async () => {
try {
const currentParams = await getCurrentParams();
form.setFieldsValue(currentParams);
} catch (error) {
console.error("Error loading default params:", error);
}
}, "loadParams");
loadParams();
}, [flowKey, form, getCurrentParams]);
const handleValuesChange = (0, import_react.useCallback)(
(changedValues, allValues) => {
Object.entries(allValues).forEach(([stepKey, stepValues]) => {
if (stepValues && typeof stepValues === "object") {
model.setStepParams(flowKey, stepKey, stepValues);
}
});
},
[model, flowKey]
);
if (!flow) {
return /* @__PURE__ */ import_react.default.createElement(import_antd.Alert, { message: `\u672A\u627E\u5230Key\u4E3A ${flowKey} \u7684\u6D41\u7A0B`, type: "error" });
}
if (configurableSteps.length === 0) {
return /* @__PURE__ */ import_react.default.createElement(import_antd.Alert, { message: "\u6B64\u6D41\u7A0B\u6CA1\u6709\u53EF\u914D\u7F6E\u7684\u53C2\u6570", type: "info" });
}
const renderFormFields = /* @__PURE__ */ __name(() => {
return configurableSteps.map(({ stepKey, uiSchema }) => {
return Object.entries(uiSchema).map(([fieldKey, schema]) => {
const fieldName = `${stepKey}.${fieldKey}`;
const renderField = /* @__PURE__ */ __name(() => {
var _a, _b, _c, _d;
switch (schema["x-component"]) {
case "Select":
return /* @__PURE__ */ import_react.default.createElement(
import_antd.Select,
{
placeholder: ((_a = schema["x-component-props"]) == null ? void 0 : _a.placeholder) || `\u8BF7\u9009\u62E9${schema.title}`,
options: schema.enum || [],
...schema["x-component-props"] || {}
}
);
case "InputNumber":
return /* @__PURE__ */ import_react.default.createElement(
import_antd.InputNumber,
{
placeholder: ((_b = schema["x-component-props"]) == null ? void 0 : _b.placeholder) || `\u8BF7\u8F93\u5165${schema.title}`,
...schema["x-component-props"] || {}
}
);
case "Switch":
return /* @__PURE__ */ import_react.default.createElement(import_antd.Switch, { ...schema["x-component-props"] || {} });
case "Input.TextArea":
return /* @__PURE__ */ import_react.default.createElement(
import_antd.Input.TextArea,
{
placeholder: ((_c = schema["x-component-props"]) == null ? void 0 : _c.placeholder) || `\u8BF7\u8F93\u5165${schema.title}`,
...schema["x-component-props"] || {}
}
);
default:
return /* @__PURE__ */ import_react.default.createElement(
import_antd.Input,
{
placeholder: ((_d = schema["x-component-props"]) == null ? void 0 : _d.placeholder) || `\u8BF7\u8F93\u5165${schema.title}`,
...schema["x-component-props"] || {}
}
);
}
}, "renderField");
return /* @__PURE__ */ import_react.default.createElement(
import_antd.Form.Item,
{
key: fieldName,
name: [stepKey, fieldKey],
label: schema.title || fieldKey,
rules: schema.required ? [{ required: true, message: `\u8BF7\u8F93\u5165${schema.title || fieldKey}` }] : []
},
renderField()
);
});
});
}, "renderFormFields");
return /* @__PURE__ */ import_react.default.createElement(import_antd.Form, { form, layout: "vertical", onValuesChange: handleValuesChange }, renderFormFields());
});
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
FlowSettings
});