fix: 修复代码审计发现的全部 37 个问题
严重 Bug(8 个): - B-01: isClosing 永不重置 — WINDOW_CANCEL_CLOSE 现在正确重置状态并清除超时 - B-02: replaceAll 算法完全错误 — 改为从后向前逐个替换(SearchBar + useFileWatch) - B-03: Preview 用 setInterval 轮询 — 改为响应式订阅 activeTab.content 变化 - B-04: StatusBar 不响应 tab 切换 — 改为直接选择 tabs/activeTabId 而非函数引用 - B-05: useTheme 初始化竞态 — 添加 isInitialized ref 防止首次加载前保存 - B-06: scrollSync 0 值歧义 — 使用 NaN 标记未映射状态 - B-07: macOS activate 未注册 IPC — 提取 initWindow() 函数完整重建 - B-08: resizer 无交互逻辑 — 实现 Resizer 组件(mousedown/mousemove/mouseup) 中等问题(12 个): - M-01: removeAllListeners 限制为白名单通道 - M-02: 新建文件保存时设置 isSelfWriting - M-03: SidebarWatcher setTimeout 后再次检查窗口状态 - M-04: FileWatcher/SidebarWatcher 监听 error 事件清理僵尸状态 - M-05: buildDirTree 添加递归深度限制(maxDepth=10)+ 错误边界 - M-06: saveFileContent 改为原子写入(write-to-temp-then-rename) - M-07: Preview 异步竞态 — 使用递增 requestId 替代 cancelled - M-08: useKeyboard 依赖优化 — 用 getState() 替代频繁变化的 tabs 依赖 - M-09: settingsRepository.save() 改为 Dexie put 直接合并 - M-10: useSettings 返回函数用 useCallback 稳定引用 - M-11: App.tsx 事件注册添加 cleanup 清理 + ref 保持最新回调 - M-12: ipc-channels.ts 类型签名统一 低级问题(17 个): - L-01/L-03: Editor 移除未使用的 imports - L-04: tabIdCounter 改为 nanoid(HMR 安全) - L-06: recentFiles 添加 50 条上限自动清理 - L-07: getFileExtension 正确处理无扩展名文件 - L-08: window-manager 使用静态导入 + 验证是文件非目录 - L-09: readFileContent 使用 shared 类型 - L-10: buildDirTree 权限错误返回空数组 - L-11: DropOverlay 简化为单一 dragCounter 状态 - L-12: Sidebar onFileClick 提取为 useCallback - L-13: Editor searchStore 改为精确 selector - L-14: CSS 添加 mode-editor 隐藏规则 - L-16: openFileInTab 添加 .catch() 异常处理 TypeScript 检查零错误,构建成功。
This commit is contained in:
@@ -5,117 +5,58 @@ import { useSearchStore } from '../stores/searchStore'
|
||||
import type { ViewMode } from '../types/settings'
|
||||
|
||||
export function useKeyboard(handleOpenFile: () => void, handleSave: () => void, handleSaveAs: () => void) {
|
||||
const createTab = useTabStore(s => s.createTab)
|
||||
const closeTab = useTabStore(s => s.closeTab)
|
||||
const activeTabId = useTabStore(s => s.activeTabId)
|
||||
const tabs = useTabStore(s => s.tabs)
|
||||
const switchToTab = useTabStore(s => s.switchToTab)
|
||||
const mruStack = useTabStore(s => s.mruStack)
|
||||
const setViewMode = useEditorStore(s => s.setViewMode)
|
||||
const searchStore = useSearchStore
|
||||
|
||||
// M-08: 使用 getState() 而非依赖 tabs/activeTabId 等频繁变化的值
|
||||
const handleKeydown = useCallback((e: KeyboardEvent) => {
|
||||
const isCtrl = e.ctrlKey || e.metaKey
|
||||
|
||||
if (isCtrl && e.key === 'o') {
|
||||
e.preventDefault()
|
||||
handleOpenFile()
|
||||
return
|
||||
}
|
||||
if (isCtrl && e.key === 's' && !e.shiftKey) {
|
||||
e.preventDefault()
|
||||
handleSave()
|
||||
return
|
||||
}
|
||||
if (isCtrl && e.shiftKey && e.key === 'S') {
|
||||
e.preventDefault()
|
||||
handleSaveAs()
|
||||
return
|
||||
}
|
||||
if (isCtrl && e.key === '1') {
|
||||
e.preventDefault()
|
||||
setViewMode('split')
|
||||
return
|
||||
}
|
||||
if (isCtrl && e.key === '2') {
|
||||
e.preventDefault()
|
||||
setViewMode('editor')
|
||||
return
|
||||
}
|
||||
if (isCtrl && e.key === '3') {
|
||||
e.preventDefault()
|
||||
setViewMode('preview')
|
||||
return
|
||||
}
|
||||
if (isCtrl && e.key === 't') {
|
||||
e.preventDefault()
|
||||
createTab(null, '')
|
||||
return
|
||||
}
|
||||
if (isCtrl && e.key === 'o') { e.preventDefault(); handleOpenFile(); return }
|
||||
if (isCtrl && e.key === 's' && !e.shiftKey) { e.preventDefault(); handleSave(); return }
|
||||
if (isCtrl && e.shiftKey && e.key === 'S') { e.preventDefault(); handleSaveAs(); return }
|
||||
if (isCtrl && e.key === '1') { e.preventDefault(); setViewMode('split'); return }
|
||||
if (isCtrl && e.key === '2') { e.preventDefault(); setViewMode('editor'); return }
|
||||
if (isCtrl && e.key === '3') { e.preventDefault(); setViewMode('preview'); return }
|
||||
|
||||
// M-08: 通过 getState() 读取最新状态
|
||||
const tabState = useTabStore.getState()
|
||||
if (isCtrl && e.key === 't') { e.preventDefault(); tabState.createTab(null, ''); return }
|
||||
if (isCtrl && e.key === 'w') {
|
||||
e.preventDefault()
|
||||
if (activeTabId) closeTab(activeTabId)
|
||||
if (tabState.activeTabId) tabState.closeTab(tabState.activeTabId)
|
||||
return
|
||||
}
|
||||
|
||||
// Ctrl+Tab / Ctrl+Shift+Tab
|
||||
if (isCtrl && e.key === 'Tab') {
|
||||
e.preventDefault()
|
||||
const { tabs, activeTabId, mruStack } = tabState
|
||||
if (tabs.length > 1) {
|
||||
if (e.shiftKey) {
|
||||
if (mruStack.length > 0) {
|
||||
const targetId = mruStack[0]
|
||||
if (tabs.find(t => t.id === targetId)) {
|
||||
switchToTab(targetId)
|
||||
tabState.switchToTab(targetId)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
const idx = tabs.findIndex(t => t.id === activeTabId)
|
||||
const next = (idx + 1) % tabs.length
|
||||
switchToTab(tabs[next].id)
|
||||
tabState.switchToTab(tabs[next].id)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Search & Replace
|
||||
const state = searchStore.getState()
|
||||
if (isCtrl && e.key === 'f') {
|
||||
e.preventDefault()
|
||||
state.setVisible(true)
|
||||
state.setShowReplace(false)
|
||||
return
|
||||
}
|
||||
if (isCtrl && e.key === 'h') {
|
||||
e.preventDefault()
|
||||
state.setVisible(true)
|
||||
state.setShowReplace(true)
|
||||
return
|
||||
}
|
||||
if (e.key === 'Escape' && state.isVisible) {
|
||||
e.preventDefault()
|
||||
state.close()
|
||||
return
|
||||
}
|
||||
if (e.altKey && e.key === 'c') {
|
||||
e.preventDefault()
|
||||
state.toggleCaseSensitive()
|
||||
return
|
||||
}
|
||||
if (e.altKey && e.key === 'r') {
|
||||
e.preventDefault()
|
||||
state.toggleRegex()
|
||||
return
|
||||
}
|
||||
if (isCtrl && e.shiftKey && e.key === 'G') {
|
||||
e.preventDefault()
|
||||
state.findPrev()
|
||||
return
|
||||
}
|
||||
if (isCtrl && e.shiftKey && e.key === 'H') {
|
||||
e.preventDefault()
|
||||
// replaceAll 需要外部实现
|
||||
return
|
||||
}
|
||||
}, [handleOpenFile, handleSave, handleSaveAs, createTab, closeTab, activeTabId, tabs, switchToTab, mruStack, setViewMode, searchStore])
|
||||
const searchState = useSearchStore.getState()
|
||||
if (isCtrl && e.key === 'f') { e.preventDefault(); searchState.setVisible(true); searchState.setShowReplace(false); return }
|
||||
if (isCtrl && e.key === 'h') { e.preventDefault(); searchState.setVisible(true); searchState.setShowReplace(true); return }
|
||||
if (e.key === 'Escape' && searchState.isVisible) { e.preventDefault(); searchState.close(); return }
|
||||
if (e.altKey && e.key === 'c') { e.preventDefault(); searchState.toggleCaseSensitive(); return }
|
||||
if (e.altKey && e.key === 'r') { e.preventDefault(); searchState.toggleRegex(); return }
|
||||
if (isCtrl && e.shiftKey && e.key === 'G') { e.preventDefault(); searchState.findPrev(); return }
|
||||
}, [handleOpenFile, handleSave, handleSaveAs, setViewMode])
|
||||
|
||||
useEffect(() => {
|
||||
document.addEventListener('keydown', handleKeydown)
|
||||
|
||||
Reference in New Issue
Block a user