mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-07-31 17:38:48 +00:00
fix blob input in text viewer (#1942)
This commit is contained in:
committed by
Eugenio Romano
parent
d3eee53ee4
commit
c6bfe892ee
@@ -72,7 +72,7 @@ export class AboutComponent implements OnInit {
|
||||
if (corePackage) {
|
||||
let commitIsh = corePackage.version.split('-');
|
||||
if (commitIsh.length > 1) {
|
||||
this.githubUrlCommitAlpha = this.githubUrlCommitAlpha + commitIsh;
|
||||
this.githubUrlCommitAlpha = this.githubUrlCommitAlpha + commitIsh[1];
|
||||
} else {
|
||||
this.githubUrlCommitAlpha = this.githubUrlCommitAlpha + corePackage.version;
|
||||
}
|
||||
|
@@ -29,7 +29,7 @@ export class ImgViewerComponent implements OnChanges {
|
||||
urlFile: string;
|
||||
|
||||
@Input()
|
||||
blobFile: any;
|
||||
blobFile: Blob;
|
||||
|
||||
@Input()
|
||||
nameFile: string;
|
||||
|
@@ -3,6 +3,7 @@
|
||||
overflow: auto;
|
||||
font-size: 1em;
|
||||
padding: 20px;
|
||||
max-width: 70%;
|
||||
}
|
||||
|
||||
.full_width{
|
||||
|
@@ -52,16 +52,16 @@ describe('Test ng2-alfresco-viewer Text View component', () => {
|
||||
debug = fixture.debugElement;
|
||||
element = fixture.nativeElement;
|
||||
component = fixture.componentInstance;
|
||||
component.urlFile = require('../assets/fake-test-file.txt');
|
||||
});
|
||||
|
||||
describe('View', () => {
|
||||
|
||||
it('Should text container be present with urlfile', (done) => {
|
||||
fixture.detectChanges();
|
||||
let change = new SimpleChange(null, null, true);
|
||||
let urlFile = require('../assets/fake-test-file.txt');
|
||||
let change = new SimpleChange(null, urlFile, true);
|
||||
|
||||
component.ngOnChanges(change).then(() => {
|
||||
component.ngOnChanges({ 'urlFile': change }).then(() => {
|
||||
fixture.detectChanges();
|
||||
fixture.whenStable().then(() => {
|
||||
expect(element.querySelector('#adf-viewer-text-container').textContent).toContain('example');
|
||||
@@ -69,5 +69,20 @@ describe('Test ng2-alfresco-viewer Text View component', () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('Should text container be present with Blob file', (done) => {
|
||||
let blobFile = new Blob(['text example'], {type: 'text/txt'});
|
||||
|
||||
let change = new SimpleChange(null, blobFile, true);
|
||||
|
||||
component.ngOnChanges({ 'blobFile': change }).then(() => {
|
||||
fixture.detectChanges();
|
||||
fixture.whenStable().then(() => {
|
||||
expect(element.querySelector('#adf-viewer-text-container').textContent).toContain('example');
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
|
@@ -16,8 +16,7 @@
|
||||
*/
|
||||
|
||||
import { Component, Input } from '@angular/core';
|
||||
import { SimpleChange } from '@angular/core';
|
||||
import { ContentService } from 'ng2-alfresco-core';
|
||||
import { SimpleChanges } from '@angular/core';
|
||||
import { Http, Response, RequestOptions, ResponseContentType } from '@angular/http';
|
||||
import 'rxjs/add/operator/toPromise';
|
||||
|
||||
@@ -32,36 +31,59 @@ export class TxtViewerComponent {
|
||||
urlFile: any;
|
||||
|
||||
@Input()
|
||||
blobFile: any;
|
||||
blobFile: Blob;
|
||||
|
||||
content: string;
|
||||
|
||||
constructor(private http: Http, private contentService: ContentService) {
|
||||
constructor(private http: Http) {
|
||||
}
|
||||
|
||||
ngOnChanges(changes: SimpleChange) {
|
||||
ngOnChanges(changes: SimpleChanges): Promise<any> {
|
||||
|
||||
let blobFile = changes['blobFile'];
|
||||
if (blobFile && blobFile.currentValue) {
|
||||
this.urlFile = this.contentService.createTrustedUrl(this.blobFile);
|
||||
return this.readBlob(blobFile.currentValue);
|
||||
}
|
||||
|
||||
let urlFile = changes['urlFile'];
|
||||
if (urlFile && urlFile.currentValue) {
|
||||
return this.getUrlContent(urlFile.currentValue);
|
||||
}
|
||||
|
||||
if (!this.urlFile && !this.blobFile) {
|
||||
throw new Error('Attribute urlFile or blobFile is required');
|
||||
}
|
||||
}
|
||||
|
||||
private getUrlContent(url: string): Promise<any> {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.getUrlContent(resolve, reject);
|
||||
|
||||
this.http.get(url, new RequestOptions({
|
||||
responseType: ResponseContentType.Text
|
||||
})).toPromise().then(
|
||||
(res: Response) => {
|
||||
this.content = res.text();
|
||||
resolve();
|
||||
}, (event) => {
|
||||
reject(event);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
private getUrlContent(resolve, reject): void {
|
||||
this.http.get(this.urlFile, new RequestOptions({
|
||||
responseType: ResponseContentType.Text
|
||||
})).toPromise().then(
|
||||
(res: Response) => {
|
||||
this.content = res.text();
|
||||
private readBlob(blob: Blob): Promise<any> {
|
||||
return new Promise((resolve, reject) => {
|
||||
let reader = new FileReader();
|
||||
|
||||
reader.onload = () => {
|
||||
this.content = reader.result;
|
||||
resolve();
|
||||
}, (event) => {
|
||||
reject(event);
|
||||
});
|
||||
};
|
||||
|
||||
reader.onerror = (error: ErrorEvent) => {
|
||||
reject(error);
|
||||
};
|
||||
|
||||
reader.readAsText(blob);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@@ -239,7 +239,7 @@ export class ViewerComponent {
|
||||
* @returns {boolean}
|
||||
*/
|
||||
private isText(): boolean {
|
||||
return this.extension === 'txt' || this.mimeType === 'text/txt';
|
||||
return this.extension === 'txt' || this.mimeType === 'text/txt' || this.mimeType === 'text/plain';
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user