Skip to content

Commit 601ce19

Browse files
authored
Refactor/style switcher (#390)
* refactor: refactored style-switcher code * fix
1 parent f5d4a1b commit 601ce19

File tree

8 files changed

+90
-91
lines changed

8 files changed

+90
-91
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@watergis/svelte-maplibre-style-switcher": patch
3+
---
4+
5+
refactor: refactored style-switcher code

packages/style-switcher/src/lib/StyleSwitcher.svelte

Lines changed: 80 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,85 @@
1+
<script context="module" lang="ts">
2+
export type StyleSwitcherOption = {
3+
title: string;
4+
uri: string;
5+
};
6+
7+
export class StyleUrl {
8+
private STYLE_PATHNAME = 'style';
9+
10+
private getUrl() {
11+
const location = window.location;
12+
const url = new URL(location.href);
13+
url.hash = location.hash;
14+
return url;
15+
}
16+
17+
private updateUrl(url: URL) {
18+
history.replaceState('', '', url.toString());
19+
}
20+
21+
public get(): string | null {
22+
const url = this.getUrl();
23+
const style = url.searchParams.get(this.STYLE_PATHNAME);
24+
return style;
25+
}
26+
27+
public set(style: string) {
28+
const url = this.getUrl();
29+
url.searchParams.set(this.STYLE_PATHNAME, style);
30+
this.updateUrl(url);
31+
}
32+
33+
public delete() {
34+
const url = this.getUrl();
35+
url.searchParams.delete(this.STYLE_PATHNAME);
36+
this.updateUrl(url);
37+
}
38+
39+
public getMatchedStyleByTitle(
40+
styles: StyleSwitcherOption[],
41+
title: string
42+
): StyleSwitcherOption | undefined {
43+
for (const style of styles) {
44+
if (style.title === title) {
45+
return style;
46+
}
47+
}
48+
return;
49+
}
50+
51+
public getMatchedStyleByUrl(
52+
styles: StyleSwitcherOption[],
53+
url: string
54+
): StyleSwitcherOption | undefined {
55+
for (const style of styles) {
56+
if (style.uri === url) {
57+
return style;
58+
}
59+
}
60+
return;
61+
}
62+
63+
public getInitialStyle(styles: StyleSwitcherOption[]) {
64+
const defaultStyle = styles[0];
65+
const styleFromUrl = this.get();
66+
let initialStyle = defaultStyle;
67+
if (styleFromUrl) {
68+
const styleObj = this.getMatchedStyleByTitle(styles, styleFromUrl);
69+
if (styleObj) {
70+
initialStyle = styleObj;
71+
}
72+
} else {
73+
this.set(initialStyle.title);
74+
}
75+
return initialStyle;
76+
}
77+
}
78+
</script>
79+
180
<script lang="ts">
2-
import { createEventDispatcher } from 'svelte';
3-
import { StyleUrl } from '$lib/style-url';
481
import type { Map } from 'maplibre-gl';
5-
import type { StyleSwitcherOption } from './types';
82+
import { createEventDispatcher } from 'svelte';
683
784
export let map: Map;
885
export let styles: StyleSwitcherOption[];

packages/style-switcher/src/lib/StyleSwitcherControl.svelte

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
<script lang="ts">
2+
import type { ControlPosition, Map } from 'maplibre-gl';
23
import { onMount } from 'svelte';
3-
import type { Map } from 'maplibre-gl';
4-
import type { Position, StyleSwitcherOption } from './types';
54
import MapButtons from './MapButtons.svelte';
6-
import { StyleUrl } from '$lib/style-url';
5+
import { StyleUrl, type StyleSwitcherOption } from './StyleSwitcher.svelte';
76
87
export let styles: StyleSwitcherOption[];
98
export let map: Map;
10-
export let position: Position = 'bottom-left';
9+
export let position: ControlPosition = 'bottom-left';
1110
export let selectedStyle: StyleSwitcherOption;
1211
1312
let styleSwitcherDiv: HTMLDivElement;
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import { default as StyleSwitcher } from './StyleSwitcher.svelte';
2-
import { default as StyleSwitcherControl } from './StyleSwitcherControl.svelte';
3-
import type { StyleSwitcherOption } from './types';
4-
import { StyleUrl } from './style-url';
1+
import StyleSwitcher, { StyleUrl, type StyleSwitcherOption } from './StyleSwitcher.svelte';
2+
import StyleSwitcherControl from './StyleSwitcherControl.svelte';
53

64
export { StyleSwitcher, StyleSwitcherControl, StyleUrl };
75
export type { StyleSwitcherOption };

packages/style-switcher/src/lib/style-url.ts

Lines changed: 0 additions & 73 deletions
This file was deleted.

packages/style-switcher/src/lib/types/Position.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

packages/style-switcher/src/lib/types/StyleSwitcherOption.ts

Lines changed: 0 additions & 4 deletions
This file was deleted.

packages/style-switcher/src/lib/types/index.ts

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)