Skip to content

Commit ba303eb

Browse files
committed
feat: 支持插件禁用启用
1 parent 6f425ad commit ba303eb

File tree

8 files changed

+129
-62
lines changed

8 files changed

+129
-62
lines changed

src/App.vue

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ const {
114114
getLanguageDisplayName,
115115
getCurrentConsoleType,
116116
handleLanguageChange,
117+
refreshLanguageList,
117118
initialize
118119
} = useLanguageManager(code, clearOutput, toast)
119120
@@ -136,13 +137,13 @@ const {
136137
const editorConfigKey = ref(0)
137138
const consoleType = ref('console')
138139
139-
// 处理设置变更
140-
const handleSettingsChanged = (config: any) => {
140+
const handleSettingsChanged = async (config: any) => {
141141
console.log('主组件接收到设置变更:', config)
142-
// 延迟一点点再刷新,减少闪烁
143142
setTimeout(() => {
144143
editorConfigKey.value++
145144
}, 50)
145+
146+
await refreshLanguageList()
146147
}
147148
148149
const loadExample = (content: string) => {

src/components/Settings.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
<!-- 语言配置 -->
2020
<template #language>
21-
<Language v-if="activeTab === 'language'"/>
21+
<Language v-if="activeTab === 'language'" @settings-changed="handleLanguageSettingsChanged"/>
2222
</template>
2323
</Tabs>
2424
</Modal>
@@ -43,6 +43,7 @@ const {
4343
activeTab,
4444
tabsData,
4545
handleEditorSettingsChanged,
46+
handleLanguageSettingsChanged,
4647
handleEditorError,
4748
closeSettings,
4849
initialize

src/components/setting/Language.vue

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,18 @@
88
:nav-class="['max-h-[70vh] overflow-y-auto']"
99
:tabs="tabsPluginData"
1010
@change="handleTabChange">
11+
<template #tab-button="{ tab }">
12+
<div class="flex w-full px-3 py-2 space-x-2">
13+
<Switch v-model="pluginEnabledStates[tab.key as string]"
14+
size="sm"
15+
@click.stop
16+
@change="(value, event) => handlePluginToggle(tab.key as string, value, event)"/>
17+
<div class="flex items-center space-x-2">
18+
<img v-if="tab.svgUrl" :src="tab.svgUrl" class="w-5 h-5" :alt="tab.label"/>
19+
<span>{{ tab.label }}</span>
20+
</div>
21+
</div>
22+
</template>
1123
<template #[activePlugin]="{ tab }">
1224
<h3 class="text-lg font-semibold text-gray-900 dark:text-white mb-4 flex items-center space-x-2">
1325
<img :src="`/icons/${activePlugin.replace(/\d+$/, '')}.svg`" class="w-6 h-6" :alt="tab.label"/>
@@ -21,6 +33,10 @@
2133
:nav-class="['w-full', 'justify-center']">
2234
<template #general>
2335
<div class="space-y-4">
36+
<Label label="启用插件">
37+
<Switch v-model="pluginConfig.enabled"/>
38+
</Label>
39+
2440
<Label label="编译前执行的命令">
2541
<Input v-model="pluginConfig.before_compile" class="w-full" placeholder="编译前执行的命令"/>
2642
</Label>
@@ -94,41 +110,37 @@
94110
</template>
95111

96112
<script setup lang="ts">
97-
import {onMounted} from 'vue'
98-
import {Folder} from 'lucide-vue-next'
99-
import {Codemirror} from 'vue-codemirror'
113+
import { onMounted } from 'vue'
114+
import { Folder } from 'lucide-vue-next'
115+
import { Codemirror } from 'vue-codemirror'
100116
import Button from '../../ui/Button.vue'
101117
import Tabs from '../../ui/Tabs.vue'
102118
import Number from '../../ui/Number.vue'
103119
import Label from '../../ui/Label.vue'
104120
import Input from '../../ui/Input.vue'
105-
import {useLanguageSettings} from '../../composables/useLanguageSettings'
121+
import { useLanguageSettings } from '../../composables/useLanguageSettings'
106122
import type PluginConfig from '../../types/plugin'
107123
import Select from "../../ui/Select.vue";
124+
import Switch from '../../ui/Switch.vue'
108125
109126
const emit = defineEmits<{
110127
'settings-changed': [config: PluginConfig]
111128
'error': [message: string]
112129
}>()
113130
114131
const {
115-
// 标签页状态
116132
activeTab,
117133
tabsData,
118134
consoleTypes,
119-
120-
// 插件配置
121135
activePlugin,
122136
tabsPluginData,
123137
pluginConfig,
138+
pluginEnabledStates,
124139
handleTabChange,
140+
handlePluginToggle,
125141
selectExecuteHome,
126-
127-
// 编辑器状态
128142
isEditorReady,
129143
currentExtensions,
130-
131-
// 方法
132144
initialize
133145
} = useLanguageSettings(emit)
134146

src/composables/useLanguageManager.ts

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,25 @@ export function useLanguageManager(
9696
const getSupportedLanguages = async () => {
9797
try {
9898
const languages = await invoke<Language[]>('get_supported_languages')
99-
supportedLanguages.value = languages.map((language) => ({
99+
const allLanguages = languages.map((language) => ({
100100
name: language.name,
101101
value: language.value,
102102
svgUrl: `/icons/${language.value.replace(/\d+$/, '')}.svg`
103103
}))
104+
105+
if (globalConfig.value && globalConfig.value.plugins) {
106+
const filtered = allLanguages.filter((language) => {
107+
const plugin = globalConfig.value.plugins.find((p: any) => p.language === language.value)
108+
const enabled = !plugin || plugin.enabled !== false
109+
console.log(`语言 ${language.name} (${language.value}): enabled=${enabled}`)
110+
return enabled
111+
})
112+
console.log('过滤后的语言列表:', filtered.map(l => l.name))
113+
supportedLanguages.value = filtered
114+
} else {
115+
console.log('未找到配置,显示所有语言')
116+
supportedLanguages.value = allLanguages
117+
}
104118
}
105119
catch (error) {
106120
console.error('Error getting supported languages:', error)
@@ -138,14 +152,33 @@ export function useLanguageManager(
138152
toast.info(`已切换到 ${getLanguageDisplayName(newLanguage)}`)
139153
}
140154

141-
const initialize = async () => {
142-
// 获取支持的语言列表
155+
const refreshLanguageList = async () => {
156+
console.log('=== 开始刷新语言列表 ===')
157+
console.log('刷新前的语言列表:', supportedLanguages.value.map(l => l.name))
158+
159+
await getConfigure()
160+
console.log('配置已重新加载')
161+
143162
await getSupportedLanguages()
163+
console.log('语言列表已重新获取:', supportedLanguages.value.map(l => l.name))
164+
165+
const currentStillAvailable = supportedLanguages.value.some(lang => lang.value === currentLanguage.value)
166+
console.log(`当前语言 ${currentLanguage.value} 是否仍然可用:`, currentStillAvailable)
144167

145-
// 获取配置
168+
if (!currentStillAvailable && supportedLanguages.value.length > 0) {
169+
currentLanguage.value = supportedLanguages.value[0].value
170+
code.value = filterPluginTemplate(currentLanguage.value)
171+
await refreshEnvInfo()
172+
console.log('当前语言已禁用,切换到:', currentLanguage.value)
173+
}
174+
console.log('=== 刷新语言列表完成 ===')
175+
}
176+
177+
const initialize = async () => {
146178
await getConfigure()
147179

148-
// 设置默认语言和初始代码模板
180+
await getSupportedLanguages()
181+
149182
if (supportedLanguages.value.length > 0) {
150183
currentLanguage.value = supportedLanguages.value[0].value
151184
console.log('当前语言:', currentLanguage.value)
@@ -169,6 +202,7 @@ export function useLanguageManager(
169202
isLoadingEnvInfo,
170203
getLanguageDisplayName,
171204
handleLanguageChange,
205+
refreshLanguageList,
172206
initialize,
173207
getCurrentPluginConfig,
174208
getCurrentConsoleType

src/composables/useLanguageSettings.ts

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import {useCodeMirrorEditor} from './useCodeMirrorEditor'
55

66
export function useLanguageSettings(emit: any)
77
{
8-
// 标签页管理
98
const activeTab = ref('general')
9+
const pluginEnabledStates = ref<Record<string, boolean>>({})
1010

1111
const tabsData = [
1212
{
@@ -33,13 +33,14 @@ export function useLanguageSettings(emit: any)
3333

3434
const consoleTypes = [{label: '控制台', value: 'console'}, {label: 'Web', value: 'web'}]
3535

36-
// 插件配置管理
3736
const {
3837
activePlugin,
3938
tabsPluginData,
4039
pluginConfig,
40+
globalConfig,
4141
handleTabChange,
4242
selectExecuteHome,
43+
updateGlobalConfig,
4344
initializePlugin
4445
} = usePluginConfig(emit)
4546

@@ -110,46 +111,70 @@ export function useLanguageSettings(emit: any)
110111
console.log('Template changed:', newTemplate)
111112
}, {immediate: false})
112113

113-
// 初始化所有功能
114+
const handlePluginToggle = async (language: string, enabled: boolean, event: Event) => {
115+
if (!globalConfig.value || !globalConfig.value.plugins) return
116+
117+
const plugin = globalConfig.value.plugins.find((p: any) => p.language === language)
118+
if (plugin) {
119+
plugin.enabled = enabled
120+
await updateGlobalConfig(plugin)
121+
}
122+
}
123+
124+
const syncPluginStates = () => {
125+
if (globalConfig.value && globalConfig.value.plugins) {
126+
const states: Record<string, boolean> = {}
127+
tabsPluginData.value.forEach((tab) => {
128+
const plugin = globalConfig.value.plugins.find((p: any) => p.language === tab.key)
129+
states[tab.key] = plugin?.enabled !== false
130+
})
131+
pluginEnabledStates.value = states
132+
}
133+
}
134+
135+
watch(tabsPluginData, () => {
136+
syncPluginStates()
137+
}, { immediate: true })
138+
139+
watch(pluginConfig, () => {
140+
if (activePlugin.value && pluginConfig.value) {
141+
pluginEnabledStates.value[activePlugin.value] = pluginConfig.value.enabled
142+
}
143+
}, { deep: true })
144+
114145
const initialize = async () => {
115146
console.log('Component mounted')
116147

117-
// 先初始化插件配置
118148
await initializePlugin()
119149
console.log('Plugin initialized:', {
120150
activePlugin: activePlugin.value,
121151
template: pluginConfig.value?.template
122152
})
123153

124-
// 再初始化编辑器
154+
syncPluginStates()
155+
125156
await initializeEditor()
126157
console.log('Editor initialized')
127158

128-
// 更新扩展
129159
await updateExtensions()
130160
console.log('Extensions updated:', currentExtensions.value)
131161
}
132162

133163
return {
134-
// 标签页状态
135164
activeTab,
136165
tabsData,
137166
consoleTypes,
138-
139-
// 插件配置
140167
activePlugin,
141168
tabsPluginData,
142169
pluginConfig,
170+
pluginEnabledStates,
143171
handleTabChange,
172+
handlePluginToggle,
144173
selectExecuteHome,
145-
146-
// 编辑器状态
147174
isEditorReady,
148175
currentExtensions,
149176
currentLanguage,
150177
templateContent,
151-
152-
// 方法
153178
updateExtensions,
154179
initialize
155180
}

src/composables/usePluginConfig.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export function usePluginConfig(emit?: any)
2929
const globalConfig = ref<any>(null)
3030

3131
const pluginConfig = ref<PluginConfig>({
32-
enabled: false,
32+
enabled: true,
3333
execute_home: '',
3434
extension: '',
3535
language: '',
@@ -92,9 +92,8 @@ export function usePluginConfig(emit?: any)
9292
}
9393
else {
9494
console.warn('未找到插件配置:', activePlugin.value)
95-
// 创建默认配置
9695
pluginConfig.value = {
97-
enabled: false,
96+
enabled: true,
9897
execute_home: '',
9998
extension: '',
10099
language: activePlugin.value,
@@ -187,10 +186,9 @@ export function usePluginConfig(emit?: any)
187186
}
188187
}
189188

190-
// 重置插件配置
191189
const resetPluginConfig = (language: string) => {
192190
pluginConfig.value = {
193-
enabled: false,
191+
enabled: true,
194192
execute_home: '',
195193
extension: '',
196194
language: language,

src/composables/useSettings.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,16 @@ export function useSettings(emit: any)
1414
{ key: 'language', label: '语言', icon: BracesIcon }
1515
]
1616

17-
// 处理编辑器设置变更
1817
const handleEditorSettingsChanged = (config: any) => {
1918
console.log('设置模态框接收到编辑器配置变更:', config)
20-
// 向上传递事件到主组件
2119
emit('settings-changed', config)
2220
}
2321

24-
// 处理编辑器错误
22+
const handleLanguageSettingsChanged = (config: any) => {
23+
console.log('设置模态框接收到语言配置变更:', config)
24+
emit('settings-changed', config)
25+
}
26+
2527
const handleEditorError = (message: string) => {
2628
console.error('编辑器设置错误:', message)
2729
}
@@ -44,13 +46,11 @@ export function useSettings(emit: any)
4446
}
4547

4648
return {
47-
// 状态
4849
isVisible,
4950
activeTab,
5051
tabsData,
51-
52-
// 方法
5352
handleEditorSettingsChanged,
53+
handleLanguageSettingsChanged,
5454
handleEditorError,
5555
closeSettings,
5656
initialize

0 commit comments

Comments
 (0)