Skip to content

Commit 7dc4741

Browse files
committed
Removed tests/handle types/handle Filereader busy
1 parent df192ea commit 7dc4741

File tree

9 files changed

+19
-94
lines changed

9 files changed

+19
-94
lines changed

README.md

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
# FctrlxAngularFileToBase64
2-
3-
This library was generated with [Angular CLI](https://github.com/angular/angular-cli) version 7.2.0.
4-
5-
## Code scaffolding
6-
7-
Run `ng generate component component-name --project fctrlx-angular-file-to-base64` to generate a new component. You can also use `ng generate directive|pipe|service|class|guard|interface|enum|module --project fctrlx-angular-file-to-base64`.
8-
> Note: Don't forget to add `--project fctrlx-file-to-base64` or else it will be added to the default project in your `angular.json` file.
2+
--- TODO ---
93

104
## Build
115

@@ -14,11 +8,3 @@ Run `ng build fctrlx-file-to-base64` to build the project. The build artifacts w
148
## Publishing
159

1610
After building your library with `ng build fctrlx-file-to-base64`, go to the dist folder `cd dist/fctrlx-angular-file-to-base64` and run `npm publish`.
17-
18-
## Running unit tests
19-
20-
Run `ng test fctrlx-file-to-base64` to execute the unit tests via [Karma](https://karma-runner.github.io).
21-
22-
## Further help
23-
24-
To get more help on the Angular CLI use `ng help` or go check out the [Angular CLI README](https://github.com/angular/angular-cli/blob/master/README.md).

karma.conf.js

Lines changed: 0 additions & 32 deletions
This file was deleted.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "fctrlx-file-to-base64",
3-
"version": "0.0.1",
3+
"version": "0.0.2",
44
"peerDependencies": {
55
"@angular/common": "^7.2.0",
66
"@angular/core": "^7.2.0"

src/lib/converted.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export interface Converted {
2+
name: string;
3+
size: number;
4+
type: string;
5+
base64?: string;
6+
}

src/lib/fctrlx-file-to-base64.directive.spec.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Directive, ElementRef, EventEmitter, Input, Output, OnInit, OnDestroy } from '@angular/core';
2+
import { Converted } from './converted';
23

34
@Directive({
45
selector: '[fileToBase64]'
@@ -13,33 +14,31 @@ export class FctrlxFileToBase64Directive implements OnInit, OnDestroy {
1314

1415
private readonly TYPE_FILE: string = 'file';
1516

16-
private reader: FileReader = new FileReader();
17-
private converted: any = [];
17+
private converted: Array<Converted> = [];
1818
private currentIndex: number = 0;
1919

2020
constructor(private element: ElementRef) {}
2121

2222
ngOnInit(): void {
2323
if(this.type === this.TYPE_FILE) {
2424
this.element.nativeElement.addEventListener('change', this.filesChanged.bind(this), false);
25-
26-
this.reader.onload = (file) => {
27-
this.readFile(file)
28-
}
2925
}
3026
}
3127

3228
get isMultiple(): boolean {
3329
return !(typeof this.multiple === 'undefined');
3430
}
3531

36-
filesChanged(event) {
37-
let files = event.target.files;
32+
filesChanged(event: Event): void {
33+
const files = (<HTMLInputElement>event.target).files;
3834

3935
this.converted = [];
4036
this.currentIndex = 0;
4137

42-
Object.keys(files).forEach((key) => {
38+
Object.keys(files).forEach( (key: string) => {
39+
const reader = new FileReader();
40+
reader.onload = (file) => this.storeBase64(file);
41+
4342
const { name, size, type, base64 } = files[key];
4443

4544
this.converted.push({
@@ -49,18 +48,18 @@ export class FctrlxFileToBase64Directive implements OnInit, OnDestroy {
4948
base64
5049
});
5150

52-
this.reader.readAsDataURL(files[key]);
51+
reader.readAsDataURL(files[key]);
5352
});
5453

5554
this.filesChange.next(this.isMultiple ? this.converted : this.converted[0]);
5655
}
5756

58-
readFile(file) {
57+
storeBase64(file: { target }) {
5958
this.converted[this.currentIndex].base64 = file.target.result;
59+
this.currentIndex++;
6060
}
6161

6262
ngOnDestroy(): void {
6363
this.element.nativeElement.removeEventListener('change', this.filesChanged.bind(this), false);
64-
this.reader.onload = null;
6564
}
6665
}

src/test.ts

Lines changed: 0 additions & 22 deletions
This file was deleted.

tsconfig.lib.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,5 @@
2626
"enableResourceInlining": true
2727
},
2828
"exclude": [
29-
"src/test.ts",
30-
"**/*.spec.ts"
3129
]
3230
}

tsconfig.spec.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@
88
]
99
},
1010
"files": [
11-
"src/test.ts"
1211
],
1312
"include": [
14-
"**/*.spec.ts",
1513
"**/*.d.ts"
1614
]
1715
}

0 commit comments

Comments
 (0)