88 :tabs =" tabsPluginData"
99 @change =" handleTabChange" >
1010 <template #[activePlugin ]=" { tab } " >
11- <h3 class =" text-lg font-semibold text-gray-900 dark:text-white mb-4 flex items-center" >
12- <LanguagesIcon class =" w-5 h-5 mr-2 " />
13- {{ `语言 [ ${ tab.label } ] 配置` }}
11+ <h3 class =" text-lg font-semibold text-gray-900 dark:text-white mb-4 flex items-center space-x-2 " >
12+ <img :src = " `/icons/${activePlugin.replace(/\d+$/, '')}.svg` " class =" w-6 h-6 " :alt = " tab.label " />
13+ < span > {{ `语言 [ ${ tab.label } ] 配置` }}</ span >
1414 </h3 >
1515
1616 <Tabs v-model =" activeTab"
9696 </template >
9797
9898 <template #template >
99- <div >
100- <label class =" block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2" >
101- 文件模板
102- </label >
103- <div class =" flex" >
104- <textarea v-model =" pluginConfig.template"
105- placeholder =" 文件模板"
106- rows =" 20"
107- class =" flex-1 px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md bg-white dark:bg-gray-700 text-gray-900 dark:text-white focus:outline-none focus:ring-1 focus:ring-blue-500 focus:border-transparent text-sm" />
99+ <div class =" w-[98%]" >
100+ <Codemirror v-if =" isEditorReady && pluginConfig.template !== undefined"
101+ style =" width : 100% ; height : 380px "
102+ v-model =" pluginConfig.template"
103+ :extensions =" currentExtensions"
104+ class =" flex-1 border border-gray-300 dark:border-gray-600 rounded-md overflow-hidden" />
105+ <div v-else class =" flex-1 flex items-center justify-center h-64 border border-gray-300 dark:border-gray-600 rounded-md" >
106+ <div class =" text-gray-500" >加载编辑器中...</div >
108107 </div >
109108 </div >
110109 </template >
129128</template >
130129
131130<script setup lang="ts">
132- import { onMounted } from ' vue'
133- import { ContainerIcon , FileIcon , Folder , LanguagesIcon , PickaxeIcon , Settings2 } from ' lucide-vue-next'
131+ import { computed , nextTick , onMounted , ref , watch } from ' vue'
132+ import { ContainerIcon , FileIcon , Folder , PickaxeIcon , Settings2 } from ' lucide-vue-next'
133+ import { Codemirror } from ' vue-codemirror'
134134import Button from ' ../../ui/Button.vue'
135135import Tabs from ' ../../ui/Tabs.vue'
136136import { usePluginConfig } from ' ../../composables/usePluginConfig'
137137import type PluginConfig from ' ../../types/plugin'
138+ import { useCodeMirrorEditor } from ' ../../composables/useCodeMirrorEditor.ts'
138139
139140const emit = defineEmits <{
140141 ' settings-changed' : [config : PluginConfig ]
@@ -151,6 +152,78 @@ const {
151152 initializePlugin
152153} = usePluginConfig (emit )
153154
155+ // 编辑器状态
156+ const isEditorReady = ref (false )
157+ const currentExtensions = ref <any []>([])
158+
159+ // 创建 computed 来响应式地获取当前语言
160+ const currentLanguage = computed (() => {
161+ return activePlugin .value || ' '
162+ })
163+
164+ // 创建 computed 来响应式地获取模板内容
165+ const templateContent = computed ({
166+ get : () => pluginConfig .value ?.template || ' ' ,
167+ set : (value : string ) => {
168+ if (pluginConfig .value ) {
169+ pluginConfig .value .template = value
170+ }
171+ }
172+ })
173+
174+ // 使用 useCodeMirrorEditor composable
175+ const {
176+ initializeEditor,
177+ getLanguageExtension,
178+ getThemeExtension
179+ } = useCodeMirrorEditor (
180+ {
181+ modelValue: templateContent .value ,
182+ language: currentLanguage .value
183+ },
184+ // emit 函数用于更新 modelValue
185+ (event : ' update:modelValue' , value : string ) => {
186+ templateContent .value = value
187+ }
188+ )
189+
190+ // 更新扩展的函数
191+ const updateExtensions = async () => {
192+ const newExtensions = []
193+
194+ // 添加主题扩展
195+ const themeExtension = getThemeExtension ()
196+ newExtensions .push (themeExtension )
197+
198+ // 添加语言扩展
199+ if (currentLanguage .value ) {
200+ const langExtension = getLanguageExtension (currentLanguage .value )
201+ if (langExtension ) {
202+ newExtensions .push (langExtension )
203+ }
204+ }
205+
206+ currentExtensions .value = newExtensions
207+
208+ if (! isEditorReady .value ) {
209+ await nextTick ()
210+ isEditorReady .value = true
211+ }
212+ }
213+
214+ // 监听语言变化
215+ watch (currentLanguage , async (newLanguage ) => {
216+ console .log (' Language changed to:' , newLanguage )
217+ if (newLanguage ) {
218+ await updateExtensions ()
219+ }
220+ }, { immediate: false })
221+
222+ // 监听插件配置变化
223+ watch (() => pluginConfig .value ?.template , (newTemplate ) => {
224+ console .log (' Template changed:' , newTemplate )
225+ }, { immediate: false })
226+
154227// 标签页数据
155228const tabsData = [
156229 {
@@ -176,6 +249,21 @@ const tabsData = [
176249]
177250
178251onMounted (async () => {
252+ console .log (' Component mounted' )
253+
254+ // 先初始化插件配置
179255 await initializePlugin ()
256+ console .log (' Plugin initialized:' , {
257+ activePlugin: activePlugin .value ,
258+ template: pluginConfig .value ?.template
259+ })
260+
261+ // 再初始化编辑器
262+ await initializeEditor ()
263+ console .log (' Editor initialized' )
264+
265+ // 更新扩展
266+ await updateExtensions ()
267+ console .log (' Extensions updated:' , currentExtensions .value )
180268})
181269 </script >
0 commit comments