mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-06 18:54:17 +00:00
dcbabe998b
Share the service-worker raster match with prefetch and the offline renderer. Keep an allowed custom host online and switch to the stored basemap offline when its tiles are outside the readable cache.
51 lines
2.4 KiB
TypeScript
51 lines
2.4 KiB
TypeScript
import { OFM_DARK, OFM_POSITRON } from '../../constants/mapDefaults';
|
|
import { isNativeIos } from '../../native/platform';
|
|
import { hasGlRasterCache } from '../../sync/glTileCache';
|
|
import { isOfflineForbiddenTemplate, isVectorStyle } from '../../utils/tileUrl';
|
|
import { isGlRasterStyle, type GlStyle } from './glBasemap';
|
|
import { normalizeStyleForProvider, type GlMapProvider } from './glProviders';
|
|
|
|
/**
|
|
* The basemap a map falls back to offline, so that whatever the user picked
|
|
* online (Mapbox, a raster template, another OpenFreeMap style) they never
|
|
* face a grey map once the connection goes.
|
|
*
|
|
* The two OpenFreeMap styles below are the ones "prepare for offline" stores
|
|
* for every trip, whatever the configured provider (sync/tilePrefetcher.ts):
|
|
* both share one planet tile set, so the dark one only adds its style, sprite
|
|
* and glyphs. A fallback has to be exactly a style that was stored, or its
|
|
* sprite and labels are missing offline.
|
|
*/
|
|
const OFFLINE_STYLES: readonly string[] = [OFM_POSITRON, OFM_DARK];
|
|
|
|
/** The stored style that matches the colour scheme. */
|
|
export function offlineStyleFor(dark: boolean): string {
|
|
return dark ? OFM_DARK : OFM_POSITRON;
|
|
}
|
|
|
|
/**
|
|
* Whether a GL map can be drawn offline as configured. Only MapLibre can, on
|
|
* one of the stored styles (another OpenFreeMap style lacks its sprite and
|
|
* glyphs offline) or on a raster template the worker caches with CORS. Mapbox
|
|
* has nothing stored to draw from. `style` is the map's resolved style.
|
|
*/
|
|
export function glWorksOffline(provider: GlMapProvider, style: GlStyle | null | undefined): boolean {
|
|
if (provider !== 'maplibre-gl') return false;
|
|
if (isGlRasterStyle(style)) {
|
|
const template = style.metadata['trek:template'];
|
|
return leafletWorksOffline(template) && hasGlRasterCache(template);
|
|
}
|
|
return OFFLINE_STYLES.includes(normalizeStyleForProvider('maplibre-gl', style));
|
|
}
|
|
|
|
/**
|
|
* Whether the Leaflet map's basemap can be drawn offline as configured. A
|
|
* stored vector style can. A raster template can where the service worker
|
|
* serves the prefetched tiles, which the iOS app's WebView does not run for a
|
|
* self-hosted server, and never for a host whose tiles may not be stored.
|
|
*/
|
|
export function leafletWorksOffline(tileUrl: string, nativeIos: boolean = isNativeIos()): boolean {
|
|
if (isVectorStyle(tileUrl)) return OFFLINE_STYLES.includes(tileUrl);
|
|
return !nativeIos && !isOfflineForbiddenTemplate(tileUrl);
|
|
}
|