mirror of
https://gh.catmak.name/https://github.com/mihomo-party-org/mihomo-party
synced 2025-12-27 13:10:30 +08:00
support import local themes
This commit is contained in:
parent
f156dd1311
commit
68aba2afc3
@ -1,4 +1,4 @@
|
|||||||
import { readdir, readFile } from 'fs/promises'
|
import { copyFile, readdir, readFile } from 'fs/promises'
|
||||||
import { themesDir } from '../utils/dirs'
|
import { themesDir } from '../utils/dirs'
|
||||||
import path from 'path'
|
import path from 'path'
|
||||||
import axios from 'axios'
|
import axios from 'axios'
|
||||||
@ -40,6 +40,12 @@ export async function fetchThemes(): Promise<void> {
|
|||||||
zip.extractAllTo(themesDir(), true)
|
zip.extractAllTo(themesDir(), true)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function importThemes(files: string[]): Promise<void> {
|
||||||
|
for (const file of files) {
|
||||||
|
if (existsSync(file)) await copyFile(file, path.join(themesDir(), path.basename(file)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export async function applyTheme(theme: string): Promise<void> {
|
export async function applyTheme(theme: string): Promise<void> {
|
||||||
if (theme === 'default.css') {
|
if (theme === 'default.css') {
|
||||||
if (insertedCSSKey) {
|
if (insertedCSSKey) {
|
||||||
|
|||||||
@ -65,7 +65,7 @@ import { getInterfaces } from '../sys/interface'
|
|||||||
import { copyEnv } from '../resolve/tray'
|
import { copyEnv } from '../resolve/tray'
|
||||||
import { registerShortcut } from '../resolve/shortcut'
|
import { registerShortcut } from '../resolve/shortcut'
|
||||||
import { mainWindow } from '..'
|
import { mainWindow } from '..'
|
||||||
import { applyTheme, fetchThemes, resolveThemes } from '../resolve/theme'
|
import { applyTheme, fetchThemes, importThemes, resolveThemes } from '../resolve/theme'
|
||||||
import { subStoreCollections, subStoreSubs } from '../core/subStoreApi'
|
import { subStoreCollections, subStoreSubs } from '../core/subStoreApi'
|
||||||
import { logDir } from './dirs'
|
import { logDir } from './dirs'
|
||||||
import path from 'path'
|
import path from 'path'
|
||||||
@ -204,6 +204,7 @@ export function registerIpcMainHandlers(): void {
|
|||||||
})
|
})
|
||||||
ipcMain.handle('resolveThemes', () => ipcErrorWrapper(resolveThemes)())
|
ipcMain.handle('resolveThemes', () => ipcErrorWrapper(resolveThemes)())
|
||||||
ipcMain.handle('fetchThemes', () => ipcErrorWrapper(fetchThemes)())
|
ipcMain.handle('fetchThemes', () => ipcErrorWrapper(fetchThemes)())
|
||||||
|
ipcMain.handle('importThemes', (_e, file) => ipcErrorWrapper(importThemes)(file))
|
||||||
ipcMain.handle('applyTheme', (_e, theme) => ipcErrorWrapper(applyTheme)(theme))
|
ipcMain.handle('applyTheme', (_e, theme) => ipcErrorWrapper(applyTheme)(theme))
|
||||||
ipcMain.handle('copyEnv', (_e, type) => ipcErrorWrapper(copyEnv)(type))
|
ipcMain.handle('copyEnv', (_e, type) => ipcErrorWrapper(copyEnv)(type))
|
||||||
ipcMain.handle('alert', (_e, msg) => {
|
ipcMain.handle('alert', (_e, msg) => {
|
||||||
|
|||||||
@ -2,7 +2,7 @@ import React, { useEffect, useState } from 'react'
|
|||||||
import SettingCard from '../base/base-setting-card'
|
import SettingCard from '../base/base-setting-card'
|
||||||
import SettingItem from '../base/base-setting-item'
|
import SettingItem from '../base/base-setting-item'
|
||||||
import { Button, Input, Select, SelectItem, Switch, Tab, Tabs, Tooltip } from '@nextui-org/react'
|
import { Button, Input, Select, SelectItem, Switch, Tab, Tabs, Tooltip } from '@nextui-org/react'
|
||||||
import { BiCopy } from 'react-icons/bi'
|
import { BiCopy, BiSolidFileImport } from 'react-icons/bi'
|
||||||
import useSWR from 'swr'
|
import useSWR from 'swr'
|
||||||
import {
|
import {
|
||||||
checkAutoRun,
|
checkAutoRun,
|
||||||
@ -10,6 +10,8 @@ import {
|
|||||||
disableAutoRun,
|
disableAutoRun,
|
||||||
enableAutoRun,
|
enableAutoRun,
|
||||||
fetchThemes,
|
fetchThemes,
|
||||||
|
getFilePath,
|
||||||
|
importThemes,
|
||||||
relaunchApp,
|
relaunchApp,
|
||||||
resolveThemes,
|
resolveThemes,
|
||||||
restartCore
|
restartCore
|
||||||
@ -223,27 +225,48 @@ const GeneralConfig: React.FC = () => {
|
|||||||
<SettingItem
|
<SettingItem
|
||||||
title="主题"
|
title="主题"
|
||||||
actions={
|
actions={
|
||||||
<Button
|
<>
|
||||||
size="sm"
|
<Button
|
||||||
isLoading={fetching}
|
size="sm"
|
||||||
isIconOnly
|
isLoading={fetching}
|
||||||
title="拉取主题"
|
isIconOnly
|
||||||
variant="light"
|
title="拉取主题"
|
||||||
className="ml-2"
|
variant="light"
|
||||||
onPress={async () => {
|
className="ml-2"
|
||||||
setFetching(true)
|
onPress={async () => {
|
||||||
try {
|
setFetching(true)
|
||||||
await fetchThemes()
|
try {
|
||||||
setCustomThemes(await resolveThemes())
|
await fetchThemes()
|
||||||
} catch (e) {
|
setCustomThemes(await resolveThemes())
|
||||||
alert(e)
|
} catch (e) {
|
||||||
} finally {
|
alert(e)
|
||||||
setFetching(false)
|
} finally {
|
||||||
}
|
setFetching(false)
|
||||||
}}
|
}
|
||||||
>
|
}}
|
||||||
<IoMdCloudDownload className="text-lg" />
|
>
|
||||||
</Button>
|
<IoMdCloudDownload className="text-lg" />
|
||||||
|
</Button>
|
||||||
|
<Button
|
||||||
|
size="sm"
|
||||||
|
isIconOnly
|
||||||
|
title="导入主题"
|
||||||
|
variant="light"
|
||||||
|
className="ml-2"
|
||||||
|
onPress={async () => {
|
||||||
|
const files = await getFilePath(['css'])
|
||||||
|
if (!files) return
|
||||||
|
try {
|
||||||
|
await importThemes(files)
|
||||||
|
setCustomThemes(await resolveThemes())
|
||||||
|
} catch (e) {
|
||||||
|
alert(e)
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<BiSolidFileImport className="text-lg" />
|
||||||
|
</Button>
|
||||||
|
</>
|
||||||
}
|
}
|
||||||
>
|
>
|
||||||
<Select
|
<Select
|
||||||
|
|||||||
@ -339,6 +339,10 @@ export async function fetchThemes(): Promise<void> {
|
|||||||
return ipcErrorWrapper(await window.electron.ipcRenderer.invoke('fetchThemes'))
|
return ipcErrorWrapper(await window.electron.ipcRenderer.invoke('fetchThemes'))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function importThemes(files: string[]): Promise<void> {
|
||||||
|
return ipcErrorWrapper(await window.electron.ipcRenderer.invoke('importThemes', files))
|
||||||
|
}
|
||||||
|
|
||||||
export async function applyTheme(theme: string): Promise<void> {
|
export async function applyTheme(theme: string): Promise<void> {
|
||||||
return ipcErrorWrapper(await window.electron.ipcRenderer.invoke('applyTheme', theme))
|
return ipcErrorWrapper(await window.electron.ipcRenderer.invoke('applyTheme', theme))
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user