aio-map 2.0.0 → 3.0.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.
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 { Leaflet } from 'aio-deps';
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,29 @@ 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 { Leaflet, ReactLeaflet } from 'aio-deps';
14
+ import MarkerClusterGroup from "react-leaflet-cluster";
15
15
  import AIOInput from "aio-input";
16
16
  import 'leaflet/dist/leaflet.css';
17
17
  import './index.css';
18
+ import { Debounce } from "aio-utils";
18
19
  const MAPCTX = createContext({});
19
20
  const AIMap = (props) => {
20
- var _a;
21
- const { value = [35.699939, 51.338497], getSearchResult, mapRef } = props;
21
+ const { pos: propsPos = [35.699939, 51.338497], getSearchResult, mapRef, zoom: propsZoom = 14, onChangePos = () => { } } = props;
22
22
  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) {
23
+ const [z, setZoom] = useState(propsZoom);
24
+ const zoomRef = useRef(z);
25
+ zoomRef.current = z;
26
+ const [p, setPos] = useState(propsPos);
27
+ const posRef = useRef(p);
28
+ posRef.current = p;
29
+ function changeZoom(newZoom, caller) {
30
+ if (newZoom === zoomRef.current) {
27
31
  return;
28
32
  }
29
- setZoom(zoom);
33
+ setZoom(newZoom);
30
34
  }
31
35
  if (mapRef) {
32
36
  mapRef.current = map;
@@ -34,39 +38,38 @@ const AIMap = (props) => {
34
38
  if (props.actionsRef) {
35
39
  props.actionsRef.current = {
36
40
  flyTo: (p) => {
37
- lockChangeZoomState.current = true;
38
41
  map.flyTo([p.lat, p.lng], p.zoom);
39
42
  const onMoveEnd = () => {
40
43
  map.off("moveend", onMoveEnd);
41
- lockChangeZoomState.current = false;
42
- changeZoom(p.zoom);
43
- p.callback();
44
+ if (p.zoom !== zoomRef.current) {
45
+ changeZoom(p.zoom, 'onMoveEnd in flyTo');
46
+ p.callback();
47
+ }
44
48
  };
45
49
  map.on("moveend", onMoveEnd);
46
50
  }
47
51
  };
48
52
  }
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);
53
+ function changePos(newPos) {
54
+ if (newPos[0] === posRef.current[0] && newPos[1] === posRef.current[1]) {
55
+ return;
58
56
  }
57
+ setPos(newPos);
58
+ Debounce(() => onChangePos(posRef.current), 1000);
59
59
  }
60
60
  function getDefaultMarkerIcon() {
61
61
  return _jsxs("div", { className: 'marker-icon', children: [_jsx("div", { className: 'marker-icon-circle' }), _jsx("div", { className: 'marker-icon-arrow' })] });
62
62
  }
63
- function getContext() { return { pos, setMap, rootProps: props, move, getDefaultMarkerIcon, changeZoom, zoom }; }
63
+ function getContext() { return { pos: posRef.current, setMap, rootProps: props, changePos, getDefaultMarkerIcon, changeZoom, zoom: zoomRef.current }; }
64
64
  useEffect(() => {
65
65
  if (map !== null) {
66
- map.setView(value, zoom, { animate: false });
66
+ map.setView(propsPos, zoomRef.current, { animate: false });
67
67
  }
68
- setPos(value);
69
- }, [value[0] + '-' + value[1] + '-' + zoom]);
68
+ setPos(propsPos);
69
+ }, [propsPos[0] + '-' + propsPos[1]]);
70
+ useEffect(() => {
71
+ setZoom(propsZoom);
72
+ }, [propsZoom]);
70
73
  const attrs = AddToAttrs(props.attrs, { className: 'ai-map' });
71
74
  return (_jsx(MAPCTX.Provider, { value: getContext(), children: _jsxs("div", Object.assign({}, attrs, { children: [!!getSearchResult && _jsx(MapHeader, {}), _jsx(MapBody, {}), _jsx(MapFooter, {})] })) }));
72
75
  };
@@ -76,7 +79,7 @@ const MapBody = () => {
76
79
  const { rootProps, pos, setMap, getDefaultMarkerIcon, zoom } = useContext(MAPCTX);
77
80
  const { mapStyle, dragging = true, children, shapes = [], marker, markers = [], whenReady } = rootProps;
78
81
  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] }));
82
+ 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
83
  };
81
84
  const MagnifyIcon = () => {
82
85
  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 +115,10 @@ const MapLayers = () => {
112
115
  return null;
113
116
  }
114
117
  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));
118
+ return (_jsx(ReactLeaflet.LayersControl, { position: position, children: items.map((o, i) => {
119
+ const { shapes = [], markers = [], active = true, cluster } = o;
120
+ const content = markers.map((marker, j) => _jsx(MapMarker, { pos: marker.pos, html: marker.html, eventHandlers: marker.eventHandlers }, j));
121
+ 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
122
  }) }));
119
123
  };
120
124
  const MapShape = ({ shape }) => {
@@ -124,13 +128,13 @@ const MapShape = ({ shape }) => {
124
128
  const pathOptions = { fillColor: fillColor, color: strokeColor, fillOpacity: opacity, weight: width, dashArray: dash };
125
129
  if (type === 'circle') {
126
130
  const { center, radius = 100 } = shape;
127
- return (_jsx(Circle, { center: center, pathOptions: pathOptions, radius: radius }));
131
+ return (_jsx(ReactLeaflet.Circle, { center: center, pathOptions: pathOptions, radius: radius }));
128
132
  }
129
133
  else if (type === 'rect') {
130
- return (_jsx(Rectangle, { bounds: shape.points, pathOptions: pathOptions }));
134
+ return (_jsx(ReactLeaflet.Rectangle, { bounds: shape.points, pathOptions: pathOptions }));
131
135
  }
132
136
  else if (type === 'polyline') {
133
- return (_jsx(Polyline, { positions: shape.points, pathOptions: pathOptions }));
137
+ return (_jsx(ReactLeaflet.Polyline, { positions: shape.points, pathOptions: pathOptions }));
134
138
  }
135
139
  return null;
136
140
  };
@@ -145,10 +149,10 @@ const MapFooter = () => {
145
149
  const MapMarker = ({ pos, html, eventHandlers }) => {
146
150
  const { getDefaultMarkerIcon } = useContext(MAPCTX);
147
151
  function getHtmlIcon(html) {
148
- return divIcon({
152
+ return Leaflet.divIcon({
149
153
  html: JSXToHTML(html),
150
- className: '',
151
- iconSize: [32, 32],
154
+ className: '', // Optional, for adding custom styles
155
+ iconSize: [32, 32], // size of the icon
152
156
  iconAnchor: [16, 32], // point of the icon which will correspond to marker's location
153
157
  });
154
158
  }
@@ -157,21 +161,21 @@ const MapMarker = ({ pos, html, eventHandlers }) => {
157
161
  if (html) {
158
162
  props.icon = getHtmlIcon(html);
159
163
  }
160
- return _jsx(Marker, Object.assign({}, props, { animate: false, eventHandlers: eventHandlers }));
164
+ return _jsx(ReactLeaflet.Marker, Object.assign({}, props, { animate: false, eventHandlers: eventHandlers }));
161
165
  };
162
166
  function MapEvents() {
163
- const { rootProps, move, changeZoom } = useContext(MAPCTX);
164
- useMapEvents({
167
+ const { rootProps, changePos, changeZoom } = useContext(MAPCTX);
168
+ ReactLeaflet.useMapEvents({
165
169
  click: () => rootProps.onClick ? rootProps.onClick() : undefined,
166
170
  move: (e) => {
167
171
  if (rootProps.dragging === false) {
168
172
  return;
169
173
  }
170
174
  let { lat, lng } = e.target.getCenter();
171
- move([lat, lng]);
175
+ changePos([lat, lng]);
172
176
  },
173
177
  zoom: (e) => {
174
- changeZoom(e.target._zoom);
178
+ changeZoom(e.target._zoom, 'useMapEvents');
175
179
  },
176
180
  locationfound: (location) => {
177
181
  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.0",
4
4
  "description": "",
5
5
  "types": "index.d.ts",
6
6
  "main": "index.js",
@@ -12,8 +12,7 @@
12
12
  "url": "git+https://github.com/mohammadFeiz/aio-map.git"
13
13
  },
14
14
  "dependencies": {
15
- "leaflet": "^1.9.4",
16
- "react-leaflet": "^4.2.1",
15
+ "aio-deps": "latest",
17
16
  "aio-input":"latest"
18
17
  },
19
18
  "author": "mohammad sharif feiz feiz.ms@gmail.com",