Line data Source code
1 : <template>
2 : <DefineTrigger>
3 : <Button
4 : variant="textonly"
5 : size="unset"
6 : :tooltip="tooltipText"
7 : tooltip-side="bottom"
8 : :data-testid="`subgraph-breadcrumb-item-${item.key}`"
9 : :data-active="isActive ? '' : undefined"
10 : draggable="false"
11 : :class="
12 : cn(
13 : 'flex h-8 justify-start gap-0 overflow-hidden px-[calc(var(--breadcrumb-item-margin)+var(--breadcrumb-item-padding))] py-0 text-inherit select-none [font:inherit] hover:bg-transparent',
14 : isRoot && 'pl-(--breadcrumb-item-padding)',
15 : isActive && 'gap-1 text-foreground',
16 : menuOpen && 'bg-accent text-foreground hover:bg-accent'
17 : )
18 : "
19 : @click="handleClick"
20 : @keydown="handleKeydown"
21 : >
22 : <i
23 : v-if="hasMissingNodes && isRoot"
24 : data-testid="subgraph-breadcrumb-missing-nodes-icon"
25 : class="icon-[lucide--triangle-alert] text-warning"
26 : />
27 : <span ref="labelRef" class="max-w-72 truncate px-2">
28 : {{ item.label }}
29 : </span>
30 : <Badge
31 : v-if="item.isBlueprint"
32 : data-testid="subgraph-breadcrumb-blueprint-tag"
33 : severity="primary"
34 : >
35 : {{ t('breadcrumbsMenu.blueprint') }}
36 : </Badge>
37 : <i v-if="isActive" class="icon-[lucide--chevron-down] size-2.5"></i>
38 : </Button>
39 : </DefineTrigger>
40 : <Menu
41 : v-if="isActive"
42 : :open="menuOpen"
43 : :items="menuItems"
44 : :data-testid="`subgraph-breadcrumb-menu-${item.key}`"
45 : @update:open="menuOpen = Boolean($event && !isEditing)"
46 : @close-auto-focus="onMenuCloseAutoFocus"
47 : >
48 : <template #trigger><ReuseTrigger /></template>
49 : </Menu>
50 : <ReuseTrigger v-else />
51 : <Input
52 : v-if="isEditing"
53 : ref="itemInputRef"
54 : v-model="itemLabel"
55 : data-testid="subgraph-breadcrumb-rename-input"
56 : class="fixed z-overlay w-50 p-2 text-[.8rem]"
57 : @blur="inputBlur(false)"
58 : @click.stop
59 : @keydown.enter="inputBlur(true)"
60 : @keydown.esc="inputBlur(false)"
61 : />
62 : </template>
63 :
64 : <script setup lang="ts">
65 : import { cn } from '@comfyorg/tailwind-utils'
66 : import { createReusableTemplate } from '@vueuse/core'
67 : import { computed, nextTick, ref, toValue } from 'vue'
68 : import { useI18n } from 'vue-i18n'
69 :
70 : import Badge from '@comfyorg/design-system/badge/Badge'
71 : import Button from '@comfyorg/design-system/button/Button'
72 : import Input from '@comfyorg/design-system/input/Input'
73 : import Menu from '@comfyorg/design-system/menu/Menu'
74 : import type { MenuItemAction } from '@comfyorg/design-system/lib/menu/types'
75 : import { useWorkflowActionsMenu } from '@/composables/useWorkflowActionsMenu'
76 : import { useWorkflowService } from '@/platform/workflow/core/services/workflowService'
77 : import {
78 : ComfyWorkflow,
79 : useWorkflowStore
80 : } from '@/platform/workflow/management/stores/workflowStore'
81 : import { app } from '@/scripts/app'
82 : import { useDialogService } from '@/services/dialogService'
83 : import { useCommandStore } from '@/platform/keybindings/commandStore'
84 : import { useNodeDefStore } from '@/stores/nodeDefStore'
85 : import { isMissingWarningVisible } from '@/platform/settings/missingWarningVisibility'
86 : import { useSubgraphNavigationStore } from '@/stores/subgraphNavigationStore'
87 : import { ensureWorkflowSuffix, getWorkflowSuffix } from '@/utils/formatUtil'
88 : import { graphHasMissingNodes } from '@/platform/customNodes/manager/utils/graphHasMissingNodes'
89 :
90 : export interface BreadcrumbItem extends MenuItemAction {
91 : isBlueprint?: boolean
92 : updateTitle?: (title: string) => void
93 : }
94 :
95 : interface Props {
96 : item: BreadcrumbItem
97 : isActive?: boolean
98 : }
99 :
100 : const { item, isActive } = defineProps<Props>()
101 2110 : const [DefineTrigger, ReuseTrigger] = createReusableTemplate()
102 :
103 2110 : const nodeDefStore = useNodeDefStore()
104 2110 : const hasMissingNodes = computed(
105 : () =>
106 : isMissingWarningVisible('nodes') &&
107 : graphHasMissingNodes(app.rootGraph, nodeDefStore.nodeDefsByName)
108 : )
109 :
110 2110 : const { t } = useI18n()
111 2110 : const menuOpen = ref(false)
112 2110 : const dialogService = useDialogService()
113 2110 : const workflowStore = useWorkflowStore()
114 2110 : const workflowService = useWorkflowService()
115 2110 : const isEditing = ref(false)
116 2110 : const itemLabel = ref<string>()
117 2110 : const itemInputRef = ref<InstanceType<typeof Input>>()
118 2110 : const labelRef = ref<HTMLSpanElement>()
119 :
120 2110 : const rename = async (
121 : newName: string | null | undefined,
122 : initialName: string
123 : ) => {
124 1 : if (newName && newName !== initialName) {
125 : // Synchronize the node titles with the new name
126 1 : item.updateTitle?.(newName)
127 :
128 1 : if (workflowStore.activeSubgraph) {
129 1 : workflowStore.activeSubgraph.name = newName
130 0 : } else if (workflowStore.activeWorkflow) {
131 0 : try {
132 0 : const suffix = getWorkflowSuffix(workflowStore.activeWorkflow.suffix)
133 0 : await workflowService.renameWorkflow(
134 : workflowStore.activeWorkflow,
135 : ComfyWorkflow.basePath + ensureWorkflowSuffix(newName, suffix)
136 : )
137 : } catch (error) {
138 0 : console.error(error)
139 0 : dialogService.showErrorDialog(error)
140 0 : return
141 : }
142 : }
143 :
144 : // Force the navigation stack to recompute the labels
145 : // TODO: investigate if there is a better way to do this
146 1 : const navigationStore = useSubgraphNavigationStore()
147 1 : navigationStore.restoreState(navigationStore.exportState())
148 : }
149 : }
150 :
151 2110 : const isRoot = item.key === 'root'
152 :
153 2110 : const tooltipText = computed(() => {
154 5458 : if (hasMissingNodes.value && isRoot) {
155 53 : return t('breadcrumbsMenu.missingNodesWarning')
156 : }
157 5405 : return toValue(item.label)
158 : })
159 :
160 2110 : const startRename = async () => {
161 : // Check if element is hidden (collapsed breadcrumb)
162 : // When collapsed, root item is hidden via CSS display:none, so use rename command
163 2 : if (isRoot && labelRef.value?.offsetParent === null) {
164 0 : await useCommandStore().execute('Comfy.RenameWorkflow')
165 0 : return
166 : }
167 :
168 2 : isEditing.value = true
169 2 : itemLabel.value = item.label as string
170 2 : void nextTick(() => {
171 2 : itemInputRef.value?.focus()
172 2 : itemInputRef.value?.select()
173 : })
174 : }
175 :
176 2110 : const { menuItems } = useWorkflowActionsMenu(startRename, { isRoot })
177 :
178 2 : function onMenuCloseAutoFocus(event: Event) {
179 2 : if (isEditing.value) event.preventDefault()
180 : }
181 :
182 1 : function handleKeydown(event: KeyboardEvent) {
183 1 : if (isActive || isEditing.value || !['Enter', ' '].includes(event.key)) return
184 1 : event.preventDefault()
185 1 : event.stopPropagation()
186 1 : item.command?.({ item, originalEvent: event })
187 : }
188 :
189 2110 : const handleClick = (event: MouseEvent) => {
190 20 : if (isEditing.value) {
191 0 : return
192 : }
193 :
194 20 : if (event.detail === 1) {
195 18 : if (!isActive) {
196 15 : item.command?.({ item: item, originalEvent: event })
197 : }
198 2 : } else if (isActive && event.detail === 2) {
199 2 : menuOpen.value = false
200 2 : event.stopPropagation()
201 2 : event.preventDefault()
202 2 : void startRename()
203 : }
204 : }
205 :
206 2110 : const inputBlur = async (doRename: boolean) => {
207 4 : if (doRename) {
208 1 : await rename(itemLabel.value, item.label as string)
209 : }
210 :
211 4 : isEditing.value = false
212 : }
213 : </script>
|