@@ -60,14 +60,8 @@ import {
6060import { invoke } from '@tauri-apps/api/core'
6161import { useToast } from '../plugins/toast'
6262import { StreamLanguage } from '@codemirror/language'
63-
64- interface EditorConfig
65- {
66- theme ?: string
67- indent_with_tab ?: boolean
68- tab_size ?: number
69- font_size ?: number
70- }
63+ import { EditorConfig } from '../types/app.ts'
64+ import { EditorView } from '@codemirror/view'
7165
7266interface Props
7367{
@@ -83,6 +77,13 @@ export function useCodeMirrorEditor(props: Props)
8377 const isReady = ref ( false )
8478 const extensions = ref < any [ ] > ( [ ] )
8579 const editorConfig = ref < EditorConfig > ( { } )
80+ const defaultConfig = {
81+ theme : 'githubLight' ,
82+ indent_with_tab : true ,
83+ tab_size : 2 ,
84+ font_size : 14 ,
85+ show_line_numbers : false
86+ }
8687
8788 // 主题映射
8889 const themeMap : Record < string , any > = {
@@ -180,8 +181,18 @@ export function useCodeMirrorEditor(props: Props)
180181 }
181182 }
182183
184+ // 隐藏行号的主题扩展
185+ const hideLineNumbersTheme = EditorView . theme ( {
186+ '.cm-lineNumbers' : {
187+ display : 'none !important'
188+ } ,
189+ '.cm-gutters' : {
190+ display : 'none !important'
191+ }
192+ } )
193+
183194 // 更新扩展的函数
184- const updateExtensions = async ( ) => {
195+ const updateExtensions = async ( showLineNumbers ?: boolean ) => {
185196 const result = [ ]
186197
187198 // 添加主题扩展
@@ -196,6 +207,14 @@ export function useCodeMirrorEditor(props: Props)
196207 }
197208 }
198209
210+ // 处理行号显示逻辑
211+ const shouldShowLineNumbers = showLineNumbers ?? editorConfig . value ?. show_line_numbers ?? false
212+
213+ // 如果配置为不显示行号,则添加隐藏行号的扩展
214+ if ( ! shouldShowLineNumbers ) {
215+ result . push ( hideLineNumbersTheme )
216+ }
217+
199218 extensions . value = result
200219
201220 // 如果组件还没准备好,等待下一个 tick 后设置为准备好
@@ -219,26 +238,14 @@ export function useCodeMirrorEditor(props: Props)
219238 }
220239 else {
221240 // 如果没有配置,使用默认配置
222- editorConfig . value = {
223- theme : 'githubLight' ,
224- indent_with_tab : true ,
225- tab_size : 2 ,
226- font_size : 14
227- }
241+ editorConfig . value = defaultConfig
228242 await updateExtensions ( )
229243 }
230244 }
231245 catch ( error ) {
232246 console . error ( '获取配置失败:' , error )
233247 toast . error ( '获取配置失败 - 错误信息: ' + error )
234-
235- // 失败时使用默认配置
236- editorConfig . value = {
237- theme : 'githubLight' ,
238- indent_with_tab : true ,
239- tab_size : 2 ,
240- font_size : 14
241- }
248+ editorConfig . value = defaultConfig
242249 await updateExtensions ( )
243250 }
244251 }
@@ -296,6 +303,12 @@ export function useCodeMirrorEditor(props: Props)
296303 await reRenderEditor ( )
297304 } , { immediate : false } )
298305
306+ // 监听行号显示配置变化
307+ watch ( ( ) => editorConfig . value ?. show_line_numbers , async ( ) => {
308+ console . log ( '行号显示配置变化:' , editorConfig . value ?. show_line_numbers )
309+ await reRenderEditor ( )
310+ } , { immediate : false } )
311+
299312 return {
300313 // 状态
301314 isReady,
@@ -313,4 +326,4 @@ export function useCodeMirrorEditor(props: Props)
313326 getThemeExtension,
314327 getLanguageExtension
315328 }
316- }
329+ }
0 commit comments