Line data Source code
1 : <template>
2 3547 : <RovingFocusGroup
3 : as="div"
4 : orientation="vertical"
5 : :loop="true"
6 : data-slot="node-search-categories"
7 : class="group/categories flex min-h-0 flex-col overflow-y-auto py-2.5 select-none"
8 : >
9 : <!-- Preset categories -->
10 : <div v-if="!hidePresets" class="flex flex-col px-3">
11 3671 : <RovingFocusItem
12 : v-for="preset in topCategories"
13 : :key="preset.id"
14 : as-child
15 : >
16 : <Button
17 : type="button"
18 : :data-testid="`category-${preset.id}`"
19 : :aria-current="selectedCategory === preset.id || undefined"
20 : :class="categoryBtnClass(preset.id)"
21 : @click="selectCategory(preset.id)"
22 : >
23 : {{ preset.label }}
24 : </Button>
25 : </RovingFocusItem>
26 : </div>
27 :
28 : <!-- Category tree -->
29 : <div
30 : role="tree"
31 : :aria-label="t('g.category')"
32 : :class="
33 : cn(
34 : 'flex flex-col px-3',
35 : !hidePresets && 'mt-2 border-t border-border/50 pt-2'
36 : )
37 : "
38 : >
39 62177 : <NodeSearchCategoryTreeNode
40 : v-for="category in categoryTree"
41 : :key="category.key"
42 : :node="category"
43 : :selected-category="selectedCategory"
44 : :expanded-category="expandedCategory"
45 : :hide-chevrons="hideChevrons"
46 : @select="selectCategory"
47 : @collapse="collapseCategory"
48 : />
49 : </div>
50 : </RovingFocusGroup>
51 : </template>
52 :
53 : <script lang="ts">
54 2355 : export const DEFAULT_CATEGORY = 'most-relevant'
55 : </script>
56 :
57 : <script setup lang="ts">
58 : import { computed, ref, watch } from 'vue'
59 : import { useI18n } from 'vue-i18n'
60 : import { RovingFocusGroup, RovingFocusItem } from 'reka-ui'
61 :
62 : import NodeSearchCategoryTreeNode, {
63 : CATEGORY_SELECTED_CLASS,
64 : CATEGORY_UNSELECTED_CLASS
65 : } from '@/components/searchbox/v2/NodeSearchCategoryTreeNode.vue'
66 : import Button from '@comfyorg/design-system/button/Button'
67 : import type { CategoryNode } from '@/components/searchbox/v2/NodeSearchCategoryTreeNode.vue'
68 : import { nodeOrganizationService } from '@/services/nodeOrganizationService'
69 : import type { ComfyNodeDefImpl } from '@/core/graph/nodeDef/ComfyNodeDefImpl'
70 : import { useNodeDefStore } from '@/stores/nodeDefStore'
71 : import type { TreeNode } from '@/types/treeExplorerTypes'
72 : import { cn } from '@comfyorg/tailwind-utils'
73 :
74 : const {
75 : hideChevrons = false,
76 : hidePresets = false,
77 : nodeDefs,
78 : rootLabel,
79 : rootKey
80 : } = defineProps<{
81 : hideChevrons?: boolean
82 : hidePresets?: boolean
83 : nodeDefs?: ComfyNodeDefImpl[]
84 : rootLabel?: string
85 : rootKey?: string
86 : }>()
87 :
88 109 : const selectedCategory = defineModel<string>('selectedCategory', {
89 : required: true
90 : })
91 :
92 109 : const emit = defineEmits<{
93 : autoExpand: [key: string]
94 : }>()
95 :
96 109 : const { t } = useI18n()
97 109 : const nodeDefStore = useNodeDefStore()
98 :
99 109 : const topCategories = computed(() => [
100 : { id: DEFAULT_CATEGORY, label: t('g.mostRelevant') }
101 : ])
102 :
103 109 : const categoryTree = computed<CategoryNode[]>(() => {
104 3314 : const defs = nodeDefs ?? nodeDefStore.visibleNodeDefs
105 3314 : const tree = nodeOrganizationService.organizeNodes(defs, {
106 : groupBy: 'category'
107 : })
108 :
109 3314 : const stripRootPrefix = (key: string) => key.replace(/^root\//, '')
110 :
111 845091 : function mapNode(node: TreeNode<ComfyNodeDefImpl>): CategoryNode {
112 845091 : const children = node.children?.filter((child) => !child.leaf).map(mapNode)
113 845091 : return {
114 : key: stripRootPrefix(node.key),
115 : label: node.label,
116 : ...(children?.length ? { children } : {})
117 : }
118 : }
119 :
120 3314 : const nodes = (tree.children ?? []).filter((node) => !node.leaf).map(mapNode)
121 :
122 3314 : if (rootLabel && nodes.length > 1) {
123 2 : const key = rootKey ?? rootLabel.toLowerCase()
124 428 : function prefixKeys(node: CategoryNode): CategoryNode {
125 428 : return {
126 : key: key + '/' + node.key,
127 : label: node.label,
128 : ...(node.children?.length
129 : ? { children: node.children.map(prefixKeys) }
130 : : {})
131 : }
132 : }
133 2 : return [{ key, label: rootLabel, children: nodes.map(prefixKeys) }]
134 : }
135 :
136 3312 : return nodes
137 : })
138 :
139 : // Notify parent when there is only a single root category to auto-expand
140 109 : watch(
141 : categoryTree,
142 : (nodes) => {
143 3314 : if (nodes.length === 1 && nodes[0].children?.length) {
144 4 : const rootKey = nodes[0].key
145 4 : if (
146 : selectedCategory.value !== rootKey &&
147 : !selectedCategory.value.startsWith(rootKey + '/')
148 : ) {
149 4 : emit('autoExpand', rootKey)
150 : }
151 : }
152 : },
153 : { immediate: true }
154 : )
155 :
156 3672 : function categoryBtnClass(id: string) {
157 3672 : return cn(
158 : 'h-auto justify-start bg-transparent py-2.5 pr-3 text-sm font-normal',
159 : hideChevrons ? 'pl-3' : 'pl-9',
160 : selectedCategory.value === id
161 : ? CATEGORY_SELECTED_CLASS
162 : : CATEGORY_UNSELECTED_CLASS
163 : )
164 : }
165 :
166 109 : const expandedCategory = ref(selectedCategory.value)
167 : // Skips the watch when selectCategory/collapseCategory set selectedCategory,
168 : // so their expandedCategory toggle isn't immediately undone.
169 109 : let lastEmittedCategory = ''
170 :
171 109 : watch(selectedCategory, (val) => {
172 18 : if (val !== lastEmittedCategory) {
173 4 : expandedCategory.value = val
174 : }
175 18 : lastEmittedCategory = ''
176 : })
177 :
178 1 : function parentCategory(key: string): string {
179 1 : const i = key.lastIndexOf('/')
180 1 : return i > 0 ? key.slice(0, i) : ''
181 : }
182 :
183 15 : function selectCategory(categoryId: string) {
184 15 : if (expandedCategory.value === categoryId) {
185 1 : expandedCategory.value = parentCategory(categoryId)
186 : } else {
187 14 : expandedCategory.value = categoryId
188 : }
189 15 : lastEmittedCategory = categoryId
190 15 : selectedCategory.value = categoryId
191 : }
192 :
193 0 : function collapseCategory(categoryId: string) {
194 0 : expandedCategory.value = parentCategory(categoryId)
195 0 : lastEmittedCategory = categoryId
196 0 : selectedCategory.value = categoryId
197 : }
198 : </script>
|