🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
✨ feat(desktop): 支持 Ctrl+数字切换标签并修复连续切换 (#22225)
Mocha2f6102562b08
diff --git a/packages/const/src/hotkeys.ts b/packages/const/src/hotkeys.tsindex b92cb2fac03..25d91483b21 100644--- a/packages/const/src/hotkeys.ts+++ b/packages/const/src/hotkeys.ts@@ -1,5 +1,7 @@import type { HotkeyGroupId, HotkeyId, HotkeyRegistration, HotkeyScopeId } from '@lobechat/types';+import { isDesktop } from './version';+const combineKeys = (keys: string[]) => keys.join('+');export const KeyEnum = {@@ -92,17 +94,22 @@ export const HOTKEYS_REGISTRATION: HotkeyRegistration = [keys: combineKeys([KeyEnum.Mod, 'j']),scopes: [HotkeyScopeEnum.Global],},- {- group: HotkeyGroupEnum.Essential,- id: HotkeyEnum.SwitchAgent,- keys: combineKeys([KeyEnum.Ctrl, KeyEnum.Number]),- nonEditable: true,- scopes: [HotkeyScopeEnum.Global],- },+ // Desktop's number shortcuts select titlebar tabs, not agents.+ ...(!isDesktop+ ? [+ {+ group: HotkeyGroupEnum.Essential,+ id: HotkeyEnum.SwitchAgent,+ keys: combineKeys([KeyEnum.Ctrl, KeyEnum.Number]),+ nonEditable: true,+ scopes: [HotkeyScopeEnum.Global],+ },+ ]+ : []),{group: HotkeyGroupEnum.Essential,id: HotkeyEnum.SwitchTab,- keys: combineKeys([KeyEnum.Mod, KeyEnum.Number]),+ keys: combineKeys([KeyEnum.Ctrl, KeyEnum.Number]),nonEditable: true,scopes: [HotkeyScopeEnum.Global],},diff --git a/src/hooks/useHotkeys/desktopTabScope.test.ts b/src/hooks/useHotkeys/desktopTabScope.test.tsnew file mode 100644index 00000000000..a3f20176395--- /dev/null+++ b/src/hooks/useHotkeys/desktopTabScope.test.ts@@ -0,0 +1,112 @@+import { act, cleanup, renderHook } from '@testing-library/react';+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';++import { useRegisterDesktopTabHotkeys } from './desktopTabScope';++const state = vi.hoisted(() => ({+ activeTabId: 'second',+ switchTab: vi.fn(),+ tabs: [{ id: 'first' }, { id: 'second' }, { id: 'third' }],+}));++vi.mock('@/store/electron', () => ({+ useElectronStore: { getState: () => state },+}));++const press = (+ key: string,+ code: string,+ modifiers: KeyboardEventInit = {},+ target: EventTarget = document,+) => {+ act(() => {+ target.dispatchEvent(new KeyboardEvent('keydown', { key, code, bubbles: true, ...modifiers }));+ target.dispatchEvent(new KeyboardEvent('keyup', { key, code, bubbles: true, ...modifiers }));+ });+};++describe('desktop tab shortcuts', () => {+ beforeEach(() => {+ state.activeTabId = 'second';+ state.tabs = [{ id: 'first' }, { id: 'second' }, { id: 'third' }];+ state.switchTab.mockReset();+ state.switchTab.mockImplementation((id: string) => {+ state.activeTabId = id;+ });+ });++ afterEach(() => {+ cleanup();+ document.body.replaceChildren();+ });++ it('selects tabs by physical number key on non-US keyboard layouts', () => {+ renderHook(useRegisterDesktopTabHotkeys);++ press('&', 'Digit1', { ctrlKey: true });+ expect(state.switchTab).toHaveBeenLastCalledWith('first');++ press('3', 'Digit3', { ctrlKey: true });+ expect(state.switchTab).toHaveBeenLastCalledWith('third');+ });++ it('cycles in both directions and wraps at either end', () => {+ renderHook(useRegisterDesktopTabHotkeys);++ state.activeTabId = 'third';+ press('Tab', 'Tab', { ctrlKey: true });+ expect(state.switchTab).toHaveBeenLastCalledWith('first');++ state.activeTabId = 'first';+ press('Tab', 'Tab', { ctrlKey: true, shiftKey: true });+ expect(state.switchTab).toHaveBeenLastCalledWith('third');+ });++ it('works while the chat editor is focused', () => {+ const editor = document.createElement('div');+ editor.contentEditable = 'true';+ // jsdom does not implement this browser property.+ Object.defineProperty(editor, 'isContentEditable', { value: true });+ document.body.append(editor);+ editor.focus();+ renderHook(useRegisterDesktopTabHotkeys);++ press('1', 'Digit1', { ctrlKey: true }, editor);+ expect(state.switchTab).toHaveBeenLastCalledWith('first');+ press('Tab', 'Tab', { ctrlKey: true }, editor);+ expect(state.switchTab).toHaveBeenLastCalledWith('second');+ });++ it('keeps cycling after the destination chat editor takes focus, without another click', () => {+ const editor = document.createElement('div');+ editor.contentEditable = 'true';+ Object.defineProperty(editor, 'isContentEditable', { value: true });+ document.body.append(editor);+ renderHook(useRegisterDesktopTabHotkeys);++ press('Tab', 'Tab', { ctrlKey: true });+ expect(state.activeTabId).toBe('third');+ editor.focus();++ for (const expected of ['first', 'second', 'third', 'first']) {+ press('Tab', 'Tab', { ctrlKey: true }, editor);+ expect(state.activeTabId).toBe(expected);+ }+ for (const expected of ['third', 'second', 'first', 'third']) {+ press('Tab', 'Tab', { ctrlKey: true, shiftKey: true }, editor);+ expect(state.activeTabId).toBe(expected);+ }+ });++ it('ignores missing tabs, empty lists and unrelated modifier combinations', () => {+ renderHook(useRegisterDesktopTabHotkeys);+ press('9', 'Digit9', { ctrlKey: true });+ press('1', 'Digit1', { altKey: true });+ press('1', 'Digit1', { altKey: true, ctrlKey: true });+ press('Tab', 'Tab', { altKey: true });+ state.tabs = [];+ press('Tab', 'Tab', { ctrlKey: true });+ press('Tab', 'Tab', { ctrlKey: true, shiftKey: true });+ expect(state.switchTab).not.toHaveBeenCalled();+ });+});diff --git a/src/hooks/useHotkeys/desktopTabScope.ts b/src/hooks/useHotkeys/desktopTabScope.tsindex 7adf4338b24..1fb3ab8363a 100644--- a/src/hooks/useHotkeys/desktopTabScope.ts+++ b/src/hooks/useHotkeys/desktopTabScope.ts@@ -1,12 +1,12 @@'use client';-import { useCallback } from 'react';+import { DEFAULT_HOTKEY_CONFIG } from '@lobechat/const';import { useHotkeys } from 'react-hotkeys-hook';import { useElectronStore } from '@/store/electron';/**- * Mod+1–9: jump to the Nth tab.+ * Ctrl+1–9: jump to the Nth tab.* Ctrl+Tab: cycle to the next tab, wrapping around.* Ctrl+Shift+Tab: cycle to the previous tab, wrapping around.*@@ -14,25 +14,20 @@ import { useElectronStore } from '@/store/electron';* (e.g. TabBar) — no `isDesktop` guard needed.*/export const useRegisterDesktopTabHotkeys = () => {- const switchToTabByIndex = useCallback((index: number) => {- const { tabs, switchTab } = useElectronStore.getState();- if (index < 0 || index >= tabs.length) return;-- const target = tabs[index];- switchTab(target.id);- }, []);-- // Mod+1 through Mod+9+ // The settings page displays a range; react-hotkeys-hook needs individual keys.useHotkeys(- 'mod+1,mod+2,mod+3,mod+4,mod+5,mod+6,mod+7,mod+8,mod+9',+ Array.from({ length: 9 }, (_, i) =>+ DEFAULT_HOTKEY_CONFIG.switchTab.replace('1-9', String(i + 1)),+ ),(e) => {- e.preventDefault();- const digit = Number(e.key);- if (digit >= 1 && digit <= 9) {- switchToTabByIndex(digit - 1);- }+ // Use the physical number key, including keyboard layouts such as AZERTY.+ const digit = Number(e.code.replace(/^(Digit|Numpad)/, ''));+ const { tabs, switchTab } = useElectronStore.getState();+ const target = tabs[digit - 1];+ if (target) switchTab(target.id);},{+ enableOnContentEditable: true,enableOnFormTags: true,preventDefault: true,},@@ -40,7 +35,7 @@ export const useRegisterDesktopTabHotkeys = () => {// Ctrl+Tab: next tab (wrap around)useHotkeys(- 'ctrl+tab',+ DEFAULT_HOTKEY_CONFIG.nextTab,(e) => {e.preventDefault();const { tabs, activeTabId, switchTab } = useElectronStore.getState();@@ -53,6 +48,7 @@ export const useRegisterDesktopTabHotkeys = () => {switchTab(target.id);},{+ enableOnContentEditable: true,enableOnFormTags: true,preventDefault: true,},@@ -60,7 +56,7 @@ export const useRegisterDesktopTabHotkeys = () => {// Ctrl+Shift+Tab: previous tab (wrap around)useHotkeys(- 'ctrl+shift+tab',+ DEFAULT_HOTKEY_CONFIG.prevTab,(e) => {e.preventDefault();const { tabs, activeTabId, switchTab } = useElectronStore.getState();@@ -73,6 +69,7 @@ export const useRegisterDesktopTabHotkeys = () => {switchTab(target.id);},{+ enableOnContentEditable: true,enableOnFormTags: true,preventDefault: true,},