-
Notifications
You must be signed in to change notification settings - Fork 405
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add hotkey for opening search dialog (#2633)
- Loading branch information
Showing
7 changed files
with
112 additions
and
7 deletions.
There are no files selected for viewing
23 changes: 19 additions & 4 deletions
23
plugins/base/frontend/src/main/components/search/dokkaSearchAnchor.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import {detectOsKind, OsKind} from "./os"; | ||
|
||
type ModifierKey = { | ||
name: string | ||
keyArg: string | ||
} | ||
|
||
class ModifierKeys { | ||
static metaKey: ModifierKey = {name: "Command", keyArg: "Meta"} | ||
static ctrlKey: ModifierKey = {name: "Ctrl", keyArg: "Control"} | ||
static altKey: ModifierKey = {name: "Alt", keyArg: "Alt"} | ||
static shiftKey: ModifierKey = {name: "Shift", keyArg: "Shift"} | ||
} | ||
|
||
const setOfKeys = [ModifierKeys.altKey, ModifierKeys.shiftKey, ModifierKeys.ctrlKey, ModifierKeys.metaKey] | ||
|
||
export class Hotkey { | ||
private readonly osKind: OsKind; | ||
|
||
constructor() { | ||
this.osKind = detectOsKind() | ||
} | ||
|
||
public getOsAccelKeyName() { | ||
return this.getOsAccelKey().name | ||
} | ||
|
||
/** | ||
* Register a hotkey of combination Accel key (Cmd/Ctrl depending on OS). | ||
* The method also checks that other modifiers key is not pressed to avoid shortcuts intersection. | ||
* E.g. don't trigger [Ctrl+K] if [Ctrl + Shift + K] pressed | ||
*/ | ||
public registerHotkeyWithAccel = (event: () => void, letter: string) => { | ||
const osMetaKey = this.getOsAccelKey() | ||
document.onkeydown = (keyDownEvent) => { | ||
const isMetaKeyPressed = keyDownEvent.getModifierState(osMetaKey.keyArg) | ||
const isOtherModifierKeyPressed = setOfKeys | ||
.filter(key => key !== osMetaKey) | ||
.map((otherKeys: ModifierKey) => keyDownEvent.getModifierState(otherKeys.keyArg)) | ||
.some(value => value) | ||
|
||
if (isMetaKeyPressed && !isOtherModifierKeyPressed && keyDownEvent.key === letter) { | ||
keyDownEvent.preventDefault() | ||
event() | ||
} | ||
}; | ||
} | ||
|
||
private getOsAccelKey(): ModifierKey { | ||
switch (this.osKind) { | ||
case OsKind.MACOS: | ||
return ModifierKeys.metaKey | ||
default: | ||
return ModifierKeys.ctrlKey | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export enum OsKind{ | ||
WINDOWS, | ||
MACOS, | ||
LINUX, | ||
OTHER | ||
} | ||
|
||
export const detectOsKind = (): OsKind => { | ||
const userAgent = navigator.userAgent | ||
if(userAgent.includes("Mac")) return OsKind.MACOS | ||
else if (userAgent.includes("Win")) return OsKind.WINDOWS | ||
else if (userAgent.includes("Linux")) return OsKind.LINUX | ||
else return OsKind.OTHER | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
declare module '@jetbrains/ring-ui' { | ||
export const Tooltip: any; | ||
export const Select: any; | ||
export const List: any; | ||
} |