Line data Source code
1 : <template>
2 : <TreeItem
3 : v-slot="{ isExpanded, handleToggle, handleSelect }"
4 : :value="item.value"
5 : :level="item.level"
6 : as-child
7 : >
8 : <!-- Node -->
9 : <div
10 : v-if="item.value.type === 'node'"
11 : v-bind="$attrs"
12 : :class="cn(treeItemClass, 'overflow-hidden')"
13 : :style="treeItemIndent(item.level)"
14 : draggable="true"
15 : @click.stop="handleClick($event, handleToggle, handleSelect)"
16 : @contextmenu="handleContextMenu"
17 : @pointerdown="handleContextMenu"
18 : @mouseenter="handleMouseEnter"
19 : @mouseleave="handleMouseLeave"
20 : @dragstart="handleDragStart"
21 : @dragend="handleDragEnd"
22 : >
23 : <i class="icon-[comfy--node] text-muted-foreground" />
24 : <span class="min-w-0 flex-1 truncate">
25 : <slot name="node" :node="item.value">
26 : {{ item.value.label }}
27 : </slot>
28 : </span>
29 : <div class="flex shrink-0 items-center gap-0.5">
30 : <Button
31 : v-if="isUserBlueprint"
32 : variant="muted-textonly"
33 : size="icon-sm"
34 : class="opacity-0 group-hover/tree-node:opacity-100"
35 : :aria-label="$t('g.delete')"
36 : @click.stop="deleteBlueprint"
37 : >
38 : <i class="icon-[lucide--trash-2] bg-destructive" />
39 : </Button>
40 : <Button
41 : v-if="isUserBlueprint"
42 : variant="muted-textonly"
43 : size="icon-sm"
44 : class="opacity-0 group-hover/tree-node:opacity-100"
45 : :aria-label="$t('g.edit')"
46 : @click.stop="editBlueprint"
47 : >
48 : <i class="icon-[lucide--square-pen]" />
49 : </Button>
50 : <Button
51 : variant="muted-textonly"
52 : size="icon-sm"
53 : class="opacity-0 group-hover/tree-node:opacity-100"
54 : :aria-label="$t('icon.bookmark')"
55 : @click.stop="toggleBookmark"
56 : >
57 : <i :class="isBookmarked ? 'pi pi-bookmark-fill' : 'pi pi-bookmark'" />
58 : </Button>
59 : </div>
60 : </div>
61 :
62 : <!-- Folder -->
63 : <div
64 : v-else
65 : v-bind="$attrs"
66 : :class="cn(treeItemClass, 'overflow-hidden')"
67 : :style="treeItemIndent(item.level)"
68 : @click.stop="handleClick($event, handleToggle, handleSelect)"
69 : @contextmenu="clearContextMenuNode"
70 : @pointerdown="clearContextMenuNode"
71 : >
72 : <i
73 : v-if="item.hasChildren"
74 : :class="
75 : cn(
76 : 'icon-[lucide--chevron-down] text-muted-foreground transition-transform',
77 : !isExpanded && '-rotate-90'
78 : )
79 : "
80 : />
81 : <i :class="cn(item.value.icon, 'text-muted-foreground')" />
82 : <span class="min-w-0 flex-1 truncate">
83 : <slot name="folder" :node="item.value">
84 : {{ item.value.label }}
85 : </slot>
86 : </span>
87 : </div>
88 : </TreeItem>
89 :
90 : <Teleport
91 : v-if="showPreview && item.value.type === 'node' && item.value.data"
92 : to="body"
93 : >
94 : <div ref="previewRef" :style="nodePreviewStyle">
95 : <NodePreviewCard :node-def="item.value.data as ComfyNodeDefImpl" />
96 : </div>
97 : </Teleport>
98 : </template>
99 :
100 : <script setup lang="ts">
101 : import type { FlattenedItem } from 'reka-ui'
102 : import { TreeItem } from 'reka-ui'
103 : import { computed, inject, useTemplateRef } from 'vue'
104 :
105 : import NodePreviewCard from '@/components/node/NodePreviewCard.vue'
106 : import { useNodePreviewAndDrag } from '@/composables/node/useNodePreviewAndDrag'
107 : import Button from '@comfyorg/design-system/button/Button'
108 : import {
109 : treeItemClass,
110 : treeItemIndent
111 : } from '@comfyorg/design-system/lib/tree/treeStyles'
112 : import { useNodeBookmarkStore } from '@/stores/nodeBookmarkStore'
113 : import type { ComfyNodeDefImpl } from '@/core/graph/nodeDef/ComfyNodeDefImpl'
114 : import { useSubgraphStore } from '@/stores/subgraphStore'
115 : import { InjectKeyContextMenuNode } from '@/types/treeExplorerTypes'
116 : import type { RenderedTreeExplorerNode } from '@/types/treeExplorerTypes'
117 : import { cn } from '@comfyorg/tailwind-utils'
118 :
119 : defineOptions({
120 : inheritAttrs: false
121 : })
122 :
123 : const { item } = defineProps<{
124 : item: FlattenedItem<RenderedTreeExplorerNode<ComfyNodeDefImpl>>
125 : }>()
126 :
127 750 : const emit = defineEmits<{
128 : nodeClick: [
129 : node: RenderedTreeExplorerNode<ComfyNodeDefImpl>,
130 : event: MouseEvent
131 : ]
132 : }>()
133 :
134 750 : const contextMenuNode = inject(InjectKeyContextMenuNode)
135 750 : const nodeBookmarkStore = useNodeBookmarkStore()
136 750 : const subgraphStore = useSubgraphStore()
137 750 : const previewRef = useTemplateRef('previewRef')
138 :
139 750 : const nodeDef = computed(() => item.value.data)
140 :
141 750 : const isBookmarked = computed(() => {
142 49 : if (!nodeDef.value) return false
143 49 : return nodeBookmarkStore.isBookmarked(nodeDef.value)
144 : })
145 :
146 750 : const isUserBlueprint = computed(() =>
147 : subgraphStore.isUserBlueprint(nodeDef.value?.name)
148 : )
149 :
150 0 : function toggleBookmark() {
151 0 : if (nodeDef.value) {
152 0 : nodeBookmarkStore.toggleBookmark(nodeDef.value)
153 : }
154 : }
155 :
156 0 : function deleteBlueprint() {
157 0 : if (nodeDef.value) {
158 0 : void subgraphStore.deleteBlueprint(nodeDef.value.name)
159 : }
160 : }
161 750 : const editBlueprint = async () => {
162 2 : if (!nodeDef.value) {
163 0 : console.error('Failed to edit subgraph blueprint lacking backing node data')
164 0 : return
165 : }
166 2 : await useSubgraphStore().editBlueprint(nodeDef.value.name)
167 : }
168 :
169 750 : const {
170 : showPreview,
171 : nodePreviewStyle,
172 : handleMouseEnter: baseHandleMouseEnter,
173 : handleMouseLeave,
174 : handleDragStart: baseHandleDragStart,
175 : handleDragEnd
176 : } = useNodePreviewAndDrag(nodeDef, previewRef)
177 :
178 16 : function handleClick(
179 : e: MouseEvent,
180 : handleToggle: () => void,
181 : handleSelect: () => void
182 : ) {
183 16 : handleSelect()
184 16 : if (item.value.type === 'folder') {
185 15 : handleToggle()
186 : }
187 16 : emit('nodeClick', item.value, e)
188 : }
189 :
190 8 : function handleContextMenu() {
191 8 : if (contextMenuNode) {
192 8 : contextMenuNode.value = item.value
193 : }
194 : }
195 :
196 15 : function clearContextMenuNode() {
197 15 : if (contextMenuNode) {
198 15 : contextMenuNode.value = null
199 : }
200 : }
201 :
202 9 : function handleMouseEnter(e: MouseEvent) {
203 9 : if (item.value.type !== 'node') return
204 9 : baseHandleMouseEnter(e)
205 : }
206 :
207 1 : function handleDragStart(e: DragEvent) {
208 1 : if (item.value.type !== 'node' || !item.value.data) return
209 1 : baseHandleDragStart(e)
210 : }
211 : </script>
|