LCOV - code coverage report
Current view: top level - packages/keybindings/src - commandStore.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 96.4 % 55 53
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import { defineStore } from 'pinia'
       2              : import { computed, ref } from 'vue'
       3              : 
       4              : import type { CanvasInteractionModeReader } from '@comfyorg/graph-model/CanvasInteractionMode'
       5              : import { EDITABLE_INTERACTION_MODE } from '@comfyorg/graph-model/CanvasInteractionMode'
       6              : import type { KeybindingImpl } from './keybinding'
       7              : import { useKeybindingStore } from './keybindingStore'
       8              : 
       9              : export interface ComfyCommand {
      10              :   id: string
      11              :   function: (metadata?: Record<string, unknown>) => void | Promise<void>
      12              : 
      13              :   label?: string | (() => string)
      14              :   icon?: string | (() => string)
      15              :   tooltip?: string | (() => string)
      16              :   menubarLabel?: string | (() => string)
      17              :   versionAdded?: string
      18              :   confirmation?: string
      19              :   source?: string
      20              :   active?: () => boolean
      21              :   category?: 'essentials' | 'view-controls'
      22              :   /** Refused by `execute()` while the canvas interaction mode is select-only. */
      23              :   mutatesGraph?: boolean | (() => boolean)
      24              : }
      25              : 
      26         1220 : function isBlockedByInteractionMode(
      27              :   command: ComfyCommand,
      28              :   interactionMode: CanvasInteractionModeReader
      29              : ): boolean {
      30         1220 :   if (!interactionMode.isSelectOnly()) return false
      31         1220 :   return typeof command.mutatesGraph === 'function'
      32              :     ? command.mutatesGraph()
      33              :     : command.mutatesGraph === true
      34              : }
      35              : 
      36         2356 : export class ComfyCommandImpl implements ComfyCommand {
      37              :   id: string
      38              :   function: (metadata?: Record<string, unknown>) => void | Promise<void>
      39              :   _label?: string | (() => string)
      40              :   _icon?: string | (() => string)
      41              :   _tooltip?: string | (() => string)
      42              :   _menubarLabel?: string | (() => string)
      43              :   versionAdded?: string
      44              :   confirmation?: string
      45              :   source?: string
      46              :   active?: () => boolean
      47              :   category?: 'essentials' | 'view-controls'
      48              :   mutatesGraph?: boolean | (() => boolean)
      49              : 
      50              :   constructor(command: ComfyCommand) {
      51       256778 :     this.id = command.id
      52       256778 :     this.function = command.function
      53       256778 :     this._label = command.label
      54       256778 :     this._icon = command.icon
      55       256778 :     this._tooltip = command.tooltip
      56       256778 :     this._menubarLabel = command.menubarLabel ?? command.label
      57       256778 :     this.versionAdded = command.versionAdded
      58       256778 :     this.confirmation = command.confirmation
      59       256778 :     this.source = command.source
      60       256778 :     this.active = command.active
      61       256778 :     this.category = command.category
      62       256778 :     this.mutatesGraph = command.mutatesGraph
      63              :   }
      64              : 
      65              :   get label() {
      66         6256 :     return typeof this._label === 'function' ? this._label() : this._label
      67              :   }
      68              : 
      69              :   get icon() {
      70        73022 :     return typeof this._icon === 'function' ? this._icon() : this._icon
      71              :   }
      72              : 
      73              :   get tooltip() {
      74        73020 :     return typeof this._tooltip === 'function' ? this._tooltip() : this._tooltip
      75              :   }
      76              : 
      77              :   get menubarLabel() {
      78        73020 :     return typeof this._menubarLabel === 'function'
      79              :       ? this._menubarLabel()
      80              :       : this._menubarLabel
      81              :   }
      82              : 
      83              :   get keybinding(): KeybindingImpl | null {
      84         6114 :     return useKeybindingStore().getKeybindingByCommandId(this.id) ?? null
      85              :   }
      86              : }
      87              : 
      88         2356 : export function defineCommandStore(
      89              :   useDefaultErrorHandler: () => (error: unknown) => void
      90              : ) {
      91         2356 :   return defineStore('command', () => {
      92         2355 :     const commandsById = ref<Record<string, ComfyCommandImpl>>({})
      93         2355 :     const commands = computed(() => Object.values(commandsById.value))
      94              : 
      95         2355 :     const registerCommand = (command: ComfyCommand) => {
      96       256778 :       if (command.id in commandsById.value) {
      97            0 :         console.warn(`Command ${command.id} already registered`)
      98              :       }
      99       256778 :       commandsById.value[command.id] = new ComfyCommandImpl(command)
     100              :     }
     101              : 
     102         2355 :     const registerCommands = (commands: ComfyCommand[]) => {
     103         2355 :       for (const command of commands) {
     104       216660 :         registerCommand(command)
     105              :       }
     106              :     }
     107              : 
     108         2355 :     const getCommand = (command: string) => {
     109        78647 :       return commandsById.value[command]
     110              :     }
     111              : 
     112         2355 :     let interactionMode: CanvasInteractionModeReader = EDITABLE_INTERACTION_MODE
     113         2355 :     const setInteractionMode = (mode: CanvasInteractionModeReader) => {
     114         2355 :       interactionMode = mode
     115              :     }
     116              : 
     117         2355 :     const defaultErrorHandler = useDefaultErrorHandler()
     118         2355 :     const execute = async (
     119              :       commandId: string,
     120              :       {
     121              :         errorHandler = defaultErrorHandler,
     122              :         metadata
     123              :       }: {
     124              :         errorHandler?: (error: unknown) => void
     125              :         metadata?: Record<string, unknown>
     126              :       } = {}
     127              :     ) => {
     128         1220 :       if (!(commandId in commandsById.value)) {
     129            0 :         throw new Error(`Command ${commandId} not found`)
     130              :       }
     131         1220 :       const command = getCommand(commandId)
     132         1220 :       if (isBlockedByInteractionMode(command, interactionMode)) return
     133         1220 :       try {
     134         1220 :         await command.function(metadata)
     135              :       } catch (error) {
     136            6 :         errorHandler(error)
     137              :       }
     138              :     }
     139              : 
     140         2355 :     const isRegistered = (command: string) => {
     141          280 :       return !!commandsById.value[command]
     142              :     }
     143              : 
     144         2355 :     const loadExtensionCommands = (extension: {
     145              :       name: string
     146              :       commands?: ComfyCommand[]
     147              :     }) => {
     148        82789 :       if (extension.commands) {
     149         4778 :         for (const command of extension.commands) {
     150        21263 :           registerCommand({
     151              :             ...command,
     152              :             source: extension.name
     153              :           })
     154              :         }
     155              :       }
     156              :     }
     157              : 
     158         2355 :     const formatKeySequence = (command: ComfyCommandImpl): string => {
     159              :       const sequences = command.keybinding?.combo.getKeySequences() || []
     160         4329 :       return sequences
     161              :         .map((seq) =>
     162              :           seq.replace(/Control/g, 'Ctrl').replace(/Shift/g, 'Shift')
     163              :         )
     164              :         .join(' + ')
     165              :     }
     166              : 
     167         2355 :     return {
     168              :       commands,
     169              :       execute,
     170              :       getCommand,
     171              :       registerCommand,
     172              :       registerCommands,
     173              :       isRegistered,
     174              :       setInteractionMode,
     175              :       loadExtensionCommands,
     176              :       formatKeySequence
     177              :     }
     178              :   })
     179              : }
        

Generated by: LCOV version 2.0-1