refactor: main code

This commit is contained in:
kangfenmao
2024-11-02 23:29:16 +08:00
parent 2f05435ce6
commit bddedb7759
13 changed files with 81 additions and 63 deletions
+19
View File
@@ -0,0 +1,19 @@
import Store from 'electron-store'
export class ConfigManager {
private store: Store
constructor() {
this.store = new Store()
}
getTheme(): 'light' | 'dark' {
return this.store.get('theme', 'light') as 'light' | 'dark'
}
setTheme(theme: 'light' | 'dark') {
this.store.set('theme', theme)
}
}
export const configManager = new ConfigManager()
+3 -3
View File
@@ -6,13 +6,13 @@ import { dialog } from 'electron'
import Logger from 'electron-log'
import MarkdownIt from 'markdown-it'
import FileManager from './FileManager'
import FileStorage from './FileStorage'
export class ExportService {
private fileManager: FileManager
private fileManager: FileStorage
private md: MarkdownIt
constructor(fileManager: FileManager) {
constructor(fileManager: FileStorage) {
this.fileManager = fileManager
this.md = new MarkdownIt()
}
@@ -20,7 +20,7 @@ import { chdir } from 'process'
import sharp from 'sharp'
import { v4 as uuidv4 } from 'uuid'
class FileManager {
class FileStorage {
private storageDir = path.join(app.getPath('userData'), 'Data', 'Files')
private tempDir = path.join(app.getPath('temp'), 'CherryStudio')
@@ -135,13 +135,13 @@ class FileManager {
.jpeg({ quality: 80 })
.toFile(destPath)
logger.info('[FileManager] Image compressed successfully:', sourcePath)
logger.info('[FileStorage] Image compressed successfully:', sourcePath)
} else {
// 小图片直接复制
await fs.promises.copyFile(sourcePath, destPath)
}
} catch (error) {
logger.error('[FileManager] Image compression failed:', error)
logger.error('[FileStorage] Image compression failed:', error)
// 压缩失败时直接复制原文件
await fs.promises.copyFile(sourcePath, destPath)
}
@@ -159,7 +159,7 @@ class FileManager {
const ext = path.extname(origin_name).toLowerCase()
const destPath = path.join(this.storageDir, uuid + ext)
logger.info('[FileManager] Uploading file:', file.path)
logger.info('[FileStorage] Uploading file:', file.path)
// 根据文件类型选择处理方式
if (imageExts.includes(ext)) {
@@ -411,7 +411,7 @@ class FileManager {
return fileMetadata
} catch (error) {
logger.error('[FileManager] Download file error:', error)
logger.error('[FileStorage] Download file error:', error)
throw error
}
}
@@ -447,12 +447,12 @@ class FileManager {
// 复制文件
await fs.promises.copyFile(sourcePath, destPath)
logger.info('[FileManager] File copied successfully:', { from: sourcePath, to: destPath })
logger.info('[FileStorage] File copied successfully:', { from: sourcePath, to: destPath })
} catch (error) {
logger.error('[FileManager] Copy file failed:', error)
logger.error('[FileStorage] Copy file failed:', error)
throw error
}
}
}
export default FileManager
export default FileStorage
+53
View File
@@ -0,0 +1,53 @@
import { BrowserWindow, globalShortcut } from 'electron'
export function registerZoomShortcut(mainWindow: BrowserWindow) {
const registerShortcuts = () => {
// 注册放大快捷键 (Ctrl+Plus 或 Cmd+Plus)
globalShortcut.register('CommandOrControl+=', () => {
if (mainWindow) {
const currentZoom = mainWindow.webContents.getZoomFactor()
const newZoom = currentZoom + 0.1
// Prevent zoom factor from exceeding reasonable limits
if (newZoom <= 5.0) {
mainWindow.webContents.setZoomFactor(newZoom)
}
}
})
// 注册缩小快捷键 (Ctrl+Minus 或 Cmd+Minus)
globalShortcut.register('CommandOrControl+-', () => {
if (mainWindow) {
const currentZoom = mainWindow.webContents.getZoomFactor()
const newZoom = currentZoom - 0.1
// Prevent zoom factor from going below 0.1
if (newZoom >= 0.1) {
mainWindow.webContents.setZoomFactor(newZoom)
}
}
})
// 注册重置缩放快捷键 (Ctrl+0 或 Cmd+0)
globalShortcut.register('CommandOrControl+0', () => {
if (mainWindow) {
mainWindow.webContents.setZoomFactor(1)
}
})
}
const unregisterShortcuts = () => {
globalShortcut.unregister('CommandOrControl+=')
globalShortcut.unregister('CommandOrControl+-')
globalShortcut.unregister('CommandOrControl+0')
}
// 当窗口获得焦点时注册快捷键
mainWindow.on('focus', registerShortcuts)
// 当窗口失去焦点时注销快捷键
mainWindow.on('blur', unregisterShortcuts)
// 初始注册(如果窗口已经处于焦点状态)
if (mainWindow.isFocused()) {
registerShortcuts()
}
}