Skip to content

Commit ee27297

Browse files
committed
fix: code editor yaml & json validation
1 parent 9433bb6 commit ee27297

File tree

5 files changed

+312
-80
lines changed

5 files changed

+312
-80
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@devtron-labs/devtron-fe-common-lib",
3-
"version": "1.2.5-beta-3",
3+
"version": "1.2.5-beta-4",
44
"description": "Supporting common component library",
55
"type": "module",
66
"main": "dist/index.js",

src/Common/CodeEditor/CodeEditor.tsx

Lines changed: 75 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,16 @@
1515
*/
1616

1717
import React, { useEffect, useMemo, useReducer, useRef, useState } from 'react'
18+
import * as monaco from 'monaco-editor'
19+
import { configureMonacoYaml } from 'monaco-yaml'
1820
import MonacoEditor, { MonacoDiffEditor } from 'react-monaco-editor'
1921
import YAML from 'yaml'
2022
import ReactGA from 'react-ga4'
21-
import * as monaco from 'monaco-editor/esm/vs/editor/editor.api'
22-
import { configureMonacoYaml } from 'monaco-yaml'
2323

2424
import { ReactComponent as ICWarningY5 } from '@Icons/ic-warning-y5.svg'
2525
import { ReactComponent as Info } from '../../Assets/Icon/ic-info-filled.svg'
2626
import { ReactComponent as ErrorIcon } from '../../Assets/Icon/ic-error-exclamation.svg'
2727
import './codeEditor.scss'
28-
import 'monaco-editor'
2928

3029
import { YAMLStringify, cleanKubeManifest, useJsonYaml } from '../Helper'
3130
import { useWindowSize } from '../Hooks'
@@ -43,6 +42,7 @@ import {
4342
} from './types'
4443
import { CodeEditorReducer, initialState } from './CodeEditor.reducer'
4544
import { MODES } from '../Constants'
45+
import jsonSchema from './jsonSchema.json' assert { type: 'json' }
4646

4747
const CodeEditorContext = React.createContext(null)
4848

@@ -56,6 +56,66 @@ function useCodeEditorContext() {
5656

5757
const INITIAL_HEIGHT_WHEN_DYNAMIC_HEIGHT = 100
5858

59+
const yamlConfig = configureMonacoYaml(monaco, { validate: true, isKubernetes: true, completion: false })
60+
61+
monaco.languages.json.jsonDefaults.setDiagnosticsOptions({
62+
enableSchemaRequest: true,
63+
schemas: [
64+
{
65+
uri: 'https://json-schema.org/draft-07/schema#',
66+
fileMatch: ['*'],
67+
schema: jsonSchema,
68+
},
69+
],
70+
validate: true,
71+
trailingCommas: 'error',
72+
schemaValidation: 'error',
73+
})
74+
75+
monaco.editor.defineTheme(CodeEditorThemesKeys.vsDarkDT, {
76+
base: 'vs-dark',
77+
inherit: true,
78+
rules: [
79+
// @ts-ignore
80+
{ background: '#0B0F22' },
81+
],
82+
colors: {
83+
'editor.background': '#0B0F22',
84+
},
85+
})
86+
87+
monaco.editor.defineTheme(CodeEditorThemesKeys.networkStatusInterface, {
88+
base: 'vs-dark',
89+
inherit: true,
90+
rules: [
91+
// @ts-ignore
92+
{ background: '#1A1A1A' },
93+
],
94+
colors: {
95+
'editor.background': '#1A1A1A',
96+
},
97+
})
98+
99+
monaco.editor.defineTheme(CodeEditorThemesKeys.deleteDraft, {
100+
base: 'vs',
101+
inherit: true,
102+
rules: [],
103+
colors: {
104+
'diffEditor.insertedTextBackground': '#ffd4d1',
105+
'diffEditor.removedTextBackground': '#ffffff33',
106+
},
107+
})
108+
109+
monaco.editor.defineTheme(CodeEditorThemesKeys.unpublished, {
110+
base: 'vs',
111+
inherit: true,
112+
rules: [],
113+
colors: {
114+
'diffEditor.insertedTextBackground': '#eaf1dd',
115+
'diffEditor.removedTextBackground': '#ffffff33',
116+
},
117+
})
118+
59119
const CodeEditor: React.FC<CodeEditorInterface> & CodeEditorComposition = React.memo(
60120
({
61121
value,
@@ -105,49 +165,6 @@ const CodeEditor: React.FC<CodeEditorInterface> & CodeEditorComposition = React.
105165
* */
106166
const onChangeRef = useRef(onChange)
107167
onChangeRef.current = onChange
108-
monaco.editor.defineTheme(CodeEditorThemesKeys.vsDarkDT, {
109-
base: 'vs-dark',
110-
inherit: true,
111-
rules: [
112-
// @ts-ignore
113-
{ background: '#0B0F22' },
114-
],
115-
colors: {
116-
'editor.background': '#0B0F22',
117-
},
118-
})
119-
120-
monaco.editor.defineTheme(CodeEditorThemesKeys.networkStatusInterface, {
121-
base: 'vs-dark',
122-
inherit: true,
123-
rules: [
124-
// @ts-ignore
125-
{ background: '#1A1A1A' },
126-
],
127-
colors: {
128-
'editor.background': '#1A1A1A',
129-
},
130-
})
131-
132-
monaco.editor.defineTheme(CodeEditorThemesKeys.deleteDraft, {
133-
base: 'vs',
134-
inherit: true,
135-
rules: [],
136-
colors: {
137-
'diffEditor.insertedTextBackground': '#ffd4d1',
138-
'diffEditor.removedTextBackground': '#ffffff33',
139-
},
140-
})
141-
142-
monaco.editor.defineTheme(CodeEditorThemesKeys.unpublished, {
143-
base: 'vs',
144-
inherit: true,
145-
rules: [],
146-
colors: {
147-
'diffEditor.insertedTextBackground': '#eaf1dd',
148-
'diffEditor.removedTextBackground': '#ffffff33',
149-
},
150-
})
151168

152169
useEffect(() => {
153170
const rule = !disableSearch
@@ -222,23 +239,21 @@ const CodeEditor: React.FC<CodeEditorInterface> & CodeEditorComposition = React.
222239
}, [height, contentHeight, adjustEditorHeightToContent])
223240

224241
useEffect(() => {
225-
if (!validatorSchema) {
226-
return
227-
}
228-
const config = configureMonacoYaml(monaco, {
242+
yamlConfig.update({
229243
enableSchemaRequest: true,
244+
validate: true,
230245
isKubernetes,
231-
schemas: [
232-
{
233-
uri: `https://github.com/devtron-labs/devtron/tree/main/scripts/devtron-reference-helm-charts/reference-chart_${chartVersion}/schema.json`, // id of the first schema
234-
fileMatch: ['*'], // associate with our model
235-
schema: validatorSchema,
236-
},
237-
],
246+
completion: false,
247+
schemas: validatorSchema
248+
? [
249+
{
250+
uri: `https://github.com/devtron-labs/devtron/tree/main/scripts/devtron-reference-helm-charts/reference-chart_${chartVersion}/schema.json`, // id of the first schema
251+
fileMatch: ['*'], // associate with our model
252+
schema: validatorSchema,
253+
},
254+
]
255+
: [],
238256
})
239-
return () => {
240-
config.dispose()
241-
}
242257
// eslint-disable-next-line react-hooks/exhaustive-deps
243258
}, [validatorSchema, chartVersion])
244259
useEffect(() => {

0 commit comments

Comments
 (0)