Skip to content

Commit

Permalink
fix:nz-upload drag drop will open new tab when use firefox versions i…
Browse files Browse the repository at this point in the history
…n 91 and 92
  • Loading branch information
alice.zhang committed Jan 7, 2022
1 parent 2513845 commit c211a50
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions components/upload/upload.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ import {
Output,
TemplateRef,
ViewChild,
NgZone,
Inject,
ViewEncapsulation
} from '@angular/core';
import { Observable, of, Subject, Subscription } from 'rxjs';
import { DOCUMENT } from '@angular/common';
import { Observable, of, Subject, Subscription, fromEvent } from 'rxjs';
import { filter, takeUntil } from 'rxjs/operators';

import { BooleanInput, NumberInput, NzSafeAny } from 'ng-zorro-antd/core/types';
Expand Down Expand Up @@ -176,6 +179,8 @@ export class NzUploadComponent implements OnInit, AfterViewInit, OnChanges, OnDe
// #endregion

constructor(
private ngZone: NgZone,
@Inject(DOCUMENT) private document: Document,
private cdr: ChangeDetectorRef,
private i18n: NzI18nService,
@Optional() private directionality: Directionality
Expand Down Expand Up @@ -329,12 +334,6 @@ export class NzUploadComponent implements OnInit, AfterViewInit, OnChanges, OnDe
this.cdr.detectChanges();
}

// fix firefox drop open new tab
private handleDropOpenNewTab(e: DragEvent){
e.preventDefault();
e.stopPropagation()
}

// #endregion

ngOnInit(): void {
Expand All @@ -352,7 +351,11 @@ export class NzUploadComponent implements OnInit, AfterViewInit, OnChanges, OnDe
}

ngAfterViewInit(): void {
document.body.addEventListener('drop', this.handleDropOpenNewTab);
// fix firefox drop open new tab
this.ngZone.runOutsideAngular(() => fromEvent(this.document.body, 'drop').pipe(takeUntil(this.destroy$)).subscribe((event) => {
event.preventDefault();
event.stopPropagation();
}));
}

ngOnChanges(): void {
Expand All @@ -362,6 +365,5 @@ export class NzUploadComponent implements OnInit, AfterViewInit, OnChanges, OnDe
ngOnDestroy(): void {
this.destroy$.next();
this.destroy$.complete();
document.body.removeEventListener('drop', this.handleDropOpenNewTab)
}
}

0 comments on commit c211a50

Please sign in to comment.