I'm working in Angular 7 trying to create a drag and drop file upload component. I have it working below.
public stopPreventAndSetClass(b: boolean, event: any): void {
if (event.target === this.enterTarget) {
this.draggedOverTable = b;
}
console.log(event.target);
this.stopAndPrevent(event);
}
public stopAndPrevent($event: any): void {
event.preventDefault();
event.stopPropagation();
}
<div class="document-container"
(drop)="saveFiles($event); stopPreventAndSetClass(false, $event)"
(dragenter)="enterTarget = $event.target; stopPreventAndSetClass(true, $event)"
(dragover)="stopAndPrevent($event);"
(dragleave)="stopPreventAndSetClass(false, $event)"
[ngClass]="{'showDropContainerBorder': draggedOverTable}">
<!-- An Angular Material table of uploaded files sits here. So think many child elements. -->
</div>
My problem is that the performance on this is awful (~4 second lag time between drop and saveFiles() running) because change detection is running for every dragover
and dragleave
event fired. After doing a good amount of research, I found that the best solution is supposedly to remove the dragover
event from the ngzone which will prevent the change detection from firing. From here: https://github.com/angular/angular/pull/21681
A very easy way of doing this seems to be (dragover.nozone)="stopAndPrevent($event)"
. This does fix the performance issue, but it also no longer works, as the browser reverts back to using its default behavior (loading file in browser) ignoring the event.preventDefault();
. Does anyone know of a better way to do this or know how to fix the performance issue I'm running into here?