[Refactor] Simplify keybindingStore with _.groupBy (#2180)

This commit is contained in:
Chenlei Hu
2025-01-06 19:46:20 -05:00
committed by GitHub
parent ffb20b8789
commit 077ded2cce
2 changed files with 15 additions and 13 deletions

View File

@@ -1,3 +1,4 @@
import _ from 'lodash'
import { defineStore } from 'pinia'
import { Ref, computed, ref, toRaw } from 'vue'
@@ -145,20 +146,9 @@ export const useKeybindingStore = defineStore('keybinding', () => {
return keybindingByKeyCombo.value[combo.serialize()]
}
function createKeybindingsByCommandId(keybindings: KeybindingImpl[]) {
const result: Record<string, KeybindingImpl[]> = {}
for (const keybinding of keybindings) {
if (!(keybinding.commandId in result)) {
result[keybinding.commandId] = []
}
result[keybinding.commandId].push(keybinding)
}
return result
}
const keybindingsByCommandId = computed<Record<string, KeybindingImpl[]>>(
() => {
return createKeybindingsByCommandId(keybindings.value)
return _.groupBy(keybindings.value, 'commandId')
}
)
@@ -169,7 +159,7 @@ export const useKeybindingStore = defineStore('keybinding', () => {
const defaultKeybindingsByCommandId = computed<
Record<string, KeybindingImpl[]>
>(() => {
return createKeybindingsByCommandId(Object.values(defaultKeybindings.value))
return _.groupBy(Object.values(defaultKeybindings.value), 'commandId')
})
function getKeybindingByCommandId(commandId: string) {

View File

@@ -33,6 +33,18 @@ describe('useKeybindingStore', () => {
expect(store.getKeybinding(keybinding.combo)).toEqual(keybinding)
})
it('should get keybindings by command id', () => {
const store = useKeybindingStore()
const keybinding = new KeybindingImpl({
commandId: 'test.command',
combo: { key: 'C', ctrl: true }
})
store.addDefaultKeybinding(keybinding)
expect(store.getKeybindingsByCommandId('test.command')).toEqual([
keybinding
])
})
it('should override default keybindings with user keybindings', () => {
const store = useKeybindingStore()
const defaultKeybinding = new KeybindingImpl({