UNPKG

@nocobase/flow-engine

Version:

A standalone flow engine for NocoBase, managing workflows, models, and actions.

162 lines (160 loc) 5.55 kB
/** * 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 __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; 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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var schema_utils_exports = {}; __export(schema_utils_exports, { compileUiSchema: () => compileUiSchema, resolveStepUiSchema: () => resolveStepUiSchema, resolveUiMode: () => resolveUiMode }); module.exports = __toCommonJS(schema_utils_exports); var import_json_schema = require("@formily/json-schema"); var import_flowContext = require("../flowContext"); var import_setupRuntimeContextSteps = require("./setupRuntimeContextSteps"); async function resolveUiSchema(uiSchema, ctx) { if (!uiSchema) { return {}; } if (typeof uiSchema === "function") { try { const result = await uiSchema(ctx); return result || {}; } catch (error) { console.error("Error resolving uiSchema function:", error); return {}; } } return uiSchema; } __name(resolveUiSchema, "resolveUiSchema"); async function resolveUiMode(uiMode, ctx) { if (!uiMode) { return "dialog"; } if (typeof uiMode === "function") { try { const result = await uiMode(ctx); return result || "dialog"; } catch (error) { console.error("Error resolving uiMode function:", error); return "dialog"; } } return uiMode; } __name(resolveUiMode, "resolveUiMode"); const compileCache = {}; function compileUiSchema(scope, uiSchema, options = {}) { const { noCache = false } = options; const hasVariable = /* @__PURE__ */ __name((source) => { const reg = /\{\{.*?\}\}/g; return reg.test(source); }, "hasVariable"); const compile = /* @__PURE__ */ __name((source) => { let shouldCompile = false; let cacheKey; if (typeof source === "string" && source.startsWith("{{")) { shouldCompile = true; cacheKey = source; } if (source && typeof source === "object" && !Array.isArray(source)) { try { cacheKey = JSON.stringify(source); } catch (e) { console.warn("Failed to stringify:", e); return source; } if (compileCache[cacheKey]) return compileCache[cacheKey]; shouldCompile = hasVariable(cacheKey); } if (Array.isArray(source)) { try { cacheKey = JSON.stringify(source); } catch (e) { console.warn("Failed to stringify:", e); return source; } if (compileCache[cacheKey]) return compileCache[cacheKey]; shouldCompile = hasVariable(cacheKey); } if (shouldCompile) { if (!cacheKey) { try { return import_json_schema.Schema.compile(source, scope); } catch (error) { console.warn("Failed to compile with Formily Schema.compile:", error); return source; } } try { if (noCache) { return import_json_schema.Schema.compile(source, scope); } compileCache[cacheKey] = compileCache[cacheKey] || import_json_schema.Schema.compile(source, scope); return compileCache[cacheKey]; } catch (e) { console.log("compileUiSchema error", source, e); try { return import_json_schema.Schema.compile(source, scope); } catch (error) { return source; } } } return source; }, "compile"); return compile(uiSchema); } __name(compileUiSchema, "compileUiSchema"); async function resolveStepUiSchema(model, flow, step) { var _a, _b; const flowRuntimeContext = new import_flowContext.FlowRuntimeContext(model, flow.key, "settings"); (0, import_setupRuntimeContextSteps.setupRuntimeContextSteps)(flowRuntimeContext, flow.steps, model, flow.key); flowRuntimeContext.defineProperty("currentStep", { value: step }); let stepUiSchema = step.uiSchema; if (step.use) { try { const action = (_b = (_a = model.flowEngine) == null ? void 0 : _a.getAction) == null ? void 0 : _b.call(_a, step.use); if (action && action.uiSchema) { stepUiSchema = stepUiSchema || action.uiSchema; } } catch (error) { console.warn(`Failed to get action ${step.use}:`, error); } } const resolvedStepUiSchema = await resolveUiSchema(stepUiSchema || {}, flowRuntimeContext); if (Object.keys(resolvedStepUiSchema).length === 0) { return null; } return resolvedStepUiSchema; } __name(resolveStepUiSchema, "resolveStepUiSchema"); // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { compileUiSchema, resolveStepUiSchema, resolveUiMode });