ajout du drag and drop pour ajouter une image

This commit is contained in:
MiharyR 2021-11-09 14:10:36 +01:00
parent 54324794e9
commit 2f9c9c2b77
12 changed files with 377 additions and 12 deletions

View file

@ -0,0 +1,8 @@
import { DragAndDropDirective } from './drag-and-drop.directive';
describe('DragAndDropDirective', () => {
it('should create an instance', () => {
const directive = new DragAndDropDirective();
expect(directive).toBeTruthy();
});
});

View file

@ -0,0 +1,36 @@
import {Directive, EventEmitter, HostBinding, HostListener, Output} from '@angular/core';
@Directive({
selector: '[appDragAndDrop]'
})
export class DragAndDropDirective
{
@HostBinding('class.fileover') fileOver: boolean;
@Output() fileDropped = new EventEmitter<any>();
// Dragover listener
@HostListener('dragover', ['$event']) onDragOver(evt) {
evt.preventDefault();
evt.stopPropagation();
this.fileOver = true;
}
// Dragleave listener
@HostListener('dragleave', ['$event']) public onDragLeave(evt) {
evt.preventDefault();
evt.stopPropagation();
this.fileOver = false;
}
// Drop listener
@HostListener('drop', ['$event']) public ondrop(evt) {
evt.preventDefault();
evt.stopPropagation();
this.fileOver = false;
let files = evt.dataTransfer.files;
if (files.length > 0) {
this.fileDropped.emit(files);
}
}
}