Skip to content

Commit b3de8da

Browse files
committed
Merge branch 'map-terrain-reloading'
* map-terrain-reloading: Updated changelogs Smoother terrain reloading Add style layers directly to map Don't set fog by default Removed terrain functions entirely from map-interface Remove unused terrain functions Integrate terrain utilities into mapbox-react Break out default layers Moved bulk of terrain code to mapbox-react Terrain loads but with unstyled flash Story showing issue with map reloading Created skeletal terrain reloading demo
2 parents ffd849b + 39b1021 commit b3de8da

File tree

15 files changed

+427
-147
lines changed

15 files changed

+427
-147
lines changed

packages/map-interface/CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ All notable changes to this project will be documented in this file. The format
44
is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this
55
project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [1.2.3] - 2025-02-23
8+
9+
- Allow map interface to be rendered without a context panel
10+
- Create a story for streamlining style reloading with 3D terrain
11+
- Move all terrain management to `@macrostrat/mapbox-react`
12+
- New approach to setting up map styles that reduces the chance of full
13+
re-renders of terrain layers. This enables smoother transitions between
14+
minimally varying map styles.
15+
716
## [1.2.2] - 2025-02-16
817

918
- Improve styles for map sidebar and expansion panels

packages/map-interface/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@macrostrat/map-interface",
3-
"version": "1.2.2",
3+
"version": "1.2.3",
44
"description": "Map interface for Macrostrat",
55
"main": "dist/cjs/index.js",
66
"module": "dist/esm/index.js",

packages/map-interface/src/container.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ function _MapAreaContainer({
118118
);
119119

120120
let contextStack = null;
121-
if (navbar != null && contextPanel != null) {
121+
if (navbar != null || contextPanel != null) {
122122
contextStack = h(ContextStack, { navbar, ...contextStackProps }, [
123123
h.if(contextPanelTrans.shouldMount)([contextPanel]),
124124
]);

packages/map-interface/src/map-view/index.ts renamed to packages/map-interface/src/map-view.ts

Lines changed: 51 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,30 @@ import {
33
useMapRef,
44
useMapDispatch,
55
useMapPosition,
6+
setup3DTerrain,
7+
use3DTerrain,
8+
addTerrainToStyle,
69
} from "@macrostrat/mapbox-react";
710
import {
811
mapViewInfo,
912
MapPosition,
1013
setMapPosition,
14+
getMapPosition,
15+
getMapboxStyle,
1116
} from "@macrostrat/mapbox-utils";
1217
import classNames from "classnames";
1318
import mapboxgl from "mapbox-gl";
14-
import { useEffect, useRef, useCallback } from "react";
15-
import styles from "../main.module.sass";
16-
import { enable3DTerrain } from "./terrain";
19+
import { useEffect, useRef } from "react";
20+
import styles from "./main.module.sass";
1721
import {
1822
MapLoadingReporter,
1923
MapMovedReporter,
2024
MapPaddingManager,
2125
MapResizeManager,
22-
} from "../helpers";
26+
} from "./helpers";
2327
import "mapbox-gl/dist/mapbox-gl.css";
24-
import { getMapPadding } from "../utils";
28+
import { getMapPadding } from "./utils";
29+
import { useAsyncEffect } from "@macrostrat/ui-components";
2530

2631
const h = hyper.styled(styles);
2732

@@ -116,16 +121,45 @@ export function MapView(props: MapViewProps) {
116121
const ref = useRef<HTMLDivElement>();
117122
const parentRef = useRef<HTMLDivElement>();
118123

119-
useEffect(() => {
124+
useAsyncEffect(async () => {
125+
/** Manager to update map style */
120126
if (style == null) return;
121127
let map = mapRef.current;
128+
129+
/** If we can, we try to update the map style with terrain information
130+
* immediately, before the style is loaded. This allows us to avoid a
131+
* flash of the map without terrain.
132+
*
133+
* To do this, we need to estimate the map position before load, which
134+
* doesn't always work.
135+
*/
136+
// We either get the map position directly from the map or from props
137+
const estMapPosition: MapPosition | null =
138+
map == null ? mapPosition : getMapPosition(map);
139+
let newStyle = style;
140+
const { mapUse3D } = mapViewInfo(estMapPosition);
141+
142+
/** If style is a string, we can't update it with terrain layers immediately.
143+
* We need to wait for the style to load and then update it.
144+
*/
145+
if (typeof style === "string") {
146+
newStyle = await getMapboxStyle(style, {
147+
access_token: mapboxgl.accessToken,
148+
});
149+
}
150+
151+
if (mapUse3D) {
152+
// We can update the style with terrain layers immediately
153+
newStyle = addTerrainToStyle(newStyle as mapboxgl.Style, terrainSourceID);
154+
}
155+
122156
if (map != null) {
123-
console.log("Setting style", style);
124-
map.setStyle(style);
157+
console.log("Setting style", newStyle);
158+
map.setStyle(newStyle);
125159
} else {
126-
console.log("Initializing map", style);
160+
console.log("Initializing map", newStyle);
127161
const map = initializeMap(ref.current, {
128-
style,
162+
style: newStyle,
129163
projection,
130164
mapPosition,
131165
transformRequest,
@@ -138,11 +172,13 @@ export function MapView(props: MapViewProps) {
138172

139173
const loadCallback = () => {
140174
onStyleLoaded?.(map);
175+
// Set initial terrain state
141176
dispatch({ type: "set-style-loaded", payload: true });
142177
};
143178

144179
map = mapRef.current;
145-
if (map.isStyleLoaded()) {
180+
181+
if (map.style?._loaded) {
146182
// Catch a race condition where the style is loaded before the callback is set
147183
loadCallback();
148184
}
@@ -174,24 +210,21 @@ export function MapView(props: MapViewProps) {
174210
h(MapMovedReporter, { onMapMoved }),
175211
h(MapResizeManager, { containerRef: ref }),
176212
h(MapPaddingManager, { containerRef: ref, parentRef, infoMarkerPosition }),
177-
h(MapTerrainManager, { mapUse3D, terrainSourceID }),
213+
h(MapTerrainManager, { mapUse3D, terrainSourceID, style }),
178214
children,
179215
]);
180216
}
181217

182218
export function MapTerrainManager({
183219
mapUse3D,
184220
terrainSourceID,
221+
style,
185222
}: {
186223
mapUse3D?: boolean;
187224
terrainSourceID?: string;
225+
style?: mapboxgl.Style | string;
188226
}) {
189-
const mapRef = useMapRef();
227+
use3DTerrain(mapUse3D, terrainSourceID);
190228

191-
useEffect(() => {
192-
const map = mapRef.current;
193-
if (map == null) return;
194-
enable3DTerrain(map, mapUse3D, terrainSourceID);
195-
}, [mapRef.current, mapUse3D]);
196229
return null;
197230
}

packages/map-interface/src/map-view/terrain.ts

Lines changed: 0 additions & 63 deletions
This file was deleted.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.header-block
2+
.top-row
3+
display: flex
4+
justify-content: space-between
5+
align-items: center
6+
h3.header-title
7+
margin: 0.2em 0 0.5em
8+
text-overflow: ellipsis
9+
10+
.controls>:last-child
11+
margin-bottom: 0

0 commit comments

Comments
 (0)