collection share using screenshot pt1
This commit is contained in:
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 () {
|
||||
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -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) {
|
||||
|
||||
+3
-1
@@ -28,6 +28,7 @@
|
||||
</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)="shareScreenshot()">Share (as image)</a></li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
@@ -40,7 +41,8 @@
|
||||
<div *ngIf="collection.description != ''">{{collection.description}}</div>
|
||||
<hr>
|
||||
<!-- ! grid -->
|
||||
<div *ngIf="viewMode == 'grid'">
|
||||
<div id="gridDiv" *ngIf="viewMode == 'grid'">
|
||||
<h1 *ngIf="screenMode">{{collection.name}}</h1>
|
||||
<app-userCollection-entry-grid [entries]="collection.entries"
|
||||
[collectionId]="getListId()"></app-userCollection-entry-grid>
|
||||
</div>
|
||||
|
||||
+39
-5
@@ -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]);
|
||||
});
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user