From 55bc557512983994cd46d9069c870d9780eddd26 Mon Sep 17 00:00:00 2001 From: Nicolas Constant Date: Sun, 29 Sep 2019 19:21:43 -0400 Subject: [PATCH] added hotkeys --- .../left-side-bar/left-side-bar.component.ts | 60 +++++++++++++++++++ src/app/services/tools.service.ts | 4 +- 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/src/app/components/left-side-bar/left-side-bar.component.ts b/src/app/components/left-side-bar/left-side-bar.component.ts index 13ec9a0d..d0c7a576 100644 --- a/src/app/components/left-side-bar/left-side-bar.component.ts +++ b/src/app/components/left-side-bar/left-side-bar.component.ts @@ -3,6 +3,7 @@ import { Subscription, Observable } from "rxjs"; import { Store } from "@ngxs/store"; import { faPlus, faCog, faSearch } from "@fortawesome/free-solid-svg-icons"; import { faCommentAlt, faCalendarAlt } from "@fortawesome/free-regular-svg-icons"; +import { HotkeysService, Hotkey } from 'angular2-hotkeys'; import { AccountWrapper } from "../../models/account.models"; import { AccountInfo, SelectAccount } from "../../states/accounts.state"; @@ -33,6 +34,7 @@ export class LeftSideBarComponent implements OnInit, OnDestroy { private notificationSub: Subscription; constructor( + private readonly hotkeysService: HotkeysService, private readonly scheduledStatusService: ScheduledStatusService, private readonly toolsService: ToolsService, private readonly userNotificationServiceService: UserNotificationService, @@ -40,6 +42,63 @@ export class LeftSideBarComponent implements OnInit, OnDestroy { private readonly store: Store) { this.accounts$ = this.store.select(state => state.registeredaccounts.accounts); + + this.hotkeysService.add(new Hotkey('n', (event: KeyboardEvent): boolean => { + this.createNewStatus(); + return false; + })); + + this.hotkeysService.add(new Hotkey('s', (event: KeyboardEvent): boolean => { + this.openSearch(); + return false; + })); + + this.hotkeysService.add(new Hotkey('a', (event: KeyboardEvent): boolean => { + this.addNewAccount(); + return false; + })); + + this.hotkeysService.add(new Hotkey('c', (event: KeyboardEvent): boolean => { + this.navigationService.openPanel(LeftPanelType.Closed); + return false; + })); + + this.hotkeysService.add(new Hotkey('escape', (event: KeyboardEvent): boolean => { + this.navigationService.openPanel(LeftPanelType.Closed); + return false; + })); + + this.hotkeysService.add(new Hotkey('ctrl+up', (event: KeyboardEvent): boolean => { + this.selectPreviousAccount(); + return false; + })); + + this.hotkeysService.add(new Hotkey('ctrl+down', (event: KeyboardEvent): boolean => { + this.selectNextAccount(); + return false; + })); + } + + private selectPreviousAccount(){ + let accounts = this.store.snapshot().registeredaccounts.accounts; + let selectedAccount = accounts.find(x => x.isSelected); + let selectedIndex = accounts.indexOf(selectedAccount); + + if(selectedIndex > 0){ + let previousAccount = accounts[selectedIndex - 1]; + this.store.dispatch([new SelectAccount(previousAccount)]); + } + } + + private selectNextAccount(){ + let accounts = this.store.snapshot().registeredaccounts.accounts; + let selectedAccount = accounts.find(x => x.isSelected); + let selectedIndex = accounts.indexOf(selectedAccount); + + if(selectedIndex < accounts.length - 1){ + let nextAccount = accounts[selectedIndex + 1]; + this.store.dispatch([new SelectAccount(nextAccount)]); + } } ngOnInit() { @@ -100,6 +159,7 @@ export class LeftSideBarComponent implements OnInit, OnDestroy { ngOnDestroy(): void { this.accountSub.unsubscribe(); this.notificationSub.unsubscribe(); + this.scheduledSub.unsubscribe(); } onToogleAccountNotify(acc: AccountWrapper) { diff --git a/src/app/services/tools.service.ts b/src/app/services/tools.service.ts index 356603c6..17ed6d2f 100644 --- a/src/app/services/tools.service.ts +++ b/src/app/services/tools.service.ts @@ -60,12 +60,12 @@ export class ToolsService { } getSelectedAccounts(): AccountInfo[] { - var regAccounts = this.store.snapshot().registeredaccounts.accounts; + let regAccounts = this.store.snapshot().registeredaccounts.accounts; return regAccounts.filter(x => x.isSelected); } getAccountSettings(account: AccountInfo): AccountSettings { - var accountsSettings = this.store.snapshot().globalsettings.settings.accountSettings; + let accountsSettings = this.store.snapshot().globalsettings.settings.accountSettings; let accountSettings = accountsSettings.find(x => x.accountId === account.id); if (!accountSettings) { accountSettings = new AccountSettings();