diff --git a/ui/src/workflow-canvas/nodes/loop-body-node/index.vue b/ui/src/workflow-canvas/nodes/loop-body-node/index.vue index 706fcd34463..eb20e46eb8a 100644 --- a/ui/src/workflow-canvas/nodes/loop-body-node/index.vue +++ b/ui/src/workflow-canvas/nodes/loop-body-node/index.vue @@ -163,8 +163,37 @@ const renderGraphData = (data?: LogicFlow.GraphConfigData) => { lfInstance.render(data ?? {}) } +// 嵌套画布初始化时按 getBoundingClientRect 取尺寸,会受主画布缩放影响,折叠时还会取到 0; +// 且容器尺寸变化(展开、放大)不会触发 LogicFlow 自身的 resize。 +// 这里监听容器布局尺寸(clientWidth/clientHeight 不受 transform 影响),同步画布尺寸并把内容居中。 +let containerResizeObserver: ResizeObserver | null = null +let centerTimer: ReturnType | undefined + +function centerLoopBody() { + const container = containerRef.value + const lfInstance = lf.value + if (!container || !lfInstance) return + const { clientWidth, clientHeight } = container + if (clientWidth <= 0 || clientHeight <= 0) return + lfInstance.resize(clientWidth, clientHeight) + lfInstance.translateCenter() +} + +function scheduleCenterLoopBody() { + if (centerTimer) clearTimeout(centerTimer) + // 等待嵌套节点完成高度测量后再居中 + centerTimer = setTimeout(() => { + centerTimer = undefined + centerLoopBody() + }, 200) +} + onMounted(() => { renderGraphData(cloneDeep(model.properties.workflow as LogicFlow.GraphConfigData)) + if (containerRef.value) { + containerResizeObserver = new ResizeObserver(scheduleCenterLoopBody) + containerResizeObserver.observe(containerRef.value) + } set(model, 'setLoopBody', setLoopBody) set(model, 'set_loop_body', setLoopBody) set(model, 'validate', validate) @@ -174,6 +203,9 @@ onMounted(() => { }) onUnmounted(() => { + containerResizeObserver?.disconnect() + containerResizeObserver = null + if (centerTimer) clearTimeout(centerTimer) disconnectByFlow(lf.value?.graphModel.flowId ?? '') lf.value = null })