From 6088be077fbda4fdd4ec3bdf6c78aa8e3543a6a6 Mon Sep 17 00:00:00 2001 From: Nicolas Constant Date: Sun, 24 May 2020 16:36:10 -0400 Subject: [PATCH] added escape hotkey, fix #287 --- .../media-viewer/media-viewer.component.ts | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/app/components/media-viewer/media-viewer.component.ts b/src/app/components/media-viewer/media-viewer.component.ts index 9f78c03d..0fc31a62 100644 --- a/src/app/components/media-viewer/media-viewer.component.ts +++ b/src/app/components/media-viewer/media-viewer.component.ts @@ -1,7 +1,8 @@ -import { Component, OnInit, Input, Output, ElementRef, ViewChild, HostListener } from '@angular/core'; +import { Component, OnInit, Input, Output, ElementRef, ViewChild, HostListener, OnDestroy } from '@angular/core'; import { SafeHtml } from '@angular/platform-browser'; import { faTimes, faAngleLeft, faAngleRight } from "@fortawesome/free-solid-svg-icons"; import { Subject } from 'rxjs'; +import { HotkeysService, Hotkey } from 'angular2-hotkeys'; import { OpenMediaEvent } from '../../models/common.model'; import { Attachment, PleromaAttachment } from '../../services/models/mastodon.interfaces'; @@ -12,7 +13,7 @@ import { Attachment, PleromaAttachment } from '../../services/models/mastodon.in templateUrl: './media-viewer.component.html', styleUrls: ['./media-viewer.component.scss'] }) -export class MediaViewerComponent implements OnInit { +export class MediaViewerComponent implements OnInit, OnDestroy { private _mediaEvent: OpenMediaEvent; faTimes = faTimes; faAngleLeft = faAngleLeft; @@ -64,9 +65,20 @@ export class MediaViewerComponent implements OnInit { } } - constructor() { } + private escapeHotkey = new Hotkey('escape', (event: KeyboardEvent): boolean => { + console.warn('CLOSE'); + this.close(); + return false; + }); + constructor(private readonly hotkeysService: HotkeysService) { } + ngOnInit() { + this.hotkeysService.add(this.escapeHotkey); + } + + ngOnDestroy(): void { + this.hotkeysService.remove(this.escapeHotkey); } private setBrowsing() {