严重 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 检查零错误,构建成功。
261 lines
8.6 KiB
TypeScript
261 lines
8.6 KiB
TypeScript
import React, { useState, useCallback, useEffect, useRef } from 'react'
|
|
import { useTabStore } from './stores/tabStore'
|
|
import { useEditorStore } from './stores/editorStore'
|
|
import { useSearchStore } from './stores/searchStore'
|
|
import { useTheme } from './hooks/useTheme'
|
|
import { useSettings } from './hooks/useSettings'
|
|
import { useKeyboard } from './hooks/useKeyboard'
|
|
import { useUnsavedWarning } from './hooks/useUnsavedWarning'
|
|
import { useFileWatch } from './hooks/useFileWatch'
|
|
import { Toolbar } from './components/Toolbar/Toolbar'
|
|
import { TabBar } from './components/TabBar/TabBar'
|
|
import { Editor } from './components/Editor/Editor'
|
|
import { Preview } from './components/Preview/Preview'
|
|
import { Sidebar } from './components/Sidebar/Sidebar'
|
|
import { SearchBar } from './components/SearchBar/SearchBar'
|
|
import { StatusBar } from './components/StatusBar/StatusBar'
|
|
import { WelcomeScreen } from './components/WelcomeScreen/WelcomeScreen'
|
|
import { Toast } from './components/Toast/Toast'
|
|
import { ModifiedBanner } from './components/ModifiedBanner/ModifiedBanner'
|
|
import { DropOverlay } from './components/DropOverlay/DropOverlay'
|
|
import { useDragDrop } from './hooks/useDragDrop'
|
|
|
|
export default function App() {
|
|
const tabs = useTabStore(s => s.tabs)
|
|
const activeTabId = useTabStore(s => s.activeTabId)
|
|
const getActiveTab = useTabStore(s => s.getActiveTab)
|
|
const createTab = useTabStore(s => s.createTab)
|
|
const setModified = useTabStore(s => s.setModified)
|
|
const updateTabContent = useTabStore(s => s.updateTabContent)
|
|
|
|
const viewMode = useEditorStore(s => s.viewMode)
|
|
const splitRatio = useEditorStore(s => s.splitRatio)
|
|
|
|
const { darkMode, toggleDarkMode } = useTheme()
|
|
const { saveViewMode, saveSplitRatio } = useSettings()
|
|
|
|
const [showModifiedBanner, setShowModifiedBanner] = useState(false)
|
|
const [modifiedFilePath, setModifiedFilePath] = useState<string | null>(null)
|
|
const [toastMessage, setToastMessage] = useState<string | null>(null)
|
|
|
|
const showToast = useCallback((msg: string) => {
|
|
setToastMessage(msg)
|
|
setTimeout(() => setToastMessage(null), 3000)
|
|
}, [])
|
|
|
|
// 拖拽打开
|
|
useDragDrop(showToast)
|
|
|
|
// 文件修改检测
|
|
useFileWatch()
|
|
|
|
// 未保存提醒
|
|
useUnsavedWarning(() => tabs.some(t => t.isModified))
|
|
|
|
// 文件外部修改事件
|
|
useEffect(() => {
|
|
const handler = (e: Event) => {
|
|
const filePath = (e as CustomEvent).detail
|
|
const tab = getActiveTab()
|
|
if (tab && tab.filePath === filePath) {
|
|
setShowModifiedBanner(true)
|
|
setModifiedFilePath(filePath)
|
|
}
|
|
}
|
|
window.addEventListener('file-externally-modified', handler)
|
|
return () => window.removeEventListener('file-externally-modified', handler)
|
|
}, [getActiveTab])
|
|
|
|
// 打开文件
|
|
const handleOpenFile = useCallback(async () => {
|
|
if (window.electronAPI) {
|
|
const result = await window.electronAPI.openFile()
|
|
if (result && 'filePath' in result) {
|
|
createTab(result.filePath, result.content)
|
|
}
|
|
}
|
|
}, [createTab])
|
|
|
|
// 保存文件
|
|
const handleSave = useCallback(async () => {
|
|
const tab = getActiveTab()
|
|
if (!tab) return
|
|
if (window.electronAPI) {
|
|
const result = await window.electronAPI.saveFile({
|
|
filePath: tab.filePath,
|
|
content: tab.content
|
|
})
|
|
if (result.success) {
|
|
useTabStore.getState().setModified(tab.id, false)
|
|
showToast('已保存')
|
|
}
|
|
}
|
|
}, [getActiveTab, showToast])
|
|
|
|
// 另存为
|
|
const handleSaveAs = useCallback(async () => {
|
|
const tab = getActiveTab()
|
|
if (!tab) return
|
|
if (window.electronAPI) {
|
|
const result = await window.electronAPI.saveFileAs({ content: tab.content })
|
|
if (result.success) {
|
|
useTabStore.getState().setModified(tab.id, false)
|
|
showToast('已保存')
|
|
}
|
|
}
|
|
}, [getActiveTab, showToast])
|
|
|
|
// 快捷键
|
|
useKeyboard(handleOpenFile, handleSave, handleSaveAs)
|
|
|
|
// 视图模式切换
|
|
const handleViewModeChange = useCallback((mode: 'split' | 'editor' | 'preview') => {
|
|
saveViewMode(mode)
|
|
}, [saveViewMode])
|
|
|
|
// M-11: 主进程事件注册(使用 ref 保持最新回调引用 + cleanup 清理)
|
|
const handleSaveRef = useRef(handleSave)
|
|
const handleSaveAsRef = useRef(handleSaveAs)
|
|
handleSaveRef.current = handleSave
|
|
handleSaveAsRef.current = handleSaveAs
|
|
|
|
useEffect(() => {
|
|
if (!window.electronAPI) return
|
|
const api = window.electronAPI
|
|
|
|
const onOpen = (data: { filePath: string; content: string }) => {
|
|
createTab(data.filePath, data.content)
|
|
}
|
|
const onSave = () => handleSaveRef.current()
|
|
const onSaveAs = () => handleSaveAsRef.current()
|
|
|
|
api.onFileOpenInTab(onOpen)
|
|
api.onMenuSave(onSave)
|
|
api.onMenuSaveAs(onSaveAs)
|
|
|
|
return () => {
|
|
api.removeAllListeners('file:openInTab')
|
|
api.removeAllListeners('menu:save')
|
|
api.removeAllListeners('menu:saveAs')
|
|
}
|
|
}, [createTab])
|
|
|
|
const activeTab = getActiveTab()
|
|
const hasTabs = tabs.length > 0
|
|
|
|
// 分屏样式
|
|
const editorStyle: React.CSSProperties = viewMode === 'split'
|
|
? { flex: `0 0 ${splitRatio}%` }
|
|
: {}
|
|
const previewStyle: React.CSSProperties = viewMode === 'split'
|
|
? { flex: `0 0 ${100 - splitRatio}%` }
|
|
: {}
|
|
|
|
return (
|
|
<div id="app" className={`mode-${viewMode}`}>
|
|
<Toolbar
|
|
onOpen={handleOpenFile}
|
|
onSave={handleSave}
|
|
viewMode={viewMode}
|
|
onViewModeChange={handleViewModeChange}
|
|
darkMode={darkMode}
|
|
onToggleDark={toggleDarkMode}
|
|
/>
|
|
|
|
<div id="workspace">
|
|
<Sidebar />
|
|
|
|
<div id="main-content">
|
|
<TabBar />
|
|
|
|
{showModifiedBanner && (
|
|
<ModifiedBanner
|
|
onReload={() => {
|
|
if (window.electronAPI && modifiedFilePath) {
|
|
window.electronAPI.reloadFile().then(result => {
|
|
if (result.success && result.content) {
|
|
const tab = getActiveTab()
|
|
if (tab) {
|
|
updateTabContent(tab.id, result.content)
|
|
setModified(tab.id, false)
|
|
}
|
|
}
|
|
setShowModifiedBanner(false)
|
|
})
|
|
}
|
|
}}
|
|
onDismiss={() => setShowModifiedBanner(false)}
|
|
/>
|
|
)}
|
|
|
|
{hasTabs ? (
|
|
<div id="content-wrapper">
|
|
{viewMode !== 'preview' && (
|
|
<div id="editor-panel" style={editorStyle}>
|
|
<SearchBar />
|
|
<Editor />
|
|
</div>
|
|
)}
|
|
{viewMode !== 'editor' && <Resizer onResize={saveSplitRatio} />}
|
|
{viewMode !== 'editor' && (
|
|
<div id="preview-panel" style={previewStyle}>
|
|
<Preview />
|
|
</div>
|
|
)}
|
|
</div>
|
|
) : (
|
|
<WelcomeScreen
|
|
onOpen={handleOpenFile}
|
|
onNew={() => createTab(null, '')}
|
|
/>
|
|
)}
|
|
</div>
|
|
</div>
|
|
|
|
<StatusBar />
|
|
|
|
{toastMessage && <Toast message={toastMessage} />}
|
|
<DropOverlay />
|
|
</div>
|
|
)
|
|
}
|
|
|
|
// B-08: 分屏调节器组件
|
|
function Resizer({ onResize }: { onResize: (ratio: number) => void }) {
|
|
const handleMouseDown = useCallback((e: React.MouseEvent) => {
|
|
e.preventDefault()
|
|
document.body.style.cursor = 'col-resize'
|
|
document.body.style.userSelect = 'none'
|
|
|
|
const handleMouseMove = (ev: MouseEvent) => {
|
|
const wrapper = document.getElementById('content-wrapper')
|
|
if (!wrapper) return
|
|
const rect = wrapper.getBoundingClientRect()
|
|
const pct = Math.max(20, Math.min(80, ((ev.clientX - rect.left) / rect.width) * 100))
|
|
const editorPanel = document.getElementById('editor-panel')
|
|
const previewPanel = document.getElementById('preview-panel')
|
|
if (editorPanel) editorPanel.style.flex = `0 0 ${pct}%`
|
|
if (previewPanel) previewPanel.style.flex = `0 0 ${100 - pct}%`
|
|
}
|
|
|
|
const handleMouseUp = (ev: MouseEvent) => {
|
|
document.removeEventListener('mousemove', handleMouseMove)
|
|
document.removeEventListener('mouseup', handleMouseUp)
|
|
document.body.style.cursor = ''
|
|
document.body.style.userSelect = ''
|
|
// 保存最终比例
|
|
const wrapper = document.getElementById('content-wrapper')
|
|
if (wrapper) {
|
|
const rect = wrapper.getBoundingClientRect()
|
|
const pct = Math.max(20, Math.min(80, ((ev.clientX - rect.left) / rect.width) * 100))
|
|
onResize(pct)
|
|
}
|
|
}
|
|
|
|
document.addEventListener('mousemove', handleMouseMove)
|
|
document.addEventListener('mouseup', handleMouseUp)
|
|
}, [onResize])
|
|
|
|
return <div id="resizer" onMouseDown={handleMouseDown} />
|
|
}
|