|
| 1 | +import { |
| 2 | + OvertureMapsControl, |
| 3 | + type OvertureMapsControlOptions, |
| 4 | + type OvertureMapsState, |
| 5 | +} from "maplibre-gl-overture-maps"; |
| 6 | +import type { |
| 7 | + GeoLibreAppAPI, |
| 8 | + GeoLibreMapControlPosition, |
| 9 | + GeoLibrePlugin, |
| 10 | +} from "../types"; |
| 11 | + |
| 12 | +let overturePosition: GeoLibreMapControlPosition = "top-left"; |
| 13 | + |
| 14 | +const OVERTURE_OPTIONS = { |
| 15 | + collapsed: false, |
| 16 | + title: "Overture Maps", |
| 17 | + panelWidth: 340, |
| 18 | + className: "geolibre-overture-control", |
| 19 | +} satisfies Omit<OvertureMapsControlOptions, "position">; |
| 20 | + |
| 21 | +let overtureControl: OvertureMapsControl | null = null; |
| 22 | +// Holds the panel state while the control is detached so re-activating or |
| 23 | +// repositioning it restores the user's release, visibility, and opacity. |
| 24 | +let pendingState: Partial<OvertureMapsState> | null = null; |
| 25 | + |
| 26 | +function getOvertureControlOptions(): OvertureMapsControlOptions { |
| 27 | + return { |
| 28 | + ...OVERTURE_OPTIONS, |
| 29 | + ...(pendingState?.collapsed != null |
| 30 | + ? { collapsed: pendingState.collapsed } |
| 31 | + : {}), |
| 32 | + ...(pendingState?.panelWidth != null |
| 33 | + ? { panelWidth: pendingState.panelWidth } |
| 34 | + : {}), |
| 35 | + ...(pendingState?.release ? { release: pendingState.release } : {}), |
| 36 | + position: overturePosition, |
| 37 | + }; |
| 38 | +} |
| 39 | + |
| 40 | +function createOvertureControl(): OvertureMapsControl { |
| 41 | + const control = new OvertureMapsControl(getOvertureControlOptions()); |
| 42 | + if (pendingState) { |
| 43 | + control.setState(pendingState); |
| 44 | + } |
| 45 | + return control; |
| 46 | +} |
| 47 | + |
| 48 | +function isOvertureMapsState( |
| 49 | + value: unknown, |
| 50 | +): value is Partial<OvertureMapsState> { |
| 51 | + return Boolean(value) && typeof value === "object" && !Array.isArray(value); |
| 52 | +} |
| 53 | + |
| 54 | +export const maplibreOvertureMapsPlugin: GeoLibrePlugin = { |
| 55 | + id: "maplibre-gl-overture-maps", |
| 56 | + name: "Overture Maps", |
| 57 | + version: "0.2.0", |
| 58 | + activate: (app: GeoLibreAppAPI) => { |
| 59 | + if (!overtureControl) { |
| 60 | + overtureControl = createOvertureControl(); |
| 61 | + } |
| 62 | + const added = app.addMapControl(overtureControl, overturePosition); |
| 63 | + if (!added) { |
| 64 | + overtureControl = null; |
| 65 | + return false; |
| 66 | + } |
| 67 | + // Open the panel on activation. Deferring past the current click avoids |
| 68 | + // the menu click that activated the plugin being treated as a |
| 69 | + // click-outside that immediately re-collapses the panel. |
| 70 | + setTimeout(() => overtureControl?.expand(), 0); |
| 71 | + }, |
| 72 | + deactivate: (app: GeoLibreAppAPI) => { |
| 73 | + if (!overtureControl) return; |
| 74 | + pendingState = overtureControl.getState(); |
| 75 | + app.removeMapControl(overtureControl); |
| 76 | + overtureControl = null; |
| 77 | + }, |
| 78 | + getMapControlPosition: () => overturePosition, |
| 79 | + setMapControlPosition: ( |
| 80 | + app: GeoLibreAppAPI, |
| 81 | + position: GeoLibreMapControlPosition, |
| 82 | + ) => { |
| 83 | + overturePosition = position; |
| 84 | + if (!overtureControl) return; |
| 85 | + app.removeMapControl(overtureControl); |
| 86 | + const added = app.addMapControl(overtureControl, overturePosition); |
| 87 | + if (!added) { |
| 88 | + pendingState = overtureControl.getState(); |
| 89 | + overtureControl = null; |
| 90 | + return false; |
| 91 | + } |
| 92 | + setTimeout(() => overtureControl?.expand(), 0); |
| 93 | + }, |
| 94 | + getProjectState: () => |
| 95 | + overtureControl?.getState() ?? pendingState ?? undefined, |
| 96 | + applyProjectState: (_app: GeoLibreAppAPI, state: unknown) => { |
| 97 | + if (!isOvertureMapsState(state)) return false; |
| 98 | + pendingState = state; |
| 99 | + overtureControl?.setState(state); |
| 100 | + }, |
| 101 | +}; |
0 commit comments