Skip to content

Commit a36062c

Browse files
committed
feat (core): 拆分插件配置
1 parent 37f48e9 commit a36062c

2 files changed

Lines changed: 309 additions & 123 deletions

File tree

Lines changed: 29 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
<template>
22
<div class="-mt-2">
3-
<Tabs v-model="activePlugin" type="card" size="md" position="left" :tab-button-class="['w-36']" :tabs="tabsPluginData" @change="handleTabChange">
3+
<Tabs v-model="activePlugin"
4+
type="card"
5+
size="md"
6+
position="left"
7+
:tab-button-class="['w-36']"
8+
:tabs="tabsPluginData"
9+
@change="handleTabChange">
410
<template #[activePlugin]="{ tab }">
511
<h3 class="text-lg font-semibold text-gray-900 dark:text-white mb-4 flex items-center">
612
<LanguagesIcon class="w-5 h-5 mr-2"/>
713
{{ `语言 [ ${ tab.label } ] 配置` }}
814
</h3>
915

10-
<Tabs v-model="activeTab" type="card" size="md" :tabs="tabsData" :nav-class="['w-full', 'justify-center']">
16+
<Tabs v-model="activeTab"
17+
type="card"
18+
size="md"
19+
:tabs="tabsData"
20+
:nav-class="['w-full', 'justify-center']">
1121
<template #general>
1222
<div class="space-y-4">
1323
<div>
@@ -119,37 +129,39 @@
119129
</template>
120130

121131
<script setup lang="ts">
122-
import { onMounted, ref, watch } from 'vue'
123-
import { invoke } from '@tauri-apps/api/core'
124-
import { debounce } from 'lodash-es'
125-
import { open as openDialog } from '@tauri-apps/plugin-dialog'
132+
import { onMounted } from 'vue'
126133
import { ContainerIcon, FileIcon, Folder, LanguagesIcon, PickaxeIcon, Settings2 } from 'lucide-vue-next'
127134
import Button from '../../ui/Button.vue'
128-
import { useToast } from '../../plugins/toast'
129135
import Tabs from '../../ui/Tabs.vue'
136+
import { usePluginConfig } from '../../composables/usePluginConfig'
130137
import type PluginConfig from '../../types/plugin'
131138
132139
const emit = defineEmits<{
133140
'settings-changed': [config: PluginConfig]
134141
'error': [message: string]
135142
}>()
136143
137-
const toast = useToast()
138-
139-
const activePlugin = ref('')
140-
const tabsPluginData = ref([] as any[])
141-
142-
const activeTab = ref('general')
144+
const {
145+
activePlugin,
146+
activeTab,
147+
tabsPluginData,
148+
pluginConfig,
149+
handleTabChange,
150+
selectExecuteHome,
151+
initializePlugin
152+
} = usePluginConfig(emit)
153+
154+
// 标签页数据
143155
const tabsData = [
144156
{
145157
key: 'general',
146158
label: '通用配置',
147159
icon: Settings2
148160
},
149161
{
150-
'key': 'environment',
151-
'label': '环境配置',
152-
'icon': ContainerIcon
162+
key: 'environment',
163+
label: '环境配置',
164+
icon: ContainerIcon
153165
},
154166
{
155167
key: 'template',
@@ -163,113 +175,7 @@ const tabsData = [
163175
}
164176
]
165177
166-
const globalConfig = ref(null as any)
167-
const pluginConfig = ref<PluginConfig>({
168-
enabled: false,
169-
execute_home: '',
170-
extension: '',
171-
language: '',
172-
before_compile: '',
173-
after_compile: '',
174-
run_command: '',
175-
template: '',
176-
timeout: 30
177-
})
178-
179-
const getSupportedLanguages = async () => {
180-
try {
181-
const languages = await invoke<any[]>('get_supported_languages')
182-
tabsPluginData.value = languages.map((language) => ({
183-
key: language.value,
184-
label: language.name,
185-
svgUrl: `/icons/${ language.value.replace(/\d+$/, '') }.svg`
186-
}))
187-
188-
if (tabsPluginData.value.length > 0 && !activePlugin.value) {
189-
activePlugin.value = tabsPluginData.value[0].key
190-
}
191-
}
192-
catch (error) {
193-
toast.error('获取支持的语言失败 - 错误信息: ' + error)
194-
tabsPluginData.value = []
195-
}
196-
}
197-
198-
const getConfigure = async () => {
199-
try {
200-
globalConfig.value = await invoke<any>('get_app_config')
201-
202-
handleTabChange()
203-
}
204-
catch (error) {
205-
toast.error('获取配置失败 - 错误信息: ' + error)
206-
}
207-
}
208-
209-
const handleTabChange = () => {
210-
if (globalConfig.value && globalConfig.value.plugins) {
211-
pluginConfig.value = globalConfig.value.plugins.find((plugin: any) => plugin.language === activePlugin.value)
212-
}
213-
}
214-
215-
const selectExecuteHome = async () => {
216-
try {
217-
const selected = await openDialog({
218-
directory: true,
219-
multiple: false,
220-
title: '选择语言环境目录'
221-
})
222-
223-
if (selected) {
224-
pluginConfig.value.execute_home = selected as string
225-
}
226-
}
227-
catch (error) {
228-
console.error('Failed to select directory:', error)
229-
emit('error', '选择目录失败')
230-
}
231-
}
232-
233-
const updateGlobalConfig = async (updatedPlugin: PluginConfig) => {
234-
if (!globalConfig.value || !globalConfig.value.plugins) {
235-
return
236-
}
237-
238-
const pluginIndex = globalConfig.value.plugins.findIndex(
239-
(plugin: any) => plugin.language === updatedPlugin.language
240-
)
241-
242-
if (pluginIndex !== -1) {
243-
globalConfig.value.plugins[pluginIndex] = { ...updatedPlugin }
244-
}
245-
246-
try {
247-
await invoke('update_app_config', { config: globalConfig.value })
248-
toast.success(`${ tabsPluginData.value.find((tab: any) => tab.key === updatedPlugin.language).label } 配置已保存`)
249-
emit('settings-changed', updatedPlugin)
250-
}
251-
catch (error) {
252-
toast.error('保存配置失败 - 错误信息: ' + error)
253-
emit('error', '保存配置失败')
254-
}
255-
}
256-
257-
watch(pluginConfig, (newConfig, oldConfig) => {
258-
if (oldConfig && newConfig.language) {
259-
// 防抖处理,避免频繁保存
260-
debounceUpdate(newConfig)
261-
}
262-
}, {
263-
deep: true,
264-
flush: 'post'
265-
})
266-
267-
const debounceUpdate = debounce((config: PluginConfig) => {
268-
updateGlobalConfig(config)
269-
}, 1000)
270-
271178
onMounted(async () => {
272-
await getSupportedLanguages()
273-
await getConfigure()
179+
await initializePlugin()
274180
})
275181
</script>

0 commit comments

Comments
 (0)