Line data Source code
1 : import type { Positionable } from '@comfyorg/graph-model/interfaces'
2 : import type { IContextMenuValue } from '@/lib/litegraph/src/types/contextMenu'
3 : import { LGraphEventMode, LGraphGroup } from '@/lib/litegraph/src/litegraph'
4 : import type { LGraphNode, LGraphCanvas } from '@/lib/litegraph/src/litegraph'
5 : import { useSettingStore } from '@/platform/settings/settingStore'
6 : import type { ComfyExtension } from '@/types/comfy'
7 :
8 0 : function setNodeMode(node: LGraphNode, mode: LGraphEventMode) {
9 0 : node.mode = mode
10 0 : node.graph?.change()
11 : }
12 :
13 2355 : const MODE_MENU_ITEMS = [
14 : { content: 'Set Group Nodes to Always', mode: LGraphEventMode.ALWAYS },
15 : { content: 'Set Group Nodes to Never', mode: LGraphEventMode.NEVER },
16 : { content: 'Bypass Group Nodes', mode: LGraphEventMode.BYPASS }
17 : ] as const
18 :
19 0 : function addNodesToGroup(group: LGraphGroup, items: Iterable<Positionable>) {
20 0 : const padding = useSettingStore().get('Comfy.GroupSelectedNodes.Padding')
21 0 : group.resizeTo([...group.children, ...items], padding)
22 : }
23 :
24 2355 : export const groupOptionsExtension: ComfyExtension = {
25 : name: 'Comfy.GroupOptions',
26 :
27 : getCanvasMenuItems(canvas: LGraphCanvas): (IContextMenuValue | null)[] {
28 32 : const items: (IContextMenuValue | null)[] = []
29 32 : const graph = canvas.graph
30 32 : if (!graph) return items
31 :
32 32 : const group = graph.getGroupOnPos(
33 : canvas.graph_mouse[0],
34 : canvas.graph_mouse[1]
35 : )
36 :
37 32 : if (!group) {
38 31 : if (canvas.selectedItems.size > 0) {
39 14 : items.push({
40 : content: 'Add Group For Selected Nodes',
41 : callback: () => {
42 0 : const group = new LGraphGroup()
43 0 : addNodesToGroup(group, canvas.selectedItems)
44 0 : graph.add(group)
45 0 : graph.change()
46 :
47 0 : group.recomputeInsideNodes()
48 : }
49 : })
50 : }
51 :
52 31 : return items
53 : }
54 :
55 : // Group nodes aren't recomputed until the group is moved, this ensures the nodes are up-to-date
56 1 : group.recomputeInsideNodes()
57 1 : const nodesInGroup = group.nodes
58 :
59 1 : items.push({
60 : content: 'Add Selected Nodes To Group',
61 : disabled: !canvas.selectedItems.size,
62 : callback: () => {
63 0 : addNodesToGroup(group, canvas.selectedItems)
64 0 : graph.change()
65 : }
66 : })
67 :
68 : // No nodes in group, return default options
69 1 : if (nodesInGroup.length === 0) {
70 1 : return items
71 : } else {
72 : // Add a separator between the default options and the group options
73 0 : items.push(null)
74 : }
75 :
76 0 : const firstMode = nodesInGroup[0].mode
77 32 : const sharedMode = nodesInGroup.every((node) => node.mode === firstMode)
78 : ? firstMode
79 : : undefined
80 :
81 32 : items.push({
82 : content: 'Fit Group To Nodes',
83 : callback: () => {
84 0 : group.recomputeInsideNodes()
85 0 : const padding = useSettingStore().get(
86 : 'Comfy.GroupSelectedNodes.Padding'
87 : )
88 0 : group.resizeTo(group.children, padding)
89 0 : graph.change()
90 : }
91 : })
92 :
93 32 : items.push({
94 : content: 'Select Nodes',
95 : callback: () => {
96 0 : canvas.selectNodes(nodesInGroup)
97 0 : graph.change()
98 0 : canvas.canvas.focus()
99 : }
100 : })
101 :
102 32 : for (const { content, mode } of MODE_MENU_ITEMS) {
103 0 : if (mode === sharedMode) continue
104 :
105 0 : items.push({
106 : content,
107 : callback: () => {
108 0 : for (const node of nodesInGroup) {
109 0 : setNodeMode(node, mode)
110 : }
111 : }
112 : })
113 : }
114 :
115 0 : return items
116 : }
117 : }
|