MCPcopy Create free account
hub / github.com/CatMuse/HiNote / deleteComment

Method deleteComment

src/services/comment/CommentService.ts:159–211  ·  view source on GitHub ↗

* 删除评论

(highlight: HighlightInfo, commentId: string)

Source from the content-addressed store, hash-verified

157 * 删除评论
158 */
159 async deleteComment(highlight: HighlightInfo, commentId: string): Promise<void> {
160 const file = await this.getFileForHighlight(highlight);
161 if (!file || !highlight.comments) return;
162 let removedHighlight = false;
163
164 // 过滤掉要删除的批注
165 highlight.comments = highlight.comments.filter(c => c.id !== commentId);
166 highlight.updatedAt = Date.now();
167
168 // 检查高亮是否没有评论了
169 if (highlight.comments.length === 0) {
170 // 检查高亮是否关联了闪卡
171 const hasFlashcard = highlight.id ? this.checkHasFlashcard(highlight.id) : false;
172
173 // 如果是虚拟高亮或者没有关联闪卡,则删除整个高亮
174 if (highlight.isVirtual || !hasFlashcard) {
175 // 从 HighlightManager 中删除高亮
176 await this.highlightManager.removeHighlight(file, highlight);
177 removedHighlight = true;
178
179 // 从当前高亮列表中移除
180 this.highlights = this.highlights.filter(h => {
181 // 如果有 ID,通过 ID 比较
182 if (h.id && highlight.id) {
183 return h.id !== highlight.id;
184 }
185 // 如果没有 ID,通过位置和文本比较
186 return !(h.position === highlight.position && h.text === highlight.text);
187 });
188
189 // 通知外部更新高亮列表
190 if (this.onHighlightsUpdate) {
191 this.onHighlightsUpdate(this.highlights);
192 }
193 } else {
194 // 有关联闪卡,只更新评论
195 await this.highlightManager.addHighlight(file, highlight);
196 }
197 } else {
198 // 还有其他评论,只更新评论
199 await this.highlightManager.addHighlight(file, highlight);
200 }
201
202 // 只更新单个卡片,而不是刷新整个视图
203 if (removedHighlight && this.onCardRemove) {
204 this.onCardRemove(highlight);
205 } else if (this.onCardUpdate) {
206 this.onCardUpdate(highlight);
207 } else if (this.onRefreshView) {
208 // 降级方案:如果没有 onCardUpdate,则刷新整个视图
209 await this.onRefreshView();
210 }
211 }
212
213 /**
214 * 删除虚拟高亮(当取消添加评论时)

Callers 1

configureMethod · 0.80

Calls 4

getFileForHighlightMethod · 0.95
checkHasFlashcardMethod · 0.95
removeHighlightMethod · 0.80
addHighlightMethod · 0.80

Tested by

no test coverage detected