-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Consolidate keyboard shortcuts into a single registry #7314
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,26 +1,69 @@ | ||
| import { LitElement } from "lit-element"; | ||
| import { Constructor } from "../types"; | ||
|
|
||
| type RegisteredShortcuts = { | ||
| [key in HAKeyboardShortcut]?: () => void; | ||
| }; | ||
|
|
||
| export enum HAKeyboardShortcut { | ||
| CTRL_S = "CtrlS", | ||
| CTRL_P = "CtrlP", | ||
| CTRL_SHIFT_P = "CtrlShiftP", | ||
| } | ||
|
|
||
| const isMac = /(Mac|iPhone|iPod|iPad)/i.test(navigator.platform); | ||
|
|
||
| export const KeyboardShortcutMixin = <T extends Constructor<LitElement>>( | ||
| superClass: T | ||
| ) => | ||
| class extends superClass { | ||
| private _keydownEvent = (event: KeyboardEvent) => { | ||
| if ((event.ctrlKey || event.metaKey) && event.key === "s") { | ||
| event.preventDefault(); | ||
| this.handleKeyboardSave(); | ||
| } | ||
| }; | ||
|
|
||
| public connectedCallback() { | ||
| super.connectedCallback(); | ||
| this.addEventListener("keydown", this._keydownEvent); | ||
| } | ||
| private _registeredShortcuts: RegisteredShortcuts = {}; | ||
|
|
||
| public disconnectedCallback() { | ||
| this.removeEventListener("keydown", this._keydownEvent); | ||
donkawechico marked this conversation as resolved.
Show resolved
Hide resolved
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we remove the listener on disconnected, we should add it again on connected. The firstupdated will only run once, so after the element is disconnected the listener will never be added again on connect
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, this will not remove it now, as the callbacks for the add and remove are different. |
||
| super.disconnectedCallback(); | ||
| } | ||
|
|
||
| protected handleKeyboardSave() {} | ||
| private _executeShortcut( | ||
| event: KeyboardEvent, | ||
| shortcut: HAKeyboardShortcut | ||
| ) { | ||
| if (Object.keys(this._registeredShortcuts).includes(shortcut)) { | ||
| event.preventDefault(); | ||
| const shortcutAction = this._registeredShortcuts[shortcut]; | ||
| if (shortcutAction) { | ||
| shortcutAction(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| protected registerShortcut( | ||
| shortcut: HAKeyboardShortcut, | ||
| callback: () => void, | ||
| element: HTMLElement | Document = this | ||
| ) { | ||
| element.addEventListener("keydown", (event) => this._keydownEvent(event)); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should not do this in |
||
| this._registeredShortcuts[shortcut] = callback; | ||
| } | ||
|
|
||
| private _keydownEvent = (event: KeyboardEvent) => { | ||
| if (this.isOSCtrlKey(event)) { | ||
| switch (event.code) { | ||
| case "KeyS": | ||
| this._executeShortcut(event, HAKeyboardShortcut.CTRL_S); | ||
| break; | ||
| case "KeyP": | ||
| if (event.shiftKey) { | ||
| this._executeShortcut(event, HAKeyboardShortcut.CTRL_SHIFT_P); | ||
| } else { | ||
| this._executeShortcut(event, HAKeyboardShortcut.CTRL_P); | ||
| } | ||
| break; | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| private isOSCtrlKey(e: KeyboardEvent) { | ||
| return isMac ? e.metaKey : e.ctrlKey; | ||
| } | ||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needed due to issue with making
enumtypes in typescript with default eslint.