mirror of
https://github.com/1Panel-dev/MaxKB.git
synced 2025-12-30 01:32:49 +00:00
refactor: implement debounced async linting in CodemirrorEditor
--bug=1057664 --user=刘瑞斌 【工具】创建工具,工具内容输入特定的内容页面会崩溃 https://www.tapd.cn/62980211/s/1726594
This commit is contained in:
parent
217e3ce58d
commit
8625dd543a
|
|
@ -46,6 +46,7 @@ import { python } from '@codemirror/lang-python'
|
|||
import { oneDark } from '@codemirror/theme-one-dark'
|
||||
import { linter, type Diagnostic } from '@codemirror/lint'
|
||||
import { loadSharedApi } from '@/utils/dynamics-api/shared-api'
|
||||
import debounce from 'lodash/debounce'
|
||||
|
||||
defineOptions({ name: 'CodemirrorEditor' })
|
||||
|
||||
|
|
@ -86,27 +87,31 @@ function getRangeFromLineAndColumn(state: any, line: number, column: number, end
|
|||
}
|
||||
}
|
||||
|
||||
const asyncLint = debounce(async (doc: string) => {
|
||||
const res = await loadSharedApi({ type: 'tool', systemType: apiType.value }).postPylint(doc)
|
||||
return res.data
|
||||
}, 500)
|
||||
|
||||
const regexpLinter = linter(async (view) => {
|
||||
const diagnostics: Diagnostic[] = []
|
||||
await loadSharedApi({ type: 'tool', systemType: apiType.value })
|
||||
.postPylint(view.state.doc.toString())
|
||||
.then((ok: any) => {
|
||||
ok.data.forEach((element: any) => {
|
||||
const range = getRangeFromLineAndColumn(
|
||||
view.state,
|
||||
element.line,
|
||||
element.column,
|
||||
element.endColumn,
|
||||
)
|
||||
|
||||
diagnostics.push({
|
||||
from: range.form,
|
||||
to: range.to,
|
||||
severity: element.type,
|
||||
message: element.message,
|
||||
})
|
||||
})
|
||||
const lintResults = await asyncLint(view.state.doc.toString())
|
||||
if (!lintResults || lintResults.length === 0) {
|
||||
return diagnostics
|
||||
}
|
||||
lintResults.forEach((element: any) => {
|
||||
const range = getRangeFromLineAndColumn(
|
||||
view.state,
|
||||
element.line,
|
||||
element.column,
|
||||
element.endColumn,
|
||||
)
|
||||
diagnostics.push({
|
||||
from: range.form,
|
||||
to: range.to,
|
||||
severity: element.type,
|
||||
message: element.message,
|
||||
})
|
||||
})
|
||||
return diagnostics
|
||||
})
|
||||
const extensions = [python(), regexpLinter, oneDark]
|
||||
|
|
|
|||
Loading…
Reference in New Issue