@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
@@ -4,10 +4,12 @@ import { attachAppEvents } from './appEvents.js'
4
4
  import { createMapLabelNavigator } from './utils/labels.js'
5
5
  import { updateHighlightedFeatures } from './utils/highlightFeatures.js'
6
6
  import { queryFeatures } from './utils/queryFeatures.js'
7
- import { addSymbolsToMap } from './utils/symbolImages.js'
8
- import { addPatternsToMap } from './utils/patternImages.js'
7
+ import { SymbolImageVariants, getSymbolIconLayout } from './utils/symbolImages.js'
8
+ import { PatternImages } from './utils/patternImages.js'
9
9
  import { getAreaDimensions, getCardinalMove, getResolution, getPaddedBounds, isGeometryObscured } from './utils/spatial.js'
10
- import { symbolRegistry } from '../../../src/services/symbolRegistry.js'
10
+ import { createSymbolRegistry } from '../../../src/services/symbolRegistry.js'
11
+
12
+ const symbolRegistry = createSymbolRegistry()
11
13
 
12
14
  jest.mock('./defaults.js', () => ({
13
15
  DEFAULTS: { animationDuration: 400, coordinatePrecision: 7 },
@@ -36,8 +38,10 @@ jest.mock('./utils/labels.js', () => ({
36
38
  }))
37
39
  jest.mock('./utils/highlightFeatures.js', () => ({ updateHighlightedFeatures: jest.fn(() => []) }))
38
40
  jest.mock('./utils/queryFeatures.js', () => ({ queryFeatures: jest.fn(() => []) }))
39
- jest.mock('./utils/symbolImages.js', () => ({ addSymbolsToMap: jest.fn(() => Promise.resolve()) }))
40
- jest.mock('./utils/patternImages.js', () => ({ addPatternsToMap: jest.fn(() => Promise.resolve()) }))
41
+ jest.mock('./utils/symbolImages.js', () => ({
42
+ ...jest.requireActual('./utils/symbolImages.js'),
43
+ getSymbolIconLayout: jest.fn(() => ({ 'icon-anchor': 'bottom', 'icon-offset': [0, 2] }))
44
+ }))
41
45
 
42
46
  describe('MapLibreProvider', () => {
43
47
  let map, eventBus, maplibreModule, loadCallback
@@ -275,7 +279,7 @@ describe('MapLibreProvider', () => {
275
279
  expect(queryFeatures).toHaveBeenCalledWith(map, { x: 10, y: 20 }, { radius: 5 })
276
280
  p.updateHighlightedFeatures(['feat'], null, { style: 1 })
277
281
  expect(updateHighlightedFeatures).toHaveBeenCalledWith({
278
- LngLatBounds: maplibreModule.LngLatBounds, map, selectedFeatures: ['feat'], activeFeatures: null, stylesMap: { style: 1 }
282
+ LngLatBounds: maplibreModule.LngLatBounds, map, selectedFeatures: ['feat'], activeFeatures: null, stylesMap: { style: 1 }, symbolImages: p.symbolImages
279
283
  })
280
284
  })
281
285
 
@@ -297,51 +301,101 @@ describe('MapLibreProvider', () => {
297
301
  expect(map.queryRenderedFeatures).toHaveBeenCalledWith(undefined, { layers: ['l1'] })
298
302
  })
299
303
 
300
- test('addSymbolsToMap delegates to utility with map instance', async () => {
304
+ // The provider's own record of registered symbol images, created with its map
305
+ const initWithSymbolImages = async () => {
301
306
  const p = makeProvider()
302
307
  await doInitMap(p)
308
+ const registerSymbols = jest.spyOn(p.symbolImages, 'registerSymbols').mockResolvedValue()
309
+ return { p, registerSymbols }
310
+ }
311
+
312
+ test('owns a SymbolImageVariants for its map', async () => {
313
+ const p = makeProvider()
314
+ await doInitMap(p)
315
+ expect(p.symbolImages).toBeInstanceOf(SymbolImageVariants)
316
+ expect(p.symbolImages.map).toBe(map)
317
+ })
318
+
319
+ test('addSymbolsToMap registers the symbols with its symbol images', async () => {
320
+ const { p, registerSymbols } = await initWithSymbolImages()
303
321
  const configs = [{ symbol: 'pin' }]
304
322
  const mapStyle = { id: 'test', selectedColor: '#0b0c0c' }
305
323
  await p.addSymbolsToMap(configs, mapStyle, symbolRegistry)
306
- expect(addSymbolsToMap).toHaveBeenCalledWith(map, configs, mapStyle, symbolRegistry, expect.any(Number))
324
+ expect(registerSymbols).toHaveBeenCalledWith(configs, mapStyle, symbolRegistry, expect.any(Number))
307
325
  })
308
326
 
309
327
  test('addSymbolsToMap computes pixelRatio from getPixelRatio and mapSize scale factor', async () => {
310
- const p = makeProvider()
311
- await doInitMap(p)
328
+ const { p, registerSymbols } = await initWithSymbolImages()
312
329
  map.getPixelRatio.mockReturnValue(2)
313
330
  p.mapSize = 'medium'
314
331
  await p.addSymbolsToMap([], { id: 'test' }, symbolRegistry)
315
- expect(addSymbolsToMap).toHaveBeenCalledWith(map, [], { id: 'test' }, symbolRegistry, 2)
332
+ expect(registerSymbols).toHaveBeenCalledWith([], { id: 'test' }, symbolRegistry, 2)
316
333
  })
317
334
 
318
- test('addSymbolsToMap falls back to pixelRatio 1 when getPixelRatio returns 0', async () => {
335
+ test('addSymbolsToMap registers at a given pixelRatio instead of the map\'s', async () => {
336
+ const { p, registerSymbols } = await initWithSymbolImages()
337
+ map.getPixelRatio.mockReturnValue(2)
338
+ await p.addSymbolsToMap([], { id: 'test' }, symbolRegistry, 3)
339
+ expect(registerSymbols).toHaveBeenCalledWith([], { id: 'test' }, symbolRegistry, 3)
340
+ })
341
+
342
+ test('getSymbolIconLayout delegates to the symbol image utility', () => {
343
+ const symbolDef = { anchor: [0.5, 0.9], viewBox: '0 0 44 44' }
344
+ expect(makeProvider().getSymbolIconLayout(symbolDef)).toEqual({ 'icon-anchor': 'bottom', 'icon-offset': [0, 2] })
345
+ expect(getSymbolIconLayout).toHaveBeenCalledWith(symbolDef)
346
+ })
347
+
348
+ test('looks up a registered symbol\'s active and selected variants', async () => {
319
349
  const p = makeProvider()
320
350
  await doInitMap(p)
351
+ expect(p.getActiveSymbolImageId('img')).toBeNull()
352
+ expect(p.getSelectedSymbolImageId('img')).toBeNull()
353
+ p.symbolImages.activeImageIds.set('img', 'img-active')
354
+ p.symbolImages.selectedImageIds.set('img', 'img-selected')
355
+ expect(p.getActiveSymbolImageId('img')).toBe('img-active')
356
+ expect(p.getSelectedSymbolImageId('img')).toBe('img-selected')
357
+ })
358
+
359
+ test('addSymbolsToMap falls back to pixelRatio 1 when getPixelRatio returns 0', async () => {
360
+ const { p, registerSymbols } = await initWithSymbolImages()
321
361
  map.getPixelRatio.mockReturnValue(0)
322
362
  p.mapSize = 'small' // scaleFactor['small'] = 1
323
363
  await p.addSymbolsToMap([], { id: 'test' }, symbolRegistry)
324
- expect(addSymbolsToMap).toHaveBeenCalledWith(map, [], { id: 'test' }, symbolRegistry, 1)
364
+ expect(registerSymbols).toHaveBeenCalledWith([], { id: 'test' }, symbolRegistry, 1)
325
365
  })
326
366
 
327
- test('addPatternsToMap falls back to pixelRatio 1 when getPixelRatio returns 0', async () => {
367
+ // The provider's own pattern images, created with its map
368
+ const initWithPatternImages = async () => {
369
+ const p = makeProvider()
370
+ await doInitMap(p)
371
+ const registerPatterns = jest.spyOn(p.patternImages, 'registerPatterns').mockResolvedValue()
372
+ return { p, registerPatterns }
373
+ }
374
+
375
+ test('owns a PatternImages for its map', async () => {
328
376
  const p = makeProvider()
329
377
  await doInitMap(p)
378
+ expect(p.patternImages).toBeInstanceOf(PatternImages)
379
+ expect(p.patternImages.map).toBe(map)
380
+ })
381
+
382
+ test('addPatternsToMap falls back to pixelRatio 1 when getPixelRatio returns 0', async () => {
383
+ const { p, registerPatterns } = await initWithPatternImages()
330
384
  map.getPixelRatio.mockReturnValue(0)
331
385
  const configs = [{ fillPattern: 'dot' }]
332
386
  const registry = {}
333
387
  await p.addPatternsToMap(configs, 'test', registry)
334
- expect(addPatternsToMap).toHaveBeenCalledWith(map, configs, 'test', registry, 1)
388
+ expect(registerPatterns).toHaveBeenCalledWith(configs, 'test', registry, 1)
335
389
  })
336
390
 
337
- test('addPatternsToMap is called with pixelRatio from getPixelRatio', async () => {
338
- const p = makeProvider()
339
- await doInitMap(p)
391
+ test('addPatternsToMap registers at the map\'s pixel ratio, or a given one', async () => {
392
+ const { p, registerPatterns } = await initWithPatternImages()
340
393
  map.getPixelRatio.mockReturnValue(2)
341
- p.mapSize = 'medium'
342
394
  const registry = {}
343
395
  await p.addPatternsToMap([], 'test', registry)
344
- expect(addPatternsToMap).toHaveBeenCalledWith(map, [], 'test', registry, 2)
396
+ expect(registerPatterns).toHaveBeenLastCalledWith([], 'test', registry, 2)
397
+ await p.addPatternsToMap([], 'test', registry, 3)
398
+ expect(registerPatterns).toHaveBeenLastCalledWith([], 'test', registry, 3)
345
399
  })
346
400
 
347
401
  describe('setHoverCursor', () => {
@@ -9,8 +9,11 @@ const SELECTED_PREFIX = 'selected-highlight'
9
9
  // Inner and selected both use the selected colour/width (black, thin)
10
10
  const usesSelectedStyle = (prefix) => prefix === SELECTED_PREFIX || prefix === ACTIVE_INNER_PREFIX
11
11
 
12
- const getActiveImageId = (map, imageId) => map._activeSymbolImageMap?.[imageId] ?? null
13
- const getSelectedImageId = (map, imageId) => map._selectedSymbolImageMap?.[imageId] ?? null
12
+ // Which variant of a symbol a highlight shows: its image id lookup on the provider's
13
+ // symbolImages, and draw's per-feature property holding the same id
14
+ const symbolVariant = (symbolImages, variant) => variant === 'active'
15
+ ? { property: 'user_symbolActiveImageId', getImageId: (imageId) => symbolImages?.getActiveImageId(imageId) ?? null }
16
+ : { property: 'user_symbolSelectedImageId', getImageId: (imageId) => symbolImages?.getSelectedImageId(imageId) ?? null }
14
17
 
15
18
  // Grouped by layerId, not sourceId: mapbox-gl-draw stores every geometry type in the same
16
19
  // cold/hot sources, so a point (symbol layer) and a polygon (fill layer) selected together
@@ -138,14 +141,13 @@ const calculateBounds = (LngLatBounds, renderedFeatures) => {
138
141
  // Draw's point-symbol layer has a per-feature data-driven icon-image, so there's no single
139
142
  // shared image id to reverse-map — pointSymbolImages.js precomputes each point's own active/
140
143
  // selected variant as a property, and the highlight layer just reads that directly.
141
- const applySymbolGeomHighlight = (map, base, sourceId, srcLayer, layerId, filter, getSymbolImageId) => {
144
+ const applySymbolGeomHighlight = (map, base, sourceId, srcLayer, layerId, filter, variant) => {
142
145
  const imageId = map.getLayoutProperty(layerId, ICON_IMAGE)
143
146
  if (Array.isArray(imageId)) {
144
- const property = getSymbolImageId === getActiveImageId ? 'user_symbolActiveImageId' : 'user_symbolSelectedImageId'
145
- applySymbolHighlightLayer(map, `${base}-symbol`, sourceId, srcLayer, layerId, ['get', property], filter)
147
+ applySymbolHighlightLayer(map, `${base}-symbol`, sourceId, srcLayer, layerId, ['get', variant.property], filter)
146
148
  return
147
149
  }
148
- const symbolImageId = getSymbolImageId(map, imageId)
150
+ const symbolImageId = variant.getImageId(imageId)
149
151
  if (symbolImageId) {
150
152
  applySymbolHighlightLayer(map, `${base}-symbol`, sourceId, srcLayer, layerId, symbolImageId, filter)
151
153
  }
@@ -193,7 +195,7 @@ const applyLineHighlight = (map, base, sourceId, srcLayer, lineColor, lineWidth,
193
195
  // active-highlight → yellow active ring (stroke only)
194
196
  // active-highlight-inner → black thin ring drawn on top of active
195
197
  // selected-highlight → black thin ring + fill for polygon features
196
- const applyLayerHighlight = (map, layerId, featuresByLayer, stylesMap, prefix, getSymbolImageId) => {
198
+ const applyLayerHighlight = (map, layerId, featuresByLayer, stylesMap, prefix, variant) => {
197
199
  const { ids, fillIds, idProperty, sourceId, hasFillGeometry } = featuresByLayer[layerId]
198
200
  const baseLayer = map.getLayer(layerId)
199
201
  // Only the recorded layerId is registered in stylesMap — falls back to its cold/hot sibling
@@ -227,14 +229,14 @@ const applyLayerHighlight = (map, layerId, featuresByLayer, stylesMap, prefix, g
227
229
  applyLineHighlight(map, base, sourceId, srcLayer, lineColor, lineWidth, filter)
228
230
  break
229
231
  case 'symbol':
230
- applySymbolGeomHighlight(map, base, sourceId, srcLayer, layerId, filter, getSymbolImageId)
232
+ applySymbolGeomHighlight(map, base, sourceId, srcLayer, layerId, filter, variant)
231
233
  break
232
234
  default:
233
235
  break
234
236
  }
235
237
  }
236
238
 
237
- const applyFeatureHighlights = (map, features, stylesMap, prefix, getSymbolImageId) => {
239
+ const applyFeatureHighlights = (map, features, stylesMap, prefix, variant) => {
238
240
  const featuresByLayer = groupFeaturesByLayer(map, features)
239
241
  const currentLayerIds = new Set(Object.keys(featuresByLayer))
240
242
  const storageKey = `_${prefix.replaceAll('-', '')}Sources`
@@ -242,7 +244,7 @@ const applyFeatureHighlights = (map, features, stylesMap, prefix, getSymbolImage
242
244
 
243
245
  cleanupStaleLayers(map, previousLayerIds, currentLayerIds, prefix)
244
246
  map[storageKey] = currentLayerIds
245
- currentLayerIds.forEach(layerId => applyLayerHighlight(map, layerId, featuresByLayer, stylesMap, prefix, getSymbolImageId))
247
+ currentLayerIds.forEach(layerId => applyLayerHighlight(map, layerId, featuresByLayer, stylesMap, prefix, variant))
246
248
 
247
249
  return featuresByLayer
248
250
  }
@@ -260,17 +262,20 @@ const applyFeatureHighlights = (map, features, stylesMap, prefix, getSymbolImage
260
262
  * @param {Array} opts.activeFeatures - keyboard-cursor features
261
263
  * @param {object} opts.stylesMap - keyed by layerId; each value is
262
264
  * { stroke, selectionStroke, strokeWidth, activeStrokeWidth, fill }
265
+ * @param {object} [opts.symbolImages] - the provider's SymbolImageVariants, for symbol highlights
263
266
  * @returns {number[]|null} [west, south, east, north] bounds or null if nothing is selected
264
267
  */
265
- export function updateHighlightedFeatures ({ LngLatBounds, map, selectedFeatures, activeFeatures, stylesMap }) {
268
+ export function updateHighlightedFeatures ({ LngLatBounds, map, selectedFeatures, activeFeatures, stylesMap, symbolImages }) {
266
269
  if (!map) {
267
270
  return null
268
271
  }
272
+ const activeVariant = symbolVariant(symbolImages, 'active')
273
+ const selectedVariant = symbolVariant(symbolImages, 'selected')
269
274
  // Active cursor features — rendered first so selected layers appear on top
270
275
  if (activeFeatures?.length) {
271
- applyFeatureHighlights(map, activeFeatures, stylesMap, ACTIVE_PREFIX, getActiveImageId)
276
+ applyFeatureHighlights(map, activeFeatures, stylesMap, ACTIVE_PREFIX, activeVariant)
272
277
  // Black selected stroke on top of yellow active (mirrors resolveActive for symbols)
273
- applyFeatureHighlights(map, activeFeatures, stylesMap, ACTIVE_INNER_PREFIX, getSelectedImageId)
278
+ applyFeatureHighlights(map, activeFeatures, stylesMap, ACTIVE_INNER_PREFIX, selectedVariant)
274
279
  } else {
275
280
  clearPrefixLayers(map, ACTIVE_PREFIX)
276
281
  clearPrefixLayers(map, ACTIVE_INNER_PREFIX)
@@ -279,7 +284,7 @@ export function updateHighlightedFeatures ({ LngLatBounds, map, selectedFeatures
279
284
  // Selection features
280
285
  let featuresByLayer = {}
281
286
  if (selectedFeatures?.length) {
282
- featuresByLayer = applyFeatureHighlights(map, selectedFeatures, stylesMap, SELECTED_PREFIX, getSelectedImageId)
287
+ featuresByLayer = applyFeatureHighlights(map, selectedFeatures, stylesMap, SELECTED_PREFIX, selectedVariant)
283
288
  } else {
284
289
  clearPrefixLayers(map, SELECTED_PREFIX)
285
290
  }
@@ -183,6 +183,12 @@ describe('Highlighting Utils — layer management', () => {
183
183
  })
184
184
  })
185
185
 
186
+ // Stands in for the provider's SymbolImageVariants
187
+ const makeSymbolImages = ({ active = {}, selected = {} } = {}) => ({
188
+ getActiveImageId: (imageId) => active[imageId] ?? null,
189
+ getSelectedImageId: (imageId) => selected[imageId] ?? null
190
+ })
191
+
186
192
  // ─── Symbol layers ────────────────────────────────────────────────────────────
187
193
 
188
194
  describe('Highlighting Utils — symbol layers (active cursor)', () => {
@@ -192,17 +198,17 @@ describe('Highlighting Utils — symbol layers (active cursor)', () => {
192
198
  const ICON_ANCHOR = 'icon-anchor'
193
199
  const POINT_FEATURE = { featureId: 1, layerId: 'l1', geometry: { type: 'Point' } }
194
200
 
195
- let map
201
+ let map, symbolImages
196
202
 
197
203
  beforeEach(() => {
198
204
  map = makeMap()
199
- map._activeSymbolImageMap = { [SYMBOL_IMAGE]: ACTIVE_IMAGE }
205
+ symbolImages = makeSymbolImages({ active: { [SYMBOL_IMAGE]: ACTIVE_IMAGE } })
200
206
  map.getLayer.mockImplementation(id => id === 'l1' ? { source: 's1', type: 'symbol' } : null) // NOSONAR
201
207
  map.getLayoutProperty.mockReturnValue(SYMBOL_IMAGE)
202
208
  })
203
209
 
204
210
  const run = (activeFeatures = [POINT_FEATURE]) =>
205
- updateHighlightedFeatures({ LngLatBounds, map, activeFeatures, stylesMap: { l1: {} } })
211
+ updateHighlightedFeatures({ LngLatBounds, map, activeFeatures, stylesMap: { l1: {} }, symbolImages })
206
212
 
207
213
  test('creates symbol highlight layer with cursor image variant', () => {
208
214
  run()
@@ -251,9 +257,9 @@ describe('Highlighting Utils — symbol layers (active cursor)', () => {
251
257
  expect(map.setLayoutProperty).toHaveBeenCalledWith(HIGHLIGHT_LAYER, ICON_IMAGE, ACTIVE_IMAGE)
252
258
  })
253
259
 
254
- test('skips highlight when icon-image has no entry in _activeSymbolImageMap', () => {
260
+ test('skips highlight when the icon-image has no registered active variant', () => {
255
261
  map.getLayoutProperty.mockReturnValue(SYMBOL_IMAGE)
256
- map._activeSymbolImageMap = {} // no mapping registered
262
+ symbolImages = makeSymbolImages()
257
263
  run()
258
264
  expect(map.addLayer).not.toHaveBeenCalled()
259
265
  })
@@ -307,17 +313,17 @@ describe('Highlighting Utils — symbol layers (committed selection)', () => {
307
313
  const ICON_IMAGE = 'icon-image'
308
314
  const POINT_FEATURE = { featureId: 1, layerId: 'l1', geometry: { type: 'Point' } }
309
315
 
310
- let map
316
+ let map, symbolImages
311
317
 
312
318
  beforeEach(() => {
313
319
  map = makeMap()
314
- map._selectedSymbolImageMap = { [SYMBOL_IMAGE]: SELECTED_IMAGE }
320
+ symbolImages = makeSymbolImages({ selected: { [SYMBOL_IMAGE]: SELECTED_IMAGE } })
315
321
  map.getLayer.mockImplementation(id => id === 'l1' ? { source: 's1', type: 'symbol' } : null) // NOSONAR
316
322
  map.getLayoutProperty.mockReturnValue(SYMBOL_IMAGE)
317
323
  })
318
324
 
319
325
  const run = (selectedFeatures = [POINT_FEATURE]) =>
320
- updateHighlightedFeatures({ LngLatBounds, map, selectedFeatures, stylesMap: { l1: {} } })
326
+ updateHighlightedFeatures({ LngLatBounds, map, selectedFeatures, stylesMap: { l1: {} }, symbolImages })
321
327
 
322
328
  test('creates selected-highlight symbol layer with selected image variant', () => {
323
329
  run()
@@ -328,8 +334,8 @@ describe('Highlighting Utils — symbol layers (committed selection)', () => {
328
334
  }))
329
335
  })
330
336
 
331
- test('skips highlight when icon-image has no entry in _selectedSymbolImageMap', () => {
332
- map._selectedSymbolImageMap = {}
337
+ test('skips highlight when the icon-image has no registered selected variant', () => {
338
+ symbolImages = makeSymbolImages()
333
339
  run()
334
340
  expect(map.addLayer).not.toHaveBeenCalled()
335
341
  })
@@ -509,7 +515,6 @@ describe('Highlighting Utils — mapbox-gl-draw cold/hot sibling layers', () =>
509
515
  })
510
516
  map.getLayoutProperty.mockImplementation((id, prop) => // NOSONAR
511
517
  prop === 'icon-image' ? SYMBOL_IMAGE : undefined)
512
- map._selectedSymbolImageMap = { [SYMBOL_IMAGE]: 'symbol-sel-xyz' }
513
518
 
514
519
  // The feature's own current geometry lives on the hot source (it moved there when its
515
520
  // properties were last refreshed) — only stylesMap['point-symbol.cold'] is registered,
@@ -518,7 +523,8 @@ describe('Highlighting Utils — mapbox-gl-draw cold/hot sibling layers', () =>
518
523
  LngLatBounds,
519
524
  map,
520
525
  selectedFeatures: [{ featureId: 'p1', layerId: 'point-symbol.cold', geometry: { type: 'Point' } }],
521
- stylesMap: { 'point-symbol.cold': { stroke: 'red', selectionStroke: 'black', strokeWidth: 2 } }
526
+ stylesMap: { 'point-symbol.cold': { stroke: 'red', selectionStroke: 'black', strokeWidth: 2 } },
527
+ symbolImages: makeSymbolImages({ selected: { [SYMBOL_IMAGE]: 'symbol-sel-xyz' } })
522
528
  })
523
529
 
524
530
  // A highlight layer gets created/filtered for BOTH the recorded source and its sibling —
@@ -558,7 +564,6 @@ describe('Highlighting Utils — multi-select across geometry types sharing a so
558
564
  })
559
565
  map.getLayoutProperty.mockImplementation((id, prop) => // NOSONAR
560
566
  prop === 'icon-image' ? SYMBOL_IMAGE : undefined)
561
- map._selectedSymbolImageMap = { [SYMBOL_IMAGE]: 'symbol-sel-xyz' }
562
567
 
563
568
  updateHighlightedFeatures({
564
569
  LngLatBounds,
@@ -570,7 +575,8 @@ describe('Highlighting Utils — multi-select across geometry types sharing a so
570
575
  stylesMap: {
571
576
  'point-symbol.cold': { stroke: 'red', selectionStroke: 'black', strokeWidth: 2 },
572
577
  'fill-inactive.cold': { stroke: 'red', selectionStroke: 'black', fill: 'blue', strokeWidth: 2 }
573
- }
578
+ },
579
+ symbolImages: makeSymbolImages({ selected: { [SYMBOL_IMAGE]: 'symbol-sel-xyz' } })
574
580
  })
575
581
 
576
582
  expect(map.addLayer).toHaveBeenCalledWith(expect.objectContaining({
@@ -1,37 +1,54 @@
1
1
  import { getEffectivePixelRatio } from '../../../../src/utils/patternUtils.js'
2
+ import { RecentImageSets } from '../../../../src/utils/recentImageSets.js'
2
3
 
3
4
  /**
4
- * Register pattern images for the given pre-resolved pattern configs.
5
- * Skips images that are already registered (safe to call on style change).
6
- * Rasterisation itself lives in patternRegistry.rasterisePatternImage, shared with OpenLayers —
7
- * this only owns the MapLibre-specific "last mile" of registering via map.addImage().
8
- *
9
- * @param {Object} map - MapLibre map instance
10
- * @param {Object[]} styleArray - an array of pattern style configs
11
- * @param {string} mapStyleId
12
- * @param {Object} patternRegistry
13
- * @param {number} pixelRatio
14
- * @returns {Promise<void>}
5
+ * The MapLibre provider's pattern images, in the map's own image store (map.addImage). One per
6
+ * map, shared by every caller that registers patterns on it. Images are kept for the current and
7
+ * previous map size and style; ones only older sizes and styles used are removed from the map,
8
+ * so they don't build up as the map size or style changes.
15
9
  */
16
- export const addPatternsToMap = async (map, styleArray, mapStyleId, patternRegistry, pixelRatio) => {
17
- if (!styleArray.length) {
18
- return
10
+ export class PatternImages {
11
+ recentImageSets = new RecentImageSets()
12
+
13
+ /** @param {Object} map - MapLibre map instance */
14
+ constructor (map) {
15
+ this.map = map
19
16
  }
20
17
 
21
- const effectiveRatio = getEffectivePixelRatio(pixelRatio)
22
- // Build a unique set of imagesToAdd callbacks to avoid redundant rasterisation and map.addImage calls
23
- const addImages = styleArray.reduce((imagesToAdd, style) => {
24
- const imageId = patternRegistry.getPatternImageId(style, mapStyleId, pixelRatio)
25
- if (imageId && !imagesToAdd[imageId] && !map.hasImage(imageId)) {
26
- imagesToAdd[imageId] = async () => {
27
- const result = await patternRegistry.rasterisePatternImage(style, mapStyleId, pixelRatio)
28
- if (result && !map.hasImage(result.imageId)) {
29
- map.addImage(result.imageId, result.imageData, { pixelRatio: effectiveRatio })
30
- }
18
+ /**
19
+ * Rasterises and adds each pattern's image to the map. Images the map already has are reused,
20
+ * and styles that resolve to the same image are only rasterised once.
21
+ *
22
+ * @param {Object[]} styleArray - pattern style configs (fillPattern/fillPatternSvgContent plus colours)
23
+ * @param {string} mapStyleId
24
+ * @param {Object} patternRegistry
25
+ * @param {number} pixelRatio - Device pixel ratio × map size scale factor
26
+ * @returns {Promise<void>}
27
+ */
28
+ async registerPatterns (styleArray, mapStyleId, patternRegistry, pixelRatio) {
29
+ const { map } = this
30
+ const stylesByImageId = new Map()
31
+ styleArray.forEach((style) => {
32
+ const imageId = patternRegistry.getPatternImageId(style, mapStyleId, pixelRatio)
33
+ if (imageId && !stylesByImageId.has(imageId)) {
34
+ stylesByImageId.set(imageId, style)
35
+ }
36
+ })
37
+ const effectiveRatio = getEffectivePixelRatio(pixelRatio)
38
+ await Promise.all(Array.from(stylesByImageId, async ([imageId, style]) => {
39
+ if (map.hasImage(imageId)) {
40
+ return
31
41
  }
32
- }
33
- return imagesToAdd
34
- }, {})
35
- // Execute the unique set of addImage callbacks in parallel
36
- await Promise.all(Object.values(addImages).map(addImage => addImage()))
42
+ const result = await patternRegistry.rasterisePatternImage(style, mapStyleId, pixelRatio)
43
+ if (result && !map.hasImage(result.imageId)) {
44
+ map.addImage(result.imageId, result.imageData, { pixelRatio: effectiveRatio })
45
+ }
46
+ }))
47
+ this.recentImageSets.add(`${mapStyleId}|${pixelRatio}`, Array.from(stylesByImageId.keys()))
48
+ .forEach((imageId) => {
49
+ if (map.hasImage(imageId)) {
50
+ map.removeImage(imageId)
51
+ }
52
+ })
53
+ }
37
54
  }
@@ -1,5 +1,7 @@
1
- import { addPatternsToMap } from './patternImages.js'
2
- import { patternRegistry } from '../../../../src/services/patternRegistry.js'
1
+ import { PatternImages } from './patternImages.js'
2
+ import { createPatternRegistry } from '../../../../src/services/patternRegistry.js'
3
+
4
+ const patternRegistry = createPatternRegistry()
3
5
 
4
6
  const OUTDOOR = 'outdoor'
5
7
 
@@ -31,6 +33,10 @@ const makeMap = (existingIds = []) => ({
31
33
  addImage: jest.fn()
32
34
  })
33
35
 
36
+ // Registers the configs with a fresh PatternImages for the map
37
+ const addPatternsToMap = (map, configs, mapStyleId, registry, pixelRatio = 1) =>
38
+ new PatternImages(map).registerPatterns(configs, mapStyleId, registry, pixelRatio)
39
+
34
40
  // ─── addPatternsToMap ─────────────────────────────────────────────────────────
35
41
  describe('addPatternsToMap', () => {
36
42
  beforeEach(() => {
@@ -56,7 +62,7 @@ describe('addPatternsToMap', () => {
56
62
  it('skips addImage when image is already registered', async () => {
57
63
  const style = { fillPattern: 'stripes' }
58
64
  const pixelRatio = 1
59
- const map = makeMap(['pattern-mpxwil-2x'])
65
+ const map = makeMap([patternRegistry.getPatternImageId(style, OUTDOOR, pixelRatio)])
60
66
  await addPatternsToMap(map, [style], OUTDOOR, patternRegistry, pixelRatio)
61
67
  expect(map.addImage).not.toHaveBeenCalled()
62
68
  })
@@ -77,7 +83,6 @@ describe('addPatternsToMap', () => {
77
83
  it('processes multiple configs in parallel', async () => {
78
84
  const map = makeMap()
79
85
  patternRegistry.register('dots', "fillPatternForegroundColor: '#aabbcc', fillPatternBackgroundColor: '#112233'")
80
- console.log('patternRegistry.list', patternRegistry.list())
81
86
  await addPatternsToMap(map, [{ fillPattern: 'stripes' }, { fillPattern: 'dots' }], OUTDOOR, patternRegistry)
82
87
  expect(map.addImage).toHaveBeenCalledTimes(2)
83
88
  })
@@ -188,3 +193,46 @@ describe('addPatternsToMap', () => {
188
193
  })
189
194
  })
190
195
  })
196
+
197
+ describe('PatternImages — removing images from older map sizes and styles', () => {
198
+ // A map that keeps the images added to it, like MapLibre's image store
199
+ const makeImageStore = () => {
200
+ const images = new Set()
201
+ return {
202
+ images,
203
+ hasImage: jest.fn((id) => images.has(id)),
204
+ addImage: jest.fn((id) => images.add(id)),
205
+ removeImage: jest.fn((id) => images.delete(id))
206
+ }
207
+ }
208
+ const style = { fillPatternSvgContent: SVG_CONTENT }
209
+
210
+ it('keeps the current and previous size, and removes an older one\'s image when a third arrives', async () => {
211
+ const map = makeImageStore()
212
+ const patternImages = new PatternImages(map)
213
+ await patternImages.registerPatterns([style], OUTDOOR, patternRegistry, 1)
214
+ const oldId = patternRegistry.getPatternImageId(style, OUTDOOR, 1)
215
+ await patternImages.registerPatterns([style], OUTDOOR, patternRegistry, 2)
216
+ expect(map.images.has(oldId)).toBe(true)
217
+
218
+ await patternImages.registerPatterns([style], OUTDOOR, patternRegistry, 3)
219
+ expect(map.images.has(oldId)).toBe(false)
220
+ expect(map.images.size).toBe(2)
221
+ })
222
+
223
+ it('skips removing an old image the map no longer has, e.g. after a style change', async () => {
224
+ const map = makeImageStore()
225
+ const patternImages = new PatternImages(map)
226
+ await patternImages.registerPatterns([style], OUTDOOR, patternRegistry, 1)
227
+ map.images.clear()
228
+ await patternImages.registerPatterns([style], OUTDOOR, patternRegistry, 2)
229
+ await patternImages.registerPatterns([style], OUTDOOR, patternRegistry, 3)
230
+ expect(map.removeImage).not.toHaveBeenCalled()
231
+ })
232
+
233
+ it('rasterises a pattern shared by several styles once', async () => {
234
+ const map = makeImageStore()
235
+ await new PatternImages(map).registerPatterns([style, { ...style }], OUTDOOR, patternRegistry, 1)
236
+ expect(map.addImage).toHaveBeenCalledTimes(1)
237
+ })
238
+ })