LCOV - code coverage report
Current view: top level - src/components/sidebar/tabs/nodeLibrary - NodeBookmarkTreeExplorer.vue Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 86.3 % 73 63
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : <template>
       2              :   <TreeExplorer
       3              :     ref="treeExplorerRef"
       4              :     v-model:expanded-keys="expandedKeys"
       5              :     class="node-lib-bookmark-tree-explorer"
       6              :     data-testid="node-library-bookmark-tree"
       7              :     :root="renderedBookmarkedRoot"
       8              :   >
       9              :     <template #node="{ node }">
      10              :       <TreeExplorerTreeNode :node>
      11              :         <template #before-label>
      12              :           <Badge v-if="node.data?.experimental" severity="primary">
      13              :             {{ $t('g.experimental') }}
      14              :           </Badge>
      15              :           <Badge v-if="node.data?.deprecated" severity="danger">
      16              :             {{ $t('g.deprecated') }}
      17              :           </Badge>
      18              :         </template>
      19              :         <template v-if="node.data" #actions>
      20              :           <template v-if="subgraphStore.isUserBlueprint(node.data.name)">
      21              :             <Button
      22              :               variant="destructive"
      23              :               size="icon-sm"
      24              :               :aria-label="$t('g.delete')"
      25              :               @click.stop="void subgraphStore.deleteBlueprint(node.data.name)"
      26              :             >
      27              :               <i class="icon-[lucide--trash-2]" />
      28              :             </Button>
      29              :             <Button
      30              :               variant="muted-textonly"
      31              :               size="icon-sm"
      32              :               :aria-label="$t('g.edit')"
      33              :               @click.stop="void subgraphStore.editBlueprint(node.data.name)"
      34              :             >
      35              :               <i class="icon-[lucide--square-pen]" />
      36              :             </Button>
      37              :           </template>
      38              :           <template v-else>
      39              :             <Button
      40              :               variant="muted-textonly"
      41              :               size="icon-sm"
      42              :               :aria-label="$t('icon.bookmark')"
      43              :               @click.stop="void nodeBookmarkStore.toggleBookmark(node.data)"
      44              :             >
      45              :               <i
      46              :                 :class="
      47              :                   cn(
      48              :                     nodeBookmarkStore.isBookmarked(node.data)
      49              :                       ? 'pi pi-bookmark-fill'
      50              :                       : 'pi pi-bookmark',
      51              :                     'size-3.5'
      52              :                   )
      53              :                 "
      54              :               />
      55              :             </Button>
      56              :             <Button
      57              :               :tooltip="$t('g.learnMore')"
      58              :               tooltip-side="bottom"
      59              :               variant="muted-textonly"
      60              :               size="icon-sm"
      61              :               :aria-label="$t('g.learnMore')"
      62              :               @click.stop="openNodeHelp(node.data)"
      63              :             >
      64              :               <i class="pi pi-question size-3.5" />
      65              :             </Button>
      66              :           </template>
      67              :         </template>
      68              :       </TreeExplorerTreeNode>
      69              :     </template>
      70              :     <template #preview="{ node }">
      71              :       <NodePreview v-if="node.data" :node-def="node.data" />
      72              :     </template>
      73              :   </TreeExplorer>
      74              : 
      75              :   <FolderCustomizationDialog
      76              :     v-model="showCustomizationDialog"
      77              :     :initial-icon="initialIcon"
      78              :     :initial-color="initialColor"
      79              :     @confirm="updateCustomization"
      80              :   />
      81              : </template>
      82              : 
      83              : <script setup lang="ts">
      84              : import { computed, getCurrentInstance, nextTick, ref, watch } from 'vue'
      85              : import { useI18n } from 'vue-i18n'
      86              : 
      87              : import FolderCustomizationDialog from '@/components/common/CustomizationDialog.vue'
      88              : import TreeExplorer from '@/components/common/TreeExplorer.vue'
      89              : import TreeExplorerTreeNode from '@/components/common/TreeExplorerTreeNode.vue'
      90              : import NodePreview from '@/components/node/NodePreview.vue'
      91              : import { renderNodePreview } from '@/components/node/renderNodePreview'
      92              : import Badge from '@comfyorg/design-system/badge/Badge'
      93              : import Button from '@comfyorg/design-system/button/Button'
      94              : import { useTreeExpansion } from '@/composables/useTreeExpansion'
      95              : import { withNodeAddSource } from '@/platform/telemetry/nodeAdded/nodeAddSource'
      96              : import { useLitegraphService } from '@/services/litegraphService'
      97              : import { useNodeBookmarkStore } from '@/stores/nodeBookmarkStore'
      98              : import type { ComfyNodeDefImpl } from '@/core/graph/nodeDef/ComfyNodeDefImpl'
      99              : import { useSubgraphStore } from '@/stores/subgraphStore'
     100              : import type {
     101              :   RenderedTreeExplorerNode,
     102              :   TreeExplorerDragAndDropData,
     103              :   TreeExplorerNode,
     104              :   TreeNode
     105              : } from '@/types/treeExplorerTypes'
     106              : import { cn } from '@comfyorg/tailwind-utils'
     107              : 
     108              : const instance = getCurrentInstance()!
     109           56 : const appContext = instance.appContext
     110              : const { filteredNodeDefs, openNodeHelp } = defineProps<{
     111              :   filteredNodeDefs: ComfyNodeDefImpl[]
     112              :   openNodeHelp: (nodeDef: ComfyNodeDefImpl) => void
     113              : }>()
     114              : 
     115           56 : const expandedKeys = ref<Record<string, boolean>>({})
     116           56 : const { expandNode, toggleNodeOnEvent } = useTreeExpansion(expandedKeys)
     117              : 
     118           56 : const nodeBookmarkStore = useNodeBookmarkStore()
     119           56 : const subgraphStore = useSubgraphStore()
     120           56 : const bookmarkedRoot = computed<TreeNode<ComfyNodeDefImpl>>(() => {
     121           86 :   const filterTree = (
     122              :     node: TreeNode<ComfyNodeDefImpl>
     123              :   ): TreeNode<ComfyNodeDefImpl> | null => {
     124            7 :     if (node.leaf) {
     125            2 :       if (!node.data) return null
     126            2 :       const nodeData = node.data
     127              :       // Check if the node's display_name is in the filteredNodeDefs list
     128            2 :       return filteredNodeDefs.some((def) => def.name === nodeData.name)
     129              :         ? node
     130              :         : null
     131              :     }
     132              : 
     133            7 :     const filteredChildren = node.children
     134              :       ?.map(filterTree)
     135              :       .filter((child): child is TreeNode<ComfyNodeDefImpl> => child !== null)
     136              : 
     137            7 :     if (filteredChildren && filteredChildren.length > 0) {
     138            2 :       return {
     139              :         ...node,
     140              :         children: filteredChildren
     141              :       }
     142              :     }
     143              : 
     144            3 :     return null // Remove empty folders
     145              :   }
     146              : 
     147           86 :   return filteredNodeDefs.length
     148              :     ? filterTree(nodeBookmarkStore.bookmarkedRoot) || {
     149              :         key: 'root',
     150              :         label: 'Root',
     151              :         children: []
     152              :       }
     153              :     : nodeBookmarkStore.bookmarkedRoot
     154              : })
     155           56 : watch(
     156              :   () => filteredNodeDefs,
     157              :   async (newValue) => {
     158            3 :     if (newValue.length) {
     159            3 :       await nextTick()
     160            3 :       expandNode(bookmarkedRoot.value)
     161              :     }
     162              :   }
     163              : )
     164              : 
     165           56 : const { t } = useI18n()
     166           56 : const extraMenuItems = (
     167              :   menuTargetNode: RenderedTreeExplorerNode<ComfyNodeDefImpl>
     168              : ) => [
     169              :   {
     170              :     label: t('g.customize'),
     171              :     icon: 'icon-[lucide--palette]',
     172              :     command: () => {
     173            3 :       if (!menuTargetNode.data) return
     174              : 
     175            3 :       const customization =
     176              :         nodeBookmarkStore.bookmarksCustomization[menuTargetNode.data.nodePath]
     177            3 :       initialIcon.value =
     178              :         customization?.icon || nodeBookmarkStore.defaultBookmarkIcon
     179            3 :       initialColor.value =
     180              :         customization?.color || nodeBookmarkStore.defaultBookmarkColor
     181              : 
     182            3 :       showCustomizationDialog.value = true
     183            3 :       customizationTargetNodePath.value = menuTargetNode.data.nodePath
     184              :     },
     185              :     visible: !menuTargetNode?.leaf
     186              :   }
     187              : ]
     188              : 
     189          268 : function getCustomization(node: TreeNode<ComfyNodeDefImpl>) {
     190          268 :   return node.data
     191              :     ? nodeBookmarkStore.bookmarksCustomization[node.data.nodePath]
     192              :     : undefined
     193              : }
     194              : 
     195           56 : const renderedBookmarkedRoot = computed<TreeExplorerNode<ComfyNodeDefImpl>>(
     196              :   () => {
     197           86 :     const fillNodeInfo = (
     198              :       node: TreeNode<ComfyNodeDefImpl>
     199              :     ): TreeExplorerNode<ComfyNodeDefImpl> => {
     200              :       const children = node.children?.map(fillNodeInfo)
     201              : 
     202              :       // Sort children: non-leaf nodes first, then leaf nodes, both alphabetically
     203          114 :       const sortedChildren = children?.sort((a, b) => {
     204            2 :         if (a.leaf === b.leaf) {
     205            0 :           return a.label.localeCompare(b.label)
     206              :         }
     207            2 :         return a.leaf ? 1 : -1
     208              :       })
     209              : 
     210          114 :       return {
     211              :         key: node.key,
     212              :         label: node.leaf ? (node.data?.display_name ?? node.label) : node.label,
     213              :         leaf: node.leaf,
     214              :         data: node.data,
     215              :         getIcon() {
     216          144 :           if (this.leaf) {
     217           10 :             return 'pi pi-circle-fill'
     218              :           }
     219          134 :           const customization = getCustomization(node)
     220          144 :           return customization?.icon
     221              :             ? 'pi ' + customization.icon
     222              :             : 'pi pi-bookmark-fill'
     223              :         },
     224              :         getIconColor() {
     225          144 :           return this.leaf ? undefined : getCustomization(node)?.color
     226              :         },
     227              :         children: sortedChildren,
     228              :         draggable: node.leaf,
     229              :         async handleAddFolder(newName: string) {
     230            2 :           if (newName !== '') {
     231            2 :             await nodeBookmarkStore.addNewBookmarkFolder(this.data, newName)
     232              :           }
     233              :         },
     234              :         renderDragPreview(container) {
     235            0 :           if (!node.data) return
     236            0 :           return renderNodePreview(container, node.data, appContext)
     237              :         },
     238              :         droppable: !node.leaf,
     239              :         async handleDrop(data: TreeExplorerDragAndDropData<ComfyNodeDefImpl>) {
     240            1 :           const folderNodeDef = node.data
     241            1 :           const nodeDefToAdd = data.data.data
     242            1 :           if (!folderNodeDef || !nodeDefToAdd) return
     243              :           // Remove bookmark if the source is the top level bookmarked node.
     244            1 :           if (nodeBookmarkStore.isBookmarked(nodeDefToAdd)) {
     245            0 :             await nodeBookmarkStore.toggleBookmark(nodeDefToAdd)
     246              :           }
     247            1 :           const nodePath = folderNodeDef.category + '/' + nodeDefToAdd.name
     248            1 :           await nodeBookmarkStore.addBookmark(nodePath)
     249              :         },
     250              :         handleClick(e: MouseEvent) {
     251            0 :           const nodeDef = this.data
     252            0 :           if (this.leaf && nodeDef) {
     253            0 :             withNodeAddSource('sidebar_drag', () =>
     254              :               useLitegraphService().addNodeOnGraph(nodeDef)
     255              :             )
     256              :           } else {
     257            0 :             toggleNodeOnEvent(e, node)
     258              :           }
     259              :         },
     260              :         contextMenuItems: extraMenuItems,
     261              :         ...(node.leaf
     262              :           ? node.data && subgraphStore.isUserBlueprint(node.data.name)
     263              :             ? {
     264              :                 async handleDelete() {
     265            0 :                   if (this.data) {
     266            0 :                     await subgraphStore.deleteBlueprint(this.data.name)
     267              :                   }
     268              :                 }
     269              :               }
     270              :             : {}
     271              :           : {
     272              :               async handleRename(newName: string) {
     273            2 :                 if (this.data && this.data.isDummyFolder) {
     274            2 :                   await nodeBookmarkStore.renameBookmarkFolder(
     275              :                     this.data,
     276              :                     newName
     277              :                   )
     278              :                 }
     279              :               },
     280              :               async handleDelete() {
     281            2 :                 if (!this.data) return
     282            2 :                 await nodeBookmarkStore.deleteBookmarkFolder(this.data)
     283              :               }
     284              :             })
     285              :       }
     286              :     }
     287           86 :     return fillNodeInfo(bookmarkedRoot.value)
     288              :   }
     289              : )
     290              : 
     291              : interface TreeExplorerExposed {
     292              :   addFolderCommand: (targetNodeKey: string) => void
     293              : }
     294              : 
     295           56 : const treeExplorerRef = ref<TreeExplorerExposed | null>(null)
     296           56 : defineExpose({
     297              :   addNewBookmarkFolder: () => treeExplorerRef.value?.addFolderCommand('root')
     298              : })
     299              : 
     300           56 : const showCustomizationDialog = ref(false)
     301           56 : const initialIcon = ref(nodeBookmarkStore.defaultBookmarkIcon)
     302           56 : const initialColor = ref(nodeBookmarkStore.defaultBookmarkColor)
     303           56 : const customizationTargetNodePath = ref('')
     304           56 : const updateCustomization = async (icon: string, color: string) => {
     305            3 :   if (customizationTargetNodePath.value) {
     306            3 :     await nodeBookmarkStore.updateBookmarkCustomization(
     307              :       customizationTargetNodePath.value,
     308              :       { icon, color }
     309              :     )
     310              :   }
     311              : }
     312              : </script>
        

Generated by: LCOV version 2.0-1