@@ -14,6 +14,7 @@ import xmlFormat from 'xml-formatter';
1414import { EditorView , basicSetup } from 'codemirror' ;
1515import { EditorState } from '@codemirror/state' ;
1616import { xml } from '@codemirror/lang-xml' ;
17+ import { json } from '@codemirror/lang-json' ;
1718
1819const mapserverUrl = import . meta. env . VITE_MAPSERVER_BASE_URL ;
1920const mapfilesPath = import . meta. env . VITE_MAPFILES_PATH ;
@@ -33,6 +34,7 @@ const demoLayers = [
3334 { id : 'notched-rect' , type : 'point' } ,
3435 { id : 'square-spaced' , type : 'polygon' } ,
3536 { id : 'wave' , type : 'polygon' } ,
37+ { id : 'fills' , type : 'polygon' } ,
3638] ;
3739
3840const sldParser = new SLDParser ( ) ;
@@ -72,6 +74,7 @@ function setText(view, text) {
7274const classView = createViewer ( 'class-text' , mapfileLanguage ) ;
7375const symView = createViewer ( 'sym-text' , mapfileLanguage ) ;
7476const sldView = createViewer ( 'sld-text' , xml ( ) ) ;
77+ const geostylerView = createViewer ( 'geostyler-text' , json ( ) ) ;
7578
7679function showSld ( text ) {
7780 let formatted = text ;
@@ -124,6 +127,7 @@ async function fetchSld(layerConfig) {
124127
125128/**
126129 * Convert an SLD to an OpenLayers style with GeoStyler, and apply it.
130+ * Returns the GeoStyler style the OpenLayers style was created from.
127131 */
128132async function applySld ( sldXmlString , vectorLayer ) {
129133 const gs = await sldParser . readStyle ( sldXmlString ) ;
@@ -148,6 +152,7 @@ async function applySld(sldXmlString, vectorLayer) {
148152 console . log ( `OpenLayers style for layer ${ vectorLayer . get ( 'name' ) } :` , ol . output ) ;
149153
150154 vectorLayer . setStyle ( ol . output ) ;
155+ return gs . output ;
151156}
152157
153158function createVectorLayer ( layerConfig ) {
@@ -206,19 +211,20 @@ function main() {
206211 const classFile = `${ layerConfig . id } .class` ;
207212 const symFile = `${ layerConfig . id } .sym` ;
208213
209- const [ classText , symText , sld ] = await Promise . all ( [
214+ const [ classText , symText , { sld, geostylerStyle } ] = await Promise . all ( [
210215 fetchText ( `${ includesUrl } ${ classFile } ` )
211216 . catch ( ( error ) => `# Could not load ${ classFile } : ${ error . message } ` ) ,
212217 fetchOptionalText ( `${ includesUrl } ${ symFile } ` )
213218 . catch ( ( ) => null ) ,
214219 fetchSld ( layerConfig )
215220 . then ( async ( sldXmlString ) => {
216- await applySld ( sldXmlString , vectorLayer ) ;
217- return sldXmlString ;
221+ const style = await applySld ( sldXmlString , vectorLayer ) ;
222+ return { sld : sldXmlString , geostylerStyle : style } ;
218223 } )
219224 . catch ( ( error ) => {
220225 console . error ( `Failed to style layer ${ layerConfig . id } ` , error ) ;
221- return `Could not load or apply the SLD: ${ error . message } ` ;
226+ const message = `Could not load or apply the SLD: ${ error . message } ` ;
227+ return { sld : message , geostylerStyle : null } ;
222228 } ) ,
223229 ] ) ;
224230
@@ -235,8 +241,24 @@ function main() {
235241 setText ( symView , symText ?? '' ) ;
236242
237243 showSld ( sld ) ;
244+ setText ( geostylerView , geostylerStyle
245+ ? JSON . stringify ( geostylerStyle , null , 2 )
246+ : '// No GeoStyler style: see the SLD tab for the error' ) ;
238247
239248 mapRight . getLayers ( ) . clear ( ) ;
249+
250+ // Tabs in the bottom-right panel: show one viewer at a time
251+ const tabs = document . querySelectorAll ( '.tab' ) ;
252+ tabs . forEach ( ( tab ) => {
253+ tab . addEventListener ( 'click' , ( ) => {
254+ tabs . forEach ( ( t ) => {
255+ const active = t === tab ;
256+ t . classList . toggle ( 'active' , active ) ;
257+ document . getElementById ( t . dataset . tab ) . hidden = ! active ;
258+ } ) ;
259+ } ) ;
260+ } ) ;
261+
240262 mapRight . addLayer ( baseLayer ( ) ) ;
241263 mapRight . addLayer ( vectorLayer ) ;
242264 }
0 commit comments