@pansy/react-mapbox-gl
Version:
🌍 基于 Mapbox GL 封装的 React 组件库
74 lines (72 loc) • 2.28 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
for (var prop in b || (b = {}))
if (__hasOwnProp.call(b, prop))
__defNormalProp(a, prop, b[prop]);
if (__getOwnPropSymbols)
for (var prop of __getOwnPropSymbols(b)) {
if (__propIsEnum.call(b, prop))
__defNormalProp(a, prop, b[prop]);
}
return a;
};
// src/components/Source/utils.ts
import { assert } from "../../utils/assert";
import { isStyleLoaded } from "../../utils/isStyleLoaded";
import { deepEqual } from "../../utils/deepEqual";
function createSource(map, id, props) {
if (isStyleLoaded(map)) {
const options = __spreadValues({}, props);
delete options.id;
delete options.children;
map.addSource(id, options);
return map.getSource(id);
}
}
function updateSource(source, props, prevProps) {
assert(props.id === prevProps.id, "source id changed");
assert(props.type === prevProps.type, "source type changed");
let changedKey = "";
let changedKeyCount = 0;
for (const key in props) {
if (key !== "children" && key !== "id" && // @ts-ignore
!deepEqual(prevProps[key], props[key])) {
changedKey = key;
changedKeyCount++;
}
}
if (!changedKeyCount) {
return;
}
const type = props.type;
if (type === "geojson") {
source.setData(props.data);
} else if (type === "image") {
source.updateImage({
url: props.url,
coordinates: props.coordinates
});
} else if ("setCoordinates" in source && changedKeyCount === 1 && changedKey === "coordinates") {
source.setCoordinates(props.coordinates);
} else if ("setUrl" in source) {
switch (changedKey) {
case "url":
source.setUrl(props.url);
break;
case "tiles":
source.setTiles(props.tiles);
break;
default:
}
} else {
console.warn(`Unable to update <Source> prop: ${changedKey}`);
}
}
export {
createSource,
updateSource
};