refactor of image modal code

This commit is contained in:
itzMiney committed 2026-03-17 01:44:26 +01:00
1 parent 293a06e9a4
commit 357e1b6cf3
2 files changed
+50 -89

No files matched your search

@@ -1,4 +1,4 @@
import {AfterViewInit, Component, ElementRef, OnInit, ViewChild} from '@angular/core'; import {Component, OnInit, ViewEncapsulation} from '@angular/core';
import { DatePipe, NgStyle } from '@angular/common'; import { DatePipe, NgStyle } from '@angular/common';
import {ArticleService} from '../../shared/services/article.service'; import {ArticleService} from '../../shared/services/article.service';
import {ActivatedRoute} from '@angular/router'; import {ActivatedRoute} from '@angular/router';
@@ -20,6 +20,7 @@ interface Article {
@Component({ @Component({
selector: 'app-article-page', selector: 'app-article-page',
encapsulation: ViewEncapsulation.None,
imports: [ imports: [
DatePipe, DatePipe,
NgStyle, NgStyle,
@@ -29,16 +30,14 @@ interface Article {
templateUrl: './article-page.component.html', templateUrl: './article-page.component.html',
styleUrl: './article-page.component.css' styleUrl: './article-page.component.css'
}) })
export class ArticlePageComponent implements OnInit, AfterViewInit { export class ArticlePageComponent implements OnInit {
@ViewChild('articleContent') articleContent!: ElementRef<HTMLDivElement>;
selectedImage: string | null = null; selectedImage: string | null = null;
private imagesProcessed = false; article: Article | null = null;
article: Article | undefined;
currentArticle: any;
error: boolean = false; error: boolean = false;
copied: boolean = false; copied: boolean = false;
loading: boolean = true; loading: boolean = true;
constructor( constructor(
private articleService: ArticleService, private articleService: ArticleService,
private titleService: Title, private titleService: Title,
@@ -49,28 +48,57 @@ export class ArticlePageComponent implements OnInit, AfterViewInit {
ngOnInit() { ngOnInit() {
this.route.params.subscribe((params) => { this.route.params.subscribe((params) => {
const slug = params['slug']; const slug = params['slug'];
if (slug) {
this.loadTitleBySlug(slug);
} else {
this.titleService.setTitle('Article | itzMiney');
}
});
const slug = this.route.snapshot.paramMap.get('slug') || ''; this.loading = true;
this.articleService.getArticleBySlug(slug).subscribe( this.error = false;
(article) => { this.article = null;
if (!slug) {
this.titleService.setTitle('Error | itzMiney');
return;
}
this.articleService.getArticleBySlug(slug).subscribe({
next: (article: Article | undefined) => {
if (!article) {
this.article = null;
this.loading = false;
this.error = true;
return;
}
this.article = article; this.article = article;
this.titleService.setTitle(`${article.title} | itzMiney`);
this.loading = false; this.loading = false;
console.log('Article found!')
}, },
(error) => { error: (error) => {
console.error('Error fetching article:', error); console.error('Error fetching article:', error);
this.article = null;
this.loading = false; this.loading = false;
if (error.status === 404) { if (error.status === 404) {
this.error = true; this.error = true;
} }
} }
); });
});
}
onArticleContentClick(event: MouseEvent) {
const target = event.target as HTMLElement;
if (target.tagName === 'IMG') {
const img = target as HTMLImageElement;
this.openPreview(img.src);
}
}
openPreview(src: string) {
this.selectedImage = src;
}
closePreview() {
this.selectedImage = null;
} }
copyLink() { copyLink() {
@@ -84,48 +112,4 @@ export class ArticlePageComponent implements OnInit, AfterViewInit {
navigator.clipboard.writeText(shareLink).then() navigator.clipboard.writeText(shareLink).then()
} }
ngAfterViewInit() {
this.makeImagesClickable();
this.imagesProcessed = true;
}
private makeImagesClickable() {
const imgs: NodeListOf<HTMLImageElement> = this.articleContent.nativeElement.querySelectorAll('img');
imgs.forEach(img => {
img.style.cursor = 'pointer';
if (!img.dataset['clickable']) {
img.addEventListener('click', () => this.openPreview(img.src));
img.dataset['clickable'] = 'true';
}
});
}
openPreview(src: string) {
this.selectedImage = src;
}
closePreview() {
this.selectedImage = null;
}
loadTitleBySlug(slug: string) {
this.articleService.getArticleBySlug(slug).subscribe(
(article) => {
this.article = article;
this.loading = false;
setTimeout(() => this.makeImagesClickable(), 0);
console.log('Article found!')
},
(error) => {
console.error('Error fetching article:', error);
this.loading = false;
if (error.status === 404) {
this.error = true;
}
}
);
}
} }
+3 -26
View File
@@ -1,14 +1,14 @@
import {Component, OnInit} from '@angular/core'; import {Component, OnInit, ViewEncapsulation} from '@angular/core';
import {DeviceDetectorService} from '../shared/services/device-detector.service'; import {DeviceDetectorService} from '../shared/services/device-detector.service';
import { CommonModule, NgStyle } from '@angular/common'; import { CommonModule, NgStyle } from '@angular/common';
import {Title} from '@angular/platform-browser'; import {Title} from '@angular/platform-browser';
import {AudioPlayerComponent} from '../shared/audio-player/audio-player.component'; import {AudioPlayerComponent} from '../shared/audio-player/audio-player.component';
import {TranslatePipe} from '@ngx-translate/core'; import {TranslatePipe} from '@ngx-translate/core';
import {animate, stagger, style, transition, trigger, query} from '@angular/animations';
import {ParallaxComponent} from '../shared/parallax/parallax.component'; import {ParallaxComponent} from '../shared/parallax/parallax.component';
@Component({ @Component({
selector: 'app-portfolio', selector: 'app-portfolio',
encapsulation: ViewEncapsulation.None,
imports: [ imports: [
NgStyle, NgStyle,
AudioPlayerComponent, AudioPlayerComponent,
@@ -17,30 +17,7 @@ import {ParallaxComponent} from '../shared/parallax/parallax.component';
ParallaxComponent ParallaxComponent
], ],
templateUrl: './portfolio.component.html', templateUrl: './portfolio.component.html',
styleUrl: './portfolio.component.css', styleUrl: './portfolio.component.css'
animations: [
trigger('fadeInOut', [
transition(':enter', [
style({ opacity: 0, transform: 'translateY(20px)' }),
animate('0.5s ease-out', style({ opacity: 1, transform: 'translateY(0)' }))
]),
transition(':leave', [
animate('0.3s ease-in', style({ opacity: 0, transform: 'translateY(20px)' }))
])
]),
trigger('staggerGallery', [
transition(':enter', [
query('.gallery-item', [
style({ opacity: 0, transform: 'translateY(20px)' }),
stagger(100, [
animate('0.5s ease-out', style({ opacity: 1, transform: 'translateY(0)' }))
])
], {
optional: true
})
])
])
]
}) })
export class PortfolioComponent implements OnInit { export class PortfolioComponent implements OnInit {
galleryItems = [ galleryItems = [