Skip to content

Commit 0b72dfe

Browse files
committed
feat (ui): 支持编辑器行号配置
1 parent 49d2cce commit 0b72dfe

5 files changed

Lines changed: 59 additions & 36 deletions

File tree

src-tauri/src/config.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ static CONFIG_MANAGER: Mutex<Option<ConfigManager>> = Mutex::new(None);
1212

1313
#[derive(Debug, Clone, Serialize, Deserialize)]
1414
pub struct EditorConfig {
15-
pub indent_with_tab: Option<bool>, // 是否使用 tab 缩进
16-
pub tab_size: Option<u32>, // tab 缩进, 空格数,默认为 2
17-
pub theme: Option<String>, // 编辑器主题
18-
pub font_size: Option<u32>, // 编辑器字体大小
15+
pub indent_with_tab: Option<bool>, // 是否使用 tab 缩进
16+
pub tab_size: Option<u32>, // tab 缩进, 空格数,默认为 2
17+
pub theme: Option<String>, // 编辑器主题
18+
pub font_size: Option<u32>, // 编辑器字体大小
19+
pub show_line_numbers: Option<bool>, // 是否显示行号
1920
}
2021

2122
#[derive(Debug, Clone, Serialize, Deserialize)]
@@ -41,6 +42,7 @@ impl Default for AppConfig {
4142
tab_size: Some(2),
4243
theme: Some("githubLight".to_string()),
4344
font_size: Some(14),
45+
show_line_numbers: Some(true),
4446
}),
4547
}
4648
}
@@ -97,6 +99,7 @@ impl ConfigManager {
9799
tab_size: Some(2),
98100
theme: Some("githubLight".to_string()),
99101
font_size: Some(14),
102+
show_line_numbers: Some(true),
100103
});
101104
println!("读取配置 -> 添加默认 editor 配置");
102105
}
@@ -201,6 +204,7 @@ impl ConfigManager {
201204
tab_size: Some(2),
202205
theme: Some("githubLight".to_string()),
203206
font_size: Some(14),
207+
show_line_numbers: Some(true),
204208
}),
205209
}
206210
}

src/components/setting/Editor.vue

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
<Switch v-model="editorConfig.indent_with_tab"/>
55
</Label>
66

7+
<Label label="是否显示行号">
8+
<Switch v-model="editorConfig.show_line_numbers"/>
9+
</Label>
10+
711
<Label label="缩进空格数">
812
<Number v-model="editorConfig.tab_size" :min="1" :max="8" placeholder="缩进空格数"/>
913
</Label>

src/composables/useCodeMirrorEditor.ts

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,8 @@ import {
6060
import { invoke } from '@tauri-apps/api/core'
6161
import { useToast } from '../plugins/toast'
6262
import { 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

7266
interface 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+
}

src/composables/useEditorConfig.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,7 @@ import { ref, watch } from 'vue'
22
import { debounce } from 'lodash-es'
33
import { invoke } from '@tauri-apps/api/core'
44
import { useToast } from '../plugins/toast'
5-
6-
interface EditorConfig
7-
{
8-
tab_size?: number
9-
theme?: string
10-
indent_with_tab?: boolean
11-
font_size?: number
12-
}
5+
import { EditorConfig } from '../types/app.ts'
136

147
interface ThemeOption
158
{

src/types/app.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,12 @@ export interface AppInfo
4444
platform: string
4545
arch: string
4646
}
47+
48+
export interface EditorConfig
49+
{
50+
theme?: string
51+
indent_with_tab?: boolean
52+
tab_size?: number
53+
font_size?: number
54+
show_line_numbers?: boolean
55+
}

0 commit comments

Comments
 (0)