import { Component, Input, OnInit } from '@angular/core'; import { CommonModule } from '@angular/common'; import { MatIconModule } from '@angular/material/icon'; import { MatExpansionModule } from '@angular/material/expansion'; import { MatButtonModule } from '@angular/material/button'; import { MatProgressSpinnerModule } from '@angular/material/progress-spinner'; import { MatTooltipModule } from '@angular/material/tooltip'; import { DocumentSession, DocumentType, DocumentTypeDisplayName, DocumentTypeIcon, getDocumentTypeOrder } from '../models/document.model'; import { MarkdownPipe } from '../shared/pipes/markdown.pipe'; @Component({ selector: 'app-document-list', standalone: true, imports: [ CommonModule, MatIconModule, MatExpansionModule, MatButtonModule, MatProgressSpinnerModule, MatTooltipModule, MarkdownPipe ], templateUrl: './document-list.component.html', styleUrl: './document-list.component.scss' }) export class DocumentListComponent implements OnInit { @Input() documents: DocumentSession[] = []; @Input() defaultExpanded: boolean = false; // 按固定顺序排序的文档类型 documentTypeOrder = getDocumentTypeOrder(); // 控制每个文档的展开状态 expandedStates: Record = { [DocumentType.UserRequirement]: false, [DocumentType.Requirement]: false, [DocumentType.Technical]: false, [DocumentType.Implementation]: false, [DocumentType.Test]: false }; ngOnInit() { // 初始化展开状态 if (this.defaultExpanded) { this.documentTypeOrder.forEach(docType => { this.expandedStates[docType] = true; }); } } /** * 获取指定类型的文档 */ getDocumentByType(docType: DocumentType): DocumentSession | undefined { return this.documents.find(doc => doc.type === docType); } /** * 切换文档的展开状态 */ toggleDocument(docType: DocumentType) { this.expandedStates[docType] = !this.expandedStates[docType]; } /** * 获取文档显示名称 */ getDisplayName(docType: DocumentType): string { return DocumentTypeDisplayName[docType]; } /** * 获取文档图标 */ getIcon(docType: DocumentType): string { return DocumentTypeIcon[docType]; } /** * 检查文档是否有内容 */ hasContent(doc: DocumentSession): boolean { return !!doc.hasContent && !!doc.content && doc.content.trim().length > 0; } /** * 获取文档最后更新时间显示文本 */ getLastUpdatedText(doc: DocumentSession): string { if (!doc.lastUpdated) return ''; const now = new Date(); const diffMs = now.getTime() - doc.lastUpdated.getTime(); const diffMins = Math.floor(diffMs / (1000 * 60)); const diffHours = Math.floor(diffMs / (1000 * 60 * 60)); const diffDays = Math.floor(diffMs / (1000 * 60 * 60 * 24)); if (diffMins < 60) { return `${diffMins}分钟前`; } else if (diffHours < 24) { return `${diffHours}小时前`; } else { return `${diffDays}天前`; } } /** * 格式化内容长度,用于预览 */ getContentPreview(content: string): string { if (!content) return '暂无内容'; const maxLength = 100; const plainText = content.replace(/[#\*`]/g, '').replace(/\n/g, ' '); if (plainText.length <= maxLength) { return plainText; } return plainText.substring(0, maxLength) + '...'; } }