mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-01 00:43:15 +00:00
feat(settings): pick your own map template or the theme style for MapLibre
The MapLibre style picker offers two more entries after the OpenFreeMap styles: OpenFreeMap that follows the light or dark theme (trek:ofm-auto), and the user's own map template (trek:template), which brings the template field under the picker, and the CARTO key for a CARTO template. Where the trip map will draw the template with the classic 2D map (Amap, a host the CSP does not name) the form says so and the preview shows that map. The preview draws exactly what the trip map will. Every place the forms load or save a MapLibre style keeps a keyword now, where it used to turn it into Liberty on the first unchanged save: the user form on desktop and phone (load, save, the phone's mirrored field, a provider switch there and back) and the admin defaults (provider switch, preset pick, typed style). The rules live in mapSettingsModel, so both shells render from one decision: - the template keyword with an empty template saves Liberty; - switching from Leaflet with a template and an empty MapLibre slot preselects the template, shown and not saved until Save; - Leaflet never writes a keyword into the Mapbox slot, and Mapbox shows its default for one. New strings in all locales.
This commit is contained in:
@@ -158,4 +158,44 @@ describe('useDefaultUserSettings', () => {
|
||||
await waitFor(() => expect(update).toHaveBeenLastCalledWith({ maplibre_style: OPENFREEMAP_DEFAULT_STYLE }));
|
||||
await waitFor(() => expect(toast.success).toHaveBeenCalledTimes(3));
|
||||
});
|
||||
|
||||
it('FE-COMP-DEFAULTSETTINGS-007: a MapLibre keyword default survives loading, a switch, a pick and a typed commit', async () => {
|
||||
const update = stub({
|
||||
map_provider: 'maplibre-gl',
|
||||
maplibre_style: 'trek:template',
|
||||
mapbox_style: 'mapbox://styles/me/x',
|
||||
});
|
||||
const { result } = await loaded();
|
||||
expect(result.current.mapboxStyle).toBe('trek:template');
|
||||
// The picker names both keywords after the OpenFreeMap styles.
|
||||
expect(result.current.glStylePresets.slice(-2)).toEqual([
|
||||
{ name: 'settings.mapStyleFollowTheme', url: 'trek:ofm-auto', tags: ['OpenFreeMap'] },
|
||||
{ name: 'settings.mapStyleTemplate', url: 'trek:template' },
|
||||
]);
|
||||
|
||||
act(() => result.current.commitStyle());
|
||||
await waitFor(() => expect(update).toHaveBeenLastCalledWith({ maplibre_style: 'trek:template' }));
|
||||
|
||||
act(() => result.current.saveMapProvider('mapbox-gl'));
|
||||
await waitFor(() =>
|
||||
expect(update).toHaveBeenLastCalledWith({ map_provider: 'mapbox-gl', mapbox_style: 'mapbox://styles/me/x' })
|
||||
);
|
||||
act(() => result.current.saveMapProvider('maplibre-gl'));
|
||||
await waitFor(() =>
|
||||
expect(update).toHaveBeenLastCalledWith({ map_provider: 'maplibre-gl', maplibre_style: 'trek:template' })
|
||||
);
|
||||
expect(result.current.mapboxStyle).toBe('trek:template');
|
||||
|
||||
act(() => result.current.pickStylePreset('trek:ofm-auto'));
|
||||
await waitFor(() => expect(update).toHaveBeenLastCalledWith({ maplibre_style: 'trek:ofm-auto' }));
|
||||
});
|
||||
|
||||
it('FE-COMP-DEFAULTSETTINGS-008: a keyword typed for Mapbox is not kept, Mapbox draws neither', async () => {
|
||||
const update = stub({ map_provider: 'mapbox-gl', mapbox_style: 'trek:template' });
|
||||
const { result } = await loaded();
|
||||
expect(result.current.mapboxStyle).toBe(MAPBOX_DEFAULT_STYLE);
|
||||
expect(result.current.glStylePresets.map((p) => p.url)).not.toContain('trek:template');
|
||||
act(() => result.current.commitStyle());
|
||||
await waitFor(() => expect(update).toHaveBeenLastCalledWith({ mapbox_style: MAPBOX_DEFAULT_STYLE }));
|
||||
});
|
||||
});
|
||||
|
||||
@@ -7,16 +7,13 @@ import { useAuthStore } from '../../store/authStore';
|
||||
import { useSettingsStore } from '../../store/settingsStore';
|
||||
import type { DateFormat, DistanceUnit, Place, WeekStart } from '../../types';
|
||||
import { normalizeTileUrl } from '../../utils/tileUrl';
|
||||
import {
|
||||
defaultStyleForProvider,
|
||||
getStylePresets,
|
||||
normalizeStyleForProvider,
|
||||
styleSettingKey,
|
||||
} from '../Map/glProviders';
|
||||
import { defaultStyleForProvider, styleSettingKey } from '../Map/glProviders';
|
||||
import {
|
||||
type MapProvider,
|
||||
glStylePresetsFor,
|
||||
mapboxOffered,
|
||||
mapPresetsFor,
|
||||
normalizeGlSelection,
|
||||
normalizeProvider,
|
||||
styleForProvider,
|
||||
} from '../Settings/mapSettingsModel';
|
||||
@@ -127,7 +124,8 @@ export function useDefaultUserSettings({ errorMessage, previewCreatedAt }: Defau
|
||||
const [mapPreviewPlaces] = useState((): Place[] => [adminPreviewPlace(previewCreatedAt())]);
|
||||
|
||||
const mapProvider = normalizeProvider(defaults.map_provider);
|
||||
const glStylePresets = mapProvider === 'leaflet' ? [] : getStylePresets(mapProvider);
|
||||
// MapLibre also offers its keywords: a default that follows the colour scheme, or the template above.
|
||||
const glStylePresets = mapProvider === 'leaflet' ? [] : glStylePresetsFor(mapProvider, t);
|
||||
const styleKey: keyof Defaults = mapProvider === 'maplibre-gl' ? 'maplibre_style' : 'mapbox_style';
|
||||
|
||||
const saveMapProvider = (nextProvider: MapProvider) => {
|
||||
@@ -158,11 +156,11 @@ export function useDefaultUserSettings({ errorMessage, previewCreatedAt }: Defau
|
||||
}
|
||||
};
|
||||
|
||||
/** A typed style is normalized for the provider before it is saved. */
|
||||
/** A typed style is normalized for the provider before it is saved; a MapLibre keyword stays. */
|
||||
const commitStyle = () => {
|
||||
// The style input never renders for leaflet; this only narrows the provider type.
|
||||
if (mapProvider === 'leaflet') return;
|
||||
const nextStyle = normalizeStyleForProvider(mapProvider, mapboxStyle);
|
||||
const nextStyle = normalizeGlSelection(mapProvider, mapboxStyle);
|
||||
setMapboxStyle(nextStyle);
|
||||
void save({ [styleKey]: nextStyle });
|
||||
};
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// FE-COMP-MAP-001 to FE-COMP-MAP-036
|
||||
// FE-COMP-MAP-001 to FE-COMP-MAP-037
|
||||
import { render, screen, waitFor, within, fireEvent } from '../../../tests/helpers/render';
|
||||
import userEvent from '@testing-library/user-event';
|
||||
import { useAuthStore } from '../../store/authStore';
|
||||
@@ -482,4 +482,28 @@ describe('MapSettingsTab – CARTO key', () => {
|
||||
await user.click(screen.getByText('Save Map'));
|
||||
expect(updateSettings).toHaveBeenCalledWith(expect.objectContaining({ map_hide_basemap_pois: true }));
|
||||
});
|
||||
|
||||
it('FE-COMP-MAP-037: MapLibre picks its own map template from the style picker and saves both keys', async () => {
|
||||
const user = userEvent.setup();
|
||||
const updateSettings = vi.fn().mockResolvedValue(undefined);
|
||||
seedStore(useSettingsStore, { settings: buildSettings({ map_provider: 'maplibre-gl', map_tile_url: '' }), updateSettings });
|
||||
render(<MapSettingsTab />);
|
||||
expect(screen.queryByText('Map Template')).not.toBeInTheDocument();
|
||||
|
||||
await user.click(within(styleDropdown()).getByRole('button'));
|
||||
await user.click(await screen.findByText('My map template'));
|
||||
// The URL box goes, the template field and its picker follow the style picker.
|
||||
expect(screen.queryByDisplayValue('trek:template')).not.toBeInTheDocument();
|
||||
expect(screen.getByText('Map Template')).toBeInTheDocument();
|
||||
|
||||
await user.click(screen.getByText('Select template...'));
|
||||
await user.click(await screen.findByText('OpenStreetMap DE'));
|
||||
await user.click(screen.getByText('Save Map'));
|
||||
|
||||
expect(updateSettings).toHaveBeenCalledWith(expect.objectContaining({
|
||||
map_provider: 'maplibre-gl',
|
||||
maplibre_style: 'trek:template',
|
||||
map_tile_url: 'https://tile.openstreetmap.de/{z}/{x}/{y}.png',
|
||||
}));
|
||||
});
|
||||
});
|
||||
|
||||
@@ -14,7 +14,7 @@ import Section from './Section'
|
||||
import ToggleSwitch from './ToggleSwitch'
|
||||
import { SETTINGS_BUTTON_PRIMARY, SettingRow, SettingRows, SettingsHint, StatusPill } from './settingsKit'
|
||||
import { withTileApiKey } from '../../utils/tileUrl'
|
||||
import { defaultStyleForProvider, getStylePresets, type GlMapProvider } from '../Map/glProviders'
|
||||
import { defaultStyleForProvider, type GlMapProvider, type GlStylePreset } from '../Map/glProviders'
|
||||
import { PREVIEW_CENTER, PREVIEW_ZOOM } from './mapSettingsModel'
|
||||
import { useMapSettingsForm } from './useMapSettingsForm'
|
||||
|
||||
@@ -30,11 +30,15 @@ function TagChip({ tag }: { tag: string }) {
|
||||
)
|
||||
}
|
||||
|
||||
function StyleDropdown({ value, provider, onChange }: { value: string; provider: GlMapProvider; onChange: (v: string) => void }) {
|
||||
function StyleDropdown({ value, provider, presets, onChange }: {
|
||||
value: string
|
||||
provider: GlMapProvider
|
||||
presets: GlStylePreset[]
|
||||
onChange: (v: string) => void
|
||||
}) {
|
||||
const { t } = useTranslation()
|
||||
const [open, setOpen] = useState(false)
|
||||
const ref = useRef<HTMLDivElement>(null)
|
||||
const presets = getStylePresets(provider)
|
||||
|
||||
useEffect(() => {
|
||||
if (!open) return
|
||||
@@ -130,8 +134,9 @@ function ProviderTile({ active, onClick, icon: Icon, name, subtitle, badge }: {
|
||||
export default function MapSettingsTab(): React.ReactElement {
|
||||
const { t } = useTranslation()
|
||||
const {
|
||||
managed, presets, canMapbox, saving, provider, changeProvider, mapTileUrl, setMapTileUrl, mapboxToken, setMapboxToken,
|
||||
cartoKey, setCartoKey, cartoNeedsKey, mapboxStyle, setMapboxStyle, mapbox3d, setMapbox3d,
|
||||
managed, presets, stylePresets, canMapbox, saving, provider, changeProvider, mapTileUrl, setMapTileUrl,
|
||||
templateMode, templateNeedsClassic, showCartoKey, mapboxToken, setMapboxToken,
|
||||
cartoKey, setCartoKey, cartoNeedsKey, mapboxStyle, setMapboxStyle, showStyleUrl, previewStyle, mapbox3d, setMapbox3d,
|
||||
mapboxQuality, setMapboxQuality, hideBasemapPois, setHideBasemapPois, mapboxPreview, previewPlaces, glPreview,
|
||||
webgl2Missing, save: saveMapSettings,
|
||||
} = useMapSettingsForm()
|
||||
@@ -145,6 +150,60 @@ export default function MapSettingsTab(): React.ReactElement {
|
||||
const supports3d = true
|
||||
const link = 'font-medium text-content-secondary underline decoration-edge underline-offset-2 hover:text-content'
|
||||
|
||||
// The map template and its CARTO key: right under the provider on Leaflet, under the
|
||||
// style picker on MapLibre once its own map template is picked there.
|
||||
const templateFields = (
|
||||
<>
|
||||
<EditorField label={t('settings.mapTemplate')} htmlFor={`${fieldId}-tile`} hint={t('settings.mapDefaultHint')}>
|
||||
<div className="flex flex-col gap-2">
|
||||
<CustomSelect
|
||||
value={mapTileUrl}
|
||||
onChange={(value: string) => { if (value) setMapTileUrl(value) }}
|
||||
placeholder={t('settings.mapTemplatePlaceholder.select')}
|
||||
options={presets.map(p => ({ value: p.url, label: p.name }))}
|
||||
/>
|
||||
<input
|
||||
id={`${fieldId}-tile`}
|
||||
type="text"
|
||||
value={mapTileUrl}
|
||||
onChange={(e: React.ChangeEvent<HTMLInputElement>) => setMapTileUrl(e.target.value)}
|
||||
placeholder="https://tile.openstreetmap.org/{z}/{x}/{y}.png"
|
||||
className={CODE_INPUT}
|
||||
/>
|
||||
</div>
|
||||
</EditorField>
|
||||
{templateNeedsClassic && <SettingsHint>{t('settings.mapTemplateClassicHint')}</SettingsHint>}
|
||||
{/* Same deal as the Mapbox token: a managed install brings its own key. */}
|
||||
{showCartoKey && (
|
||||
<EditorField
|
||||
label={t('settings.mapCartoKey')}
|
||||
htmlFor={`${fieldId}-carto`}
|
||||
hint={
|
||||
<>
|
||||
{t('settings.mapCartoKeyHint')}{' '}
|
||||
<a href="https://carto.com/basemaps/apikey/" target="_blank" rel="noreferrer" className={link}>
|
||||
{t('settings.mapCartoKeyLink')}
|
||||
</a>
|
||||
{cartoNeedsKey && (
|
||||
<span className="mt-1 block font-medium text-warning">{t('settings.mapCartoKeyMissing')}</span>
|
||||
)}
|
||||
</>
|
||||
}
|
||||
>
|
||||
<input
|
||||
id={`${fieldId}-carto`}
|
||||
type="text"
|
||||
value={cartoKey}
|
||||
onChange={(e: React.ChangeEvent<HTMLInputElement>) => setCartoKey(e.target.value)}
|
||||
spellCheck={false}
|
||||
autoComplete="off"
|
||||
className={CODE_INPUT}
|
||||
/>
|
||||
</EditorField>
|
||||
)}
|
||||
</>
|
||||
)
|
||||
|
||||
return (
|
||||
<Section title={t('settings.map')} icon={Map}>
|
||||
{/* Provider picker — big tiles so the choice is obvious */}
|
||||
@@ -179,55 +238,7 @@ export default function MapSettingsTab(): React.ReactElement {
|
||||
</SettingRows>
|
||||
|
||||
{/* Leaflet settings */}
|
||||
{provider === 'leaflet' && (
|
||||
<EditorField label={t('settings.mapTemplate')} htmlFor={`${fieldId}-tile`} hint={t('settings.mapDefaultHint')}>
|
||||
<div className="flex flex-col gap-2">
|
||||
<CustomSelect
|
||||
value={mapTileUrl}
|
||||
onChange={(value: string) => { if (value) setMapTileUrl(value) }}
|
||||
placeholder={t('settings.mapTemplatePlaceholder.select')}
|
||||
options={presets.map(p => ({ value: p.url, label: p.name }))}
|
||||
/>
|
||||
<input
|
||||
id={`${fieldId}-tile`}
|
||||
type="text"
|
||||
value={mapTileUrl}
|
||||
onChange={(e: React.ChangeEvent<HTMLInputElement>) => setMapTileUrl(e.target.value)}
|
||||
placeholder="https://tile.openstreetmap.org/{z}/{x}/{y}.png"
|
||||
className={CODE_INPUT}
|
||||
/>
|
||||
</div>
|
||||
</EditorField>
|
||||
)}
|
||||
|
||||
{/* Same deal as the Mapbox token: a managed install brings its own key. */}
|
||||
{provider === 'leaflet' && !managed && (
|
||||
<EditorField
|
||||
label={t('settings.mapCartoKey')}
|
||||
htmlFor={`${fieldId}-carto`}
|
||||
hint={
|
||||
<>
|
||||
{t('settings.mapCartoKeyHint')}{' '}
|
||||
<a href="https://carto.com/basemaps/apikey/" target="_blank" rel="noreferrer" className={link}>
|
||||
{t('settings.mapCartoKeyLink')}
|
||||
</a>
|
||||
{cartoNeedsKey && (
|
||||
<span className="mt-1 block font-medium text-warning">{t('settings.mapCartoKeyMissing')}</span>
|
||||
)}
|
||||
</>
|
||||
}
|
||||
>
|
||||
<input
|
||||
id={`${fieldId}-carto`}
|
||||
type="text"
|
||||
value={cartoKey}
|
||||
onChange={(e: React.ChangeEvent<HTMLInputElement>) => setCartoKey(e.target.value)}
|
||||
spellCheck={false}
|
||||
autoComplete="off"
|
||||
className={CODE_INPUT}
|
||||
/>
|
||||
</EditorField>
|
||||
)}
|
||||
{provider === 'leaflet' && templateFields}
|
||||
|
||||
{/* GL settings */}
|
||||
{provider !== 'leaflet' && (
|
||||
@@ -264,18 +275,23 @@ export default function MapSettingsTab(): React.ReactElement {
|
||||
hint={provider === 'maplibre-gl' ? t('settings.mapOpenFreeMapStyleHint') : t('settings.mapStyleHint')}
|
||||
>
|
||||
<div className="flex flex-col gap-2">
|
||||
<StyleDropdown value={mapboxStyle} provider={provider} onChange={setMapboxStyle} />
|
||||
<input
|
||||
id={`${fieldId}-style`}
|
||||
type="text"
|
||||
value={mapboxStyle}
|
||||
onChange={(e) => setMapboxStyle(e.target.value)}
|
||||
placeholder={defaultStyleForProvider(provider)}
|
||||
className={CODE_INPUT}
|
||||
/>
|
||||
<StyleDropdown value={mapboxStyle} provider={provider} presets={stylePresets} onChange={setMapboxStyle} />
|
||||
{showStyleUrl && (
|
||||
<input
|
||||
id={`${fieldId}-style`}
|
||||
type="text"
|
||||
value={mapboxStyle}
|
||||
onChange={(e) => setMapboxStyle(e.target.value)}
|
||||
placeholder={defaultStyleForProvider(provider)}
|
||||
className={CODE_INPUT}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
</EditorField>
|
||||
|
||||
{/* MapLibre drawing the user's own template: its fields follow the picker. */}
|
||||
{templateMode && templateFields}
|
||||
|
||||
{provider === 'mapbox-gl' && (
|
||||
<>
|
||||
<SettingRows>
|
||||
@@ -333,7 +349,7 @@ export default function MapSettingsTab(): React.ReactElement {
|
||||
<GlMapPreview
|
||||
provider={provider}
|
||||
token={mapboxToken}
|
||||
style={mapboxStyle}
|
||||
style={previewStyle}
|
||||
lat={PREVIEW_CENTER[0]}
|
||||
lng={PREVIEW_CENTER[1]}
|
||||
// Zoom in close so the style's character (3D buildings,
|
||||
|
||||
@@ -49,6 +49,7 @@ import { MAPBOX_DEFAULT_STYLE, OPENFREEMAP_DEFAULT_STYLE } from '../Map/glProvid
|
||||
import mapboxgl from 'mapbox-gl';
|
||||
import * as maplibregl from 'maplibre-gl';
|
||||
import GlMapPreviewWithEngine from './MapboxPreview';
|
||||
import { mapLibreBasemap } from '../Map/glBasemap';
|
||||
|
||||
// The engine is a prop now, not a module import — that is what keeps mapbox-gl and
|
||||
// maplibre-gl in separate chunks. This shim preserves the suite's existing call
|
||||
@@ -138,6 +139,13 @@ describe('GlMapPreview', () => {
|
||||
expect(mapOptions(maplibreCtor).style).toBe(OPENFREEMAP_DEFAULT_STYLE);
|
||||
});
|
||||
|
||||
it('FE-COMP-GLPREVIEW-006b: a style a MapLibre keyword resolved to is drawn as it is, up to the raster zoom', () => {
|
||||
const style = mapLibreBasemap('trek:template', 'https://tile.openstreetmap.de/{z}/{x}/{y}.png', false, []).style;
|
||||
render(<GlMapPreview provider="maplibre-gl" style={style} lat={48.8} lng={2.3} zoom={16} enable3d={false} />);
|
||||
|
||||
expect(mapOptions(maplibreCtor)).toMatchObject({ style, maxZoom: 19 });
|
||||
});
|
||||
|
||||
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 />);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { useEffect, useRef } from 'react'
|
||||
import { isStandardFamily } from '../Map/mapboxSetup'
|
||||
import { applyGl3d, glMapOptions } from '../Map/glMapOptions'
|
||||
import type { GlStyle } from '../Map/glBasemap'
|
||||
import { normalizeStyleForProvider, type GlMapProvider } from '../Map/glProviders'
|
||||
import { buildGlMap } from '../Map/glSupport'
|
||||
import { setBasemapConfig } from '../Map/useStandardBasemapConfig'
|
||||
@@ -9,7 +10,8 @@ import { fs } from '../shared/DialogShell'
|
||||
interface Props {
|
||||
provider?: GlMapProvider
|
||||
token?: string
|
||||
style: string
|
||||
/** A style URL, or the style a MapLibre keyword resolved to (useMapSettingsForm). */
|
||||
style: GlStyle
|
||||
lat: number
|
||||
lng: number
|
||||
zoom: number
|
||||
@@ -34,7 +36,7 @@ export default function GlMapPreview({ provider = 'mapbox-gl', token = '', style
|
||||
const onClickRef = useRef(onClick)
|
||||
onClickRef.current = onClick
|
||||
const isMapLibre = provider === 'maplibre-gl'
|
||||
const glStyle = normalizeStyleForProvider(provider, style)
|
||||
const glStyle = typeof style === 'string' ? normalizeStyleForProvider(provider, style) : style
|
||||
const enableMapbox3d = !isMapLibre && enable3d
|
||||
|
||||
useEffect(() => {
|
||||
|
||||
@@ -1,7 +1,17 @@
|
||||
// FE-MAP-MODEL-001 to FE-MAP-MODEL-004
|
||||
// FE-MAP-MODEL-001 onwards
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { AMAP_ROAD, AMAP_SATELLITE } from '../../constants/mapDefaults';
|
||||
import { MAP_PRESETS, mapboxOffered, mapPresetsFor } from './mapSettingsModel';
|
||||
import { MAPBOX_DEFAULT_STYLE, OPENFREEMAP_DEFAULT_STYLE } from '../Map/glProviders';
|
||||
import {
|
||||
glStylePresetsFor,
|
||||
MAP_PRESETS,
|
||||
mapboxOffered,
|
||||
mapLibreStyleOnSwitch,
|
||||
mapLibreStyleToSave,
|
||||
mapPresetsFor,
|
||||
normalizeGlSelection,
|
||||
styleForProvider,
|
||||
} from './mapSettingsModel';
|
||||
|
||||
const urls = (list: { url: string }[]) => list.map((p) => p.url);
|
||||
|
||||
@@ -31,3 +41,53 @@ describe('mapboxOffered', () => {
|
||||
expect(mapboxOffered(true, 'pk.operator')).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('the MapLibre keywords in the forms', () => {
|
||||
const OSM = 'https://tile.openstreetmap.org/{z}/{x}/{y}.png';
|
||||
const BRIGHT = 'https://tiles.openfreemap.org/styles/bright';
|
||||
|
||||
it('FE-MAP-MODEL-005: MapLibre keeps a keyword, Mapbox shows its default for one', () => {
|
||||
expect(styleForProvider('maplibre-gl', 'trek:template')).toBe('trek:template');
|
||||
expect(styleForProvider('maplibre-gl', 'trek:ofm-auto')).toBe('trek:ofm-auto');
|
||||
expect(styleForProvider('mapbox-gl', 'trek:template')).toBe(MAPBOX_DEFAULT_STYLE);
|
||||
expect(styleForProvider('leaflet', 'trek:template')).toBe('trek:template');
|
||||
expect(normalizeGlSelection('maplibre-gl', ' trek:template ')).toBe('trek:template');
|
||||
expect(normalizeGlSelection('maplibre-gl', 'mapbox://styles/x/y')).toBe(OPENFREEMAP_DEFAULT_STYLE);
|
||||
// Mapbox keeps what was typed, as it always did.
|
||||
expect(normalizeGlSelection('mapbox-gl', BRIGHT)).toBe(BRIGHT);
|
||||
expect(normalizeGlSelection('mapbox-gl', '')).toBe(MAPBOX_DEFAULT_STYLE);
|
||||
});
|
||||
|
||||
it('FE-MAP-MODEL-006: a switch to MapLibre keeps a MapLibre choice and brings a stored keyword back', () => {
|
||||
expect(mapLibreStyleOnSwitch(BRIGHT, 'trek:template', OSM, true)).toBe(BRIGHT);
|
||||
expect(mapLibreStyleOnSwitch('trek:ofm-auto', null, OSM, false)).toBe('trek:ofm-auto');
|
||||
expect(mapLibreStyleOnSwitch(MAPBOX_DEFAULT_STYLE, 'trek:template', '', false)).toBe('trek:template');
|
||||
expect(mapLibreStyleOnSwitch(MAPBOX_DEFAULT_STYLE, BRIGHT, OSM, true)).toBe(OPENFREEMAP_DEFAULT_STYLE);
|
||||
});
|
||||
|
||||
it('FE-MAP-MODEL-007: from Leaflet with a template and an empty slot, the template is preselected', () => {
|
||||
expect(mapLibreStyleOnSwitch(MAPBOX_DEFAULT_STYLE, '', OSM, true)).toBe('trek:template');
|
||||
expect(mapLibreStyleOnSwitch(MAPBOX_DEFAULT_STYLE, undefined, OSM, true)).toBe('trek:template');
|
||||
// Not from Mapbox, not without a template.
|
||||
expect(mapLibreStyleOnSwitch(MAPBOX_DEFAULT_STYLE, '', OSM, false)).toBe(OPENFREEMAP_DEFAULT_STYLE);
|
||||
expect(mapLibreStyleOnSwitch(MAPBOX_DEFAULT_STYLE, '', ' ', true)).toBe(OPENFREEMAP_DEFAULT_STYLE);
|
||||
});
|
||||
|
||||
it('FE-MAP-MODEL-008: the template keyword without a template saves Liberty', () => {
|
||||
expect(mapLibreStyleToSave('trek:template', OSM)).toBe('trek:template');
|
||||
expect(mapLibreStyleToSave('trek:template', ' ')).toBe(OPENFREEMAP_DEFAULT_STYLE);
|
||||
expect(mapLibreStyleToSave('trek:ofm-auto', '')).toBe('trek:ofm-auto');
|
||||
expect(mapLibreStyleToSave('', OSM)).toBe(OPENFREEMAP_DEFAULT_STYLE);
|
||||
});
|
||||
|
||||
it('FE-MAP-MODEL-009: the MapLibre picker adds the two keywords after the OpenFreeMap styles, Mapbox none', () => {
|
||||
const t = (key: string) => `t:${key}`;
|
||||
const maplibre = glStylePresetsFor('maplibre-gl', t);
|
||||
expect(maplibre.slice(-2)).toEqual([
|
||||
{ name: 't:settings.mapStyleFollowTheme', url: 'trek:ofm-auto', tags: ['OpenFreeMap'] },
|
||||
{ name: 't:settings.mapStyleTemplate', url: 'trek:template' },
|
||||
]);
|
||||
expect(maplibre[0]?.url).toBe(OPENFREEMAP_DEFAULT_STYLE);
|
||||
expect(glStylePresetsFor('mapbox-gl', t).map((p) => p.url)).not.toContain('trek:template');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,10 +1,16 @@
|
||||
import { MAPLIBRE_FROM_TEMPLATE, MAPLIBRE_OFM_AUTO } from '@trek/shared';
|
||||
|
||||
import { AMAP_ROAD, AMAP_SATELLITE } from '../../constants/mapDefaults';
|
||||
import type { Place } from '../../types';
|
||||
import {
|
||||
MAPBOX_DEFAULT_STYLE,
|
||||
getStylePresets,
|
||||
isMapLibreKeyword,
|
||||
isOpenFreeMapStyle,
|
||||
normalizeMapLibreSelection,
|
||||
normalizeStyleForProvider,
|
||||
type GlMapProvider,
|
||||
type GlStylePreset,
|
||||
} from '../Map/glProviders';
|
||||
|
||||
/**
|
||||
@@ -67,12 +73,141 @@ export function normalizeProvider(value: unknown): MapProvider {
|
||||
return value === 'mapbox-gl' || value === 'maplibre-gl' ? value : 'leaflet';
|
||||
}
|
||||
|
||||
/**
|
||||
* The style a form shows for a provider. MapLibre keeps an OpenFreeMap style or
|
||||
* a keyword (trek:template, trek:ofm-auto); Mapbox draws neither, so either
|
||||
* shows its default; Leaflet keeps whatever the field holds.
|
||||
*/
|
||||
export function styleForProvider(provider: MapProvider, style?: string | null): string {
|
||||
if (provider === 'leaflet') return style || MAPBOX_DEFAULT_STYLE;
|
||||
if (provider === 'mapbox-gl' && isOpenFreeMapStyle(style)) return MAPBOX_DEFAULT_STYLE;
|
||||
if (provider === 'maplibre-gl') return normalizeMapLibreSelection(style);
|
||||
if (isOpenFreeMapStyle(style) || isMapLibreKeyword(style)) return MAPBOX_DEFAULT_STYLE;
|
||||
return normalizeStyleForProvider(provider, style);
|
||||
}
|
||||
|
||||
/**
|
||||
* A typed or picked style as it is saved: MapLibre keeps its OpenFreeMap styles
|
||||
* and keywords, Mapbox keeps what was typed, an empty field becomes the default.
|
||||
*/
|
||||
export function normalizeGlSelection(provider: GlMapProvider, style?: string | null): string {
|
||||
return provider === 'maplibre-gl' ? normalizeMapLibreSelection(style) : normalizeStyleForProvider(provider, style);
|
||||
}
|
||||
|
||||
/**
|
||||
* The MapLibre style a form shows once the user switches to MapLibre, given the
|
||||
* style the field held (`carried`), the one MapLibre's slot holds (`stored`) and
|
||||
* the map template. A MapLibre choice in the field stays; a keyword in the slot
|
||||
* comes back, so the template survives a switch away and back. Coming from
|
||||
* Leaflet with a template and nothing in the slot, the template is preselected:
|
||||
* MapLibre then draws the map the user had. Nothing is saved before "Save".
|
||||
*/
|
||||
export function mapLibreStyleOnSwitch(
|
||||
carried: string,
|
||||
stored: string | null | undefined,
|
||||
template: string,
|
||||
fromLeaflet: boolean
|
||||
): string {
|
||||
if (isOpenFreeMapStyle(carried) || isMapLibreKeyword(carried)) return carried.trim();
|
||||
if (isMapLibreKeyword(stored)) return normalizeMapLibreSelection(stored);
|
||||
if (fromLeaflet && !stored?.trim() && template.trim()) return MAPLIBRE_FROM_TEMPLATE;
|
||||
return normalizeMapLibreSelection(carried);
|
||||
}
|
||||
|
||||
/**
|
||||
* The MapLibre style to save. The template keyword with no template behind it
|
||||
* would draw the app default, so Liberty is saved instead, what MapLibre draws
|
||||
* for a user who chose nothing.
|
||||
*/
|
||||
export function mapLibreStyleToSave(style: string, template: string): string {
|
||||
const selection = normalizeMapLibreSelection(style);
|
||||
return selection === MAPLIBRE_FROM_TEMPLATE && !template.trim() ? normalizeMapLibreSelection('') : selection;
|
||||
}
|
||||
|
||||
/**
|
||||
* The style the field shows once the user picks another provider. MapLibre
|
||||
* follows mapLibreStyleOnSwitch, with the MapLibre slot as saved and the
|
||||
* template as the form holds it; Mapbox drops what only MapLibre draws; Leaflet
|
||||
* leaves the field as it is (it has no style of its own).
|
||||
*/
|
||||
export function styleOnProviderChange(
|
||||
from: MapProvider,
|
||||
to: MapProvider,
|
||||
style: string,
|
||||
context: { savedMaplibreStyle: string | undefined; template: string }
|
||||
): string {
|
||||
if (to === 'maplibre-gl') {
|
||||
return mapLibreStyleOnSwitch(style, context.savedMaplibreStyle, context.template, from === 'leaflet');
|
||||
}
|
||||
return to === 'leaflet' ? style : styleForProvider(to, style);
|
||||
}
|
||||
|
||||
/**
|
||||
* What a save writes for the style, and what the field shows afterwards. Each
|
||||
* GL provider writes its own slot; Leaflet writes the Mapbox slot as it always
|
||||
* did, except that a MapLibre keyword still in the field after a switch never
|
||||
* lands there: that slot keeps the style it holds (`storedMapboxStyle`).
|
||||
*/
|
||||
export function styleToSave(
|
||||
provider: MapProvider,
|
||||
style: string,
|
||||
template: string,
|
||||
storedMapboxStyle: string | undefined
|
||||
): { shown: string; patch: { maplibre_style: string } | { mapbox_style: string } } {
|
||||
if (provider === 'maplibre-gl') {
|
||||
const saved = mapLibreStyleToSave(style, template);
|
||||
return { shown: saved, patch: { maplibre_style: saved } };
|
||||
}
|
||||
if (provider === 'mapbox-gl') {
|
||||
const saved = normalizeGlSelection(provider, style);
|
||||
return { shown: saved, patch: { mapbox_style: saved } };
|
||||
}
|
||||
const kept = isMapLibreKeyword(style) ? styleForProvider('leaflet', storedMapboxStyle) : style;
|
||||
return { shown: style, patch: { mapbox_style: kept } };
|
||||
}
|
||||
|
||||
interface MapFormInput {
|
||||
provider: MapProvider;
|
||||
style: string;
|
||||
template: string;
|
||||
cartoKey: string;
|
||||
managed: boolean;
|
||||
/** MapLibre cannot draw the template here (glBasemap.ts), so the trip map draws it with Leaflet. */
|
||||
needsLeaflet: boolean;
|
||||
}
|
||||
|
||||
/** Which parts of the map form show, from what it holds. Both shells render from this. */
|
||||
export function mapFormView({ provider, style, template, cartoKey, managed, needsLeaflet }: MapFormInput) {
|
||||
const templateMode = provider === 'maplibre-gl' && style.trim() === MAPLIBRE_FROM_TEMPLATE;
|
||||
// Only CARTO burns a watermark into keyless tiles, so the key and its nudge are scoped to its hosts.
|
||||
const cartoTemplate = template.includes('basemaps.cartocdn.com');
|
||||
return {
|
||||
/** MapLibre draws the template: the template field (always there on Leaflet) shows under the style picker. */
|
||||
templateMode,
|
||||
/** The trip map draws the template with the classic 2D map (GCJ-02, a host outside the policy). */
|
||||
templateNeedsClassic: templateMode && needsLeaflet,
|
||||
/** A managed install brings its own key; MapLibre asks for one only for a CARTO template. */
|
||||
showCartoKey: !managed && (provider === 'leaflet' || (templateMode && cartoTemplate)),
|
||||
cartoNeedsKey: cartoTemplate && !cartoKey.trim(),
|
||||
/** A keyword is chosen from the picker; there is no URL to show or type. */
|
||||
showStyleUrl: !isMapLibreKeyword(style),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* The style presets a picker offers for a provider. MapLibre adds the two
|
||||
* keywords after the OpenFreeMap styles: the style that follows the colour
|
||||
* scheme and the user's own map template. Both are named through `t`, the
|
||||
* OpenFreeMap and Mapbox names are their own.
|
||||
*/
|
||||
export function glStylePresetsFor(provider: GlMapProvider, t: (key: string) => string): GlStylePreset[] {
|
||||
if (provider !== 'maplibre-gl') return getStylePresets(provider);
|
||||
return [
|
||||
...getStylePresets(provider),
|
||||
{ name: t('settings.mapStyleFollowTheme'), url: MAPLIBRE_OFM_AUTO, tags: ['OpenFreeMap'] },
|
||||
{ name: t('settings.mapStyleTemplate'), url: MAPLIBRE_FROM_TEMPLATE },
|
||||
];
|
||||
}
|
||||
|
||||
// Each GL provider has its own style slot, so toggling providers never clobbers the
|
||||
// other one's style. Leaflet/Mapbox use mapbox_style; MapLibre uses maplibre_style.
|
||||
export function slotStyle(
|
||||
|
||||
@@ -134,4 +134,119 @@ describe('useMapSettingsForm', () => {
|
||||
const { result } = renderHook(() => useMapSettingsForm());
|
||||
expect(result.current.mapboxPreview).toEqual({ enable3d: false, quality: false, hidePois: false });
|
||||
});
|
||||
|
||||
describe("MapLibre drawing the user's template (trek:template)", () => {
|
||||
const OSM_DE = 'https://tile.openstreetmap.de/{z}/{x}/{y}.png';
|
||||
|
||||
it('FE-COMP-MAPFORM-011: an unchanged save keeps the keyword and the template, on both shells', async () => {
|
||||
for (const mirrorStyleBeforeSave of [false, true]) {
|
||||
updateSettings.mockClear();
|
||||
seed({ map_provider: 'maplibre-gl', maplibre_style: 'trek:template', map_tile_url: OSM_DE });
|
||||
const { result } = renderHook(() => useMapSettingsForm({ mirrorStyleBeforeSave }));
|
||||
expect(result.current.mapboxStyle).toBe('trek:template');
|
||||
await act(() => result.current.save());
|
||||
expect(updateSettings).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
map_provider: 'maplibre-gl',
|
||||
maplibre_style: 'trek:template',
|
||||
map_tile_url: OSM_DE,
|
||||
})
|
||||
);
|
||||
expect(result.current.mapboxStyle).toBe('trek:template');
|
||||
}
|
||||
});
|
||||
|
||||
it('FE-COMP-MAPFORM-012: picking a raster template writes both keys', async () => {
|
||||
seed({ map_provider: 'maplibre-gl' });
|
||||
const { result } = renderHook(() => useMapSettingsForm());
|
||||
act(() => result.current.setMapboxStyle('trek:template'));
|
||||
act(() => result.current.setMapTileUrl(OSM_DE));
|
||||
expect(result.current.templateMode).toBe(true);
|
||||
await act(() => result.current.save());
|
||||
expect(updateSettings).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ maplibre_style: 'trek:template', map_tile_url: OSM_DE })
|
||||
);
|
||||
});
|
||||
|
||||
it('FE-COMP-MAPFORM-013: the template keyword with no template saves Liberty', async () => {
|
||||
seed({ map_provider: 'maplibre-gl' });
|
||||
const { result } = renderHook(() => useMapSettingsForm());
|
||||
act(() => result.current.setMapboxStyle('trek:template'));
|
||||
await act(() => result.current.save());
|
||||
expect(updateSettings).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ maplibre_style: OPENFREEMAP_DEFAULT_STYLE })
|
||||
);
|
||||
});
|
||||
|
||||
it('FE-COMP-MAPFORM-014: from Leaflet with a template and an empty MapLibre slot, the template is preselected', () => {
|
||||
seed({ map_provider: 'leaflet', map_tile_url: OSM_DE, maplibre_style: '' });
|
||||
const { result } = renderHook(() => useMapSettingsForm());
|
||||
act(() => result.current.changeProvider('maplibre-gl'));
|
||||
expect(result.current.mapboxStyle).toBe('trek:template');
|
||||
expect(result.current.templateMode).toBe(true);
|
||||
// Shown, not saved: nothing leaves the form before "Save".
|
||||
expect(updateSettings).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('FE-COMP-MAPFORM-015: the keyword survives a switch to Leaflet and to Mapbox and back', async () => {
|
||||
seed({
|
||||
map_provider: 'maplibre-gl',
|
||||
maplibre_style: 'trek:template',
|
||||
map_tile_url: OSM_DE,
|
||||
mapbox_style: 'mapbox://styles/me/x',
|
||||
});
|
||||
const { result } = renderHook(() => useMapSettingsForm());
|
||||
act(() => result.current.changeProvider('leaflet'));
|
||||
act(() => result.current.changeProvider('maplibre-gl'));
|
||||
expect(result.current.mapboxStyle).toBe('trek:template');
|
||||
act(() => result.current.changeProvider('mapbox-gl'));
|
||||
expect(result.current.mapboxStyle).toBe(MAPBOX_DEFAULT_STYLE);
|
||||
act(() => result.current.changeProvider('maplibre-gl'));
|
||||
expect(result.current.mapboxStyle).toBe('trek:template');
|
||||
});
|
||||
|
||||
it('FE-COMP-MAPFORM-016: saving Leaflet with the keyword still in the field leaves the Mapbox slot as it was', async () => {
|
||||
seed({
|
||||
map_provider: 'maplibre-gl',
|
||||
maplibre_style: 'trek:template',
|
||||
map_tile_url: OSM_DE,
|
||||
mapbox_style: 'mapbox://styles/me/x',
|
||||
});
|
||||
const { result } = renderHook(() => useMapSettingsForm());
|
||||
act(() => result.current.changeProvider('leaflet'));
|
||||
await act(() => result.current.save());
|
||||
expect(updateSettings).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ map_provider: 'leaflet', mapbox_style: 'mapbox://styles/me/x' })
|
||||
);
|
||||
});
|
||||
|
||||
it('FE-COMP-MAPFORM-017: the template field, the CARTO key and the URL box follow the keyword', () => {
|
||||
seed({ map_provider: 'maplibre-gl', maplibre_style: 'trek:template', map_tile_url: MAP_PRESETS[4].url });
|
||||
const { result } = renderHook(() => useMapSettingsForm());
|
||||
expect(result.current.showStyleUrl).toBe(false);
|
||||
expect(result.current.showCartoKey).toBe(true);
|
||||
act(() => result.current.setMapTileUrl(OSM_DE));
|
||||
expect(result.current.showCartoKey).toBe(false);
|
||||
act(() => result.current.setMapboxStyle(OPENFREEMAP_DEFAULT_STYLE));
|
||||
expect(result.current.templateMode).toBe(false);
|
||||
expect(result.current.showStyleUrl).toBe(true);
|
||||
});
|
||||
|
||||
it('FE-COMP-MAPFORM-018: a template only Leaflet can draw says so and previews with Leaflet', () => {
|
||||
seed({ map_provider: 'maplibre-gl', maplibre_style: 'trek:template', map_tile_url: AMAP_ROAD });
|
||||
const { result } = renderHook(() => useMapSettingsForm());
|
||||
expect(result.current.templateNeedsClassic).toBe(true);
|
||||
expect(result.current.glPreview).toBe(false);
|
||||
});
|
||||
|
||||
it('FE-COMP-MAPFORM-019: the preview draws what MapLibre makes of the keyword', () => {
|
||||
seed({ map_provider: 'maplibre-gl', maplibre_style: 'trek:ofm-auto' });
|
||||
const { result } = renderHook(() => useMapSettingsForm());
|
||||
expect(result.current.previewStyle).toBe(OPENFREEMAP_DEFAULT_STYLE);
|
||||
expect(result.current.templateNeedsClassic).toBe(false);
|
||||
expect(result.current.stylePresets.map((p) => p.url)).toEqual(
|
||||
expect.arrayContaining(['trek:ofm-auto', 'trek:template'])
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,20 +1,27 @@
|
||||
import { useEffect, useMemo, useState } from 'react';
|
||||
|
||||
import { OFM_POSITRON } from '../../constants/mapDefaults';
|
||||
import { useIsDark } from '../../hooks/useIsDark';
|
||||
import { useTranslation } from '../../i18n';
|
||||
import { useAuthStore } from '../../store/authStore';
|
||||
import { useSettingsStore } from '../../store/settingsStore';
|
||||
import type { Place } from '../../types';
|
||||
import { normalizeStyleForProvider } from '../Map/glProviders';
|
||||
import { resolveTileUrl } from '../../utils/tileUrl';
|
||||
import { mapLibreBasemap } from '../Map/glBasemap';
|
||||
import { useWebGL2 } from '../Map/glSupport';
|
||||
import { useToast } from '../shared/Toast';
|
||||
import {
|
||||
type MapProvider,
|
||||
glStylePresetsFor,
|
||||
mapboxOffered,
|
||||
mapFormView,
|
||||
mapPresetsFor,
|
||||
normalizeProvider,
|
||||
previewPlace,
|
||||
slotStyle,
|
||||
styleForProvider,
|
||||
styleOnProviderChange,
|
||||
styleToSave,
|
||||
} from './mapSettingsModel';
|
||||
|
||||
export interface MapSettingsFormOptions {
|
||||
@@ -31,6 +38,10 @@ export interface MapSettingsFormOptions {
|
||||
* and CARTO key, the Mapbox token, the GL style, the 3D and quality toggles and the switch
|
||||
* for the basemap's own points of interest,
|
||||
* seeded from the settings store and saved back in one patch.
|
||||
*
|
||||
* On MapLibre the style can also be a keyword: the style that follows the colour
|
||||
* scheme, or the user's map template, which brings the template field (and the
|
||||
* CARTO key for a CARTO template) to MapLibre too.
|
||||
*/
|
||||
export function useMapSettingsForm({ mirrorStyleBeforeSave = false }: MapSettingsFormOptions = {}) {
|
||||
const { settings, updateSettings } = useSettingsStore();
|
||||
@@ -42,6 +53,7 @@ export function useMapSettingsForm({ mirrorStyleBeforeSave = false }: MapSetting
|
||||
const [mapTileUrl, setMapTileUrl] = useState<string>(settings.map_tile_url || '');
|
||||
const managed = useAuthStore((s) => s.managed);
|
||||
const hasAmapKey = useAuthStore((s) => s.hasAmapKey);
|
||||
const extraHosts = useAuthStore((s) => s.mapExtraConnectHosts);
|
||||
const [mapboxToken, setMapboxToken] = useState<string>(settings.mapbox_access_token || '');
|
||||
const [cartoKey, setCartoKey] = useState<string>(settings.carto_api_key || '');
|
||||
const [mapboxStyle, setMapboxStyle] = useState<string>(
|
||||
@@ -67,28 +79,48 @@ export function useMapSettingsForm({ mirrorStyleBeforeSave = false }: MapSetting
|
||||
// Both GL engines need WebGL2. Without it the preview shows the Leaflet map the
|
||||
// trip planner falls back to, and both shells say why.
|
||||
const webgl2 = useWebGL2();
|
||||
const glPreview = provider !== 'leaflet' && webgl2;
|
||||
const dark = useIsDark();
|
||||
// What MapLibre makes of a keyword, from the template as it is being edited,
|
||||
// resolved the way the trip map resolves it, so the preview shows that map.
|
||||
const keyword = useMemo(
|
||||
() =>
|
||||
mapLibreBasemap(
|
||||
provider === 'maplibre-gl' ? mapboxStyle : '',
|
||||
resolveTileUrl(mapTileUrl, OFM_POSITRON, cartoKey),
|
||||
dark,
|
||||
extraHosts
|
||||
),
|
||||
[provider, mapboxStyle, mapTileUrl, cartoKey, dark, extraHosts]
|
||||
);
|
||||
const view = mapFormView({
|
||||
provider,
|
||||
style: mapboxStyle,
|
||||
template: mapTileUrl,
|
||||
cartoKey,
|
||||
managed,
|
||||
needsLeaflet: keyword.needsLeaflet,
|
||||
});
|
||||
const glPreview = provider !== 'leaflet' && webgl2 && !keyword.needsLeaflet;
|
||||
const webgl2Missing = provider !== 'leaflet' && !webgl2;
|
||||
|
||||
const save = async (): Promise<void> => {
|
||||
setSaving(true);
|
||||
try {
|
||||
const glStyle = provider === 'leaflet' ? mapboxStyle : normalizeStyleForProvider(provider, mapboxStyle);
|
||||
if (mirrorStyleBeforeSave) setMapboxStyle(glStyle);
|
||||
// Save into the active provider's own slot so the other provider's style survives.
|
||||
const stylePatch = provider === 'maplibre-gl' ? { maplibre_style: glStyle } : { mapbox_style: glStyle };
|
||||
// Each provider saves into its own slot, so the other provider's style survives.
|
||||
const { shown, patch } = styleToSave(provider, mapboxStyle, mapTileUrl, settings.mapbox_style);
|
||||
if (mirrorStyleBeforeSave) setMapboxStyle(shown);
|
||||
await updateSettings({
|
||||
map_provider: provider,
|
||||
map_tile_url: mapTileUrl,
|
||||
mapbox_access_token: mapboxToken,
|
||||
carto_api_key: cartoKey,
|
||||
...stylePatch,
|
||||
...patch,
|
||||
mapbox_3d_enabled: mapbox3d,
|
||||
mapbox_quality_mode: mapboxQuality,
|
||||
map_hide_basemap_pois: hideBasemapPois,
|
||||
});
|
||||
// Only mirror the normalized style into the form once it is actually persisted.
|
||||
if (!mirrorStyleBeforeSave) setMapboxStyle(glStyle);
|
||||
if (!mirrorStyleBeforeSave) setMapboxStyle(shown);
|
||||
toast.success(t('settings.toast.mapSaved'));
|
||||
} catch (err: unknown) {
|
||||
toast.error(err instanceof Error ? err.message : t('common.error'));
|
||||
@@ -99,28 +131,29 @@ export function useMapSettingsForm({ mirrorStyleBeforeSave = false }: MapSetting
|
||||
|
||||
const changeProvider = (nextProvider: MapProvider) => {
|
||||
setProvider(nextProvider);
|
||||
if (nextProvider !== 'leaflet') setMapboxStyle(styleForProvider(nextProvider, mapboxStyle));
|
||||
const context = { savedMaplibreStyle: settings.maplibre_style, template: mapTileUrl };
|
||||
setMapboxStyle(styleOnProviderChange(provider, nextProvider, mapboxStyle, context));
|
||||
};
|
||||
|
||||
// Only CARTO burns a watermark into keyless tiles, so the nudge is scoped to its hosts.
|
||||
const cartoNeedsKey = mapTileUrl.includes('basemaps.cartocdn.com') && !cartoKey.trim();
|
||||
|
||||
return {
|
||||
managed,
|
||||
presets: mapPresetsFor(managed, hasAmapKey),
|
||||
stylePresets: provider === 'leaflet' ? [] : glStylePresetsFor(provider, t),
|
||||
canMapbox: mapboxOffered(managed, settings.mapbox_access_token),
|
||||
saving,
|
||||
provider,
|
||||
changeProvider,
|
||||
mapTileUrl,
|
||||
setMapTileUrl,
|
||||
...view,
|
||||
mapboxToken,
|
||||
setMapboxToken,
|
||||
cartoKey,
|
||||
setCartoKey,
|
||||
cartoNeedsKey,
|
||||
mapboxStyle,
|
||||
setMapboxStyle,
|
||||
/** What the GL preview draws: the style, or what MapLibre makes of a keyword. */
|
||||
previewStyle: keyword.style ?? mapboxStyle,
|
||||
mapbox3d,
|
||||
setMapbox3d,
|
||||
mapboxQuality,
|
||||
|
||||
Reference in New Issue
Block a user