15
15
*/
16
16
17
17
import React , { useEffect , useMemo , useReducer , useRef , useState } from 'react'
18
+ import * as monaco from 'monaco-editor'
19
+ import { configureMonacoYaml } from 'monaco-yaml'
18
20
import MonacoEditor , { MonacoDiffEditor } from 'react-monaco-editor'
19
21
import YAML from 'yaml'
20
22
import ReactGA from 'react-ga4'
21
- import * as monaco from 'monaco-editor/esm/vs/editor/editor.api'
22
- import { configureMonacoYaml } from 'monaco-yaml'
23
23
24
24
import { ReactComponent as ICWarningY5 } from '@Icons/ic-warning-y5.svg'
25
25
import { ReactComponent as Info } from '../../Assets/Icon/ic-info-filled.svg'
26
26
import { ReactComponent as ErrorIcon } from '../../Assets/Icon/ic-error-exclamation.svg'
27
27
import './codeEditor.scss'
28
- import 'monaco-editor'
29
28
30
29
import { YAMLStringify , cleanKubeManifest , useJsonYaml } from '../Helper'
31
30
import { useWindowSize } from '../Hooks'
@@ -43,6 +42,7 @@ import {
43
42
} from './types'
44
43
import { CodeEditorReducer , initialState } from './CodeEditor.reducer'
45
44
import { MODES } from '../Constants'
45
+ import jsonSchema from './jsonSchema.json' assert { type : 'json' }
46
46
47
47
const CodeEditorContext = React . createContext ( null )
48
48
@@ -56,6 +56,66 @@ function useCodeEditorContext() {
56
56
57
57
const INITIAL_HEIGHT_WHEN_DYNAMIC_HEIGHT = 100
58
58
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
+
59
119
const CodeEditor : React . FC < CodeEditorInterface > & CodeEditorComposition = React . memo (
60
120
( {
61
121
value,
@@ -105,49 +165,6 @@ const CodeEditor: React.FC<CodeEditorInterface> & CodeEditorComposition = React.
105
165
* */
106
166
const onChangeRef = useRef ( onChange )
107
167
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
- } )
151
168
152
169
useEffect ( ( ) => {
153
170
const rule = ! disableSearch
@@ -222,23 +239,21 @@ const CodeEditor: React.FC<CodeEditorInterface> & CodeEditorComposition = React.
222
239
} , [ height , contentHeight , adjustEditorHeightToContent ] )
223
240
224
241
useEffect ( ( ) => {
225
- if ( ! validatorSchema ) {
226
- return
227
- }
228
- const config = configureMonacoYaml ( monaco , {
242
+ yamlConfig . update ( {
229
243
enableSchemaRequest : true ,
244
+ validate : true ,
230
245
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
+ : [ ] ,
238
256
} )
239
- return ( ) => {
240
- config . dispose ( )
241
- }
242
257
// eslint-disable-next-line react-hooks/exhaustive-deps
243
258
} , [ validatorSchema , chartVersion ] )
244
259
useEffect ( ( ) => {
0 commit comments