Skip to content

Commit

Permalink
[Refactor] Simplify keybindingStore with _.groupBy (#2180)
Browse files Browse the repository at this point in the history
  • Loading branch information
huchenlei authored Jan 7, 2025
1 parent ffb20b8 commit 077ded2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
16 changes: 3 additions & 13 deletions src/stores/keybindingStore.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import _ from 'lodash'
import { defineStore } from 'pinia'
import { Ref, computed, ref, toRaw } from 'vue'

Expand Down Expand Up @@ -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')
}
)

Expand All @@ -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) {
Expand Down
12 changes: 12 additions & 0 deletions tests-ui/tests/store/keybindingStore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down

0 comments on commit 077ded2

Please sign in to comment.