Nav apraksta
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

document-list.component.ts 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import { Component, Input, OnInit } from '@angular/core';
  2. import { CommonModule } from '@angular/common';
  3. import { MatIconModule } from '@angular/material/icon';
  4. import { MatExpansionModule } from '@angular/material/expansion';
  5. import { MatButtonModule } from '@angular/material/button';
  6. import { MatProgressSpinnerModule } from '@angular/material/progress-spinner';
  7. import { MatTooltipModule } from '@angular/material/tooltip';
  8. import { DocumentSession, DocumentType, DocumentTypeDisplayName, DocumentTypeIcon, getDocumentTypeOrder } from '../models/document.model';
  9. import { MarkdownPipe } from '../shared/pipes/markdown.pipe';
  10. @Component({
  11. selector: 'app-document-list',
  12. standalone: true,
  13. imports: [
  14. CommonModule,
  15. MatIconModule,
  16. MatExpansionModule,
  17. MatButtonModule,
  18. MatProgressSpinnerModule,
  19. MatTooltipModule,
  20. MarkdownPipe
  21. ],
  22. templateUrl: './document-list.component.html',
  23. styleUrl: './document-list.component.scss'
  24. })
  25. export class DocumentListComponent implements OnInit {
  26. @Input() documents: DocumentSession[] = [];
  27. @Input() defaultExpanded: boolean = false;
  28. // 按固定顺序排序的文档类型
  29. documentTypeOrder = getDocumentTypeOrder();
  30. // 控制每个文档的展开状态
  31. expandedStates: Record<DocumentType, boolean> = {
  32. [DocumentType.UserRequirement]: false,
  33. [DocumentType.Requirement]: false,
  34. [DocumentType.Technical]: false,
  35. [DocumentType.Implementation]: false,
  36. [DocumentType.Test]: false
  37. };
  38. ngOnInit() {
  39. // 初始化展开状态
  40. if (this.defaultExpanded) {
  41. this.documentTypeOrder.forEach(docType => {
  42. this.expandedStates[docType] = true;
  43. });
  44. }
  45. }
  46. /**
  47. * 获取指定类型的文档
  48. */
  49. getDocumentByType(docType: DocumentType): DocumentSession | undefined {
  50. return this.documents.find(doc => doc.type === docType);
  51. }
  52. /**
  53. * 切换文档的展开状态
  54. */
  55. toggleDocument(docType: DocumentType) {
  56. this.expandedStates[docType] = !this.expandedStates[docType];
  57. }
  58. /**
  59. * 获取文档显示名称
  60. */
  61. getDisplayName(docType: DocumentType): string {
  62. return DocumentTypeDisplayName[docType];
  63. }
  64. /**
  65. * 获取文档图标
  66. */
  67. getIcon(docType: DocumentType): string {
  68. return DocumentTypeIcon[docType];
  69. }
  70. /**
  71. * 检查文档是否有内容
  72. */
  73. hasContent(doc: DocumentSession): boolean {
  74. return !!doc.hasContent && !!doc.content && doc.content.trim().length > 0;
  75. }
  76. /**
  77. * 获取文档最后更新时间显示文本
  78. */
  79. getLastUpdatedText(doc: DocumentSession): string {
  80. if (!doc.lastUpdated) return '';
  81. const now = new Date();
  82. const diffMs = now.getTime() - doc.lastUpdated.getTime();
  83. const diffMins = Math.floor(diffMs / (1000 * 60));
  84. const diffHours = Math.floor(diffMs / (1000 * 60 * 60));
  85. const diffDays = Math.floor(diffMs / (1000 * 60 * 60 * 24));
  86. if (diffMins < 60) {
  87. return `${diffMins}分钟前`;
  88. } else if (diffHours < 24) {
  89. return `${diffHours}小时前`;
  90. } else {
  91. return `${diffDays}天前`;
  92. }
  93. }
  94. /**
  95. * 格式化内容长度,用于预览
  96. */
  97. getContentPreview(content: string): string {
  98. if (!content) return '暂无内容';
  99. const maxLength = 100;
  100. const plainText = content.replace(/[#\*`]/g, '').replace(/\n/g, ' ');
  101. if (plainText.length <= maxLength) {
  102. return plainText;
  103. }
  104. return plainText.substring(0, maxLength) + '...';
  105. }
  106. }