Line data Source code
1 : import type { InjectionKey } from 'vue'
2 :
3 : import type { LGraphGroup, LGraphNode } from '@/lib/litegraph/src/litegraph'
4 : import { containsCentre, containsRect } from '@/lib/litegraph/src/measure'
5 :
6 : /** Provides the group a node is listed under in the panel, or null. */
7 2355 : export const GetNodeParentGroupKey: InjectionKey<
8 : (node: LGraphNode) => LGraphGroup | null
9 : > = Symbol('getNodeParentGroup')
10 :
11 : /** The innermost, then smallest, group whose bounds contain the node's centre. */
12 79 : export function findParentGroup(
13 : groups: readonly LGraphGroup[],
14 : node: LGraphNode
15 : ): LGraphGroup | null {
16 79 : let parent: LGraphGroup | null = null
17 :
18 79 : for (const group of groups) {
19 0 : const groupRect = group.boundingRect
20 0 : if (!containsCentre(groupRect, node.boundingRect)) continue
21 :
22 0 : if (!parent) {
23 0 : parent = group
24 0 : continue
25 : }
26 :
27 0 : const parentRect = parent.boundingRect
28 0 : const candidateInsideParent = containsRect(parentRect, groupRect)
29 0 : const parentInsideCandidate = containsRect(groupRect, parentRect)
30 :
31 0 : if (candidateInsideParent && !parentInsideCandidate) {
32 0 : parent = group
33 0 : continue
34 : }
35 :
36 : const candidateArea = groupRect[2] * groupRect[3]
37 : const parentArea = parentRect[2] * parentRect[3]
38 :
39 0 : if (candidateArea < parentArea) parent = group
40 : }
41 :
42 79 : return parent
43 : }
|