fix(client): shortcut keys without modifiers affecting normal usage

This commit is contained in:
Elian Doran 2025-10-01 21:08:51 +03:00
parent 95b1c82ccb
commit b4c20d9683
No known key found for this signature in database
2 changed files with 13 additions and 8 deletions

View File

@ -119,11 +119,6 @@ describe("shortcuts", () => {
metaKey: options.metaKey || false metaKey: options.metaKey || false
} as KeyboardEvent); } as KeyboardEvent);
it("should match simple key shortcuts", () => {
const event = createKeyboardEvent({ key: "a", code: "KeyA" });
expect(matchesShortcut(event, "a")).toBe(true);
});
it("should match shortcuts with modifiers", () => { it("should match shortcuts with modifiers", () => {
const event = createKeyboardEvent({ key: "a", code: "KeyA", ctrlKey: true }); const event = createKeyboardEvent({ key: "a", code: "KeyA", ctrlKey: true });
expect(matchesShortcut(event, "ctrl+a")).toBe(true); expect(matchesShortcut(event, "ctrl+a")).toBe(true);
@ -148,6 +143,11 @@ describe("shortcuts", () => {
expect(matchesShortcut(event, "a")).toBe(false); expect(matchesShortcut(event, "a")).toBe(false);
}); });
it("should not match when no modifiers are used", () => {
const event = createKeyboardEvent({ key: "a", code: "KeyA" });
expect(matchesShortcut(event, "a")).toBe(false);
});
it("should handle alternative modifier names", () => { it("should handle alternative modifier names", () => {
const ctrlEvent = createKeyboardEvent({ key: "a", code: "KeyA", ctrlKey: true }); const ctrlEvent = createKeyboardEvent({ key: "a", code: "KeyA", ctrlKey: true });
expect(matchesShortcut(ctrlEvent, "control+a")).toBe(true); expect(matchesShortcut(ctrlEvent, "control+a")).toBe(true);

View File

@ -51,7 +51,7 @@ export function isIMEComposing(e: KeyboardEvent): boolean {
if (!e) { if (!e) {
return false; return false;
} }
// Standard check for composition state // Standard check for composition state
// e.isComposing is true when IME is actively composing // e.isComposing is true when IME is actively composing
// e.keyCode === 229 is a fallback for older browsers where 229 indicates IME processing // e.keyCode === 229 is a fallback for older browsers where 229 indicates IME processing
@ -86,13 +86,13 @@ function bindElShortcut($el: JQuery<ElementType | Element>, keyboardShortcut: st
} }
const e = evt as KeyboardEvent; const e = evt as KeyboardEvent;
// Skip processing if IME is composing to prevent shortcuts from // Skip processing if IME is composing to prevent shortcuts from
// interfering with text input in CJK languages // interfering with text input in CJK languages
if (isIMEComposing(e)) { if (isIMEComposing(e)) {
return; return;
} }
if (matchesShortcut(e, keyboardShortcut)) { if (matchesShortcut(e, keyboardShortcut)) {
e.preventDefault(); e.preventDefault();
e.stopPropagation(); e.stopPropagation();
@ -162,6 +162,11 @@ export function matchesShortcut(e: KeyboardEvent, shortcut: string): boolean {
const expectedShift = modifiers.includes('shift'); const expectedShift = modifiers.includes('shift');
const expectedMeta = modifiers.includes('meta') || modifiers.includes('cmd') || modifiers.includes('command'); const expectedMeta = modifiers.includes('meta') || modifiers.includes('cmd') || modifiers.includes('command');
// Refuse key combinations that don't include modifiers because they interfere with the normal usage of the application.
if (!(expectedCtrl || expectedAlt || expectedShift || expectedMeta)) {
return false;
}
return e.ctrlKey === expectedCtrl && return e.ctrlKey === expectedCtrl &&
e.altKey === expectedAlt && e.altKey === expectedAlt &&
e.shiftKey === expectedShift && e.shiftKey === expectedShift &&