diff --git a/example/src/App.svelte b/example/src/App.svelte index 0e2b07b..83d2f4c 100644 --- a/example/src/App.svelte +++ b/example/src/App.svelte @@ -13,6 +13,7 @@ constructMapboxStyleFromEsri, fetchEsriStyleJson, resolveEsriRelativePaths, + constructMapboxStyleFromEsriAbsolute, } from "@esri-style-ft-mapbox-style/esriToMapbox"; import { writable, get } from "svelte/store"; @@ -29,9 +30,22 @@ // mapboxStyleJson.set(`Error: ${(error as Error).message}`); } } - function handleEditorChange(event: CustomEvent) { + function handleEsriEditorChange(event: CustomEvent) { esriStyleJson.set(event.detail); } + function handleMaplibreEditorChange(event: CustomEvent) { + maplibreStyleJson.set(event.detail); + } + + async function convertEsritoMaplibreStyle(): Promise { + const theFetchedMaplibreStyleJson = + await constructMapboxStyleFromEsriAbsolute( + urlInput, + JSON.parse(get(esriStyleJson)) + ); + + maplibreStyleJson.set(JSON.stringify(theFetchedMaplibreStyleJson, null, 2)); + } // Watch for changes in mapboxStyleJson and update maplibreStyle $: { @@ -50,7 +64,7 @@
@@ -72,8 +86,11 @@
+ -
@@ -81,11 +98,11 @@
- + on:change={handleMaplibreEditorChange} + >
{ + // Normalize the base URL to ensure it does not end with a slash + const normalizedBaseUrl = baseUrl.endsWith('/') ? baseUrl.slice(0, -1) : baseUrl; + + // Fetch the Esri style JSON if it's not provided + if (!esriStyleJson) { + esriStyleJson = await fetchEsriStyleJson(normalizedBaseUrl); + } + + // Adjust the 'sources' to use the correct tile URL pattern while preserving other properties + if (esriStyleJson.sources) { + for (const sourceKey in esriStyleJson.sources) { + const source = esriStyleJson.sources[sourceKey]; + if (source.type === 'vector' && source.url) { + // Preserve all source properties + const updatedSource = { ...source }; + // Update tiles URL, remove 'url' property + updatedSource.tiles = [`${source.url}tile/{z}/{y}/{x}.pbf`]; + delete updatedSource.url; + + + // Update the source with the new properties + esriStyleJson.sources[sourceKey] = updatedSource; + } + } + } + + + return esriStyleJson; +} + + -export { constructMapboxStyleFromEsri, fetchEsriStyleJson, resolveEsriRelativePaths }; +export { constructMapboxStyleFromEsri, fetchEsriStyleJson, resolveEsriRelativePaths, constructMapboxStyleFromEsriAbsolute };