aio-map 1.0.0 → 1.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.
Files changed (3) hide show
  1. package/index.d.ts +2 -1
  2. package/index.js +188 -1
  3. package/package.json +1 -1
package/index.d.ts CHANGED
@@ -48,7 +48,8 @@ type I_Map = {
48
48
  markers?: I_marker[];
49
49
  value?: I_pos;
50
50
  marker?: ReactNode | false;
51
- style?: any;
51
+ mapStyle?: any;
52
+ attrs?: any;
52
53
  onClick?: () => void;
53
54
  dragging?: boolean;
54
55
  submitText?: string;
package/index.js CHANGED
@@ -1 +1,188 @@
1
- import{divIcon as e}from"leaflet";import{createContext as r,isValidElement as t,useContext as o,useEffect as n,useRef as a,useState as l}from"react";import{Circle as i,FeatureGroup as c,LayersControl as p,MapContainer as s,Marker as m,Polyline as _,Rectangle as d,TileLayer as u,useMapEvents as h}from"react-leaflet";import f from"aio-input";import{JSXToHTML as v}from"aio-utils";import"leaflet/dist/leaflet.css";import"./index.css";import{jsx as $}from"react/jsx-runtime";import{jsxs as M}from"react/jsx-runtime";let MAPCTX=r({}),AIMap=e=>{var r;let{value:t=[35.699939,51.338497],getSearchResult:o,onSearch:i,mapRef:c}=e,[p,s]=l(null),[m,_]=l((null===(r=e.zoom)||void 0===r?void 0:r.value)||14),d=a(!1);function u(e){!0!==d.current&&_(e)}c&&(c.current=p),e.actionsRef&&(e.actionsRef.current={flyTo(e){d.current=!0,p.flyTo([e.lat,e.lng],e.zoom);let r=()=>{p.off("moveend",r),d.current=!1,u(e.zoom),e.callback()};p.on("moveend",r)}});let[h,f]=l(t),v=a(void 0);function g(r){f(r),e.onChange&&(clearTimeout(v.current),v.current=setTimeout(()=>{e.onChange&&e.onChange(r)},600))}function C(){return M("div",{className:"marker-icon",children:[$("div",{className:"marker-icon-circle"}),$("div",{className:"marker-icon-arrow"})]})}function y(){return{pos:h,setMap:s,rootProps:e,move:g,getDefaultMarkerIcon:C,changeZoom:u,zoom:m}}return n(()=>{null!==p&&p.setView(t,m,{animate:!1}),f(t)},[t[0]+"-"+t[1]+"-"+m]),$(MAPCTX.Provider,{value:y(),children:M("div",{className:"ai-map",children:[!!o&&$(MapHeader,{}),$(MapBody,{}),$(MapFooter,{})]})})};export default AIMap;let MapBody=()=>{var e,r;let{rootProps:n,pos:a,setMap:l,getDefaultMarkerIcon:i,zoom:c}=o(MAPCTX),{style:p,dragging:m=!0,children:_,shapes:d=[],marker:h,markers:f=[],whenReady:v}=n,g={width:"100%",height:"100%"};return M(s,{center:a,style:{...g,...p},zoom:c,scrollWheelZoom:null!==(e=n.zoom)&&void 0!==e&&e.wheel?"center":void 0,zoomControl:(null===(r=n.zoom)||void 0===r?void 0:r.control)!==!1,attributionControl:!0,dragging:m,ref:l,whenReady:v,children:[$(u,{url:"https://{s}.tile.openstreetmap.de/{z}/{x}/{y}.png"}),$(MapEvents,{}),!1!==h&&$(MapMarker,{pos:a,html:t(h)?h:i()},"main-marker"),f.map((e,r)=>$(MapMarker,{pos:e.pos,html:e.html,eventHandlers:e.eventHandlers},`marker-${r}`)),d.map((e,r)=>$(MapShape,{shape:e},r)),$(MapLayers,{}),_]})},MagnifyIcon=()=>$("svg",{viewBox:"0 0 24 24",xmlns:"http://www.w3.org/2000/svg",width:24,height:24,children:$("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"})}),MapHeader=()=>{let{rootProps:e,move:r}=o(MAPCTX),{getSearchResult:t,onSearch:n}=e,[i,c]=l(""),[p,s]=l([]),m=a();function _(e){c(e),clearTimeout(m.current),m.current=setTimeout(async()=>{if(t){let r=await t(e);Array.isArray(r)&&s(r)}},1200)}return $("div",{className:"ai-map-header",children:$(f,{type:"text",value:i,options:p||[],before:$(MagnifyIcon,{}),onChange:e=>_(e),option:{onClick(e){n&&n(e)}}})})},MapLayers=()=>{let{rootProps:e}=o(MAPCTX),{layers:r}=e;if(!r)return null;let{position:t="topright",items:n=[]}=r;return $(p,{position:t,children:n.map((e,r)=>{let{shapes:t=[],markers:o=[],active:n=!0}=e;return $(p.Overlay,{name:e.name,checked:n,children:M(c,{children:[o.map((e,r)=>$(MapMarker,{pos:e.pos,html:e.html,eventHandlers:e.eventHandlers},r)),t.map((e,r)=>$(MapShape,{shape:e},r))]})},r)})})},MapShape=({shape:e})=>{let{style:r={},type:t}=e,{stroke:o={},fill:n={}}=r,{width:a=4,dash:l,color:c="orange"}=o,{color:p="orange",opacity:s=.3}=n,m={fillColor:p,color:c,fillOpacity:s,weight:a,dashArray:l};if("circle"===t){let{center:u,radius:h=100}=e;return $(i,{center:u,pathOptions:m,radius:h})}return"rect"===t?$(d,{bounds:e.points,pathOptions:m}):"polyline"===t?$(_,{positions:e.points,pathOptions:m}):null},MapFooter=()=>{let{rootProps:e,pos:r}=o(MAPCTX),{submitText:t="Submit",onSubmit:n,footer:a}=e;return n||a?M("div",{className:"ai-map-footer",children:[!!n&&$("button",{type:"button",onClick:()=>n(r),children:t}),$("div",{className:"ai-map-footer-html",children:a||null})]}):null},MapMarker=({pos:r,html:t,eventHandlers:n})=>{let{getDefaultMarkerIcon:a}=o(MAPCTX);function l(r){return e({html:v(r),className:"",iconSize:[32,32],iconAnchor:[16,32]})}t=t||a();let i={position:r};return t&&(i.icon=l(t)),$(m,{...i,animate:!1,eventHandlers:n})};function MapEvents(){let{rootProps:e,move:r,changeZoom:t}=o(MAPCTX);return h({click:()=>e.onClick?e.onClick():void 0,move(t){if(!1===e.dragging)return;let{lat:o,lng:n}=t.target.getCenter();r([o,n])},zoom(e){t(e.target._zoom)},locationfound(e){console.log("location found:",e)},moveend(r){e.onMoveEnd&&e.onMoveEnd(r)},movestart(e){}}),null}
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
11
+ import { divIcon } from "leaflet";
12
+ import { createContext, isValidElement, useContext, useEffect, useRef, useState } from "react";
13
+ import { Circle, FeatureGroup, LayersControl, MapContainer, Marker, Polyline, Rectangle, TileLayer, useMapEvents } from "react-leaflet";
14
+ import AIOInput from "aio-input";
15
+ import { AddToAttrs, JSXToHTML } from "aio-utils";
16
+ import 'leaflet/dist/leaflet.css';
17
+ import './index.css';
18
+ const MAPCTX = createContext({});
19
+ const AIMap = (props) => {
20
+ var _a;
21
+ const { value = [35.699939, 51.338497], getSearchResult, onSearch, mapRef } = props;
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) {
27
+ return;
28
+ }
29
+ setZoom(zoom);
30
+ }
31
+ if (mapRef) {
32
+ mapRef.current = map;
33
+ }
34
+ if (props.actionsRef) {
35
+ props.actionsRef.current = {
36
+ flyTo: (p) => {
37
+ lockChangeZoomState.current = true;
38
+ map.flyTo([p.lat, p.lng], p.zoom);
39
+ const onMoveEnd = () => {
40
+ map.off("moveend", onMoveEnd);
41
+ lockChangeZoomState.current = false;
42
+ changeZoom(p.zoom);
43
+ p.callback();
44
+ };
45
+ map.on("moveend", onMoveEnd);
46
+ }
47
+ };
48
+ }
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);
58
+ }
59
+ }
60
+ function getDefaultMarkerIcon() {
61
+ return _jsxs("div", { className: 'marker-icon', children: [_jsx("div", { className: 'marker-icon-circle' }), _jsx("div", { className: 'marker-icon-arrow' })] });
62
+ }
63
+ function getContext() { return { pos, setMap, rootProps: props, move, getDefaultMarkerIcon, changeZoom, zoom }; }
64
+ useEffect(() => {
65
+ if (map !== null) {
66
+ map.setView(value, zoom, { animate: false });
67
+ }
68
+ setPos(value);
69
+ }, [value[0] + '-' + value[1] + '-' + zoom]);
70
+ const attrs = AddToAttrs(props.attrs, { className: 'ai-map' });
71
+ return (_jsx(MAPCTX.Provider, { value: getContext(), children: _jsxs("div", Object.assign({}, attrs, { children: [!!getSearchResult && _jsx(MapHeader, {}), _jsx(MapBody, {}), _jsx(MapFooter, {})] })) }));
72
+ };
73
+ export default AIMap;
74
+ const MapBody = () => {
75
+ var _a, _b;
76
+ const { rootProps, pos, setMap, getDefaultMarkerIcon, zoom } = useContext(MAPCTX);
77
+ const { mapStyle, dragging = true, children, shapes = [], marker, markers = [], whenReady } = rootProps;
78
+ 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] }));
80
+ };
81
+ const MagnifyIcon = () => {
82
+ 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" }) }));
83
+ };
84
+ const MapHeader = () => {
85
+ const { rootProps, move } = useContext(MAPCTX);
86
+ const { getSearchResult, onSearch } = rootProps;
87
+ const [searchValue, setSearchValue] = useState('');
88
+ const [searchResult, setSerachResult] = useState([]);
89
+ const timeout = useRef();
90
+ function changeSearch(searchValue) {
91
+ setSearchValue(searchValue);
92
+ clearTimeout(timeout.current);
93
+ timeout.current = setTimeout(() => __awaiter(this, void 0, void 0, function* () {
94
+ if (getSearchResult) {
95
+ const res = yield getSearchResult(searchValue);
96
+ if (Array.isArray(res)) {
97
+ setSerachResult(res);
98
+ }
99
+ }
100
+ }), 1200);
101
+ }
102
+ return (_jsx("div", { className: "ai-map-header", children: _jsx(AIOInput, { type: 'text', value: searchValue, options: (searchResult || []), before: _jsx(MagnifyIcon, {}), onChange: (searchValue) => changeSearch(searchValue), option: {
103
+ onClick: (option) => { if (onSearch) {
104
+ onSearch(option);
105
+ } }
106
+ } }) }));
107
+ };
108
+ const MapLayers = () => {
109
+ const { rootProps } = useContext(MAPCTX);
110
+ const { layers } = rootProps;
111
+ if (!layers) {
112
+ return null;
113
+ }
114
+ 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
+ }) }));
119
+ };
120
+ const MapShape = ({ shape }) => {
121
+ const { style = {}, type } = shape, { stroke = {}, fill = {} } = style;
122
+ const { width = 4, dash, color: strokeColor = 'orange' } = stroke;
123
+ const { color: fillColor = 'orange', opacity = 0.3 } = fill;
124
+ const pathOptions = { fillColor: fillColor, color: strokeColor, fillOpacity: opacity, weight: width, dashArray: dash };
125
+ if (type === 'circle') {
126
+ const { center, radius = 100 } = shape;
127
+ return (_jsx(Circle, { center: center, pathOptions: pathOptions, radius: radius }));
128
+ }
129
+ else if (type === 'rect') {
130
+ return (_jsx(Rectangle, { bounds: shape.points, pathOptions: pathOptions }));
131
+ }
132
+ else if (type === 'polyline') {
133
+ return (_jsx(Polyline, { positions: shape.points, pathOptions: pathOptions }));
134
+ }
135
+ return null;
136
+ };
137
+ const MapFooter = () => {
138
+ const { rootProps, pos } = useContext(MAPCTX);
139
+ const { submitText = 'Submit', onSubmit, footer } = rootProps;
140
+ if (!onSubmit && !footer) {
141
+ return null;
142
+ }
143
+ return (_jsxs("div", { className: "ai-map-footer", children: [!!onSubmit && _jsx("button", { type: 'button', onClick: () => onSubmit(pos), children: submitText }), _jsx("div", { className: "ai-map-footer-html", children: footer || null })] }));
144
+ };
145
+ const MapMarker = ({ pos, html, eventHandlers }) => {
146
+ const { getDefaultMarkerIcon } = useContext(MAPCTX);
147
+ function getHtmlIcon(html) {
148
+ return divIcon({
149
+ html: JSXToHTML(html),
150
+ className: '',
151
+ iconSize: [32, 32],
152
+ iconAnchor: [16, 32], // point of the icon which will correspond to marker's location
153
+ });
154
+ }
155
+ html = html || getDefaultMarkerIcon();
156
+ let props = { position: pos };
157
+ if (html) {
158
+ props.icon = getHtmlIcon(html);
159
+ }
160
+ return _jsx(Marker, Object.assign({}, props, { animate: false, eventHandlers: eventHandlers }));
161
+ };
162
+ function MapEvents() {
163
+ const { rootProps, move, changeZoom } = useContext(MAPCTX);
164
+ const map = useMapEvents({
165
+ click: () => rootProps.onClick ? rootProps.onClick() : undefined,
166
+ move: (e) => {
167
+ if (rootProps.dragging === false) {
168
+ return;
169
+ }
170
+ let { lat, lng } = e.target.getCenter();
171
+ move([lat, lng]);
172
+ },
173
+ zoom: (e) => {
174
+ changeZoom(e.target._zoom);
175
+ },
176
+ locationfound: (location) => {
177
+ console.log('location found:', location);
178
+ },
179
+ moveend: (e) => {
180
+ if (rootProps.onMoveEnd) {
181
+ rootProps.onMoveEnd(e);
182
+ }
183
+ },
184
+ movestart: (e) => {
185
+ }
186
+ });
187
+ return null;
188
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aio-map",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "",
5
5
  "types": "index.d.ts",
6
6
  "main": "index.js",