refactor: 删除自定义搜索,统一使用 CodeMirror 6 搜索

删除文件:
- SearchBar/SearchBar.tsx — 自定义搜索栏组件
- stores/searchStore.ts — 自定义搜索状态管理
- lib/searchEngine.ts — 自定义搜索匹配引擎
- types/search.ts — 搜索相关类型定义

清理代码:
- App.tsx:移除 SearchBar 导入和渲染
- useKeyboard.ts:移除 searchStore 引用和搜索快捷键
- Editor.tsx:移除 searchStore 引用和搜索逻辑
- types/index.ts:移除 search 类型导出
- global.css:删除旧搜索栏样式(~113 行)

CodeMirror 6 搜索面板移到顶部:
- .cm-panels { top: 0; bottom: auto; }
- .cm-panels-bottom { top: 0; bottom: auto; }

快捷键(CodeMirror 内置):
- Ctrl+F 搜索
- Ctrl+H 替换
- Enter/Shift+Enter 下一个/上一个
- Alt+C 大小写
- Alt+R 正则
This commit is contained in:
thzxx
2026-05-27 23:54:55 +08:00
parent e40a8bd88e
commit d57a2b2aac
9 changed files with 19 additions and 425 deletions
+3 -13
View File
@@ -1,6 +1,5 @@
import React, { useEffect, useCallback } from 'react'
import { useTabStore } from '../../stores/tabStore'
import { useSearchStore } from '../../stores/searchStore'
import { useCodeMirror } from './useCodeMirror'
import { EditorToolbar } from './EditorToolbar'
@@ -20,12 +19,10 @@ export function Editor({ darkMode }: EditorProps) {
containerRef,
viewRef,
setContent,
getContent,
getScrollTop,
setScrollTop,
getSelection,
setSelection,
scrollTo
setSelection
} = useCodeMirror({
content: activeTab?.content ?? '',
onChange: useCallback((value: string) => {
@@ -43,15 +40,13 @@ export function Editor({ darkMode }: EditorProps) {
useEffect(() => {
if (!activeTab) return
setContent(activeTab.content)
// 恢复滚动和选区
requestAnimationFrame(() => {
setScrollTop(activeTab.scrollTop)
setSelection(activeTab.selectionStart, activeTab.selectionEnd)
})
}, [activeTabId])
// 保存当前标签状态(切换前)
// 保存当前标签状态
useEffect(() => {
return () => {
if (!activeTabId) return
@@ -63,12 +58,10 @@ export function Editor({ darkMode }: EditorProps) {
}
}, [activeTabId])
// 搜索:Ctrl+F 聚焦搜索框,Enter 导航
// Ctrl+B 粗体, Ctrl+I 斜体
useEffect(() => {
const handleKeyDown = (e: KeyboardEvent) => {
const isCtrl = e.ctrlKey || e.metaKey
// Ctrl+B 粗体
if (isCtrl && e.key === 'b') {
e.preventDefault()
const view = viewRef.current
@@ -80,8 +73,6 @@ export function Editor({ darkMode }: EditorProps) {
selection: { anchor: from + 2, head: from + 2 + selected.length }
})
}
// Ctrl+I 斜体
if (isCtrl && e.key === 'i') {
e.preventDefault()
const view = viewRef.current
@@ -94,7 +85,6 @@ export function Editor({ darkMode }: EditorProps) {
})
}
}
document.addEventListener('keydown', handleKeyDown)
return () => document.removeEventListener('keydown', handleKeyDown)
}, [viewRef])
@@ -1,147 +0,0 @@
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>
)
}