File size: 3,623 Bytes
f8b5d42 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | import paths from "./paths";
import { useEffect } from "react";
import { userFromStorage } from "./request";
import { TOGGLE_LLM_SELECTOR_EVENT } from "@/components/WorkspaceChat/ChatContainer/PromptInput/LLMSelector/action";
export const KEYBOARD_SHORTCUTS_HELP_EVENT = "keyboard-shortcuts-help";
export const isMac = navigator.platform.toUpperCase().indexOf("MAC") >= 0;
export const SHORTCUTS = {
"⌘ + ,": {
translationKey: "settings",
action: () => {
window.location.href = paths.settings.interface();
},
},
"⌘ + H": {
translationKey: "home",
action: () => {
window.location.href = paths.home();
},
},
"⌘ + I": {
translationKey: "workspaces",
action: () => {
window.location.href = paths.settings.workspaces();
},
},
"⌘ + K": {
translationKey: "apiKeys",
action: () => {
window.location.href = paths.settings.apiKeys();
},
},
"⌘ + L": {
translationKey: "llmPreferences",
action: () => {
window.location.href = paths.settings.llmPreference();
},
},
"⌘ + Shift + C": {
translationKey: "chatSettings",
action: () => {
window.location.href = paths.settings.chat();
},
},
"⌘ + Shift + ?": {
translationKey: "help",
action: () => {
window.dispatchEvent(
new CustomEvent(KEYBOARD_SHORTCUTS_HELP_EVENT, {
detail: { show: true },
})
);
},
},
F1: {
translationKey: "help",
action: () => {
window.dispatchEvent(
new CustomEvent(KEYBOARD_SHORTCUTS_HELP_EVENT, {
detail: { show: true },
})
);
},
},
"⌘ + Shift + L": {
translationKey: "showLLMSelector",
action: () => {
window.dispatchEvent(new Event(TOGGLE_LLM_SELECTOR_EVENT));
},
},
};
const LISTENERS = {};
const modifier = isMac ? "meta" : "ctrl";
for (const key in SHORTCUTS) {
const listenerKey = key
.replace("⌘", modifier)
.replaceAll(" ", "")
.toLowerCase();
LISTENERS[listenerKey] = SHORTCUTS[key].action;
}
// Convert keyboard event to shortcut key
function getShortcutKey(event) {
let key = "";
if (event.metaKey || event.ctrlKey) key += modifier + "+";
if (event.shiftKey) key += "shift+";
if (event.altKey) key += "alt+";
// Handle special keys
if (event.key === ",") key += ",";
// Handle question mark or slash for help shortcut
else if (event.key === "?" || event.key === "/") key += "?";
else if (event.key === "Control")
return ""; // Ignore Control key by itself
else if (event.key === "Shift")
return ""; // Ignore Shift key by itself
else key += event.key.toLowerCase();
return key;
}
// Initialize keyboard shortcuts
export function initKeyboardShortcuts() {
function handleKeyDown(event) {
const shortcutKey = getShortcutKey(event);
if (!shortcutKey) return;
const action = LISTENERS[shortcutKey];
if (action) {
event.preventDefault();
action();
}
}
window.addEventListener("keydown", handleKeyDown);
return () => window.removeEventListener("keydown", handleKeyDown);
}
function useKeyboardShortcuts() {
useEffect(() => {
// If there is a user and the user is not an admin do not register the event listener
// since some of the shortcuts are only available in multi-user mode as admin
const user = userFromStorage();
if (!!user && user?.role !== "admin") return;
const cleanup = initKeyboardShortcuts();
return () => cleanup();
}, []);
return;
}
export function KeyboardShortcutWrapper({ children }) {
useKeyboardShortcuts();
return children;
}
|