fix: 修复 Windows 快捷键显示异常 (#2599)
Co-authored-by: staff <staff@qimaos-MacBook-Pro.local>
This commit is contained in:
1 parent
526540d570
commit
dc50356000
7 files changed
+102
-17
No files matched your search
@@ -1,3 +1,4 @@
|
||||
import { parseShortcutParts } from "@/lib/editor/shortcutDisplay";
|
||||
import { normalizeShortcutSettings, type ShortcutActionId, type ShortcutSettings } from "@/lib/editor/shortcutRegistry";
|
||||
|
||||
export interface ShortcutLikeEvent {
|
||||
@@ -11,11 +12,13 @@ export interface ShortcutLikeEvent {
|
||||
|
||||
function normalizeKey(key: string): string {
|
||||
if (key === " ") return "Space";
|
||||
if (key === "+" || key === "Plus") return "Plus";
|
||||
return key.length === 1 ? key.toLowerCase() : key;
|
||||
}
|
||||
|
||||
function shortcutKeyName(key: string): string | null {
|
||||
if (key === " ") return "Space";
|
||||
if (key === "+") return "Plus";
|
||||
if (["Control", "Meta", "Shift", "Alt"].includes(key)) return null;
|
||||
if (key.length === 1) return key.toUpperCase();
|
||||
return key;
|
||||
@@ -28,7 +31,7 @@ export function eventToShortcut(event: ShortcutLikeEvent): string | null {
|
||||
if (!key) return null;
|
||||
|
||||
const hasModifier = !!event.metaKey || !!event.ctrlKey || !!event.altKey || !!event.shiftKey;
|
||||
if (!hasModifier && key.length === 1) return null;
|
||||
if (!hasModifier && event.key.length === 1 && event.key !== " ") return null;
|
||||
|
||||
const parts: string[] = [];
|
||||
if (event.shiftKey) parts.push("Shift");
|
||||
@@ -40,7 +43,7 @@ export function eventToShortcut(event: ShortcutLikeEvent): string | null {
|
||||
|
||||
export function matchesShortcut(event: ShortcutLikeEvent, shortcut: string): boolean {
|
||||
if (event.isComposing || !shortcut) return false;
|
||||
const parts = shortcut.split("+");
|
||||
const parts = parseShortcutParts(shortcut);
|
||||
const key = parts[parts.length - 1] ?? "";
|
||||
const modifiers = new Set(parts.slice(0, -1));
|
||||
const usesMod = modifiers.has("Mod");
|
||||
|
||||
@@ -2,6 +2,47 @@ export function isMacShortcutPlatform(platform = globalThis.navigator?.platform
|
||||
return platform.toLowerCase().includes("mac");
|
||||
}
|
||||
|
||||
export function parseShortcutParts(shortcut?: string): string[] {
|
||||
if (!shortcut) return [];
|
||||
|
||||
if (shortcut.endsWith("++")) {
|
||||
const prefix = shortcut.slice(0, -2);
|
||||
return [...prefix.split("+").filter(Boolean), "Plus"];
|
||||
}
|
||||
|
||||
return shortcut.split("+").filter(Boolean);
|
||||
}
|
||||
|
||||
function shortcutDisplayOrder(parts: string[], platform = globalThis.navigator?.platform || ""): string[] {
|
||||
if (parts.length <= 2 || isMacShortcutPlatform(platform)) return parts;
|
||||
|
||||
const key = parts[parts.length - 1];
|
||||
const modifierOrder = new Map([
|
||||
["Mod", 0],
|
||||
["Ctrl", 0],
|
||||
["Control", 0],
|
||||
["Meta", 0],
|
||||
["Cmd", 0],
|
||||
["Shift", 1],
|
||||
["Alt", 2],
|
||||
]);
|
||||
const modifiers = parts.slice(0, -1);
|
||||
if (!modifiers.some((part) => modifierOrder.get(part) === 0)) return parts;
|
||||
|
||||
return [
|
||||
...[...modifiers].sort((a, b) => {
|
||||
const rankA = modifierOrder.get(a) ?? 99;
|
||||
const rankB = modifierOrder.get(b) ?? 99;
|
||||
return rankA - rankB;
|
||||
}),
|
||||
key,
|
||||
];
|
||||
}
|
||||
|
||||
export function shortcutDisplayParts(shortcut?: string, platform = globalThis.navigator?.platform || ""): string[] {
|
||||
return shortcutDisplayOrder(parseShortcutParts(shortcut), platform);
|
||||
}
|
||||
|
||||
export function shortcutKeyLabel(part: string, platform = globalThis.navigator?.platform || ""): string {
|
||||
const isMac = isMacShortcutPlatform(platform);
|
||||
if (part === "Mod") return isMac ? "⌘" : "Ctrl";
|
||||
@@ -19,16 +60,12 @@ export function shortcutKeyLabel(part: string, platform = globalThis.navigator?.
|
||||
if (part === "ArrowLeft") return "←";
|
||||
if (part === "ArrowRight") return "→";
|
||||
if (part === " ") return "Space";
|
||||
if (part === "Plus") return "+";
|
||||
return part.length === 1 ? part.toUpperCase() : part;
|
||||
}
|
||||
|
||||
export function shortcutDisplayKeys(shortcut?: string, platform = globalThis.navigator?.platform || ""): string[] {
|
||||
return (
|
||||
shortcut
|
||||
?.split("+")
|
||||
.filter(Boolean)
|
||||
.map((part) => shortcutKeyLabel(part, platform)) || []
|
||||
);
|
||||
return shortcutDisplayParts(shortcut, platform).map((part) => shortcutKeyLabel(part, platform));
|
||||
}
|
||||
|
||||
export function formatShortcutDisplay(shortcut: string, platform = globalThis.navigator?.platform || ""): string {
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import { parseShortcutParts, shortcutDisplayParts } from "@/lib/editor/shortcutDisplay";
|
||||
|
||||
export type ShortcutActionId =
|
||||
| "executeSql"
|
||||
| "formatSql"
|
||||
@@ -350,19 +352,19 @@ export function normalizeShortcutSettings(settings?: Partial<ShortcutSettings>):
|
||||
}
|
||||
|
||||
export function shortcutToCodeMirrorKey(shortcut: string): string {
|
||||
return shortcut
|
||||
.split("+")
|
||||
return parseShortcutParts(shortcut)
|
||||
.map((part) => (part.length === 1 ? part.toLowerCase() : part))
|
||||
.map((part) => (part === "Plus" ? "+" : part))
|
||||
.join("-");
|
||||
}
|
||||
|
||||
export function formatShortcut(shortcut: string, platform = globalThis.navigator?.platform || ""): string {
|
||||
const isMac = platform.toLowerCase().includes("mac");
|
||||
return shortcut
|
||||
.split("+")
|
||||
return shortcutDisplayParts(shortcut, platform)
|
||||
.map((part) => {
|
||||
if (part === "Mod") return isMac ? "Cmd" : "Ctrl";
|
||||
if (part === "Meta") return isMac ? "Cmd" : "Meta";
|
||||
if (part === "Plus") return "+";
|
||||
return part;
|
||||
})
|
||||
.join("+");
|
||||
|
||||
Reference in new issue
Block a user