@mapka/maplibre-gl-sdk 0.11.0 → 0.13.0

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.
Files changed (41) hide show
  1. package/README.md +325 -3
  2. package/lib/.buildInfo.json +1 -1
  3. package/lib/components/DownloadIcon.d.ts +4 -0
  4. package/lib/components/DownloadIcon.d.ts.map +1 -0
  5. package/lib/components/DownloadIcon.js +10 -0
  6. package/lib/components/PopupContent.d.ts.map +1 -1
  7. package/lib/components/ProgressDownIcon.d.ts +4 -0
  8. package/lib/components/ProgressDownIcon.d.ts.map +1 -0
  9. package/lib/components/ProgressDownIcon.js +14 -0
  10. package/lib/controls/MapkaExportControl.d.ts +21 -0
  11. package/lib/controls/MapkaExportControl.d.ts.map +1 -0
  12. package/lib/controls/MapkaExportControl.js +76 -0
  13. package/lib/index.d.ts +2 -0
  14. package/lib/index.d.ts.map +1 -1
  15. package/lib/index.js +2 -0
  16. package/lib/map.d.ts +13 -3
  17. package/lib/map.d.ts.map +1 -1
  18. package/lib/map.js +16 -8
  19. package/lib/modules/export.d.ts +4 -0
  20. package/lib/modules/export.d.ts.map +1 -0
  21. package/lib/modules/export.js +40 -0
  22. package/lib/modules/markers.d.ts +1 -1
  23. package/lib/modules/markers.d.ts.map +1 -1
  24. package/lib/modules/markers.js +1 -1
  25. package/lib/modules/popup.d.ts +3 -2
  26. package/lib/modules/popup.d.ts.map +1 -1
  27. package/lib/modules/popup.js +13 -5
  28. package/lib/types/export.d.ts +7 -0
  29. package/lib/types/export.d.ts.map +1 -0
  30. package/lib/types/export.js +1 -0
  31. package/package.json +3 -2
  32. package/src/components/DownloadIcon.tsx +25 -0
  33. package/src/components/PopupContent.tsx +1 -0
  34. package/src/components/ProgressDownIcon.tsx +29 -0
  35. package/src/controls/MapkaExportControl.tsx +111 -0
  36. package/src/index.ts +5 -0
  37. package/src/map.ts +25 -7
  38. package/src/modules/export.ts +55 -0
  39. package/src/modules/markers.ts +1 -1
  40. package/src/modules/popup.tsx +21 -23
  41. package/src/types/export.ts +6 -0
package/src/index.ts CHANGED
@@ -2,6 +2,11 @@ export * from "maplibre-gl";
2
2
  export * from "./types/layer.js";
3
3
  export * from "./types/marker.js";
4
4
  export * from "./types/style.js";
5
+ export * from "./types/export.js";
5
6
 
6
7
  export { MapkaMap as Map } from "./map.js";
7
8
  export { MapkaMapOptions as MapOptions } from "./map.js";
9
+ export {
10
+ MapkaExportControl,
11
+ MapkaExportControlOptions,
12
+ } from "./controls/MapkaExportControl.js";
package/src/map.ts CHANGED
@@ -5,13 +5,14 @@ import {
5
5
  closePopupsById,
6
6
  getPopupId,
7
7
  openPopup,
8
+ removePopups,
8
9
  updatePopup,
9
10
  } from "./modules/popup.js";
10
11
  import {
11
12
  addMarkers,
12
13
  addStyleDiffMarkers,
13
14
  addStyleMarkers,
14
- clearMarkers,
15
+ removeMarkers,
15
16
  updateMarkers,
16
17
  } from "./modules/markers.js";
17
18
  import type {
@@ -26,6 +27,8 @@ import type {
26
27
  StyleSpecification,
27
28
  } from "maplibre-gl";
28
29
  import type { MapkaMarkerOptions, MapkaPopupOptions } from "./types/marker.js";
30
+ import type { MapkaExportOptions } from "./types/export.js";
31
+ import { exportMap } from "./modules/export.js";
29
32
 
30
33
  export interface MapkaMapOptions extends MapOptions {
31
34
  maxPopups?: number;
@@ -71,9 +74,16 @@ export type MapMapkaMarker = {
71
74
  marker: Marker;
72
75
  };
73
76
 
77
+ interface Logger {
78
+ log: (...args: unknown[]) => void;
79
+ warn: (...args: unknown[]) => void;
80
+ error: (...args: unknown[]) => void;
81
+ }
82
+
74
83
  export class MapkaMap extends maplibregl.Map {
75
84
  static env: string = "prod";
76
85
 
86
+ public logger: Logger = console;
77
87
  public markers: MapMapkaMarker[] = [];
78
88
 
79
89
  public maxPopups: number = 1;
@@ -124,19 +134,27 @@ export class MapkaMap extends maplibregl.Map {
124
134
  updateMarkers(this, markers);
125
135
  }
126
136
 
127
- public clearMarkers() {
128
- clearMarkers(this);
137
+ public removeMarkers() {
138
+ removeMarkers(this);
129
139
  }
130
140
 
131
- public openPopup(popup: MapkaPopupOptions, id: string = getPopupId(popup)) {
132
- return openPopup(this, popup, id);
141
+ public openPopup(popup: MapkaPopupOptions) {
142
+ return openPopup(this, popup);
133
143
  }
134
144
 
135
145
  public closePopup(id: string) {
136
146
  closePopupsById(this, id);
137
147
  }
138
148
 
139
- public updatePopup(popup: MapkaPopupOptions, id: string = getPopupId(popup)) {
140
- return updatePopup(this, popup, id);
149
+ public updatePopup(popup: MapkaPopupOptions) {
150
+ return updatePopup(this, popup);
151
+ }
152
+
153
+ public removePopups() {
154
+ removePopups(this);
155
+ }
156
+
157
+ public async export(options?: MapkaExportOptions) {
158
+ return exportMap(this, options);
141
159
  }
142
160
  }
@@ -0,0 +1,55 @@
1
+ import { toPng } from "html-to-image";
2
+ import type { MapkaMap } from "../map.js";
3
+ import type { MapkaExportOptions } from "../types/export.js";
4
+
5
+ export async function exportMap(
6
+ map: MapkaMap,
7
+ options: MapkaExportOptions = {},
8
+ ): Promise<HTMLImageElement> {
9
+ const { hideControls = false, hideMarkers = false, hidePopups = false, bbox } = options;
10
+
11
+ const container = map.getContainer();
12
+ const originalBounds = map.getBounds();
13
+
14
+ if (bbox) {
15
+ map.fitBounds(bbox, { padding: 20, animate: false });
16
+ }
17
+
18
+ return new Promise<HTMLImageElement>((resolve, reject) => {
19
+ map.once("render", () => {
20
+ toPng(container, {
21
+ skipFonts: navigator.userAgent.includes("Firefox"),
22
+ filter: (node) => {
23
+ if (node.classList) {
24
+ const isControlsVisibleOrNotControl =
25
+ !hideControls || !node.classList.contains("maplibregl-control-container");
26
+ const isMarkersVisibleOrNotMarker =
27
+ !hideMarkers || !node.classList.contains("maplibregl-marker");
28
+ const isPopupsVisibleOrNotPopup =
29
+ !hidePopups || !node.classList.contains("maplibregl-popup");
30
+
31
+ return (
32
+ isControlsVisibleOrNotControl &&
33
+ isMarkersVisibleOrNotMarker &&
34
+ isPopupsVisibleOrNotPopup
35
+ );
36
+ }
37
+ return true;
38
+ },
39
+ })
40
+ .then((dataUrl) => {
41
+ const img = new Image();
42
+ img.src = dataUrl;
43
+ img.onload = () => resolve(img);
44
+ img.onerror = reject;
45
+ })
46
+ .catch(reject)
47
+ .finally(() => {
48
+ if (bbox) {
49
+ map.fitBounds(originalBounds, { animate: false });
50
+ }
51
+ });
52
+ });
53
+ map.triggerRepaint();
54
+ });
55
+ }
@@ -129,7 +129,7 @@ export function updateMarkers(map: MapkaMap, markersOptions: MapkaMarkerOptions[
129
129
  addMarkers(map, markersOptions);
130
130
  }
131
131
 
132
- export function clearMarkers(map: MapkaMap) {
132
+ export function removeMarkers(map: MapkaMap) {
133
133
  for (const marker of map.markers) {
134
134
  marker.marker.remove();
135
135
  }
@@ -24,8 +24,8 @@ export function enforceMaxPopups(map: MapkaMap) {
24
24
  }
25
25
  }
26
26
 
27
- export function openPopup(map: MapkaMap, options: MapkaPopupOptions, id: string) {
28
- const { lngLat, content, closeButton, ...popupOptions } = options;
27
+ export function openPopup(map: MapkaMap, options: MapkaPopupOptions) {
28
+ const { lngLat, content, closeButton, id = getPopupId(options), ...popupOptions } = options;
29
29
  if (content instanceof HTMLElement) {
30
30
  const popup = new Popup({
31
31
  ...popupOptions,
@@ -70,14 +70,10 @@ export function openPopup(map: MapkaMap, options: MapkaPopupOptions, id: string)
70
70
  return id;
71
71
  } else if (typeof content === "function") {
72
72
  const newContent = content(id);
73
- return openPopup(
74
- map,
75
- {
76
- ...options,
77
- content: newContent,
78
- },
79
- id,
80
- );
73
+ return openPopup(map, {
74
+ ...options,
75
+ content: newContent,
76
+ });
81
77
  }
82
78
 
83
79
  throw new Error("Invalid popup content");
@@ -102,11 +98,9 @@ export function updatePopupBaseOptions(
102
98
  return popup;
103
99
  }
104
100
 
105
- export function updatePopup(
106
- map: MapkaMap,
107
- { content, ...newOptions }: MapkaPopupOptions,
108
- id: string,
109
- ) {
101
+ export function updatePopup(map: MapkaMap, { content, ...newOptions }: MapkaPopupOptions) {
102
+ const id = getPopupId(newOptions);
103
+
110
104
  if (content instanceof HTMLElement) {
111
105
  const mapkaPopups = map.popups.filter((popup) => popup.id === id);
112
106
  for (const { popup, options } of mapkaPopups) {
@@ -125,14 +119,10 @@ export function updatePopup(
125
119
  }
126
120
  } else if (typeof content === "function") {
127
121
  const newContent = content(id);
128
- return updatePopup(
129
- map,
130
- {
131
- ...newOptions,
132
- content: newContent,
133
- },
134
- id,
135
- );
122
+ return updatePopup(map, {
123
+ ...newOptions,
124
+ content: newContent,
125
+ });
136
126
  }
137
127
  }
138
128
 
@@ -153,3 +143,11 @@ export function closePopupsById(map: MapkaMap, id: string) {
153
143
  popup.container.remove();
154
144
  }
155
145
  }
146
+
147
+ export function removePopups(map: MapkaMap) {
148
+ for (const popup of map.popups) {
149
+ popup.popup.remove();
150
+ popup.container.remove();
151
+ }
152
+ map.popups = [];
153
+ }
@@ -0,0 +1,6 @@
1
+ export type MapkaExportOptions = {
2
+ hideControls?: boolean;
3
+ hideMarkers?: boolean;
4
+ hidePopups?: boolean;
5
+ bbox?: [number, number, number, number];
6
+ };