refactor(maps): build every GL map from one set of options

The trip map, the journey map and the settings preview each assembled
their own constructor options and their own 3D setup on load, and the
copies had drifted: the preview laid terrain under every style outside
the Standard family, while the trip map only under the satellite and
outdoor styles, so the preview showed a map the trip never drew.

glMapOptions builds the options for all three and applyGl3d sets up
terrain, buildings and the flattened Standard DEM. For Mapbox the options
are exactly the ones each map built before, key for key and in the same
order, which a test pins; the preview now follows the trip map's terrain
rule. The next steps tune MapLibre in this one place.

MapViewGL.tsx shrinks, and its size baseline follows.
This commit is contained in:
Maurice
2026-11-30 14:38:03 +01:00
parent 0a72ed5e76
commit 6dafbc6d05
7 changed files with 333 additions and 76 deletions
@@ -39,11 +39,12 @@ vi.mock('maplibre-gl/dist/maplibre-gl.css', () => ({}));
vi.mock('../Map/mapboxSetup', () => ({
isStandardFamily: vi.fn(() => false),
supportsCustom3d: vi.fn(() => true),
wantsTerrain: vi.fn(() => false),
addCustom3dBuildings: vi.fn(),
addTerrainAndSky: vi.fn(),
}));
import { isStandardFamily, supportsCustom3d, addCustom3dBuildings, addTerrainAndSky } from '../Map/mapboxSetup';
import { isStandardFamily, supportsCustom3d, wantsTerrain, addCustom3dBuildings, addTerrainAndSky } from '../Map/mapboxSetup';
import { MAPBOX_DEFAULT_STYLE, OPENFREEMAP_DEFAULT_STYLE } from '../Map/glProviders';
import mapboxgl from 'mapbox-gl';
import * as maplibregl from 'maplibre-gl';
@@ -59,6 +60,7 @@ function GlMapPreview(props: any) {
}
const STREETS = 'mapbox://styles/mapbox/streets-v12';
const OUTDOORS = 'mapbox://styles/mapbox/outdoors-v12';
function mapOptions(ctor: typeof mapboxCtor): Record<string, unknown> {
return ctor.mock.calls[0][0] as Record<string, unknown>;
@@ -71,6 +73,7 @@ beforeEach(() => {
document.documentElement.classList.remove('dark');
vi.mocked(isStandardFamily).mockReturnValue(false);
vi.mocked(supportsCustom3d).mockReturnValue(true);
vi.mocked(wantsTerrain).mockReturnValue(false);
});
describe('GlMapPreview', () => {
@@ -135,15 +138,25 @@ describe('GlMapPreview', () => {
expect(mapOptions(maplibreCtor).style).toBe(OPENFREEMAP_DEFAULT_STYLE);
});
it('FE-COMP-GLPREVIEW-007: loading a 3D-capable custom style adds terrain and buildings', () => {
render(<GlMapPreview token="pk.test" style={STREETS} lat={48.8} lng={2.3} zoom={16} enable3d />);
it('FE-COMP-GLPREVIEW-007: loading a style that wants terrain adds terrain and buildings', () => {
vi.mocked(wantsTerrain).mockReturnValue(true);
render(<GlMapPreview token="pk.test" style={OUTDOORS} lat={48.8} lng={2.3} zoom={16} enable3d />);
act(() => glHandlers.load?.());
expect(wantsTerrain).toHaveBeenCalledWith(OUTDOORS);
expect(addTerrainAndSky).toHaveBeenCalledWith(glMap);
expect(addCustom3dBuildings).toHaveBeenCalledWith(glMap, false);
expect(glMap.setTerrain).not.toHaveBeenCalled();
});
it('FE-COMP-GLPREVIEW-007b: a flat vector style gets buildings but no terrain, as on the trip map', () => {
render(<GlMapPreview token="pk.test" style={STREETS} lat={48.8} lng={2.3} zoom={16} enable3d />);
act(() => glHandlers.load?.());
expect(addTerrainAndSky).not.toHaveBeenCalled();
expect(addCustom3dBuildings).toHaveBeenCalledWith(glMap, false);
});
it('FE-COMP-GLPREVIEW-008: dark mode is passed through to the 3D building colours', () => {
document.documentElement.classList.add('dark');
render(<GlMapPreview token="pk.test" style={STREETS} lat={48.8} lng={2.3} zoom={16} enable3d />);
@@ -1,6 +1,7 @@
import { useEffect, useRef } from 'react'
import { isStandardFamily, supportsCustom3d, addCustom3dBuildings, addTerrainAndSky } from '../Map/mapboxSetup'
import { MAPBOX_DEFAULT_STYLE, normalizeStyleForProvider, type GlMapProvider } from '../Map/glProviders'
import { isStandardFamily } from '../Map/mapboxSetup'
import { applyGl3d, glMapOptions } from '../Map/glMapOptions'
import { normalizeStyleForProvider, type GlMapProvider } from '../Map/glProviders'
import { buildGlMap } from '../Map/glSupport'
import { setBasemapConfig } from '../Map/useStandardBasemapConfig'
import { fs } from '../shared/DialogShell'
@@ -40,19 +41,16 @@ export default function GlMapPreview({ provider = 'mapbox-gl', token = '', style
if (!containerRef.current || (!isMapLibre && !token)) return
if (!isMapLibre) gl.accessToken = token
const mapOptions: Record<string, unknown> = {
const mapOptions = glMapOptions({
provider,
container: containerRef.current,
style: glStyle,
center: [lng, lat],
zoom,
pitch: enableMapbox3d ? 45 : 0,
attributionControl: true,
antialias: quality,
}
if (!isMapLibre) mapOptions.projection = quality ? 'globe' : 'mercator'
// MapLibre's around-center mouse rotate ping-pongs near mid-screen (#1545)
// — see MapViewGL. Keep the plain dx-based rotate everywhere.
if (isMapLibre) mapOptions.aroundCenter = false
quality,
})
// Null when the GPU refused WebGL2: the settings form has switched the preview to Leaflet by then.
const map = buildGlMap(() => new gl.Map(mapOptions as any))
@@ -60,16 +58,8 @@ export default function GlMapPreview({ provider = 'mapbox-gl', token = '', style
mapRef.current = map
map.on('load', () => {
if (enableMapbox3d) {
if (!isStandardFamily(glStyle)) addTerrainAndSky(map)
if (supportsCustom3d(glStyle)) {
const dark = document.documentElement.classList.contains('dark')
addCustom3dBuildings(map, dark)
}
}
if (glStyle === MAPBOX_DEFAULT_STYLE) {
try { map.setTerrain(null) } catch { /* noop */ }
}
// The trip map's 3D, terrain rule included, so the preview shows what the trip will.
applyGl3d(map, glStyle, enableMapbox3d, document.documentElement.classList.contains('dark'))
if (hidePois && !isMapLibre && isStandardFamily(glStyle)) setBasemapConfig(map, 'showPointOfInterestLabels', false)
})