Skip to content

Commit fe1b0de

Browse files
author
lukaw3d
committed
Refactor validation in CodeDisplay
1 parent 6a07170 commit fe1b0de

1 file changed

Lines changed: 38 additions & 37 deletions

File tree

src/components/CodeDisplay/index.tsx

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,16 @@ export const CodeDisplay: FC<CodeDisplayProps> = ({ data, className, readOnly =
6262
})
6363
}
6464

65-
const highlightErrorLogs = (value: string) => {
65+
const highlightErrorLogs = (newData: string | undefined) => {
6666
const monacoInstance = monacoInstanceRef.current
67-
if (!monacoInstance || !editorRef.current || value === undefined) {
67+
if (!monacoInstance || !editorRef.current || newData === undefined) {
6868
return
6969
}
7070
const model = editorRef.current.getModel()
7171
if (!model) return
7272

7373
const markers: monaco.editor.IMarkerData[] = []
74-
for (const [i, line] of value.split('\n').entries()) {
74+
for (const [i, line] of newData.split('\n').entries()) {
7575
if (
7676
line.includes('"err"') ||
7777
line.toLowerCase().includes('error') ||
@@ -91,6 +91,37 @@ export const CodeDisplay: FC<CodeDisplayProps> = ({ data, className, readOnly =
9191
monacoInstance.editor.setModelMarkers(model, 'highlightErrorLogs', markers)
9292
}
9393

94+
const highlightYamlErrors = (newData: string | undefined) => {
95+
const monacoInstance = monacoInstanceRef.current
96+
if (!monacoInstance || !editorRef.current || newData === undefined) {
97+
return
98+
}
99+
const model = editorRef.current.getModel()
100+
if (!model) return
101+
102+
const markers: monaco.editor.IMarkerData[] = []
103+
104+
try {
105+
yaml.parse(newData)
106+
} catch (e) {
107+
const yamlError = e as { message: string; linePos: { line: number; col: number }[] }
108+
if (yamlError.linePos && yamlError.linePos.length > 0) {
109+
const line = yamlError.linePos[0].line
110+
const column = yamlError.linePos[0].col
111+
markers.push({
112+
startLineNumber: line,
113+
endLineNumber: line,
114+
startColumn: column,
115+
endColumn: model.getLineMaxColumn(line),
116+
message: yamlError.message,
117+
severity: monacoInstance.MarkerSeverity.Error,
118+
})
119+
}
120+
}
121+
122+
monacoInstance.editor.setModelMarkers(model, 'highlightYamlErrors', markers)
123+
}
124+
94125
const handleEditorDidMount = (
95126
editor: monaco.editor.IStandaloneCodeEditor,
96127
monacoInstance: typeof monaco,
@@ -100,42 +131,12 @@ export const CodeDisplay: FC<CodeDisplayProps> = ({ data, className, readOnly =
100131
highlightErrorLogs(data)
101132
}
102133

103-
const handleEditorChange = (value: string | undefined) => {
104-
const monacoInstance = monacoInstanceRef.current
105-
if (!monacoInstance || !editorRef.current || value === undefined) {
106-
return
107-
}
108-
109-
const model = editorRef.current.getModel()
110-
if (!model) return
111-
112-
if (!readOnly) {
113-
const markers: monaco.editor.IMarkerData[] = []
114-
115-
try {
116-
yaml.parse(value)
117-
} catch (e) {
118-
const yamlError = e as { message: string; linePos: { line: number; col: number }[] }
119-
if (yamlError.linePos && yamlError.linePos.length > 0) {
120-
const line = yamlError.linePos[0].line
121-
const column = yamlError.linePos[0].col
122-
markers.push({
123-
startLineNumber: line,
124-
endLineNumber: line,
125-
startColumn: column,
126-
endColumn: model.getLineMaxColumn(line),
127-
message: yamlError.message,
128-
severity: monacoInstance.MarkerSeverity.Error,
129-
})
130-
}
131-
}
132-
133-
monacoInstance.editor.setModelMarkers(model, 'yaml-validator', markers)
134-
}
134+
const handleEditorChange = (newData: string | undefined) => {
135+
if (!readOnly) highlightYamlErrors(newData)
135136

136-
highlightErrorLogs(value)
137+
highlightErrorLogs(newData)
137138

138-
onChange?.(value)
139+
onChange?.(newData)
139140
}
140141

141142
return (

0 commit comments

Comments
 (0)