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

            Line data    Source code
       1              : import { groupBy } from 'es-toolkit/compat'
       2              : import { defineStore } from 'pinia'
       3              : import type { Ref } from 'vue'
       4              : import { computed, ref, shallowRef } from 'vue'
       5              : 
       6              : import type { KeyComboImpl } from './keyCombo'
       7              : import { KeybindingImpl } from './keybinding'
       8              : import type { KeybindingPreset } from './types'
       9              : 
      10              : type KeybindingMap = Partial<Record<string, KeybindingImpl>>
      11              : 
      12         4271 : function keybindingValues(keybindings: KeybindingMap): KeybindingImpl[] {
      13         4271 :   return Object.values(keybindings).filter(
      14              :     (keybinding): keybinding is KeybindingImpl => keybinding !== undefined
      15              :   )
      16              : }
      17              : 
      18         2356 : export const useKeybindingStore = defineStore('keybinding', () => {
      19         2355 :   const defaultKeybindings = shallowRef<KeybindingMap>({})
      20         2355 :   const userKeybindings = shallowRef<KeybindingMap>({})
      21         2355 :   const userUnsetKeybindings = shallowRef<KeybindingMap>({})
      22         2355 :   const pendingUnsetKeybindings = shallowRef<KeybindingImpl[]>([])
      23              : 
      24         2355 :   const currentPresetName = ref('default')
      25         2355 :   const savedPresetData = ref<KeybindingPreset | null>(null)
      26              : 
      27         2355 :   const serializeBinding = (b: KeybindingImpl) =>
      28              :     `${b.commandId}:${b.combo.serialize()}:${b.targetElementId ?? ''}`
      29              : 
      30         2355 :   const savedPresetSerialized = computed(() => {
      31            6 :     if (!savedPresetData.value) return null
      32              :     const savedNew = savedPresetData.value.newBindings
      33              :       .map((b) => serializeBinding(new KeybindingImpl(b)))
      34              :       .sort()
      35              :       .join('|')
      36              :     const savedUnset = savedPresetData.value.unsetBindings
      37              :       .map((b) => serializeBinding(new KeybindingImpl(b)))
      38              :       .sort()
      39              :       .join('|')
      40            6 :     return { savedNew, savedUnset }
      41              :   })
      42              : 
      43         2355 :   const isCurrentPresetModified = computed(() => {
      44           58 :     const newBindings = keybindingValues(userKeybindings.value)
      45              : 
      46           58 :     if (currentPresetName.value === 'default') {
      47           52 :       return (
      48              :         newBindings.length > 0 ||
      49              :         keybindingValues(userUnsetKeybindings.value).length > 0
      50              :       )
      51              :     }
      52              : 
      53           14 :     if (!savedPresetSerialized.value) return false
      54              : 
      55            6 :     const currentNew = newBindings.map(serializeBinding).sort().join('|')
      56            6 :     const currentUnset = getUserUnsetKeybindingValues()
      57              :       .map(serializeBinding)
      58              :       .sort()
      59              :       .join('|')
      60              : 
      61           58 :     return (
      62              :       currentNew !== savedPresetSerialized.value.savedNew ||
      63              :       currentUnset !== savedPresetSerialized.value.savedUnset
      64              :     )
      65              :   })
      66              : 
      67            0 :   function getUserKeybindings() {
      68            0 :     return userKeybindings.value
      69              :   }
      70              : 
      71           25 :   function getUserKeybindingValues() {
      72           25 :     return keybindingValues(userKeybindings.value)
      73              :   }
      74              : 
      75            0 :   function getUserUnsetKeybindings() {
      76            0 :     return userUnsetKeybindings.value
      77              :   }
      78              : 
      79           31 :   function getUserUnsetKeybindingValues() {
      80           31 :     return [
      81              :       ...keybindingValues(userUnsetKeybindings.value),
      82              :       ...pendingUnsetKeybindings.value
      83              :     ]
      84              :   }
      85              : 
      86         2355 :   const keybindingByKeyCombo = computed<KeybindingMap>(() => {
      87         2069 :     const result: KeybindingMap = {
      88              :       ...defaultKeybindings.value
      89              :     }
      90              : 
      91         2069 :     for (const keybinding of keybindingValues(userUnsetKeybindings.value)) {
      92           10 :       const serializedCombo = keybinding.combo.serialize()
      93           10 :       if (result[serializedCombo]?.equals(keybinding)) {
      94           10 :         delete result[serializedCombo]
      95              :       }
      96              :     }
      97              : 
      98         2069 :     return {
      99              :       ...result,
     100              :       ...userKeybindings.value
     101              :     }
     102              :   })
     103              : 
     104         2355 :   const keybindings = computed<KeybindingImpl[]>(() =>
     105              :     keybindingValues(keybindingByKeyCombo.value)
     106              :   )
     107              : 
     108          857 :   function getKeybinding(combo: KeyComboImpl): KeybindingImpl | undefined {
     109          857 :     return keybindingByKeyCombo.value[combo.serialize()]
     110              :   }
     111              : 
     112         2355 :   const keybindingsByCommandId = computed<
     113              :     Partial<Record<string, KeybindingImpl[]>>
     114              :   >(() => {
     115         2004 :     return groupBy(keybindings.value, 'commandId')
     116              :   })
     117              : 
     118        26501 :   function getKeybindingsByCommandId(commandId: string) {
     119        26501 :     return keybindingsByCommandId.value[commandId] ?? []
     120              :   }
     121              : 
     122         2355 :   const defaultKeybindingsByCommandId = computed<
     123              :     Partial<Record<string, KeybindingImpl[]>>
     124              :   >(() => groupBy(keybindingValues(defaultKeybindings.value), 'commandId'))
     125              : 
     126        20244 :   function getKeybindingByCommandId(commandId: string) {
     127        20244 :     return getKeybindingsByCommandId(commandId).at(0)
     128              :   }
     129              : 
     130        82452 :   function addKeybinding(
     131              :     target: Ref<KeybindingMap>,
     132              :     keybinding: KeybindingImpl,
     133              :     { existOk }: { existOk: boolean }
     134              :   ) {
     135        82452 :     const existing = target.value[keybinding.combo.serialize()]
     136        82452 :     if (!existOk && existing) {
     137            0 :       throw new Error(
     138              :         `Keybinding on ${keybinding.combo} already exists on ${existing.commandId}`
     139              :       )
     140              :     }
     141        82452 :     target.value = {
     142              :       ...target.value,
     143              :       [keybinding.combo.serialize()]: keybinding
     144              :     }
     145              :   }
     146              : 
     147        82432 :   function addDefaultKeybinding(keybinding: KeybindingImpl) {
     148        82432 :     addKeybinding(defaultKeybindings, keybinding, { existOk: false })
     149        82432 :     const pending = pendingUnsetKeybindings.value.find((unset) =>
     150              :       unset.equals(keybinding)
     151              :     )
     152        82432 :     if (!pending) return
     153            0 :     pendingUnsetKeybindings.value = pendingUnsetKeybindings.value.filter(
     154              :       (unset) => unset !== pending
     155              :     )
     156            0 :     addKeybinding(userUnsetKeybindings, keybinding, { existOk: true })
     157              :   }
     158              : 
     159            5 :   function addUserUnsetKeybinding(keybinding: KeybindingImpl) {
     160            5 :     const registeredDefault =
     161              :       defaultKeybindings.value[keybinding.combo.serialize()]
     162            5 :     if (registeredDefault) {
     163            5 :       if (registeredDefault.equals(keybinding)) {
     164            5 :         addKeybinding(userUnsetKeybindings, keybinding, { existOk: true })
     165              :       }
     166            5 :       return
     167              :     }
     168            0 :     if (pendingUnsetKeybindings.value.some((unset) => unset.equals(keybinding)))
     169            0 :       return
     170            0 :     pendingUnsetKeybindings.value = [
     171              :       ...pendingUnsetKeybindings.value,
     172              :       keybinding
     173              :     ]
     174              :   }
     175              : 
     176           11 :   function addUserKeybinding(keybinding: KeybindingImpl) {
     177           11 :     const serializedCombo = keybinding.combo.serialize()
     178           11 :     const defaultKeybinding = defaultKeybindings.value[serializedCombo]
     179           11 :     const userUnsetKeybinding = userUnsetKeybindings.value[serializedCombo]
     180              : 
     181           11 :     if (
     182              :       keybinding.equals(defaultKeybinding) &&
     183              :       keybinding.equals(userUnsetKeybinding)
     184              :     ) {
     185            0 :       const updated = { ...userUnsetKeybindings.value }
     186            0 :       delete updated[keybinding.combo.serialize()]
     187            0 :       userUnsetKeybindings.value = updated
     188            0 :       return
     189              :     }
     190              : 
     191           11 :     if (defaultKeybinding && !defaultKeybinding.equals(userUnsetKeybinding)) {
     192            0 :       unsetKeybinding(defaultKeybinding)
     193              :     }
     194              : 
     195           11 :     addKeybinding(userKeybindings, keybinding, { existOk: true })
     196              :   }
     197              : 
     198            8 :   function unsetKeybinding(keybinding: KeybindingImpl) {
     199            8 :     const serializedCombo = keybinding.combo.serialize()
     200            8 :     if (!(serializedCombo in keybindingByKeyCombo.value)) {
     201            0 :       console.warn(
     202              :         `Trying to unset non-exist keybinding: ${JSON.stringify(keybinding)}`
     203              :       )
     204            0 :       return
     205              :     }
     206              : 
     207            8 :     if (userKeybindings.value[serializedCombo]?.equals(keybinding)) {
     208            4 :       const updated = { ...userKeybindings.value }
     209            4 :       delete updated[serializedCombo]
     210            4 :       userKeybindings.value = updated
     211            4 :       return
     212              :     }
     213              : 
     214            8 :     if (defaultKeybindings.value[serializedCombo]?.equals(keybinding)) {
     215            4 :       addKeybinding(userUnsetKeybindings, keybinding, { existOk: false })
     216            4 :       return
     217              :     }
     218              : 
     219            0 :     console.warn(`Unset unknown keybinding: ${JSON.stringify(keybinding)}`)
     220              :   }
     221              : 
     222            0 :   function updateKeybindingOnCommand(keybinding: KeybindingImpl): boolean {
     223            0 :     const currentKeybinding = getKeybindingByCommandId(keybinding.commandId)
     224            0 :     if (currentKeybinding?.equals(keybinding)) {
     225            0 :       return false
     226              :     }
     227            0 :     if (currentKeybinding) {
     228            0 :       unsetKeybinding(currentKeybinding)
     229              :     }
     230            0 :     addUserKeybinding(keybinding)
     231            0 :     return true
     232              :   }
     233              : 
     234            7 :   function resetAllKeybindings() {
     235            7 :     userKeybindings.value = {}
     236            7 :     userUnsetKeybindings.value = {}
     237            7 :     pendingUnsetKeybindings.value = []
     238              :   }
     239              : 
     240            1 :   function removeAllKeybindingsForCommand(commandId: string): boolean {
     241            1 :     const currentBindings = getKeybindingsByCommandId(commandId)
     242            1 :     if (currentBindings.length === 0) return false
     243            1 :     for (const binding of currentBindings) {
     244            2 :       unsetKeybinding(binding)
     245              :     }
     246            1 :     return true
     247              :   }
     248              : 
     249            0 :   function updateSpecificKeybinding(
     250              :     oldBinding: KeybindingImpl,
     251              :     newBinding: KeybindingImpl
     252              :   ) {
     253            0 :     unsetKeybinding(oldBinding)
     254            0 :     addUserKeybinding(newBinding)
     255              :   }
     256              : 
     257            2 :   function resetKeybindingForCommand(commandId: string): boolean {
     258            2 :     const currentBindings = getKeybindingsByCommandId(commandId)
     259            2 :     const defaultBindings = defaultKeybindingsByCommandId.value[commandId] ?? []
     260              : 
     261            2 :     if (defaultBindings.length === 0) {
     262            0 :       if (currentBindings.length > 0) {
     263            0 :         for (const binding of currentBindings) {
     264            0 :           unsetKeybinding(binding)
     265              :         }
     266            0 :         return true
     267              :       }
     268            0 :       return false
     269              :     }
     270              : 
     271            2 :     if (!isCommandKeybindingModified(commandId)) {
     272            0 :       return false
     273              :     }
     274              : 
     275            2 :     for (const binding of currentBindings) {
     276            4 :       unsetKeybinding(binding)
     277              :     }
     278              : 
     279            2 :     const updatedUnset = { ...userUnsetKeybindings.value }
     280            2 :     for (const defaultBinding of defaultBindings) {
     281            2 :       const serializedCombo = defaultBinding.combo.serialize()
     282            2 :       if (updatedUnset[serializedCombo]?.equals(defaultBinding)) {
     283            2 :         delete updatedUnset[serializedCombo]
     284              :       }
     285              :     }
     286            2 :     userUnsetKeybindings.value = updatedUnset
     287              : 
     288            2 :     return true
     289              :   }
     290              : 
     291         2355 :   const modifiedCommandIds = computed<Set<string>>(() => {
     292           56 :     const result = new Set<string>()
     293           56 :     const allCommandIds = new Set([
     294              :       ...Object.keys(keybindingsByCommandId.value),
     295              :       ...Object.keys(defaultKeybindingsByCommandId.value)
     296              :     ])
     297              : 
     298           56 :     for (const commandId of allCommandIds) {
     299         1739 :       const currentBindings = keybindingsByCommandId.value[commandId] ?? []
     300         1739 :       const defaultBindings =
     301              :         defaultKeybindingsByCommandId.value[commandId] ?? []
     302              : 
     303         1739 :       if (currentBindings.length !== defaultBindings.length) {
     304            8 :         result.add(commandId)
     305            8 :         continue
     306              :       }
     307         1739 :       if (currentBindings.length === 0) continue
     308              : 
     309         1731 :       const sortedCurrent = [...currentBindings]
     310              :         .map((b) => b.combo.serialize())
     311              :         .sort()
     312         1731 :       const sortedDefault = [...defaultBindings]
     313              :         .map((b) => b.combo.serialize())
     314              :         .sort()
     315              : 
     316         1739 :       if (sortedCurrent.some((combo, i) => combo !== sortedDefault[i])) {
     317            4 :         result.add(commandId)
     318              :       }
     319              :     }
     320              : 
     321           56 :     return result
     322              :   })
     323              : 
     324         6256 :   function isCommandKeybindingModified(commandId: string): boolean {
     325         6256 :     return modifiedCommandIds.value.has(commandId)
     326              :   }
     327              : 
     328         2355 :   return {
     329              :     keybindings,
     330              :     getUserKeybindings,
     331              :     getUserKeybindingValues,
     332              :     getUserUnsetKeybindings,
     333              :     getUserUnsetKeybindingValues,
     334              :     getKeybinding,
     335              :     getKeybindingsByCommandId,
     336              :     getKeybindingByCommandId,
     337              :     addDefaultKeybinding,
     338              :     addUserKeybinding,
     339              :     addUserUnsetKeybinding,
     340              :     unsetKeybinding,
     341              :     updateKeybindingOnCommand,
     342              :     resetAllKeybindings,
     343              :     resetKeybindingForCommand,
     344              :     isCommandKeybindingModified,
     345              :     currentPresetName,
     346              :     savedPresetData,
     347              :     isCurrentPresetModified,
     348              :     removeAllKeybindingsForCommand,
     349              :     updateSpecificKeybinding
     350              :   }
     351              : })
        

Generated by: LCOV version 2.0-1