Skip to content

Commit 6b1378b

Browse files
committed
feat: drag and drop files handling
1 parent b962e7c commit 6b1378b

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

projects/angular-editor/src/lib/angular-editor.component.html

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727

2828
<div
2929
class="angular-editor-wrapper"
30+
draggable="true"
31+
ngClass="{{dragAreaClass}}"
3032
#editorWrapper
3133
>
3234
<div

projects/angular-editor/src/lib/angular-editor.component.scss

+17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
@import "style";
22

3+
.droparea {
4+
font-size: 1.5rem;
5+
border: 3px dashed #bbb;
6+
padding: 1.5rem;
7+
background-color: #eff;
8+
color: #aaa;
9+
}
10+
11+
.dragarea {
12+
font-size: 1.5rem;
13+
border: 3px solid #bbb;
14+
padding: 1.5rem;
15+
background-color: #fff;
16+
color: #bbb;
17+
}
18+
19+
320
.angular-editor {
421
position: relative;
522

projects/angular-editor/src/lib/angular-editor.component.ts

+57
Original file line numberDiff line numberDiff line change
@@ -803,4 +803,61 @@ export class AngularEditorComponent
803803
html = html.replace('position: fixed;', '');
804804
return html;
805805
}
806+
807+
// TODO: Improve event handling
808+
// TODO: Improve styles
809+
dragAreaClass = '';
810+
@HostListener('dragover', ['$event']) onDragOver(event: any) {
811+
this.dragAreaClass = 'droparea';
812+
event.preventDefault();
813+
}
814+
@HostListener('dragenter', ['$event']) onDragEnter(event: any) {
815+
this.dragAreaClass = 'droparea';
816+
event.preventDefault();
817+
}
818+
@HostListener('dragend', ['$event']) onDragEnd(event: any) {
819+
this.dragAreaClass = '';
820+
event.preventDefault();
821+
}
822+
@HostListener('dragleave', ['$event']) onDragLeave(event: any) {
823+
this.dragAreaClass = '';
824+
event.preventDefault();
825+
}
826+
@HostListener('drop', ['$event']) onDrop(event: any) {
827+
this.dragAreaClass = '';
828+
event.preventDefault();
829+
event.stopPropagation();
830+
if (event.dataTransfer.files) {
831+
let files: FileList = event.dataTransfer.files;
832+
this.saveFiles(files);
833+
}
834+
}
835+
836+
// TODO: Use error
837+
error: string;
838+
839+
isValidFile(file): boolean {
840+
const fileType = file['type'];
841+
const validImageTypes = ['image/gif', 'image/jpeg', 'image/png'];
842+
if (!validImageTypes.includes(fileType)) {
843+
return false;
844+
}
845+
return true;
846+
}
847+
848+
saveFiles(files: FileList): void {
849+
if (files.length > 1) {
850+
this.error = 'Only one file at time allow';
851+
return;
852+
}
853+
854+
const file = files[0];
855+
if (!this.isValidFile(file)) {
856+
this.error = `Invalid file type`;
857+
return;
858+
}
859+
860+
this.error = '';
861+
this.editorService.uploadImage(file);
862+
}
806863
}

0 commit comments

Comments
 (0)