From d6f951145cb13cfdac58d6feb8ffb99233d5c670 Mon Sep 17 00:00:00 2001 From: huangmin <2927933426@qq.com> Date: Fri, 29 May 2026 16:33:43 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E8=8A=82=E7=82=B9?= =?UTF-8?q?=E6=B5=8B=E9=87=8F=E5=8A=9F=E8=83=BD=E5=B9=B6=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E7=BC=96=E8=BE=91=E6=93=8D=E4=BD=9C=E6=A0=B7=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/PopiaiEmbedBridge.tsx | 46 +++++++++++++++++++++++++++- src/lib/chat/editOperations.ts | 2 +- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/src/components/PopiaiEmbedBridge.tsx b/src/components/PopiaiEmbedBridge.tsx index 51be11d1..e163fa0a 100644 --- a/src/components/PopiaiEmbedBridge.tsx +++ b/src/components/PopiaiEmbedBridge.tsx @@ -15,7 +15,8 @@ type BridgeRequest = | "popitv:apply-edit-operations" | "popitv:run-workflow" | "popitv:run-selected" - | "popitv:stop-workflow"; + | "popitv:stop-workflow" + | "popitv:measure-nodes"; interface BridgeMessage { source?: string; @@ -25,6 +26,41 @@ interface BridgeMessage { operations?: EditOperation[]; } +interface NodeDimensions { + id: string; + width: number; + height: number; +} + +function measureNodes(nodeIds: string[]): NodeDimensions[] { + const state = useWorkflowStore.getState(); + const measurements: NodeDimensions[] = []; + + for (const nodeId of nodeIds) { + const node = state.nodes.find(n => n.id === nodeId); + if (node?.measured) { + measurements.push({ + id: nodeId, + width: node.measured.width ?? 0, + height: node.measured.height ?? 0, + }); + } else { + // 如果 measured 不存在,尝试从 DOM 获取 + const nodeElement = document.querySelector(`[data-id="${nodeId}"]`); + if (nodeElement) { + const rect = nodeElement.getBoundingClientRect(); + measurements.push({ + id: nodeId, + width: rect.width, + height: rect.height, + }); + } + } + } + + return measurements; +} + function isSafeParentOrigin(origin: string | null): origin is string { if (!origin) return false; return origin === "file://" || /^https?:\/\/(localhost|127\.0\.0\.1|\[::1\])(:\d+)?$/.test(origin); @@ -258,6 +294,14 @@ export function PopiaiEmbedBridge() { ); }); postSnapshot(requestId); + return; + } + + if (event.data.type === "popitv:measure-nodes") { + const nodeIds = event.data.nodeIds ?? []; + const measurements = measureNodes(nodeIds); + postToParent("popitv:node-dimensions", { measurements }, requestId); + return; } }; diff --git a/src/lib/chat/editOperations.ts b/src/lib/chat/editOperations.ts index 601b7dd1..27b4635b 100644 --- a/src/lib/chat/editOperations.ts +++ b/src/lib/chat/editOperations.ts @@ -101,7 +101,7 @@ export function applyEditOperations( type: operation.nodeType, position, data: nodeData, - measured: dimensions, + style: { width: dimensions.width, height: dimensions.height }, }; nodes.push(newNode);