| 1 | import React from "react"; |
| 2 | import "../../../src/index.css"; |
| 3 | import { |
| 4 | getAltKeyLabel, |
| 5 | getFontSize, |
| 6 | getMetaKeyLabel, |
| 7 | getPlatform, |
| 8 | } from "../../util"; |
| 9 | import "./Shortcut.css"; |
| 10 | |
| 11 | interface ShortcutProps { |
| 12 | children: string; |
| 13 | } |
| 14 | |
| 15 | const fontSize = getFontSize(); |
| 16 | |
| 17 | const metaKeys = ["meta", "⌘", "ctrl", "cmd", "^"]; |
| 18 | const altKeys = ["alt", "option", "opt", "⌥"]; |
| 19 | const modifierKeys = [...metaKeys, ...altKeys]; |
| 20 | |
| 21 | const getSpecialKeyMap = (platform: string): Record<string, string> => ({ |
| 22 | uparrow: "UpArrow ↑", |
| 23 | downarrow: "DownArrow ↓", |
| 24 | leftarrow: "LeftArrow ←", |
| 25 | rightarrow: "RightArrow →", |
| 26 | enter: "Enter ⏎", |
| 27 | esc: "Esc", |
| 28 | backspace: platform === "mac" ? "Delete ⌫" : "Backspace ⌫", |
| 29 | delete: platform === "mac" ? "Delete ⌫" : "Backspace ⌫", |
| 30 | "⌫": platform === "mac" ? "Delete ⌫" : "Backspace ⌫", |
| 31 | }); |
| 32 | |
| 33 | const parseShortcut = (shortcut: string, platform: string) => { |
| 34 | if (!shortcut || typeof shortcut !== "string") { |
| 35 | console.warn("Invalid shortcut provided:", shortcut); |
| 36 | return []; |
| 37 | } |
| 38 | |
| 39 | const specialKeyMap = getSpecialKeyMap(platform); |
| 40 | return shortcut |
| 41 | .split(",") |
| 42 | .map((combo) => |
| 43 | combo |
| 44 | .trim() |
| 45 | .split(" ") |
| 46 | .filter((key) => key) |
| 47 | .map((key) => { |
| 48 | const lowerKey = key.toLowerCase(); |
| 49 | if (metaKeys.includes(lowerKey)) return getMetaKeyLabel(); |
| 50 | if (altKeys.includes(lowerKey)) return getAltKeyLabel(); |
| 51 | return specialKeyMap[lowerKey] || capitalizeKey(key); |
| 52 | }), |
| 53 | ) |
| 54 | .filter((combo) => combo.length > 0); |
| 55 | }; |
| 56 | |
| 57 | const capitalizeKey = (key: string) => |
| 58 | key.charAt(0).toUpperCase() + key.slice(1).toLowerCase(); |
| 59 | |
| 60 | const isSingleCharNonModifier = (key: string) => |
| 61 | key && key.length === 1 && /^[a-zA-Z0-9]$/.test(key); |
| 62 | |
| 63 | const Shortcut: React.FC<ShortcutProps> = ({ children }) => { |
| 64 | const platform = getPlatform(); |
| 65 | if (!children || typeof children !== "string") { |
| 66 | return <span>Error: Invalid shortcut key</span>; |
| 67 | } |
| 68 | |
| 69 | const shortcuts = parseShortcut(children, platform); |
| 70 | |
| 71 | return ( |
| 72 | <> |
| 73 | {shortcuts.map((combo, comboIndex) => ( |
| 74 | <React.Fragment key={comboIndex}> |
| 75 | {combo.map((key, keyIndex) => ( |
| 76 | <React.Fragment key={keyIndex}> |
| 77 | <kbd |
| 78 | className={`keyboard-key ${ |
| 79 | !isSingleCharNonModifier(key) || key === "⌫" |
| 80 | ? "keyboard-key-special" |
| 81 | : "keyboard-key-normal" |
| 82 | }`} |
| 83 | style={{ |
| 84 | fontSize: |
| 85 | key && |
| 86 | modifierKeys.includes(key.toLowerCase()) && |
| 87 | platform === "mac" |
| 88 | ? `${fontSize - 2}px` |
| 89 | : `${fontSize - 3}px`, |
| 90 | }} |
| 91 | > |
| 92 | {key || "?"} |
| 93 | </kbd> |
| 94 | {keyIndex < combo.length - 1 && ( |
| 95 | <span className="separator">+</span> |
| 96 | )} |
| 97 | </React.Fragment> |
| 98 | ))} |
| 99 | {comboIndex < shortcuts.length - 1 && ( |
| 100 | <span className="separator">,</span> |
| 101 | )} |
| 102 | </React.Fragment> |
| 103 | ))} |
| 104 | </> |
| 105 | ); |
| 106 | }; |
| 107 | |
| 108 | export default Shortcut; |