UNPKG

@nocobase/flow-engine

Version:

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

218 lines (216 loc) 9.4 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 __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 __typeError = (msg) => { throw TypeError(msg); }; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; 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 __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg); var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value); var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method); var FlowRunJSContext_exports = {}; __export(FlowRunJSContext_exports, { FlowRunJSContext: () => FlowRunJSContext }); module.exports = __toCommonJS(FlowRunJSContext_exports); var antd = __toESM(require("antd")); var import_react = __toESM(require("react")); var ReactDOMClient = __toESM(require("react-dom/client")); var _FlowRunJSContext_instances, exposeProps_fn, exposeMethods_fn; const classMeta = /* @__PURE__ */ new WeakMap(); const classDocCache = /* @__PURE__ */ new WeakMap(); function deepMerge(base, patch) { if (patch === null) return void 0; if (Array.isArray(base) || Array.isArray(patch) || typeof base !== "object" || typeof patch !== "object") { return patch ?? base; } const out = { ...base }; for (const k of Object.keys(patch)) { const v = deepMerge(base == null ? void 0 : base[k], patch[k]); if (typeof v === "undefined") delete out[k]; else out[k] = v; } return out; } __name(deepMerge, "deepMerge"); const _FlowRunJSContext = class _FlowRunJSContext { constructor(delegate) { __privateAdd(this, _FlowRunJSContext_instances); __publicField(this, "_delegate"); this._delegate = delegate; const self = this; self.React = import_react.default; self.react = import_react.default; self.antd = antd; self.ReactDOM = ReactDOMClient; self.dispatchModelEvent = async (modelOrUid, eventName, inputArgs) => { var _a, _b, _c, _d, _e, _f, _g, _h; let model = null; const engine = this._delegate.engine; if (typeof modelOrUid === "string") { model = await ((_a = engine == null ? void 0 : engine.loadModel) == null ? void 0 : _a.call(engine, { uid: modelOrUid })); } else if (modelOrUid && typeof modelOrUid === "object" && typeof modelOrUid.dispatchEvent === "function") { model = modelOrUid; } if (model) { (_c = (_b = model.context) == null ? void 0 : _b.addDelegate) == null ? void 0 : _c.call(_b, self); model.dispatchEvent(eventName, { navigation: false, ...((_e = (_d = self.model) == null ? void 0 : _d["getInputArgs"]) == null ? void 0 : _e.call(_d)) || {}, ...inputArgs || {} }); } else { (_h = (_f = self.message) == null ? void 0 : _f.error) == null ? void 0 : _h.call( _f, ((_g = self.t) == null ? void 0 : _g.call(self, "Model with ID {{uid}} not found", { uid: String(modelOrUid) })) || "Model not found" ); } }; __privateMethod(this, _FlowRunJSContext_instances, exposeProps_fn).call(this, [ "message", "notification", "logger", "resource", "urlSearchParams", "token", "role", "auth", "api", "ref", "model" ]); __privateMethod(this, _FlowRunJSContext_instances, exposeMethods_fn).call(this, ["t", "requireAsync", "copyToClipboard", "resolveJsonTemplate", "runAction", "onRefReady"]); } // 供子类/外部一致使用的定义 API(与 FlowContext 的接口保持一致风格) defineProperty(key, options) { if (options && typeof options.get === "function") { Object.defineProperty(this, key, { configurable: true, enumerable: true, get: /* @__PURE__ */ __name(() => { var _a; return (_a = options.get) == null ? void 0 : _a.call(options, this); }, "get") }); return; } Object.defineProperty(this, key, { configurable: true, enumerable: true, writable: false, value: options == null ? void 0 : options.value }); } defineMethod(name, fn) { Object.defineProperty(this, name, { configurable: true, enumerable: false, writable: false, value: fn.bind(this) }); } static define(meta) { const prev = classMeta.get(this) || {}; classMeta.set(this, deepMerge(prev, meta)); classDocCache.delete(this); } static getDoc() { const self = this; if (classDocCache.has(self)) return classDocCache.get(self); const chain = []; let cur = self; while (cur && cur.prototype) { chain.unshift(cur); cur = Object.getPrototypeOf(cur); } let merged = {}; for (const cls of chain) merged = deepMerge(merged, classMeta.get(cls) || {}); classDocCache.set(self, merged); return merged; } }; _FlowRunJSContext_instances = new WeakSet(); // 工具:将委托上的属性以 getter 暴露 exposeProps_fn = /* @__PURE__ */ __name(function(names) { for (const k of names) { if (Object.prototype.hasOwnProperty.call(this, k)) continue; this.defineProperty(k, { get: /* @__PURE__ */ __name(() => this._delegate[k], "get") }); } }, "#exposeProps"); // 工具:将委托上的同名方法绑定暴露 exposeMethods_fn = /* @__PURE__ */ __name(function(names) { for (const k of names) { if (Object.prototype.hasOwnProperty.call(this, k)) continue; const src = this._delegate[k]; if (typeof src === "function") { this.defineMethod(k, src.bind(this._delegate)); } } }, "#exposeMethods"); __name(_FlowRunJSContext, "FlowRunJSContext"); __publicField(_FlowRunJSContext, "allow"); let FlowRunJSContext = _FlowRunJSContext; FlowRunJSContext.define({ label: "RunJS base", properties: { t: "\u56FD\u9645\u5316\u51FD\u6570\u3002\u793A\u4F8B\uFF1A`ctx.t('Hello {name}', { name: 'World' })`", logger: "Pino logger \u5B50\u5B9E\u4F8B\u3002`ctx.logger.info({ foo: 1 }, 'msg')`", message: "AntD \u5168\u5C40\u6D88\u606F\u3002`ctx.message.success('done')`", notification: "AntD \u901A\u77E5\u3002`ctx.notification.open({ message: 'Hi' })`", requireAsync: "\u5F02\u6B65\u52A0\u8F7D\u5916\u90E8\u5E93\u3002`const x = await ctx.requireAsync(url)`", copyToClipboard: "\u590D\u5236\u6587\u672C\u5230\u526A\u8D34\u677F\u3002`await ctx.copyToClipboard(text)`", resolveJsonTemplate: "\u89E3\u6790\u542B {{ }} \u7684\u6A21\u677F/\u8868\u8FBE\u5F0F", runAction: "\u8FD0\u884C\u5F53\u524D\u6A21\u578B\u52A8\u4F5C\u3002`await ctx.runAction(name, params)`", resource: "\u6570\u636E\u8D44\u6E90\uFF08\u6309\u59D4\u6258\u53EF\u89C1\uFF09", urlSearchParams: "URL \u67E5\u8BE2\u53C2\u6570\u5BF9\u8C61", token: "API Token", role: "\u5F53\u524D\u89D2\u8272", auth: "\u8BA4\u8BC1\u4FE1\u606F\uFF08locale/role/user/token\uFF09", api: "APIClient \u5B9E\u4F8B", React: "React \u547D\u540D\u7A7A\u95F4\uFF08RunJS \u73AF\u5883\u53EF\u7528\uFF09", react: "React \u522B\u540D\uFF08\u5C0F\u5199\uFF09", ReactDOM: "ReactDOM \u5BA2\u6237\u7AEF\uFF08\u542B createRoot\uFF09", antd: "AntD \u7EC4\u4EF6\u5E93\uFF08RunJS \u73AF\u5883\u53EF\u7528\uFF09" }, methods: { dispatchModelEvent: "\u89E6\u53D1\u6A21\u578B\u4E8B\u4EF6\uFF1A`await ctx.dispatchModelEvent(modelUid, 'click', { ... })`" } }); // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { FlowRunJSContext });