mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-30 16:33:16 +00:00
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:
@@ -7,7 +7,7 @@
|
||||
"src/components/Budget/CostsPanel.tsx": 1607,
|
||||
"src/components/Map/MapView.tsx": 1191,
|
||||
"src/components/Map/MapViewGL.test.tsx": 2919,
|
||||
"src/components/Map/MapViewGL.tsx": 2107,
|
||||
"src/components/Map/MapViewGL.tsx": 2083,
|
||||
"src/components/Planner/DayDetailPanel.test.tsx": 2331,
|
||||
"src/components/Planner/DayPlanSidebar.test.tsx": 5349,
|
||||
"src/components/Planner/DayPlanSidebar.tsx": 2908,
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
import { useEffect, useLayoutEffect, useRef, useImperativeHandle, useCallback, type Ref } from 'react'
|
||||
import type mapboxgl from 'mapbox-gl'
|
||||
import { useSettingsStore } from '../../store/settingsStore'
|
||||
import { isStandardFamily, supportsCustom3d, wantsTerrain, addCustom3dBuildings, addTerrainAndSky } from '../Map/mapboxSetup'
|
||||
import { isStandardFamily } from '../Map/mapboxSetup'
|
||||
import { applyGl3d, glMapOptions } from '../Map/glMapOptions'
|
||||
import { MAPBOX_DEFAULT_STYLE, styleForActiveProvider, basemapLanguage, type GlMapProvider } from '../Map/glProviders'
|
||||
import { buildGlMap, initialMapStyle } from '../Map/glSupport'
|
||||
import { applySatellite } from '../Map/glRasterLayers'
|
||||
import type { BaseLayer } from '../Map/MapLayerSwitcher'
|
||||
import { mapboxTileCacheRequest, tileCacheActive } from '../../native/tileCache'
|
||||
import type { JourneyTrack } from '@trek/shared'
|
||||
import { SHOT_INDEX_ATTR, ensureJourneyPopupStyle, formatMarkerDate, journeyPopupHtml } from './journeyMapPopup'
|
||||
import { clusterPhotos, photoMarkerHtml, type MapPhoto } from './journeyPhotoLayer'
|
||||
@@ -420,7 +420,8 @@ function JourneyMapGL(
|
||||
const bounds = markBounds(gl, [], entryPoints)
|
||||
const hasPoints = items.length > 0 || stableTrail.length > 0
|
||||
|
||||
const mapOptions: Record<string, unknown> = {
|
||||
const mapOptions = glMapOptions({
|
||||
provider: glProvider,
|
||||
container: containerRef.current,
|
||||
style: initialMapStyle(glProvider, glStyle),
|
||||
center: hasPoints ? bounds.getCenter() : [0, 30],
|
||||
@@ -430,15 +431,9 @@ function JourneyMapGL(
|
||||
// carousel across the bottom of this map and the strip read through it. The
|
||||
// credit stays one tap away, which is what the OSM licence asks for.
|
||||
attributionControl: { compact: true },
|
||||
antialias: mapboxQuality,
|
||||
}
|
||||
if (!isMapLibre) mapOptions.projection = mapboxQuality ? 'globe' : 'mercator'
|
||||
// In the iOS app, Mapbox's own resources go through the app's tile store, so
|
||||
// what was viewed stays on the device for up to 30 days (native/tileCache.ts).
|
||||
if (!isMapLibre && tileCacheActive()) mapOptions.transformRequest = mapboxTileCacheRequest
|
||||
// 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: mapboxQuality,
|
||||
appTileStore: true,
|
||||
})
|
||||
|
||||
// Null when the GPU refused WebGL2: JourneyMapAuto has switched to Leaflet by then.
|
||||
const map = buildGlMap(() => new gl.Map(mapOptions as any))
|
||||
@@ -457,15 +452,7 @@ function JourneyMapGL(
|
||||
visibilityRef.current[kind] ? base : { ...base, visibility: 'none' }
|
||||
|
||||
map.on('load', () => {
|
||||
if (enableMapbox3d) {
|
||||
if (!isStandardFamily(glStyle) && wantsTerrain(glStyle)) addTerrainAndSky(map)
|
||||
if (supportsCustom3d(glStyle)) addCustom3dBuildings(map, !!darkRef.current)
|
||||
}
|
||||
// Flatten Mapbox Standard's built-in DEM so HTML markers (at Z=0)
|
||||
// stay pinned to their coordinates at every zoom and pitch.
|
||||
if (glStyle === MAPBOX_DEFAULT_STYLE) {
|
||||
try { map.setTerrain(null) } catch { /* noop */ }
|
||||
}
|
||||
applyGl3d(map, glStyle, enableMapbox3d, !!darkRef.current)
|
||||
// Pin the basemap label language to the UI language so labels don't fall back to the
|
||||
// browser/OS locale and stack multiple scripts per place (#1299).
|
||||
if (!isMapLibre && isStandardFamily(glStyle)) {
|
||||
|
||||
@@ -20,7 +20,8 @@ import { placeMarkerFrame, placeMarkerLook, type PlaceMarkerFlags } from './mark
|
||||
import { bottomChromePadding } from './mapChromePadding'
|
||||
import { useSelectionCameraGL } from './useSelectionCameraGL'
|
||||
import { markerPhotoHtml, placePhotoFull, placePhotoUrl } from './placePhoto'
|
||||
import { isStandardFamily, supportsCustom3d, wantsTerrain, addCustom3dBuildings, addTerrainAndSky } from './mapboxSetup'
|
||||
import { isStandardFamily } from './mapboxSetup'
|
||||
import { applyGl3d, glMapOptions } from './glMapOptions'
|
||||
import { attachLocationMarker, type LocationMarkerHandle } from './locationMarkerMapbox'
|
||||
import { ReservationMapboxOverlay } from './reservationsMapbox'
|
||||
import { useFlightAircraftGL } from './useFlightAircraftGL'
|
||||
@@ -30,7 +31,6 @@ import { safeHexColor } from '../../utils/safeColor'
|
||||
import { MAPBOX_DEFAULT_STYLE, styleForActiveProvider, type GlMapProvider } from './glProviders'
|
||||
import { useStandardBasemapConfig } from './useStandardBasemapConfig'
|
||||
import { buildGlMap, initialMapStyle } from './glSupport'
|
||||
import { mapboxTileCacheRequest, tileCacheActive } from '../../native/tileCache'
|
||||
import LocationButton from './LocationButton'
|
||||
import { useIsPhone } from '../../mobile/useIsPhone'
|
||||
import { useGeolocation } from '../../hooks/useGeolocation'
|
||||
@@ -941,21 +941,17 @@ export function MapViewGL({
|
||||
framedOnMountRef.current = framed !== null
|
||||
const initial = framed ?? { center, zoom }
|
||||
|
||||
const mapOptions: Record<string, unknown> = {
|
||||
const mapOptions = glMapOptions({
|
||||
provider: glProvider,
|
||||
container: containerRef.current,
|
||||
style: initialMapStyle(glProvider, glStyle),
|
||||
center: [initial.center[1], initial.center[0]],
|
||||
zoom: initial.zoom,
|
||||
pitch: enableMapbox3d ? 45 : 0,
|
||||
attributionControl: true,
|
||||
antialias: mapboxQuality,
|
||||
}
|
||||
if (!isMapLibre) mapOptions.projection = mapboxQuality ? 'globe' : 'mercator'
|
||||
// In the iOS app, Mapbox's own resources go through the app's tile store, so
|
||||
// what was viewed stays on the device for up to 30 days (native/tileCache.ts).
|
||||
if (!isMapLibre && tileCacheActive()) mapOptions.transformRequest = mapboxTileCacheRequest
|
||||
// MapLibre's around-center rotate ping-pongs near mid-screen (#1545); false is mapbox-gl's dx rotate.
|
||||
if (isMapLibre) mapOptions.aroundCenter = false
|
||||
quality: mapboxQuality,
|
||||
appTileStore: true,
|
||||
})
|
||||
|
||||
// Null when the GPU refused WebGL2: MapViewAuto has switched to Leaflet by then.
|
||||
const map = buildGlMap(() => new gl.Map(mapOptions as any))
|
||||
@@ -994,27 +990,7 @@ export function MapViewGL({
|
||||
;(window as any).__trek_map = map
|
||||
|
||||
map.on('load', () => {
|
||||
if (enableMapbox3d) {
|
||||
// Terrain is only valuable on satellite styles — on clean vector
|
||||
// styles it makes route lines drift off the HTML markers because
|
||||
// the lines snap to DEM height while markers stay at sea level.
|
||||
if (!isStandardFamily(glStyle) && wantsTerrain(glStyle)) addTerrainAndSky(map)
|
||||
if (supportsCustom3d(glStyle)) {
|
||||
const dark = document.documentElement.classList.contains('dark')
|
||||
addCustom3dBuildings(map, dark)
|
||||
}
|
||||
}
|
||||
|
||||
// Mapbox Standard ships its own DEM-based terrain that kicks in
|
||||
// below zoom 13.7. HTML markers project at sea level, so when the
|
||||
// terrain exaggeration ramps up at lower zooms the markers drift
|
||||
// away from the 3D buildings and route lines they belong to. The
|
||||
// non-satellite Standard style still looks great without terrain,
|
||||
// so flatten it out to keep markers pinned. (Satellite variants
|
||||
// are left alone — the DEM is what gives them their character.)
|
||||
if (glStyle === MAPBOX_DEFAULT_STYLE) {
|
||||
try { map.setTerrain(null) } catch { /* noop */ }
|
||||
}
|
||||
applyGl3d(map, glStyle, enableMapbox3d, document.documentElement.classList.contains('dark'))
|
||||
// The ways of driving one leg, offered while the picker is open (#1797). Added
|
||||
// BEFORE the route source so its layers sit underneath: the current route stays the
|
||||
// brightest thing on the map, and the options read as what they are — suggestions.
|
||||
|
||||
@@ -0,0 +1,205 @@
|
||||
/**
|
||||
* The construction options of every GL map (FE-MAP-GLOPTIONS-*).
|
||||
*
|
||||
* Mapbox GL is TREK's 3D map and its options are pinned here exactly as the
|
||||
* trip map, the journey map and the settings preview built them before they
|
||||
* shared this builder: every key, every value, nothing more. A change to the
|
||||
* Mapbox branch has to change this test on purpose.
|
||||
*/
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
import { applyGl3d, glMapOptions } from './glMapOptions';
|
||||
|
||||
const env = vi.hoisted(() => ({ tileStore: false }));
|
||||
const transformRequest = vi.hoisted(() => () => ({ url: '' }));
|
||||
|
||||
vi.mock('../../native/tileCache', () => ({
|
||||
tileCacheActive: () => env.tileStore,
|
||||
mapboxTileCacheRequest: transformRequest,
|
||||
}));
|
||||
|
||||
const container = document.createElement('div');
|
||||
const STREETS = 'mapbox://styles/mapbox/streets-v12';
|
||||
const STANDARD = 'mapbox://styles/mapbox/standard';
|
||||
const SATELLITE = 'mapbox://styles/mapbox/satellite-v9';
|
||||
const LIBERTY = 'https://tiles.openfreemap.org/styles/liberty';
|
||||
|
||||
function tripMap(provider: 'mapbox-gl' | 'maplibre-gl', quality: boolean, style = STREETS) {
|
||||
return glMapOptions({
|
||||
provider,
|
||||
container,
|
||||
style,
|
||||
center: [2.35, 48.85],
|
||||
zoom: 12,
|
||||
pitch: 45,
|
||||
attributionControl: true,
|
||||
quality,
|
||||
appTileStore: true,
|
||||
});
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
env.tileStore = false;
|
||||
});
|
||||
|
||||
describe('glMapOptions: Mapbox, byte for byte as before', () => {
|
||||
/** What the trip map built, key for key. */
|
||||
const tripMapOptions = {
|
||||
container,
|
||||
style: STREETS,
|
||||
center: [2.35, 48.85],
|
||||
zoom: 12,
|
||||
pitch: 45,
|
||||
attributionControl: true,
|
||||
antialias: false,
|
||||
projection: 'mercator',
|
||||
};
|
||||
|
||||
it('FE-MAP-GLOPTIONS-001: the trip map', () => {
|
||||
expect(tripMap('mapbox-gl', false)).toStrictEqual(tripMapOptions);
|
||||
});
|
||||
|
||||
it('FE-MAP-GLOPTIONS-002: the quality mode antialiases and draws the globe', () => {
|
||||
expect(tripMap('mapbox-gl', true)).toStrictEqual({ ...tripMapOptions, antialias: true, projection: 'globe' });
|
||||
});
|
||||
|
||||
it('FE-MAP-GLOPTIONS-003: in the iOS app with its tile store, Mapbox resources go through it', () => {
|
||||
env.tileStore = true;
|
||||
expect(tripMap('mapbox-gl', false)).toStrictEqual({ ...tripMapOptions, transformRequest });
|
||||
});
|
||||
|
||||
it('FE-MAP-GLOPTIONS-004: the journey map, with its compact credit', () => {
|
||||
env.tileStore = true;
|
||||
const lngLat = { lng: 9.1, lat: 45.4 };
|
||||
const options = glMapOptions({
|
||||
provider: 'mapbox-gl',
|
||||
container,
|
||||
style: STANDARD,
|
||||
center: lngLat,
|
||||
zoom: 2,
|
||||
pitch: 0,
|
||||
attributionControl: { compact: true },
|
||||
quality: false,
|
||||
appTileStore: true,
|
||||
});
|
||||
expect(options).toStrictEqual({
|
||||
...tripMapOptions,
|
||||
style: STANDARD,
|
||||
center: lngLat,
|
||||
zoom: 2,
|
||||
pitch: 0,
|
||||
attributionControl: { compact: true },
|
||||
transformRequest,
|
||||
});
|
||||
});
|
||||
|
||||
it('FE-MAP-GLOPTIONS-005: the settings preview never goes through the tile store', () => {
|
||||
env.tileStore = true;
|
||||
const options = glMapOptions({
|
||||
provider: 'mapbox-gl',
|
||||
container,
|
||||
style: STREETS,
|
||||
center: [2.3, 48.8],
|
||||
zoom: 16,
|
||||
pitch: 0,
|
||||
attributionControl: true,
|
||||
quality: true,
|
||||
});
|
||||
expect(options).toStrictEqual({
|
||||
...tripMapOptions,
|
||||
center: [2.3, 48.8],
|
||||
zoom: 16,
|
||||
pitch: 0,
|
||||
antialias: true,
|
||||
projection: 'globe',
|
||||
});
|
||||
});
|
||||
|
||||
it('FE-MAP-GLOPTIONS-006: keys come in the order they always had', () => {
|
||||
env.tileStore = true;
|
||||
expect(Object.keys(tripMap('mapbox-gl', false))).toEqual([...Object.keys(tripMapOptions), 'transformRequest']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('glMapOptions: MapLibre', () => {
|
||||
it('FE-MAP-GLOPTIONS-010: rotates around the pointer and has no projection or tile store', () => {
|
||||
env.tileStore = true;
|
||||
const options = tripMap('maplibre-gl', false, LIBERTY);
|
||||
expect(options).toMatchObject({ style: LIBERTY, antialias: false, aroundCenter: false });
|
||||
expect(Object.keys(options)).toEqual([
|
||||
'container',
|
||||
'style',
|
||||
'center',
|
||||
'zoom',
|
||||
'pitch',
|
||||
'attributionControl',
|
||||
'antialias',
|
||||
'aroundCenter',
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('applyGl3d', () => {
|
||||
function fakeMap() {
|
||||
return {
|
||||
getLayer: vi.fn(() => undefined),
|
||||
getSource: vi.fn(() => undefined),
|
||||
addSource: vi.fn(),
|
||||
addLayer: vi.fn(),
|
||||
setTerrain: vi.fn(),
|
||||
getStyle: vi.fn(() => ({ layers: [] })),
|
||||
};
|
||||
}
|
||||
type GlMap = Parameters<typeof applyGl3d>[0];
|
||||
|
||||
it('FE-MAP-GLOPTIONS-020: a satellite style in 3D gets terrain and buildings', () => {
|
||||
const map = fakeMap();
|
||||
applyGl3d(map as unknown as GlMap, SATELLITE, true, false);
|
||||
expect(map.setTerrain).toHaveBeenCalledWith({ source: 'mapbox-dem', exaggeration: 1.2 });
|
||||
expect(map.addLayer).toHaveBeenCalledWith(expect.objectContaining({ id: 'trek-3d-buildings' }), undefined);
|
||||
});
|
||||
|
||||
it('FE-MAP-GLOPTIONS-021: a flat vector style in 3D gets buildings and no terrain', () => {
|
||||
const map = fakeMap();
|
||||
applyGl3d(map as unknown as GlMap, STREETS, true, true);
|
||||
expect(map.setTerrain).not.toHaveBeenCalled();
|
||||
expect(map.addLayer).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
id: 'trek-3d-buildings',
|
||||
paint: expect.objectContaining({ 'fill-extrusion-color': '#3b3b3f' }),
|
||||
}),
|
||||
undefined
|
||||
);
|
||||
});
|
||||
|
||||
it('FE-MAP-GLOPTIONS-022: Mapbox Standard brings its own 3D and has its terrain flattened, 3D or not', () => {
|
||||
for (const enable3d of [true, false]) {
|
||||
const map = fakeMap();
|
||||
applyGl3d(map as unknown as GlMap, STANDARD, enable3d, false);
|
||||
expect(map.addLayer).not.toHaveBeenCalled();
|
||||
expect(map.setTerrain).toHaveBeenCalledWith(null);
|
||||
}
|
||||
});
|
||||
|
||||
it('FE-MAP-GLOPTIONS-023: a style not ready for terrain does not break the load', () => {
|
||||
const map = fakeMap();
|
||||
map.setTerrain.mockImplementation(() => {
|
||||
throw new Error('style not loaded');
|
||||
});
|
||||
expect(() => applyGl3d(map as unknown as GlMap, STANDARD, false, false)).not.toThrow();
|
||||
});
|
||||
|
||||
it('FE-MAP-GLOPTIONS-024: with 3D off a non-Standard style is left as it is', () => {
|
||||
const map = fakeMap();
|
||||
applyGl3d(map as unknown as GlMap, SATELLITE, false, false);
|
||||
expect(map.addLayer).not.toHaveBeenCalled();
|
||||
expect(map.setTerrain).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('FE-MAP-GLOPTIONS-025: a MapLibre style is left as it is', () => {
|
||||
const map = fakeMap();
|
||||
applyGl3d(map as unknown as GlMap, LIBERTY, false, false);
|
||||
expect(map.addLayer).not.toHaveBeenCalled();
|
||||
expect(map.setTerrain).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,86 @@
|
||||
import type mapboxgl from 'mapbox-gl';
|
||||
|
||||
import { mapboxTileCacheRequest, tileCacheActive } from '../../native/tileCache';
|
||||
import { MAPBOX_DEFAULT_STYLE, type GlMapProvider } from './glProviders';
|
||||
import {
|
||||
addCustom3dBuildings,
|
||||
addTerrainAndSky,
|
||||
isStandardFamily,
|
||||
supportsCustom3d,
|
||||
wantsTerrain,
|
||||
} from './mapboxSetup';
|
||||
|
||||
/*
|
||||
* How every GL map in TREK is constructed: the trip map, the journey map and the
|
||||
* settings preview. The three used to carry their own copy of these options and
|
||||
* of the 3D setup, and the copies had drifted (the preview laid terrain under
|
||||
* every non-Standard style, the trip map only under the satellite and outdoor
|
||||
* ones). Like the renderers it serves, this module stays free of both SDKs at
|
||||
* runtime (`check:gl-split`): the engine arrives as the caller's `gl`.
|
||||
*
|
||||
* The Mapbox options are exactly what the three built before; a test pins them.
|
||||
*/
|
||||
|
||||
interface GlMapOptionsInput {
|
||||
provider: GlMapProvider;
|
||||
container: HTMLElement;
|
||||
/** A style URL, or a style document for a stored or inline style. */
|
||||
style: unknown;
|
||||
/** `[lng, lat]`, or the engine's own LngLat. */
|
||||
center: unknown;
|
||||
zoom: number;
|
||||
pitch: number;
|
||||
attributionControl: boolean | { compact: boolean };
|
||||
/** The quality mode: antialiasing, and on Mapbox the globe instead of the flat projection. */
|
||||
quality: boolean;
|
||||
/**
|
||||
* In the iOS app, Mapbox's own resources go through the app's tile store, so
|
||||
* what was viewed stays on the device for up to 30 days (native/tileCache.ts).
|
||||
* The maps a user travels with ask for it; the settings preview does not.
|
||||
*/
|
||||
appTileStore?: boolean;
|
||||
}
|
||||
|
||||
/** The constructor options for `new gl.Map(...)`. */
|
||||
export function glMapOptions(input: GlMapOptionsInput): Record<string, unknown> {
|
||||
const isMapLibre = input.provider === 'maplibre-gl';
|
||||
const options: Record<string, unknown> = {
|
||||
container: input.container,
|
||||
style: input.style,
|
||||
center: input.center,
|
||||
zoom: input.zoom,
|
||||
pitch: input.pitch,
|
||||
attributionControl: input.attributionControl,
|
||||
antialias: input.quality,
|
||||
};
|
||||
if (!isMapLibre) options.projection = input.quality ? 'globe' : 'mercator';
|
||||
if (!isMapLibre && input.appTileStore && tileCacheActive()) options.transformRequest = mapboxTileCacheRequest;
|
||||
// MapLibre's around-center rotate ping-pongs near mid-screen (#1545); false is mapbox-gl's dx rotate.
|
||||
if (isMapLibre) options.aroundCenter = false;
|
||||
return options;
|
||||
}
|
||||
|
||||
/**
|
||||
* The 3D look of a Mapbox style, on the map's `load`.
|
||||
*
|
||||
* Terrain only under the styles that benefit from elevation (satellite and
|
||||
* outdoors): on a flat vector style it lifts route lines onto the DEM while the
|
||||
* HTML markers stay at sea level, and they drift apart once the map is pitched.
|
||||
* Buildings go on every style outside the Standard family, which brings its own.
|
||||
* Mapbox Standard's built-in DEM is flattened in any case, so the markers stay
|
||||
* on their coordinates at every zoom; the satellite variant keeps it, the DEM is
|
||||
* what gives it its character. A MapLibre style matches none of these.
|
||||
*/
|
||||
export function applyGl3d(map: mapboxgl.Map, style: string, enable3d: boolean, dark: boolean): void {
|
||||
if (enable3d) {
|
||||
if (!isStandardFamily(style) && wantsTerrain(style)) addTerrainAndSky(map);
|
||||
if (supportsCustom3d(style)) addCustom3dBuildings(map, dark);
|
||||
}
|
||||
if (style === MAPBOX_DEFAULT_STYLE) {
|
||||
try {
|
||||
map.setTerrain(null);
|
||||
} catch {
|
||||
/* the style is not ready for terrain yet; it has none to flatten then */
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user