1
- import { type JSONSchemaForSchemaStoreOrgCatalogFiles } from '@schemastore/schema-catalog'
2
- import { editor , languages , MarkerSeverity , type Position , Range , Uri } from 'monaco-editor'
3
- import * as monaco from 'monaco-editor'
4
- import { ILanguageFeaturesService } from 'monaco-editor/esm/vs/editor/common/services/languageFeatures.js'
5
- import { OutlineModel } from 'monaco-editor/esm/vs/editor/contrib/documentSymbols/browser/outlineModel.js'
6
- import { StandaloneServices } from 'monaco-editor/esm/vs/editor/standalone/browser/standaloneServices.js'
7
- import { configureMonacoYaml , type SchemasSettings } from 'monaco-yaml'
1
+ import { type JSONSchemaForSchemaStoreOrgCatalogFiles } from "@schemastore/schema-catalog" ;
2
+ import {
3
+ editor ,
4
+ languages ,
5
+ MarkerSeverity ,
6
+ type Position ,
7
+ Range ,
8
+ Uri ,
9
+ } from "monaco-editor" ;
10
+ import * as monaco from "monaco-editor" ;
11
+ import { ILanguageFeaturesService } from "monaco-editor/esm/vs/editor/common/services/languageFeatures.js" ;
12
+ import { OutlineModel } from "monaco-editor/esm/vs/editor/contrib/documentSymbols/browser/outlineModel.js" ;
13
+ import { StandaloneServices } from "monaco-editor/esm/vs/editor/standalone/browser/standaloneServices.js" ;
14
+ import { configureMonacoYaml , type SchemasSettings } from "monaco-yaml" ;
8
15
9
- import ' ./index.css'
10
- import schema from ' ./schema.json'
16
+ import " ./index.css" ;
17
+ import schema from " ./schema.json" ;
11
18
12
19
window . MonacoEnvironment = {
13
20
getWorker ( moduleId , label ) {
14
21
switch ( label ) {
15
- case 'editorWorkerService' :
16
- return new Worker ( new URL ( 'monaco-editor/esm/vs/editor/editor.worker' , import . meta. url ) )
17
- case 'yaml' :
18
- return new Worker ( new URL ( 'monaco-yaml/yaml.worker' , import . meta. url ) )
22
+ case "editorWorkerService" :
23
+ return new Worker (
24
+ new URL ( "monaco-editor/esm/vs/editor/editor.worker" , import . meta. url ) ,
25
+ ) ;
26
+ case "yaml" :
27
+ return new Worker ( new URL ( "monaco-yaml/yaml.worker" , import . meta. url ) ) ;
19
28
default :
20
- throw new Error ( `Unknown label ${ label } ` )
29
+ throw new Error ( `Unknown label ${ label } ` ) ;
21
30
}
22
- }
23
- }
31
+ } ,
32
+ } ;
24
33
25
34
const defaultSchema : SchemasSettings = {
26
35
uri : window . location . href ,
27
36
schema,
28
- fileMatch : [ ' plotly-graph.yaml' ]
29
- }
37
+ fileMatch : [ " plotly-graph.yaml" ] ,
38
+ } ;
30
39
31
40
const monacoYaml = configureMonacoYaml ( monaco , {
32
- schemas : [ defaultSchema ]
33
- } )
41
+ schemas : [ defaultSchema ] ,
42
+ completion : true ,
43
+ format : true ,
44
+ hover : true ,
45
+ validate : true ,
46
+ } ) ;
34
47
35
- function showToast ( message :string ) {
36
- const toastContainer = document . getElementById ( ' toast-container' ) ! ;
37
- const toast = document . createElement ( ' div' ) ;
38
- toast . className = ' toast' ;
48
+ function showToast ( message : string ) {
49
+ const toastContainer = document . getElementById ( " toast-container" ) ! ;
50
+ const toast = document . createElement ( " div" ) ;
51
+ toast . className = " toast" ;
39
52
toast . innerText = message ;
40
53
toastContainer . appendChild ( toast ) ;
41
54
setTimeout ( ( ) => {
42
- toast . classList . add ( ' show' ) ;
55
+ toast . classList . add ( " show" ) ;
43
56
} , 100 ) ;
44
57
setTimeout ( ( ) => {
45
- toast . classList . remove ( ' show' ) ;
46
- setTimeout ( ( ) => toast . remove ( ) , 500 ) ; // Remove after animation
58
+ toast . classList . remove ( " show" ) ;
59
+ setTimeout ( ( ) => toast . remove ( ) , 500 ) ; // Remove after animation
47
60
} , 3000 ) ;
48
61
}
49
62
50
- if ( localStorage [ "plotly-graph" ] ) showToast ( "Recovered yaml from local storage" )
63
+ if ( localStorage [ "plotly-graph" ] )
64
+ showToast ( "Recovered yaml from local storage" ) ;
51
65
52
- const value = localStorage [ "plotly-graph" ] ||
53
- `type: custom:plotly-graph
66
+ const value =
67
+ localStorage [ "plotly-graph" ] ||
68
+ `type: custom:plotly-graph
54
69
entities:
55
70
- entity: sensor.x
56
71
- entity: sensor.y
57
- `
72
+ ` ;
58
73
59
-
60
-
61
- const ed = editor . create ( document . getElementById ( 'editor' ) ! , {
74
+ const ed = editor . create ( document . getElementById ( "editor" ) ! , {
62
75
automaticLayout : true ,
63
- model : editor . createModel ( value , 'yaml' , Uri . parse ( 'plotly-graph.yaml' ) ) ,
64
- theme : window . matchMedia ( '(prefers-color-scheme: dark)' ) . matches ? 'vs-dark' : 'vs-light' ,
76
+ model : editor . createModel ( value , "yaml" , Uri . parse ( "plotly-graph.yaml" ) ) ,
77
+ theme : window . matchMedia ( "(prefers-color-scheme: dark)" ) . matches
78
+ ? "vs-dark"
79
+ : "vs-light" ,
65
80
quickSuggestions : {
66
81
other : true ,
67
82
comments : false ,
68
- strings : true
83
+ strings : true ,
69
84
} ,
70
- formatOnType : true
71
- } )
85
+ formatOnType : true ,
86
+ } ) ;
72
87
73
88
/**
74
89
* Get the document symbols that contain the given position.
@@ -82,79 +97,88 @@ const ed = editor.create(document.getElementById('editor')!, {
82
97
*/
83
98
function * iterateSymbols (
84
99
symbols : languages . DocumentSymbol [ ] ,
85
- position : Position
100
+ position : Position ,
86
101
) : Iterable < languages . DocumentSymbol > {
87
102
for ( const symbol of symbols ) {
88
103
if ( Range . containsPosition ( symbol . range , position ) ) {
89
- yield symbol
104
+ yield symbol ;
90
105
if ( symbol . children ) {
91
- yield * iterateSymbols ( symbol . children , position )
106
+ yield * iterateSymbols ( symbol . children , position ) ;
92
107
}
93
108
}
94
109
}
95
110
}
96
111
97
- ed . onDidChangeModelContent ( ( ) => {
98
- localStorage [ "plotly-graph" ] = ed . getValue ( )
99
-
100
- } )
112
+ ed . onDidChangeModelContent ( ( ) => {
113
+ localStorage [ "plotly-graph" ] = ed . getValue ( ) ;
114
+ } ) ;
101
115
102
116
ed . onDidChangeCursorPosition ( async ( event ) => {
103
- const breadcrumbs = document . getElementById ( 'breadcrumbs' ) !
104
- const { documentSymbolProvider } = StandaloneServices . get ( ILanguageFeaturesService )
105
- const outline = await OutlineModel . create ( documentSymbolProvider , ed . getModel ( ) ! )
106
- const symbols = outline . asListOfDocumentSymbols ( )
117
+ const breadcrumbs = document . getElementById ( "breadcrumbs" ) ! ;
118
+ const { documentSymbolProvider } = StandaloneServices . get (
119
+ ILanguageFeaturesService ,
120
+ ) ;
121
+ const outline = await OutlineModel . create (
122
+ documentSymbolProvider ,
123
+ ed . getModel ( ) ! ,
124
+ ) ;
125
+ const symbols = outline . asListOfDocumentSymbols ( ) ;
107
126
while ( breadcrumbs . lastChild ) {
108
- breadcrumbs . lastChild . remove ( )
127
+ breadcrumbs . lastChild . remove ( ) ;
109
128
}
110
129
for ( const symbol of iterateSymbols ( symbols , event . position ) ) {
111
- const breadcrumb = document . createElement ( ' span' )
112
- breadcrumb . setAttribute ( ' role' , ' button' )
113
- breadcrumb . classList . add ( ' breadcrumb' )
114
- breadcrumb . textContent = symbol . name
115
- breadcrumb . title = symbol . detail
130
+ const breadcrumb = document . createElement ( " span" ) ;
131
+ breadcrumb . setAttribute ( " role" , " button" ) ;
132
+ breadcrumb . classList . add ( " breadcrumb" ) ;
133
+ breadcrumb . textContent = symbol . name ;
134
+ breadcrumb . title = symbol . detail ;
116
135
if ( symbol . kind === languages . SymbolKind . Array ) {
117
- breadcrumb . classList . add ( ' array' )
136
+ breadcrumb . classList . add ( " array" ) ;
118
137
} else if ( symbol . kind === languages . SymbolKind . Module ) {
119
- breadcrumb . classList . add ( ' object' )
138
+ breadcrumb . classList . add ( " object" ) ;
120
139
}
121
- breadcrumb . addEventListener ( ' click' , ( ) => {
140
+ breadcrumb . addEventListener ( " click" , ( ) => {
122
141
ed . setPosition ( {
123
142
lineNumber : symbol . range . startLineNumber ,
124
- column : symbol . range . startColumn
125
- } )
126
- ed . focus ( )
127
- } )
128
- breadcrumbs . append ( breadcrumb )
143
+ column : symbol . range . startColumn ,
144
+ } ) ;
145
+ ed . focus ( ) ;
146
+ } ) ;
147
+ breadcrumbs . append ( breadcrumb ) ;
129
148
}
130
- } )
149
+ } ) ;
131
150
132
151
editor . onDidChangeMarkers ( ( [ resource ] ) => {
133
- const problems = document . getElementById ( ' problems' ) !
134
- const markers = editor . getModelMarkers ( { resource } )
152
+ const problems = document . getElementById ( " problems" ) ! ;
153
+ const markers = editor . getModelMarkers ( { resource } ) ;
135
154
while ( problems . lastChild ) {
136
- problems . lastChild . remove ( )
155
+ problems . lastChild . remove ( ) ;
137
156
}
138
157
for ( const marker of markers ) {
139
158
if ( marker . severity === MarkerSeverity . Hint ) {
140
- continue
159
+ continue ;
141
160
}
142
- const wrapper = document . createElement ( ' div' )
143
- wrapper . setAttribute ( ' role' , ' button' )
144
- const codicon = document . createElement ( ' div' )
145
- const text = document . createElement ( ' div' )
146
- wrapper . classList . add ( ' problem' )
161
+ const wrapper = document . createElement ( " div" ) ;
162
+ wrapper . setAttribute ( " role" , " button" ) ;
163
+ const codicon = document . createElement ( " div" ) ;
164
+ const text = document . createElement ( " div" ) ;
165
+ wrapper . classList . add ( " problem" ) ;
147
166
codicon . classList . add (
148
- 'codicon' ,
149
- marker . severity === MarkerSeverity . Warning ? 'codicon-warning' : 'codicon-error'
150
- )
151
- text . classList . add ( 'problem-text' )
152
- text . textContent = marker . message
153
- wrapper . append ( codicon , text )
154
- wrapper . addEventListener ( 'click' , ( ) => {
155
- ed . setPosition ( { lineNumber : marker . startLineNumber , column : marker . startColumn } )
156
- ed . focus ( )
157
- } )
158
- problems . append ( wrapper )
167
+ "codicon" ,
168
+ marker . severity === MarkerSeverity . Warning
169
+ ? "codicon-warning"
170
+ : "codicon-error" ,
171
+ ) ;
172
+ text . classList . add ( "problem-text" ) ;
173
+ text . textContent = marker . message ;
174
+ wrapper . append ( codicon , text ) ;
175
+ wrapper . addEventListener ( "click" , ( ) => {
176
+ ed . setPosition ( {
177
+ lineNumber : marker . startLineNumber ,
178
+ column : marker . startColumn ,
179
+ } ) ;
180
+ ed . focus ( ) ;
181
+ } ) ;
182
+ problems . append ( wrapper ) ;
159
183
}
160
- } )
184
+ } ) ;
0 commit comments