@@ -10,6 +10,8 @@ import { config, sendAuthenticatedMessage } from "./crypto";
1010
1111const editorDefaultText = "<---- Select a service instance to start editing it in here" ;
1212const editorCreateText = "<---- Create a new service instance on the left and then you can edit it in here" ;
13+ const editorInvalidServiceText = "!!!!! Service of this instance couldn't be found." ;
14+ const editorNotConfigurableText = "----- This service cannot be configured." ;
1315
1416document . addEventListener ( "DOMContentLoaded" , ( ) => {
1517 config . onChange ( ( ) => {
@@ -59,18 +61,12 @@ export function onInstanceSelectChange(value: string): void {
5961 showNotice ( undefined ) ;
6062 switch ( value ) {
6163 case "new" :
62- editor ?. updateOptions ( {
63- readOnly : true ,
64- } ) ;
65- editor ?. setModel ( monaco . editor . createModel ( editorCreateText , "text" ) ) ;
64+ showInMonaco ( "text" , true , editorCreateText ) ;
6665 setCreateInputs ( true , false ) ;
6766 inputInstanceName . value = "" ;
6867 break ;
6968 case "select" :
70- editor ?. updateOptions ( {
71- readOnly : true ,
72- } ) ;
73- editor ?. setModel ( monaco . editor . createModel ( editorDefaultText , "text" ) ) ;
69+ showInMonaco ( "text" , true , editorDefaultText ) ;
7470 setCreateInputs ( false , false ) ;
7571 break ;
7672 default :
@@ -82,30 +78,15 @@ function showConfig(value: string) {
8278 const inst = config . data ?. instances [ value ] ;
8379 const service = config . data ?. services . find ( ( svc ) => svc . serviceType === inst ?. serviceType ) ;
8480
85- editor ?. updateOptions ( {
86- readOnly : false ,
87- } ) ;
88-
89- // Get rid of old models, as they have to be unique and we may add the same again
90- monaco . editor . getModels ( ) . forEach ( ( m ) => m . dispose ( ) ) ;
81+ if ( ! service ) {
82+ showInMonaco ( "text" , true , editorInvalidServiceText ) ;
83+ } else if ( service . requiresNoConfig ) {
84+ showInMonaco ( "text" , true , editorNotConfigurableText ) ;
85+ } else {
86+ const jsonString = JSON . stringify ( inst ?. config || { } , null , 4 ) ;
87+ showInMonaco ( "json" , false , jsonString , service ?. schema ) ;
88+ }
9189
92- // This model uri can be completely made up as long the uri in the schema matches with the one in the language model.
93- const modelUri = monaco . Uri . parse ( `mem://nodecg-io/${ inst ?. serviceType } .json` ) ;
94- monaco . languages . json . jsonDefaults . setDiagnosticsOptions ( {
95- validate : service ?. schema !== undefined ,
96- schemas :
97- service ?. schema !== undefined
98- ? [
99- {
100- uri : modelUri . toString ( ) ,
101- fileMatch : [ modelUri . toString ( ) ] ,
102- schema : objectDeepCopy ( service ?. schema ) ,
103- } ,
104- ]
105- : [ ] ,
106- } ) ;
107- const model = monaco . editor . createModel ( JSON . stringify ( inst ?. config || { } , null , 4 ) , "json" , modelUri ) ;
108- editor ?. setModel ( model ) ;
10990 setCreateInputs ( false , true ) ;
11091}
11192
@@ -246,3 +227,39 @@ export function showNotice(msg: string | undefined): void {
246227 spanInstanceNotice . innerText = msg !== undefined ? msg : "" ;
247228 }
248229}
230+
231+ function showInMonaco (
232+ type : "text" | "json" ,
233+ readOnly : boolean ,
234+ content : string ,
235+ schema ?: Record < string , unknown > ,
236+ ) : void {
237+ editor ?. updateOptions ( { readOnly } ) ;
238+
239+ // JSON Schema stuff
240+ // Get rid of old models, as they have to be unique and we may add the same again
241+ monaco . editor . getModels ( ) . forEach ( ( m ) => m . dispose ( ) ) ;
242+
243+ // This model uri can be completely made up as long the uri in the schema matches with the one in the language model.
244+ const modelUri = monaco . Uri . parse ( `mem://nodecg-io/selectedServiceSchema.json` ) ;
245+
246+ monaco . languages . json . jsonDefaults . setDiagnosticsOptions (
247+ schema
248+ ? {
249+ validate : true ,
250+ schemas : [
251+ {
252+ uri : modelUri . toString ( ) ,
253+ fileMatch : [ modelUri . toString ( ) ] ,
254+ schema : objectDeepCopy ( schema ) ,
255+ } ,
256+ ] ,
257+ }
258+ : {
259+ validate : false , // if not set we disable validation again.
260+ schemas : [ ] ,
261+ } ,
262+ ) ;
263+
264+ editor ?. setModel ( monaco . editor . createModel ( content , type ) ) ;
265+ }
0 commit comments