|
|
|
@ -19,15 +19,16 @@ import { ImageService } from '../../services/image/image.service';
|
|
|
|
|
export class DragDropComponent implements OnChanges, OnDestroy {
|
|
|
|
|
@Input() initialFiles: { name: string; id?: string }[] = [];
|
|
|
|
|
@Input() errorMessage: string = '';
|
|
|
|
|
fileNames: { name: string; id?: string }[] = [];
|
|
|
|
|
@Output() filesSelected = new EventEmitter<FileList>();
|
|
|
|
|
@Output() fileRemoved = new EventEmitter<string>();
|
|
|
|
|
|
|
|
|
|
hoveredFileName: string | null = null;
|
|
|
|
|
fileNames: { name: string; id?: string }[] = [];
|
|
|
|
|
hoveredFile: { name: string; id?: string } | null = null;
|
|
|
|
|
previewUrl: string | null = null;
|
|
|
|
|
private objectUrl: string | null = null;
|
|
|
|
|
|
|
|
|
|
private imageUrlCache: { [id: string]: string } = {};
|
|
|
|
|
private lastHoveredId: string | null = null;
|
|
|
|
|
private imageLocalUrlCache: { [name: string]: string } = {};
|
|
|
|
|
private inputFiles: Array<File> = [];
|
|
|
|
|
|
|
|
|
|
constructor(private imageService: ImageService) {}
|
|
|
|
|
|
|
|
|
@ -47,6 +48,7 @@ export class DragDropComponent implements OnChanges, OnDestroy {
|
|
|
|
|
const input = event.target as HTMLInputElement;
|
|
|
|
|
if (input.files) {
|
|
|
|
|
this.updateFileNames(input.files);
|
|
|
|
|
this.inputFiles = Array.from(input.files);
|
|
|
|
|
this.filesSelected.emit(input.files);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -56,6 +58,7 @@ export class DragDropComponent implements OnChanges, OnDestroy {
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
if (event.dataTransfer && event.dataTransfer.files) {
|
|
|
|
|
this.updateFileNames(event.dataTransfer.files);
|
|
|
|
|
this.inputFiles = Array.from(event.dataTransfer.files);
|
|
|
|
|
this.filesSelected.emit(event.dataTransfer.files);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -86,21 +89,23 @@ export class DragDropComponent implements OnChanges, OnDestroy {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async onFileNameMouseEnter(fileName: string): Promise<void> {
|
|
|
|
|
const fileObj = this.getFileByName(fileName);
|
|
|
|
|
if (!fileObj) {
|
|
|
|
|
this.hoveredFileName = null;
|
|
|
|
|
async onFileNameMouseEnter(fileObj: { name: string; id?: string }): Promise<void> {
|
|
|
|
|
const localFile = this.inputFiles.find((f) => f.name === fileObj.name);
|
|
|
|
|
|
|
|
|
|
if (!fileObj && !localFile) {
|
|
|
|
|
this.hoveredFile = null;
|
|
|
|
|
this.previewUrl = null;
|
|
|
|
|
this.lastHoveredId = null;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Ne rien faire si on survole le même fichier
|
|
|
|
|
if (fileObj.id && this.lastHoveredId === fileObj.id) {
|
|
|
|
|
if(this.hoveredFile === fileObj) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
this.hoveredFileName = fileName;
|
|
|
|
|
this.lastHoveredId = fileObj.id || null;
|
|
|
|
|
if (fileObj.id) {
|
|
|
|
|
|
|
|
|
|
if(fileObj && fileObj.id) { // Si le fichier est dans le backend
|
|
|
|
|
this.hoveredFile = fileObj;
|
|
|
|
|
|
|
|
|
|
if (this.imageUrlCache[fileObj.id]) {
|
|
|
|
|
this.previewUrl = this.imageUrlCache[fileObj.id];
|
|
|
|
|
} else {
|
|
|
|
@ -110,15 +115,22 @@ export class DragDropComponent implements OnChanges, OnDestroy {
|
|
|
|
|
this.previewUrl = objectUrl;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
this.previewUrl = null;
|
|
|
|
|
} else if (localFile) { // Si le fichier est dans l'input
|
|
|
|
|
this.hoveredFile = fileObj;
|
|
|
|
|
if (!this.imageLocalUrlCache[fileObj.name]) {
|
|
|
|
|
this.imageLocalUrlCache[fileObj.name] = URL.createObjectURL(localFile);
|
|
|
|
|
}
|
|
|
|
|
this.previewUrl = this.imageLocalUrlCache[fileObj.name];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@HostListener('mouseleave') onFileNameMouseLeave(): void {
|
|
|
|
|
this.hoveredFileName = null;
|
|
|
|
|
onFileNameMouseLeave(): void {
|
|
|
|
|
this.hoveredFile = null;
|
|
|
|
|
this.previewUrl = null;
|
|
|
|
|
this.lastHoveredId = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
trackByFile(index: number, file: { name: string; id?: string }): string {
|
|
|
|
|
return file.id || file.name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getFileByName(fileName: string): { name: string; id?: string } | undefined {
|
|
|
|
|