@mapcairn/map 0.0.0-stage → 0.1.0-rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +661 -0
- package/README.md +167 -2
- package/dist/chrome/Attribution/Attribution.js +304 -0
- package/dist/chrome/Attribution/Attribution.js.map +1 -0
- package/dist/chrome/Attribution/Attribution.styles.js +10 -0
- package/dist/chrome/Attribution/Attribution.styles.js.map +1 -0
- package/dist/chrome/Attribution/AttributionSkeleton.js +29 -0
- package/dist/chrome/Attribution/AttributionSkeleton.js.map +1 -0
- package/dist/chrome/ChromeSkeleton/ChromeSkeleton.js +83 -0
- package/dist/chrome/ChromeSkeleton/ChromeSkeleton.js.map +1 -0
- package/dist/chrome/EndStack/EndStack.js +157 -0
- package/dist/chrome/EndStack/EndStack.js.map +1 -0
- package/dist/chrome/EndStack/EndStack.styles.js +12 -0
- package/dist/chrome/EndStack/EndStack.styles.js.map +1 -0
- package/dist/chrome/EndStack/EndStackSkeleton.js +78 -0
- package/dist/chrome/EndStack/EndStackSkeleton.js.map +1 -0
- package/dist/chrome/EndStack/endStackHeight.js +22 -0
- package/dist/chrome/EndStack/endStackHeight.js.map +1 -0
- package/dist/chrome/MapChrome/MapChrome.js +488 -0
- package/dist/chrome/MapChrome/MapChrome.js.map +1 -0
- package/dist/chrome/MapChrome/MapChrome.styles.js +15 -0
- package/dist/chrome/MapChrome/MapChrome.styles.js.map +1 -0
- package/dist/chrome/MapChrome/chromeSurface.js +18 -0
- package/dist/chrome/MapChrome/chromeSurface.js.map +1 -0
- package/dist/chrome/MapChrome/chromeVars.js +71 -0
- package/dist/chrome/MapChrome/chromeVars.js.map +1 -0
- package/dist/chrome/MapChrome/mapKeyPlacement.js +41 -0
- package/dist/chrome/MapChrome/mapKeyPlacement.js.map +1 -0
- package/dist/chrome/MapChrome/sharesWidth.js +13 -0
- package/dist/chrome/MapChrome/sharesWidth.js.map +1 -0
- package/dist/chrome/MapReadout/MapReadout.js +199 -0
- package/dist/chrome/MapReadout/MapReadout.js.map +1 -0
- package/dist/chrome/MapReadout/MapReadout.styles.js +14 -0
- package/dist/chrome/MapReadout/MapReadout.styles.js.map +1 -0
- package/dist/chrome/Sheet/Sheet.js +412 -0
- package/dist/chrome/Sheet/Sheet.js.map +1 -0
- package/dist/chrome/Sheet/Sheet.styles.js +14 -0
- package/dist/chrome/Sheet/Sheet.styles.js.map +1 -0
- package/dist/chrome/focusRegistry.js +76 -0
- package/dist/chrome/focusRegistry.js.map +1 -0
- package/dist/chrome/layoutContext.js +30 -0
- package/dist/chrome/layoutContext.js.map +1 -0
- package/dist/chrome/useScrollEdges.js +53 -0
- package/dist/chrome/useScrollEdges.js.map +1 -0
- package/dist/host/MapContext.js +16 -0
- package/dist/host/MapContext.js.map +1 -0
- package/dist/host/MapView.js +189 -0
- package/dist/host/MapView.js.map +1 -0
- package/dist/host/MapView.styles.js +16 -0
- package/dist/host/MapView.styles.js.map +1 -0
- package/dist/host/overlays/AccuracyCircle.js +36 -0
- package/dist/host/overlays/AccuracyCircle.js.map +1 -0
- package/dist/host/overlays/CentreCrosshair.js +40 -0
- package/dist/host/overlays/CentreCrosshair.js.map +1 -0
- package/dist/host/overlays/DrawOverlay.js +221 -0
- package/dist/host/overlays/DrawOverlay.js.map +1 -0
- package/dist/host/overlays/MapOverlayLayer.js +27 -0
- package/dist/host/overlays/MapOverlayLayer.js.map +1 -0
- package/dist/host/overlays/MeasureOverlay.js +87 -0
- package/dist/host/overlays/MeasureOverlay.js.map +1 -0
- package/dist/host/overlays/PinMarker.js +40 -0
- package/dist/host/overlays/PinMarker.js.map +1 -0
- package/dist/host/overlays/geometry.js +41 -0
- package/dist/host/overlays/geometry.js.map +1 -0
- package/dist/host/overlays/overlays.styles.js +27 -0
- package/dist/host/overlays/overlays.styles.js.map +1 -0
- package/dist/host/pointerTargets.js +22 -0
- package/dist/host/pointerTargets.js.map +1 -0
- package/dist/host/useLongPress.js +102 -0
- package/dist/host/useLongPress.js.map +1 -0
- package/dist/host/useMapEngine.js +352 -0
- package/dist/host/useMapEngine.js.map +1 -0
- package/dist/host/usePointerPosition.js +60 -0
- package/dist/host/usePointerPosition.js.map +1 -0
- package/dist/index.d.ts +3845 -0
- package/dist/index.js +89 -0
- package/dist/layers/CatalogueStatus.js +138 -0
- package/dist/layers/CatalogueStatus.js.map +1 -0
- package/dist/layers/DateTimeControl.js +101 -0
- package/dist/layers/DateTimeControl.js.map +1 -0
- package/dist/layers/FeatureCard.js +147 -0
- package/dist/layers/FeatureCard.js.map +1 -0
- package/dist/layers/FeatureChoices.js +101 -0
- package/dist/layers/FeatureChoices.js.map +1 -0
- package/dist/layers/FeatureGlyph.js +56 -0
- package/dist/layers/FeatureGlyph.js.map +1 -0
- package/dist/layers/IdentifyStatus.js +36 -0
- package/dist/layers/IdentifyStatus.js.map +1 -0
- package/dist/layers/LayerItem.js +321 -0
- package/dist/layers/LayerItem.js.map +1 -0
- package/dist/layers/LayerKey.js +36 -0
- package/dist/layers/LayerKey.js.map +1 -0
- package/dist/layers/Layers.styles.js +50 -0
- package/dist/layers/Layers.styles.js.map +1 -0
- package/dist/layers/LayersSection.js +267 -0
- package/dist/layers/LayersSection.js.map +1 -0
- package/dist/layers/MapDataLayers.js +261 -0
- package/dist/layers/MapDataLayers.js.map +1 -0
- package/dist/layers/MapKey.js +131 -0
- package/dist/layers/MapKey.js.map +1 -0
- package/dist/layers/MapKey.styles.js +16 -0
- package/dist/layers/MapKey.styles.js.map +1 -0
- package/dist/layers/MarkerGlyph.js +84 -0
- package/dist/layers/MarkerGlyph.js.map +1 -0
- package/dist/layers/SelectionProvider.js +27 -0
- package/dist/layers/SelectionProvider.js.map +1 -0
- package/dist/layers/ShapeGlyph.js +122 -0
- package/dist/layers/ShapeGlyph.js.map +1 -0
- package/dist/layers/TurnOffActions.js +147 -0
- package/dist/layers/TurnOffActions.js.map +1 -0
- package/dist/layers/answersTap.js +16 -0
- package/dist/layers/answersTap.js.map +1 -0
- package/dist/layers/catalogueNotice.js +65 -0
- package/dist/layers/catalogueNotice.js.map +1 -0
- package/dist/layers/catalogueReconcile.js +55 -0
- package/dist/layers/catalogueReconcile.js.map +1 -0
- package/dist/layers/catalogueView.js +32 -0
- package/dist/layers/catalogueView.js.map +1 -0
- package/dist/layers/featureTitle.js +14 -0
- package/dist/layers/featureTitle.js.map +1 -0
- package/dist/layers/identifyFeedback.js +30 -0
- package/dist/layers/identifyFeedback.js.map +1 -0
- package/dist/layers/identifyRun.js +148 -0
- package/dist/layers/identifyRun.js.map +1 -0
- package/dist/layers/layerStatus.js +13 -0
- package/dist/layers/layerStatus.js.map +1 -0
- package/dist/layers/loadOutcome.js +107 -0
- package/dist/layers/loadOutcome.js.map +1 -0
- package/dist/layers/loadReport.js +33 -0
- package/dist/layers/loadReport.js.map +1 -0
- package/dist/layers/loadRequests.js +73 -0
- package/dist/layers/loadRequests.js.map +1 -0
- package/dist/layers/mapFeatures.js +307 -0
- package/dist/layers/mapFeatures.js.map +1 -0
- package/dist/layers/mapKeyBlocks.js +24 -0
- package/dist/layers/mapKeyBlocks.js.map +1 -0
- package/dist/layers/reveal.js +119 -0
- package/dist/layers/reveal.js.map +1 -0
- package/dist/layers/selectionMessages.js +53 -0
- package/dist/layers/selectionMessages.js.map +1 -0
- package/dist/layers/selectionResolve.js +111 -0
- package/dist/layers/selectionResolve.js.map +1 -0
- package/dist/layers/useDataLayers.js +940 -0
- package/dist/layers/useDataLayers.js.map +1 -0
- package/dist/layers/useFeatureSelection.js +179 -0
- package/dist/layers/useFeatureSelection.js.map +1 -0
- package/dist/layers/useScrolls.js +37 -0
- package/dist/layers/useScrolls.js.map +1 -0
- package/dist/panels/BasemapPicker/BasemapPicker.js +169 -0
- package/dist/panels/BasemapPicker/BasemapPicker.js.map +1 -0
- package/dist/panels/BasemapPicker/BasemapPicker.styles.js +42 -0
- package/dist/panels/BasemapPicker/BasemapPicker.styles.js.map +1 -0
- package/dist/panels/ExportSection/ExportSection.js +91 -0
- package/dist/panels/ExportSection/ExportSection.js.map +1 -0
- package/dist/panels/HelpSection/HelpSection.js +163 -0
- package/dist/panels/HelpSection/HelpSection.js.map +1 -0
- package/dist/panels/HelpSection/HelpSection.styles.js +9 -0
- package/dist/panels/HelpSection/HelpSection.styles.js.map +1 -0
- package/dist/panels/PinSection/PinSection.js +39 -0
- package/dist/panels/PinSection/PinSection.js.map +1 -0
- package/dist/panels/Section/Section.js +43 -0
- package/dist/panels/Section/Section.js.map +1 -0
- package/dist/panels/Section/Section.styles.js +13 -0
- package/dist/panels/Section/Section.styles.js.map +1 -0
- package/dist/panels/SettingsSection/LanguageField.js +126 -0
- package/dist/panels/SettingsSection/LanguageField.js.map +1 -0
- package/dist/panels/SettingsSection/SettingsSection.js +175 -0
- package/dist/panels/SettingsSection/SettingsSection.js.map +1 -0
- package/dist/panels/SettingsSection/SettingsSection.styles.js +9 -0
- package/dist/panels/SettingsSection/SettingsSection.styles.js.map +1 -0
- package/dist/panels/ShapesSection/ShapesSection.js +229 -0
- package/dist/panels/ShapesSection/ShapesSection.js.map +1 -0
- package/dist/panels/ShapesSection/ShapesSection.styles.js +7 -0
- package/dist/panels/ShapesSection/ShapesSection.styles.js.map +1 -0
- package/dist/panels/SheetContent/SheetContent.js +92 -0
- package/dist/panels/SheetContent/SheetContent.js.map +1 -0
- package/dist/panels/SheetContent/SheetContent.styles.js +6 -0
- package/dist/panels/SheetContent/SheetContent.styles.js.map +1 -0
- package/dist/services/ServicesProvider.js +42 -0
- package/dist/services/ServicesProvider.js.map +1 -0
- package/dist/services/activeMessages.js +140 -0
- package/dist/services/activeMessages.js.map +1 -0
- package/dist/services/basemapAvailability.js +78 -0
- package/dist/services/basemapAvailability.js.map +1 -0
- package/dist/services/englishMessages.js +10 -0
- package/dist/services/englishMessages.js.map +1 -0
- package/dist/services/hooks.js +214 -0
- package/dist/services/hooks.js.map +1 -0
- package/dist/styles.css +2866 -0
- package/dist/styles.css.d.ts +1 -0
- package/dist/tools/Directions/Directions.styles.js +25 -0
- package/dist/tools/Directions/Directions.styles.js.map +1 -0
- package/dist/tools/Directions/DirectionsOverlay.js +46 -0
- package/dist/tools/Directions/DirectionsOverlay.js.map +1 -0
- package/dist/tools/Directions/DirectionsPanel.js +428 -0
- package/dist/tools/Directions/DirectionsPanel.js.map +1 -0
- package/dist/tools/Directions/MapDirections.js +84 -0
- package/dist/tools/Directions/MapDirections.js.map +1 -0
- package/dist/tools/Directions/useDirections.js +226 -0
- package/dist/tools/Directions/useDirections.js.map +1 -0
- package/dist/tools/Directions/useFreeCentre.js +30 -0
- package/dist/tools/Directions/useFreeCentre.js.map +1 -0
- package/dist/tools/DrawToolbar/DrawMapBindings.js +34 -0
- package/dist/tools/DrawToolbar/DrawMapBindings.js.map +1 -0
- package/dist/tools/DrawToolbar/DrawToolbar.js +296 -0
- package/dist/tools/DrawToolbar/DrawToolbar.js.map +1 -0
- package/dist/tools/DrawToolbar/DrawToolbar.styles.js +21 -0
- package/dist/tools/DrawToolbar/DrawToolbar.styles.js.map +1 -0
- package/dist/tools/DrawToolbar/imageDrawing.js +24 -0
- package/dist/tools/DrawToolbar/imageDrawing.js.map +1 -0
- package/dist/tools/DrawToolbar/useDraw.js +543 -0
- package/dist/tools/DrawToolbar/useDraw.js.map +1 -0
- package/dist/tools/MeasureToolbar/MeasureToolbar.js +116 -0
- package/dist/tools/MeasureToolbar/MeasureToolbar.js.map +1 -0
- package/dist/tools/MeasureToolbar/MeasureToolbar.styles.js +12 -0
- package/dist/tools/MeasureToolbar/MeasureToolbar.styles.js.map +1 -0
- package/dist/tools/MeasureToolbar/useMeasure.js +75 -0
- package/dist/tools/MeasureToolbar/useMeasure.js.map +1 -0
- package/dist/tools/PinCard/PinCard.js +174 -0
- package/dist/tools/PinCard/PinCard.js.map +1 -0
- package/dist/tools/PinCard/PinCard.styles.js +16 -0
- package/dist/tools/PinCard/PinCard.styles.js.map +1 -0
- package/dist/tools/PinCard/useReverseAddress.js +77 -0
- package/dist/tools/PinCard/useReverseAddress.js.map +1 -0
- package/dist/tools/SearchPill/SearchPill.js +292 -0
- package/dist/tools/SearchPill/SearchPill.js.map +1 -0
- package/dist/tools/SearchPill/SearchPill.styles.js +16 -0
- package/dist/tools/SearchPill/SearchPill.styles.js.map +1 -0
- package/dist/tools/SearchPill/SearchPillSkeleton.js +41 -0
- package/dist/tools/SearchPill/SearchPillSkeleton.js.map +1 -0
- package/dist/tools/SearchPill/useSearch.js +143 -0
- package/dist/tools/SearchPill/useSearch.js.map +1 -0
- package/dist/tools/SearchPill/useSuggestions.js +105 -0
- package/dist/tools/SearchPill/useSuggestions.js.map +1 -0
- package/dist/tools/Tour/TourCard.js +156 -0
- package/dist/tools/Tour/TourCard.js.map +1 -0
- package/dist/tools/Tour/TourCard.styles.js +10 -0
- package/dist/tools/Tour/TourCard.styles.js.map +1 -0
- package/dist/tools/Tour/useTour.js +110 -0
- package/dist/tools/Tour/useTour.js.map +1 -0
- package/dist/tools/locate/useLocate.js +94 -0
- package/dist/tools/locate/useLocate.js.map +1 -0
- package/dist/ui/Button/Button.js +36 -0
- package/dist/ui/Button/Button.js.map +1 -0
- package/dist/ui/Button/Button.styles.js +17 -0
- package/dist/ui/Button/Button.styles.js.map +1 -0
- package/dist/ui/Chip/Chip.js +37 -0
- package/dist/ui/Chip/Chip.js.map +1 -0
- package/dist/ui/Chip/Chip.styles.js +29 -0
- package/dist/ui/Chip/Chip.styles.js.map +1 -0
- package/dist/ui/ControlGroup/ControlGroup.js +23 -0
- package/dist/ui/ControlGroup/ControlGroup.js.map +1 -0
- package/dist/ui/ControlGroup/ControlGroup.styles.js +6 -0
- package/dist/ui/ControlGroup/ControlGroup.styles.js.map +1 -0
- package/dist/ui/EmptyState/EmptyState.js +37 -0
- package/dist/ui/EmptyState/EmptyState.js.map +1 -0
- package/dist/ui/EmptyState/EmptyState.styles.js +10 -0
- package/dist/ui/EmptyState/EmptyState.styles.js.map +1 -0
- package/dist/ui/Icon/Icon.js +28 -0
- package/dist/ui/Icon/Icon.js.map +1 -0
- package/dist/ui/Icon/Icon.styles.js +8 -0
- package/dist/ui/Icon/Icon.styles.js.map +1 -0
- package/dist/ui/IconButton/IconButton.js +47 -0
- package/dist/ui/IconButton/IconButton.js.map +1 -0
- package/dist/ui/IconButton/IconButton.styles.js +27 -0
- package/dist/ui/IconButton/IconButton.styles.js.map +1 -0
- package/dist/ui/Isolate/Isolate.js +26 -0
- package/dist/ui/Isolate/Isolate.js.map +1 -0
- package/dist/ui/Isolate/fillSlots.js +27 -0
- package/dist/ui/Isolate/fillSlots.js.map +1 -0
- package/dist/ui/ListRow/ListRow.js +59 -0
- package/dist/ui/ListRow/ListRow.js.map +1 -0
- package/dist/ui/ListRow/ListRow.styles.js +30 -0
- package/dist/ui/ListRow/ListRow.styles.js.map +1 -0
- package/dist/ui/LiveRegion/LiveRegion.js +88 -0
- package/dist/ui/LiveRegion/LiveRegion.js.map +1 -0
- package/dist/ui/LiveRegion/announcer.js +114 -0
- package/dist/ui/LiveRegion/announcer.js.map +1 -0
- package/dist/ui/SegmentedControl/SegmentedControl.js +55 -0
- package/dist/ui/SegmentedControl/SegmentedControl.js.map +1 -0
- package/dist/ui/SegmentedControl/SegmentedControl.styles.js +9 -0
- package/dist/ui/SegmentedControl/SegmentedControl.styles.js.map +1 -0
- package/dist/ui/SkipLink/SkipLink.js +35 -0
- package/dist/ui/SkipLink/SkipLink.js.map +1 -0
- package/dist/ui/SkipLink/SkipLink.styles.js +6 -0
- package/dist/ui/SkipLink/SkipLink.styles.js.map +1 -0
- package/dist/ui/Slider/Slider.js +68 -0
- package/dist/ui/Slider/Slider.js.map +1 -0
- package/dist/ui/Slider/Slider.styles.js +13 -0
- package/dist/ui/Slider/Slider.styles.js.map +1 -0
- package/dist/ui/StatusMessage/StatusMessage.js +124 -0
- package/dist/ui/StatusMessage/StatusMessage.js.map +1 -0
- package/dist/ui/StatusMessage/StatusMessage.styles.js +28 -0
- package/dist/ui/StatusMessage/StatusMessage.styles.js.map +1 -0
- package/dist/ui/StatusMessage/StatusMessageFocus.js +30 -0
- package/dist/ui/StatusMessage/StatusMessageFocus.js.map +1 -0
- package/dist/ui/Surface/Surface.js +22 -0
- package/dist/ui/Surface/Surface.js.map +1 -0
- package/dist/ui/Surface/Surface.styles.js +27 -0
- package/dist/ui/Surface/Surface.styles.js.map +1 -0
- package/dist/ui/Switch/Switch.js +58 -0
- package/dist/ui/Switch/Switch.js.map +1 -0
- package/dist/ui/Switch/Switch.styles.js +13 -0
- package/dist/ui/Switch/Switch.styles.js.map +1 -0
- package/dist/ui/Tooltip/Tooltip.js +66 -0
- package/dist/ui/Tooltip/Tooltip.js.map +1 -0
- package/dist/ui/Tooltip/Tooltip.styles.js +6 -0
- package/dist/ui/Tooltip/Tooltip.styles.js.map +1 -0
- package/dist/ui/Tooltip/inheritColours.js +27 -0
- package/dist/ui/Tooltip/inheritColours.js.map +1 -0
- package/dist/ui/VisuallyHidden/VisuallyHidden.js +16 -0
- package/dist/ui/VisuallyHidden/VisuallyHidden.js.map +1 -0
- package/dist/ui/VisuallyHidden/VisuallyHidden.styles.js +6 -0
- package/dist/ui/VisuallyHidden/VisuallyHidden.styles.js.map +1 -0
- package/package.json +82 -4
|
@@ -0,0 +1,352 @@
|
|
|
1
|
+
import { useMapAdapterFactory, usePreferences, useSaveData, useSettings } from "../services/hooks.js";
|
|
2
|
+
import { MapEngineError, omittedLayers } from "@mapcairn/core";
|
|
3
|
+
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
4
|
+
import { useLatestRef } from "@mapcairn/react";
|
|
5
|
+
// src/host/useMapEngine.ts
|
|
6
|
+
/** Zoom differences below this count as equal when deciding whether a limit is reached. */
|
|
7
|
+
var ZOOM_EPSILON = 1e-6;
|
|
8
|
+
/**
|
|
9
|
+
* Identifies what the engine was last asked to draw: the basemap and the
|
|
10
|
+
* layers it leaves out. A change of Save data redraws only a basemap whose
|
|
11
|
+
* key changes with it.
|
|
12
|
+
*/
|
|
13
|
+
function drawKey(basemap, saveData) {
|
|
14
|
+
return `${basemap.id}|${omittedLayers(basemap, saveData).join(",")}`;
|
|
15
|
+
}
|
|
16
|
+
/** Turns any mount or basemap failure into the typed engine error. */
|
|
17
|
+
function toEngineError(reason) {
|
|
18
|
+
if (reason instanceof MapEngineError) return reason;
|
|
19
|
+
const detail = reason instanceof Error ? reason.message : String(reason);
|
|
20
|
+
return new MapEngineError("load-failed", `The map engine could not start: ${detail}`);
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Owns the map engine's life: creates and mounts one adapter, swaps
|
|
24
|
+
* basemaps on it with `setBasemap`, and tracks view, loading and errors.
|
|
25
|
+
*
|
|
26
|
+
* @remarks
|
|
27
|
+
* - Save data (`useSaveData`) goes to the adapter on mount and on every swap.
|
|
28
|
+
* Switching it redraws the current basemap on the same engine, but only
|
|
29
|
+
* when that basemap lists layers to leave out (`saveDataOmitLayers`); for
|
|
30
|
+
* any other it changes nothing the engine draws, so nothing reloads.
|
|
31
|
+
* - The engine gets a fresh element, created here and appended to the slot,
|
|
32
|
+
* so React never renders into or reclasses engine DOM.
|
|
33
|
+
* - The engine mounts once, and again only after a failed mount is retried.
|
|
34
|
+
* A new mount starts from the last view the previous adapter reported
|
|
35
|
+
* (seeded from `initialView`), never from one that had not finished
|
|
36
|
+
* mounting. A basemap chosen while the engine is still mounting is applied
|
|
37
|
+
* as soon as the mount finishes.
|
|
38
|
+
* - Every `mount` result is checked against a per-mount `cancelled` flag, so
|
|
39
|
+
* late mounts are ignored. Cleanup always destroys the adapter, mounted or
|
|
40
|
+
* not.
|
|
41
|
+
* - If focus was inside the engine element, it moves to the region before
|
|
42
|
+
* that element is removed, so it never falls back to the page body.
|
|
43
|
+
* - Subscriptions are made before `mount`, because an engine may emit `load`
|
|
44
|
+
* while mounting.
|
|
45
|
+
* - An engine that stops after it loaded (`fatalError`) is reported like a
|
|
46
|
+
* failed mount, through `error` and `onEngineError`.
|
|
47
|
+
* - A mount that failed to load its basemap (`load-failed`, not a missing
|
|
48
|
+
* WebGL) is mounted again as soon as the caller asks for a different
|
|
49
|
+
* basemap, so a host can fall back from a basemap that cannot start to one
|
|
50
|
+
* that can. The same basemap is never mounted again by itself: that is
|
|
51
|
+
* `retry`.
|
|
52
|
+
* - A basemap swap that fails is not an engine error: the adapter keeps the
|
|
53
|
+
* basemap that last loaded, so `error` stays null and `onBasemapError`
|
|
54
|
+
* names both. That kept basemap counts as applied again, so a caller that
|
|
55
|
+
* goes back to it causes no swap. Only the latest swap is reported, through
|
|
56
|
+
* `onBasemapApplied` or `onBasemapError`.
|
|
57
|
+
*/
|
|
58
|
+
function useMapEngine(options) {
|
|
59
|
+
const createAdapter = useMapAdapterFactory();
|
|
60
|
+
const { reducedMotion } = usePreferences();
|
|
61
|
+
const latest = useLatestRef(options);
|
|
62
|
+
const reducedMotionRef = useLatestRef(reducedMotion);
|
|
63
|
+
const saveData = useSaveData();
|
|
64
|
+
const saveDataRef = useLatestRef(saveData);
|
|
65
|
+
const { projection } = useSettings().settings;
|
|
66
|
+
const projectionRef = useLatestRef(projection);
|
|
67
|
+
/** The shape the engine was last given, so only a change redraws the overlays. */
|
|
68
|
+
const drawnProjectionRef = useRef(projection);
|
|
69
|
+
const { basemap, adapterRef } = options;
|
|
70
|
+
const viewRef = useRef(options.initialView);
|
|
71
|
+
const frameRef = useRef(0);
|
|
72
|
+
const appliedBasemapRef = useRef(null);
|
|
73
|
+
/** The {@link drawKey} of the last basemap the engine was asked to draw. */
|
|
74
|
+
const appliedKeyRef = useRef("");
|
|
75
|
+
/** The basemap the engine last loaded: what a failed swap leaves on screen. */
|
|
76
|
+
const loadedBasemapRef = useRef(null);
|
|
77
|
+
const basemapTokenRef = useRef(0);
|
|
78
|
+
/** The basemap a failed mount was asked to draw, until a different one is asked for. */
|
|
79
|
+
const failedMountRef = useRef(null);
|
|
80
|
+
const cycleErrorRef = useRef(false);
|
|
81
|
+
const [adapter, setAdapter] = useState(null);
|
|
82
|
+
const [hostElement, setHostElement] = useState(null);
|
|
83
|
+
const [view, setView] = useState(options.initialView);
|
|
84
|
+
const [revision, setRevision] = useState(0);
|
|
85
|
+
const [zoomLimits, setZoomLimits] = useState({
|
|
86
|
+
min: 0,
|
|
87
|
+
max: 0
|
|
88
|
+
});
|
|
89
|
+
const [loaded, setLoaded] = useState(false);
|
|
90
|
+
const [busy, setBusy] = useState(false);
|
|
91
|
+
const [tileError, setTileError] = useState(false);
|
|
92
|
+
const [error, setError] = useState(null);
|
|
93
|
+
const [attempt, setAttempt] = useState(0);
|
|
94
|
+
const scheduleView = useCallback(() => {
|
|
95
|
+
if (frameRef.current !== 0) return;
|
|
96
|
+
frameRef.current = requestAnimationFrame(() => {
|
|
97
|
+
frameRef.current = 0;
|
|
98
|
+
setView(viewRef.current);
|
|
99
|
+
setRevision((value) => value + 1);
|
|
100
|
+
});
|
|
101
|
+
}, []);
|
|
102
|
+
const writeZoom = useCallback((zoom) => {
|
|
103
|
+
latest.current.hostRef.current?.setAttribute("data-zoom", zoom.toFixed(2));
|
|
104
|
+
}, [latest]);
|
|
105
|
+
const applyBasemap = useCallback((target, next) => {
|
|
106
|
+
basemapTokenRef.current += 1;
|
|
107
|
+
const token = basemapTokenRef.current;
|
|
108
|
+
appliedKeyRef.current = drawKey(next, saveDataRef.current);
|
|
109
|
+
const isCurrent = () => token === basemapTokenRef.current && adapterRef.current === target;
|
|
110
|
+
cycleErrorRef.current = false;
|
|
111
|
+
setError(null);
|
|
112
|
+
setLoaded(false);
|
|
113
|
+
target.setBasemap(next, { saveData: saveDataRef.current }).then(() => {
|
|
114
|
+
if (isCurrent()) {
|
|
115
|
+
loadedBasemapRef.current = next;
|
|
116
|
+
setZoomLimits({
|
|
117
|
+
min: target.getMinZoom(),
|
|
118
|
+
max: target.getMaxZoom()
|
|
119
|
+
});
|
|
120
|
+
latest.current.onBasemapApplied?.(next);
|
|
121
|
+
}
|
|
122
|
+
}, (reason) => {
|
|
123
|
+
if (!isCurrent()) return;
|
|
124
|
+
const kept = loadedBasemapRef.current;
|
|
125
|
+
appliedBasemapRef.current = kept;
|
|
126
|
+
appliedKeyRef.current = drawKey(kept, saveDataRef.current);
|
|
127
|
+
setLoaded(true);
|
|
128
|
+
setZoomLimits({
|
|
129
|
+
min: target.getMinZoom(),
|
|
130
|
+
max: target.getMaxZoom()
|
|
131
|
+
});
|
|
132
|
+
latest.current.onBasemapError?.(toEngineError(reason), next, kept);
|
|
133
|
+
});
|
|
134
|
+
}, [
|
|
135
|
+
adapterRef,
|
|
136
|
+
latest,
|
|
137
|
+
saveDataRef
|
|
138
|
+
]);
|
|
139
|
+
useEffect(() => {
|
|
140
|
+
const slot = latest.current.slotRef.current;
|
|
141
|
+
const host = latest.current.hostRef.current;
|
|
142
|
+
if (!slot || !host) return;
|
|
143
|
+
const target = latest.current.basemap;
|
|
144
|
+
const mountedSaveData = saveDataRef.current;
|
|
145
|
+
const mountedProjection = projectionRef.current;
|
|
146
|
+
const next = createAdapter();
|
|
147
|
+
const element = document.createElement("div");
|
|
148
|
+
element.className = latest.current.engineClassName;
|
|
149
|
+
slot.append(element);
|
|
150
|
+
let cancelled = false;
|
|
151
|
+
cycleErrorRef.current = false;
|
|
152
|
+
setLoaded(false);
|
|
153
|
+
setBusy(false);
|
|
154
|
+
setTileError(false);
|
|
155
|
+
setError(null);
|
|
156
|
+
const follow = (moved) => {
|
|
157
|
+
viewRef.current = moved;
|
|
158
|
+
scheduleView();
|
|
159
|
+
};
|
|
160
|
+
const unsubscribe = [
|
|
161
|
+
next.on("move", follow),
|
|
162
|
+
next.on("rotate", follow),
|
|
163
|
+
next.on("zoom", (moved) => {
|
|
164
|
+
follow(moved);
|
|
165
|
+
writeZoom(moved.zoom);
|
|
166
|
+
}),
|
|
167
|
+
next.on("moveEnd", (moved) => {
|
|
168
|
+
follow(moved);
|
|
169
|
+
writeZoom(moved.zoom);
|
|
170
|
+
latest.current.onMoveEnd();
|
|
171
|
+
latest.current.onViewChange?.(moved);
|
|
172
|
+
}),
|
|
173
|
+
next.on("click", (event) => {
|
|
174
|
+
if (!latest.current.shouldDropClick()) latest.current.onClick?.(event);
|
|
175
|
+
}),
|
|
176
|
+
next.on("handleDrag", (drag) => {
|
|
177
|
+
if (drag.phase === "start") latest.current.onMoveEnd();
|
|
178
|
+
}),
|
|
179
|
+
next.on("load", () => {
|
|
180
|
+
setLoaded(true);
|
|
181
|
+
if (!cycleErrorRef.current) setTileError(false);
|
|
182
|
+
}),
|
|
183
|
+
next.on("loading", (isLoading) => {
|
|
184
|
+
setBusy(isLoading);
|
|
185
|
+
if (isLoading) cycleErrorRef.current = false;
|
|
186
|
+
else if (!cycleErrorRef.current) setTileError(false);
|
|
187
|
+
}),
|
|
188
|
+
next.on("tileError", () => {
|
|
189
|
+
cycleErrorRef.current = true;
|
|
190
|
+
setTileError(true);
|
|
191
|
+
}),
|
|
192
|
+
next.on("basemapUnavailable", (unavailable) => {
|
|
193
|
+
if (!cancelled) latest.current.onBasemapUnavailable?.(unavailable);
|
|
194
|
+
}),
|
|
195
|
+
next.on("userZoom", () => {
|
|
196
|
+
latest.current.onUserZoom();
|
|
197
|
+
}),
|
|
198
|
+
next.on("fatalError", (engineError) => {
|
|
199
|
+
if (cancelled) return;
|
|
200
|
+
setError(engineError);
|
|
201
|
+
latest.current.onEngineError?.(engineError, latest.current.basemap);
|
|
202
|
+
})
|
|
203
|
+
];
|
|
204
|
+
next.mount(element, {
|
|
205
|
+
view: viewRef.current,
|
|
206
|
+
basemap: target,
|
|
207
|
+
reducedMotion: reducedMotionRef.current,
|
|
208
|
+
saveData: mountedSaveData,
|
|
209
|
+
projection: mountedProjection
|
|
210
|
+
}).then(() => {
|
|
211
|
+
if (cancelled) return;
|
|
212
|
+
adapterRef.current = next;
|
|
213
|
+
appliedBasemapRef.current = target;
|
|
214
|
+
appliedKeyRef.current = drawKey(target, mountedSaveData);
|
|
215
|
+
loadedBasemapRef.current = target;
|
|
216
|
+
drawnProjectionRef.current = mountedProjection;
|
|
217
|
+
next.setKeyboardEnabled(false);
|
|
218
|
+
viewRef.current = next.getView();
|
|
219
|
+
writeZoom(viewRef.current.zoom);
|
|
220
|
+
setView(viewRef.current);
|
|
221
|
+
setZoomLimits({
|
|
222
|
+
min: next.getMinZoom(),
|
|
223
|
+
max: next.getMaxZoom()
|
|
224
|
+
});
|
|
225
|
+
setAdapter(next);
|
|
226
|
+
setHostElement(element);
|
|
227
|
+
}, (reason) => {
|
|
228
|
+
if (cancelled) return;
|
|
229
|
+
const engineError = toEngineError(reason);
|
|
230
|
+
failedMountRef.current = engineError.reason === "load-failed" ? target : null;
|
|
231
|
+
setError(engineError);
|
|
232
|
+
latest.current.onEngineError?.(engineError, target);
|
|
233
|
+
});
|
|
234
|
+
return () => {
|
|
235
|
+
cancelled = true;
|
|
236
|
+
for (const off of unsubscribe) off();
|
|
237
|
+
cancelAnimationFrame(frameRef.current);
|
|
238
|
+
frameRef.current = 0;
|
|
239
|
+
latest.current.onMoveEnd();
|
|
240
|
+
if (adapterRef.current === next) adapterRef.current = null;
|
|
241
|
+
if (element.contains(document.activeElement)) host.focus({ preventScroll: true });
|
|
242
|
+
next.destroy();
|
|
243
|
+
element.remove();
|
|
244
|
+
setAdapter(null);
|
|
245
|
+
setHostElement(null);
|
|
246
|
+
};
|
|
247
|
+
}, [
|
|
248
|
+
createAdapter,
|
|
249
|
+
attempt,
|
|
250
|
+
adapterRef,
|
|
251
|
+
latest,
|
|
252
|
+
reducedMotionRef,
|
|
253
|
+
saveDataRef,
|
|
254
|
+
projectionRef,
|
|
255
|
+
scheduleView,
|
|
256
|
+
writeZoom
|
|
257
|
+
]);
|
|
258
|
+
useEffect(() => {
|
|
259
|
+
if (!adapter || adapterRef.current !== adapter || !appliedBasemapRef.current || appliedKeyRef.current === drawKey(basemap, saveData)) return;
|
|
260
|
+
appliedBasemapRef.current = basemap;
|
|
261
|
+
applyBasemap(adapter, basemap);
|
|
262
|
+
}, [
|
|
263
|
+
adapter,
|
|
264
|
+
adapterRef,
|
|
265
|
+
applyBasemap,
|
|
266
|
+
basemap,
|
|
267
|
+
saveData
|
|
268
|
+
]);
|
|
269
|
+
useEffect(() => {
|
|
270
|
+
adapter?.setAnimationEnabled(!reducedMotion);
|
|
271
|
+
}, [adapter, reducedMotion]);
|
|
272
|
+
useEffect(() => {
|
|
273
|
+
if (!adapter) return;
|
|
274
|
+
if (drawnProjectionRef.current !== projection) {
|
|
275
|
+
drawnProjectionRef.current = projection;
|
|
276
|
+
adapter.setProjection(projection);
|
|
277
|
+
scheduleView();
|
|
278
|
+
}
|
|
279
|
+
}, [
|
|
280
|
+
adapter,
|
|
281
|
+
projection,
|
|
282
|
+
scheduleView
|
|
283
|
+
]);
|
|
284
|
+
useEffect(() => {
|
|
285
|
+
const host = latest.current.hostRef.current;
|
|
286
|
+
if (!host) return;
|
|
287
|
+
const observer = new ResizeObserver(() => {
|
|
288
|
+
scheduleView();
|
|
289
|
+
});
|
|
290
|
+
observer.observe(host);
|
|
291
|
+
return () => {
|
|
292
|
+
observer.disconnect();
|
|
293
|
+
};
|
|
294
|
+
}, [latest, scheduleView]);
|
|
295
|
+
useEffect(() => {
|
|
296
|
+
const failed = failedMountRef.current;
|
|
297
|
+
if (error !== null && failed !== null && failed.id !== basemap.id) {
|
|
298
|
+
failedMountRef.current = null;
|
|
299
|
+
setAttempt((value) => value + 1);
|
|
300
|
+
}
|
|
301
|
+
}, [error, basemap.id]);
|
|
302
|
+
const retry = useCallback(() => {
|
|
303
|
+
if (error) {
|
|
304
|
+
setAttempt((value) => value + 1);
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
const current = adapterRef.current;
|
|
308
|
+
if (current) applyBasemap(current, latest.current.basemap);
|
|
309
|
+
}, [
|
|
310
|
+
adapterRef,
|
|
311
|
+
applyBasemap,
|
|
312
|
+
error,
|
|
313
|
+
latest
|
|
314
|
+
]);
|
|
315
|
+
const focusMap = useCallback(() => {
|
|
316
|
+
latest.current.hostRef.current?.focus({ preventScroll: true });
|
|
317
|
+
}, [latest]);
|
|
318
|
+
const minZoom = adapter ? zoomLimits.min : basemap.minZoom;
|
|
319
|
+
const maxZoom = adapter ? zoomLimits.max : basemap.maxZoom;
|
|
320
|
+
const loading = error === null && (adapter === null || !loaded || busy);
|
|
321
|
+
return useMemo(() => ({
|
|
322
|
+
adapter,
|
|
323
|
+
hostElement,
|
|
324
|
+
view,
|
|
325
|
+
projection,
|
|
326
|
+
minZoom,
|
|
327
|
+
maxZoom,
|
|
328
|
+
canZoomIn: view.zoom < maxZoom - ZOOM_EPSILON,
|
|
329
|
+
canZoomOut: view.zoom > minZoom + ZOOM_EPSILON,
|
|
330
|
+
loading,
|
|
331
|
+
tileError,
|
|
332
|
+
error,
|
|
333
|
+
retry,
|
|
334
|
+
focusMap
|
|
335
|
+
}), [
|
|
336
|
+
adapter,
|
|
337
|
+
hostElement,
|
|
338
|
+
view,
|
|
339
|
+
projection,
|
|
340
|
+
revision,
|
|
341
|
+
minZoom,
|
|
342
|
+
maxZoom,
|
|
343
|
+
loading,
|
|
344
|
+
tileError,
|
|
345
|
+
error,
|
|
346
|
+
retry,
|
|
347
|
+
focusMap
|
|
348
|
+
]);
|
|
349
|
+
}
|
|
350
|
+
export { useMapEngine };
|
|
351
|
+
|
|
352
|
+
//# sourceMappingURL=useMapEngine.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useMapEngine.js","names":[],"sources":["../../src/host/useMapEngine.ts"],"sourcesContent":["// SPDX-License-Identifier: AGPL-3.0-only\n// Copyright MapCairn contributors\nimport {\n type BasemapDefinition,\n type MapAdapter,\n type MapClickEvent,\n MapEngineError,\n omittedLayers,\n type ViewState,\n} from \"@mapcairn/core\";\nimport { useLatestRef } from \"@mapcairn/react\";\nimport {\n type RefObject,\n useCallback,\n useEffect,\n useMemo,\n useRef,\n useState,\n} from \"react\";\nimport {\n useMapAdapterFactory,\n usePreferences,\n useSaveData,\n useSettings,\n} from \"../services\";\nimport type { MapContextValue } from \"./MapContext\";\n\n/** Zoom differences below this count as equal when deciding whether a limit is reached. */\nconst ZOOM_EPSILON = 1e-6;\n\n/** Options for {@link useMapEngine}. */\nexport interface UseMapEngineOptions {\n /** The map region; it carries `data-zoom` and receives focus. */\n readonly hostRef: RefObject<HTMLElement | null>;\n /** The element the engine element is appended to. */\n readonly slotRef: RefObject<HTMLElement | null>;\n /** Written with the mounted adapter, or null, for event handlers outside this hook. */\n readonly adapterRef: RefObject<MapAdapter | null>;\n /** Class name of the element the engine mounts into. */\n readonly engineClassName: string;\n readonly initialView: ViewState;\n readonly basemap: BasemapDefinition;\n readonly onViewChange?: (view: ViewState) => void;\n readonly onEngineError?: (\n error: MapEngineError,\n basemap: BasemapDefinition,\n ) => void;\n /** Called when a basemap swap has loaded. */\n readonly onBasemapApplied?: (basemap: BasemapDefinition) => void;\n /** Called when a basemap swap failed; the map keeps `kept`. */\n readonly onBasemapError?: (\n error: MapEngineError,\n failed: BasemapDefinition,\n kept: BasemapDefinition,\n ) => void;\n /**\n * Called when every tile of the basemap the engine is drawing failed to\n * load and none loaded. The engine keeps working.\n */\n readonly onBasemapUnavailable?: (basemap: BasemapDefinition) => void;\n readonly onClick?: (event: MapClickEvent) => void;\n /**\n * Called on every `moveEnd`, before `onViewChange`, and when a press starts\n * on an edit handle. The host cancels a pending long-press with it.\n */\n readonly onMoveEnd: () => void;\n /** Returns true when an engine click should be dropped (after a long-press). */\n readonly shouldDropClick: () => boolean;\n /** Called while the person zooms with a gesture the engine handles. */\n readonly onUserZoom: () => void;\n}\n\n/**\n * What {@link useMapEngine} tracks: the map context apart from the user zoom\n * subscription, which `MapView` adds because it also owns the zoom keys.\n */\nexport type MapEngineState = Omit<MapContextValue, \"onUserZoom\">;\n\n/**\n * Identifies what the engine was last asked to draw: the basemap and the\n * layers it leaves out. A change of Save data redraws only a basemap whose\n * key changes with it.\n */\nfunction drawKey(basemap: BasemapDefinition, saveData: boolean): string {\n return `${basemap.id}|${omittedLayers(basemap, saveData).join(\",\")}`;\n}\n\n/** Turns any mount or basemap failure into the typed engine error. */\nfunction toEngineError(reason: unknown): MapEngineError {\n if (reason instanceof MapEngineError) {\n return reason;\n }\n const detail = reason instanceof Error ? reason.message : String(reason);\n return new MapEngineError(\n \"load-failed\",\n `The map engine could not start: ${detail}`,\n );\n}\n\n/**\n * Owns the map engine's life: creates and mounts one adapter, swaps\n * basemaps on it with `setBasemap`, and tracks view, loading and errors.\n *\n * @remarks\n * - Save data (`useSaveData`) goes to the adapter on mount and on every swap.\n * Switching it redraws the current basemap on the same engine, but only\n * when that basemap lists layers to leave out (`saveDataOmitLayers`); for\n * any other it changes nothing the engine draws, so nothing reloads.\n * - The engine gets a fresh element, created here and appended to the slot,\n * so React never renders into or reclasses engine DOM.\n * - The engine mounts once, and again only after a failed mount is retried.\n * A new mount starts from the last view the previous adapter reported\n * (seeded from `initialView`), never from one that had not finished\n * mounting. A basemap chosen while the engine is still mounting is applied\n * as soon as the mount finishes.\n * - Every `mount` result is checked against a per-mount `cancelled` flag, so\n * late mounts are ignored. Cleanup always destroys the adapter, mounted or\n * not.\n * - If focus was inside the engine element, it moves to the region before\n * that element is removed, so it never falls back to the page body.\n * - Subscriptions are made before `mount`, because an engine may emit `load`\n * while mounting.\n * - An engine that stops after it loaded (`fatalError`) is reported like a\n * failed mount, through `error` and `onEngineError`.\n * - A mount that failed to load its basemap (`load-failed`, not a missing\n * WebGL) is mounted again as soon as the caller asks for a different\n * basemap, so a host can fall back from a basemap that cannot start to one\n * that can. The same basemap is never mounted again by itself: that is\n * `retry`.\n * - A basemap swap that fails is not an engine error: the adapter keeps the\n * basemap that last loaded, so `error` stays null and `onBasemapError`\n * names both. That kept basemap counts as applied again, so a caller that\n * goes back to it causes no swap. Only the latest swap is reported, through\n * `onBasemapApplied` or `onBasemapError`.\n */\nexport function useMapEngine(options: UseMapEngineOptions): MapEngineState {\n const createAdapter = useMapAdapterFactory();\n const { reducedMotion } = usePreferences();\n const latest = useLatestRef(options);\n const reducedMotionRef = useLatestRef(reducedMotion);\n const saveData = useSaveData();\n const saveDataRef = useLatestRef(saveData);\n const { projection } = useSettings().settings;\n const projectionRef = useLatestRef(projection);\n /** The shape the engine was last given, so only a change redraws the overlays. */\n const drawnProjectionRef = useRef(projection);\n const { basemap, adapterRef } = options;\n\n const viewRef = useRef(options.initialView);\n const frameRef = useRef(0);\n const appliedBasemapRef = useRef<BasemapDefinition | null>(null);\n /** The {@link drawKey} of the last basemap the engine was asked to draw. */\n const appliedKeyRef = useRef(\"\");\n /** The basemap the engine last loaded: what a failed swap leaves on screen. */\n const loadedBasemapRef = useRef<BasemapDefinition | null>(null);\n const basemapTokenRef = useRef(0);\n /** The basemap a failed mount was asked to draw, until a different one is asked for. */\n const failedMountRef = useRef<BasemapDefinition | null>(null);\n const cycleErrorRef = useRef(false);\n\n const [adapter, setAdapter] = useState<MapAdapter | null>(null);\n const [hostElement, setHostElement] = useState<HTMLElement | null>(null);\n const [view, setView] = useState(options.initialView);\n const [revision, setRevision] = useState(0);\n const [zoomLimits, setZoomLimits] = useState({ min: 0, max: 0 });\n const [loaded, setLoaded] = useState(false);\n const [busy, setBusy] = useState(false);\n const [tileError, setTileError] = useState(false);\n const [error, setError] = useState<MapEngineError | null>(null);\n const [attempt, setAttempt] = useState(0);\n\n const scheduleView = useCallback(() => {\n if (frameRef.current !== 0) {\n return;\n }\n frameRef.current = requestAnimationFrame(() => {\n frameRef.current = 0;\n setView(viewRef.current);\n setRevision((value) => value + 1);\n });\n }, []);\n\n const writeZoom = useCallback(\n (zoom: number) => {\n latest.current.hostRef.current?.setAttribute(\n \"data-zoom\",\n zoom.toFixed(2),\n );\n },\n [latest],\n );\n\n const applyBasemap = useCallback(\n (target: MapAdapter, next: BasemapDefinition) => {\n basemapTokenRef.current += 1;\n const token = basemapTokenRef.current;\n appliedKeyRef.current = drawKey(next, saveDataRef.current);\n const isCurrent = () =>\n token === basemapTokenRef.current && adapterRef.current === target;\n cycleErrorRef.current = false;\n setError(null);\n setLoaded(false);\n target.setBasemap(next, { saveData: saveDataRef.current }).then(\n () => {\n if (isCurrent()) {\n loadedBasemapRef.current = next;\n setZoomLimits({\n min: target.getMinZoom(),\n max: target.getMaxZoom(),\n });\n latest.current.onBasemapApplied?.(next);\n }\n },\n (reason: unknown) => {\n if (!isCurrent()) {\n return;\n }\n // Set by the mount, which always finishes before the first swap.\n const kept = loadedBasemapRef.current as BasemapDefinition;\n appliedBasemapRef.current = kept;\n appliedKeyRef.current = drawKey(kept, saveDataRef.current);\n // The kept basemap was already drawn; the adapter reports its\n // reload through `loading`.\n setLoaded(true);\n setZoomLimits({ min: target.getMinZoom(), max: target.getMaxZoom() });\n latest.current.onBasemapError?.(toEngineError(reason), next, kept);\n },\n );\n },\n [adapterRef, latest, saveDataRef],\n );\n\n // biome-ignore lint/correctness/useExhaustiveDependencies: `attempt` is the trigger; the basemap is read from `latest` so a basemap change never remounts the engine.\n useEffect(() => {\n const slot = latest.current.slotRef.current;\n const host = latest.current.hostRef.current;\n if (!slot || !host) {\n return;\n }\n const target = latest.current.basemap;\n // What this mount is asked for, not what Save data is by the time it ends.\n const mountedSaveData = saveDataRef.current;\n const mountedProjection = projectionRef.current;\n const next = createAdapter();\n const element = document.createElement(\"div\");\n element.className = latest.current.engineClassName;\n slot.append(element);\n let cancelled = false;\n cycleErrorRef.current = false;\n setLoaded(false);\n setBusy(false);\n setTileError(false);\n setError(null);\n\n const follow = (moved: ViewState) => {\n viewRef.current = moved;\n scheduleView();\n };\n const unsubscribe = [\n next.on(\"move\", follow),\n next.on(\"rotate\", follow),\n next.on(\"zoom\", (moved) => {\n follow(moved);\n writeZoom(moved.zoom);\n }),\n next.on(\"moveEnd\", (moved) => {\n follow(moved);\n writeZoom(moved.zoom);\n latest.current.onMoveEnd();\n latest.current.onViewChange?.(moved);\n }),\n next.on(\"click\", (event) => {\n if (!latest.current.shouldDropClick()) {\n latest.current.onClick?.(event);\n }\n }),\n next.on(\"handleDrag\", (drag) => {\n // A press held on an edit handle is not a long-press.\n if (drag.phase === \"start\") {\n latest.current.onMoveEnd();\n }\n }),\n next.on(\"load\", () => {\n setLoaded(true);\n if (!cycleErrorRef.current) {\n setTileError(false);\n }\n }),\n next.on(\"loading\", (isLoading) => {\n setBusy(isLoading);\n if (isLoading) {\n cycleErrorRef.current = false;\n } else if (!cycleErrorRef.current) {\n setTileError(false);\n }\n }),\n next.on(\"tileError\", () => {\n cycleErrorRef.current = true;\n setTileError(true);\n }),\n // The engine names the basemap, because the report can come before the\n // mount has resolved (MapLibre fires `load` and the first `idle` in one\n // render), when nothing here knows what is drawn yet.\n next.on(\"basemapUnavailable\", (unavailable) => {\n if (!cancelled) {\n latest.current.onBasemapUnavailable?.(unavailable);\n }\n }),\n next.on(\"userZoom\", () => {\n latest.current.onUserZoom();\n }),\n next.on(\"fatalError\", (engineError) => {\n if (cancelled) {\n return;\n }\n setError(engineError);\n latest.current.onEngineError?.(engineError, latest.current.basemap);\n }),\n ];\n\n next\n .mount(element, {\n view: viewRef.current,\n basemap: target,\n reducedMotion: reducedMotionRef.current,\n saveData: mountedSaveData,\n projection: mountedProjection,\n })\n .then(\n () => {\n if (cancelled) {\n return;\n }\n adapterRef.current = next;\n appliedBasemapRef.current = target;\n appliedKeyRef.current = drawKey(target, mountedSaveData);\n loadedBasemapRef.current = target;\n drawnProjectionRef.current = mountedProjection;\n next.setKeyboardEnabled(false);\n viewRef.current = next.getView();\n writeZoom(viewRef.current.zoom);\n setView(viewRef.current);\n setZoomLimits({ min: next.getMinZoom(), max: next.getMaxZoom() });\n setAdapter(next);\n setHostElement(element);\n },\n (reason: unknown) => {\n if (cancelled) {\n return;\n }\n const engineError = toEngineError(reason);\n failedMountRef.current =\n engineError.reason === \"load-failed\" ? target : null;\n setError(engineError);\n latest.current.onEngineError?.(engineError, target);\n },\n );\n\n return () => {\n cancelled = true;\n for (const off of unsubscribe) {\n off();\n }\n cancelAnimationFrame(frameRef.current);\n frameRef.current = 0;\n latest.current.onMoveEnd();\n if (adapterRef.current === next) {\n adapterRef.current = null;\n }\n if (element.contains(document.activeElement)) {\n host.focus({ preventScroll: true });\n }\n next.destroy();\n element.remove();\n setAdapter(null);\n setHostElement(null);\n };\n }, [\n createAdapter,\n attempt,\n adapterRef,\n latest,\n reducedMotionRef,\n saveDataRef,\n projectionRef,\n scheduleView,\n writeZoom,\n ]);\n\n useEffect(() => {\n if (\n !adapter ||\n adapterRef.current !== adapter ||\n !appliedBasemapRef.current ||\n appliedKeyRef.current === drawKey(basemap, saveData)\n ) {\n return;\n }\n appliedBasemapRef.current = basemap;\n applyBasemap(adapter, basemap);\n }, [adapter, adapterRef, applyBasemap, basemap, saveData]);\n\n useEffect(() => {\n adapter?.setAnimationEnabled(!reducedMotion);\n }, [adapter, reducedMotion]);\n\n // A new shape moves no view event, so the overlays are told to project again.\n useEffect(() => {\n if (!adapter) {\n return;\n }\n if (drawnProjectionRef.current !== projection) {\n drawnProjectionRef.current = projection;\n adapter.setProjection(projection);\n scheduleView();\n }\n }, [adapter, projection, scheduleView]);\n\n useEffect(() => {\n const host = latest.current.hostRef.current;\n if (!host) {\n return;\n }\n const observer = new ResizeObserver(() => {\n scheduleView();\n });\n observer.observe(host);\n return () => {\n observer.disconnect();\n };\n }, [latest, scheduleView]);\n\n // A different basemap after a mount that could not load one starts over.\n useEffect(() => {\n const failed = failedMountRef.current;\n if (error !== null && failed !== null && failed.id !== basemap.id) {\n failedMountRef.current = null;\n setAttempt((value) => value + 1);\n }\n }, [error, basemap.id]);\n\n const retry = useCallback(() => {\n if (error) {\n setAttempt((value) => value + 1);\n return;\n }\n const current = adapterRef.current;\n if (current) {\n applyBasemap(current, latest.current.basemap);\n }\n }, [adapterRef, applyBasemap, error, latest]);\n\n const focusMap = useCallback(() => {\n latest.current.hostRef.current?.focus({ preventScroll: true });\n }, [latest]);\n\n const minZoom = adapter ? zoomLimits.min : basemap.minZoom;\n const maxZoom = adapter ? zoomLimits.max : basemap.maxZoom;\n const loading = error === null && (adapter === null || !loaded || busy);\n\n // biome-ignore lint/correctness/useExhaustiveDependencies: `revision` forces a new value when the projection changes without a new view (resize).\n return useMemo<MapEngineState>(\n () => ({\n adapter,\n hostElement,\n view,\n projection,\n minZoom,\n maxZoom,\n canZoomIn: view.zoom < maxZoom - ZOOM_EPSILON,\n canZoomOut: view.zoom > minZoom + ZOOM_EPSILON,\n loading,\n tileError,\n error,\n retry,\n focusMap,\n }),\n [\n adapter,\n hostElement,\n view,\n projection,\n revision,\n minZoom,\n maxZoom,\n loading,\n tileError,\n error,\n retry,\n focusMap,\n ],\n );\n}\n"],"mappings":";;;;;;AA4BA,IAAM,eAAe;;;;;;AAuDrB,SAAS,QAAQ,SAA4B,UAA2B;CACtE,OAAO,GAAG,QAAQ,GAAG,GAAG,cAAc,SAAS,QAAQ,CAAC,CAAC,KAAK,GAAG;AACnE;;AAGA,SAAS,cAAc,QAAiC;CACtD,IAAI,kBAAkB,gBACpB,OAAO;CAET,MAAM,SAAS,kBAAkB,QAAQ,OAAO,UAAU,OAAO,MAAM;CACvE,OAAO,IAAI,eACT,eACA,mCAAmC,QACrC;AACF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAsCA,SAAgB,aAAa,SAA8C;CACzE,MAAM,gBAAgB,qBAAqB;CAC3C,MAAM,EAAE,kBAAkB,eAAe;CACzC,MAAM,SAAS,aAAa,OAAO;CACnC,MAAM,mBAAmB,aAAa,aAAa;CACnD,MAAM,WAAW,YAAY;CAC7B,MAAM,cAAc,aAAa,QAAQ;CACzC,MAAM,EAAE,eAAe,YAAY,CAAC,CAAC;CACrC,MAAM,gBAAgB,aAAa,UAAU;;CAE7C,MAAM,qBAAqB,OAAO,UAAU;CAC5C,MAAM,EAAE,SAAS,eAAe;CAEhC,MAAM,UAAU,OAAO,QAAQ,WAAW;CAC1C,MAAM,WAAW,OAAO,CAAC;CACzB,MAAM,oBAAoB,OAAiC,IAAI;;CAE/D,MAAM,gBAAgB,OAAO,EAAE;;CAE/B,MAAM,mBAAmB,OAAiC,IAAI;CAC9D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WAAW,IAAI,qBAAqB;GACxC,aAAa;EACf,CAAC;EACD,SAAS,QAAQ,IAAI;EACrB,aAAa;GACX,SAAS,WAAW;EACtB;CACF,GAAG,CAAC,QAAQ,YAAY,CAAC;CAGzB,gBAAgB;EACd,MAAM,SAAS,eAAe;EAC9B,IAAI,UAAU,QAAQ,WAAW,QAAQ,OAAO,OAAO,QAAQ,IAAI;GACjE,eAAe,UAAU;GACzB,YAAY,UAAU,QAAQ,CAAC;EACjC;CACF,GAAG,CAAC,OAAO,QAAQ,EAAE,CAAC;CAEtB,MAAM,QAAQ,kBAAkB;EAC9B,IAAI,OAAO;GACT,YAAY,UAAU,QAAQ,CAAC;GAC/B;EACF;EACA,MAAM,UAAU,WAAW;EAC3B,IAAI,SACF,aAAa,SAAS,OAAO,QAAQ,OAAO;CAEhD,GAAG;EAAC;EAAY;EAAc;EAAO;CAAM,CAAC;CAE5C,MAAM,WAAW,kBAAkB;EACjC,OAAO,QAAQ,QAAQ,SAAS,MAAM,EAAE,eAAe,KAAK,CAAC;CAC/D,GAAG,CAAC,MAAM,CAAC;CAEX,MAAM,UAAU,UAAU,WAAW,MAAM,QAAQ;CACnD,MAAM,UAAU,UAAU,WAAW,MAAM,QAAQ;CACnD,MAAM,UAAU,UAAU,SAAS,YAAY,QAAQ,CAAC,UAAU;CAGlE,OAAO,eACE;EACL;EACA;EACA;EACA;EACA;EACA;EACA,WAAW,KAAK,OAAO,UAAU;EACjC,YAAY,KAAK,OAAO,UAAU;EAClC;EACA;EACA;EACA;EACA;CACF,IACA;EACE;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;CACF,CACF;AACF"}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { isCursorReadout } from "./pointerTargets.js";
|
|
2
|
+
import { useEffect, useMemo, useRef } from "react";
|
|
3
|
+
import { useLatestRef } from "@mapcairn/react";
|
|
4
|
+
// src/host/usePointerPosition.ts
|
|
5
|
+
/**
|
|
6
|
+
* Reports the geographic position under a mouse or pen, at most once per
|
|
7
|
+
* animation frame, for the cursor coordinates readout. Touch is ignored,
|
|
8
|
+
* because a finger has no hover position. Leaving the map reports null and
|
|
9
|
+
* drops any pending frame, except onto the cursor readout (`MapReadout`):
|
|
10
|
+
* the readout grows to hold the coordinates and can grow under a resting
|
|
11
|
+
* pointer, so the last position stays. The host then clears it from
|
|
12
|
+
* `MapReadout`'s `onCursorLeave`; a host that shows the position in a
|
|
13
|
+
* readout of its own clears it when the pointer leaves that readout.
|
|
14
|
+
*/
|
|
15
|
+
function usePointerPosition(options) {
|
|
16
|
+
const optionsRef = useLatestRef(options);
|
|
17
|
+
const frame = useRef(0);
|
|
18
|
+
const client = useRef({
|
|
19
|
+
x: 0,
|
|
20
|
+
y: 0
|
|
21
|
+
});
|
|
22
|
+
const handlers = useMemo(() => {
|
|
23
|
+
const report = () => {
|
|
24
|
+
frame.current = 0;
|
|
25
|
+
const { hostRef, getAdapter, onPointerMove } = optionsRef.current;
|
|
26
|
+
const adapter = getAdapter();
|
|
27
|
+
const host = hostRef.current;
|
|
28
|
+
if (!adapter || !host || !onPointerMove) return;
|
|
29
|
+
const rect = host.getBoundingClientRect();
|
|
30
|
+
onPointerMove(adapter.unproject({
|
|
31
|
+
x: client.current.x - rect.left,
|
|
32
|
+
y: client.current.y - rect.top
|
|
33
|
+
}));
|
|
34
|
+
};
|
|
35
|
+
return {
|
|
36
|
+
onPointerMove: (event) => {
|
|
37
|
+
if (event.pointerType === "touch" || !optionsRef.current.onPointerMove) return;
|
|
38
|
+
client.current = {
|
|
39
|
+
x: event.clientX,
|
|
40
|
+
y: event.clientY
|
|
41
|
+
};
|
|
42
|
+
if (frame.current === 0) frame.current = requestAnimationFrame(report);
|
|
43
|
+
},
|
|
44
|
+
onPointerLeave: (event) => {
|
|
45
|
+
if (event.pointerType === "touch" || isCursorReadout(event.relatedTarget)) return;
|
|
46
|
+
cancelAnimationFrame(frame.current);
|
|
47
|
+
frame.current = 0;
|
|
48
|
+
optionsRef.current.onPointerMove?.(null);
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
}, [optionsRef]);
|
|
52
|
+
useEffect(() => () => {
|
|
53
|
+
cancelAnimationFrame(frame.current);
|
|
54
|
+
frame.current = 0;
|
|
55
|
+
}, []);
|
|
56
|
+
return handlers;
|
|
57
|
+
}
|
|
58
|
+
export { usePointerPosition };
|
|
59
|
+
|
|
60
|
+
//# sourceMappingURL=usePointerPosition.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"usePointerPosition.js","names":[],"sources":["../../src/host/usePointerPosition.ts"],"sourcesContent":["// SPDX-License-Identifier: AGPL-3.0-only\n// Copyright MapCairn contributors\nimport type { LatLng, MapAdapter } from \"@mapcairn/core\";\nimport { useLatestRef } from \"@mapcairn/react\";\nimport {\n type PointerEvent as ReactPointerEvent,\n type RefObject,\n useEffect,\n useMemo,\n useRef,\n} from \"react\";\nimport { isCursorReadout } from \"./pointerTargets\";\n\n/** Options for {@link usePointerPosition}. */\nexport interface UsePointerPositionOptions {\n /** The map region; positions are measured from its top-left corner. */\n readonly hostRef: RefObject<HTMLElement | null>;\n /** Returns the mounted adapter, or null while there is none. */\n readonly getAdapter: () => MapAdapter | null;\n /** Receives the position under a mouse or pen, or null when it leaves the map. */\n readonly onPointerMove?: (latlng: LatLng | null) => void;\n}\n\n/** The handlers to spread on the map region. */\nexport interface UsePointerPosition {\n readonly onPointerMove: (event: ReactPointerEvent) => void;\n readonly onPointerLeave: (event: ReactPointerEvent) => void;\n}\n\n/**\n * Reports the geographic position under a mouse or pen, at most once per\n * animation frame, for the cursor coordinates readout. Touch is ignored,\n * because a finger has no hover position. Leaving the map reports null and\n * drops any pending frame, except onto the cursor readout (`MapReadout`):\n * the readout grows to hold the coordinates and can grow under a resting\n * pointer, so the last position stays. The host then clears it from\n * `MapReadout`'s `onCursorLeave`; a host that shows the position in a\n * readout of its own clears it when the pointer leaves that readout.\n */\nexport function usePointerPosition(\n options: UsePointerPositionOptions,\n): UsePointerPosition {\n const optionsRef = useLatestRef(options);\n const frame = useRef(0);\n const client = useRef({ x: 0, y: 0 });\n\n const handlers = useMemo<UsePointerPosition>(() => {\n const report = () => {\n frame.current = 0;\n const { hostRef, getAdapter, onPointerMove } = optionsRef.current;\n const adapter = getAdapter();\n const host = hostRef.current;\n if (!adapter || !host || !onPointerMove) {\n return;\n }\n const rect = host.getBoundingClientRect();\n onPointerMove(\n adapter.unproject({\n x: client.current.x - rect.left,\n y: client.current.y - rect.top,\n }),\n );\n };\n return {\n onPointerMove: (event) => {\n if (\n event.pointerType === \"touch\" ||\n !optionsRef.current.onPointerMove\n ) {\n return;\n }\n client.current = { x: event.clientX, y: event.clientY };\n if (frame.current === 0) {\n frame.current = requestAnimationFrame(report);\n }\n },\n onPointerLeave: (event) => {\n if (\n event.pointerType === \"touch\" ||\n isCursorReadout(event.relatedTarget)\n ) {\n return;\n }\n cancelAnimationFrame(frame.current);\n frame.current = 0;\n optionsRef.current.onPointerMove?.(null);\n },\n };\n }, [optionsRef]);\n\n useEffect(\n () => () => {\n cancelAnimationFrame(frame.current);\n frame.current = 0;\n },\n [],\n );\n\n return handlers;\n}\n"],"mappings":";;;;;;;;;;;;;;AAuCA,SAAgB,mBACd,SACoB;CACpB,MAAM,aAAa,aAAa,OAAO;CACvC,MAAM,QAAQ,OAAO,CAAC;CACtB,MAAM,SAAS,OAAO;EAAE,GAAG;EAAG,GAAG;CAAE,CAAC;CAEpC,MAAM,WAAW,cAAkC;EACjD,MAAM,eAAe;GACnB,MAAM,UAAU;GAChB,MAAM,EAAE,SAAS,YAAY,kBAAkB,WAAW;GAC1D,MAAM,UAAU,WAAW;GAC3B,MAAM,OAAO,QAAQ;GACrB,IAAI,CAAC,WAAW,CAAC,QAAQ,CAAC,eACxB;GAEF,MAAM,OAAO,KAAK,sBAAsB;GACxC,cACE,QAAQ,UAAU;IAChB,GAAG,OAAO,QAAQ,IAAI,KAAK;IAC3B,GAAG,OAAO,QAAQ,IAAI,KAAK;GAC7B,CAAC,CACH;EACF;EACA,OAAO;GACL,gBAAgB,UAAU;IACxB,IACE,MAAM,gBAAgB,WACtB,CAAC,WAAW,QAAQ,eAEpB;IAEF,OAAO,UAAU;KAAE,GAAG,MAAM;KAAS,GAAG,MAAM;IAAQ;IACtD,IAAI,MAAM,YAAY,GACpB,MAAM,UAAU,sBAAsB,MAAM;GAEhD;GACA,iBAAiB,UAAU;IACzB,IACE,MAAM,gBAAgB,WACtB,gBAAgB,MAAM,aAAa,GAEnC;IAEF,qBAAqB,MAAM,OAAO;IAClC,MAAM,UAAU;IAChB,WAAW,QAAQ,gBAAgB,IAAI;GACzC;EACF;CACF,GAAG,CAAC,UAAU,CAAC;CAEf,sBACc;EACV,qBAAqB,MAAM,OAAO;EAClC,MAAM,UAAU;CAClB,GACA,CAAC,CACH;CAEA,OAAO;AACT"}
|