import { Injectable, EventEmitter } from '@angular/core'; import * as domtoimage from 'dom-to-image'; import { ShareComponent } from './share/share.component'; import { MatDialog, MatDialogRef } from '@angular/material'; import { HttpClient } from '@angular/common/http'; import { IcsDriveService } from '@varmasagi/ics-drive'; import { IcsToastService } from '@varmasagi/ics-toast'; /** * Service to enable sharing of files through Context Menu. * Internal implementation, not intended for end users. */ @Injectable() export class SharingService { public s3Link: string; /** * EventEmitter to trigger the loading animation when request is being processed. */ processingEmitter: EventEmitter = new EventEmitter(); constructor( public dialog: MatDialog, private http: HttpClient, private driveService: IcsDriveService, private toastService: IcsToastService ) {} /** * * @param id - Id of the div who's image is to be captured. */ openModalShare(id: string) { const node = document.getElementById(id); this.processingEmitter.emit(true); setTimeout( () => domtoimage .toJpeg(node, { quality: 0.95, bgcolor: '#ffffff' }) .then(dataUrl => { var img = new Image(); img.src = dataUrl; var data = { name: 'my-image.jpeg', file: dataUrl }; this.driveService.sharingFileUpload(data).subscribe( response => { this.s3Link = response.key; this.dialog.open(ShareComponent, { panelClass: 'detailDialog', width: '50%', height: '40%', data: { dataKey: this.s3Link } }).afterOpen().subscribe(()=>{ this.processingEmitter.emit(false); }); }, err => { const message = err.message ? err.message : 'Oops! Something went wrong!'; this.toastService.error(message); } ); }), 100 ); } downloadElementImage(id: string) { const node = document.getElementById(id); setTimeout( () => domtoimage .toJpeg(node, { quality: 0.95, bgcolor: '#ffffff' }) .then(dataUrl => { const img = new Image(); img.src = dataUrl; const link = document.createElement('a'); link.download = new Date()+'_image.jpeg'; link.href = dataUrl; link.click(); }), 100 ); } }