Skip to content

Commit d587615

Browse files
feat(module:image): close image preview when escape key pressed (#8809)
1 parent 99fd4de commit d587615

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

components/image/image-preview.component.ts

+16-1
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@
55

66
import { AnimationEvent } from '@angular/animations';
77
import { CdkDrag, CdkDragEnd, CdkDragHandle } from '@angular/cdk/drag-drop';
8+
import { ESCAPE } from '@angular/cdk/keycodes';
9+
import { DOCUMENT } from '@angular/common';
810
import {
911
ChangeDetectionStrategy,
1012
ChangeDetectorRef,
1113
Component,
1214
ElementRef,
1315
EventEmitter,
16+
Inject,
1417
NgZone,
1518
OnInit,
1619
ViewChild,
@@ -248,7 +251,8 @@ export class NzImagePreviewComponent implements OnInit {
248251
public nzConfigService: NzConfigService,
249252
public config: NzImagePreviewOptions,
250253
private destroy$: NzDestroyService,
251-
private sanitizer: DomSanitizer
254+
private sanitizer: DomSanitizer,
255+
@Inject(DOCUMENT) private document: Document
252256
) {
253257
this.zoom = this.config.nzZoom ?? this._defaultNzZoom;
254258
this.scaleStep = this.config.nzScaleStep ?? this._defaultNzScaleStep;
@@ -273,6 +277,17 @@ export class NzImagePreviewComponent implements OnInit {
273277
.subscribe(event => {
274278
this.ngZone.run(() => this.wheelZoomEventHandler(event));
275279
});
280+
281+
fromEvent<KeyboardEvent>(this.document, 'keydown')
282+
.pipe(takeUntil(this.destroy$))
283+
.subscribe(event => {
284+
this.ngZone.run(() => {
285+
if (event.keyCode === ESCAPE) {
286+
this.onClose();
287+
this.markForCheck();
288+
}
289+
});
290+
});
276291
});
277292
}
278293

components/image/image.spec.ts

+18-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
44
*/
55

6-
import { LEFT_ARROW, RIGHT_ARROW } from '@angular/cdk/keycodes';
6+
import { ESCAPE, LEFT_ARROW, RIGHT_ARROW } from '@angular/cdk/keycodes';
77
import { Overlay, OverlayContainer } from '@angular/cdk/overlay';
88
import { Component, DebugElement, NgModule, NgZone, ViewChild } from '@angular/core';
99
import { ComponentFixture, discardPeriodicTasks, fakeAsync, flush, inject, TestBed, tick } from '@angular/core/testing';
@@ -463,6 +463,23 @@ describe('Preview', () => {
463463
expect(previewInstance['reCenterImage']).toHaveBeenCalled();
464464
}));
465465

466+
it('should close image preview when escape is pressed', fakeAsync(() => {
467+
context.images = [{ src: QUICK_SRC }];
468+
context.createUsingService();
469+
const previewInstance = context.previewRef?.previewInstance!;
470+
previewInstance.ngOnInit();
471+
tickChanges();
472+
spyOn(previewInstance, 'onClose');
473+
474+
const event: KeyboardEvent = new KeyboardEvent('keydown', {
475+
keyCode: ESCAPE
476+
});
477+
document.dispatchEvent(event);
478+
tick();
479+
480+
expect(previewInstance.onClose).toHaveBeenCalled();
481+
}));
482+
466483
it('should container click work', fakeAsync(() => {
467484
context.firstSrc = QUICK_SRC;
468485
fixture.detectChanges();

0 commit comments

Comments
 (0)