aio-map 2.0.0 → 3.0.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.
package/index.css CHANGED
@@ -76,3 +76,30 @@
76
76
  position: absolute;
77
77
 
78
78
  }
79
+ .marker-cluster{
80
+ display:flex;
81
+ align-items: center;
82
+ justify-content: center;
83
+ }
84
+ .marker-cluster div{
85
+ display:flex;
86
+ align-items: center;
87
+ justify-content: center;
88
+ width:fit-content;
89
+ height:fit-content;
90
+ margin:0 !important;
91
+ border-radius:100%;
92
+ box-shadow:0 0 0 3px red;
93
+ background:yellow
94
+ }
95
+
96
+ .marker-cluster span{
97
+ width:36px !important;
98
+ height:36px !important;
99
+ display:flex !important;
100
+ align-items: center;
101
+ justify-content: center;
102
+ }
103
+ .ai-map .leaflet-top.leaflet-right{
104
+ display:block !important;
105
+ }
package/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { LeafletEvent } from "leaflet";
2
1
  import { FC, ReactNode } from "react";
2
+ import * as Leaflet from 'leaflet';
3
3
  import 'leaflet/dist/leaflet.css';
4
4
  import './index.css';
5
5
  type I_pos = [number, number];
@@ -38,15 +38,15 @@ export type I_polyline = {
38
38
  export type I_shape = I_circle | I_rect | I_polyline;
39
39
  type I_Map = {
40
40
  children?: React.ReactNode;
41
- onChange?: (coords: I_pos) => void;
42
- zoom?: {
43
- value?: number;
41
+ onChangePos?: (coords: I_pos) => void;
42
+ onChangeZoom?: (newZoom: number) => void;
43
+ pos?: I_pos;
44
+ zoom?: number;
45
+ zoomOptions?: {
44
46
  wheel?: boolean;
45
47
  control?: boolean;
46
- onChange?: (zoom: number) => void;
47
48
  };
48
49
  markers?: I_marker[];
49
- value?: I_pos;
50
50
  marker?: ReactNode | false;
51
51
  mapStyle?: any;
52
52
  attrs?: any;
@@ -61,7 +61,7 @@ type I_Map = {
61
61
  onSearch?: (searchResult: I_searchResult) => void;
62
62
  mapRef?: any;
63
63
  whenReady?: () => void;
64
- onMoveEnd?: (e: LeafletEvent) => void;
64
+ onMoveEnd?: (e: Leaflet.LeafletEvent) => void;
65
65
  actionsRef?: any;
66
66
  };
67
67
  export type I_layers = {
@@ -73,6 +73,7 @@ export type I_layerItem = {
73
73
  markers?: I_marker[];
74
74
  shapes?: I_shape[];
75
75
  active?: boolean;
76
+ cluster?: boolean;
76
77
  };
77
78
  declare const AIMap: FC<I_Map>;
78
79
  export default AIMap;
package/index.js CHANGED
@@ -8,25 +8,30 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  });
9
9
  };
10
10
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
11
- import { divIcon } from "leaflet";
12
11
  import * as ReactDOMServer from 'react-dom/server';
13
12
  import { createContext, isValidElement, useContext, useEffect, useRef, useState } from "react";
14
- import { Circle, FeatureGroup, LayersControl, MapContainer, Marker, Polyline, Rectangle, TileLayer, useMapEvents } from "react-leaflet";
13
+ import * as Leaflet from 'leaflet';
14
+ import * as ReactLeaflet from 'react-leaflet';
15
+ import MarkerClusterGroup from "react-leaflet-cluster";
15
16
  import AIOInput from "aio-input";
16
17
  import 'leaflet/dist/leaflet.css';
17
18
  import './index.css';
19
+ import { Debounce } from "aio-utils";
18
20
  const MAPCTX = createContext({});
19
21
  const AIMap = (props) => {
20
- var _a;
21
- const { value = [35.699939, 51.338497], getSearchResult, mapRef } = props;
22
+ const { pos: propsPos = [35.699939, 51.338497], getSearchResult, mapRef, zoom: propsZoom = 14, onChangePos = () => { } } = props;
22
23
  const [map, setMap] = useState(null);
23
- const [zoom, setZoom] = useState(((_a = props.zoom) === null || _a === void 0 ? void 0 : _a.value) || 14);
24
- const lockChangeZoomState = useRef(false);
25
- function changeZoom(zoom) {
26
- if (lockChangeZoomState.current === true) {
24
+ const [z, setZoom] = useState(propsZoom);
25
+ const zoomRef = useRef(z);
26
+ zoomRef.current = z;
27
+ const [p, setPos] = useState(propsPos);
28
+ const posRef = useRef(p);
29
+ posRef.current = p;
30
+ function changeZoom(newZoom, caller) {
31
+ if (newZoom === zoomRef.current) {
27
32
  return;
28
33
  }
29
- setZoom(zoom);
34
+ setZoom(newZoom);
30
35
  }
31
36
  if (mapRef) {
32
37
  mapRef.current = map;
@@ -34,39 +39,38 @@ const AIMap = (props) => {
34
39
  if (props.actionsRef) {
35
40
  props.actionsRef.current = {
36
41
  flyTo: (p) => {
37
- lockChangeZoomState.current = true;
38
42
  map.flyTo([p.lat, p.lng], p.zoom);
39
43
  const onMoveEnd = () => {
40
44
  map.off("moveend", onMoveEnd);
41
- lockChangeZoomState.current = false;
42
- changeZoom(p.zoom);
43
- p.callback();
45
+ if (p.zoom !== zoomRef.current) {
46
+ changeZoom(p.zoom, 'onMoveEnd in flyTo');
47
+ p.callback();
48
+ }
44
49
  };
45
50
  map.on("moveend", onMoveEnd);
46
51
  }
47
52
  };
48
53
  }
49
- const [pos, setPos] = useState(value);
50
- const moveTimeout = useRef(undefined);
51
- function move(pos) {
52
- setPos(pos);
53
- if (props.onChange) {
54
- clearTimeout(moveTimeout.current);
55
- moveTimeout.current = setTimeout(() => { if (props.onChange) {
56
- props.onChange(pos);
57
- } }, 600);
54
+ function changePos(newPos) {
55
+ if (newPos[0] === posRef.current[0] && newPos[1] === posRef.current[1]) {
56
+ return;
58
57
  }
58
+ setPos(newPos);
59
+ Debounce(() => onChangePos(posRef.current), 1000);
59
60
  }
60
61
  function getDefaultMarkerIcon() {
61
62
  return _jsxs("div", { className: 'marker-icon', children: [_jsx("div", { className: 'marker-icon-circle' }), _jsx("div", { className: 'marker-icon-arrow' })] });
62
63
  }
63
- function getContext() { return { pos, setMap, rootProps: props, move, getDefaultMarkerIcon, changeZoom, zoom }; }
64
+ function getContext() { return { pos: posRef.current, setMap, rootProps: props, changePos, getDefaultMarkerIcon, changeZoom, zoom: zoomRef.current }; }
64
65
  useEffect(() => {
65
66
  if (map !== null) {
66
- map.setView(value, zoom, { animate: false });
67
+ map.setView(propsPos, zoomRef.current, { animate: false });
67
68
  }
68
- setPos(value);
69
- }, [value[0] + '-' + value[1] + '-' + zoom]);
69
+ setPos(propsPos);
70
+ }, [propsPos[0] + '-' + propsPos[1]]);
71
+ useEffect(() => {
72
+ setZoom(propsZoom);
73
+ }, [propsZoom]);
70
74
  const attrs = AddToAttrs(props.attrs, { className: 'ai-map' });
71
75
  return (_jsx(MAPCTX.Provider, { value: getContext(), children: _jsxs("div", Object.assign({}, attrs, { children: [!!getSearchResult && _jsx(MapHeader, {}), _jsx(MapBody, {}), _jsx(MapFooter, {})] })) }));
72
76
  };
@@ -76,7 +80,7 @@ const MapBody = () => {
76
80
  const { rootProps, pos, setMap, getDefaultMarkerIcon, zoom } = useContext(MAPCTX);
77
81
  const { mapStyle, dragging = true, children, shapes = [], marker, markers = [], whenReady } = rootProps;
78
82
  const defaultStyle = { width: '100%', height: '100%' };
79
- return (_jsxs(MapContainer, { center: pos, style: Object.assign(Object.assign({}, defaultStyle), mapStyle), zoom: zoom, scrollWheelZoom: ((_a = rootProps.zoom) === null || _a === void 0 ? void 0 : _a.wheel) ? 'center' : undefined, zoomControl: ((_b = rootProps.zoom) === null || _b === void 0 ? void 0 : _b.control) !== false, attributionControl: true, dragging: dragging, ref: setMap, whenReady: whenReady, children: [_jsx(TileLayer, { url: "https://{s}.tile.openstreetmap.de/{z}/{x}/{y}.png" }), _jsx(MapEvents, {}), marker !== false && _jsx(MapMarker, { pos: pos, html: isValidElement(marker) ? marker : getDefaultMarkerIcon() }, 'main-marker'), markers.map((marker, i) => _jsx(MapMarker, { pos: marker.pos, html: marker.html, eventHandlers: marker.eventHandlers }, `marker-${i}`)), shapes.map((o, i) => _jsx(MapShape, { shape: o }, i)), _jsx(MapLayers, {}), children] }));
83
+ return (_jsxs(ReactLeaflet.MapContainer, { center: pos, style: Object.assign(Object.assign({}, defaultStyle), mapStyle), zoom: zoom, scrollWheelZoom: ((_a = rootProps.zoomOptions) === null || _a === void 0 ? void 0 : _a.wheel) ? 'center' : undefined, zoomControl: ((_b = rootProps.zoomOptions) === null || _b === void 0 ? void 0 : _b.control) !== false, attributionControl: true, dragging: dragging, ref: setMap, whenReady: whenReady, children: [_jsx(ReactLeaflet.TileLayer, { url: "https://{s}.tile.openstreetmap.de/{z}/{x}/{y}.png" }), _jsx(MapEvents, {}), marker !== false && _jsx(MapMarker, { pos: pos, html: isValidElement(marker) ? marker : getDefaultMarkerIcon() }, 'main-marker'), _jsx(MarkerClusterGroup, { chunkedLoading: true, showCoverageOnHover: false, children: markers.map((marker, i) => _jsx(MapMarker, { pos: marker.pos, html: marker.html, eventHandlers: marker.eventHandlers }, `marker-${i}`)) }), shapes.map((o, i) => _jsx(MapShape, { shape: o }, i)), _jsx(MapLayers, {}), children] }));
80
84
  };
81
85
  const MagnifyIcon = () => {
82
86
  return (_jsx("svg", { viewBox: "0 0 24 24", xmlns: "http://www.w3.org/2000/svg", width: 24, height: 24, children: _jsx("path", { d: "M9.5,3A6.5,6.5 0 0,1 16,9.5C16,11.11 15.41,12.59 14.44,13.73L14.71,14H15.5L20.5,19L19,20.5L14,15.5V14.71L13.73,14.44C12.59,15.41 11.11,16 9.5,16A6.5,6.5 0 0,1 3,9.5A6.5,6.5 0 0,1 9.5,3M9.5,5C7,5 5,7 5,9.5C5,12 7,14 9.5,14C12,14 14,12 14,9.5C14,7 12,5 9.5,5Z" }) }));
@@ -112,9 +116,10 @@ const MapLayers = () => {
112
116
  return null;
113
117
  }
114
118
  const { position = 'topright', items = [] } = layers;
115
- return (_jsx(LayersControl, { position: position, children: items.map((o, i) => {
116
- const { shapes = [], markers = [], active = true } = o;
117
- return (_jsx(LayersControl.Overlay, { name: o.name, checked: active, children: _jsxs(FeatureGroup, { children: [markers.map((marker, j) => _jsx(MapMarker, { pos: marker.pos, html: marker.html, eventHandlers: marker.eventHandlers }, j)), shapes.map((shape, k) => _jsx(MapShape, { shape: shape }, k))] }) }, i));
119
+ return (_jsx(ReactLeaflet.LayersControl, { position: position, children: items.map((o, i) => {
120
+ const { shapes = [], markers = [], active = true, cluster } = o;
121
+ const content = markers.map((marker, j) => _jsx(MapMarker, { pos: marker.pos, html: marker.html, eventHandlers: marker.eventHandlers }, j));
122
+ return (_jsx(ReactLeaflet.LayersControl.Overlay, { name: o.name, checked: active, children: _jsxs(ReactLeaflet.FeatureGroup, { children: [!!cluster && _jsx(MarkerClusterGroup, { chunkedLoading: true, showCoverageOnHover: false, children: content }), !cluster && content, shapes.map((shape, k) => _jsx(MapShape, { shape: shape }, k))] }) }, i));
118
123
  }) }));
119
124
  };
120
125
  const MapShape = ({ shape }) => {
@@ -124,13 +129,13 @@ const MapShape = ({ shape }) => {
124
129
  const pathOptions = { fillColor: fillColor, color: strokeColor, fillOpacity: opacity, weight: width, dashArray: dash };
125
130
  if (type === 'circle') {
126
131
  const { center, radius = 100 } = shape;
127
- return (_jsx(Circle, { center: center, pathOptions: pathOptions, radius: radius }));
132
+ return (_jsx(ReactLeaflet.Circle, { center: center, pathOptions: pathOptions, radius: radius }));
128
133
  }
129
134
  else if (type === 'rect') {
130
- return (_jsx(Rectangle, { bounds: shape.points, pathOptions: pathOptions }));
135
+ return (_jsx(ReactLeaflet.Rectangle, { bounds: shape.points, pathOptions: pathOptions }));
131
136
  }
132
137
  else if (type === 'polyline') {
133
- return (_jsx(Polyline, { positions: shape.points, pathOptions: pathOptions }));
138
+ return (_jsx(ReactLeaflet.Polyline, { positions: shape.points, pathOptions: pathOptions }));
134
139
  }
135
140
  return null;
136
141
  };
@@ -145,10 +150,10 @@ const MapFooter = () => {
145
150
  const MapMarker = ({ pos, html, eventHandlers }) => {
146
151
  const { getDefaultMarkerIcon } = useContext(MAPCTX);
147
152
  function getHtmlIcon(html) {
148
- return divIcon({
153
+ return Leaflet.divIcon({
149
154
  html: JSXToHTML(html),
150
- className: '',
151
- iconSize: [32, 32],
155
+ className: '', // Optional, for adding custom styles
156
+ iconSize: [32, 32], // size of the icon
152
157
  iconAnchor: [16, 32], // point of the icon which will correspond to marker's location
153
158
  });
154
159
  }
@@ -157,21 +162,21 @@ const MapMarker = ({ pos, html, eventHandlers }) => {
157
162
  if (html) {
158
163
  props.icon = getHtmlIcon(html);
159
164
  }
160
- return _jsx(Marker, Object.assign({}, props, { animate: false, eventHandlers: eventHandlers }));
165
+ return _jsx(ReactLeaflet.Marker, Object.assign({}, props, { animate: false, eventHandlers: eventHandlers }));
161
166
  };
162
167
  function MapEvents() {
163
- const { rootProps, move, changeZoom } = useContext(MAPCTX);
164
- useMapEvents({
168
+ const { rootProps, changePos, changeZoom } = useContext(MAPCTX);
169
+ ReactLeaflet.useMapEvents({
165
170
  click: () => rootProps.onClick ? rootProps.onClick() : undefined,
166
171
  move: (e) => {
167
172
  if (rootProps.dragging === false) {
168
173
  return;
169
174
  }
170
175
  let { lat, lng } = e.target.getCenter();
171
- move([lat, lng]);
176
+ changePos([lat, lng]);
172
177
  },
173
178
  zoom: (e) => {
174
- changeZoom(e.target._zoom);
179
+ changeZoom(e.target._zoom, 'useMapEvents');
175
180
  },
176
181
  locationfound: (location) => {
177
182
  console.log('location found:', location);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aio-map",
3
- "version": "2.0.0",
3
+ "version": "3.0.1",
4
4
  "description": "",
5
5
  "types": "index.d.ts",
6
6
  "main": "index.js",
@@ -12,9 +12,11 @@
12
12
  "url": "git+https://github.com/mohammadFeiz/aio-map.git"
13
13
  },
14
14
  "dependencies": {
15
+ "aio-deps": "latest",
16
+ "aio-input": "latest",
15
17
  "leaflet": "^1.9.4",
16
- "react-leaflet": "^4.2.1",
17
- "aio-input":"latest"
18
+ "react-leaflet": "^5.0.0",
19
+ "react-leaflet-cluster": "^2.1.0"
18
20
  },
19
21
  "author": "mohammad sharif feiz feiz.ms@gmail.com",
20
22
  "license": "ISC",