Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add style URL to the hash #62

Merged
merged 7 commits into from
Apr 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 25 additions & 7 deletions src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
validate,
} from '@mapbox/mapbox-gl-style-spec';
import { migrate as migrateMaplibre } from '@maplibre/maplibre-gl-style-spec';
import { onMount } from 'svelte';
import Fa from 'svelte-fa/src/fa.svelte';
import { faTrash, faDownload } from '@fortawesome/free-solid-svg-icons';
import { Circle } from 'svelte-loading-spinners';
Expand All @@ -32,6 +31,8 @@
export let style;
export let loadDefaultStyle = false;

let url;

let isLoading;
let loadingProgress;
loadingStore.subscribe(value => {
Expand All @@ -44,18 +45,26 @@
expandedLayers = value.layers;
});

onMount(() => {
const initializeVarsFromHash = () => {
const query = readQuery();
if (query.selectedTab) {
selectedTab = query.selectedTab;
} else {
selectedTab = 'fill';
}

if (query.url) {
url = decodeURIComponent(query.url);
}

if (loadDefaultStyle) {
loadStyleUrl('./style.json');
}
});
};

// Update variables before mount from hash so
// onMount works as expected in downstream components
initializeVarsFromHash();

const setExpandedLayers = style => {
const { layers } = style;
Expand All @@ -77,6 +86,7 @@
function getState() {
let state = {};
if (selectedTab) state.selectedTab = selectedTab;
if (url) state.url = url;
return state;
}

Expand Down Expand Up @@ -106,7 +116,7 @@
e.preventDefault();
}

function setStyle(nextStyle) {
function setStyle(nextStyle, nextUrl) {
// try to migrate w/ Mapbox, then try with MapLibre if that fails
try {
style = migrateMapbox(nextStyle);
Expand All @@ -120,6 +130,8 @@
}

style = convertStylesheetToRgb(style);
url = nextUrl;

// On dropping in a style, switch to the fill tab to refresh background layer state
handleTabChange({ detail: { tab: 'fill' } });
}
Expand All @@ -134,6 +146,7 @@

function clearStyle() {
style = undefined;
url = undefined;
displayLayersStore.set(displayLayersStoreInitialState);
svgStore.set(svgStoreInitialState);
}
Expand Down Expand Up @@ -171,12 +184,16 @@
}

const handleCustomUrl = e => {
let { style: nextStyle } = e.detail;
let { style: nextStyle, url: nextUrl } = e.detail;
if (stylesEqual(style, nextStyle)) return;
setStyle(nextStyle);
setStyle(nextStyle, nextUrl);
};

$: styleStore.set(style);
$: styleStore.set({ url, style });

$: if (url) {
updateQuery();
}
</script>

<main
Expand Down Expand Up @@ -208,6 +225,7 @@
<CustomUrlInput
on:styleload={handleCustomUrl}
activeStyle={style}
activeUrl={url}
disabled={isLoading && loadingProgress !== null}
/>
</div>
Expand Down
18 changes: 13 additions & 5 deletions src/CustomUrlInput.svelte
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<script>
import { onDestroy, onMount, createEventDispatcher } from 'svelte';
import { onDestroy, createEventDispatcher, onMount } from 'svelte';
import { fetchUrl } from './fetch-url';
import { shortcut } from './shortcut';
import { stylesEqual } from './styles/styles-equal';
import { mapboxGlAccessTokenStore } from './stores';
import { mapboxGlAccessTokenStore, styleStore } from './stores';

const dispatch = createEventDispatcher();

export let activeStyle;
export let activeUrl;
export let disabled = false;

let currentStyle;
Expand All @@ -26,6 +27,13 @@
allowPolling = false;
});

onMount(() => {
if (activeUrl) {
selectedUrl = activeUrl;
fetchStyle(activeUrl);
}
});
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We only really wanted this to run once, so I changed App.svelte to set variables from the hash before mounting in since we don't need anything to render first for that which sets us up to use onMount more appropriately downstream here.


// This will continue to poll/fetch the style at a local URL to allow live changes to be picked up
const poll = url => {
const pollCondition = str => {
Expand All @@ -52,9 +60,10 @@
if (data && typeof data === 'object') {
// TODO create checks by type for non-mapbox maps
style = data;

poll(url);
currentStyle = style;
dispatch('styleload', { style });
dispatch('styleload', { style, url });
return { status: '200' };
}
} catch (err) {
Expand All @@ -66,7 +75,7 @@
const setMapboxToken = url => {
const tokenRegex = /pk.([\w.]+)/g;
let nextToken = url.split('access_token').pop();
nextToken = nextToken.match(tokenRegex)[0];
nextToken = nextToken.match(tokenRegex)?.[0];
if ($mapboxGlAccessTokenStore === nextToken) return;
mapboxGlAccessTokenStore.set(nextToken);
};
Expand All @@ -77,7 +86,6 @@
if (status === '200') {
selectedUrl = url;
setMapboxToken(url);
// Call poll after setting selectedUrl on success
poll(url);
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/RendererSelect.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
let style;
let errors = [];

styleStore.subscribe(value => (style = value));
styleStore.subscribe(value => ({ style } = value));

const getBetterErrorMessages = errors => {
return errors.map(error => {
Expand Down
2 changes: 1 addition & 1 deletion src/stores.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { writable } from 'svelte/store';

export const styleStore = writable(null);
export const styleStore = writable({ style: null, url: null });

export const propertyValueComboLimitStore = writable(10);

Expand Down