UNPKG

@nocobase/flow-engine

Version:

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

160 lines (158 loc) 6.37 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 __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 usePopover_exports = {}; __export(usePopover_exports, { usePopover: () => usePopover }); module.exports = __toCommonJS(usePopover_exports); var import_antd = require("antd"); var import_dompurify = __toESM(require("dompurify")); var React = __toESM(require("react")); var import_usePatchElement = __toESM(require("./usePatchElement")); let uuid = 0; const PopoverComponent = React.forwardRef(({ afterClose, content, placement, rect, ...props }, ref) => { var _a, _b; const [visible, setVisible] = React.useState(true); const [config, setConfig] = React.useState({ content, placement, rect, ...props }); React.useImperativeHandle(ref, () => ({ destroy: /* @__PURE__ */ __name(() => setVisible(false), "destroy"), update: /* @__PURE__ */ __name((newConfig) => setConfig((prev) => ({ ...prev, ...newConfig })), "update"), close: /* @__PURE__ */ __name((result, _force) => setVisible(false), "close") })); React.useEffect(() => { if (!visible) { afterClose == null ? void 0 : afterClose(); } }, [visible, afterClose]); return /* @__PURE__ */ React.createElement( import_antd.Popover, { arrow: false, open: visible, trigger: ["click"], destroyTooltipOnHide: true, content: config.content, placement: config.placement, getPopupContainer: () => document.body, onOpenChange: (nextOpen) => { setVisible(nextOpen); if (!nextOpen) { afterClose == null ? void 0 : afterClose(); } }, ...config }, /* @__PURE__ */ React.createElement( "span", { style: { position: "absolute", top: (((_a = config.rect) == null ? void 0 : _a.top) ?? 0) + window.scrollY, left: (((_b = config.rect) == null ? void 0 : _b.left) ?? 0) + window.scrollX, width: 0, height: 0 } } ) ); }); function usePopover() { const holderRef = React.useRef(null); const open = /* @__PURE__ */ __name((config) => { var _a, _b; uuid += 1; const { target, placement = "rightTop", content, ...rest } = config; const popoverRef = React.createRef(); const rect = ((_a = target == null ? void 0 : target.getBoundingClientRect) == null ? void 0 : _a.call(target)) ?? { top: 0, left: 0 }; let closeFunc; let resolvePromise; const promise = new Promise((resolve) => { resolvePromise = resolve; }); const currentPopover = { type: "popover", destroy: /* @__PURE__ */ __name(() => { var _a2; return (_a2 = popoverRef.current) == null ? void 0 : _a2.destroy(); }, "destroy"), update: /* @__PURE__ */ __name((newConfig) => { var _a2; return (_a2 = popoverRef.current) == null ? void 0 : _a2.update(newConfig); }, "update"), close: /* @__PURE__ */ __name((result) => { var _a2; resolvePromise == null ? void 0 : resolvePromise(result); (_a2 = popoverRef.current) == null ? void 0 : _a2.close(result); }, "close") }; const rawChildren = typeof content === "function" ? content(currentPopover) : content; const children = typeof rawChildren === "string" ? /* @__PURE__ */ React.createElement("div", { dangerouslySetInnerHTML: { __html: import_dompurify.default.sanitize(rawChildren) } }) : rawChildren; const popover = /* @__PURE__ */ React.createElement( PopoverComponent, { key: `popover-${uuid}`, ref: popoverRef, content: children, placement, rect, afterClose: () => { var _a2; closeFunc == null ? void 0 : closeFunc(); (_a2 = config.onClose) == null ? void 0 : _a2.call(config); resolvePromise == null ? void 0 : resolvePromise(config.result); }, ...rest } ); closeFunc = (_b = holderRef.current) == null ? void 0 : _b.patchElement(popover); return Object.assign(promise, currentPopover); }, "open"); const api = React.useMemo(() => ({ open }), []); const ElementsHolder = React.memo( React.forwardRef((props, ref) => { const [elements, patchElement] = (0, import_usePatchElement.default)(); React.useImperativeHandle(ref, () => ({ patchElement }), []); return /* @__PURE__ */ React.createElement(React.Fragment, null, elements); }) ); return [api, /* @__PURE__ */ React.createElement(ElementsHolder, { key: "popover-holder", ref: holderRef })]; } __name(usePopover, "usePopover"); // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { usePopover });