diff --git a/package.json b/package.json index ae1303f..6d3f81c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mihomo-party", - "version": "0.1.6", + "version": "0.2.0", "description": "Mihomo Party", "main": "./out/main/index.js", "author": "mihomo-party", diff --git a/src/main/utils/ipc.ts b/src/main/utils/ipc.ts index 60cbad7..aa06f14 100644 --- a/src/main/utils/ipc.ts +++ b/src/main/utils/ipc.ts @@ -35,6 +35,7 @@ import { triggerSysProxy } from '../resolve/sysproxy' import { checkUpdate } from '../resolve/autoUpdater' import { exePath, mihomoCorePath } from './dirs' import { execSync } from 'child_process' +import fs from 'fs' export function registerIpcMainHandlers(): void { ipcMain.handle('mihomoVersion', mihomoVersion) @@ -70,6 +71,8 @@ export function registerIpcMainHandlers(): void { ipcMain.handle('triggerSysProxy', (_e, enable) => triggerSysProxy(enable)) ipcMain.handle('isEncryptionAvailable', isEncryptionAvailable) ipcMain.handle('encryptString', (_e, str) => safeStorage.encryptString(str)) + ipcMain.handle('getFilePath', getFilePath) + ipcMain.handle('readTextFile', (_e, filePath) => readTextFile(filePath)) ipcMain.handle('checkUpdate', () => checkUpdate()) ipcMain.handle('getVersion', () => app.getVersion()) ipcMain.handle('platform', () => process.platform) @@ -77,6 +80,18 @@ export function registerIpcMainHandlers(): void { ipcMain.handle('quitApp', () => app.quit()) } +function getFilePath(): string[] | undefined { + return dialog.showOpenDialogSync({ + title: '选择订阅文件', + filters: [{ name: 'Yaml Files', extensions: ['yml', 'yaml'] }], + properties: ['openFile'] + }) +} + +function readTextFile(filePath: string): string { + return fs.readFileSync(filePath, 'utf8') +} + async function setupFirewall(): Promise { return new Promise((resolve, reject) => { const removeCommand = ` diff --git a/src/renderer/src/pages/profiles.tsx b/src/renderer/src/pages/profiles.tsx index c7ff404..b9c76c5 100644 --- a/src/renderer/src/pages/profiles.tsx +++ b/src/renderer/src/pages/profiles.tsx @@ -2,6 +2,7 @@ import { Button, Input } from '@nextui-org/react' import BasePage from '@renderer/components/base/base-page' import ProfileItem from '@renderer/components/profiles/profile-item' import { useProfileConfig } from '@renderer/hooks/use-profile-config' +import { getFilePath, readTextFile } from '@renderer/utils/ipc' import { useEffect, useRef, useState } from 'react' import { MdContentPaste } from 'react-icons/md' @@ -40,22 +41,18 @@ const Profiles: React.FC = () => { e.stopPropagation() setFileOver(false) }) - pageRef.current?.addEventListener('drop', (event) => { + pageRef.current?.addEventListener('drop', async (event) => { event.preventDefault() event.stopPropagation() if (event.dataTransfer?.files) { const file = event.dataTransfer.files[0] if (file.name.endsWith('.yml') || file.name.endsWith('.yaml')) { - const reader = new FileReader() - reader.onload = async (e): Promise => { - const content = e.target?.result as string - try { - await addProfileItem({ name: file.name, type: 'local', file: content }) - } finally { - setFileOver(false) - } + const content = await readTextFile(file.path) + try { + await addProfileItem({ name: file.name, type: 'local', file: content }) + } finally { + setFileOver(false) } - reader.readAsText(file) } else { alert('不支持的文件类型') } @@ -74,7 +71,6 @@ const Profiles: React.FC = () => {
{ +
{ return await window.electron.ipcRenderer.invoke('encryptString', str) } +export async function getFilePath(): Promise { + return await window.electron.ipcRenderer.invoke('getFilePath') +} + +export async function readTextFile(filePath: string): Promise { + return await window.electron.ipcRenderer.invoke('readTextFile', filePath) +} + export async function checkUpdate(): Promise { return await window.electron.ipcRenderer.invoke('checkUpdate') }