@nocobase/flow-engine
Version:
A standalone flow engine for NocoBase, managing workflows, models, and actions.
207 lines (205 loc) • 8.7 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 StepSettingsDialog_exports = {};
__export(StepSettingsDialog_exports, {
openStepSettingsDialog: () => openStepSettingsDialog
});
module.exports = __toCommonJS(StepSettingsDialog_exports);
var import_core = require("@formily/core");
var import_react = require("@formily/react");
var import_reactive = require("@formily/reactive");
var import_antd = require("antd");
var import_react2 = __toESM(require("react"));
var import_useFlowSettingsContext = require("../../../../hooks/useFlowSettingsContext");
var import_utils = require("../../../../utils");
const SchemaField = (0, import_react.createSchemaField)();
const openStepSettingsDialog = /* @__PURE__ */ __name(async ({
model,
flowKey,
stepKey,
dialogWidth = 600,
dialogTitle,
mode = "dialog",
ctx,
uiModeProps,
cleanup
}) => {
var _a, _b, _c, _d;
const t = (0, import_utils.getT)(model);
const message = model.context.message;
if (!model) {
message.error(t("Invalid model provided"));
throw new Error(t("Invalid model provided"));
}
const flow = model.getFlow(flowKey);
const step = (_a = flow == null ? void 0 : flow.steps) == null ? void 0 : _a[stepKey];
if (!flow) {
message.error(t("Flow with key {{flowKey}} not found", { flowKey }));
throw new Error(t("Flow with key {{flowKey}} not found", { flowKey }));
}
if (!step) {
message.error(t("Step with key {{stepKey}} not found", { stepKey }));
throw new Error(t("Step with key {{stepKey}} not found", { stepKey }));
}
let title = step.title;
let beforeParamsSave = step.beforeParamsSave;
let afterParamsSave = step.afterParamsSave;
let actionDefaultParams = {};
if (step.use) {
const action = (_c = (_b = model.flowEngine) == null ? void 0 : _b.getAction) == null ? void 0 : _c.call(_b, step.use);
if (action) {
actionDefaultParams = action.defaultParams || {};
title = title || action.title;
beforeParamsSave = beforeParamsSave || action.beforeParamsSave;
afterParamsSave = afterParamsSave || action.afterParamsSave;
}
}
const flowDefinition = model.getFlow(flowKey);
const mergedUiSchema = await (0, import_utils.resolveStepUiSchema)(model, flowDefinition, step);
if (!mergedUiSchema) {
message.info(t("This step has no configurable parameters"));
return {};
}
const flowRuntimeContext = ctx;
if (!flowRuntimeContext.currentStep || flowRuntimeContext.currentStep !== step) {
flowRuntimeContext.defineProperty("currentStep", { value: step });
}
const flowEngine = model.flowEngine;
const scopes = {
useFlowSettingsContext: import_useFlowSettingsContext.useFlowSettingsContext,
...(_d = flowEngine.flowSettings) == null ? void 0 : _d.scopes
};
const stepParams = model.getStepParams(flowKey, stepKey) || {};
const resolvedDefaultParams = await (0, import_utils.resolveDefaultParams)(step.defaultParams, flowRuntimeContext);
const resolveActionDefaultParams = await (0, import_utils.resolveDefaultParams)(actionDefaultParams, flowRuntimeContext);
const initialValues = { ...(0, import_reactive.toJS)(resolveActionDefaultParams), ...(0, import_reactive.toJS)(resolvedDefaultParams), ...(0, import_reactive.toJS)(stepParams) };
const previousParams = { ...(0, import_reactive.toJS)(stepParams) };
const formSchema = {
type: "object",
properties: {
layout: {
type: "void",
"x-component": "FormLayout",
"x-component-props": {
layout: "vertical"
// 垂直布局
},
properties: mergedUiSchema
}
}
};
const openView = model.context.viewer[mode].bind(model.context.viewer);
const form = (0, import_core.createForm)({
initialValues: (0, import_utils.compileUiSchema)(scopes, initialValues)
});
openView({
title: dialogTitle || t(title),
width: dialogWidth,
destroyOnClose: true,
...(0, import_reactive.toJS)(uiModeProps),
onClose: /* @__PURE__ */ __name(() => {
if (cleanup) {
cleanup();
}
}, "onClose"),
content: /* @__PURE__ */ __name((currentDialog) => {
const FlowStepSettings = (0, import_react.observer)(() => {
var _a2;
(0, import_react2.useEffect)(() => {
return (0, import_reactive.autorun)(() => {
const dynamicProps = (0, import_reactive.toJS)(uiModeProps);
currentDialog.update(dynamicProps);
});
}, []);
const compiledFormSchema = (0, import_utils.compileUiSchema)(scopes, formSchema);
return /* @__PURE__ */ import_react2.default.createElement(import_react.FormProvider, { form }, /* @__PURE__ */ import_react2.default.createElement(import_useFlowSettingsContext.FlowSettingsContextProvider, { value: flowRuntimeContext }, /* @__PURE__ */ import_react2.default.createElement(
SchemaField,
{
schema: compiledFormSchema,
components: {
...(_a2 = flowEngine.flowSettings) == null ? void 0 : _a2.components
},
scope: scopes
}
), /* @__PURE__ */ import_react2.default.createElement(currentDialog.Footer, null, /* @__PURE__ */ import_react2.default.createElement(import_antd.Space, { align: "end" }, /* @__PURE__ */ import_react2.default.createElement(
import_antd.Button,
{
type: "default",
onClick: () => {
currentDialog.close();
}
},
t("Cancel")
), /* @__PURE__ */ import_react2.default.createElement(
import_antd.Button,
{
type: "primary",
onClick: async () => {
try {
await form.submit();
const currentValues = form.values;
model.setStepParams(flowKey, stepKey, currentValues);
if (beforeParamsSave) {
await beforeParamsSave(flowRuntimeContext, currentValues, previousParams);
}
currentDialog.close();
await model.saveStepParams();
message.success(t("Configuration saved"));
if (afterParamsSave) {
await afterParamsSave(flowRuntimeContext, currentValues, previousParams);
}
} catch (error) {
if (error instanceof import_utils.FlowExitException) {
currentDialog.close();
return;
}
console.error(t("Error saving configuration"), ":", error);
message.error(t("Error saving configuration, please check console"));
}
}
},
t("OK")
)))));
});
return /* @__PURE__ */ import_react2.default.createElement(FlowStepSettings, null);
}, "content")
});
}, "openStepSettingsDialog");
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
openStepSettingsDialog
});