@defra/interactive-map 0.0.52-alpha → 0.0.54-alpha

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.
Files changed (194) hide show
  1. package/dist/css/index.css +1 -1
  2. package/dist/esm/im-core.js +1 -1
  3. package/dist/esm/im-shell.js +1 -1
  4. package/dist/umd/im-core.js +1 -1
  5. package/dist/umd/index.js +1 -1
  6. package/docs/api/context.md +30 -12
  7. package/docs/api/marker-config.md +16 -8
  8. package/docs/api/symbol-config.md +24 -9
  9. package/docs/api/symbol-registry.md +49 -14
  10. package/docs/api.md +11 -3
  11. package/docs/plugins/datasets.md +6 -10
  12. package/docs/plugins/draw.md +2 -1
  13. package/docs/plugins/map-key.md +47 -4
  14. package/package.json +1 -1
  15. package/plugins/beta/map-styles/dist/esm/im-map-styles-plugin.js +1 -1
  16. package/plugins/beta/map-styles/dist/umd/im-map-styles-plugin.js +1 -1
  17. package/plugins/beta/map-styles/dist/umd/index.js +1 -1
  18. package/plugins/datasets/dist/esm/im-datasets-ml-adapter.js +1 -1
  19. package/plugins/datasets/dist/esm/im-datasets-ol-adapter.js +1 -1
  20. package/plugins/datasets/dist/esm/im-datasets-plugin.js +1 -1
  21. package/plugins/datasets/dist/esm/mapboxStyleLayerAdapter.js +1 -1
  22. package/plugins/datasets/dist/umd/im-datasets-ml-adapter.js +1 -1
  23. package/plugins/datasets/dist/umd/im-datasets-ol-adapter.js +1 -1
  24. package/plugins/datasets/dist/umd/im-datasets-plugin.js +1 -1
  25. package/plugins/datasets/src/adapters/esri/esriLayerAdapter.js +6 -6
  26. package/plugins/datasets/src/adapters/esri/registry/esriDataset.test.js +1 -1
  27. package/plugins/datasets/src/adapters/maplibre/layerBuilders.js +10 -10
  28. package/plugins/datasets/src/adapters/maplibre/layerBuilders.test.js +22 -6
  29. package/plugins/datasets/src/adapters/maplibre/maplibreLayerAdapter.js +9 -3
  30. package/plugins/datasets/src/adapters/maplibre/maplibreLayerAdapter.test.js +18 -3
  31. package/plugins/datasets/src/adapters/maplibre/registry/mapLibreDataset.js +3 -3
  32. package/plugins/datasets/src/adapters/maplibre/registry/mapLibreDataset.test.js +20 -27
  33. package/plugins/datasets/src/adapters/openlayers/canvasPatternStyle.js +16 -70
  34. package/plugins/datasets/src/adapters/openlayers/canvasPatternStyle.test.js +30 -115
  35. package/plugins/datasets/src/adapters/openlayers/layerBuilders.js +11 -13
  36. package/plugins/datasets/src/adapters/openlayers/layerBuilders.test.js +11 -3
  37. package/plugins/datasets/src/adapters/openlayers/openlayersLayerAdapter.js +48 -19
  38. package/plugins/datasets/src/adapters/openlayers/openlayersLayerAdapter.test.js +101 -13
  39. package/plugins/datasets/src/adapters/openlayers/registry/openLayersDataset.js +27 -28
  40. package/plugins/datasets/src/adapters/openlayers/registry/openLayersDataset.test.js +51 -32
  41. package/plugins/datasets/src/fetch/createDynamicSource.js +2 -2
  42. package/plugins/datasets/src/initialise/DatasetsInit.jsx +2 -2
  43. package/plugins/datasets/src/initialise/defaults.js +16 -27
  44. package/plugins/datasets/src/initialise/defaults.test.js +25 -57
  45. package/plugins/datasets/src/initialise/initialiseDatasets.js +1 -4
  46. package/plugins/datasets/src/reducers/__data__/demoDatasets.js +1 -3
  47. package/plugins/datasets/src/reducers/mappedDatasetsReducer.test.js +1 -1
  48. package/plugins/datasets/src/registry/dataset.js +23 -17
  49. package/plugins/datasets/src/registry/dataset.test.js +114 -11
  50. package/plugins/datasets/src/registry/mapboxStyleDataset.js +1 -1
  51. package/plugins/datasets/src/registry/mapboxStyleDataset.test.js +1 -1
  52. package/plugins/draw/dist/esm/im-draw-ml-adapter.js +1 -1
  53. package/plugins/draw/dist/esm/im-draw-ol-adapter.js +1 -1
  54. package/plugins/draw/dist/esm/im-draw-plugin.js +1 -1
  55. package/plugins/draw/dist/esm/index.js +1 -1
  56. package/plugins/draw/dist/esm/liveDrawChecks.js +1 -1
  57. package/plugins/draw/dist/umd/im-draw-ml-adapter.js +1 -1
  58. package/plugins/draw/dist/umd/im-draw-ol-adapter.js +1 -1
  59. package/plugins/draw/dist/umd/im-draw-plugin.js +1 -1
  60. package/plugins/draw/dist/umd/index.js +1 -1
  61. package/plugins/draw/src/DrawInit.jsx +7 -5
  62. package/plugins/draw/src/DrawInit.test.jsx +18 -2
  63. package/plugins/draw/src/adapters/maplibre/MaplibreDrawAdapter.js +11 -5
  64. package/plugins/draw/src/adapters/maplibre/MaplibreDrawAdapter.test.js +27 -11
  65. package/plugins/draw/src/adapters/maplibre/mapboxDraw.js +12 -9
  66. package/plugins/draw/src/adapters/maplibre/mapboxDraw.test.js +18 -0
  67. package/plugins/draw/src/adapters/maplibre/pointSymbolImages.js +81 -56
  68. package/plugins/draw/src/adapters/maplibre/pointSymbolImages.test.js +149 -80
  69. package/plugins/draw/src/adapters/openlayers/OLDrawAdapter.js +7 -6
  70. package/plugins/draw/src/adapters/openlayers/OLDrawAdapter.test.js +22 -25
  71. package/plugins/draw/src/adapters/openlayers/core/OLDrawManager.js +26 -8
  72. package/plugins/draw/src/adapters/openlayers/core/OLDrawManager.test.js +58 -16
  73. package/plugins/draw/src/adapters/openlayers/core/styles.js +33 -20
  74. package/plugins/draw/src/adapters/openlayers/core/styles.test.js +47 -21
  75. package/plugins/draw/src/adapters/openlayers/olDraw.js +16 -12
  76. package/plugins/draw/src/adapters/openlayers/olDraw.test.js +27 -32
  77. package/plugins/draw/src/adapters/openlayers/point/pointSymbolImages.js +44 -49
  78. package/plugins/draw/src/adapters/openlayers/point/pointSymbolImages.test.js +100 -119
  79. package/plugins/draw/src/defaults.js +2 -6
  80. package/plugins/draw/src/utils/stripInternalProperties.test.js +2 -1
  81. package/plugins/draw/src/utils/symbolKeys.js +3 -4
  82. package/plugins/interact/dist/esm/im-interact-plugin.js +1 -1
  83. package/plugins/interact/dist/umd/im-interact-plugin.js +1 -1
  84. package/plugins/interact/dist/umd/index.js +1 -1
  85. package/plugins/map-key/dist/css/index.css +1 -1
  86. package/plugins/map-key/dist/esm/im-map-key-plugin.js +1 -1
  87. package/plugins/map-key/dist/umd/im-map-key-plugin.js +1 -2
  88. package/plugins/map-key/dist/umd/index.js +1 -1
  89. package/plugins/map-key/src/components/Key/Key.jsx +8 -4
  90. package/plugins/map-key/src/components/Key/Key.module.scss +144 -53
  91. package/plugins/map-key/src/components/Key/KeyGroupItem.jsx +36 -3
  92. package/plugins/map-key/src/components/Key/KeyGroupItem.test.jsx +44 -2
  93. package/plugins/map-key/src/components/Key/KeyItem.jsx +2 -2
  94. package/plugins/map-key/src/components/Key/KeySvg.jsx +19 -18
  95. package/plugins/map-key/src/components/Key/KeySvg.test.jsx +25 -18
  96. package/plugins/map-key/src/components/Key/KeySvgPattern.jsx +1 -2
  97. package/plugins/map-key/src/components/Key/KeySvgPattern.test.jsx +3 -1
  98. package/plugins/map-key/src/components/Key/KeySvgRamp.jsx +35 -6
  99. package/plugins/map-key/src/components/Key/KeySvgRamp.test.jsx +56 -16
  100. package/plugins/map-key/src/components/Key/KeySvgSymbol.jsx +26 -12
  101. package/plugins/map-key/src/components/Key/KeySvgSymbol.test.jsx +24 -74
  102. package/plugins/map-key/src/components/Key/MapKey.jsx +3 -1
  103. package/plugins/map-key/src/components/Key/svgProperties.js +8 -2
  104. package/plugins/map-key/src/components/Key/useRampOrientation.js +61 -0
  105. package/plugins/map-key/src/components/Key/useRampOrientation.test.jsx +155 -0
  106. package/plugins/map-key/src/index.js +34 -0
  107. package/plugins/map-key/src/reducers/mergeKeyGroupItems.test.js +15 -0
  108. package/plugins/map-key/src/registry/index.js +0 -2
  109. package/plugins/map-key/src/registry/index.test.js +1 -9
  110. package/plugins/map-key/src/utils/groupStyles.js +5 -2
  111. package/plugins/menu/src/registry/index.js +0 -2
  112. package/plugins/menu/src/registry/index.test.js +1 -9
  113. package/plugins/search/dist/esm/im-search-plugin.js +1 -1
  114. package/plugins/search/dist/umd/im-search-plugin.js +1 -1
  115. package/plugins/search/dist/umd/index.js +1 -1
  116. package/plugins/search/src/Search.jsx +0 -1
  117. package/plugins/search/src/components/Form/Form.jsx +3 -3
  118. package/plugins/search/src/components/Suggestions/Suggestions.jsx +2 -0
  119. package/plugins/search/src/components/Suggestions/Suggestions.test.jsx +6 -0
  120. package/plugins/search/src/defaults.js +3 -0
  121. package/plugins/search/src/events/formHandlers.js +3 -9
  122. package/plugins/search/src/events/formHandlers.test.js +6 -9
  123. package/plugins/search/src/events/inputHandlers.js +8 -2
  124. package/plugins/search/src/events/inputHandlers.test.js +20 -0
  125. package/plugins/search/src/events/suggestionHandlers.js +3 -5
  126. package/plugins/search/src/events/suggestionHandlers.test.js +5 -8
  127. package/plugins/search/src/utils/showResult.js +9 -0
  128. package/plugins/search/src/utils/showResult.test.js +16 -0
  129. package/providers/beta/esri/dist/esm/im-esri-provider.js +1 -1
  130. package/providers/beta/openlayers/dist/esm/im-openlayers-provider.js +1 -1
  131. package/providers/beta/openlayers/dist/umd/im-openlayers-provider.js +1 -1
  132. package/providers/beta/openlayers/dist/umd/index.js +1 -1
  133. package/providers/beta/openlayers/src/openlayersProvider.js +102 -2
  134. package/providers/beta/openlayers/src/openlayersProvider.test.js +95 -0
  135. package/providers/beta/openlayers/src/utils/highlightFeatures.js +20 -25
  136. package/providers/beta/openlayers/src/utils/highlightFeatures.test.js +73 -38
  137. package/providers/beta/openlayers/src/utils/patternImages.js +61 -0
  138. package/providers/beta/openlayers/src/utils/patternImages.test.js +74 -0
  139. package/providers/beta/openlayers/src/utils/symbolImages.js +103 -95
  140. package/providers/beta/openlayers/src/utils/symbolImages.test.js +136 -85
  141. package/providers/mapProvider.js +53 -1
  142. package/providers/mapProvider.test.js +6 -0
  143. package/providers/maplibre/dist/esm/im-maplibre-provider.js +1 -1
  144. package/providers/maplibre/dist/umd/im-maplibre-provider.js +1 -1
  145. package/providers/maplibre/dist/umd/index.js +1 -1
  146. package/providers/maplibre/src/maplibreProvider.js +50 -17
  147. package/providers/maplibre/src/maplibreProvider.test.js +74 -20
  148. package/providers/maplibre/src/utils/highlightFeatures.js +19 -14
  149. package/providers/maplibre/src/utils/highlightFeatures.test.js +20 -14
  150. package/providers/maplibre/src/utils/patternImages.js +46 -29
  151. package/providers/maplibre/src/utils/patternImages.test.js +52 -4
  152. package/providers/maplibre/src/utils/symbolImages.js +80 -39
  153. package/providers/maplibre/src/utils/symbolImages.test.js +157 -54
  154. package/src/App/components/Hints/Hints.module.scss +1 -1
  155. package/src/App/components/Markers/Markers.jsx +16 -25
  156. package/src/App/components/Markers/Markers.test.jsx +34 -48
  157. package/src/App/components/Markers/SymbolLabelMarker.jsx +1 -1
  158. package/src/App/components/Markers/SymbolMarker.jsx +1 -1
  159. package/src/App/components/Viewport/Viewport.jsx +6 -4
  160. package/src/App/components/Viewport/Viewport.test.jsx +18 -1
  161. package/src/App/hooks/useMapAnnouncements.js +7 -2
  162. package/src/App/hooks/useMapAnnouncements.test.js +13 -2
  163. package/src/App/hooks/useModalPanelBehaviour.js +3 -1
  164. package/src/App/initialiseApp.js +1 -1
  165. package/src/App/store/ServiceProvider.jsx +12 -3
  166. package/src/App/store/ServiceProvider.test.jsx +39 -0
  167. package/src/InteractiveMap/InteractiveMap.js +3 -3
  168. package/src/config/appConfig.js +1 -5
  169. package/src/config/mapSizeScales.js +8 -0
  170. package/src/config/symbolConfig.js +101 -25
  171. package/src/services/mapFocus.js +64 -0
  172. package/src/services/mapFocus.test.js +76 -0
  173. package/src/services/patternRegistry.js +97 -76
  174. package/src/services/patternRegistry.test.js +19 -1
  175. package/src/services/symbolComposer.js +184 -0
  176. package/src/services/symbolComposer.test.js +132 -0
  177. package/src/services/symbolRegistry.js +326 -155
  178. package/src/services/symbolRegistry.test.js +334 -32
  179. package/src/types.js +11 -5
  180. package/src/utils/detectInterfaceType.js +16 -5
  181. package/src/utils/detectInterfaceType.test.js +32 -0
  182. package/src/utils/hashString.js +23 -0
  183. package/src/utils/hashString.test.js +29 -0
  184. package/src/utils/imageDataCache.js +43 -0
  185. package/src/utils/imageDataCache.test.js +38 -0
  186. package/src/utils/pathBounds.js +330 -0
  187. package/src/utils/pathBounds.test.js +145 -0
  188. package/src/utils/patternUtils.js +0 -8
  189. package/src/utils/patternUtils.test.js +0 -25
  190. package/src/utils/recentImageSets.js +35 -0
  191. package/src/utils/recentImageSets.test.js +46 -0
  192. package/src/utils/symbolUtils.js +26 -29
  193. package/src/utils/symbolUtils.test.js +27 -41
  194. package/plugins/map-key/dist/umd/im-map-key-plugin.js.LICENSE.txt +0 -1
@@ -1,5 +1,9 @@
1
+ import { RecentImageSets } from '../../../../src/utils/recentImageSets.js'
2
+
1
3
  const ANCHOR_LOW = 0.25
2
4
  const ANCHOR_HIGH = 0.75
5
+ const ANCHOR_CENTRE = 0.5
6
+ const HUNDREDTHS = 100
3
7
 
4
8
  // ─── MapLibre-specific anchor conversion ──────────────────────────────────────
5
9
 
@@ -39,10 +43,10 @@ export const anchorToMaplibre = ([ax, ay]) => {
39
43
  // The discrete fraction (0, 0.5 or 1) icon-anchor actually renders a given axis at — the
40
44
  // same left/right/top/bottom/center snapping xAnchor/yAnchor above already do, expressed as
41
45
  // a number instead of a string so it can be compared against the true, unsnapped fraction.
42
- const discreteFraction = (a) => {
43
- if (a <= ANCHOR_LOW) { return 0 }
44
- if (a >= ANCHOR_HIGH) { return 1 }
45
- return 0.5
46
+ const discreteFraction = (fraction) => {
47
+ if (fraction <= ANCHOR_LOW) { return 0 }
48
+ if (fraction >= ANCHOR_HIGH) { return 1 }
49
+ return ANCHOR_CENTRE
46
50
  }
47
51
 
48
52
  /**
@@ -61,7 +65,7 @@ const discreteFraction = (a) => {
61
65
  */
62
66
  // Rounded to 2dp: sub-hundredth-of-a-pixel precision is meaningless for rendering, and
63
67
  // without it fractions like 0.8 produce float noise (8.799999999999997, not 8.8).
64
- const round2dp = (n) => Math.round(n * 100) / 100
68
+ const round2dp = (value) => Math.round(value * HUNDREDTHS) / HUNDREDTHS
65
69
 
66
70
  export const anchorToMaplibreOffset = ([ax, ay], viewBox) => {
67
71
  const [,, width, height] = viewBox.split(' ').map(Number)
@@ -72,48 +76,85 @@ export const anchorToMaplibreOffset = ([ax, ay], viewBox) => {
72
76
  }
73
77
 
74
78
  /**
75
- * Register normal, active (both rings) and selected (black ring) symbol images.
76
- * Skips images that are already registered (safe to call on style change).
77
- * Merges into `map._activeSymbolImageMap` (normal→active) and `map._selectedSymbolImageMap`
78
- * (normal→selected) rather than replacing them — more than one caller (e.g. the datasets
79
- * and draw plugins) can register symbols on the same map, and each call must add to that
80
- * shared registry, not wipe out whatever another caller already registered there.
79
+ * The icon-anchor and icon-offset layout properties that place a symbol image on its point.
81
80
  *
82
- * @param {Object} map - MapLibre map instance
83
- * @param {Object[]} styleArray - an array of symbol configs
84
- * @param {Object} mapStyle - Current map style config (provides id, selectedColor, haloColor)
85
- * @param {Object} symbolRegistry
86
- * @param {number} [pixelRatio=2] - Device pixel ratio × map size scale factor (computed by caller)
87
- * @returns {Promise<void>}
81
+ * @param {Object} symbolDef - a sized symbol definition (symbolRegistry.getSymbolDef)
82
+ * @returns {{ 'icon-anchor': string, 'icon-offset': number[] }}
88
83
  */
89
- export const addSymbolsToMap = async (map, styleArray, mapStyle, symbolRegistry, pixelRatio = 2) => {
90
- if (!styleArray.length) {
91
- return
84
+ export const getSymbolIconLayout = ({ anchor, viewBox }) => ({
85
+ 'icon-anchor': anchorToMaplibre(anchor),
86
+ 'icon-offset': anchorToMaplibreOffset(anchor, viewBox)
87
+ })
88
+
89
+ /**
90
+ * The MapLibre provider's record of its registered symbol images. The images themselves live in
91
+ * the map's own image store (map.addImage); this keeps which active (both rings) and selected
92
+ * (black ring) image belongs to each normal one. One per map, shared by every caller that
93
+ * registers symbols on it (e.g. the datasets and draw plugins).
94
+ *
95
+ * Images are kept for the current and previous map size and style; ones only older sizes and
96
+ * styles used are removed from the map, so they don't build up as the map size or style changes.
97
+ */
98
+ export class SymbolImageVariants {
99
+ activeImageIds = new Map() // normal imageId → active imageId
100
+ selectedImageIds = new Map() // normal imageId → selected imageId
101
+ recentImageSets = new RecentImageSets()
102
+
103
+ /** @param {Object} map - MapLibre map instance */
104
+ constructor (map) {
105
+ this.map = map
92
106
  }
93
107
 
94
- map._activeSymbolImageMap ??= {}
95
- map._selectedSymbolImageMap ??= {}
108
+ /** A symbol's active (keyboard cursor) variant, from its normal imageId */
109
+ getActiveImageId (normalId) {
110
+ return this.activeImageIds.get(normalId) ?? null
111
+ }
96
112
 
97
- await Promise.all(styleArray.flatMap(config => {
98
- const normalId = symbolRegistry.getSymbolImageId(config, mapStyle, false, pixelRatio)
99
- const activeId = symbolRegistry.getSymbolImageId(config, mapStyle, true, pixelRatio)
100
- if (normalId && activeId) {
101
- map._activeSymbolImageMap[normalId] = activeId
113
+ /** A symbol's selected variant, from its normal imageId */
114
+ getSelectedImageId (normalId) {
115
+ return this.selectedImageIds.get(normalId) ?? null
116
+ }
117
+
118
+ removeImage (imageId) {
119
+ if (this.map.hasImage(imageId)) {
120
+ this.map.removeImage(imageId)
102
121
  }
103
- return ['normal', 'active', 'selected'].map(async (variant) => {
104
- const imageId = variant === 'active' ? activeId : normalId
105
- if (variant !== 'selected' && (!imageId || map.hasImage(imageId))) {
106
- return
122
+ this.activeImageIds.delete(imageId)
123
+ this.selectedImageIds.delete(imageId)
124
+ }
125
+
126
+ /**
127
+ * Rasterises and adds each symbol's normal, active and selected images to the map, and maps
128
+ * its normal imageId to the other two. Images the map already has are reused. Then removes
129
+ * images only map sizes and styles older than the previous one used.
130
+ *
131
+ * @param {Object[]} styleArray - an array of symbol configs
132
+ * @param {Object} mapStyle - Current map style config (provides id, selectedColor, haloColor)
133
+ * @param {Object} symbolRegistry
134
+ * @param {number} pixelRatio - Device pixel ratio × map size scale factor
135
+ * @returns {Promise<void>}
136
+ */
137
+ async registerSymbols (styleArray, mapStyle, symbolRegistry, pixelRatio) {
138
+ const { map } = this
139
+ const imageIds = await Promise.all(styleArray.map(async (config) => {
140
+ const variantIds = symbolRegistry.getSymbolImageIds(config, mapStyle, pixelRatio)
141
+ if (!variantIds) {
142
+ return []
107
143
  }
108
- const result = await symbolRegistry.rasteriseSymbolImage(config, mapStyle, variant, pixelRatio)
109
- if (result) {
110
- if (variant === 'selected' && normalId) {
111
- map._selectedSymbolImageMap[normalId] = result.imageId
144
+ this.activeImageIds.set(variantIds.normal, variantIds.active)
145
+ this.selectedImageIds.set(variantIds.normal, variantIds.selected)
146
+ await Promise.all(Object.entries(variantIds).map(async ([variant, imageId]) => {
147
+ if (map.hasImage(imageId)) {
148
+ return
112
149
  }
113
- if (!map.hasImage(result.imageId)) {
150
+ const result = await symbolRegistry.rasteriseSymbolImage(config, mapStyle, variant, pixelRatio)
151
+ if (result && !map.hasImage(result.imageId)) {
114
152
  map.addImage(result.imageId, result.imageData, { pixelRatio })
115
153
  }
116
- }
117
- })
118
- }))
154
+ }))
155
+ return Object.values(variantIds)
156
+ }))
157
+ this.recentImageSets.add(`${mapStyle?.id}|${pixelRatio}`, imageIds.flat())
158
+ .forEach((imageId) => this.removeImage(imageId))
159
+ }
119
160
  }
@@ -1,5 +1,7 @@
1
- import { anchorToMaplibre, anchorToMaplibreOffset, addSymbolsToMap } from './symbolImages.js'
2
- import { symbolRegistry } from '../../../../src/services/symbolRegistry.js'
1
+ import { anchorToMaplibre, anchorToMaplibreOffset, getSymbolIconLayout, SymbolImageVariants } from './symbolImages.js'
2
+ import { createSymbolRegistry } from '../../../../src/services/symbolRegistry.js'
3
+
4
+ const symbolRegistry = createSymbolRegistry()
3
5
 
4
6
  beforeAll(() => {
5
7
  globalThis.URL.createObjectURL = jest.fn(() => 'blob:mock')
@@ -114,104 +116,123 @@ describe('anchorToMaplibreOffset', () => {
114
116
  })
115
117
  })
116
118
 
117
- // ─── addSymbolsToMap ──────────────────────────────────────────────────────────
119
+ // ─── getSymbolIconLayout ──────────────────────────────────────────────────────
120
+
121
+ describe('getSymbolIconLayout', () => {
122
+ it('snaps to the nearest icon-anchor and offsets the rest of the way', () => {
123
+ expect(getSymbolIconLayout({ anchor: [0.5, 0.9], viewBox: '0 0 44 44' })).toEqual({ 'icon-anchor': 'bottom', 'icon-offset': [0, 4.4] })
124
+ })
125
+
126
+ it('places a sized built-in symbol on its own anchor point', () => {
127
+ const symbolDef = symbolRegistry.getSymbolDef({ symbol: 'pin' })
128
+ const [, , width, height] = symbolDef.viewBox.split(' ').map(Number)
129
+ const { 'icon-offset': [offsetX, offsetY] } = getSymbolIconLayout(symbolDef)
130
+ // icon-offset moves the image, so the point lands that far back from the snapped edge
131
+ expect(width / 2 - offsetX).toBeCloseTo(symbolDef.anchor[0] * width)
132
+ expect(height - offsetY).toBeCloseTo(symbolDef.anchor[1] * height)
133
+ })
134
+ })
135
+
136
+ // ─── SymbolImageVariants ──────────────────────────────────────────────────────
118
137
 
119
138
  const makeMap = (existingIds = []) => ({
120
- _activeSymbolImageMap: {},
121
- _selectedSymbolImageMap: {},
122
139
  hasImage: jest.fn((id) => existingIds.includes(id)),
123
140
  addImage: jest.fn()
124
141
  })
125
142
  const STYLE_ID = 'test'
126
143
  const mapStyle = { id: STYLE_ID }
144
+ const PIXEL_RATIO = 2
127
145
 
128
- describe('addSymbolsToMap — registration', () => {
129
- it('returns early and does not touch map for empty configs', async () => {
146
+ // Registers the configs with a fresh SymbolImageVariants for the map, and returns it
147
+ const register = async (map, configs, pixelRatio = PIXEL_RATIO, symbolImages = new SymbolImageVariants(map)) => {
148
+ await symbolImages.registerSymbols(configs, mapStyle, symbolRegistry, pixelRatio)
149
+ return symbolImages
150
+ }
151
+
152
+ describe('SymbolImageVariants — registration', () => {
153
+ it('does not touch the map for empty configs', async () => {
130
154
  const map = makeMap()
131
- await addSymbolsToMap(map, [], mapStyle, symbolRegistry)
155
+ await register(map, [])
132
156
  expect(map.hasImage).not.toHaveBeenCalled()
133
157
  expect(map.addImage).not.toHaveBeenCalled()
134
158
  })
135
159
 
136
- it('merges into existing _activeSymbolImageMap/_selectedSymbolImageMap rather than replacing them', async () => {
137
- // More than one caller (datasets, draw, ...) can register symbols on the same map —
138
- // a later call must add to the shared registry, not wipe an earlier caller's entries.
160
+ it('adds to the variants already registered rather than replacing them', async () => {
161
+ // More than one caller (datasets, draw, ...) registers symbols on the same map
139
162
  const map = makeMap()
140
- map._activeSymbolImageMap = { 'other-normal-id': 'other-active-id' }
141
- map._selectedSymbolImageMap = { 'other-normal-id': 'other-selected-id' }
142
- await addSymbolsToMap(map, [{ symbol: 'pin' }], mapStyle, symbolRegistry)
143
- expect(map._activeSymbolImageMap['other-normal-id']).toBe('other-active-id')
144
- expect(map._selectedSymbolImageMap['other-normal-id']).toBe('other-selected-id')
145
- const normalId = symbolRegistry.getSymbolImageId({ symbol: 'pin' }, mapStyle, false)
146
- expect(map._activeSymbolImageMap).toHaveProperty(normalId)
163
+ const symbolImages = await register(map, [{ symbol: 'circle' }])
164
+ await register(map, [{ symbol: 'pin' }], PIXEL_RATIO, symbolImages)
165
+ const circleId = symbolRegistry.getSymbolImageId({ symbol: 'circle' }, mapStyle, 'normal', PIXEL_RATIO)
166
+ const pinId = symbolRegistry.getSymbolImageId({ symbol: 'pin' }, mapStyle, 'normal', PIXEL_RATIO)
167
+ expect(symbolImages.getActiveImageId(circleId)).not.toBeNull()
168
+ expect(symbolImages.getSelectedImageId(circleId)).not.toBeNull()
169
+ expect(symbolImages.getActiveImageId(pinId)).not.toBeNull()
147
170
  })
148
171
 
149
172
  it('calls addImage for normal, active and selected variants', async () => {
150
173
  const map = makeMap()
151
- await addSymbolsToMap(map, [{ symbol: 'pin' }], mapStyle, symbolRegistry)
174
+ await register(map, [{ symbol: 'pin' }])
152
175
  expect(map.addImage).toHaveBeenCalledTimes(3) // NOSONAR S109 — normal, active, selected
153
176
  expect(map.addImage).toHaveBeenCalledWith(expect.stringMatching(/^symbol-[a-z0-9]+-\d+(\.\d+)?x$/), expect.any(Object), { pixelRatio: 2 })
154
177
  expect(map.addImage).toHaveBeenCalledWith(expect.stringMatching(/^symbol-act-[a-z0-9]+-\d+(\.\d+)?x$/), expect.any(Object), { pixelRatio: 2 })
155
178
  expect(map.addImage).toHaveBeenCalledWith(expect.stringMatching(/^symbol-sel-[a-z0-9]+-\d+(\.\d+)?x$/), expect.any(Object), { pixelRatio: 2 })
156
179
  })
157
180
 
158
- it('populates _activeSymbolImageMap and _selectedSymbolImageMap with normal → variant id pairs', async () => {
159
- const map = makeMap()
160
- await addSymbolsToMap(map, [{ symbol: 'pin' }], mapStyle, symbolRegistry)
161
- const normalId = symbolRegistry.getSymbolImageId({ symbol: 'pin' }, mapStyle, false)
162
- const activeId = symbolRegistry.getSymbolImageId({ symbol: 'pin' }, mapStyle, true)
163
- const selectedId = map._selectedSymbolImageMap[normalId]
164
- expect(map._activeSymbolImageMap[normalId]).toBe(activeId)
181
+ it('maps each normal image id to its active and selected variants', async () => {
182
+ const symbolImages = await register(makeMap(), [{ symbol: 'pin' }])
183
+ const normalId = symbolRegistry.getSymbolImageId({ symbol: 'pin' }, mapStyle, 'normal', PIXEL_RATIO)
184
+ const activeId = symbolRegistry.getSymbolImageId({ symbol: 'pin' }, mapStyle, 'active', PIXEL_RATIO)
185
+ const selectedId = symbolImages.getSelectedImageId(normalId)
186
+ expect(symbolImages.getActiveImageId(normalId)).toBe(activeId)
165
187
  expect(selectedId).toMatch(/^symbol-sel-[a-z0-9]+-\d+(\.\d+)?x$/)
166
188
  })
167
189
 
168
190
  it('skips addImage when all three variant images are already registered', async () => {
169
191
  // Run once to discover the selected image ID (not derivable without rasterising)
170
- const setupMap = makeMap()
171
- await addSymbolsToMap(setupMap, [{ symbol: 'circle' }], mapStyle, symbolRegistry)
172
- const normalId = symbolRegistry.getSymbolImageId({ symbol: 'circle' }, mapStyle, false)
173
- const activeId = symbolRegistry.getSymbolImageId({ symbol: 'circle' }, mapStyle, true)
174
- const selectedId = setupMap._selectedSymbolImageMap[normalId]
192
+ const setup = await register(makeMap(), [{ symbol: 'circle' }])
193
+ const normalId = symbolRegistry.getSymbolImageId({ symbol: 'circle' }, mapStyle, 'normal', PIXEL_RATIO)
194
+ const activeId = symbolRegistry.getSymbolImageId({ symbol: 'circle' }, mapStyle, 'active', PIXEL_RATIO)
195
+ const selectedId = setup.getSelectedImageId(normalId)
175
196
 
176
197
  const map = makeMap([normalId, activeId, selectedId])
177
- await addSymbolsToMap(map, [{ symbol: 'circle' }], mapStyle, symbolRegistry)
198
+ await register(map, [{ symbol: 'circle' }])
178
199
  expect(map.addImage).not.toHaveBeenCalled()
179
200
  })
180
201
 
181
202
  it('processes multiple configs independently', async () => {
182
203
  const map = makeMap()
183
- await addSymbolsToMap(map, [{ symbol: 'pin' }, { symbol: 'circle' }], mapStyle, symbolRegistry)
204
+ const symbolImages = await register(map, [{ symbol: 'pin' }, { symbol: 'circle' }])
184
205
  expect(map.addImage).toHaveBeenCalledTimes(6) // NOSONAR S109 — 2 configs × 3 variants each
185
- expect(Object.keys(map._activeSymbolImageMap)).toHaveLength(2)
186
- expect(Object.keys(map._selectedSymbolImageMap)).toHaveLength(2)
206
+ expect(symbolImages.activeImageIds.size).toBe(2)
207
+ expect(symbolImages.selectedImageIds.size).toBe(2)
187
208
  })
188
209
  })
189
210
 
190
- describe('addSymbolsToMap — null results and caching', () => {
211
+ describe('SymbolImageVariants — null results and caching', () => {
191
212
  it('does not call addImage when rasteriseSymbolImage returns null', async () => {
192
- // getSymbolImageId (called twice — normal + active) needs a real symbolDef to produce imageIds,
193
- // but rasteriseSymbolImage must get undefined from getSymbolDef so it returns null.
194
- // The registry.get call order: [1] getSymbolImageId normal, [2] getSymbolImageId active,
195
- // [3] rasteriseSymbolImage normal, [4] rasteriseSymbolImage active, [5] rasteriseSymbolImage selected.
196
- const pinDef = symbolRegistry.get('pin')
197
- const getSpy = jest.spyOn(symbolRegistry, 'get')
198
- .mockReturnValueOnce(pinDef)
199
- .mockReturnValueOnce(pinDef)
200
- .mockReturnValueOnce(undefined)
201
- .mockReturnValueOnce(undefined)
202
- .mockReturnValueOnce(undefined)
213
+ const rasterise = jest.spyOn(symbolRegistry, 'rasteriseSymbolImage').mockResolvedValue(null)
203
214
  const map = makeMap()
204
- await addSymbolsToMap(map, [{ symbol: 'pin' }], mapStyle, symbolRegistry)
215
+ await register(map, [{ symbol: 'pin' }])
205
216
  expect(map.addImage).not.toHaveBeenCalled()
206
- getSpy.mockRestore()
217
+ rasterise.mockRestore()
218
+ })
219
+
220
+ it('rasterises nothing for a symbol the map already has every image of', async () => {
221
+ const map = makeMap()
222
+ const symbolImages = await register(map, [{ symbol: 'pin' }])
223
+ const rasterise = jest.spyOn(symbolRegistry, 'rasteriseSymbolImage')
224
+ map.hasImage.mockReturnValue(true)
225
+ await register(map, [{ symbol: 'pin' }], PIXEL_RATIO, symbolImages)
226
+ expect(rasterise).not.toHaveBeenCalled()
227
+ rasterise.mockRestore()
207
228
  })
208
229
 
209
230
  it('skips config when symbolDef cannot be resolved', async () => {
210
231
  const map = makeMap()
211
- await addSymbolsToMap(map, [{ symbol: 'no-such-symbol' }], mapStyle, symbolRegistry)
232
+ const symbolImages = await register(map, [{ symbol: 'no-such-symbol' }])
212
233
  expect(map.addImage).not.toHaveBeenCalled()
213
- expect(map._activeSymbolImageMap).toEqual({})
214
- expect(map._selectedSymbolImageMap).toEqual({})
234
+ expect(symbolImages.activeImageIds.size).toBe(0)
235
+ expect(symbolImages.selectedImageIds.size).toBe(0)
215
236
  })
216
237
 
217
238
  it('reuses cached imageData when called again with the same pixelRatio', async () => {
@@ -220,14 +241,14 @@ describe('addSymbolsToMap — null results and caching', () => {
220
241
 
221
242
  const map1 = makeMap()
222
243
  const getContextCallsBefore = HTMLCanvasElement.prototype.getContext.mock.calls.length
223
- await addSymbolsToMap(map1, [{ symbol: 'pin' }], mapStyle, symbolRegistry, uniqueRatio)
244
+ await register(map1, [{ symbol: 'pin' }], uniqueRatio)
224
245
  const getContextCallsAfterFirst = HTMLCanvasElement.prototype.getContext.mock.calls.length
225
246
  // Rasterisation ran — canvas was used
226
247
  expect(getContextCallsAfterFirst).toBeGreaterThan(getContextCallsBefore)
227
248
 
228
249
  // Second call with a fresh map (hasImage → false) but same ratio → cache hit
229
250
  const map2 = makeMap()
230
- await addSymbolsToMap(map2, [{ symbol: 'pin' }], mapStyle, symbolRegistry, uniqueRatio)
251
+ await register(map2, [{ symbol: 'pin' }], uniqueRatio)
231
252
  const getContextCallsAfterSecond = HTMLCanvasElement.prototype.getContext.mock.calls.length
232
253
  // No new canvas — rasterisation was skipped via cache
233
254
  expect(getContextCallsAfterSecond).toBe(getContextCallsAfterFirst)
@@ -235,3 +256,85 @@ describe('addSymbolsToMap — null results and caching', () => {
235
256
  expect(map2.addImage).toHaveBeenCalledTimes(3) // NOSONAR S109 — normal, active, selected
236
257
  })
237
258
  })
259
+
260
+ describe('SymbolImageVariants — removing images from older map sizes and styles', () => {
261
+ // A map that keeps the images added to it, like MapLibre's image store
262
+ const makeImageStore = () => {
263
+ const images = new Set()
264
+ return {
265
+ images,
266
+ hasImage: jest.fn((id) => images.has(id)),
267
+ addImage: jest.fn((id) => images.add(id)),
268
+ removeImage: jest.fn((id) => images.delete(id))
269
+ }
270
+ }
271
+ const pinIds = (symbolImages, pixelRatio) => {
272
+ const normalId = symbolRegistry.getSymbolImageId({ symbol: 'pin' }, mapStyle, 'normal', pixelRatio)
273
+ return [normalId, symbolRegistry.getSymbolImageId({ symbol: 'pin' }, mapStyle, 'active', pixelRatio), symbolImages.getSelectedImageId(normalId)]
274
+ }
275
+
276
+ it('keeps the current and previous size, and removes an older one\'s images when a third arrives', async () => {
277
+ const map = makeImageStore()
278
+ const symbolImages = new SymbolImageVariants(map)
279
+ await register(map, [{ symbol: 'pin' }], 1, symbolImages)
280
+ const oldIds = pinIds(symbolImages, 1)
281
+ await register(map, [{ symbol: 'pin' }], 2, symbolImages)
282
+ oldIds.forEach((imageId) => expect(map.images.has(imageId)).toBe(true))
283
+
284
+ await register(map, [{ symbol: 'pin' }], 3, symbolImages)
285
+ oldIds.forEach((imageId) => expect(map.images.has(imageId)).toBe(false))
286
+ expect(symbolImages.getActiveImageId(oldIds[0])).toBeNull()
287
+ expect(symbolImages.getSelectedImageId(oldIds[0])).toBeNull()
288
+ pinIds(symbolImages, 2).concat(pinIds(symbolImages, 3)).forEach((imageId) => expect(map.images.has(imageId)).toBe(true))
289
+ expect(map.images.size).toBe(6) // NOSONAR S109 — two sizes × normal, active, selected
290
+ })
291
+
292
+ it('keeps an image the map no longer has, e.g. after a style change, without removing it', async () => {
293
+ const map = makeImageStore()
294
+ const symbolImages = new SymbolImageVariants(map)
295
+ await register(map, [{ symbol: 'pin' }], 1, symbolImages)
296
+ map.images.clear()
297
+ await register(map, [{ symbol: 'pin' }], 2, symbolImages)
298
+ await register(map, [{ symbol: 'pin' }], 3, symbolImages)
299
+ expect(map.removeImage).not.toHaveBeenCalled()
300
+ })
301
+ })
302
+
303
+ // End to end across the symbol pipeline: whatever the shape, size or anchor override, MapLibre's
304
+ // snapped icon-anchor plus the icon-offset must land the symbol's true anchor pixel on the
305
+ // coordinate — the same pixel HTML markers and OpenLayers place directly from the fraction.
306
+ describe('anchor placement across symbols, sizes and overrides', () => {
307
+ const ANCHOR_POSITIONS = {
308
+ left: 0, right: 1, top: 0, bottom: 1, center: 0.5
309
+ }
310
+ // icon-anchor names the point of the image placed on the coordinate, as fractions
311
+ const discretePoint = (name) => {
312
+ if (name === 'center') { return [0.5, 0.5] }
313
+ const parts = name.split('-')
314
+ const x = parts.find((p) => p === 'left' || p === 'right')
315
+ const y = parts.find((p) => p === 'top' || p === 'bottom')
316
+ return [x ? ANCHOR_POSITIONS[x] : 0.5, y ? ANCHOR_POSITIONS[y] : 0.5]
317
+ }
318
+ const styles = [
319
+ ...['pin', 'circle', 'square', 'hexagon', 'triangle', 'diamond'].map((symbol) => ({ symbol })),
320
+ { symbolSvgContent: '<rect width="30" height="20"/>', symbolViewBox: '0 0 30 20', symbolAnchor: [0.3, 0.9] }
321
+ ]
322
+ const overrides = [undefined, [0, 0], [0.5, 1], [1, 0.25]]
323
+
324
+ styles.forEach((baseStyle) => {
325
+ ['small', 'medium', 'large'].forEach((symbolSize) => {
326
+ overrides.forEach((symbolAnchor) => {
327
+ const style = { ...baseStyle, symbolSize, ...(symbolAnchor && { symbolAnchor }) }
328
+ it(`${baseStyle.symbol ?? 'custom svg'} ${symbolSize} anchor ${JSON.stringify(symbolAnchor ?? 'default')}`, () => {
329
+ const { viewBox, anchor } = symbolRegistry.getSymbolDef(style)
330
+ const [,, width, height] = viewBox.split(' ').map(Number)
331
+ const [dx, dy] = discretePoint(anchorToMaplibre(anchor))
332
+ const [ox, oy] = anchorToMaplibreOffset(anchor, viewBox)
333
+ // the discrete anchor point, shifted by the offset, is the true anchor pixel (offset is 2dp)
334
+ expect(dx * width - ox).toBeCloseTo(anchor[0] * width, 1)
335
+ expect(dy * height - oy).toBeCloseTo(anchor[1] * height, 1)
336
+ })
337
+ })
338
+ })
339
+ })
340
+ })
@@ -8,7 +8,7 @@
8
8
  left: 0;
9
9
  right: 0;
10
10
  margin-inline: auto;
11
- z-index: 1001;
11
+ z-index: 98; // Above non-modal slots, below .im-o-app__modal (99)
12
12
  width: fit-content;
13
13
  max-width: min(var(--hint-max-width), calc(100% - (2 * var(--primary-gap))));
14
14
  }
@@ -6,43 +6,34 @@ import { useMap } from '../../store/mapContext.js'
6
6
  import { useService } from '../../store/serviceContext.js'
7
7
  import { scaleFactor } from '../../../config/appConfig.js'
8
8
  import { isStandaloneLabel } from '../../../utils/symbolUtils.js'
9
+ import { DEFAULT_SYMBOL_ANCHOR, DEFAULT_SYMBOL_VIEWBOX } from '../../../config/symbolConfig.js'
9
10
  import { EVENTS } from '../../../config/events.js'
10
11
  import LabelMarker from './LabelMarker.jsx'
11
12
  import SymbolLabelMarker from './SymbolLabelMarker.jsx'
12
13
  import SymbolMarker from './SymbolMarker.jsx'
13
14
 
14
15
  // Marker properties handled internally — excluded from style value resolution
15
- const INTERNAL_KEYS = new Set(['id', 'coords', 'x', 'y', 'isVisible', 'symbol', 'symbolSvgContent', 'viewBox', 'anchor', 'selectedColor', 'label', 'showLabel'])
16
+ const INTERNAL_KEYS = new Set(['id', 'coords', 'x', 'y', 'isVisible', 'symbol', 'svgContent', 'size', 'viewBox', 'anchor', 'selectedColor', 'label', 'showLabel'])
16
17
 
17
- const resolveSymbolDef = (marker, defaults, symbolRegistry) => {
18
- const svgContent = marker.symbolSvgContent || defaults.symbolSvgContent
19
- return svgContent
20
- ? { svg: svgContent }
21
- : symbolRegistry.get(marker.symbol || defaults.symbol)
18
+ // The keyboard cursor's rings take priority over the selected ring
19
+ const markerVariant = (isSelected, isActive) => {
20
+ if (isActive) {
21
+ return 'active'
22
+ }
23
+ return isSelected ? 'selected' : 'normal'
22
24
  }
23
25
 
24
- const resolveViewBox = (marker, defaults, symbolDef) =>
25
- marker.viewBox || defaults.viewBox || symbolDef?.viewBox || '0 0 44 44'
26
-
27
- const resolveAnchor = (marker, defaults, symbolDef) =>
28
- marker.anchor ?? defaults.anchor ?? symbolDef?.anchor ?? [0.5, 0.5]
29
-
30
- const resolveSymbolProps = (marker, defaults, symbolRegistry, mapStyle, mapSize, isSelected, isActive) => {
31
- const symbolDef = resolveSymbolDef(marker, defaults, symbolRegistry)
26
+ // The marker's definition carries the viewBox and anchor for its size (see
27
+ // symbolRegistry.getMarkerSymbolDef)
28
+ const resolveSymbolProps = (marker, defaults, symbolRegistry, mapStyle, mapSize, variant) => {
29
+ const symbolDef = symbolRegistry.getMarkerSymbolDef(marker)
32
30
  const styleValues = Object.fromEntries(
33
31
  Object.entries(marker).filter(([k]) => !INTERNAL_KEYS.has(k))
34
32
  )
35
- let resolvedSvg
36
- if (isActive) {
37
- resolvedSvg = symbolRegistry.resolveActive(symbolDef, styleValues, mapStyle)
38
- } else if (isSelected) {
39
- resolvedSvg = symbolRegistry.resolveSelected(symbolDef, styleValues, mapStyle)
40
- } else {
41
- resolvedSvg = symbolRegistry.resolve(symbolDef, styleValues, mapStyle)
42
- }
43
- const viewBox = resolveViewBox(marker, defaults, symbolDef)
33
+ const resolvedSvg = symbolRegistry.resolveVariant(symbolDef, styleValues, mapStyle, variant)
34
+ const viewBox = symbolDef?.viewBox ?? DEFAULT_SYMBOL_VIEWBOX
44
35
  const [,, svgWidth, svgHeight] = viewBox.split(' ').map(Number)
45
- const anchor = resolveAnchor(marker, defaults, symbolDef)
36
+ const anchor = symbolDef?.anchor ?? DEFAULT_SYMBOL_ANCHOR
46
37
  const shapeId = marker.symbol || defaults.symbol
47
38
  const scale = scaleFactor[mapSize] ?? 1
48
39
  return { resolvedSvg, viewBox, anchor, shapeId, scaledWidth: svgWidth * scale, scaledHeight: svgHeight * scale }
@@ -98,7 +89,7 @@ export const Markers = () => {
98
89
  return <LabelMarker key={marker.id} marker={marker} mapId={id} markerRef={markerRef} />
99
90
  }
100
91
 
101
- const symbolProps = resolveSymbolProps(marker, defaults, symbolRegistry, mapStyle, mapSize, isSelected, isActive)
92
+ const symbolProps = resolveSymbolProps(marker, defaults, symbolRegistry, mapStyle, mapSize, markerVariant(isSelected, isActive))
102
93
 
103
94
  if (marker.showLabel && marker.label) {
104
95
  return <SymbolLabelMarker key={marker.id} marker={marker} mapId={id} markerRef={markerRef} isSelected={isSelected} symbolProps={symbolProps} />