collection share using screenshot pt1

This commit is contained in:
X4Lo committed 2024-08-29 19:00:46 +01:00
1 parent 3aa59eec16
commit 28f304faa2
4 files changed
+90 -8

No files matched your search

@@ -0,0 +1,46 @@
import { Injectable } from '@angular/core';
import html2canvas from 'html2canvas';
@Injectable({
providedIn: 'root'
})
export class ScreenshotService {
constructor() { }
capture(elementId: string): Promise<string> {
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<void> {
const images = element.getElementsByTagName('img');
const loadPromises = Array.from(images).map(img => {
if (img.complete && img.naturalHeight !== 0) {
return Promise.resolve();
}
return new Promise<void>((resolve, reject) => {
img.onload = () => resolve();
img.onerror = () => reject(new Error('Image failed to load.'));
});
});
return Promise.all(loadPromises).then(() => {});
}
resolve () {
}
}
@@ -48,8 +48,8 @@ export class UserCollectionGridCardComponent implements OnInit {
get posterPath() { get posterPath() {
// * Not in library, use TMDB poster // * Not in library, use TMDB poster
if (!this.entry.isInLibrary) // if (!this.entry.isInLibrary)
return `https://image.tmdb.org/t/p/w300` + this.entry.poster_path; // return `https://image.tmdb.org/t/p/w300` + this.entry.poster_path;
// * Is in library: use a local poster // * Is in library: use a local poster
if (this.entry.medias) { if (this.entry.medias) {
@@ -28,6 +28,7 @@
</li> </li>
<li><a class="dropdown-item" href="javascript:;" (click)="switchViewMode()">Switch View</a></li> <li><a class="dropdown-item" href="javascript:;" (click)="switchViewMode()">Switch View</a></li>
<li><a class="dropdown-item" href="javascript:;" (click)="deleteCollection()">Delete</a></li> <li><a class="dropdown-item" href="javascript:;" (click)="deleteCollection()">Delete</a></li>
<li><a class="dropdown-item" href="javascript:;" (click)="shareScreenshot()">Share (as image)</a></li>
</ul> </ul>
</div> </div>
</div> </div>
@@ -40,7 +41,8 @@
<div *ngIf="collection.description != ''">{{collection.description}}</div> <div *ngIf="collection.description != ''">{{collection.description}}</div>
<hr> <hr>
<!-- ! grid --> <!-- ! grid -->
<div *ngIf="viewMode == 'grid'"> <div id="gridDiv" *ngIf="viewMode == 'grid'">
<h1 *ngIf="screenMode">{{collection.name}}</h1>
<app-userCollection-entry-grid [entries]="collection.entries" <app-userCollection-entry-grid [entries]="collection.entries"
[collectionId]="getListId()"></app-userCollection-entry-grid> [collectionId]="getListId()"></app-userCollection-entry-grid>
</div> </div>
@@ -7,6 +7,7 @@ import { CollectionEntry } from '../../../../shared/models/dtos/collection-entry
import { CollectionEditModalComponent } from '../collection-edit-modal/collection-edit-modal.component'; import { CollectionEditModalComponent } from '../collection-edit-modal/collection-edit-modal.component';
import { MatDialog } from '@angular/material/dialog'; import { MatDialog } from '@angular/material/dialog';
import { ConfirmModalComponent } from '../../../../shared/components/modals/confirm-modal/confirm-modal.component'; import { ConfirmModalComponent } from '../../../../shared/components/modals/confirm-modal/confirm-modal.component';
import { ScreenshotService } from '../../../../core/services/screenshot.service';
@Component({ @Component({
selector: 'app-userCollection-info-page', selector: 'app-userCollection-info-page',
@@ -20,10 +21,12 @@ export class UserCollectionInfoPageComponent implements OnInit {
updatedAt: string = ''; updatedAt: string = '';
viewMode: string = 'grid'; viewMode: string = 'grid';
screenMode: boolean = false;
constructor( constructor(
private route: ActivatedRoute, private route: ActivatedRoute,
private userCollectionService: UserCollectionService, private userCollectionService: UserCollectionService,
private screenshotService: ScreenshotService,
private datePipe: DatePipe, private datePipe: DatePipe,
public dialog: MatDialog, public dialog: MatDialog,
private router: Router private router: Router
@@ -31,7 +34,7 @@ export class UserCollectionInfoPageComponent implements OnInit {
ngOnInit(): void { ngOnInit(): void {
const id = this.route.snapshot.paramMap.get('id'); 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( this.userCollectionService.getCollection(id.toString()).subscribe(
(data) => { (data) => {
@@ -40,10 +43,10 @@ export class UserCollectionInfoPageComponent implements OnInit {
this.createdAt = this.getCreatedDate()?.toString() || ''; this.createdAt = this.getCreatedDate()?.toString() || '';
this.updatedAt = this.getUpdatedAt()?.toString() || ''; this.updatedAt = this.getUpdatedAt()?.toString() || '';
console.log( // console.log(
`Collection "${this.collection.name}" received:`, // `Collection "${this.collection.name}" received:`,
this.collection // this.collection
); // );
}, },
(error) => { (error) => {
console.error('Error occurred:', 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]);
});
};
}
} }