Line data Source code
1 : import { LiteGraph } from '@/lib/litegraph/src/litegraph'
2 : import { layoutStore } from '@/renderer/core/layout/store/layoutStore'
3 : import type { SlotOffset, SlotOffsetMode } from '@comfyorg/graph-model/layout'
4 : import type { NodeId } from '@comfyorg/graph-model/nodeId'
5 : import { parseSlotId } from '@comfyorg/graph-model/slotId'
6 : import type { UUID } from '@/utils/uuid'
7 :
8 26404 : export function syncSlotOffsets(
9 : element: HTMLElement,
10 : graphId: UUID,
11 : nodeId: NodeId
12 : ): void {
13 26404 : const nodeElement = element.closest<HTMLElement>('[data-node-id]') ?? element
14 26404 : const mode: SlotOffsetMode =
15 : nodeElement.dataset.collapsed === undefined ? 'expanded' : 'collapsed'
16 26404 : const slotElements =
17 : nodeElement.querySelectorAll<HTMLElement>('[data-slot-key]')
18 26404 : if (slotElements.length === 0) {
19 71 : layoutStore.updateNodeSlotOffsets(graphId, nodeId, [], mode)
20 71 : return
21 : }
22 :
23 26333 : const nodeRect = nodeElement.getBoundingClientRect()
24 26404 : const scale = nodeElement.offsetWidth
25 : ? nodeRect.width / nodeElement.offsetWidth
26 : : 0
27 26404 : if (scale <= 0) return
28 :
29 26101 : const offsets: SlotOffset[] = []
30 26404 : for (const slotElement of slotElements) {
31 116113 : const slotId = parseSlotId(slotElement.dataset.slotKey ?? '')
32 116113 : if (!slotId || slotId.nodeId !== nodeId) continue
33 :
34 116113 : const slotRect = slotElement.getBoundingClientRect()
35 116113 : if (slotRect.width <= 0 || slotRect.height <= 0) continue
36 :
37 116113 : offsets.push({
38 : index: slotId.index,
39 : type: slotId.direction,
40 : position: {
41 : x:
42 : mode === 'collapsed'
43 : ? (slotRect.left + slotRect.width / 2 - nodeRect.x) / scale
44 : : slotId.direction === 'input'
45 : ? 0
46 : : nodeElement.offsetWidth,
47 : y:
48 : (slotRect.top + slotRect.height / 2 - nodeRect.y) / scale -
49 : LiteGraph.NODE_TITLE_HEIGHT
50 : }
51 : })
52 : }
53 26101 : layoutStore.updateNodeSlotOffsets(graphId, nodeId, offsets, mode)
54 : }
|