Skip to content

Commit df192ea

Browse files
committed
First version (not pulled to npm
1 parent d9d3c35 commit df192ea

12 files changed

+247
-0
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# 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.
9+
10+
## Build
11+
12+
Run `ng build fctrlx-file-to-base64` to build the project. The build artifacts will be stored in the `dist/` directory.
13+
14+
## Publishing
15+
16+
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: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Karma configuration file, see link for more information
2+
// https://karma-runner.github.io/1.0/config/configuration-file.html
3+
4+
module.exports = function (config) {
5+
config.set({
6+
basePath: '',
7+
frameworks: ['jasmine', '@angular-devkit/build-angular'],
8+
plugins: [
9+
require('karma-jasmine'),
10+
require('karma-chrome-launcher'),
11+
require('karma-jasmine-html-reporter'),
12+
require('karma-coverage-istanbul-reporter'),
13+
require('@angular-devkit/build-angular/plugins/karma')
14+
],
15+
client: {
16+
clearContext: false // leave Jasmine Spec Runner output visible in browser
17+
},
18+
coverageIstanbulReporter: {
19+
dir: require('path').join(__dirname, '../../coverage/fctrlx-file-to-base64'),
20+
reports: ['html', 'lcovonly'],
21+
fixWebpackSourcePaths: true
22+
},
23+
reporters: ['progress', 'kjhtml'],
24+
port: 9876,
25+
colors: true,
26+
logLevel: config.LOG_INFO,
27+
autoWatch: true,
28+
browsers: ['Chrome'],
29+
singleRun: false,
30+
restartOnFileChange: true
31+
});
32+
};

ng-package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"$schema": "../../node_modules/ng-packagr/ng-package.schema.json",
3+
"dest": "../../dist/fctrlx-file-to-base64",
4+
"lib": {
5+
"entryFile": "src/public-api.ts"
6+
}
7+
}

package.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "fctrlx-file-to-base64",
3+
"version": "0.0.1",
4+
"peerDependencies": {
5+
"@angular/common": "^7.2.0",
6+
"@angular/core": "^7.2.0"
7+
}
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { FctrlxFileToBase64Directive } from './fctrlx-file-to-base64.directive';
2+
3+
describe('FctrlxFileToBase64Directive', () => {
4+
it('should create an instance', () => {
5+
const directive = new FctrlxFileToBase64Directive();
6+
expect(directive).toBeTruthy();
7+
});
8+
});
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { Directive, ElementRef, EventEmitter, Input, Output, OnInit, OnDestroy } from '@angular/core';
2+
3+
@Directive({
4+
selector: '[fileToBase64]'
5+
})
6+
export class FctrlxFileToBase64Directive implements OnInit, OnDestroy {
7+
8+
@Input() files: any;
9+
@Input() type: string;
10+
@Input() multiple: any;
11+
12+
@Output() filesChange: EventEmitter<any> = new EventEmitter();
13+
14+
private readonly TYPE_FILE: string = 'file';
15+
16+
private reader: FileReader = new FileReader();
17+
private converted: any = [];
18+
private currentIndex: number = 0;
19+
20+
constructor(private element: ElementRef) {}
21+
22+
ngOnInit(): void {
23+
if(this.type === this.TYPE_FILE) {
24+
this.element.nativeElement.addEventListener('change', this.filesChanged.bind(this), false);
25+
26+
this.reader.onload = (file) => {
27+
this.readFile(file)
28+
}
29+
}
30+
}
31+
32+
get isMultiple(): boolean {
33+
return !(typeof this.multiple === 'undefined');
34+
}
35+
36+
filesChanged(event) {
37+
let files = event.target.files;
38+
39+
this.converted = [];
40+
this.currentIndex = 0;
41+
42+
Object.keys(files).forEach((key) => {
43+
const { name, size, type, base64 } = files[key];
44+
45+
this.converted.push({
46+
name,
47+
size,
48+
type,
49+
base64
50+
});
51+
52+
this.reader.readAsDataURL(files[key]);
53+
});
54+
55+
this.filesChange.next(this.isMultiple ? this.converted : this.converted[0]);
56+
}
57+
58+
readFile(file) {
59+
this.converted[this.currentIndex].base64 = file.target.result;
60+
}
61+
62+
ngOnDestroy(): void {
63+
this.element.nativeElement.removeEventListener('change', this.filesChanged.bind(this), false);
64+
this.reader.onload = null;
65+
}
66+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { NgModule } from '@angular/core';
2+
import { FctrlxFileToBase64Directive } from './fctrlx-file-to-base64.directive';
3+
4+
@NgModule({
5+
imports: [],
6+
declarations: [FctrlxFileToBase64Directive],
7+
exports: [FctrlxFileToBase64Directive],
8+
})
9+
export class FctrlxFileToBase64 { }

src/public-api.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/*
2+
* Public API Surface of fctrlx-file-to-base64
3+
*/
4+
5+
export * from './lib/fctrlx-file-to-base64.module';

src/test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// This file is required by karma.conf.js and loads recursively all the .spec and framework files
2+
3+
import 'core-js/es7/reflect';
4+
import 'zone.js/dist/zone';
5+
import 'zone.js/dist/zone-testing';
6+
import { getTestBed } from '@angular/core/testing';
7+
import {
8+
BrowserDynamicTestingModule,
9+
platformBrowserDynamicTesting
10+
} from '@angular/platform-browser-dynamic/testing';
11+
12+
declare const require: any;
13+
14+
// First, initialize the Angular testing environment.
15+
getTestBed().initTestEnvironment(
16+
BrowserDynamicTestingModule,
17+
platformBrowserDynamicTesting()
18+
);
19+
// Then we find all the tests.
20+
const context = require.context('./', true, /\.spec\.ts$/);
21+
// And load the modules.
22+
context.keys().map(context);

tsconfig.lib.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"extends": "../../tsconfig.json",
3+
"compilerOptions": {
4+
"outDir": "../../out-tsc/lib",
5+
"target": "es2015",
6+
"module": "es2015",
7+
"moduleResolution": "node",
8+
"declaration": true,
9+
"sourceMap": true,
10+
"inlineSources": true,
11+
"emitDecoratorMetadata": true,
12+
"experimentalDecorators": true,
13+
"importHelpers": true,
14+
"types": [],
15+
"lib": [
16+
"dom",
17+
"es2018"
18+
]
19+
},
20+
"angularCompilerOptions": {
21+
"annotateForClosureCompiler": true,
22+
"skipTemplateCodegen": true,
23+
"strictMetadataEmit": true,
24+
"fullTemplateTypeCheck": true,
25+
"strictInjectionParameters": true,
26+
"enableResourceInlining": true
27+
},
28+
"exclude": [
29+
"src/test.ts",
30+
"**/*.spec.ts"
31+
]
32+
}

0 commit comments

Comments
 (0)