Skip to content

Commit 8625dd5

Browse files
committed
refactor: implement debounced async linting in CodemirrorEditor
--bug=1057664 --user=刘瑞斌 【工具】创建工具,工具内容输入特定的内容页面会崩溃 https://www.tapd.cn/62980211/s/1726594
1 parent 217e3ce commit 8625dd5

File tree

1 file changed

+23
-18
lines changed

1 file changed

+23
-18
lines changed

ui/src/components/codemirror-editor/index.vue

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import { python } from '@codemirror/lang-python'
4646
import { oneDark } from '@codemirror/theme-one-dark'
4747
import { linter, type Diagnostic } from '@codemirror/lint'
4848
import { loadSharedApi } from '@/utils/dynamics-api/shared-api'
49+
import debounce from 'lodash/debounce'
4950
5051
defineOptions({ name: 'CodemirrorEditor' })
5152
@@ -86,27 +87,31 @@ function getRangeFromLineAndColumn(state: any, line: number, column: number, end
8687
}
8788
}
8889
90+
const asyncLint = debounce(async (doc: string) => {
91+
const res = await loadSharedApi({ type: 'tool', systemType: apiType.value }).postPylint(doc)
92+
return res.data
93+
}, 500)
94+
8995
const regexpLinter = linter(async (view) => {
9096
const diagnostics: Diagnostic[] = []
91-
await loadSharedApi({ type: 'tool', systemType: apiType.value })
92-
.postPylint(view.state.doc.toString())
93-
.then((ok: any) => {
94-
ok.data.forEach((element: any) => {
95-
const range = getRangeFromLineAndColumn(
96-
view.state,
97-
element.line,
98-
element.column,
99-
element.endColumn,
100-
)
101-
102-
diagnostics.push({
103-
from: range.form,
104-
to: range.to,
105-
severity: element.type,
106-
message: element.message,
107-
})
108-
})
97+
const lintResults = await asyncLint(view.state.doc.toString())
98+
if (!lintResults || lintResults.length === 0) {
99+
return diagnostics
100+
}
101+
lintResults.forEach((element: any) => {
102+
const range = getRangeFromLineAndColumn(
103+
view.state,
104+
element.line,
105+
element.column,
106+
element.endColumn,
107+
)
108+
diagnostics.push({
109+
from: range.form,
110+
to: range.to,
111+
severity: element.type,
112+
message: element.message,
109113
})
114+
})
110115
return diagnostics
111116
})
112117
const extensions = [python(), regexpLinter, oneDark]

0 commit comments

Comments
 (0)