mirror of
https://github.com/Comfy-Org/ComfyUI_frontend.git
synced 2026-01-26 19:09:52 +00:00
Add reset individual keybind button (#3423)
Co-authored-by: Benjamin Lu <templu1107@proton.me>
This commit is contained in:
@@ -27,6 +27,14 @@
|
||||
class="p-button-text"
|
||||
@click="editKeybinding(slotProps.data)"
|
||||
/>
|
||||
<Button
|
||||
icon="pi pi-replay"
|
||||
class="p-button-text p-button-warn"
|
||||
:disabled="
|
||||
!keybindingStore.isCommandKeybindingModified(slotProps.data.id)
|
||||
"
|
||||
@click="resetKeybinding(slotProps.data)"
|
||||
/>
|
||||
<Button
|
||||
icon="pi pi-trash"
|
||||
class="p-button-text p-button-danger"
|
||||
@@ -254,6 +262,16 @@ async function saveKeybinding() {
|
||||
cancelEdit()
|
||||
}
|
||||
|
||||
async function resetKeybinding(commandData: ICommandData) {
|
||||
if (keybindingStore.resetKeybindingForCommand(commandData.id)) {
|
||||
await keybindingService.persistUserKeybindings()
|
||||
} else {
|
||||
console.warn(
|
||||
`No changes made when resetting keybinding for command: ${commandData.id}`
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
const toast = useToast()
|
||||
async function resetAllKeybindings() {
|
||||
keybindingStore.resetAllKeybindings()
|
||||
|
||||
@@ -283,6 +283,47 @@ export const useKeybindingStore = defineStore('keybinding', () => {
|
||||
userUnsetKeybindings.value = {}
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets the keybinding for a given command to its default value.
|
||||
*
|
||||
* @param commandId - The commandId of the keybind to be reset
|
||||
* @returns `true` if changes were made, `false` if not
|
||||
*/
|
||||
function resetKeybindingForCommand(commandId: string): boolean {
|
||||
const currentKeybinding = getKeybindingByCommandId(commandId)
|
||||
const defaultKeybinding =
|
||||
defaultKeybindingsByCommandId.value[commandId]?.[0]
|
||||
|
||||
// No default keybinding exists, need to remove any user binding
|
||||
if (!defaultKeybinding) {
|
||||
if (currentKeybinding) {
|
||||
unsetKeybinding(currentKeybinding)
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Current binding equals default binding, no changes needed
|
||||
if (currentKeybinding?.equals(defaultKeybinding)) {
|
||||
return false
|
||||
}
|
||||
|
||||
// Unset current keybinding if exists
|
||||
if (currentKeybinding) {
|
||||
unsetKeybinding(currentKeybinding)
|
||||
}
|
||||
|
||||
// Remove the unset record if it exists
|
||||
const serializedCombo = defaultKeybinding.combo.serialize()
|
||||
if (
|
||||
userUnsetKeybindings.value[serializedCombo]?.equals(defaultKeybinding)
|
||||
) {
|
||||
delete userUnsetKeybindings.value[serializedCombo]
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
function isCommandKeybindingModified(commandId: string): boolean {
|
||||
const currentKeybinding: KeybindingImpl | undefined =
|
||||
getKeybindingByCommandId(commandId)
|
||||
@@ -307,6 +348,7 @@ export const useKeybindingStore = defineStore('keybinding', () => {
|
||||
unsetKeybinding,
|
||||
updateKeybindingOnCommand,
|
||||
resetAllKeybindings,
|
||||
resetKeybindingForCommand,
|
||||
isCommandKeybindingModified
|
||||
}
|
||||
})
|
||||
|
||||
@@ -286,4 +286,139 @@ describe('useKeybindingStore', () => {
|
||||
expect(store.getKeybinding(newKeybinding.combo)?.commandId).toBe('command2')
|
||||
expect(store.getKeybindingsByCommandId('command1')).toHaveLength(0)
|
||||
})
|
||||
|
||||
it('should return false when no default or current keybinding exists during reset', () => {
|
||||
const store = useKeybindingStore()
|
||||
const result = store.resetKeybindingForCommand('nonexistent.command')
|
||||
expect(result).toBe(false)
|
||||
})
|
||||
|
||||
it('should return false when current keybinding equals default keybinding', () => {
|
||||
const store = useKeybindingStore()
|
||||
const defaultKeybinding = new KeybindingImpl({
|
||||
commandId: 'test.command',
|
||||
combo: { key: 'L', ctrl: true }
|
||||
})
|
||||
|
||||
store.addDefaultKeybinding(defaultKeybinding)
|
||||
const result = store.resetKeybindingForCommand('test.command')
|
||||
|
||||
expect(result).toBe(false)
|
||||
expect(store.keybindings).toHaveLength(1)
|
||||
expect(store.getKeybindingByCommandId('test.command')).toEqual(
|
||||
defaultKeybinding
|
||||
)
|
||||
})
|
||||
|
||||
it('should unset user keybinding when no default keybinding exists and return true', () => {
|
||||
const store = useKeybindingStore()
|
||||
const userKeybinding = new KeybindingImpl({
|
||||
commandId: 'test.command',
|
||||
combo: { key: 'M', ctrl: true }
|
||||
})
|
||||
|
||||
store.addUserKeybinding(userKeybinding)
|
||||
expect(store.keybindings).toHaveLength(1)
|
||||
|
||||
const result = store.resetKeybindingForCommand('test.command')
|
||||
|
||||
expect(result).toBe(true)
|
||||
expect(store.keybindings).toHaveLength(0)
|
||||
})
|
||||
|
||||
it('should restore default keybinding when user has overridden it and return true', () => {
|
||||
const store = useKeybindingStore()
|
||||
|
||||
const defaultKeybinding = new KeybindingImpl({
|
||||
commandId: 'test.command',
|
||||
combo: { key: 'N', ctrl: true }
|
||||
})
|
||||
|
||||
const userKeybinding = new KeybindingImpl({
|
||||
commandId: 'test.command',
|
||||
combo: { key: 'O', alt: true }
|
||||
})
|
||||
|
||||
store.addDefaultKeybinding(defaultKeybinding)
|
||||
store.updateKeybindingOnCommand(userKeybinding)
|
||||
|
||||
expect(store.keybindings).toHaveLength(1)
|
||||
expect(store.getKeybindingByCommandId('test.command')).toEqual(
|
||||
userKeybinding
|
||||
)
|
||||
|
||||
const result = store.resetKeybindingForCommand('test.command')
|
||||
|
||||
expect(result).toBe(true)
|
||||
expect(store.keybindings).toHaveLength(1)
|
||||
expect(store.getKeybindingByCommandId('test.command')).toEqual(
|
||||
defaultKeybinding
|
||||
)
|
||||
})
|
||||
|
||||
it('should remove unset record and restore default keybinding when user has unset it', () => {
|
||||
const store = useKeybindingStore()
|
||||
|
||||
const defaultKeybinding = new KeybindingImpl({
|
||||
commandId: 'test.command',
|
||||
combo: { key: 'P', ctrl: true }
|
||||
})
|
||||
|
||||
store.addDefaultKeybinding(defaultKeybinding)
|
||||
|
||||
store.unsetKeybinding(defaultKeybinding)
|
||||
expect(store.keybindings).toHaveLength(0)
|
||||
|
||||
const serializedCombo = defaultKeybinding.combo.serialize()
|
||||
const userUnsetKeybindings = store.getUserUnsetKeybindings()
|
||||
expect(userUnsetKeybindings[serializedCombo]).toBeTruthy()
|
||||
expect(
|
||||
userUnsetKeybindings[serializedCombo].equals(defaultKeybinding)
|
||||
).toBe(true)
|
||||
|
||||
const result = store.resetKeybindingForCommand('test.command')
|
||||
|
||||
expect(result).toBe(true)
|
||||
expect(store.keybindings).toHaveLength(1)
|
||||
expect(store.getKeybindingByCommandId('test.command')).toEqual(
|
||||
defaultKeybinding
|
||||
)
|
||||
|
||||
expect(store.getUserUnsetKeybindings()[serializedCombo]).toBeUndefined()
|
||||
})
|
||||
|
||||
it('should handle complex scenario with both unset and user keybindings', () => {
|
||||
const store = useKeybindingStore()
|
||||
|
||||
// Create default keybinding
|
||||
const defaultKeybinding = new KeybindingImpl({
|
||||
commandId: 'test.command',
|
||||
combo: { key: 'Q', ctrl: true }
|
||||
})
|
||||
store.addDefaultKeybinding(defaultKeybinding)
|
||||
|
||||
// Unset default keybinding
|
||||
store.unsetKeybinding(defaultKeybinding)
|
||||
expect(store.keybindings).toHaveLength(0)
|
||||
|
||||
// Add user keybinding with different combo
|
||||
const userKeybinding = new KeybindingImpl({
|
||||
commandId: 'test.command',
|
||||
combo: { key: 'R', alt: true }
|
||||
})
|
||||
store.addUserKeybinding(userKeybinding)
|
||||
expect(store.keybindings).toHaveLength(1)
|
||||
expect(store.getKeybindingByCommandId('test.command')).toEqual(
|
||||
userKeybinding
|
||||
)
|
||||
|
||||
// Reset keybinding to default
|
||||
const result = store.resetKeybindingForCommand('test.command')
|
||||
|
||||
expect(result).toBe(true)
|
||||
expect(store.keybindings).toHaveLength(1)
|
||||
expect(store.getKeybindingByCommandId('test.command')).toEqual(
|
||||
defaultKeybinding
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user