UNPKG

@nocobase/flow-engine

Version:

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

178 lines (176 loc) 7.42 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 utils_exports = {}; __export(utils_exports, { buildContextSelectorItems: () => buildContextSelectorItems, createDefaultConverters: () => createDefaultConverters, createFinalConverters: () => createFinalConverters, formatPathToValue: () => formatPathToValue, isVariableValue: () => isVariableValue, loadMetaTreeChildren: () => loadMetaTreeChildren, parseValueToPath: () => parseValueToPath, preloadContextSelectorPath: () => preloadContextSelectorPath, searchInLoadedNodes: () => searchInLoadedNodes }); module.exports = __toCommonJS(utils_exports); var import_utils = require("../../utils"); const parseValueToPath = /* @__PURE__ */ __name((value) => { if (typeof value !== "string") return void 0; const trimmed = value.trim(); const variableRegex = /^\{\{\s*ctx(?:\.(.+?))?\s*\}\}$/; const match = trimmed.match(variableRegex); if (!match) return void 0; const pathString = match[1]; if (!pathString) return []; return pathString.split("."); }, "parseValueToPath"); const formatPathToValue = /* @__PURE__ */ __name((item) => { const path = (item == null ? void 0 : item.paths) || []; if (path.length === 0) return "{{ ctx }}"; return `{{ ctx.${path.join(".")} }}`; }, "formatPathToValue"); const loadMetaTreeChildren = /* @__PURE__ */ __name(async (metaNode) => { if (!(metaNode == null ? void 0 : metaNode.children)) return []; if (typeof metaNode.children === "function") { try { return await metaNode.children(); } catch (error) { console.warn(`Failed to load children for ${metaNode.name}:`, error); return []; } } return metaNode.children; }, "loadMetaTreeChildren"); const searchInLoadedNodes = /* @__PURE__ */ __name((options, searchText, parentPaths = []) => { if (!searchText || !searchText.trim()) return []; const lowerSearchText = searchText.toLowerCase().trim(); const results = []; const searchRecursive = /* @__PURE__ */ __name((nodes, currentPath = []) => { var _a; for (const node of nodes) { const nodePath = [...currentPath, node.value]; const labelText = typeof node.label === "string" ? node.label : typeof ((_a = node.meta) == null ? void 0 : _a.title) === "string" ? node.meta.title : String(node.value); if (labelText.toLowerCase().includes(lowerSearchText)) { results.push(node); } if (node.children && Array.isArray(node.children)) { searchRecursive(node.children, nodePath); } } }, "searchRecursive"); searchRecursive(options, []); return results; }, "searchInLoadedNodes"); const buildContextSelectorItems = /* @__PURE__ */ __name((metaTree) => { if (!metaTree || !Array.isArray(metaTree)) { console.warn("buildContextSelectorItems received invalid metaTree:", metaTree); return []; } const convertNode = /* @__PURE__ */ __name((node) => { const hasChildren = !!(node.children && (typeof node.children === "function" || Array.isArray(node.children) && node.children.length > 0)); const disabled = !!(typeof node.disabled === "function" ? node.disabled() : node.disabled); const option = { label: node.title || node.name, value: node.name, isLeaf: !hasChildren, meta: node, paths: node.paths, disabled }; if (Array.isArray(node.children) && node.children.length > 0) { option.children = node.children.map((child) => convertNode(child)); } return option; }, "convertNode"); return metaTree.map((node) => convertNode(node)); }, "buildContextSelectorItems"); const preloadContextSelectorPath = /* @__PURE__ */ __name(async (options, pathSegments, triggerUpdate) => { if (!options || !Array.isArray(options) || !pathSegments || pathSegments.length === 0) return; let list = options; for (let i = 0; i < pathSegments.length && list; i++) { const seg = String(pathSegments[i]); const opt = list.find((o) => String(o.value) === seg); if (!opt) break; const meta = opt.meta; const hasLoaded = !!opt.children && Array.isArray(opt.children); if (i < pathSegments.length - 1 && !hasLoaded && meta && typeof meta.children === "function") { opt.loading = true; try { const childNodes = await loadMetaTreeChildren(meta); meta.children = childNodes; const childOptions = buildContextSelectorItems(childNodes); opt.children = childOptions; opt.isLeaf = !childOptions || childOptions.length === 0; Promise.resolve().then(() => triggerUpdate == null ? void 0 : triggerUpdate()).catch(() => { }); } finally { opt.loading = false; } } list = Array.isArray(opt.children) ? opt.children : void 0; } }, "preloadContextSelectorPath"); const isVariableValue = /* @__PURE__ */ __name((value) => { return (0, import_utils.isVariableExpression)(value); }, "isVariableValue"); const createDefaultConverters = /* @__PURE__ */ __name(() => { return { resolvePathFromValue: /* @__PURE__ */ __name((value) => { return parseValueToPath(value); }, "resolvePathFromValue"), resolveValueFromPath: /* @__PURE__ */ __name((item) => { return formatPathToValue(item); }, "resolveValueFromPath") }; }, "createDefaultConverters"); const createFinalConverters = /* @__PURE__ */ __name((propConverters) => { const defaultConverters = createDefaultConverters(); const mergedConverters = propConverters ? { ...defaultConverters, ...propConverters } : defaultConverters; if (propConverters == null ? void 0 : propConverters.resolveValueFromPath) { const customResolveValueFromPath = propConverters.resolveValueFromPath; return { ...mergedConverters, resolveValueFromPath: /* @__PURE__ */ __name((item) => { const ret = customResolveValueFromPath(item); return ret === void 0 ? formatPathToValue(item) : ret; }, "resolveValueFromPath") }; } return mergedConverters; }, "createFinalConverters"); // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { buildContextSelectorItems, createDefaultConverters, createFinalConverters, formatPathToValue, isVariableValue, loadMetaTreeChildren, parseValueToPath, preloadContextSelectorPath, searchInLoadedNodes });