* 添加或更新高亮 * @param file 文件 * @param highlight 高亮信息 * @returns 添加的高亮
(file: TFile, highlight: HiNote)
| 28 | * @returns 添加的高亮 |
| 29 | */ |
| 30 | async addHighlight(file: TFile, highlight: HiNote): Promise<HiNote> { |
| 31 | if (!highlight.id) { |
| 32 | highlight.id = IdGenerator.generateHighlightId( |
| 33 | file.path, |
| 34 | highlight.position || 0, |
| 35 | highlight.text |
| 36 | ); |
| 37 | } |
| 38 | |
| 39 | const now = Date.now(); |
| 40 | if (!highlight.createdAt) { |
| 41 | highlight.createdAt = now; |
| 42 | } |
| 43 | highlight.updatedAt = now; |
| 44 | |
| 45 | const filePath = file.path; |
| 46 | const fileHighlights = await this.repository.getFileHighlights(filePath); |
| 47 | const existingIndex = fileHighlights.findIndex(h => h.id === highlight.id); |
| 48 | |
| 49 | if (existingIndex >= 0) { |
| 50 | fileHighlights[existingIndex] = highlight; |
| 51 | } else { |
| 52 | fileHighlights.push(highlight); |
| 53 | } |
| 54 | |
| 55 | await this.repository.saveFileHighlights(filePath, fileHighlights); |
| 56 | |
| 57 | if (this.eventManager) { |
| 58 | if (highlight.comments && highlight.comments.length > 0) { |
| 59 | const latestComment = highlight.comments[highlight.comments.length - 1]; |
| 60 | this.eventManager.emitCommentUpdate(filePath, highlight.text, latestComment.content, highlight.id); |
| 61 | } else { |
| 62 | this.eventManager.emitHighlightUpdate(filePath, highlight.text, highlight.text, highlight.id); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | return highlight; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * 移除高亮 |
no test coverage detected