@macrostrat/map-interface 2.1.0 → 2.2.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/CHANGELOG.md +13 -0
- package/dist/map-view.cjs +23 -23
- package/dist/map-view.cjs.map +1 -1
- package/dist/map-view.d.ts +7 -1
- package/dist/map-view.js +17 -16
- package/dist/map-view.js.map +1 -1
- package/package.json +4 -5
- package/src/map-view.ts +24 -14
package/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,19 @@ All notable changes to this project will be documented in this file. The format
|
|
|
4
4
|
is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this
|
|
5
5
|
project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
6
6
|
|
|
7
|
+
## [2.2.1] - 2026-02-13
|
|
8
|
+
|
|
9
|
+
- Add `width` and `height` props to `MapView`
|
|
10
|
+
- Add a default `style` to `MapView` (Mapbox Streets)
|
|
11
|
+
- Don't set `id` property by default on `MapView`
|
|
12
|
+
- New stories for `MapView`
|
|
13
|
+
|
|
14
|
+
## [2.2.0] - 2026-02-13
|
|
15
|
+
|
|
16
|
+
- Add internal logic to `MapView` to manage the addition of custom style
|
|
17
|
+
fragments, based on updates to the `@macrostrat/mapbox-react` package
|
|
18
|
+
- Add new stories
|
|
19
|
+
|
|
7
20
|
## [2.1.0] - 2026-02-09
|
|
8
21
|
|
|
9
22
|
- Remove xDD info panel components
|
package/dist/map-view.cjs
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
-
const h$1 = require("@macrostrat/hyper");
|
|
4
3
|
const mapboxReact = require("@macrostrat/mapbox-react");
|
|
5
4
|
const mapboxUtils = require("@macrostrat/mapbox-utils");
|
|
6
5
|
const classNames = require("classnames");
|
|
@@ -12,10 +11,8 @@ const helpers = require("./helpers.cjs");
|
|
|
12
11
|
const utils = require("./utils.cjs");
|
|
13
12
|
const uiComponents = require("@macrostrat/ui-components");
|
|
14
13
|
const _interopDefault = (e) => e && e.__esModule ? e : { default: e };
|
|
15
|
-
const h__default = /* @__PURE__ */ _interopDefault(h$1);
|
|
16
14
|
const classNames__default = /* @__PURE__ */ _interopDefault(classNames);
|
|
17
15
|
const mapboxgl__default = /* @__PURE__ */ _interopDefault(mapboxgl);
|
|
18
|
-
const h = h__default.default.styled(main_module.default);
|
|
19
16
|
function defaultInitializeMap(container, args = {}) {
|
|
20
17
|
const { mapPosition, ...rest } = args;
|
|
21
18
|
const map = new mapboxgl__default.default.Map({
|
|
@@ -48,8 +45,10 @@ const defaultMapPosition = {
|
|
|
48
45
|
function MapView(props) {
|
|
49
46
|
let { terrainSourceID } = props;
|
|
50
47
|
const {
|
|
48
|
+
height,
|
|
49
|
+
width,
|
|
51
50
|
enableTerrain = true,
|
|
52
|
-
style,
|
|
51
|
+
style = "mapbox://styles/mapbox/streets-v11",
|
|
53
52
|
mapPosition,
|
|
54
53
|
initializeMap = defaultInitializeMap,
|
|
55
54
|
children,
|
|
@@ -67,13 +66,10 @@ function MapView(props) {
|
|
|
67
66
|
transformStyle,
|
|
68
67
|
trackResize = true,
|
|
69
68
|
loadingIgnoredSources = ["elevationMarker", "crossSectionEndpoints"],
|
|
70
|
-
id
|
|
69
|
+
id,
|
|
71
70
|
className,
|
|
72
71
|
...rest
|
|
73
72
|
} = props;
|
|
74
|
-
if (enableTerrain) {
|
|
75
|
-
terrainSourceID ??= "mapbox-3d-dem";
|
|
76
|
-
}
|
|
77
73
|
React.useEffect(() => {
|
|
78
74
|
if (id != null) {
|
|
79
75
|
console.warn(
|
|
@@ -90,6 +86,7 @@ function MapView(props) {
|
|
|
90
86
|
const ref = React.useRef();
|
|
91
87
|
const parentRef = React.useRef();
|
|
92
88
|
const [baseStyle, setBaseStyle] = React.useState(null);
|
|
89
|
+
const _ctxOverlayStyles = mapboxReact.useMapStyleFragments();
|
|
93
90
|
const estMapPosition = mapRef.current == null ? mapPosition : mapboxUtils.getMapPosition(mapRef.current);
|
|
94
91
|
const { mapUse3D, mapIsRotated } = mapboxUtils.mapViewInfo(estMapPosition);
|
|
95
92
|
const is3DAvailable = (mapUse3D ?? false) && enableTerrain;
|
|
@@ -97,9 +94,9 @@ function MapView(props) {
|
|
|
97
94
|
if (baseStyle == null) return;
|
|
98
95
|
let map = mapRef.current;
|
|
99
96
|
let newStyle = baseStyle;
|
|
100
|
-
const
|
|
101
|
-
if (
|
|
102
|
-
newStyle = mapboxUtils.mergeStyles(newStyle, ...
|
|
97
|
+
const _overlayStyles = overlayStyles ?? [];
|
|
98
|
+
if (_overlayStyles.length > 0 || _ctxOverlayStyles.length > 0) {
|
|
99
|
+
newStyle = mapboxUtils.mergeStyles(newStyle, ..._overlayStyles, ..._ctxOverlayStyles);
|
|
103
100
|
}
|
|
104
101
|
if (is3DAvailable) {
|
|
105
102
|
const terrainStyle = mapboxReact.getTerrainLayerForStyle(newStyle, terrainSourceID);
|
|
@@ -123,7 +120,7 @@ function MapView(props) {
|
|
|
123
120
|
map2.setPadding(utils.getMapPadding(ref, parentRef), { animate: false });
|
|
124
121
|
onMapLoaded?.(map2);
|
|
125
122
|
}
|
|
126
|
-
}, [baseStyle, overlayStyles, transformStyle]);
|
|
123
|
+
}, [baseStyle, overlayStyles, _ctxOverlayStyles, transformStyle]);
|
|
127
124
|
uiComponents.useAsyncEffect(async () => {
|
|
128
125
|
let newStyle;
|
|
129
126
|
if (typeof style === "string") {
|
|
@@ -149,25 +146,29 @@ function MapView(props) {
|
|
|
149
146
|
},
|
|
150
147
|
className
|
|
151
148
|
);
|
|
152
|
-
|
|
149
|
+
const containerStyle = {
|
|
150
|
+
height,
|
|
151
|
+
width
|
|
152
|
+
};
|
|
153
|
+
return main_module.default(
|
|
153
154
|
"div.map-view-container.main-view",
|
|
154
|
-
{ ref: parentRef, className: parentClassName },
|
|
155
|
+
{ ref: parentRef, className: parentClassName, style: containerStyle },
|
|
155
156
|
[
|
|
156
|
-
|
|
157
|
-
|
|
157
|
+
main_module.default("div.mapbox-map.map-view", { ref, className: mapClassName, id }),
|
|
158
|
+
main_module.default(helpers.MapLoadingReporter, {
|
|
158
159
|
ignoredSources: loadingIgnoredSources
|
|
159
160
|
}),
|
|
160
|
-
|
|
161
|
-
|
|
161
|
+
main_module.default(StyleLoadedReporter, { onStyleLoaded }),
|
|
162
|
+
main_module.default(helpers.MapMovedReporter, { onMapMoved }),
|
|
162
163
|
// Subsitute for trackResize: true that allows map resizing to
|
|
163
164
|
// be tied to a specific ref component
|
|
164
|
-
|
|
165
|
-
|
|
165
|
+
main_module.default.if(trackResize)(helpers.MapResizeManager, { containerRef: ref }),
|
|
166
|
+
main_module.default(helpers.MapPaddingManager, {
|
|
166
167
|
containerRef: ref,
|
|
167
168
|
parentRef,
|
|
168
169
|
infoMarkerPosition
|
|
169
170
|
}),
|
|
170
|
-
|
|
171
|
+
main_module.default(MapTerrainManager, { mapUse3D: is3DAvailable, terrainSourceID, style }),
|
|
171
172
|
children
|
|
172
173
|
]
|
|
173
174
|
);
|
|
@@ -193,8 +194,7 @@ function StyleLoadedReporter({ onStyleLoaded = null }) {
|
|
|
193
194
|
}
|
|
194
195
|
function MapTerrainManager({
|
|
195
196
|
mapUse3D,
|
|
196
|
-
terrainSourceID
|
|
197
|
-
style
|
|
197
|
+
terrainSourceID
|
|
198
198
|
}) {
|
|
199
199
|
mapboxReact.use3DTerrain(mapUse3D, terrainSourceID);
|
|
200
200
|
return null;
|
package/dist/map-view.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"map-view.cjs","sources":["../src/map-view.ts"],"sourcesContent":["import hyper from \"@macrostrat/hyper\";\nimport {\n useMapRef,\n useMapDispatch,\n use3DTerrain,\n getTerrainLayerForStyle,\n useMapStatus,\n} from \"@macrostrat/mapbox-react\";\nimport React from \"react\";\nimport {\n mapViewInfo,\n MapPosition,\n setMapPosition,\n getMapPosition,\n getMapboxStyle,\n mergeStyles,\n} from \"@macrostrat/mapbox-utils\";\nimport classNames from \"classnames\";\nimport mapboxgl from \"mapbox-gl\";\nimport { useEffect, useRef, useState } from \"react\";\nimport styles from \"./main.module.sass\";\nimport {\n MapLoadingReporter,\n MapMovedReporter,\n MapPaddingManager,\n MapResizeManager,\n} from \"./helpers\";\nimport \"mapbox-gl/dist/mapbox-gl.css\";\nimport { getMapPadding } from \"./utils\";\nimport { useAsyncEffect } from \"@macrostrat/ui-components\";\n\nconst h = hyper.styled(styles);\n\ntype MapboxCoreOptions = Omit<mapboxgl.MapboxOptions, \"container\">;\n\nexport interface MapViewProps extends MapboxCoreOptions {\n showLineSymbols?: boolean;\n children?: React.ReactNode;\n mapboxToken?: string;\n // Deprecated\n accessToken?: string;\n terrainSourceID?: string;\n enableTerrain?: boolean;\n infoMarkerPosition?: mapboxgl.LngLatLike;\n mapPosition?: MapPosition;\n initializeMap?: (\n container: HTMLElement,\n args: MapboxOptionsExt,\n ) => mapboxgl.Map;\n onMapLoaded?: (map: mapboxgl.Map) => void;\n onStyleLoaded?: (map: mapboxgl.Map) => void;\n onMapMoved?: (mapPosition: MapPosition, map: mapboxgl.Map) => void;\n /** This map sets its own viewport, rather than being positioned by a parent.\n * This is a hack to ensure that the map can overflow its \"safe area\" when false */\n standalone?: boolean;\n /** Overlay styles to apply to the map: a list of mapbox style objects or fragments to\n * overlay on top of the main map style at runtime */\n overlayStyles?: Partial<mapboxgl.StyleSpecification>[];\n /** A function to transform the map style before it is loaded */\n transformStyle?: (\n style: mapboxgl.StyleSpecification,\n ) => mapboxgl.StyleSpecification;\n loadingIgnoredSources?: string[];\n id?: string;\n className?: string;\n}\n\nexport interface MapboxOptionsExt extends MapboxCoreOptions {\n mapPosition?: MapPosition;\n}\n\nfunction defaultInitializeMap(container, args: MapboxOptionsExt = {}) {\n const { mapPosition, ...rest } = args;\n\n const map = new mapboxgl.Map({\n container,\n maxZoom: 18,\n logoPosition: \"bottom-left\",\n trackResize: false,\n antialias: true,\n // This is a legacy option for Mapbox GL v2\n // @ts-ignore\n optimizeForTerrain: true,\n ...rest,\n });\n\n let _mapPosition = mapPosition;\n if (_mapPosition == null && rest.center == null && rest.bounds == null) {\n // If no map positioning information is provided, we use the default\n _mapPosition = defaultMapPosition;\n }\n\n // set initial map position\n if (_mapPosition != null) {\n setMapPosition(map, _mapPosition);\n }\n\n return map;\n}\n\nconst defaultMapPosition: MapPosition = {\n camera: {\n lat: 34,\n lng: -120,\n altitude: 300000,\n },\n};\n\nexport function MapView(props: MapViewProps) {\n let { terrainSourceID } = props;\n const {\n enableTerrain = true,\n style,\n mapPosition,\n initializeMap = defaultInitializeMap,\n children,\n mapboxToken,\n // Deprecated\n accessToken,\n infoMarkerPosition,\n transformRequest,\n projection,\n onMapLoaded = null,\n onStyleLoaded = null,\n onMapMoved = null,\n standalone = false,\n overlayStyles,\n transformStyle,\n trackResize = true,\n loadingIgnoredSources = [\"elevationMarker\", \"crossSectionEndpoints\"],\n id = \"map\",\n className,\n ...rest\n } = props;\n if (enableTerrain) {\n terrainSourceID ??= \"mapbox-3d-dem\";\n }\n\n useEffect(() => {\n if (id != null) {\n console.warn(\n \"Setting a specific element ID for the map is deprecated. Please use className instead.\",\n );\n }\n }, [id]);\n\n const _mapboxToken = mapboxToken ?? accessToken;\n\n if (_mapboxToken != null) {\n mapboxgl.accessToken = _mapboxToken;\n }\n\n const dispatch = useMapDispatch();\n let mapRef = useMapRef();\n const ref = useRef<HTMLDivElement>();\n const parentRef = useRef<HTMLDivElement>();\n\n const [baseStyle, setBaseStyle] = useState<mapboxgl.Style>(null);\n\n const estMapPosition: MapPosition | null =\n mapRef.current == null ? mapPosition : getMapPosition(mapRef.current);\n const { mapUse3D, mapIsRotated } = mapViewInfo(estMapPosition);\n const is3DAvailable = (mapUse3D ?? false) && enableTerrain;\n\n useEffect(() => {\n /** Manager to update map style */\n if (baseStyle == null) return;\n let map = mapRef.current;\n\n let newStyle: mapboxgl.StyleSpecification = baseStyle;\n\n const overlayStyles = props.overlayStyles ?? [];\n\n if (overlayStyles.length > 0) {\n newStyle = mergeStyles(newStyle, ...overlayStyles);\n }\n\n /** If we can, we try to update the map style with terrain information\n * immediately, before the style is loaded. This allows us to avoid a\n * flash of the map without terrain.\n *\n * To do this, we need to estimate the map position before load, which\n * doesn't always work.\n */\n if (is3DAvailable) {\n // We can update the style with terrain layers immediately\n const terrainStyle = getTerrainLayerForStyle(newStyle, terrainSourceID);\n newStyle = mergeStyles(newStyle, terrainStyle);\n }\n\n if (transformStyle != null) {\n newStyle = transformStyle(newStyle);\n }\n\n if (map != null) {\n dispatch({ type: \"set-style-loaded\", payload: false });\n map.setStyle(newStyle);\n } else {\n const map = initializeMap(ref.current, {\n style: newStyle,\n projection,\n mapPosition,\n transformRequest,\n ...rest,\n });\n dispatch({ type: \"set-map\", payload: map });\n map.setPadding(getMapPadding(ref, parentRef), { animate: false });\n onMapLoaded?.(map);\n }\n }, [baseStyle, overlayStyles, transformStyle]);\n\n useAsyncEffect(async () => {\n /** Manager to update map style */\n let newStyle: mapboxgl.StyleSpecification;\n if (typeof style === \"string\") {\n newStyle = await getMapboxStyle(style, {\n access_token: mapboxgl.accessToken,\n });\n } else {\n newStyle = style;\n }\n setBaseStyle(newStyle);\n }, [style]);\n\n // Get map projection\n const _projection = mapRef.current?.getProjection()?.name ?? \"mercator\";\n\n const mapClassName = classNames(\n {\n \"is-rotated\": mapIsRotated ?? false,\n \"is-3d-available\": is3DAvailable,\n },\n `${_projection}-projection`,\n );\n\n const parentClassName = classNames(\n {\n standalone,\n },\n className,\n );\n\n return h(\n \"div.map-view-container.main-view\",\n { ref: parentRef, className: parentClassName },\n [\n h(\"div.mapbox-map.map-view\", { ref, className: mapClassName, id }),\n h(MapLoadingReporter, {\n ignoredSources: loadingIgnoredSources,\n }),\n h(StyleLoadedReporter, { onStyleLoaded }),\n h(MapMovedReporter, { onMapMoved }),\n // Subsitute for trackResize: true that allows map resizing to\n // be tied to a specific ref component\n h.if(trackResize)(MapResizeManager, { containerRef: ref }),\n h(MapPaddingManager, {\n containerRef: ref,\n parentRef,\n infoMarkerPosition,\n }),\n h(MapTerrainManager, { mapUse3D: is3DAvailable, terrainSourceID, style }),\n children,\n ],\n );\n}\n\nfunction StyleLoadedReporter({ onStyleLoaded = null }) {\n /** Check back every 0.1 seconds to see if the map has loaded.\n * We do it this way because mapboxgl loading events are unreliable */\n const isStyleLoaded = useMapStatus((state) => state.isStyleLoaded);\n const mapRef = useMapRef();\n const dispatch = useMapDispatch();\n\n useEffect(() => {\n if (isStyleLoaded) return;\n const interval = setInterval(() => {\n const map = mapRef.current;\n if (map == null) return;\n if (map.isStyleLoaded()) {\n // Wait a tick before setting the style loaded state\n dispatch({ type: \"set-style-loaded\", payload: true });\n onStyleLoaded?.(map);\n clearInterval(interval);\n }\n }, 50);\n return () => clearInterval(interval);\n }, [isStyleLoaded]);\n\n return null;\n}\n\nexport function MapTerrainManager({\n mapUse3D,\n terrainSourceID,\n style,\n}: {\n mapUse3D?: boolean;\n terrainSourceID?: string;\n style?: mapboxgl.StyleSpecification | string;\n}) {\n use3DTerrain(mapUse3D, terrainSourceID);\n\n return null;\n}\n"],"names":["hyper","styles","mapboxgl","setMapPosition","useEffect","useMapDispatch","useMapRef","useRef","useState","getMapPosition","mapViewInfo","overlayStyles","mergeStyles","getTerrainLayerForStyle","map","getMapPadding","useAsyncEffect","getMapboxStyle","classNames","MapLoadingReporter","MapMovedReporter","MapResizeManager","MapPaddingManager","useMapStatus","use3DTerrain"],"mappings":";;;;;;;;;;;;;;;;;AA+BA,MAAM,IAAIA,WAAAA,QAAM,OAAOC,mBAAM;AAwC7B,SAAS,qBAAqB,WAAW,OAAyB,IAAI;AACpE,QAAM,EAAE,aAAa,GAAG,KAAA,IAAS;AAEjC,QAAM,MAAM,IAAIC,kBAAAA,QAAS,IAAI;AAAA,IAC3B;AAAA,IACA,SAAS;AAAA,IACT,cAAc;AAAA,IACd,aAAa;AAAA,IACb,WAAW;AAAA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,MAAM;AACpD,wBAAgB,GAAG;AACnB,sBAAc,QAAQ;AAAA,MACxB;AAAA,IACF,GAAG,EAAE;AACL,WAAO,MAAM,cAAc,QAAQ;AAAA,EACrC,GAAG,CAAC,aAAa,CAAC;AAElB,SAAO;AACT;AAEO,SAAS,kBAAkB;AAAA,EAChC;AAAA,EACA;AAAA,EACA;AACF,GAIG;AACDoB,cAAAA,aAAa,UAAU,eAAe;AAEtC,SAAO;AACT;;;"}
|
|
1
|
+
{"version":3,"file":"map-view.cjs","sources":["../src/map-view.ts"],"sourcesContent":["import hyper from \"@macrostrat/hyper\";\nimport {\n useMapRef,\n useMapDispatch,\n use3DTerrain,\n getTerrainLayerForStyle,\n useMapStatus,\n useMapStyleFragments,\n} from \"@macrostrat/mapbox-react\";\nimport React from \"react\";\nimport {\n mapViewInfo,\n MapPosition,\n setMapPosition,\n getMapPosition,\n getMapboxStyle,\n mergeStyles,\n} from \"@macrostrat/mapbox-utils\";\nimport classNames from \"classnames\";\nimport mapboxgl from \"mapbox-gl\";\nimport { useEffect, useRef, useState } from \"react\";\nimport h from \"./main.module.sass\";\nimport {\n MapLoadingReporter,\n MapMovedReporter,\n MapPaddingManager,\n MapResizeManager,\n} from \"./helpers\";\nimport \"mapbox-gl/dist/mapbox-gl.css\";\nimport { getMapPadding } from \"./utils\";\nimport { useAsyncEffect } from \"@macrostrat/ui-components\";\n\ntype MapboxCoreOptions = Omit<mapboxgl.MapboxOptions, \"container\">;\n\nexport interface MapViewProps extends MapboxCoreOptions {\n showLineSymbols?: boolean;\n children?: React.ReactNode;\n mapboxToken?: string;\n // Deprecated\n accessToken?: string;\n terrainSourceID?: string;\n enableTerrain?: boolean;\n infoMarkerPosition?: mapboxgl.LngLatLike;\n mapPosition?: MapPosition;\n initializeMap?: (\n container: HTMLElement,\n args: MapboxOptionsExt,\n ) => mapboxgl.Map;\n onMapLoaded?: (map: mapboxgl.Map) => void;\n onStyleLoaded?: (map: mapboxgl.Map) => void;\n onMapMoved?: (mapPosition: MapPosition, map: mapboxgl.Map) => void;\n /** This map sets its own viewport, rather than being positioned by a parent.\n * This is a hack to ensure that the map can overflow its \"safe area\" when false */\n standalone?: boolean;\n /** Overlay styles to apply to the map: a list of mapbox style objects or fragments to\n * overlay on top of the main map style at runtime */\n overlayStyles?: Partial<mapboxgl.StyleSpecification>[];\n /** A function to transform the map style before it is loaded */\n transformStyle?: (\n style: mapboxgl.StyleSpecification,\n ) => mapboxgl.StyleSpecification;\n loadingIgnoredSources?: string[];\n id?: string;\n className?: string;\n height: number;\n width: number;\n}\n\nexport interface MapboxOptionsExt extends MapboxCoreOptions {\n mapPosition?: MapPosition;\n}\n\nfunction defaultInitializeMap(container, args: MapboxOptionsExt = {}) {\n const { mapPosition, ...rest } = args;\n\n const map = new mapboxgl.Map({\n container,\n maxZoom: 18,\n logoPosition: \"bottom-left\",\n trackResize: false,\n antialias: true,\n // This is a legacy option for Mapbox GL v2\n // @ts-ignore\n optimizeForTerrain: true,\n ...rest,\n });\n\n let _mapPosition = mapPosition;\n if (_mapPosition == null && rest.center == null && rest.bounds == null) {\n // If no map positioning information is provided, we use the default\n _mapPosition = defaultMapPosition;\n }\n\n // set initial map position\n if (_mapPosition != null) {\n setMapPosition(map, _mapPosition);\n }\n\n return map;\n}\n\nconst defaultMapPosition: MapPosition = {\n camera: {\n lat: 34,\n lng: -120,\n altitude: 300000,\n },\n};\n\nexport function MapView(props: MapViewProps) {\n let { terrainSourceID } = props;\n const {\n height,\n width,\n enableTerrain = true,\n style = \"mapbox://styles/mapbox/streets-v11\",\n mapPosition,\n initializeMap = defaultInitializeMap,\n children,\n mapboxToken,\n // Deprecated\n accessToken,\n infoMarkerPosition,\n transformRequest,\n projection,\n onMapLoaded = null,\n onStyleLoaded = null,\n onMapMoved = null,\n standalone = false,\n overlayStyles,\n transformStyle,\n trackResize = true,\n loadingIgnoredSources = [\"elevationMarker\", \"crossSectionEndpoints\"],\n id,\n className,\n ...rest\n } = props;\n\n useEffect(() => {\n if (id != null) {\n console.warn(\n \"Setting a specific element ID for the map is deprecated. Please use className instead.\",\n );\n }\n }, [id]);\n\n const _mapboxToken = mapboxToken ?? accessToken;\n\n if (_mapboxToken != null) {\n mapboxgl.accessToken = _mapboxToken;\n }\n\n const dispatch = useMapDispatch();\n let mapRef = useMapRef();\n const ref = useRef<HTMLDivElement>();\n const parentRef = useRef<HTMLDivElement>();\n\n const [baseStyle, setBaseStyle] = useState<mapboxgl.Style>(null);\n\n /** Get overlay styles from map context. These are added after the base style is loaded, and can be used\n * to add layers to the map at runtime, even after initialization. They are merged with any overlay styles\n * passed in as props.\n */\n const _ctxOverlayStyles = useMapStyleFragments() as any[];\n\n const estMapPosition: MapPosition | null =\n mapRef.current == null ? mapPosition : getMapPosition(mapRef.current);\n const { mapUse3D, mapIsRotated } = mapViewInfo(estMapPosition);\n const is3DAvailable = (mapUse3D ?? false) && enableTerrain;\n\n useEffect(() => {\n /** Manager to update map style */\n if (baseStyle == null) return;\n let map = mapRef.current;\n\n let newStyle: mapboxgl.StyleSpecification = baseStyle;\n\n const _overlayStyles = overlayStyles ?? [];\n\n if (_overlayStyles.length > 0 || _ctxOverlayStyles.length > 0) {\n newStyle = mergeStyles(newStyle, ..._overlayStyles, ..._ctxOverlayStyles);\n }\n\n /** If we can, we try to update the map style with terrain information\n * immediately, before the style is loaded. This allows us to avoid a\n * flash of the map without terrain.\n *\n * To do this, we need to estimate the map position before load, which\n * doesn't always work.\n */\n if (is3DAvailable) {\n // We can update the style with terrain layers immediately\n const terrainStyle = getTerrainLayerForStyle(newStyle, terrainSourceID);\n newStyle = mergeStyles(newStyle, terrainStyle);\n }\n\n if (transformStyle != null) {\n newStyle = transformStyle(newStyle);\n }\n\n if (map != null) {\n dispatch({ type: \"set-style-loaded\", payload: false });\n map.setStyle(newStyle);\n } else {\n const map = initializeMap(ref.current, {\n style: newStyle,\n projection,\n mapPosition,\n transformRequest,\n ...rest,\n });\n dispatch({ type: \"set-map\", payload: map });\n map.setPadding(getMapPadding(ref, parentRef), { animate: false });\n onMapLoaded?.(map);\n }\n }, [baseStyle, overlayStyles, _ctxOverlayStyles, transformStyle]);\n\n useAsyncEffect(async () => {\n /** Manager to update map style */\n let newStyle: mapboxgl.StyleSpecification;\n if (typeof style === \"string\") {\n newStyle = await getMapboxStyle(style, {\n access_token: mapboxgl.accessToken,\n });\n } else {\n newStyle = style;\n }\n setBaseStyle(newStyle);\n }, [style]);\n\n // Get map projection\n const _projection = mapRef.current?.getProjection()?.name ?? \"mercator\";\n\n const mapClassName = classNames(\n {\n \"is-rotated\": mapIsRotated ?? false,\n \"is-3d-available\": is3DAvailable,\n },\n `${_projection}-projection`,\n );\n\n const parentClassName = classNames(\n {\n standalone,\n },\n className,\n );\n\n const containerStyle = {\n height,\n width,\n };\n\n return h(\n \"div.map-view-container.main-view\",\n { ref: parentRef, className: parentClassName, style: containerStyle },\n [\n h(\"div.mapbox-map.map-view\", { ref, className: mapClassName, id }),\n h(MapLoadingReporter, {\n ignoredSources: loadingIgnoredSources,\n }),\n h(StyleLoadedReporter, { onStyleLoaded }),\n h(MapMovedReporter, { onMapMoved }),\n // Subsitute for trackResize: true that allows map resizing to\n // be tied to a specific ref component\n h.if(trackResize)(MapResizeManager, { containerRef: ref }),\n h(MapPaddingManager, {\n containerRef: ref,\n parentRef,\n infoMarkerPosition,\n }),\n h(MapTerrainManager, { mapUse3D: is3DAvailable, terrainSourceID, style }),\n children,\n ],\n );\n}\n\nfunction StyleLoadedReporter({ onStyleLoaded = null }) {\n /** Check back every 0.1 seconds to see if the map has loaded.\n * We do it this way because mapboxgl loading events are unreliable */\n const isStyleLoaded = useMapStatus((state) => state.isStyleLoaded);\n const mapRef = useMapRef();\n const dispatch = useMapDispatch();\n\n useEffect(() => {\n if (isStyleLoaded) return;\n const interval = setInterval(() => {\n const map = mapRef.current;\n if (map == null) return;\n if (map.isStyleLoaded()) {\n // Wait a tick before setting the style loaded state\n dispatch({ type: \"set-style-loaded\", payload: true });\n onStyleLoaded?.(map);\n clearInterval(interval);\n }\n }, 50);\n return () => clearInterval(interval);\n }, [isStyleLoaded]);\n\n return null;\n}\n\nexport function MapTerrainManager({\n mapUse3D,\n terrainSourceID,\n}: {\n mapUse3D?: boolean;\n terrainSourceID?: string;\n style?: mapboxgl.StyleSpecification | string;\n}) {\n use3DTerrain(mapUse3D, terrainSourceID);\n\n return null;\n}\n"],"names":["mapboxgl","setMapPosition","useEffect","useMapDispatch","useMapRef","useRef","useState","useMapStyleFragments","getMapPosition","mapViewInfo","mergeStyles","getTerrainLayerForStyle","map","getMapPadding","useAsyncEffect","getMapboxStyle","classNames","h","MapLoadingReporter","MapMovedReporter","MapResizeManager","MapPaddingManager","useMapStatus","use3DTerrain"],"mappings":";;;;;;;;;;;;;;;AAwEA,SAAS,qBAAqB,WAAW,OAAyB,IAAI;AACpE,QAAM,EAAE,aAAa,GAAG,KAAA,IAAS;AAEjC,QAAM,MAAM,IAAIA,kBAAAA,QAAS,IAAI;AAAA,IAC3B;AAAA,IACA,SAAS;AAAA,IACT,cAAc;AAAA,IACd,aAAa;AAAA,IACb,WAAW;AAAA;AAAA;AAAA,IAGX,oBAAoB;AAAA,IACpB,GAAG;AAAA,EAAA,CACJ;AAED,MAAI,eAAe;AACnB,MAAI,gBAAgB,QAAQ,KAAK,UAAU,QAAQ,KAAK,UAAU,MAAM;AAEtE,mBAAe;AAAA,EACjB;AAGA,MAAI,gBAAgB,MAAM;AACxBC,gBAAAA,eAAe,KAAK,YAAY;AAAA,EAClC;AAEA,SAAO;AACT;AAEA,MAAM,qBAAkC;AAAA,EACtC,QAAQ;AAAA,IACN,KAAK;AAAA,IACL,KAAK;AAAA,IACL,UAAU;AAAA,EAAA;AAEd;AAEO,SAAS,QAAQ,OAAqB;AAC3C,MAAI,EAAE,oBAAoB;AAC1B,QAAM;AAAA,IACJ;AAAA,IACA;AAAA,IACA,gBAAgB;AAAA,IAChB,QAAQ;AAAA,IACR;AAAA,IACA,gBAAgB;AAAA,IAChB;AAAA,IACA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gnoredSources?: string[];
|
|
26
26
|
id?: string;
|
|
27
27
|
className?: string;
|
|
28
|
+
height: number;
|
|
29
|
+
width: number;
|
|
28
30
|
}
|
|
29
31
|
export interface MapboxOptionsExt extends MapboxCoreOptions {
|
|
30
32
|
mapPosition?: MapPosition;
|
|
@@ -32,8 +34,12 @@ export interface MapboxOptionsExt extends MapboxCoreOptions {
|
|
|
32
34
|
export declare function MapView(props: MapViewProps): import('react').ReactElement<{
|
|
33
35
|
ref: React.MutableRefObject<HTMLDivElement>;
|
|
34
36
|
className: string;
|
|
37
|
+
style: {
|
|
38
|
+
height: number;
|
|
39
|
+
width: number;
|
|
40
|
+
};
|
|
35
41
|
}, string | import('react').JSXElementConstructor<any>>;
|
|
36
|
-
export declare function MapTerrainManager({ mapUse3D, terrainSourceID,
|
|
42
|
+
export declare function MapTerrainManager({ mapUse3D, terrainSourceID, }: {
|
|
37
43
|
mapUse3D?: boolean;
|
|
38
44
|
terrainSourceID?: string;
|
|
39
45
|
style?: mapboxgl.StyleSpecification | string;
|
package/dist/map-view.js
CHANGED
|
@@ -1,15 +1,13 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { use3DTerrain, useMapDispatch, useMapRef, getTerrainLayerForStyle, useMapStatus } from "@macrostrat/mapbox-react";
|
|
1
|
+
import { use3DTerrain, useMapDispatch, useMapRef, useMapStyleFragments, getTerrainLayerForStyle, useMapStatus } from "@macrostrat/mapbox-react";
|
|
3
2
|
import { getMapPosition, mapViewInfo, mergeStyles, getMapboxStyle, setMapPosition } from "@macrostrat/mapbox-utils";
|
|
4
3
|
import classNames from "classnames";
|
|
5
4
|
import mapboxgl from "mapbox-gl";
|
|
6
5
|
import { useEffect, useRef, useState } from "react";
|
|
7
|
-
import h
|
|
6
|
+
import h from "./main.module.sass.js";
|
|
8
7
|
import { MapLoadingReporter, MapMovedReporter, MapResizeManager, MapPaddingManager } from "./helpers.js";
|
|
9
8
|
/* empty css */
|
|
10
9
|
import { getMapPadding } from "./utils.js";
|
|
11
10
|
import { useAsyncEffect } from "@macrostrat/ui-components";
|
|
12
|
-
const h = h$1.styled(h$2);
|
|
13
11
|
function defaultInitializeMap(container, args = {}) {
|
|
14
12
|
const { mapPosition, ...rest } = args;
|
|
15
13
|
const map = new mapboxgl.Map({
|
|
@@ -42,8 +40,10 @@ const defaultMapPosition = {
|
|
|
42
40
|
function MapView(props) {
|
|
43
41
|
let { terrainSourceID } = props;
|
|
44
42
|
const {
|
|
43
|
+
height,
|
|
44
|
+
width,
|
|
45
45
|
enableTerrain = true,
|
|
46
|
-
style,
|
|
46
|
+
style = "mapbox://styles/mapbox/streets-v11",
|
|
47
47
|
mapPosition,
|
|
48
48
|
initializeMap = defaultInitializeMap,
|
|
49
49
|
children,
|
|
@@ -61,13 +61,10 @@ function MapView(props) {
|
|
|
61
61
|
transformStyle,
|
|
62
62
|
trackResize = true,
|
|
63
63
|
loadingIgnoredSources = ["elevationMarker", "crossSectionEndpoints"],
|
|
64
|
-
id
|
|
64
|
+
id,
|
|
65
65
|
className,
|
|
66
66
|
...rest
|
|
67
67
|
} = props;
|
|
68
|
-
if (enableTerrain) {
|
|
69
|
-
terrainSourceID ??= "mapbox-3d-dem";
|
|
70
|
-
}
|
|
71
68
|
useEffect(() => {
|
|
72
69
|
if (id != null) {
|
|
73
70
|
console.warn(
|
|
@@ -84,6 +81,7 @@ function MapView(props) {
|
|
|
84
81
|
const ref = useRef();
|
|
85
82
|
const parentRef = useRef();
|
|
86
83
|
const [baseStyle, setBaseStyle] = useState(null);
|
|
84
|
+
const _ctxOverlayStyles = useMapStyleFragments();
|
|
87
85
|
const estMapPosition = mapRef.current == null ? mapPosition : getMapPosition(mapRef.current);
|
|
88
86
|
const { mapUse3D, mapIsRotated } = mapViewInfo(estMapPosition);
|
|
89
87
|
const is3DAvailable = (mapUse3D ?? false) && enableTerrain;
|
|
@@ -91,9 +89,9 @@ function MapView(props) {
|
|
|
91
89
|
if (baseStyle == null) return;
|
|
92
90
|
let map = mapRef.current;
|
|
93
91
|
let newStyle = baseStyle;
|
|
94
|
-
const
|
|
95
|
-
if (
|
|
96
|
-
newStyle = mergeStyles(newStyle, ...
|
|
92
|
+
const _overlayStyles = overlayStyles ?? [];
|
|
93
|
+
if (_overlayStyles.length > 0 || _ctxOverlayStyles.length > 0) {
|
|
94
|
+
newStyle = mergeStyles(newStyle, ..._overlayStyles, ..._ctxOverlayStyles);
|
|
97
95
|
}
|
|
98
96
|
if (is3DAvailable) {
|
|
99
97
|
const terrainStyle = getTerrainLayerForStyle(newStyle, terrainSourceID);
|
|
@@ -117,7 +115,7 @@ function MapView(props) {
|
|
|
117
115
|
map2.setPadding(getMapPadding(ref, parentRef), { animate: false });
|
|
118
116
|
onMapLoaded?.(map2);
|
|
119
117
|
}
|
|
120
|
-
}, [baseStyle, overlayStyles, transformStyle]);
|
|
118
|
+
}, [baseStyle, overlayStyles, _ctxOverlayStyles, transformStyle]);
|
|
121
119
|
useAsyncEffect(async () => {
|
|
122
120
|
let newStyle;
|
|
123
121
|
if (typeof style === "string") {
|
|
@@ -143,9 +141,13 @@ function MapView(props) {
|
|
|
143
141
|
},
|
|
144
142
|
className
|
|
145
143
|
);
|
|
144
|
+
const containerStyle = {
|
|
145
|
+
height,
|
|
146
|
+
width
|
|
147
|
+
};
|
|
146
148
|
return h(
|
|
147
149
|
"div.map-view-container.main-view",
|
|
148
|
-
{ ref: parentRef, className: parentClassName },
|
|
150
|
+
{ ref: parentRef, className: parentClassName, style: containerStyle },
|
|
149
151
|
[
|
|
150
152
|
h("div.mapbox-map.map-view", { ref, className: mapClassName, id }),
|
|
151
153
|
h(MapLoadingReporter, {
|
|
@@ -187,8 +189,7 @@ function StyleLoadedReporter({ onStyleLoaded = null }) {
|
|
|
187
189
|
}
|
|
188
190
|
function MapTerrainManager({
|
|
189
191
|
mapUse3D,
|
|
190
|
-
terrainSourceID
|
|
191
|
-
style
|
|
192
|
+
terrainSourceID
|
|
192
193
|
}) {
|
|
193
194
|
use3DTerrain(mapUse3D, terrainSourceID);
|
|
194
195
|
return null;
|
package/dist/map-view.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"map-view.js","sources":["../src/map-view.ts"],"sourcesContent":["import hyper from \"@macrostrat/hyper\";\nimport {\n useMapRef,\n useMapDispatch,\n use3DTerrain,\n getTerrainLayerForStyle,\n useMapStatus,\n} from \"@macrostrat/mapbox-react\";\nimport React from \"react\";\nimport {\n mapViewInfo,\n MapPosition,\n setMapPosition,\n getMapPosition,\n getMapboxStyle,\n mergeStyles,\n} from \"@macrostrat/mapbox-utils\";\nimport classNames from \"classnames\";\nimport mapboxgl from \"mapbox-gl\";\nimport { useEffect, useRef, useState } from \"react\";\nimport styles from \"./main.module.sass\";\nimport {\n MapLoadingReporter,\n MapMovedReporter,\n MapPaddingManager,\n MapResizeManager,\n} from \"./helpers\";\nimport \"mapbox-gl/dist/mapbox-gl.css\";\nimport { getMapPadding } from \"./utils\";\nimport { useAsyncEffect } from \"@macrostrat/ui-components\";\n\nconst h = hyper.styled(styles);\n\ntype MapboxCoreOptions = Omit<mapboxgl.MapboxOptions, \"container\">;\n\nexport interface MapViewProps extends MapboxCoreOptions {\n showLineSymbols?: boolean;\n children?: React.ReactNode;\n mapboxToken?: string;\n // Deprecated\n accessToken?: string;\n terrainSourceID?: string;\n enableTerrain?: boolean;\n infoMarkerPosition?: mapboxgl.LngLatLike;\n mapPosition?: MapPosition;\n initializeMap?: (\n container: HTMLElement,\n args: MapboxOptionsExt,\n ) => mapboxgl.Map;\n onMapLoaded?: (map: mapboxgl.Map) => void;\n onStyleLoaded?: (map: mapboxgl.Map) => void;\n onMapMoved?: (mapPosition: MapPosition, map: mapboxgl.Map) => void;\n /** This map sets its own viewport, rather than being positioned by a parent.\n * This is a hack to ensure that the map can overflow its \"safe area\" when false */\n standalone?: boolean;\n /** Overlay styles to apply to the map: a list of mapbox style objects or fragments to\n * overlay on top of the main map style at runtime */\n overlayStyles?: Partial<mapboxgl.StyleSpecification>[];\n /** A function to transform the map style before it is loaded */\n transformStyle?: (\n style: mapboxgl.StyleSpecification,\n ) => mapboxgl.StyleSpecification;\n loadingIgnoredSources?: string[];\n id?: string;\n className?: string;\n}\n\nexport interface MapboxOptionsExt extends MapboxCoreOptions {\n mapPosition?: MapPosition;\n}\n\nfunction defaultInitializeMap(container, args: MapboxOptionsExt = {}) {\n const { mapPosition, ...rest } = args;\n\n const map = new mapboxgl.Map({\n container,\n maxZoom: 18,\n logoPosition: \"bottom-left\",\n trackResize: false,\n antialias: true,\n // This is a legacy option for Mapbox GL v2\n // @ts-ignore\n optimizeForTerrain: true,\n ...rest,\n });\n\n let _mapPosition = mapPosition;\n if (_mapPosition == null && rest.center == null && rest.bounds == null) {\n // If no map positioning information is provided, we use the default\n _mapPosition = defaultMapPosition;\n }\n\n // set initial map position\n if (_mapPosition != null) {\n setMapPosition(map, _mapPosition);\n }\n\n return map;\n}\n\nconst defaultMapPosition: MapPosition = {\n camera: {\n lat: 34,\n lng: -120,\n altitude: 300000,\n },\n};\n\nexport function MapView(props: MapViewProps) {\n let { terrainSourceID } = props;\n const {\n enableTerrain = true,\n style,\n mapPosition,\n initializeMap = defaultInitializeMap,\n children,\n mapboxToken,\n // Deprecated\n accessToken,\n infoMarkerPosition,\n transformRequest,\n projection,\n onMapLoaded = null,\n onStyleLoaded = null,\n onMapMoved = null,\n standalone = false,\n overlayStyles,\n transformStyle,\n trackResize = true,\n loadingIgnoredSources = [\"elevationMarker\", \"crossSectionEndpoints\"],\n id = \"map\",\n className,\n ...rest\n } = props;\n if (enableTerrain) {\n terrainSourceID ??= \"mapbox-3d-dem\";\n }\n\n useEffect(() => {\n if (id != null) {\n console.warn(\n \"Setting a specific element ID for the map is deprecated. Please use className instead.\",\n );\n }\n }, [id]);\n\n const _mapboxToken = mapboxToken ?? accessToken;\n\n if (_mapboxToken != null) {\n mapboxgl.accessToken = _mapboxToken;\n }\n\n const dispatch = useMapDispatch();\n let mapRef = useMapRef();\n const ref = useRef<HTMLDivElement>();\n const parentRef = useRef<HTMLDivElement>();\n\n const [baseStyle, setBaseStyle] = useState<mapboxgl.Style>(null);\n\n const estMapPosition: MapPosition | null =\n mapRef.current == null ? mapPosition : getMapPosition(mapRef.current);\n const { mapUse3D, mapIsRotated } = mapViewInfo(estMapPosition);\n const is3DAvailable = (mapUse3D ?? false) && enableTerrain;\n\n useEffect(() => {\n /** Manager to update map style */\n if (baseStyle == null) return;\n let map = mapRef.current;\n\n let newStyle: mapboxgl.StyleSpecification = baseStyle;\n\n const overlayStyles = props.overlayStyles ?? [];\n\n if (overlayStyles.length > 0) {\n newStyle = mergeStyles(newStyle, ...overlayStyles);\n }\n\n /** If we can, we try to update the map style with terrain information\n * immediately, before the style is loaded. This allows us to avoid a\n * flash of the map without terrain.\n *\n * To do this, we need to estimate the map position before load, which\n * doesn't always work.\n */\n if (is3DAvailable) {\n // We can update the style with terrain layers immediately\n const terrainStyle = getTerrainLayerForStyle(newStyle, terrainSourceID);\n newStyle = mergeStyles(newStyle, terrainStyle);\n }\n\n if (transformStyle != null) {\n newStyle = transformStyle(newStyle);\n }\n\n if (map != null) {\n dispatch({ type: \"set-style-loaded\", payload: false });\n map.setStyle(newStyle);\n } else {\n const map = initializeMap(ref.current, {\n style: newStyle,\n projection,\n mapPosition,\n transformRequest,\n ...rest,\n });\n dispatch({ type: \"set-map\", payload: map });\n map.setPadding(getMapPadding(ref, parentRef), { animate: false });\n onMapLoaded?.(map);\n }\n }, [baseStyle, overlayStyles, transformStyle]);\n\n useAsyncEffect(async () => {\n /** Manager to update map style */\n let newStyle: mapboxgl.StyleSpecification;\n if (typeof style === \"string\") {\n newStyle = await getMapboxStyle(style, {\n access_token: mapboxgl.accessToken,\n });\n } else {\n newStyle = style;\n }\n setBaseStyle(newStyle);\n }, [style]);\n\n // Get map projection\n const _projection = mapRef.current?.getProjection()?.name ?? \"mercator\";\n\n const mapClassName = classNames(\n {\n \"is-rotated\": mapIsRotated ?? false,\n \"is-3d-available\": is3DAvailable,\n },\n `${_projection}-projection`,\n );\n\n const parentClassName = classNames(\n {\n standalone,\n },\n className,\n );\n\n return h(\n \"div.map-view-container.main-view\",\n { ref: parentRef, className: parentClassName },\n [\n h(\"div.mapbox-map.map-view\", { ref, className: mapClassName, id }),\n h(MapLoadingReporter, {\n ignoredSources: loadingIgnoredSources,\n }),\n h(StyleLoadedReporter, { onStyleLoaded }),\n h(MapMovedReporter, { onMapMoved }),\n // Subsitute for trackResize: true that allows map resizing to\n // be tied to a specific ref component\n h.if(trackResize)(MapResizeManager, { containerRef: ref }),\n h(MapPaddingManager, {\n containerRef: ref,\n parentRef,\n infoMarkerPosition,\n }),\n h(MapTerrainManager, { mapUse3D: is3DAvailable, terrainSourceID, style }),\n children,\n ],\n );\n}\n\nfunction StyleLoadedReporter({ onStyleLoaded = null }) {\n /** Check back every 0.1 seconds to see if the map has loaded.\n * We do it this way because mapboxgl loading events are unreliable */\n const isStyleLoaded = useMapStatus((state) => state.isStyleLoaded);\n const mapRef = useMapRef();\n const dispatch = useMapDispatch();\n\n useEffect(() => {\n if (isStyleLoaded) return;\n const interval = setInterval(() => {\n const map = mapRef.current;\n if (map == null) return;\n if (map.isStyleLoaded()) {\n // Wait a tick before setting the style loaded state\n dispatch({ type: \"set-style-loaded\", payload: true });\n onStyleLoaded?.(map);\n clearInterval(interval);\n }\n }, 50);\n return () => clearInterval(interval);\n }, [isStyleLoaded]);\n\n return null;\n}\n\nexport function MapTerrainManager({\n mapUse3D,\n terrainSourceID,\n style,\n}: {\n mapUse3D?: boolean;\n terrainSourceID?: string;\n style?: mapboxgl.StyleSpecification | string;\n}) {\n use3DTerrain(mapUse3D, terrainSourceID);\n\n return null;\n}\n"],"names":["hyper","styles","overlayStyles","map"],"mappings":";;;;;;;;;;;AA+BA,MAAM,IAAIA,IAAM,OAAOC,GAAM;AAwC7B,SAAS,qBAAqB,WAAW,OAAyB,IAAI;AACpE,QAAM,EAAE,aAAa,GAAG,KAAA,IAAS;AAEjC,QAAM,MAA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AC,aAAa,CAAC;AAElB,SAAO;AACT;AAEO,SAAS,kBAAkB;AAAA,EAChC;AAAA,EACA;AAAA,EACA;AACF,GAIG;AACD,eAAa,UAAU,eAAe;AAEtC,SAAO;AACT;"}
|
|
1
|
+
{"version":3,"file":"map-view.js","sources":["../src/map-view.ts"],"sourcesContent":["import hyper from \"@macrostrat/hyper\";\nimport {\n useMapRef,\n useMapDispatch,\n use3DTerrain,\n getTerrainLayerForStyle,\n useMapStatus,\n useMapStyleFragments,\n} from \"@macrostrat/mapbox-react\";\nimport React from \"react\";\nimport {\n mapViewInfo,\n MapPosition,\n setMapPosition,\n getMapPosition,\n getMapboxStyle,\n mergeStyles,\n} from \"@macrostrat/mapbox-utils\";\nimport classNames from \"classnames\";\nimport mapboxgl from \"mapbox-gl\";\nimport { useEffect, useRef, useState } from \"react\";\nimport h from \"./main.module.sass\";\nimport {\n MapLoadingReporter,\n MapMovedReporter,\n MapPaddingManager,\n MapResizeManager,\n} from \"./helpers\";\nimport \"mapbox-gl/dist/mapbox-gl.css\";\nimport { getMapPadding } from \"./utils\";\nimport { useAsyncEffect } from \"@macrostrat/ui-components\";\n\ntype MapboxCoreOptions = Omit<mapboxgl.MapboxOptions, \"container\">;\n\nexport interface MapViewProps extends MapboxCoreOptions {\n showLineSymbols?: boolean;\n children?: React.ReactNode;\n mapboxToken?: string;\n // Deprecated\n accessToken?: string;\n terrainSourceID?: string;\n enableTerrain?: boolean;\n infoMarkerPosition?: mapboxgl.LngLatLike;\n mapPosition?: MapPosition;\n initializeMap?: (\n container: HTMLElement,\n args: MapboxOptionsExt,\n ) => mapboxgl.Map;\n onMapLoaded?: (map: mapboxgl.Map) => void;\n onStyleLoaded?: (map: mapboxgl.Map) => void;\n onMapMoved?: (mapPosition: MapPosition, map: mapboxgl.Map) => void;\n /** This map sets its own viewport, rather than being positioned by a parent.\n * This is a hack to ensure that the map can overflow its \"safe area\" when false */\n standalone?: boolean;\n /** Overlay styles to apply to the map: a list of mapbox style objects or fragments to\n * overlay on top of the main map style at runtime */\n overlayStyles?: Partial<mapboxgl.StyleSpecification>[];\n /** A function to transform the map style before it is loaded */\n transformStyle?: (\n style: mapboxgl.StyleSpecification,\n ) => mapboxgl.StyleSpecification;\n loadingIgnoredSources?: string[];\n id?: string;\n className?: string;\n height: number;\n width: number;\n}\n\nexport interface MapboxOptionsExt extends MapboxCoreOptions {\n mapPosition?: MapPosition;\n}\n\nfunction defaultInitializeMap(container, args: MapboxOptionsExt = {}) {\n const { mapPosition, ...rest } = args;\n\n const map = new mapboxgl.Map({\n container,\n maxZoom: 18,\n logoPosition: \"bottom-left\",\n trackResize: false,\n antialias: true,\n // This is a legacy option for Mapbox GL v2\n // @ts-ignore\n optimizeForTerrain: true,\n ...rest,\n });\n\n let _mapPosition = mapPosition;\n if (_mapPosition == null && rest.center == null && rest.bounds == null) {\n // If no map positioning information is provided, we use the default\n _mapPosition = defaultMapPosition;\n }\n\n // set initial map position\n if (_mapPosition != null) {\n setMapPosition(map, _mapPosition);\n }\n\n return map;\n}\n\nconst defaultMapPosition: MapPosition = {\n camera: {\n lat: 34,\n lng: -120,\n altitude: 300000,\n },\n};\n\nexport function MapView(props: MapViewProps) {\n let { terrainSourceID } = props;\n const {\n height,\n width,\n enableTerrain = true,\n style = \"mapbox://styles/mapbox/streets-v11\",\n mapPosition,\n initializeMap = defaultInitializeMap,\n children,\n mapboxToken,\n // Deprecated\n accessToken,\n infoMarkerPosition,\n transformRequest,\n projection,\n onMapLoaded = null,\n onStyleLoaded = null,\n onMapMoved = null,\n standalone = false,\n overlayStyles,\n transformStyle,\n trackResize = true,\n loadingIgnoredSources = [\"elevationMarker\", \"crossSectionEndpoints\"],\n id,\n className,\n ...rest\n } = props;\n\n useEffect(() => {\n if (id != null) {\n console.warn(\n \"Setting a specific element ID for the map is deprecated. Please use className instead.\",\n );\n }\n }, [id]);\n\n const _mapboxToken = mapboxToken ?? accessToken;\n\n if (_mapboxToken != null) {\n mapboxgl.accessToken = _mapboxToken;\n }\n\n const dispatch = useMapDispatch();\n let mapRef = useMapRef();\n const ref = useRef<HTMLDivElement>();\n const parentRef = useRef<HTMLDivElement>();\n\n const [baseStyle, setBaseStyle] = useState<mapboxgl.Style>(null);\n\n /** Get overlay styles from map context. These are added after the base style is loaded, and can be used\n * to add layers to the map at runtime, even after initialization. They are merged with any overlay styles\n * passed in as props.\n */\n const _ctxOverlayStyles = useMapStyleFragments() as any[];\n\n const estMapPosition: MapPosition | null =\n mapRef.current == null ? mapPosition : getMapPosition(mapRef.current);\n const { mapUse3D, mapIsRotated } = mapViewInfo(estMapPosition);\n const is3DAvailable = (mapUse3D ?? false) && enableTerrain;\n\n useEffect(() => {\n /** Manager to update map style */\n if (baseStyle == null) return;\n let map = mapRef.current;\n\n let newStyle: mapboxgl.StyleSpecification = baseStyle;\n\n const _overlayStyles = overlayStyles ?? [];\n\n if (_overlayStyles.length > 0 || _ctxOverlayStyles.length > 0) {\n newStyle = mergeStyles(newStyle, ..._overlayStyles, ..._ctxOverlayStyles);\n }\n\n /** If we can, we try to update the map style with terrain information\n * immediately, before the style is loaded. This allows us to avoid a\n * flash of the map without terrain.\n *\n * To do this, we need to estimate the map position before load, which\n * doesn't always work.\n */\n if (is3DAvailable) {\n // We can update the style with terrain layers immediately\n const terrainStyle = getTerrainLayerForStyle(newStyle, terrainSourceID);\n newStyle = mergeStyles(newStyle, terrainStyle);\n }\n\n if (transformStyle != null) {\n newStyle = transformStyle(newStyle);\n }\n\n if (map != null) {\n dispatch({ type: \"set-style-loaded\", payload: false });\n map.setStyle(newStyle);\n } else {\n const map = initializeMap(ref.current, {\n style: newStyle,\n projection,\n mapPosition,\n transformRequest,\n ...rest,\n });\n dispatch({ type: \"set-map\", payload: map });\n map.setPadding(getMapPadding(ref, parentRef), { animate: false });\n onMapLoaded?.(map);\n }\n }, [baseStyle, overlayStyles, _ctxOverlayStyles, transformStyle]);\n\n useAsyncEffect(async () => {\n /** Manager to update map style */\n let newStyle: mapboxgl.StyleSpecification;\n if (typeof style === \"string\") {\n newStyle = await getMapboxStyle(style, {\n access_token: mapboxgl.accessToken,\n });\n } else {\n newStyle = style;\n }\n setBaseStyle(newStyle);\n }, [style]);\n\n // Get map projection\n const _projection = mapRef.current?.getProjection()?.name ?? \"mercator\";\n\n const mapClassName = classNames(\n {\n \"is-rotated\": mapIsRotated ?? false,\n \"is-3d-available\": is3DAvailable,\n },\n `${_projection}-projection`,\n );\n\n const parentClassName = classNames(\n {\n standalone,\n },\n className,\n );\n\n const containerStyle = {\n height,\n width,\n };\n\n return h(\n \"div.map-view-container.main-view\",\n { ref: parentRef, className: parentClassName, style: containerStyle },\n [\n h(\"div.mapbox-map.map-view\", { ref, className: mapClassName, id }),\n h(MapLoadingReporter, {\n ignoredSources: loadingIgnoredSources,\n }),\n h(StyleLoadedReporter, { onStyleLoaded }),\n h(MapMovedReporter, { onMapMoved }),\n // Subsitute for trackResize: true that allows map resizing to\n // be tied to a specific ref component\n h.if(trackResize)(MapResizeManager, { containerRef: ref }),\n h(MapPaddingManager, {\n containerRef: ref,\n parentRef,\n infoMarkerPosition,\n }),\n h(MapTerrainManager, { mapUse3D: is3DAvailable, terrainSourceID, style }),\n children,\n ],\n );\n}\n\nfunction StyleLoadedReporter({ onStyleLoaded = null }) {\n /** Check back every 0.1 seconds to see if the map has loaded.\n * We do it this way because mapboxgl loading events are unreliable */\n const isStyleLoaded = useMapStatus((state) => state.isStyleLoaded);\n const mapRef = useMapRef();\n const dispatch = useMapDispatch();\n\n useEffect(() => {\n if (isStyleLoaded) return;\n const interval = setInterval(() => {\n const map = mapRef.current;\n if (map == null) return;\n if (map.isStyleLoaded()) {\n // Wait a tick before setting the style loaded state\n dispatch({ type: \"set-style-loaded\", payload: true });\n onStyleLoaded?.(map);\n clearInterval(interval);\n }\n }, 50);\n return () => clearInterval(interval);\n }, [isStyleLoaded]);\n\n return null;\n}\n\nexport function MapTerrainManager({\n mapUse3D,\n terrainSourceID,\n}: {\n mapUse3D?: boolean;\n terrainSourceID?: string;\n style?: mapboxgl.StyleSpecification | string;\n}) {\n use3DTerrain(mapUse3D, terrainSourceID);\n\n return null;\n}\n"],"names":["map"],"mappings":";;;;;;;;;;AAwEA,SAAS,qBAAqB,WAAW,OAAyB,IAAI;AACpE,QAAM,EAAE,aAAa,GAAG,KAAA,IAAS;AAEjC,QAAM,MAAM,IAAI,SAAS,IAAI;AAAA,IAC3B;AAAA,IACA,SAAS;AAAA,IACT,cAAc;AAAA,IACd,aAAa;AAAA,IACb,WAAW;AAAA;AAAA;AAAA,IAGX,oBAAoB;AAAA,IACpB,GAAG;AAAA,EAAA,CACJ;AAED,MAAI,eAAe;AACnB,MAAI,gBAAgB,QAAQ,KAAK,UAAU,QAAQ,KAAK,UAAU,MAAM;AAEtE,mBAAe;AAAA,EACjB;AAGA,MAAI,gBAAgB,MAAM;AACxB,mBAAe,KAAK,YAAY;AAAA,EAClC;AAEA,SAAO;AACT;AAEA,MAAM,qBAAkC;AAAA,EACtC,QAAQ;AAAA,IACN,KAAK;AAAA,IACL,KAAK;AAAA,IACL,UAAU;AAAA,EAAA;AAEd;AAEO,SAAS,QAAQ,OAAqB;AAC3C,MAAI,EAAE,oBAAoB;AAC1B,QAAM;AAAA,IACJ;AAAA,IACA;AAAA,IACA,gBAAgB;AAAA,IAChB,QAAQ;AAAA,IACR;AAAA,IACA,gBAAgB;AAAA,IAChB;AAAA,IACA;AAAA;AAAA,IAEA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA,cAAc;AAAA,IACd,gBAAgB;AAAA,IAChB,aAAa;AAAA,IACb,aAAa;AAAA,IACb;AAAA,IACA;AAAA,IACA,cAAc;AAAA,IACd,wBAAwB,CAAC,mBAAmB,uBAAuB;AAAA,IACnE;AAAA,IACA;AAAA,IACA,GAAG;AAAA,EAAA,IACD;AAEJ,YAAU,MAAM;AACd,QAAI,MAAM,MAAM;AACd,cAAQ;AAAA,QACN;AAAA,MAAA;AAAA,IAEJ;AAAA,EACF,GAAG,CAAC,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AW,eAAA;AAEjB,YAAU,MAAM;AACd,QAAI,cAAe;AACnB,UAAM,WAAW,YAAY,MAAM;AACjC,YAAM,MAAM,OAAO;AACnB,UAAI,OAAO,KAAM;AACjB,UAAI,IAAI,iBAAiB;AAEvB,iBAAS,EAAE,MAAM,oBAAoB,SAAS,MAAM;AACpD,wBAAgB,GAAG;AACnB,sBAAc,QAAQ;AAAA,MACxB;AAAA,IACF,GAAG,EAAE;AACL,WAAO,MAAM,cAAc,QAAQ;AAAA,EACrC,GAAG,CAAC,aAAa,CAAC;AAElB,SAAO;AACT;AAEO,SAAS,kBAAkB;AAAA,EAChC;AAAA,EACA;AACF,GAIG;AACD,eAAa,UAAU,eAAe;AAEtC,SAAO;AACT;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@macrostrat/map-interface",
|
|
3
|
-
"version": "2.1
|
|
3
|
+
"version": "2.2.1",
|
|
4
4
|
"description": "Map interface for Macrostrat",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -48,11 +48,10 @@
|
|
|
48
48
|
"dependencies": {
|
|
49
49
|
"@blueprintjs/core": "^6.6.1",
|
|
50
50
|
"@macrostrat/color-utils": "^1.2.1",
|
|
51
|
-
"@macrostrat/data-components": "^1.1.
|
|
51
|
+
"@macrostrat/data-components": "^1.1.3",
|
|
52
52
|
"@macrostrat/hyper": "^3.0.6",
|
|
53
|
-
"@macrostrat/
|
|
54
|
-
"@macrostrat/mapbox-
|
|
55
|
-
"@macrostrat/mapbox-utils": "^1.7.2",
|
|
53
|
+
"@macrostrat/mapbox-react": "^3.1.1",
|
|
54
|
+
"@macrostrat/mapbox-utils": "^1.7.3",
|
|
56
55
|
"@macrostrat/ui-components": "^5.0.5",
|
|
57
56
|
"@mapbox/tilebelt": "^2.0.0",
|
|
58
57
|
"@visx/axis": "^3.12.0",
|
package/src/map-view.ts
CHANGED
|
@@ -5,6 +5,7 @@ import {
|
|
|
5
5
|
use3DTerrain,
|
|
6
6
|
getTerrainLayerForStyle,
|
|
7
7
|
useMapStatus,
|
|
8
|
+
useMapStyleFragments,
|
|
8
9
|
} from "@macrostrat/mapbox-react";
|
|
9
10
|
import React from "react";
|
|
10
11
|
import {
|
|
@@ -18,7 +19,7 @@ import {
|
|
|
18
19
|
import classNames from "classnames";
|
|
19
20
|
import mapboxgl from "mapbox-gl";
|
|
20
21
|
import { useEffect, useRef, useState } from "react";
|
|
21
|
-
import
|
|
22
|
+
import h from "./main.module.sass";
|
|
22
23
|
import {
|
|
23
24
|
MapLoadingReporter,
|
|
24
25
|
MapMovedReporter,
|
|
@@ -29,8 +30,6 @@ import "mapbox-gl/dist/mapbox-gl.css";
|
|
|
29
30
|
import { getMapPadding } from "./utils";
|
|
30
31
|
import { useAsyncEffect } from "@macrostrat/ui-components";
|
|
31
32
|
|
|
32
|
-
const h = hyper.styled(styles);
|
|
33
|
-
|
|
34
33
|
type MapboxCoreOptions = Omit<mapboxgl.MapboxOptions, "container">;
|
|
35
34
|
|
|
36
35
|
export interface MapViewProps extends MapboxCoreOptions {
|
|
@@ -63,6 +62,8 @@ export interface MapViewProps extends MapboxCoreOptions {
|
|
|
63
62
|
loadingIgnoredSources?: string[];
|
|
64
63
|
id?: string;
|
|
65
64
|
className?: string;
|
|
65
|
+
height: number;
|
|
66
|
+
width: number;
|
|
66
67
|
}
|
|
67
68
|
|
|
68
69
|
export interface MapboxOptionsExt extends MapboxCoreOptions {
|
|
@@ -109,8 +110,10 @@ const defaultMapPosition: MapPosition = {
|
|
|
109
110
|
export function MapView(props: MapViewProps) {
|
|
110
111
|
let { terrainSourceID } = props;
|
|
111
112
|
const {
|
|
113
|
+
height,
|
|
114
|
+
width,
|
|
112
115
|
enableTerrain = true,
|
|
113
|
-
style,
|
|
116
|
+
style = "mapbox://styles/mapbox/streets-v11",
|
|
114
117
|
mapPosition,
|
|
115
118
|
initializeMap = defaultInitializeMap,
|
|
116
119
|
children,
|
|
@@ -128,13 +131,10 @@ export function MapView(props: MapViewProps) {
|
|
|
128
131
|
transformStyle,
|
|
129
132
|
trackResize = true,
|
|
130
133
|
loadingIgnoredSources = ["elevationMarker", "crossSectionEndpoints"],
|
|
131
|
-
id
|
|
134
|
+
id,
|
|
132
135
|
className,
|
|
133
136
|
...rest
|
|
134
137
|
} = props;
|
|
135
|
-
if (enableTerrain) {
|
|
136
|
-
terrainSourceID ??= "mapbox-3d-dem";
|
|
137
|
-
}
|
|
138
138
|
|
|
139
139
|
useEffect(() => {
|
|
140
140
|
if (id != null) {
|
|
@@ -157,6 +157,12 @@ export function MapView(props: MapViewProps) {
|
|
|
157
157
|
|
|
158
158
|
const [baseStyle, setBaseStyle] = useState<mapboxgl.Style>(null);
|
|
159
159
|
|
|
160
|
+
/** Get overlay styles from map context. These are added after the base style is loaded, and can be used
|
|
161
|
+
* to add layers to the map at runtime, even after initialization. They are merged with any overlay styles
|
|
162
|
+
* passed in as props.
|
|
163
|
+
*/
|
|
164
|
+
const _ctxOverlayStyles = useMapStyleFragments() as any[];
|
|
165
|
+
|
|
160
166
|
const estMapPosition: MapPosition | null =
|
|
161
167
|
mapRef.current == null ? mapPosition : getMapPosition(mapRef.current);
|
|
162
168
|
const { mapUse3D, mapIsRotated } = mapViewInfo(estMapPosition);
|
|
@@ -169,10 +175,10 @@ export function MapView(props: MapViewProps) {
|
|
|
169
175
|
|
|
170
176
|
let newStyle: mapboxgl.StyleSpecification = baseStyle;
|
|
171
177
|
|
|
172
|
-
const
|
|
178
|
+
const _overlayStyles = overlayStyles ?? [];
|
|
173
179
|
|
|
174
|
-
if (
|
|
175
|
-
newStyle = mergeStyles(newStyle, ...
|
|
180
|
+
if (_overlayStyles.length > 0 || _ctxOverlayStyles.length > 0) {
|
|
181
|
+
newStyle = mergeStyles(newStyle, ..._overlayStyles, ..._ctxOverlayStyles);
|
|
176
182
|
}
|
|
177
183
|
|
|
178
184
|
/** If we can, we try to update the map style with terrain information
|
|
@@ -207,7 +213,7 @@ export function MapView(props: MapViewProps) {
|
|
|
207
213
|
map.setPadding(getMapPadding(ref, parentRef), { animate: false });
|
|
208
214
|
onMapLoaded?.(map);
|
|
209
215
|
}
|
|
210
|
-
}, [baseStyle, overlayStyles, transformStyle]);
|
|
216
|
+
}, [baseStyle, overlayStyles, _ctxOverlayStyles, transformStyle]);
|
|
211
217
|
|
|
212
218
|
useAsyncEffect(async () => {
|
|
213
219
|
/** Manager to update map style */
|
|
@@ -240,9 +246,14 @@ export function MapView(props: MapViewProps) {
|
|
|
240
246
|
className,
|
|
241
247
|
);
|
|
242
248
|
|
|
249
|
+
const containerStyle = {
|
|
250
|
+
height,
|
|
251
|
+
width,
|
|
252
|
+
};
|
|
253
|
+
|
|
243
254
|
return h(
|
|
244
255
|
"div.map-view-container.main-view",
|
|
245
|
-
{ ref: parentRef, className: parentClassName },
|
|
256
|
+
{ ref: parentRef, className: parentClassName, style: containerStyle },
|
|
246
257
|
[
|
|
247
258
|
h("div.mapbox-map.map-view", { ref, className: mapClassName, id }),
|
|
248
259
|
h(MapLoadingReporter, {
|
|
@@ -292,7 +303,6 @@ function StyleLoadedReporter({ onStyleLoaded = null }) {
|
|
|
292
303
|
export function MapTerrainManager({
|
|
293
304
|
mapUse3D,
|
|
294
305
|
terrainSourceID,
|
|
295
|
-
style,
|
|
296
306
|
}: {
|
|
297
307
|
mapUse3D?: boolean;
|
|
298
308
|
terrainSourceID?: string;
|