Fix keybinding conflict (#1630)

This commit is contained in:
Chenlei Hu
2024-11-21 11:49:57 -05:00
committed by GitHub
parent 479d1b28c7
commit 886c40a69a
3 changed files with 84 additions and 26 deletions

View File

@@ -182,14 +182,3 @@ export const CORE_KEYBINDINGS: Keybinding[] = [
commandId: 'Workspace.ToggleFocusMode'
}
]
export const DEPRECATED_KEYBINDINGS: Keybinding[] = [
// Ctrl+S is now used for saving the workflow after v1.4.6.
{
combo: {
key: 's',
ctrl: true
},
commandId: 'Comfy.ExportWorkflow'
}
]

View File

@@ -2,7 +2,7 @@ import { defineStore } from 'pinia'
import { computed, Ref, ref, toRaw } from 'vue'
import { Keybinding, KeyCombo } from '@/types/keyBindingTypes'
import { useSettingStore } from './settingStore'
import { CORE_KEYBINDINGS, DEPRECATED_KEYBINDINGS } from './coreKeybindings'
import { CORE_KEYBINDINGS } from './coreKeybindings'
import type { ComfyExtension } from '@/types/comfy'
export class KeybindingImpl implements Keybinding {
@@ -121,8 +121,7 @@ export const useKeybindingStore = defineStore('keybinding', () => {
const keybindingByKeyCombo = computed<Record<string, KeybindingImpl>>(() => {
const result: Record<string, KeybindingImpl> = {
...defaultKeybindings.value,
...userKeybindings.value
...defaultKeybindings.value
}
for (const keybinding of Object.values(userUnsetKeybindings.value)) {
@@ -131,7 +130,11 @@ export const useKeybindingStore = defineStore('keybinding', () => {
delete result[serializedCombo]
}
}
return result
return {
...result,
...userKeybindings.value
}
})
const keybindings = computed<KeybindingImpl[]>(() =>
@@ -215,13 +218,13 @@ export const useKeybindingStore = defineStore('keybinding', () => {
addKeybinding(userKeybindings, keybinding, { existOk: true })
}
const deprecatedKeybindings = DEPRECATED_KEYBINDINGS.map(
(k) => new KeybindingImpl(k)
)
function unsetKeybinding(keybinding: KeybindingImpl) {
const serializedCombo = keybinding.combo.serialize()
if (!(serializedCombo in keybindingByKeyCombo.value)) {
throw new Error(`Keybinding on ${keybinding.combo} does not exist`)
console.warn(
`Trying to unset non-exist keybinding: ${JSON.stringify(keybinding)}`
)
return
}
if (userKeybindings.value[serializedCombo]?.equals(keybinding)) {
@@ -234,13 +237,7 @@ export const useKeybindingStore = defineStore('keybinding', () => {
return
}
if (deprecatedKeybindings.some((k) => k.equals(keybinding))) {
return
}
console.warn(
`Trying to unset non-exist keybinding: ${JSON.stringify(keybinding)}`
)
throw new Error(`Unknown keybinding: ${JSON.stringify(keybinding)}`)
}
/**

View File

@@ -160,4 +160,76 @@ describe('useKeybindingStore', () => {
defaultKeybinding
)
})
it('Should accept same keybinding from default and user', () => {
const store = useKeybindingStore()
const keybinding = new KeybindingImpl({
commandId: 'test.command',
combo: { key: 'J', ctrl: true }
})
// Add default keybinding.
// This can happen when we change default keybindings.
store.addDefaultKeybinding(keybinding)
// Add user keybinding.
store.addUserKeybinding(keybinding)
expect(store.keybindings).toHaveLength(1)
expect(store.getKeybinding(keybinding.combo)).toEqual(keybinding)
})
it('Should keep previously customized keybindings after default keybindings change', () => {
// Initially command 'foo' was bound to 'K, Ctrl'. User unset it and bound the
// command to 'A, Ctrl'.
// Now we change the default keybindings of 'foo' to 'A, Ctrl'.
// The user customized keybinding should be kept.
const store = useKeybindingStore()
const userUnsetKeybindings = [
new KeybindingImpl({
commandId: 'foo',
combo: { key: 'K', ctrl: true }
})
]
const userNewKeybindings = [
new KeybindingImpl({
commandId: 'foo',
combo: { key: 'A', ctrl: true }
})
]
const newCoreKeybindings = [
new KeybindingImpl({
commandId: 'foo',
combo: { key: 'A', ctrl: true }
})
]
for (const keybinding of newCoreKeybindings) {
store.addDefaultKeybinding(keybinding)
}
expect(store.keybindings).toHaveLength(1)
expect(store.getKeybinding(userNewKeybindings[0].combo)).toEqual(
userNewKeybindings[0]
)
for (const keybinding of userUnsetKeybindings) {
store.unsetKeybinding(keybinding)
}
expect(store.keybindings).toHaveLength(1)
expect(store.getKeybinding(userNewKeybindings[0].combo)).toEqual(
userNewKeybindings[0]
)
for (const keybinding of userNewKeybindings) {
store.addUserKeybinding(keybinding)
}
expect(store.keybindings).toHaveLength(1)
expect(store.getKeybinding(userNewKeybindings[0].combo)).toEqual(
userNewKeybindings[0]
)
})
})