tab1.page.ts 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import { Component } from '@angular/core';
  2. interface Photo {
  3. id: number;
  4. url: string;
  5. tags: string[];
  6. }
  7. @Component({
  8. selector: 'app-tab1',
  9. templateUrl: 'tab1.page.html',
  10. styleUrls: ['tab1.page.scss'],
  11. })
  12. export class Tab1Page {
  13. photos: Photo[] = [
  14. { id: 1, url: 'assets/photos/photo1.jpg', tags: ['赛马娘', '东商改革'] },
  15. { id: 2, url: 'assets/photos/photo2.jpg', tags: ['赛马娘', '东海帝皇', '目白麦昆'] },
  16. { id: 3, url: 'assets/photos/photo3.jpg', tags: ['赛马娘', '东海帝皇'] },
  17. { id: 4, url: 'assets/photos/photo4.jpg', tags: ['赛马娘', '目白麦昆'] },
  18. { id: 5, url: 'assets/photos/photo5.jpg', tags: ['赛马娘', '草上飞'] },
  19. { id: 6, url: 'assets/photos/photo6.jpg', tags: ['赛马娘', '北部玄驹'] },
  20. // 添加更多照片
  21. ];
  22. displayedPhotos: Photo[] = [];
  23. newPhotoTags: string = '';
  24. constructor() {
  25. this.displayedPhotos = [...this.photos]; // 初始显示所有照片
  26. }
  27. onSearch(event: CustomEvent) {
  28. const searchTerm = (event.target as HTMLInputElement).value.toLowerCase().trim();
  29. if (searchTerm === '') {
  30. this.displayedPhotos = [...this.photos]; // 重置显示所有照片
  31. } else {
  32. this.displayedPhotos = this.photos.filter(photo =>
  33. photo.tags.some(tag => tag.toLowerCase().includes(searchTerm))
  34. );
  35. }
  36. }
  37. openFullscreen(imageUrl: string) {
  38. window.open(imageUrl, '_blank');
  39. }
  40. // 打开文件选择器
  41. openFilePicker() {
  42. const input = document.createElement('input');
  43. input.type = 'file';
  44. input.accept = 'image/*';
  45. input.onchange = (event: Event) => this.handleFileSelect(event);
  46. input.click();
  47. }
  48. // 处理文件选择和标签输入
  49. handleFileSelect(event: Event) {
  50. const input = event.target as HTMLInputElement;
  51. if (input.files && input.files.length > 0) {
  52. const file = input.files[0];
  53. const reader = new FileReader();
  54. reader.readAsDataURL(file);
  55. reader.onload = () => {
  56. const imageUrl = reader.result as string;
  57. const newTags = this.newPhotoTags.split(',').map(tag => tag.trim());
  58. const newPhoto: Photo = {
  59. id: this.photos.length + 1,
  60. url: imageUrl,
  61. tags: newTags,
  62. };
  63. this.photos.push(newPhoto);
  64. this.displayedPhotos = [...this.photos]; // 更新显示的照片列表
  65. this.newPhotoTags = ''; // 清空标签输入框
  66. };
  67. reader.onerror = error => {
  68. console.error('FileReader error:', error);
  69. };
  70. }
  71. }
  72. }