MCPcopy Create free account
hub / github.com/experdot/pointer / applyHighlight

Function applyHighlight

src/renderer/src/hooks/useGlobalSearchHighlight.ts:53–149  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

51 let frameId = 0
52
53 const applyHighlight = (): void => {
54 const messageEl = container.querySelector(`[data-message-id="${highlightMatch.messageId}"]`)
55 if (!messageEl) {
56 if (retryCount < MAX_HIGHLIGHT_RETRY_COUNT) {
57 retryCount++
58 frameId = requestAnimationFrame(applyHighlight)
59 }
60 return
61 }
62
63 const contentEl = messageEl.querySelector('.message-item__body')
64 if (!contentEl) {
65 if (retryCount < MAX_HIGHLIGHT_RETRY_COUNT) {
66 retryCount++
67 frameId = requestAnimationFrame(applyHighlight)
68 }
69 return
70 }
71
72 // 获取消息内容
73 const messageContent = messages.find((m) => m.id === highlightMatch.messageId)?.content ?? ''
74 const matchText = messageContent.slice(highlightMatch.contentStart, highlightMatch.contentEnd)
75 if (!matchText) return
76
77 // 收集文本节点
78 const textNodes: Text[] = []
79 const walker = document.createTreeWalker(contentEl, NodeFilter.SHOW_TEXT, null)
80 let node: Text | null
81 while ((node = walker.nextNode() as Text)) {
82 if (node.textContent) {
83 textNodes.push(node)
84 }
85 }
86
87 // 拼接所有文本节点的内容
88 let fullText = ''
89 const nodeOffsets: { node: Text; start: number; end: number }[] = []
90 for (const textNode of textNodes) {
91 const text = textNode.textContent ?? ''
92 nodeOffsets.push({
93 node: textNode,
94 start: fullText.length,
95 end: fullText.length + text.length
96 })
97 fullText += text
98 }
99
100 // 找到匹配位置(基于原始内容位置在渲染后的 DOM 中查找)
101 // 由于 Markdown 渲染可能改变位置,这里按“该消息中的第 N 个命中”定位
102 const idx = findNthMatchIndex(
103 fullText,
104 matchText,
105 highlightMatch.occurrenceIndexInMessage ?? 0
106 )
107 if (idx === -1) {
108 if (retryCount < MAX_HIGHLIGHT_RETRY_COUNT) {
109 retryCount++
110 frameId = requestAnimationFrame(applyHighlight)

Callers

nothing calls this directly

Calls 2

findNthMatchIndexFunction · 0.85

Tested by

no test coverage detected