Skip to content

Commit c78cb3f

Browse files
committed
feat (ui): 美化编辑器单词提示框
1 parent 940c2e3 commit c78cb3f

2 files changed

Lines changed: 76 additions & 29 deletions

File tree

src/composables/useCodeMirrorEditor.ts

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ import { invoke } from '@tauri-apps/api/core'
6161
import { useToast } from '../plugins/toast'
6262
import { StreamLanguage } from '@codemirror/language'
6363
import { EditorConfig } from '../types/app.ts'
64-
import { EditorView, hoverTooltip } from '@codemirror/view'
64+
import { EditorView } from '@codemirror/view'
65+
import { useCodeMirrorFunctionHelp } from './useCodeMirrorFunctionHelp'
6566

6667
interface Props
6768
{
@@ -72,6 +73,7 @@ interface Props
7273
export function useCodeMirrorEditor(props: Props)
7374
{
7475
const toast = useToast()
76+
const { showFunctionHelpHover, functionHelpTheme } = useCodeMirrorFunctionHelp()
7577

7678
// 状态管理
7779
const isReady = ref(false)
@@ -192,32 +194,6 @@ export function useCodeMirrorEditor(props: Props)
192194
}
193195
})
194196

195-
// 显示函数提示扩展
196-
const showFunctionHelpHover = hoverTooltip((view, pos, side) => {
197-
let { from, to, text } = view.state.doc.lineAt(pos)
198-
let start = pos, end = pos
199-
while (start > from && /\w/.test(text[start - from - 1])) {
200-
start--
201-
}
202-
while (end < to && /\w/.test(text[end - from])) {
203-
end++
204-
}
205-
if (start == pos && side < 0 || end == pos && side > 0) {
206-
return null
207-
}
208-
return {
209-
pos: start,
210-
end,
211-
above: true,
212-
create(_view)
213-
{
214-
let dom = document.createElement('div')
215-
dom.textContent = text.slice(start - from, end - from)
216-
return { dom }
217-
}
218-
}
219-
}, { hoverTime: 500 })
220-
221197
// 更新扩展的函数
222198
const updateExtensions = async (showLineNumbers?: boolean, showFunctionHelp?: boolean) => {
223199
const result = []
@@ -226,6 +202,9 @@ export function useCodeMirrorEditor(props: Props)
226202
const themeExtension = getThemeExtension(editorConfig.value?.theme)
227203
result.push(themeExtension)
228204

205+
// 添加函数帮助主题
206+
result.push(functionHelpTheme)
207+
229208
// 添加语言扩展
230209
if (props.language) {
231210
const langExtension = getLanguageExtension(props.language)
@@ -255,7 +234,7 @@ export function useCodeMirrorEditor(props: Props)
255234
}
256235
}
257236

258-
// 加载编辑器配置
237+
// 其余代码完全保持不变...
259238
const loadEditorConfig = async () => {
260239
try {
261240
const globalConfig = await invoke<any>('get_app_config')
@@ -363,4 +342,4 @@ export function useCodeMirrorEditor(props: Props)
363342
getThemeExtension,
364343
getLanguageExtension
365344
}
366-
}
345+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { EditorView, hoverTooltip } from '@codemirror/view'
2+
3+
export function useCodeMirrorFunctionHelp()
4+
{
5+
// 提示框
6+
const createHelpTooltip = (text: string) => {
7+
const dom = document.createElement('div')
8+
dom.className = 'cm-function-help-tooltip'
9+
10+
dom.innerHTML = `
11+
<div class="relative mb-1">
12+
<div class="px-2 py-1 bg-gray-800 text-white text-xs rounded">
13+
<span class="font-mono">${ text }</span>
14+
</div>
15+
<!-- 小三角箭头 -->
16+
<div class="absolute left-1/2 transform -translate-x-1/2 top-full">
17+
<div class="w-0 h-0 border-l-4 border-r-4 border-t-4 border-transparent border-t-gray-800"></div>
18+
</div>
19+
</div>
20+
`
21+
22+
return { dom }
23+
}
24+
25+
// 显示函数提示扩展
26+
const showFunctionHelpHover = hoverTooltip((view, pos, side) => {
27+
let { from, to, text } = view.state.doc.lineAt(pos)
28+
let start = pos, end = pos
29+
while (start > from && /\w/.test(text[start - from - 1])) {
30+
start--
31+
}
32+
while (end < to && /\w/.test(text[end - from])) {
33+
end++
34+
}
35+
if (start == pos && side < 0 || end == pos && side > 0) {
36+
return null
37+
}
38+
return {
39+
pos: start,
40+
end,
41+
above: true,
42+
create(_view)
43+
{
44+
return createHelpTooltip(text.slice(start - from, end - from))
45+
}
46+
}
47+
}, { hoverTime: 300 })
48+
49+
// 提示框样式主题
50+
const functionHelpTheme = EditorView.theme({
51+
'.cm-function-help-tooltip': {
52+
zIndex: '100',
53+
animation: 'fadeIn 0.2s ease-out'
54+
},
55+
'.cm-tooltip': {
56+
border: 'none !important'
57+
},
58+
'@keyframes fadeIn': {
59+
'from': { opacity: '0', transform: 'translateY(-2px)' },
60+
'to': { opacity: '1', transform: 'translateY(0)' }
61+
}
62+
})
63+
64+
return {
65+
showFunctionHelpHover,
66+
functionHelpTheme
67+
}
68+
}

0 commit comments

Comments
 (0)