Files
MarkLite/src/renderer/components/SearchBar/SearchBar.tsx
T
thzxx b3987e9abd fix: 修复侧边栏根目录、搜索高亮导航、行号同步
问题1: 侧边栏打开文件夹后根目录不显示
修复: FileTree 传入包装后的根节点 { name, path, type: 'dir', children: tree }
      打开文件夹时自动展开根目录 expandDirs([rootPath])

问题2: 搜索没有高亮,上一个/下一个没反应
修复: Editor 添加完整搜索功能
      - 搜索时在 textarea 上方创建 overlay div
      - 用绝对定位 div 标记每个匹配位置(单行/多行)
      - currentIndex 变化时选中匹配文本并滚动到可视区域
      - 搜索结果随内容变化自动更新

问题3: 滚动时行号跟随不准确
修复: 行高测量改为与 CSS line-height 一致的动态测量
      搜索高亮位置随编辑器滚动同步更新
2026-05-27 22:49:34 +08:00

148 lines
5.2 KiB
TypeScript

import React, { useCallback, useRef, useEffect } from 'react'
import { useSearchStore } from '../../stores/searchStore'
import { useTabStore } from '../../stores/tabStore'
import { findMatches } from '../../lib/searchEngine'
import { ChevronUp, ChevronDown, X } from '../Icons'
export function SearchBar() {
const searchInputRef = useRef<HTMLInputElement>(null)
const isVisible = useSearchStore(s => s.isVisible)
const showReplace = useSearchStore(s => s.showReplace)
const searchText = useSearchStore(s => s.searchText)
const replaceText = useSearchStore(s => s.replaceText)
const matches = useSearchStore(s => s.matches)
const currentIndex = useSearchStore(s => s.currentIndex)
const options = useSearchStore(s => s.options)
const setSearchText = useSearchStore(s => s.setSearchText)
const setReplaceText = useSearchStore(s => s.setReplaceText)
const setShowReplace = useSearchStore(s => s.setShowReplace)
const setMatches = useSearchStore(s => s.setMatches)
const findNext = useSearchStore(s => s.findNext)
const findPrev = useSearchStore(s => s.findPrev)
const toggleCaseSensitive = useSearchStore(s => s.toggleCaseSensitive)
const toggleRegex = useSearchStore(s => s.toggleRegex)
const close = useSearchStore(s => s.close)
const getActiveTab = useTabStore(s => s.getActiveTab)
const doSearch = useCallback((text: string) => {
const tab = getActiveTab()
if (!tab || !text) {
setMatches([])
return
}
const found = findMatches(tab.content, text, options)
setMatches(found)
}, [getActiveTab, options, setMatches])
const handleSearchChange = useCallback((e: React.ChangeEvent<HTMLInputElement>) => {
const text = e.target.value
setSearchText(text)
// Editor 会监听 searchText 变化并执行搜索和高亮
}, [setSearchText])
const handleReplaceCurrent = useCallback(() => {
const tab = getActiveTab()
if (!tab || matches.length === 0 || currentIndex < 0) return
const m = matches[currentIndex]
const newContent = tab.content.substring(0, m.start) + replaceText + tab.content.substring(m.end)
useTabStore.getState().updateTabContent(tab.id, newContent)
doSearch(searchText)
}, [getActiveTab, matches, currentIndex, replaceText, searchText, doSearch])
// B-02: 全部替换(从后向前逐个替换)
const handleReplaceAll = useCallback(() => {
const tab = getActiveTab()
if (!tab || matches.length === 0) return
let result = tab.content
for (let i = matches.length - 1; i >= 0; i--) {
const m = matches[i]
result = result.substring(0, m.start) + replaceText + result.substring(m.end)
}
useTabStore.getState().updateTabContent(tab.id, result)
doSearch(searchText)
}, [getActiveTab, matches, replaceText, searchText, doSearch])
useEffect(() => {
if (isVisible && searchInputRef.current) {
searchInputRef.current.focus()
}
}, [isVisible])
if (!isVisible) return null
return (
<div id="search-bar">
<div className="search-row">
<button
id="btn-toggle-replace"
className={`search-opt-btn ${showReplace ? 'expanded' : ''}`}
onClick={() => setShowReplace(!showReplace)}
title="展开替换行"
>
</button>
<input
ref={searchInputRef}
type="text"
id="search-input"
placeholder="查找..."
value={searchText}
onChange={handleSearchChange}
onKeyDown={(e) => {
if (e.key === 'Enter') {
e.preventDefault()
e.shiftKey ? findPrev() : findNext()
}
if (e.key === 'Escape') {
e.preventDefault()
close()
}
}}
/>
<span id="search-count">
{matches.length > 0 ? `${currentIndex + 1}/${matches.length}` : searchText ? '无结果' : ''}
</span>
<button
className={`search-opt-btn ${options.caseSensitive ? 'active' : ''}`}
onClick={toggleCaseSensitive}
title="区分大小写 (Alt+C)"
>
Aa
</button>
<button
className={`search-opt-btn ${options.useRegex ? 'active' : ''}`}
onClick={toggleRegex}
title="正则表达式 (Alt+R)"
>
.*
</button>
<button className="search-nav-btn" onClick={findPrev} title="上一个 (Shift+Enter)">
<ChevronUp size={12} />
</button>
<button className="search-nav-btn" onClick={findNext} title="下一个 (Enter)">
<ChevronDown size={12} />
</button>
<button className="search-nav-btn" onClick={close} title="关闭 (Escape)">
<X size={14} />
</button>
</div>
{showReplace && (
<div id="replace-row">
<input
type="text"
id="replace-input"
placeholder="替换..."
value={replaceText}
onChange={(e) => setReplaceText(e.target.value)}
/>
<button className="replace-btn" onClick={handleReplaceCurrent} title="替换 (Ctrl+Shift+G)">替换</button>
<button className="replace-btn" onClick={handleReplaceAll} title="全部替换 (Ctrl+Shift+H)">全部</button>
</div>
)}
</div>
)
}