Line data Source code
1 : import type { MaybeRefOrGetter } from 'vue'
2 : import { computed, toValue } from 'vue'
3 :
4 : import type { LGraphGroup } from '@/lib/litegraph/src/LGraphGroup'
5 : import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode'
6 : import { isLGraphGroup, isLGraphNode } from '@/utils/litegraphUtil'
7 : import type { Positionable } from '@comfyorg/graph-model/interfaces'
8 :
9 : type MixedSelectionItem = LGraphGroup | LGraphNode
10 : type FlatAndCategorizeSelectedItemsResult = {
11 : all: MixedSelectionItem[]
12 : nodes: LGraphNode[]
13 : groups: LGraphGroup[]
14 : others: Positionable[]
15 : nodeToParentGroup: Map<LGraphNode, LGraphGroup>
16 : }
17 :
18 : type FlatItemsContext = {
19 : nodeToParentGroup: Map<LGraphNode, LGraphGroup>
20 : depth: number
21 : parentGroup?: LGraphGroup
22 : }
23 :
24 : /** Flattens selected groups into their contents and splits the result into nodes, groups, and unsupported items. */
25 407 : export function flatAndCategorizeSelectedItems(
26 : items: Positionable[]
27 : ): FlatAndCategorizeSelectedItemsResult {
28 407 : const ctx: FlatItemsContext = {
29 : nodeToParentGroup: new Map<LGraphNode, LGraphGroup>(),
30 : depth: 0
31 : }
32 407 : const { all, nodes, groups, others } = flatItems(items, ctx)
33 407 : return {
34 : all: repeatItems(all),
35 : nodes: repeatItems(nodes),
36 : groups: repeatItems(groups),
37 : others: repeatItems(others),
38 : nodeToParentGroup: ctx.nodeToParentGroup
39 : }
40 : }
41 :
42 201 : export function useFlatAndCategorizeSelectedItems(
43 : items: MaybeRefOrGetter<Positionable[]>
44 : ) {
45 201 : const result = computed(() => flatAndCategorizeSelectedItems(toValue(items)))
46 :
47 201 : return {
48 : flattedItems: computed(() => result.value.all),
49 : selectedNodes: computed(() => result.value.nodes),
50 : selectedGroups: computed(() => result.value.groups),
51 : selectedOthers: computed(() => result.value.others),
52 : nodeToParentGroup: computed(() => result.value.nodeToParentGroup)
53 : }
54 : }
55 :
56 407 : function flatItems(
57 : items: Positionable[],
58 : ctx: FlatItemsContext
59 : ): Omit<FlatAndCategorizeSelectedItemsResult, 'nodeToParentGroup'> {
60 407 : const result: MixedSelectionItem[] = []
61 407 : const nodes: LGraphNode[] = []
62 407 : const groups: LGraphGroup[] = []
63 407 : const others: Positionable[] = []
64 :
65 407 : if (ctx.depth > 1000) {
66 0 : return {
67 : all: [],
68 : nodes: [],
69 : groups: [],
70 : others: []
71 : }
72 : }
73 :
74 407 : for (let i = 0; i < items.length; i++) {
75 190 : const item = items[i]
76 :
77 190 : if (isLGraphGroup(item)) {
78 0 : result.push(item)
79 0 : groups.push(item)
80 :
81 : const children = Array.from(item.children)
82 : const childCtx: FlatItemsContext = {
83 : nodeToParentGroup: ctx.nodeToParentGroup,
84 : depth: ctx.depth + 1,
85 : parentGroup: item
86 : }
87 0 : const {
88 : all: childAll,
89 : nodes: childNodes,
90 : groups: childGroups,
91 : others: childOthers
92 : } = flatItems(children, childCtx)
93 0 : result.push(...childAll)
94 0 : nodes.push(...childNodes)
95 0 : groups.push(...childGroups)
96 0 : others.push(...childOthers)
97 190 : } else if (isLGraphNode(item)) {
98 190 : result.push(item)
99 190 : nodes.push(item)
100 190 : if (ctx.parentGroup) {
101 0 : ctx.nodeToParentGroup.set(item, ctx.parentGroup)
102 : }
103 : } else {
104 0 : others.push(item)
105 : }
106 : }
107 407 : return {
108 : all: result,
109 : nodes,
110 : groups,
111 : others
112 : }
113 : }
114 :
115 1628 : function repeatItems<T>(items: T[]): T[] {
116 1628 : const itemSet = new Set<T>()
117 1628 : const result: T[] = []
118 1628 : for (const item of items) {
119 380 : if (itemSet.has(item)) continue
120 380 : itemSet.add(item)
121 380 : result.push(item)
122 : }
123 1628 : return result
124 : }
|