From 28f304faa2f0e5b7ec40bf4e972d426a1590a4af Mon Sep 17 00:00:00 2001 From: Mohammed Abad Date: Thu, 29 Aug 2024 19:00:46 +0100 Subject: [PATCH] collection share using screenshot pt1 --- .../app/core/services/screenshot.service.ts | 46 +++++++++++++++++++ .../userCollection-grid-card.component.ts | 4 +- .../userCollection-info-page.component.html | 4 +- .../userCollection-info-page.component.ts | 44 ++++++++++++++++-- 4 files changed, 90 insertions(+), 8 deletions(-) create mode 100644 frontend/src/app/core/services/screenshot.service.ts diff --git a/frontend/src/app/core/services/screenshot.service.ts b/frontend/src/app/core/services/screenshot.service.ts new file mode 100644 index 0000000..c142b2e --- /dev/null +++ b/frontend/src/app/core/services/screenshot.service.ts @@ -0,0 +1,46 @@ +import { Injectable } from '@angular/core'; +import html2canvas from 'html2canvas'; + +@Injectable({ + providedIn: 'root' +}) +export class ScreenshotService { + + constructor() { } + + capture(elementId: string): Promise { + const element = document.getElementById(elementId); + if (!element) { + throw new Error('Element not found'); + } + + return this.waitForImagesToLoad(element).then(() => { + return html2canvas(element, { + useCORS: true, + logging: true, // Enables logging for debugging + allowTaint: false // Set to false to respect CORS, true can be used if images are all same-origin and CORS isn't needed + }).then(canvas => { + return canvas.toDataURL('image/png'); + }); + }); + } + + private waitForImagesToLoad(element: HTMLElement): Promise { + const images = element.getElementsByTagName('img'); + const loadPromises = Array.from(images).map(img => { + if (img.complete && img.naturalHeight !== 0) { + return Promise.resolve(); + } + return new Promise((resolve, reject) => { + img.onload = () => resolve(); + img.onerror = () => reject(new Error('Image failed to load.')); + }); + }); + + return Promise.all(loadPromises).then(() => {}); + } + + resolve () { + + } +} \ No newline at end of file diff --git a/frontend/src/app/features/collections/user-collection-info/userCollection-grid-card/userCollection-grid-card.component.ts b/frontend/src/app/features/collections/user-collection-info/userCollection-grid-card/userCollection-grid-card.component.ts index d1b88b5..ce18830 100644 --- a/frontend/src/app/features/collections/user-collection-info/userCollection-grid-card/userCollection-grid-card.component.ts +++ b/frontend/src/app/features/collections/user-collection-info/userCollection-grid-card/userCollection-grid-card.component.ts @@ -48,8 +48,8 @@ export class UserCollectionGridCardComponent implements OnInit { get posterPath() { // * Not in library, use TMDB poster - if (!this.entry.isInLibrary) - return `https://image.tmdb.org/t/p/w300` + this.entry.poster_path; + // if (!this.entry.isInLibrary) + // return `https://image.tmdb.org/t/p/w300` + this.entry.poster_path; // * Is in library: use a local poster if (this.entry.medias) { diff --git a/frontend/src/app/features/collections/user-collection-info/userCollection-info-page/userCollection-info-page.component.html b/frontend/src/app/features/collections/user-collection-info/userCollection-info-page/userCollection-info-page.component.html index 2ae88df..9f1612c 100644 --- a/frontend/src/app/features/collections/user-collection-info/userCollection-info-page/userCollection-info-page.component.html +++ b/frontend/src/app/features/collections/user-collection-info/userCollection-info-page/userCollection-info-page.component.html @@ -28,6 +28,7 @@
  • Switch View
  • Delete
  • +
  • Share (as image)
  • @@ -40,7 +41,8 @@
    {{collection.description}}

    -
    +
    +

    {{collection.name}}

    diff --git a/frontend/src/app/features/collections/user-collection-info/userCollection-info-page/userCollection-info-page.component.ts b/frontend/src/app/features/collections/user-collection-info/userCollection-info-page/userCollection-info-page.component.ts index 9dc5c25..51ec8f5 100644 --- a/frontend/src/app/features/collections/user-collection-info/userCollection-info-page/userCollection-info-page.component.ts +++ b/frontend/src/app/features/collections/user-collection-info/userCollection-info-page/userCollection-info-page.component.ts @@ -7,6 +7,7 @@ import { CollectionEntry } from '../../../../shared/models/dtos/collection-entry import { CollectionEditModalComponent } from '../collection-edit-modal/collection-edit-modal.component'; import { MatDialog } from '@angular/material/dialog'; import { ConfirmModalComponent } from '../../../../shared/components/modals/confirm-modal/confirm-modal.component'; +import { ScreenshotService } from '../../../../core/services/screenshot.service'; @Component({ selector: 'app-userCollection-info-page', @@ -20,10 +21,12 @@ export class UserCollectionInfoPageComponent implements OnInit { updatedAt: string = ''; viewMode: string = 'grid'; + screenMode: boolean = false; constructor( private route: ActivatedRoute, private userCollectionService: UserCollectionService, + private screenshotService: ScreenshotService, private datePipe: DatePipe, public dialog: MatDialog, private router: Router @@ -31,7 +34,7 @@ export class UserCollectionInfoPageComponent implements OnInit { ngOnInit(): void { const id = this.route.snapshot.paramMap.get('id'); - if (id == null) return; + if (id == null) return; // todo redirect if no id is provided this.userCollectionService.getCollection(id.toString()).subscribe( (data) => { @@ -40,10 +43,10 @@ export class UserCollectionInfoPageComponent implements OnInit { this.createdAt = this.getCreatedDate()?.toString() || ''; this.updatedAt = this.getUpdatedAt()?.toString() || ''; - console.log( - `Collection "${this.collection.name}" received:`, - this.collection - ); + // console.log( + // `Collection "${this.collection.name}" received:`, + // this.collection + // ); }, (error) => { console.error('Error occurred:', error); @@ -170,4 +173,35 @@ export class UserCollectionInfoPageComponent implements OnInit { } }); } + + shareScreenshot() { + this.screenMode = true; + const oldViewMode = this.viewMode; + this.viewMode = 'grid'; + + this.screenshotService.capture('gridDiv').then(dataUrl => { + this.copyToClipboard(dataUrl); + }).catch(error => { + console.error('Error capturing screenshot:', error); + }); + + this.screenMode = false; + this.viewMode = oldViewMode; + } + + private copyToClipboard(dataUrl: string) { + const img = new Image(); + img.src = dataUrl; + const canvas = document.createElement('canvas'); + const ctx = canvas.getContext('2d'); + img.onload = () => { + canvas.width = img.width; + canvas.height = img.height; + ctx!.drawImage(img, 0, 0); + canvas.toBlob(blob => { + const item = new ClipboardItem({ 'image/png': blob as Blob }); + navigator.clipboard.write([item]); + }); + }; + } }