Line data Source code
1 : import { reactive, readonly } from 'vue'
2 :
3 : import type { LinkDirection } from '@comfyorg/graph-model/globalEnums'
4 : import type { Point2D as Point } from '@comfyorg/shared-frontend-utils/geometry'
5 : import type { SlotLayout } from '@comfyorg/graph-model/layout'
6 : import type { NodeId } from '@comfyorg/graph-model/nodeId'
7 :
8 : /**
9 : * Slot link drag UI state
10 : *
11 : * Reactive, shared state for a single drag interaction that UI components subscribe to.
12 : * Tracks pointer position, source slot, and resolved drop candidate. Also exposes
13 : * a compatibility map used to dim incompatible slots during drag.
14 : */
15 :
16 : type SlotDragType = 'input' | 'output'
17 :
18 : interface SlotDragSource {
19 : nodeId: NodeId
20 : slotIndex: number
21 : type: SlotDragType
22 : direction: LinkDirection
23 : position: Readonly<Point>
24 : linkId?: number
25 : movingExistingOutput?: boolean
26 : }
27 :
28 : export interface SlotDropCandidate {
29 : layout: SlotLayout
30 : compatible: boolean
31 : }
32 :
33 : interface PointerPosition {
34 : client: Point
35 : canvas: Point
36 : }
37 :
38 : interface SlotDragState {
39 : active: boolean
40 : pointerId: number | null
41 : source: SlotDragSource | null
42 : pointer: PointerPosition
43 : candidate: SlotDropCandidate | null
44 : compatible: Map<string, boolean>
45 : }
46 :
47 2355 : const state = reactive<SlotDragState>({
48 : active: false,
49 : pointerId: null,
50 : source: null,
51 : pointer: {
52 : client: { x: 0, y: 0 },
53 : canvas: { x: 0, y: 0 }
54 : },
55 : candidate: null,
56 : compatible: new Map<string, boolean>()
57 : })
58 :
59 498 : function updatePointerPosition(
60 : clientX: number,
61 : clientY: number,
62 : canvasX: number,
63 : canvasY: number
64 : ) {
65 498 : state.pointer.client.x = clientX
66 498 : state.pointer.client.y = clientY
67 498 : state.pointer.canvas.x = canvasX
68 498 : state.pointer.canvas.y = canvasY
69 : }
70 :
71 68 : function setCandidate(candidate: SlotDropCandidate | null) {
72 68 : state.candidate = candidate
73 : }
74 :
75 65 : function beginDrag(source: SlotDragSource, pointerId: number) {
76 65 : state.active = true
77 65 : state.source = source
78 65 : state.pointerId = pointerId
79 65 : state.candidate = null
80 65 : state.compatible.clear()
81 : }
82 :
83 65 : function endDrag() {
84 65 : state.active = false
85 65 : state.pointerId = null
86 65 : state.source = null
87 65 : state.pointer.client.x = 0
88 65 : state.pointer.client.y = 0
89 65 : state.pointer.canvas.x = 0
90 65 : state.pointer.canvas.y = 0
91 65 : state.candidate = null
92 65 : state.compatible.clear()
93 : }
94 :
95 71724 : export function useSlotLinkDragUIState() {
96 71724 : return {
97 : state: readonly(state),
98 : beginDrag,
99 : endDrag,
100 : updatePointerPosition,
101 : setCandidate,
102 : setCompatibleMap: (entries: Iterable<[string, boolean]>) => {
103 0 : state.compatible.clear()
104 0 : for (const [key, value] of entries) state.compatible.set(key, value)
105 : },
106 : setCompatibleForKey: (key: string, value: boolean) => {
107 850 : state.compatible.set(key, value)
108 : },
109 : clearCompatible: () => state.compatible.clear()
110 : }
111 : }
|