nicecode-v2/packages/map/es/MapBox.js
2024-05-29 14:43:41 +08:00

195 lines
13 KiB
JavaScript

function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
var _excluded = ["style", "children", "height", "width", "draw", "markerData", "sluterData", "popUpInfo", "showPopUp", "toolsBarOpen", "showMarker", "showCluster", "clusterProps", "interactiveLayerIds", "buttonList", "onMarkerClick", "onPopUpClose", "customMarkerRender", "onLoad", "onDrawCreate", "onDrawUpdate", "onDrawDelete", "onToolClick"];
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
import 'mapbox-gl/dist/mapbox-gl.css';
import '@mapbox/mapbox-gl-draw/dist/mapbox-gl-draw.css';
import React, { forwardRef, useImperativeHandle, useMemo, useRef, useState } from 'react';
import Map from 'react-map-gl';
import classnames from 'classnames';
import { merge } from '@zhst/func';
import Tools from "./components/tools";
import DrawControl from "./components/drawControl";
import { defaultMapConfig } from "./utils/constants";
import "./index.less";
import mapboxDrawStyle from "./utils/drawStyle";
import Marker from "./components/marker";
import PopUp from "./components/popup";
import Cluster from "./components/clusters/Clusters";
import { clusterLayer } from "./components/clusters/layers";
var componentName = 'zhst-map';
var MapBox = /*#__PURE__*/forwardRef(function (props, ref) {
var _ref = props || {},
_ref$style = _ref.style,
style = _ref$style === void 0 ? {} : _ref$style,
children = _ref.children,
_ref$height = _ref.height,
height = _ref$height === void 0 ? 600 : _ref$height,
_ref$width = _ref.width,
width = _ref$width === void 0 ? '100%' : _ref$width,
draw = _ref.draw,
_ref$markerData = _ref.markerData,
markerData = _ref$markerData === void 0 ? [] : _ref$markerData,
_ref$sluterData = _ref.sluterData,
sluterData = _ref$sluterData === void 0 ? [] : _ref$sluterData,
_ref$popUpInfo = _ref.popUpInfo,
popUpInfo = _ref$popUpInfo === void 0 ? {
longitude: 0,
latitude: 0
} : _ref$popUpInfo,
showPopUp = _ref.showPopUp,
toolsBarOpen = _ref.toolsBarOpen,
_ref$showMarker = _ref.showMarker,
showMarker = _ref$showMarker === void 0 ? true : _ref$showMarker,
_ref$showCluster = _ref.showCluster,
showCluster = _ref$showCluster === void 0 ? false : _ref$showCluster,
clusterProps = _ref.clusterProps,
_ref$interactiveLayer = _ref.interactiveLayerIds,
interactiveLayerIds = _ref$interactiveLayer === void 0 ? [] : _ref$interactiveLayer,
_ref$buttonList = _ref.buttonList,
buttonList = _ref$buttonList === void 0 ? [{
label: '圆形框选',
key: 'circle',
icon: 'icon-yuan',
onClick: function onClick() {
var _drawControlRef$curre, _drawControlRef$curre2;
return (_drawControlRef$curre = drawControlRef.current) === null || _drawControlRef$curre === void 0 || (_drawControlRef$curre = _drawControlRef$curre.drawer) === null || _drawControlRef$curre === void 0 || (_drawControlRef$curre2 = _drawControlRef$curre.changeMode) === null || _drawControlRef$curre2 === void 0 ? void 0 : _drawControlRef$curre2.call(_drawControlRef$curre, 'simple_select');
}
}, {
label: '矩形框选',
key: 'rect',
icon: 'icon-fang',
onClick: function onClick() {
var _drawControlRef$curre3, _drawControlRef$curre4;
return (_drawControlRef$curre3 = drawControlRef.current) === null || _drawControlRef$curre3 === void 0 || (_drawControlRef$curre3 = _drawControlRef$curre3.drawer) === null || _drawControlRef$curre3 === void 0 || (_drawControlRef$curre4 = _drawControlRef$curre3.changeMode) === null || _drawControlRef$curre4 === void 0 ? void 0 : _drawControlRef$curre4.call(_drawControlRef$curre3, 'draw_rect');
},
popoverProps: {
placement: 'bottom',
content: '自定义内容'
}
}, {
label: '多边形框选',
key: 'more',
icon: 'icon-duobianxing',
onClick: function onClick() {
var _drawControlRef$curre5, _drawControlRef$curre6;
return (_drawControlRef$curre5 = drawControlRef.current) === null || _drawControlRef$curre5 === void 0 || (_drawControlRef$curre5 = _drawControlRef$curre5.drawer) === null || _drawControlRef$curre5 === void 0 || (_drawControlRef$curre6 = _drawControlRef$curre5.changeMode) === null || _drawControlRef$curre6 === void 0 ? void 0 : _drawControlRef$curre6.call(_drawControlRef$curre5, 'draw_polygon');
}
}, {
label: '路径框选',
key: 'path',
icon: 'icon-lujingkuangxuannor',
onClick: function onClick() {
var _drawControlRef$curre7, _drawControlRef$curre8;
return (_drawControlRef$curre7 = drawControlRef.current) === null || _drawControlRef$curre7 === void 0 || (_drawControlRef$curre7 = _drawControlRef$curre7.drawer) === null || _drawControlRef$curre7 === void 0 || (_drawControlRef$curre8 = _drawControlRef$curre7.changeMode) === null || _drawControlRef$curre8 === void 0 ? void 0 : _drawControlRef$curre8.call(_drawControlRef$curre7, 'draw_line_string');
}
}, {
label: '测距',
key: 'path',
icon: 'icon-ceju',
onClick: function onClick() {
var _drawControlRef$curre9, _drawControlRef$curre10;
return (_drawControlRef$curre9 = drawControlRef.current) === null || _drawControlRef$curre9 === void 0 || (_drawControlRef$curre9 = _drawControlRef$curre9.drawer) === null || _drawControlRef$curre9 === void 0 || (_drawControlRef$curre10 = _drawControlRef$curre9.changeMode) === null || _drawControlRef$curre10 === void 0 ? void 0 : _drawControlRef$curre10.call(_drawControlRef$curre9, 'draw_line_string');
}
}, {
label: '清除',
key: 'clear',
icon: 'icon-gongjuxiangguanbi',
onClick: function onClick() {
var _drawControlRef$curre11;
return (_drawControlRef$curre11 = drawControlRef.current) === null || _drawControlRef$curre11 === void 0 || (_drawControlRef$curre11 = _drawControlRef$curre11.drawer) === null || _drawControlRef$curre11 === void 0 ? void 0 : _drawControlRef$curre11.deleteAll();
}
}] : _ref$buttonList,
onMarkerClick = _ref.onMarkerClick,
onPopUpClose = _ref.onPopUpClose,
customMarkerRender = _ref.customMarkerRender,
onLoad = _ref.onLoad,
onDrawCreate = _ref.onDrawCreate,
onDrawUpdate = _ref.onDrawUpdate,
onDrawDelete = _ref.onDrawDelete,
onToolClick = _ref.onToolClick,
others = _objectWithoutProperties(_ref, _excluded);
var mapRef = useRef(null);
var drawControlRef = useRef(null);
// 默认绘制配置
var _useState = useState({
displayControlsDefault: false,
position: 'top-left',
styles: mapboxDrawStyle,
// Select which mapbox-gl-draw control buttons to add to the map.
// @ts-ignore
controls: false,
// The user does not have to click the polygon control button first.
defaultMode: 'draw_polygon'
}),
_useState2 = _slicedToArray(_useState, 1),
drawConfig = _useState2[0];
var initMarker = useMemo(function () {
return markerData.map(function (_item) {
return /*#__PURE__*/React.createElement(Marker, _extends({
customMarkerRender: customMarkerRender,
onMarkerClick: onMarkerClick
}, _item));
});
}, [markerData]);
useImperativeHandle(ref, function () {
var _drawControlRef$curre12;
return {
mapRef: mapRef.current,
drawer: (_drawControlRef$curre12 = drawControlRef.current) === null || _drawControlRef$curre12 === void 0 ? void 0 : _drawControlRef$curre12.drawer
};
});
return (
/*#__PURE__*/
//@ts-ignore
React.createElement("div", {
className: classnames("".concat(componentName))
}, /*#__PURE__*/React.createElement(Tools, {
open: toolsBarOpen,
buttonList: buttonList,
onToolClick: onToolClick
}), /*#__PURE__*/React.createElement(Map, _extends({
ref: mapRef,
onLoad: onLoad,
style: _objectSpread({
width: width,
height: height
}, style),
interactiveLayerIds: [clusterLayer.id].concat(_toConsumableArray(interactiveLayerIds))
}, merge(defaultMapConfig, others)), showMarker && initMarker, showPopUp && popUpInfo && /*#__PURE__*/React.createElement(PopUp, _extends({
onClose: onPopUpClose
}, popUpInfo)), showCluster && !showMarker && /*#__PURE__*/React.createElement(Cluster, _extends({
type: "geojson",
cluster: true,
clusterMaxZoom: 14,
clusterRadius: 50,
data: sluterData
}, clusterProps)), draw && /*#__PURE__*/React.createElement(DrawControl, _extends({
ref: drawControlRef,
onCreate: onDrawCreate,
onUpdate: onDrawUpdate,
onDelete: onDrawDelete,
onSelectionChange: function onSelectionChange(e) {
return console.log('e', e);
}
}, drawConfig)), children))
);
});
export default MapBox;