@versatiles/svelte 2.1.0 → 2.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,32 +4,41 @@
4
4
  import { getCountryName } from '../../utils/location.js';
5
5
  import BasicMap from '../BasicMap/BasicMap.svelte';
6
6
  import { isDarkMode } from '../../utils/map_style.js';
7
- import type { BBox } from 'geojson';
8
7
  import { loadBBoxes } from './BBoxMap.js';
9
- import { BBoxDrawer } from './lib/bbox.js';
8
+ import { BBoxDrawer, isSameBBox, type BBox } from './lib/bbox.js';
10
9
 
11
10
  let { selectedBBox = $bindable() }: { selectedBBox?: BBox } = $props();
12
11
  const startTime = Date.now();
13
- let bboxDrawer: BBoxDrawer;
12
+ let bboxDrawer: BBoxDrawer | undefined;
14
13
  let map: MaplibreMapType | undefined = $state();
15
14
  let bboxes: { key: string; value: BBox }[] | undefined = $state();
16
15
  let mapContainer: HTMLElement;
17
16
 
17
+ $effect(() => setBBox(selectedBBox));
18
+
18
19
  async function onMapInit(_map: MaplibreMapType) {
19
20
  map = _map;
20
21
  mapContainer = map.getContainer();
21
22
  map.setPadding({ top: 42, right: 10, bottom: 15, left: 10 });
22
- bboxDrawer = new BBoxDrawer(map!, [-180, -85, 180, 85], isDarkMode(mapContainer) ? '#FFFFFF' : '#000000');
23
23
  bboxes = await loadBBoxes();
24
- // If an initial bbox is already provided by the parent, display it instead of guessing the user's country
25
- if (selectedBBox) flyToBBox(selectedBBox);
26
- bboxDrawer.bbox.subscribe((bbox) => (selectedBBox = bbox));
24
+ const bbox = selectedBBox || [-180, -85, 180, 85];
25
+ bboxDrawer = new BBoxDrawer(map!, bbox, isDarkMode(mapContainer) ? '#FFFFFF' : '#000000');
26
+ bboxDrawer.bbox.subscribe(setBBox);
27
+ if (selectedBBox) setBBox(selectedBBox);
27
28
  }
28
29
 
29
- function flyToBBox(bbox: BBox) {
30
+ function setBBox(bbox?: BBox) {
30
31
  if (!map || !bbox) return;
31
32
 
32
- bboxDrawer.setGeometry(bbox);
33
+ if (!selectedBBox || !isSameBBox(selectedBBox, bbox)) {
34
+ selectedBBox = bbox;
35
+ }
36
+
37
+ if (!bboxDrawer) return;
38
+
39
+ if (!isSameBBox(bboxDrawer.getBBox(), bbox)) {
40
+ bboxDrawer.setBBox(bbox);
41
+ }
33
42
 
34
43
  const transform = map.cameraForBounds(bboxDrawer.getBounds()) as CameraOptions;
35
44
  if (transform == null) return;
@@ -37,7 +46,7 @@
37
46
  transform.bearing = 0;
38
47
  transform.pitch = 0;
39
48
 
40
- if (Date.now() - startTime < 1000) {
49
+ if (Date.now() - startTime < 3000) {
41
50
  map.jumpTo(transform);
42
51
  } else {
43
52
  map.flyTo({ ...transform, essential: true, speed: 5 });
@@ -66,12 +75,12 @@
66
75
  <AutoComplete
67
76
  items={bboxes}
68
77
  placeholder="Find country, region or city …"
69
- change={(bbox) => flyToBBox(bbox)}
78
+ change={(bbox) => (selectedBBox = bbox)}
70
79
  initialInputText={getInitialInputText()}
71
80
  />
72
81
  </div>
73
82
  {/if}
74
- <BasicMap {onMapInit} emptyStyle={true}></BasicMap>
83
+ <BasicMap {onMapInit}></BasicMap>
75
84
  </div>
76
85
 
77
86
  <style>
@@ -1,4 +1,4 @@
1
- import type { BBox } from 'geojson';
1
+ import { type BBox } from './lib/bbox.js';
2
2
  type $$ComponentProps = {
3
3
  selectedBBox?: BBox;
4
4
  };
@@ -9,6 +9,7 @@ export declare const DragPointMap: Map<DragPoint, {
9
9
  }>;
10
10
  export type BBox = [number, number, number, number];
11
11
  export declare class BBoxDrawer {
12
+ #private;
12
13
  private sourceId;
13
14
  private dragPoint;
14
15
  private isDragging;
@@ -19,7 +20,8 @@ export declare class BBoxDrawer {
19
20
  constructor(map: maplibregl.Map, bbox: BBox, color: string, inverted?: boolean);
20
21
  private updateDragPoint;
21
22
  private getAsFeatureCollection;
22
- setGeometry(bbox: geojson.BBox): void;
23
+ setBBox(bbox: geojson.BBox): void;
24
+ getBBox(): BBox;
23
25
  getBounds(): maplibregl.LngLatBounds;
24
26
  private redraw;
25
27
  private getAsPixel;
@@ -27,3 +29,4 @@ export declare class BBoxDrawer {
27
29
  private getCursor;
28
30
  private doDrag;
29
31
  }
32
+ export declare function isSameBBox(a: geojson.BBox, b: geojson.BBox): boolean;
@@ -1,6 +1,5 @@
1
- import { get, writable } from 'svelte/store';
1
+ import { writable } from 'svelte/store';
2
2
  import maplibregl from 'maplibre-gl';
3
- import { getMapStyle } from '../../../utils/map_style.js';
4
3
  // prettier-ignore
5
4
  export const DragPointMap = new Map([
6
5
  ['n', { cursor: 'ns-resize', flipH: 'n', flipV: 's' }],
@@ -21,16 +20,14 @@ export class BBoxDrawer {
21
20
  map;
22
21
  canvas;
23
22
  inverted;
24
- bbox;
23
+ bbox = writable(worldBBox);
24
+ #bbox = [0, 0, 0, 0];
25
25
  constructor(map, bbox, color, inverted) {
26
- this.bbox = writable(bbox);
27
26
  this.inverted = inverted ?? true;
28
27
  this.map = map;
29
28
  this.sourceId = 'bbox_' + Math.random().toString(36).slice(2);
30
- const style = getMapStyle();
31
- style.transition = { duration: 0, delay: 0 };
32
- style.sources[this.sourceId] = { type: 'geojson', data: this.getAsFeatureCollection() };
33
- style.layers.push({
29
+ map.addSource(this.sourceId, { type: 'geojson', data: this.getAsFeatureCollection() });
30
+ map.addLayer({
34
31
  id: 'bbox-line_' + Math.random().toString(36).slice(2),
35
32
  type: 'line',
36
33
  source: this.sourceId,
@@ -38,7 +35,7 @@ export class BBoxDrawer {
38
35
  layout: { 'line-cap': 'round', 'line-join': 'round' },
39
36
  paint: { 'line-color': color }
40
37
  });
41
- style.layers.push({
38
+ map.addLayer({
42
39
  id: 'bbox-fill_' + Math.random().toString(36).slice(2),
43
40
  type: 'fill',
44
41
  source: this.sourceId,
@@ -46,7 +43,6 @@ export class BBoxDrawer {
46
43
  layout: {},
47
44
  paint: { 'fill-color': color, 'fill-opacity': 0.2 }
48
45
  });
49
- map.setStyle(style);
50
46
  this.canvas = map.getCanvasContainer();
51
47
  map.on('mousemove', (e) => {
52
48
  if (e.originalEvent.buttons % 2 === 0)
@@ -74,6 +70,7 @@ export class BBoxDrawer {
74
70
  this.isDragging = false;
75
71
  this.updateDragPoint(false);
76
72
  });
73
+ this.setBBox(bbox);
77
74
  }
78
75
  updateDragPoint(dragPoint) {
79
76
  if (this.dragPoint === dragPoint)
@@ -82,7 +79,7 @@ export class BBoxDrawer {
82
79
  this.canvas.style.cursor = this.getCursor(dragPoint);
83
80
  }
84
81
  getAsFeatureCollection() {
85
- const ring = getRing(get(this.bbox));
82
+ const ring = getRing(this.getBBox());
86
83
  return {
87
84
  type: 'FeatureCollection',
88
85
  features: [this.inverted ? polygon(getRing(worldBBox), ring) : polygon(ring), linestring(ring)]
@@ -102,12 +99,19 @@ export class BBoxDrawer {
102
99
  return { type: 'Feature', geometry: { type: 'LineString', coordinates }, properties: {} };
103
100
  }
104
101
  }
105
- setGeometry(bbox) {
106
- this.bbox.set(bbox.slice(0, 4));
102
+ setBBox(bbox) {
103
+ const newBbox = bbox.slice(0, 4);
104
+ if (this.#bbox && isSameBBox(this.#bbox, newBbox))
105
+ return;
106
+ this.#bbox = bbox.slice(0, 4);
107
107
  this.redraw();
108
+ this.bbox.set(this.#bbox);
109
+ }
110
+ getBBox() {
111
+ return this.#bbox;
108
112
  }
109
113
  getBounds() {
110
- return new maplibregl.LngLatBounds(get(this.bbox));
114
+ return new maplibregl.LngLatBounds(this.getBBox());
111
115
  }
112
116
  redraw() {
113
117
  const source = this.map.getSource(this.sourceId);
@@ -118,7 +122,7 @@ export class BBoxDrawer {
118
122
  }
119
123
  }
120
124
  getAsPixel() {
121
- const bbox = get(this.bbox);
125
+ const bbox = this.getBBox();
122
126
  const p0 = this.map.project([bbox[0], bbox[1]]);
123
127
  const p1 = this.map.project([bbox[2], bbox[3]]);
124
128
  return [Math.min(p0.x, p1.x), Math.min(p0.y, p1.y), Math.max(p0.x, p1.x), Math.max(p0.y, p1.y)];
@@ -149,7 +153,7 @@ export class BBoxDrawer {
149
153
  return DragPointMap.get(drag)?.cursor ?? 'default';
150
154
  }
151
155
  doDrag(lngLat) {
152
- this.bbox.update((bbox) => {
156
+ this.#bbox = ((bbox) => {
153
157
  const x = Math.round(lngLat.lng * 1e3) / 1e3;
154
158
  const y = Math.round(lngLat.lat * 1e3) / 1e3;
155
159
  // prettier-ignore
@@ -195,6 +199,9 @@ export class BBoxDrawer {
195
199
  this.updateDragPoint(DragPointMap.get(this.dragPoint)?.flipV ?? false);
196
200
  }
197
201
  return bbox;
198
- });
202
+ })(this.#bbox);
199
203
  }
200
204
  }
205
+ export function isSameBBox(a, b) {
206
+ return a[0] === b[0] && a[1] === b[1] && a[2] === b[2] && a[3] === b[3];
207
+ }
@@ -19,8 +19,8 @@ export declare class MapLayerSymbol extends MapLayer<LayerSymbol> {
19
19
  label: Writable<string>;
20
20
  labelAlign: Writable<number>;
21
21
  symbolInfo: import("svelte/store").Readable<import("../symbols.js").SymbolInfo>;
22
- textAnchor: import("svelte/store").Readable<"center" | "top" | "bottom" | "right" | "left" | undefined>;
23
- textVariableAnchor: import("svelte/store").Readable<("center" | "top-left" | "top-right" | "bottom-left" | "bottom-right" | "top" | "bottom" | "right" | "left")[] | undefined>;
22
+ textAnchor: import("svelte/store").Readable<"center" | "left" | "right" | "top" | "bottom" | undefined>;
23
+ textVariableAnchor: import("svelte/store").Readable<("center" | "top-left" | "top-right" | "bottom-left" | "bottom-right" | "left" | "right" | "top" | "bottom")[] | undefined>;
24
24
  constructor(manager: GeometryManager, id: string, source: string);
25
25
  getState(): StateStyle | undefined;
26
26
  setState(state: StateStyle): void;
package/dist/index.d.ts CHANGED
@@ -3,3 +3,4 @@ import BBoxMap from './components/BBoxMap/BBoxMap.svelte';
3
3
  import LocatorMap from './components/LocatorMap/LocatorMap.svelte';
4
4
  import MapEditor from './components/MapEditor/MapEditor.svelte';
5
5
  export { BasicMap, BBoxMap, LocatorMap, MapEditor };
6
+ export { type BBox } from './components/BBoxMap/lib/bbox.js';
package/dist/index.js CHANGED
@@ -3,3 +3,4 @@ import BBoxMap from './components/BBoxMap/BBoxMap.svelte';
3
3
  import LocatorMap from './components/LocatorMap/LocatorMap.svelte';
4
4
  import MapEditor from './components/MapEditor/MapEditor.svelte';
5
5
  export { BasicMap, BBoxMap, LocatorMap, MapEditor };
6
+ export {} from './components/BBoxMap/lib/bbox.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@versatiles/svelte",
3
- "version": "2.1.0",
3
+ "version": "2.1.1",
4
4
  "license": "MIT",
5
5
  "scripts": {
6
6
  "build": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json && vite build && npm run package",