refactor: v2.0 全量重构 — TypeScript + React + Zustand + IndexedDB

技术栈升级:
- JavaScript → TypeScript 5.6(全量类型安全)
- 原生 DOM → React 18(函数组件 + Hooks)
- 全局变量 → Zustand 5(轻量状态管理)
- localStorage → IndexedDB / Dexie.js 4(大容量、异步、索引)
- marked.js → unified / remark / rehype(插件化渲染管线)
- 无打包 → electron-vite 3(HMR 热更新)
- 纯 CSS → CSS Variables + CSS Modules

新增功能:
- 标签页状态 IndexedDB 持久化(关闭后可恢复)
- 最近打开文件列表
- 大文件虚拟化行号(>2000 行)
- 搜索高亮二分查找优化 O(log N)
- rehype-sanitize HTML 安全过滤

文件结构:
- 7 个源文件 → 51 个模块化文件
- src/main/     主进程(6 文件)
- src/preload/  预加载(1 文件)
- src/renderer/ 渲染进程(42 文件:组件/hooks/stores/lib/db/types/styles)
- src/shared/   共享类型(2 文件)

构建验证:
- TypeScript 检查零错误
- electron-vite build 成功
- 产物:main 15kB + preload 2kB + renderer 1.5MB
This commit is contained in:
thzxx
2026-05-27 20:29:23 +08:00
parent c2cdba546b
commit 5e1c89d280
59 changed files with 4674 additions and 314 deletions
+792
View File
@@ -0,0 +1,792 @@
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html, body {
height: 100%;
font-family: var(--font-ui);
font-size: 14px;
color: var(--text);
background: var(--bg);
overflow: hidden;
user-select: none;
}
#app {
display: flex;
flex-direction: column;
height: 100vh;
}
/* Toolbar */
#toolbar {
height: var(--toolbar-height);
background: var(--bg);
border-bottom: 1px solid var(--border);
display: flex;
align-items: center;
justify-content: space-between;
padding: 0 8px;
flex-shrink: 0;
z-index: 10;
}
.toolbar-left, .toolbar-right {
display: flex;
align-items: center;
gap: 2px;
}
.toolbar-btn {
display: flex;
align-items: center;
gap: 5px;
padding: 6px 12px;
border: none;
background: transparent;
color: var(--text-secondary);
font-size: 13px;
font-family: var(--font-ui);
border-radius: var(--radius);
cursor: pointer;
transition: all 0.15s ease;
white-space: nowrap;
}
.toolbar-btn:hover {
background: var(--bg-tertiary);
color: var(--text);
}
.toolbar-btn.active {
background: var(--primary-light);
color: var(--primary);
}
.toolbar-btn svg { flex-shrink: 0; }
.toolbar-divider {
width: 1px;
height: 24px;
background: var(--border);
margin: 0 6px;
}
/* Workspace */
#workspace {
flex: 1;
display: flex;
flex-direction: row;
overflow: hidden;
min-height: 0;
}
#main-content {
flex: 1;
display: flex;
flex-direction: column;
overflow: hidden;
position: relative;
min-width: 0;
}
#content-wrapper {
flex: 1;
display: flex;
flex-direction: row;
overflow: hidden;
min-height: 0;
}
/* Editor Panel */
#editor-panel {
flex: 1;
display: flex;
flex-direction: column;
min-width: 0;
border-right: 1px solid var(--border);
}
#editor-wrapper {
flex: 1;
display: flex;
overflow: hidden;
position: relative;
}
#line-numbers {
width: 50px;
background: var(--bg-secondary);
border-right: 1px solid var(--border-light);
padding: 12px 0;
text-align: right;
font-family: var(--font-mono);
font-size: 13px;
line-height: 1.6;
color: var(--text-tertiary);
overflow: hidden;
user-select: none;
flex-shrink: 0;
}
#line-numbers .line-num {
padding-right: 12px;
height: 20.8px;
}
#editor {
flex: 1;
width: 100%;
padding: 12px 16px;
border: none;
outline: none;
resize: none;
font-family: var(--font-mono);
font-size: 13px;
line-height: 1.6;
color: var(--text);
background: var(--bg);
tab-size: 4;
overflow-y: auto;
user-select: text;
}
#editor::placeholder { color: var(--text-tertiary); }
/* Resizer */
#resizer {
width: 4px;
background: var(--border);
cursor: col-resize;
transition: background 0.15s ease;
flex-shrink: 0;
}
#resizer:hover, #resizer.active {
background: var(--primary);
}
/* Preview Panel */
#preview-panel {
flex: 1;
overflow-y: auto;
min-width: 0;
background: var(--bg);
}
#preview {
padding: 24px 32px;
max-width: 900px;
margin: 0 auto;
user-select: text;
cursor: text;
}
/* Tab Bar */
#tab-bar {
height: 36px;
background: var(--bg-secondary);
border-bottom: 1px solid var(--border);
display: flex;
align-items: flex-end;
padding: 0 4px;
flex-shrink: 0;
overflow-x: auto;
overflow-y: hidden;
}
#tab-bar::-webkit-scrollbar { height: 0; }
#tab-list {
display: flex;
align-items: flex-end;
gap: 1px;
flex: 1;
min-width: 0;
overflow-x: auto;
overflow-y: hidden;
}
#tab-list::-webkit-scrollbar { height: 0; }
.tab-item {
display: flex;
align-items: center;
gap: 6px;
padding: 6px 12px;
background: transparent;
border: none;
border-bottom: 2px solid transparent;
color: var(--text-secondary);
font-size: 12px;
font-family: var(--font-ui);
cursor: pointer;
white-space: nowrap;
max-width: 180px;
min-width: 60px;
flex-shrink: 0;
transition: all 0.15s ease;
position: relative;
}
.tab-item:hover {
color: var(--text);
background: var(--bg-tertiary);
}
.tab-item.active {
color: var(--text);
background: var(--bg);
border-bottom-color: var(--primary);
}
.tab-item.modified .tab-name::after {
content: ' •';
color: var(--primary);
}
.tab-name {
overflow: hidden;
text-overflow: ellipsis;
pointer-events: none;
}
.tab-close {
display: flex;
align-items: center;
justify-content: center;
width: 16px;
height: 16px;
border: none;
background: transparent;
color: var(--text-tertiary);
border-radius: 3px;
cursor: pointer;
flex-shrink: 0;
padding: 0;
opacity: 0;
transition: all 0.1s ease;
}
.tab-item:hover .tab-close,
.tab-item.active .tab-close { opacity: 1; }
.tab-close:hover {
background: var(--border);
color: var(--text);
}
.tab-add-btn {
display: flex;
align-items: center;
justify-content: center;
width: 28px;
height: 28px;
border: none;
background: transparent;
color: var(--text-secondary);
border-radius: var(--radius);
cursor: pointer;
margin-left: 2px;
margin-bottom: 4px;
flex-shrink: 0;
transition: all 0.15s ease;
}
.tab-add-btn:hover {
background: var(--bg-tertiary);
color: var(--text);
}
/* Modified Banner */
#modified-banner {
display: flex;
align-items: center;
gap: 12px;
padding: 6px 12px;
background: #fff3cd;
border-bottom: 1px solid #ffc107;
font-size: 13px;
color: #856404;
flex-shrink: 0;
}
:root.dark #modified-banner {
background: #3a3000;
border-bottom-color: #665500;
color: #ffd54f;
}
.banner-btn {
padding: 2px 10px;
border: 1px solid #ffc107;
border-radius: 4px;
background: transparent;
color: #856404;
font-size: 12px;
cursor: pointer;
font-family: var(--font-ui);
}
:root.dark .banner-btn {
border-color: #665500;
color: #ffd54f;
}
.banner-btn:hover {
background: #ffc107;
color: #000;
}
/* Status Bar */
#statusbar {
height: var(--statusbar-height);
background: var(--bg-secondary);
border-top: 1px solid var(--border);
display: flex;
align-items: center;
justify-content: space-between;
padding: 0 12px;
font-size: 12px;
color: var(--text-secondary);
flex-shrink: 0;
}
.status-left, .status-right {
display: flex;
align-items: center;
gap: 8px;
}
.status-divider { color: var(--border); }
/* Drop Overlay */
#drop-overlay {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: rgba(26, 115, 232, 0.1);
backdrop-filter: blur(4px);
display: flex;
align-items: center;
justify-content: center;
z-index: 1000;
border: 3px dashed var(--primary);
margin: 8px;
border-radius: 12px;
}
.drop-content {
text-align: center;
color: var(--primary);
}
.drop-content svg {
margin-bottom: 12px;
opacity: 0.7;
}
.drop-content p {
font-size: 18px;
font-weight: 500;
}
/* Welcome Screen */
#welcome-screen {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
display: flex;
align-items: center;
justify-content: center;
background: var(--bg);
z-index: 5;
}
.welcome-content {
text-align: center;
max-width: 480px;
padding: 40px;
animation: fadeIn 0.3s ease;
}
.welcome-icon { margin-bottom: 24px; }
.welcome-content h1 {
font-size: 28px;
font-weight: 600;
color: var(--text);
margin-bottom: 8px;
}
.welcome-content > p {
font-size: 16px;
color: var(--text-secondary);
margin-bottom: 32px;
}
.welcome-actions {
display: flex;
gap: 12px;
justify-content: center;
margin-bottom: 32px;
}
.welcome-btn {
display: flex;
align-items: center;
gap: 8px;
padding: 10px 24px;
border: none;
border-radius: var(--radius);
font-size: 14px;
font-family: var(--font-ui);
font-weight: 500;
cursor: pointer;
transition: all 0.15s ease;
}
.welcome-btn.primary {
background: var(--primary);
color: white;
}
.welcome-btn.primary:hover {
background: var(--primary-dark);
box-shadow: var(--shadow-lg);
}
.welcome-btn.secondary {
background: var(--bg-secondary);
color: var(--text);
border: 1px solid var(--border);
}
.welcome-btn.secondary:hover { background: var(--bg-tertiary); }
.welcome-tips {
text-align: left;
background: var(--bg-secondary);
border-radius: var(--radius);
padding: 16px 20px;
border: 1px solid var(--border-light);
}
.welcome-tips p {
font-size: 13px;
color: var(--text-secondary);
margin-bottom: 6px;
}
.welcome-tips p:last-child { margin-bottom: 0; }
/* Sidebar */
#sidebar {
width: var(--sidebar-width);
min-width: 180px;
max-width: 500px;
background: var(--sidebar-bg);
border-right: 1px solid var(--sidebar-border);
display: flex;
flex-direction: column;
flex-shrink: 0;
overflow: hidden;
user-select: none;
position: relative;
}
#sidebar.collapsed { display: none; }
#sidebar-header {
display: flex;
align-items: center;
justify-content: space-between;
padding: 8px 12px;
border-bottom: 1px solid var(--sidebar-border);
flex-shrink: 0;
}
#sidebar-title {
font-size: 12px;
font-weight: 600;
color: var(--text-secondary);
text-transform: uppercase;
letter-spacing: 0.5px;
}
.sidebar-header-btn {
display: flex;
align-items: center;
justify-content: center;
width: 24px;
height: 24px;
border: none;
background: transparent;
color: var(--text-secondary);
border-radius: 4px;
cursor: pointer;
transition: all 0.15s ease;
}
.sidebar-header-btn:hover {
background: var(--sidebar-hover);
color: var(--text);
}
#sidebar-tree {
flex: 1;
overflow-y: auto;
overflow-x: hidden;
padding: 4px 0;
}
.tree-item {
display: flex;
align-items: center;
padding: 3px 8px 3px 0;
cursor: pointer;
font-size: 13px;
color: var(--text);
white-space: nowrap;
transition: background 0.1s ease;
border-radius: 0;
}
.tree-item:hover { background: var(--sidebar-hover); }
.tree-item.active {
background: var(--sidebar-active);
color: var(--primary);
font-weight: 500;
}
.tree-indent { flex-shrink: 0; }
.tree-arrow {
display: flex;
align-items: center;
justify-content: center;
width: 16px;
height: 16px;
flex-shrink: 0;
color: var(--text-tertiary);
transition: transform 0.15s ease;
}
.tree-arrow.expanded { transform: rotate(90deg); }
.tree-icon {
display: flex;
align-items: center;
justify-content: center;
width: 16px;
height: 16px;
flex-shrink: 0;
margin-right: 4px;
}
.tree-icon svg { width: 14px; height: 14px; }
.tree-name {
overflow: hidden;
text-overflow: ellipsis;
}
/* Independent Files Section */
.independent-files-section {
border-bottom: 1px solid var(--border);
margin-bottom: 4px;
padding-bottom: 4px;
}
.sidebar-section-header, .independent-files-header {
font-size: 11px;
font-weight: 600;
color: var(--text-tertiary);
text-transform: uppercase;
letter-spacing: 0.5px;
padding: 8px 12px 4px 12px;
}
.independent-file-item .independent-modified-dot {
color: var(--primary);
font-size: 14px;
margin-left: 4px;
}
.sidebar-resize-handle {
position: absolute;
top: 0;
right: -2px;
width: 4px;
height: 100%;
cursor: col-resize;
z-index: 5;
}
.sidebar-resize-handle:hover,
.sidebar-resize-handle:active {
background: var(--primary);
}
/* Search & Replace Bar */
#btn-toggle-replace {
font-size: 10px;
transition: transform 0.15s ease;
}
#btn-toggle-replace.expanded { transform: rotate(90deg); }
#search-bar {
background: var(--search-bg);
border-bottom: 1px solid var(--search-border);
padding: 6px 10px;
flex-shrink: 0;
}
.search-row, #replace-row {
display: flex;
align-items: center;
gap: 4px;
}
#replace-row { margin-top: 4px; }
#search-input, #replace-input {
flex: 1;
min-width: 0;
height: 26px;
padding: 0 8px;
border: 1px solid var(--border);
border-radius: 4px;
background: var(--bg);
color: var(--text);
font-size: 13px;
font-family: var(--font-ui);
outline: none;
transition: border-color 0.15s ease;
}
#search-input:focus, #replace-input:focus { border-color: var(--primary); }
#search-count {
font-size: 11px;
color: var(--text-tertiary);
white-space: nowrap;
min-width: 40px;
text-align: center;
}
.search-opt-btn {
display: flex;
align-items: center;
justify-content: center;
min-width: 26px;
height: 26px;
padding: 0 4px;
border: 1px solid var(--border);
border-radius: 4px;
background: transparent;
color: var(--text-secondary);
font-size: 11px;
font-weight: 600;
font-family: var(--font-mono);
cursor: pointer;
transition: all 0.15s ease;
}
.search-opt-btn:hover { background: var(--bg-tertiary); }
.search-opt-btn.active {
background: var(--primary-light);
color: var(--primary);
border-color: var(--primary);
}
.search-nav-btn {
display: flex;
align-items: center;
justify-content: center;
width: 26px;
height: 26px;
border: none;
background: transparent;
color: var(--text-secondary);
border-radius: 4px;
cursor: pointer;
font-size: 14px;
transition: all 0.15s ease;
}
.search-nav-btn:hover {
background: var(--bg-tertiary);
color: var(--text);
}
.replace-btn {
height: 26px;
padding: 0 10px;
border: 1px solid var(--border);
border-radius: 4px;
background: transparent;
color: var(--text-secondary);
font-size: 12px;
font-family: var(--font-ui);
cursor: pointer;
white-space: nowrap;
transition: all 0.15s ease;
}
.replace-btn:hover {
background: var(--bg-tertiary);
color: var(--text);
}
/* View Modes */
#app.mode-preview #editor-panel,
#app.mode-preview #resizer { display: none; }
/* Toast */
#toast-notification {
position: fixed;
bottom: 40px;
left: 50%;
transform: translateX(-50%) translateY(20px);
background: var(--text);
color: var(--bg);
padding: 8px 20px;
border-radius: 6px;
font-size: 13px;
font-family: var(--font-ui);
box-shadow: var(--shadow-lg);
opacity: 0;
pointer-events: none;
transition: opacity 0.3s ease, transform 0.3s ease;
z-index: 9999;
max-width: 480px;
text-align: center;
}
#toast-notification.show {
opacity: 1;
transform: translateX(-50%) translateY(0);
pointer-events: auto;
}
/* Scrollbar */
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-tertiary); }
/* Animations */
@keyframes fadeIn {
from { opacity: 0; transform: translateY(8px); }
to { opacity: 1; transform: translateY(0); }
}