LCOV - code coverage report
Current view: top level - src/composables - useWorkflowActionsMenu.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 71.8 % 78 56
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import type { ComputedRef, Ref } from 'vue'
       2              : import { computed } from 'vue'
       3              : import { useI18n } from 'vue-i18n'
       4              : 
       5              : import { useEnterBuilder } from '@/components/builder/useEnterBuilder'
       6              : import { useErrorHandling } from '@/composables/useErrorHandling'
       7              : import { isCloud } from '@/platform/distribution/types'
       8              : import { openDeployToComfyApiDialog } from '@/platform/workflow/deploy/composables/lazyDeployToComfyApiDialog'
       9              : import { DEPLOY_TO_COMFY_API_ACTION_ID } from '@/platform/workflow/deploy/constants'
      10              : import { openShareDialog } from '@/platform/workflow/sharing/composables/lazyShareDialog'
      11              : import { useWorkflowService } from '@/platform/workflow/core/services/workflowService'
      12              : import type { ComfyWorkflow } from '@/platform/workflow/management/stores/workflowStore'
      13              : import {
      14              :   useWorkflowBookmarkStore,
      15              :   useWorkflowStore
      16              : } from '@/platform/workflow/management/stores/workflowStore'
      17              : import { useCommandStore } from '@/platform/keybindings/commandStore'
      18              : import { useSubgraphStore } from '@/stores/subgraphStore'
      19              : import { useAppModeStore } from '@/stores/appModeStore'
      20              : import type {
      21              :   WorkflowMenuAction,
      22              :   WorkflowMenuItem
      23              : } from '@/types/workflowMenuItem'
      24              : 
      25              : interface WorkflowActionsMenuOptions {
      26              :   /** Whether this is the root workflow level. Defaults to true. */
      27              :   isRoot?: boolean
      28              :   /** Whether to include the delete workflow action. Defaults to true. */
      29              :   includeDelete?: boolean
      30              :   /** Override the workflow to operate on. If not provided, uses activeWorkflow. */
      31              :   workflow?: Ref<ComfyWorkflow | null> | ComputedRef<ComfyWorkflow | null>
      32              : }
      33              : 
      34              : interface AddItemOptions {
      35              :   id: string
      36              :   label: string
      37              :   icon: string
      38              :   command: () => void
      39              :   visible?: boolean
      40              :   disabled?: boolean
      41              :   prependSeparator?: boolean
      42              :   newItemBadge?: string
      43              : }
      44              : 
      45         7573 : export function useWorkflowActionsMenu(
      46              :   startRename: () => void,
      47              :   options: WorkflowActionsMenuOptions = {}
      48              : ) {
      49         7573 :   const { isRoot = true, includeDelete = true, workflow } = options
      50         7573 :   const { t } = useI18n()
      51         7573 :   const workflowStore = useWorkflowStore()
      52         7573 :   const workflowService = useWorkflowService()
      53         7573 :   const bookmarkStore = useWorkflowBookmarkStore()
      54         7573 :   const commandStore = useCommandStore()
      55         7573 :   const subgraphStore = useSubgraphStore()
      56         7573 :   const appModeStore = useAppModeStore()
      57         7573 :   const { pruneLinearData } = appModeStore
      58         7573 :   const { enterBuilder } = useEnterBuilder()
      59         7573 :   const { toastErrorHandler } = useErrorHandling()
      60              : 
      61         7573 :   const targetWorkflow = computed(
      62              :     () => workflow?.value ?? workflowStore.activeWorkflow
      63              :   )
      64              : 
      65              :   /** Switch to the target workflow tab if it's not already active */
      66         7573 :   const ensureWorkflowActive = async (wf: ComfyWorkflow | null) => {
      67           61 :     if (!wf || wf === workflowStore.activeWorkflow) return true
      68            0 :     return await workflowService.openWorkflow(wf, { initiator: 'user' })
      69              :   }
      70              : 
      71              :   const menuItems = computed<WorkflowMenuItem[]>(() => {
      72        16694 :     const workflow = targetWorkflow.value
      73        16694 :     const isBlueprint = workflow
      74              :       ? subgraphStore.isSubgraphBlueprint(workflow)
      75              :       : false
      76              : 
      77        16694 :     const items: WorkflowMenuItem[] = []
      78              : 
      79        16694 :     const addItem = ({
      80              :       id,
      81              :       label,
      82              :       icon,
      83              :       command,
      84              :       visible = true,
      85              :       disabled = false,
      86              :       prependSeparator = false,
      87              :       newItemBadge
      88              :     }: AddItemOptions) => {
      89       234618 :       if (prependSeparator && visible) items.push({ separator: true })
      90       234618 :       const item: WorkflowMenuAction = { id, label, icon, command, disabled }
      91       234618 :       if (!visible) item.visible = false
      92       234618 :       if (newItemBadge) item.badge = newItemBadge
      93       234618 :       items.push(item)
      94              :     }
      95              : 
      96              :     const workflowMode =
      97              :       workflow?.activeMode ?? workflow?.initialMode ?? 'graph'
      98        16694 :     const isLinearMode = workflowMode === 'app'
      99        16694 :     const isBookmarked = bookmarkStore.isBookmarked(workflow?.path ?? '')
     100              : 
     101        16694 :     const toggleLinear = async () => {
     102            0 :       await ensureWorkflowActive(targetWorkflow.value)
     103            0 :       await commandStore.execute('Comfy.ToggleLinear', {
     104              :         metadata: { source: 'breadcrumb_menu' }
     105              :       })
     106              :     }
     107        16694 :     addItem({
     108              :       id: 'rename',
     109              :       label: t('g.rename'),
     110              :       icon: 'icon-[lucide--pencil]',
     111              :       command: async () => {
     112            0 :         await ensureWorkflowActive(targetWorkflow.value)
     113            0 :         startRename()
     114              :       },
     115              :       disabled: isRoot && !workflow?.isPersisted
     116              :     })
     117              : 
     118        16694 :     addItem({
     119              :       id: 'duplicate',
     120              :       label: t('breadcrumbsMenu.duplicate'),
     121              :       icon: 'icon-[lucide--copy]',
     122              :       command: async () => {
     123            0 :         if (workflow) {
     124            0 :           await workflowService.duplicateWorkflow(workflow)
     125              :         }
     126              :       },
     127              :       visible: isRoot && !isBlueprint
     128              :     })
     129              : 
     130        16694 :     addItem({
     131              :       id: 'toggle-bookmark',
     132              :       label: isBookmarked
     133              :         ? t('tabMenu.removeFromBookmarks')
     134              :         : t('tabMenu.addToBookmarks'),
     135              :       icon: 'pi pi-bookmark' + (isBookmarked ? '-fill' : ''),
     136              :       command: async () => {
     137            0 :         if (workflow?.path) {
     138            0 :           await bookmarkStore.toggleBookmarked(workflow.path)
     139              :         }
     140              :       },
     141              :       visible: isRoot,
     142              :       disabled: workflow?.isTemporary ?? false
     143              :     })
     144              : 
     145        16694 :     addItem({
     146              :       id: 'save',
     147              :       label: t('menuLabels.Save'),
     148              :       icon: 'icon-[lucide--save]',
     149              :       command: async () => {
     150            0 :         await ensureWorkflowActive(workflow)
     151            0 :         await commandStore.execute('Comfy.SaveWorkflow')
     152              :       },
     153              :       visible: isRoot,
     154              :       prependSeparator: true
     155              :     })
     156              : 
     157        16694 :     addItem({
     158              :       id: 'save-as',
     159              :       label: t('menuLabels.Save As'),
     160              :       icon: 'icon-[lucide--save]',
     161              :       command: async () => {
     162            0 :         await ensureWorkflowActive(workflow)
     163            0 :         await commandStore.execute('Comfy.SaveWorkflowAs')
     164              :       },
     165              :       visible: isRoot
     166              :     })
     167              : 
     168        16694 :     addItem({
     169              :       id: 'export',
     170              :       label: t('menuLabels.Export'),
     171              :       icon: 'icon-[lucide--download]',
     172              :       command: async () => {
     173            0 :         await ensureWorkflowActive(workflow)
     174            0 :         await commandStore.execute('Comfy.ExportWorkflow')
     175              :       },
     176              :       visible: isRoot,
     177              :       prependSeparator: true
     178              :     })
     179              : 
     180        16694 :     addItem({
     181              :       id: 'export-api',
     182              :       label: t('menuLabels.Export (API)'),
     183              :       icon: 'icon-[lucide--download]',
     184              :       command: async () => {
     185            0 :         await ensureWorkflowActive(workflow)
     186            0 :         await commandStore.execute('Comfy.ExportWorkflowAPI')
     187              :       },
     188              :       visible: isRoot
     189              :     })
     190              : 
     191        16694 :     addItem({
     192              :       id: 'share',
     193              :       label: t('breadcrumbsMenu.share'),
     194              :       icon: 'icon-[comfy--send]',
     195              :       command: () => openShareDialog().catch(toastErrorHandler),
     196              :       visible: isCloud
     197              :     })
     198              : 
     199        16694 :     addItem({
     200              :       id: DEPLOY_TO_COMFY_API_ACTION_ID,
     201              :       label: t('deployToComfyApi.buttonLabel'),
     202              :       icon: 'icon-[lucide--rocket]',
     203              :       command: async () => {
     204            1 :         if (!(await ensureWorkflowActive(targetWorkflow.value))) return
     205            1 :         await openDeployToComfyApiDialog().catch(toastErrorHandler)
     206              :       },
     207              :       visible: isRoot && !isCloud,
     208              :       newItemBadge: t('g.new')
     209              :     })
     210              : 
     211        16694 :     addItem({
     212              :       id: 'enter-app-mode',
     213              :       label: t('breadcrumbsMenu.enterAppMode'),
     214              :       icon: 'icon-[lucide--panels-top-left]',
     215              :       command: toggleLinear,
     216              :       visible: isRoot && !isLinearMode,
     217              :       prependSeparator: true,
     218              :       newItemBadge: t('g.experimental')
     219              :     })
     220              : 
     221        16694 :     addItem({
     222              :       id: 'exit-app-mode',
     223              :       label: t('breadcrumbsMenu.exitAppMode'),
     224              :       icon: 'icon-[comfy--workflow]',
     225              :       command: toggleLinear,
     226              :       visible: isLinearMode,
     227              :       prependSeparator: true
     228              :     })
     229              : 
     230        16694 :     if (!workflow) return items
     231        12746 :     const isActive = workflow === workflowStore.activeWorkflow
     232        12746 :     const getLinearData = (
     233              :       tracker: typeof workflow.changeTracker | undefined
     234              :     ) => {
     235            0 :       if (!tracker) return undefined
     236            0 :       return tracker.activeState.extra?.linearData
     237              :     }
     238        16694 :     const rawLd = isActive
     239              :       ? {
     240              :           inputs: appModeStore.selectedInputs,
     241              :           outputs: appModeStore.selectedOutputs
     242              :         }
     243              :       : getLinearData(workflow.changeTracker)
     244        16694 :     let hasLinearData: boolean
     245        16694 :     if (rawLd) {
     246        12746 :       const { inputs, outputs } = pruneLinearData(rawLd)
     247        12746 :       hasLinearData = inputs.length > 0 || outputs.length > 0
     248              :     } else {
     249            0 :       hasLinearData = workflow.path.endsWith('.app.json')
     250              :     }
     251              : 
     252        16694 :     addItem({
     253              :       id: 'enter-builder-mode',
     254              :       label: hasLinearData
     255              :         ? t('breadcrumbsMenu.editBuilderMode')
     256              :         : t('breadcrumbsMenu.enterBuilderMode'),
     257              :       icon: 'icon-[lucide--hammer]',
     258              :       command: async () => {
     259           60 :         await ensureWorkflowActive(targetWorkflow.value)
     260           60 :         enterBuilder()
     261              :       },
     262              :       visible: isRoot,
     263              :       newItemBadge: t('g.experimental')
     264              :     })
     265              : 
     266        16694 :     addItem({
     267              :       id: 'clear-workflow',
     268              :       label: t('breadcrumbsMenu.clearWorkflow'),
     269              :       icon: 'icon-[lucide--trash-2]',
     270              :       command: async () => {
     271            0 :         await ensureWorkflowActive(workflow)
     272            0 :         await commandStore.execute('Comfy.ClearWorkflow')
     273              :       },
     274              :       prependSeparator: true
     275              :     })
     276              : 
     277        16694 :     addItem({
     278              :       id: 'publish',
     279              :       label: t('subgraphStore.publish'),
     280              :       icon: 'icon-[lucide--upload]',
     281              :       command: async () => await workflowService.saveWorkflowAs(workflow),
     282              :       visible: isRoot && isBlueprint,
     283              :       prependSeparator: true
     284              :     })
     285              : 
     286        16694 :     addItem({
     287              :       id: 'delete',
     288              :       label: isBlueprint
     289              :         ? t('breadcrumbsMenu.deleteBlueprint')
     290              :         : t('breadcrumbsMenu.deleteWorkflow'),
     291              :       icon: 'icon-[lucide--x]',
     292              :       command: async () => await workflowService.deleteWorkflow(workflow),
     293              :       visible: isRoot && includeDelete,
     294              :       prependSeparator: true
     295              :     })
     296              : 
     297        16694 :     return items
     298              :   })
     299              : 
     300         7573 :   return {
     301              :     menuItems
     302              :   }
     303              : }
        

Generated by: LCOV version 2.0-1