UNPKG

@logicflow/vue-node-registry

Version:

LogicFlow Vue Component Node Registry

129 lines 5.11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.connect = connect; exports.disconnect = disconnect; exports.isActive = isActive; exports.getTeleport = getTeleport; exports.createTeleportContainer = createTeleportContainer; exports.destroyTeleportContainer = destroyTeleportContainer; var vue_demi_1 = require("vue-demi"); var active = false; var appInstances = new Map(); var appNodesMap = new Map(); // 用于储存当前流程图节点id当节点都销毁时同时卸载vueApp实例 var items = (0, vue_demi_1.reactive)({}); function connect(id, component, container, node, graph) { if (active) { if (graph.isMiniMap) { createTeleportContainer(container, graph.flowId); } items[id] = (0, vue_demi_1.markRaw)((0, vue_demi_1.defineComponent)({ render: function () { return (0, vue_demi_1.h)(vue_demi_1.Teleport, { to: container }, [ (0, vue_demi_1.h)(component, { node: node, graph: graph }), ]); }, provide: function () { return ({ getNode: function () { return node; }, getGraph: function () { return graph; }, }); }, })); } } function disconnect(id, flowId) { if (active) { delete items[id]; if (appInstances.has(flowId)) { var appNodeList = appNodesMap.get(flowId) || []; var index = appNodeList.indexOf(id); if (index > -1) { appNodeList.splice(index, 1); if (appNodeList.length === 0) { destroyTeleportContainer(flowId); } else { appNodesMap.set(flowId, appNodeList); } } } } } function isActive() { return active; } function getTeleport() { if (!vue_demi_1.isVue3) { throw new Error('teleport is only available in Vue3'); } active = true; return (0, vue_demi_1.defineComponent)({ props: { flowId: { type: String, required: true, }, }, setup: function (props) { return function () { var children = []; Object.keys(items).forEach(function (id) { // https://github.com/didi/LogicFlow/issues/1768 // 多个不同的VueNodeView都会connect注册到items中,因此items存储了可能有多个flowId流程图的数据 // 当使用多个LogicFlow时,会创建多个flowId + 同时使用KeepAlive // 每一次items改变,会触发不同flowId持有的setup()执行,由于每次setup()执行就是遍历items,因此存在多次重复渲染元素的问题 // 即items[0]会在Page1的setup()执行,items[0]也会在Page2的setup()执行,从而生成两个items[0] // 比对当前界面显示的flowId,只更新items[当前页面flowId:nodeId]的数据 // 比如items[0]属于Page1的数据,那么Page2无论active=true/false,都无法执行items[0] if (id.startsWith(props.flowId)) { if (appInstances.has(props.flowId)) { var appNodeList = appNodesMap.get(props.flowId) || []; if (!appNodeList.includes(id)) { appNodeList.push(id); appNodesMap.set(props.flowId, appNodeList); } } children.push(items[id]); } }); return (0, vue_demi_1.h)(vue_demi_1.Fragment, {}, children.map(function (item) { return (0, vue_demi_1.h)(item); })); }; }, }); } /** * 创建并挂载 Teleport 容器组件 * @param container 目标容器元素 * @param flowId 当前流程图的唯一标识 */ function createTeleportContainer(container, flowId) { if (!vue_demi_1.isVue3 || !flowId || !container || !active) return; // 获取 Teleport 组件 var TeleportContainer = getTeleport(); // 不重新创建 Teleport 容器组件 if (appInstances.has(flowId)) { return; } // ✅ 1. 创建独立容器放到目标容器中 var mountPoint = document.createElement('div'); container.appendChild(mountPoint); // ✅ 2. 创建并挂载 Vue 应用到新容器 var app = (0, vue_demi_1.createApp)(TeleportContainer, { flowId: flowId }); app.mount(mountPoint); appInstances.set(flowId, app); appNodesMap.set(flowId, []); } /** * 卸载 Teleport 容器组件 * @param flowId 需要卸载流程图的唯一标识 */ function destroyTeleportContainer(flowId) { if (!vue_demi_1.isVue3 || !flowId || !active) return; var app = appInstances.get(flowId); if (app) { app.unmount(); appInstances.delete(flowId); appNodesMap.delete(flowId); } } //# sourceMappingURL=teleport.js.map