Skip to content

Commit f0f52a4

Browse files
authored
perf(module:image): do not run change detection if there are no containerClick listeners (#7147)
1 parent 2306e0d commit f0f52a4

File tree

1 file changed

+24
-18
lines changed

1 file changed

+24
-18
lines changed

components/image/image-preview.component.ts

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,17 @@ import {
1111
Component,
1212
ElementRef,
1313
EventEmitter,
14-
OnDestroy,
14+
NgZone,
15+
OnInit,
1516
ViewChild,
1617
ViewEncapsulation
1718
} from '@angular/core';
18-
import { Subject } from 'rxjs';
19+
import { fromEvent } from 'rxjs';
20+
import { takeUntil } from 'rxjs/operators';
1921

2022
import { fadeMotion } from 'ng-zorro-antd/core/animation';
2123
import { NzConfigService } from 'ng-zorro-antd/core/config';
24+
import { NzDestroyService } from 'ng-zorro-antd/core/services';
2225
import { NzSafeAny } from 'ng-zorro-antd/core/types';
2326
import { isNotNil } from 'ng-zorro-antd/core/util';
2427

@@ -113,12 +116,12 @@ const initialPosition = {
113116
'[@fadeMotion]': 'animationState',
114117
'(@fadeMotion.start)': 'onAnimationStart($event)',
115118
'(@fadeMotion.done)': 'onAnimationDone($event)',
116-
'(click)': 'onContainerClick($event)',
117119
tabindex: '-1',
118120
role: 'document'
119-
}
121+
},
122+
providers: [NzDestroyService]
120123
})
121-
export class NzImagePreviewComponent implements OnDestroy {
124+
export class NzImagePreviewComponent implements OnInit {
122125
images: NzImage[] = [];
123126
index = 0;
124127
isDragging = false;
@@ -176,7 +179,6 @@ export class NzImagePreviewComponent implements OnDestroy {
176179

177180
private zoom: number;
178181
private rotate: number;
179-
private destroy$ = new Subject();
180182

181183
get animationDisabled(): boolean {
182184
return this.config.nzNoAnimation ?? false;
@@ -188,10 +190,13 @@ export class NzImagePreviewComponent implements OnDestroy {
188190
}
189191

190192
constructor(
193+
private ngZone: NgZone,
194+
private host: ElementRef<HTMLElement>,
191195
private cdr: ChangeDetectorRef,
192196
public nzConfigService: NzConfigService,
193197
public config: NzImagePreviewOptions,
194-
private overlayRef: OverlayRef
198+
private overlayRef: OverlayRef,
199+
private destroy$: NzDestroyService
195200
) {
196201
this.zoom = this.config.nzZoom ?? 1;
197202
this.rotate = this.config.nzRotate ?? 0;
@@ -200,6 +205,18 @@ export class NzImagePreviewComponent implements OnDestroy {
200205
this.updatePreviewImageWrapperTransform();
201206
}
202207

208+
ngOnInit(): void {
209+
this.ngZone.runOutsideAngular(() => {
210+
fromEvent(this.host.nativeElement, 'click')
211+
.pipe(takeUntil(this.destroy$))
212+
.subscribe(event => {
213+
if (event.target === event.currentTarget && this.maskClosable && this.containerClick.observers.length) {
214+
this.ngZone.run(() => this.containerClick.emit());
215+
}
216+
});
217+
});
218+
}
219+
203220
setImages(images: NzImage[]): void {
204221
this.images = images;
205222
this.cdr.markForCheck();
@@ -278,12 +295,6 @@ export class NzImagePreviewComponent implements OnDestroy {
278295
this.next();
279296
}
280297

281-
onContainerClick(e: MouseEvent): void {
282-
if (e.target === e.currentTarget && this.maskClosable) {
283-
this.containerClick.emit();
284-
}
285-
}
286-
287298
onAnimationStart(event: AnimationEvent): void {
288299
if (event.toState === 'enter') {
289300
this.setEnterAnimationClass();
@@ -333,11 +344,6 @@ export class NzImagePreviewComponent implements OnDestroy {
333344
}
334345
}
335346

336-
ngOnDestroy(): void {
337-
this.destroy$.next();
338-
this.destroy$.complete();
339-
}
340-
341347
private updatePreviewImageTransform(): void {
342348
this.previewImageTransform = `scale3d(${this.zoom}, ${this.zoom}, 1) rotate(${this.rotate}deg)`;
343349
}

0 commit comments

Comments
 (0)