From eb1157d608f08811390891bac90c46fd87933311 Mon Sep 17 00:00:00 2001 From: Willem Dantuma Date: Thu, 5 Aug 2021 16:33:31 +0200 Subject: [PATCH] Fix thumbnail editing, add blobtodataurl method --- .../thumbnail/thumbnail.component.html | 6 +-- .../thumbnail/thumbnail.component.ts | 47 ++++++++++++++----- .../common/src/fm/services/image.service.ts | 14 ++++++ 3 files changed, 53 insertions(+), 14 deletions(-) diff --git a/projects/common/src/fm/components/thumbnail/thumbnail.component.html b/projects/common/src/fm/components/thumbnail/thumbnail.component.html index a5a560a..89ebb4e 100644 --- a/projects/common/src/fm/components/thumbnail/thumbnail.component.html +++ b/projects/common/src/fm/components/thumbnail/thumbnail.component.html @@ -1,9 +1,9 @@
- -
+ +
- + diff --git a/projects/common/src/fm/components/thumbnail/thumbnail.component.ts b/projects/common/src/fm/components/thumbnail/thumbnail.component.ts index 075be8e..cece25a 100644 --- a/projects/common/src/fm/components/thumbnail/thumbnail.component.ts +++ b/projects/common/src/fm/components/thumbnail/thumbnail.component.ts @@ -1,7 +1,8 @@ -import { Component,Input ,ViewChild,ElementRef,ChangeDetectorRef} from '@angular/core'; +import { Component,Input ,ViewChild,ElementRef,ChangeDetectorRef, Output,EventEmitter} from '@angular/core'; import { Store } from '@ngrx/store'; import { IListItem } from '../../models/list.item'; +import { ImageService } from '../../services/image.service'; import { commonReducers,ItemTypeService } from '../../../public-api' import { EditImageModalComponent} from '../edit-image-modal/edit-image-modal.component'; import { AppConfig } from "../../shared/app.config"; @@ -16,14 +17,25 @@ import { AppConfig } from "../../shared/app.config"; export class ThumbnailComponent { @Input() public item: IListItem; @Input() public edit: boolean = false; + @Input() public save: boolean = true; + @Output() changed = new EventEmitter(); @ViewChild('thumbnail') el:ElementRef; @ViewChild('modal') modal:EditImageModalComponent; - constructor(public store: Store, public itemTypeService: ItemTypeService,public appConfig: AppConfig,private changeDetector:ChangeDetectorRef) { + public image = null; + private endpoint = ""; + public thumnailUrl = ""; + + constructor(public store: Store, public itemTypeService: ItemTypeService,public appConfig: AppConfig,private changeDetector:ChangeDetectorRef,public imageService:ImageService) { } getThumbnailUrl(item:IListItem):string { - return this.appConfig.getConfig("apiEndPoint")+item.url+'/thumbnail?v=' + Date.parse(item.updated); + if(this.image == null && this.item.thumbnail) { + this.thumnailUrl = this.appConfig.getConfig("apiEndPoint")+item.url+'/thumbnail?v=' + Date.parse(item.updated); + } else if (this.image == null) { + this.thumnailUrl = ""; + } + return this.thumnailUrl; } getFontSize():string { @@ -47,15 +59,28 @@ import { AppConfig } from "../../shared/app.config"; return this.edit && this.item != null; } - onEditClick() { - var endpoint = `${this.appConfig.getConfig("apiEndPoint")}/api/v1/items/${this.item.code}/thumbnail`; - this.modal.open(endpoint,4/3); + hasThumbnail():boolean { + return (this.item && this.item.thumbnail) || this.image != null; } - onChanged() { - if(this.item) { - this.item.updated = new Date(new Date().getTime()).toISOString(); - this.changeDetector.detectChanges(); - } + onEditClick() { + this.endpoint = `${this.appConfig.getConfig("apiEndPoint")}/api/v1/items/${this.item.code}/thumbnail`; + this.modal.open(this.endpoint,4/3,false,200,false); } + + onChanged(event:any) { + this.image = event.croppedImage; + this.imageService.blobToDataUrl(event.croppedImage).then(url => { + this.thumnailUrl = url + this.changeDetector.detectChanges(); + if(this.save) this.saveImage(); + this.changed.emit(event.croppedImage); + }); + } + + saveImage() { + if(this.image) { + this.imageService.putImage(this.endpoint,this.image).subscribe(() => {}); + } + } } \ No newline at end of file diff --git a/projects/common/src/fm/services/image.service.ts b/projects/common/src/fm/services/image.service.ts index fdf960c..e16f7df 100644 --- a/projects/common/src/fm/services/image.service.ts +++ b/projects/common/src/fm/services/image.service.ts @@ -48,4 +48,18 @@ export class ImageService { blobToFile(blob:Blob, filename:string):File { return new File([blob],filename,{type:blob.type}); } + + blobToDataUrl(blob:File):Promise { + + return new Promise((resolve) => { + let reader = new FileReader(); + reader.addEventListener('error', () => { + resolve(""); + }); + reader.addEventListener("load", function () { + resolve(reader.result as string); + }, false); + reader.readAsDataURL(blob); + }); + } }