@defra/interactive-map 0.0.58-alpha → 0.0.60-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 (140) hide show
  1. package/assets/templates/add-polygons.njk +2 -2
  2. package/assets/templates/add-symbols.njk +2 -2
  3. package/dist/css/index.css +1 -1
  4. package/dist/esm/im-core.js +1 -1
  5. package/dist/esm/im-shell.js +1 -1
  6. package/dist/umd/im-core.js +1 -1
  7. package/dist/umd/index.js +1 -1
  8. package/docs/api/panel-definition.md +30 -0
  9. package/docs/api/symbol-config.md +12 -0
  10. package/docs/api.md +21 -2
  11. package/docs/assets/images/slot-map.svg +3 -3
  12. package/docs/plugins/datasets.md +11 -1
  13. package/docs/plugins/draw.md +95 -13
  14. package/package.json +1 -1
  15. package/plugins/datasets/dist/esm/index.js +1 -1
  16. package/plugins/datasets/dist/esm/layerAdapter.js +1 -1
  17. package/plugins/datasets/dist/umd/im-datasets-ml-adapter.js +1 -1
  18. package/plugins/datasets/dist/umd/im-datasets-ol-adapter.js +1 -1
  19. package/plugins/datasets/dist/umd/im-datasets-plugin.js +1 -1
  20. package/plugins/datasets/dist/umd/index.js +1 -1
  21. package/plugins/datasets/src/index.js +0 -1
  22. package/plugins/datasets/src/index.test.js +2 -8
  23. package/plugins/datasets/src/registry/dataset.test.js +9 -0
  24. package/plugins/draw/dist/esm/im-draw-ml-adapter.js +1 -1
  25. package/plugins/draw/dist/esm/im-draw-ol-adapter.js +1 -1
  26. package/plugins/draw/dist/esm/im-draw-plugin.js +1 -1
  27. package/plugins/draw/dist/esm/index.js +1 -1
  28. package/plugins/draw/dist/esm/liveDrawChecks.js +1 -1
  29. package/plugins/draw/dist/umd/im-draw-ml-adapter.js +1 -1
  30. package/plugins/draw/dist/umd/im-draw-ol-adapter.js +1 -1
  31. package/plugins/draw/dist/umd/im-draw-plugin.js +1 -1
  32. package/plugins/draw/dist/umd/index.js +1 -1
  33. package/plugins/draw/src/adapterEvents.js +2 -0
  34. package/plugins/draw/src/adapters/adapterContract.test.js +1 -0
  35. package/plugins/draw/src/adapters/maplibre/MaplibreDrawAdapter.js +15 -3
  36. package/plugins/draw/src/adapters/maplibre/MaplibreDrawAdapter.test.js +27 -0
  37. package/plugins/draw/src/adapters/maplibre/modes/editPointMode/pointerHandlers.js +8 -19
  38. package/plugins/draw/src/adapters/maplibre/modes/editPointMode/pointerHandlers.test.js +20 -0
  39. package/plugins/draw/src/adapters/maplibre/modes/editVertexMode/pointerHandlers.js +6 -17
  40. package/plugins/draw/src/adapters/maplibre/modes/editVertexMode/pointerHandlers.test.js +19 -0
  41. package/plugins/draw/src/adapters/maplibre/utils/editModeEvents.js +29 -1
  42. package/plugins/draw/src/adapters/openlayers/OLDrawAdapter.js +13 -3
  43. package/plugins/draw/src/adapters/openlayers/OLDrawAdapter.test.js +28 -0
  44. package/plugins/draw/src/api/addFeature.js +9 -6
  45. package/plugins/draw/src/api/addFeature.test.js +37 -10
  46. package/plugins/draw/src/api/editFeature.js +1 -1
  47. package/plugins/draw/src/api/recheckGeometry.js +1 -1
  48. package/plugins/draw/src/api/recheckGeometry.test.js +2 -2
  49. package/plugins/draw/src/api/setProperties.js +39 -0
  50. package/plugins/draw/src/api/setProperties.test.js +74 -0
  51. package/plugins/draw/src/api/setStyle.js +16 -19
  52. package/plugins/draw/src/api/setStyle.test.js +17 -2
  53. package/plugins/draw/src/api/split.js +7 -4
  54. package/plugins/draw/src/api/split.test.js +25 -2
  55. package/plugins/draw/src/events.js +95 -53
  56. package/plugins/draw/src/events.test.js +425 -6
  57. package/plugins/draw/src/manifest.js +6 -4
  58. package/plugins/draw/src/manifest.test.js +20 -26
  59. package/plugins/draw/src/reducer.js +7 -0
  60. package/plugins/draw/src/reducer.test.js +11 -0
  61. package/plugins/draw/src/utils/flattenStyleProperties.js +54 -2
  62. package/plugins/draw/src/utils/flattenStyleProperties.test.js +74 -1
  63. package/plugins/draw/src/utils/isPlainObject.js +14 -0
  64. package/plugins/draw/src/utils/isPlainObject.test.js +24 -0
  65. package/plugins/draw/src/utils/mergeFeatureProperties.js +45 -0
  66. package/plugins/draw/src/utils/mergeFeatureProperties.test.js +48 -0
  67. package/plugins/draw/src/utils/stripInternalProperties.js +3 -2
  68. package/plugins/draw/src/validation/commitChecks.js +29 -11
  69. package/plugins/draw/src/validation/commitChecks.test.js +67 -11
  70. package/plugins/draw/src/validation/validateGeometry.js +12 -5
  71. package/plugins/draw/src/validation/validateGeometry.test.js +18 -0
  72. package/plugins/interact/dist/esm/im-interact-plugin.js +1 -1
  73. package/plugins/interact/dist/umd/im-interact-plugin.js +1 -1
  74. package/plugins/map-key/dist/esm/im-map-key-plugin.js +1 -1
  75. package/plugins/map-key/dist/umd/im-map-key-plugin.js +1 -1
  76. package/plugins/search/dist/css/index.css +1 -1
  77. package/plugins/search/src/components/Form/Form.module.scss +1 -0
  78. package/providers/esri/dist/esm/im-esri-provider.js +1 -1
  79. package/providers/esri/src/esriProvider.js +10 -21
  80. package/providers/esri/src/utils/coords.js +37 -7
  81. package/providers/esri/src/utils/coords.test.js +22 -1
  82. package/providers/esri/src/utils/spatial.js +1 -47
  83. package/providers/maplibre/dist/esm/im-maplibre-provider.js +1 -1
  84. package/providers/maplibre/dist/umd/im-maplibre-provider.js +1 -1
  85. package/providers/maplibre/src/maplibreProvider.js +5 -14
  86. package/providers/maplibre/src/maplibreProvider.test.js +143 -146
  87. package/providers/maplibre/src/utils/spatial.js +0 -40
  88. package/providers/maplibre/src/utils/spatial.test.js +0 -35
  89. package/providers/openlayers/dist/esm/im-openlayers-provider.js +1 -1
  90. package/providers/openlayers/dist/umd/im-openlayers-provider.js +1 -1
  91. package/providers/openlayers/src/openlayersProvider.js +5 -14
  92. package/providers/openlayers/src/openlayersProvider.test.js +12 -10
  93. package/providers/openlayers/src/utils/spatial.js +0 -39
  94. package/providers/openlayers/src/utils/spatial.test.js +1 -26
  95. package/src/App/components/Hints/Hints.jsx +9 -21
  96. package/src/App/components/Hints/Hints.test.jsx +20 -33
  97. package/src/App/components/Panel/Panel.jsx +16 -1
  98. package/src/App/components/Panel/Panel.test.jsx +44 -0
  99. package/src/App/hooks/useCrossHairAPI.js +27 -28
  100. package/src/App/hooks/useCrossHairAPI.test.js +64 -7
  101. package/src/App/hooks/useLayoutMeasurements.js +175 -131
  102. package/src/App/hooks/useLayoutMeasurements.test.js +227 -122
  103. package/src/App/hooks/useMapProviderOverrides.js +114 -35
  104. package/src/App/hooks/useMapProviderOverrides.test.js +188 -5
  105. package/src/App/hooks/useModalPanelBehaviour.js +18 -32
  106. package/src/App/hooks/useModalPanelBehaviour.test.js +45 -33
  107. package/src/App/hooks/useVisibleGeometry.js +35 -45
  108. package/src/App/hooks/useVisibleGeometry.test.js +149 -121
  109. package/src/App/layout/Layout.jsx +4 -4
  110. package/src/App/layout/layout.module.scss +124 -61
  111. package/src/App/store/AppProvider.jsx +3 -0
  112. package/src/App/store/MapProvider.jsx +1 -0
  113. package/src/App/store/MapProvider.test.jsx +25 -0
  114. package/src/App/store/appDispatchMiddleware.js +18 -10
  115. package/src/App/store/appDispatchMiddleware.test.js +42 -4
  116. package/src/App/store/mapActionsMap.js +6 -0
  117. package/src/App/store/mapActionsMap.test.js +7 -0
  118. package/src/App/store/mapReducer.js +2 -0
  119. package/src/App/store/mapReducer.test.js +2 -0
  120. package/src/InteractiveMap/InteractiveMap.js +3 -2
  121. package/src/InteractiveMap/InteractiveMap.test.js +2 -2
  122. package/src/config/events.js +1 -1
  123. package/src/config/mapPanelSlots.js +8 -0
  124. package/src/config/symbolConfig.js +1 -1
  125. package/src/services/symbolRegistry.js +1 -1
  126. package/src/services/symbolRegistry.test.js +19 -0
  127. package/src/types.js +29 -8
  128. package/src/utils/getFitMove.js +73 -0
  129. package/src/utils/getFitMove.test.js +114 -0
  130. package/src/utils/getSafeZoneInset.js +122 -77
  131. package/src/utils/getSafeZoneInset.test.js +241 -54
  132. package/src/utils/getValueForStyle.js +10 -5
  133. package/src/utils/getValueForStyle.test.js +13 -2
  134. package/src/utils/renderedBoxes.js +25 -0
  135. package/src/utils/renderedBoxes.test.js +61 -0
  136. package/src/utils/screenBox.js +70 -0
  137. package/src/utils/screenBox.test.js +69 -0
  138. package/src/utils/symbolUtils.js +9 -9
  139. package/src/utils/symbolUtils.test.js +21 -0
  140. package/providers/esri/src/utils/spatial.test.js +0 -58
package/src/types.js CHANGED
@@ -318,8 +318,9 @@
318
318
  * @property {(offset: [number, number]) => void} panBy
319
319
  * Pan map by pixel offset [x, y]. Positive x pans right, positive y pans down.
320
320
  *
321
- * @property {(bounds: [number, number, number, number] | object) => void} fitToBounds
321
+ * @property {(bounds: [number, number, number, number] | object, options?: { padding?: { top: number, right: number, bottom: number, left: number } }) => void} fitToBounds
322
322
  * Fit map view to the specified bounds [west, south, east, north] or [minX, minY, maxX, maxY] depending on the crs, or a GeoJSON Feature, FeatureCollection, or geometry.
323
+ * `padding` is extra space in pixels kept around the bounds, inside the map's padding.
323
324
  *
324
325
  * @property {(padding: { top?: number, bottom?: number, left?: number, right?: number }) => void} setPadding
325
326
  * Set map padding as pixel insets from the top, bottom, left and right edges of the map.
@@ -366,10 +367,21 @@
366
367
  *
367
368
  * @property {() => void} [clearHighlightedLabel]
368
369
  * @experimental Clear any highlighted label.
370
+ */
371
+
372
+ /**
373
+ * Options for `interactiveMap.fitToBounds()`.
374
+ *
375
+ * @typedef {Object} FitToBoundsOptions
369
376
  *
370
- * @property {(geojson: object, panelRect: DOMRect) => boolean} [isGeometryObscured]
371
- * Returns true if the geometry's screen bounding box overlaps the given panel element rectangle.
372
- * Used internally by useVisibleGeometry to decide whether to pan/zoom when a panel opens.
377
+ * @property {boolean} [preferPan]
378
+ * Leave the map where it is if the bounds are already inside the safe zone and not under an open
379
+ * panel. Otherwise pan to centre
380
+ * them in the safe zone, keeping the current zoom, and only zoom out if they don't fit.
381
+ *
382
+ * @property {number | { top?: number, right?: number, bottom?: number, left?: number }} [paddingPercent]
383
+ * Space kept clear inside the safe zone, as a percentage of its size: left and right use its width,
384
+ * top and bottom its height. One value for every side, or set per side. Limited to 0–25. Defaults to 0.
373
385
  */
374
386
 
375
387
  /**
@@ -657,16 +669,25 @@
657
669
  * @property {PanelBreakpointConfig} mobile
658
670
  * Mobile breakpoint configuration.
659
671
  *
672
+ * @property {number | { top?: number, right?: number, bottom?: number, left?: number }} [paddingPercent=0]
673
+ * Space to keep around `visibleGeometry`, as a percentage of the safe zone's size. See `FitToBoundsOptions`.
674
+ *
675
+ * @property {boolean} [preferPan=true]
676
+ * Whether to pan rather than zoom to show `visibleGeometry`. With `false`, the map fits the geometry when the panel opens over it.
677
+ *
660
678
  * @property {ComponentType} [render]
661
679
  * Render component.
662
680
  *
663
681
  * @property {PanelBreakpointConfig} tablet
664
682
  * Tablet breakpoint configuration.
665
683
  *
666
- * @property {object} [visibleGeometry]
667
- * GeoJSON Feature, FeatureCollection, or geometry to keep visible when this panel opens.
668
- * If any part of the geometry's bounding box is obscured by the safe zone after the panel opens,
669
- * the map automatically adjusts: Point or MultiPoint geometry routes to setView(), all other types to fitToBounds().
684
+ * @property {object | ((context: object) => object)} [visibleGeometry]
685
+ * GeoJSON Feature, FeatureCollection, or geometry to keep visible when this panel opens, or a function
686
+ * that receives the context and returns it, called each time the panel opens.
687
+ * If the panel opens over any part of the geometry's bounding box, the map pans to centre it in the safe zone, or zooms
688
+ * out to fit it if it doesn't fit at the current zoom. If it isn't under the panel, including when it's off screen, the
689
+ * map doesn't move.
690
+ * Applies to panels in the left, right and drawer slots. Panels in other slots, and modal panels, don't move the map.
670
691
  */
671
692
 
672
693
  /**
@@ -0,0 +1,73 @@
1
+ import { getBoxCenter, isInside, isOverlapping } from './screenBox.js'
2
+
3
+ const MAX_PADDING_PERCENT = 25
4
+ // Turns a percentage into a fraction
5
+ const PERCENT_DIVISOR = 100
6
+
7
+ const SIDES = ['top', 'right', 'bottom', 'left']
8
+
9
+ const clampPercent = (value) => Math.min(Math.max(Number(value) || 0, 0), MAX_PADDING_PERCENT)
10
+
11
+ /**
12
+ * Normalise `paddingPercent` into a percentage for each side, limited to 0–25.
13
+ *
14
+ * @param {number|{ top?: number, right?: number, bottom?: number, left?: number }} [paddingPercent]
15
+ * @returns {{ top: number, right: number, bottom: number, left: number }}
16
+ */
17
+ export const normalisePaddingPercent = (paddingPercent) => {
18
+ const isPerSide = paddingPercent !== null && typeof paddingPercent === 'object'
19
+ return Object.fromEntries(SIDES.map(side => [side, clampPercent(isPerSide ? paddingPercent[side] : paddingPercent)]))
20
+ }
21
+
22
+ /**
23
+ * How `fitToBounds` should move the map to show a box. Every value is in the same pixels as
24
+ * `box` and `safeZone`.
25
+ *
26
+ * Without `preferPan`, the box is always fitted. With it, a box already inside the safe zone and
27
+ * clear of every panel doesn't move, a box that fits inside the safe zone less its margin is
28
+ * centred there, and any other box is fitted. Panels are checked separately because the safe zone
29
+ * leaves out a panel too small to narrow it.
30
+ *
31
+ * @param {object} params
32
+ * @param {{ left: number, top: number, right: number, bottom: number }} [params.box] - The geometry's on-screen box. Needed with `preferPan`.
33
+ * @param {{ left: number, top: number, right: number, bottom: number }} params.safeZone - The safe zone's edges.
34
+ * @param {Array<{ left: number, top: number, right: number, bottom: number }>} [params.panels] - Open panels over the map.
35
+ * @param {boolean} [params.preferPan]
36
+ * @param {number|object} [params.paddingPercent] - Space kept clear inside the safe zone, as a percentage of its size.
37
+ * @returns {{ type: 'none' } | { type: 'pan', offset: [number, number] } | { type: 'fit', margin: { top: number, right: number, bottom: number, left: number } }}
38
+ */
39
+ export const getFitMove = ({ box, safeZone, panels = [], preferPan = false, paddingPercent }) => {
40
+ const percent = normalisePaddingPercent(paddingPercent)
41
+ const width = safeZone.right - safeZone.left
42
+ const height = safeZone.bottom - safeZone.top
43
+ const margin = {
44
+ top: height * percent.top / PERCENT_DIVISOR,
45
+ right: width * percent.right / PERCENT_DIVISOR,
46
+ bottom: height * percent.bottom / PERCENT_DIVISOR,
47
+ left: width * percent.left / PERCENT_DIVISOR
48
+ }
49
+
50
+ if (!preferPan) {
51
+ return { type: 'fit', margin }
52
+ }
53
+
54
+ if (isInside(box, safeZone) && !panels.some(panel => isOverlapping(box, panel))) {
55
+ return { type: 'none' }
56
+ }
57
+
58
+ const inner = {
59
+ left: safeZone.left + margin.left,
60
+ top: safeZone.top + margin.top,
61
+ right: safeZone.right - margin.right,
62
+ bottom: safeZone.bottom - margin.bottom
63
+ }
64
+ const isFitting = box.right - box.left <= inner.right - inner.left && box.bottom - box.top <= inner.bottom - inner.top
65
+
66
+ if (!isFitting) {
67
+ return { type: 'fit', margin }
68
+ }
69
+
70
+ const [boxX, boxY] = getBoxCenter(box)
71
+ const [innerX, innerY] = getBoxCenter(inner)
72
+ return { type: 'pan', offset: [boxX - innerX, boxY - innerY] }
73
+ }
@@ -0,0 +1,114 @@
1
+ import { getFitMove, normalisePaddingPercent } from './getFitMove.js'
2
+
3
+ // A 400 × 200 safe zone, from (100, 50) to (500, 250)
4
+ const safeZone = { left: 100, top: 50, right: 500, bottom: 250 }
5
+
6
+ describe('normalisePaddingPercent', () => {
7
+ test('defaults every side to 0', () => {
8
+ expect(normalisePaddingPercent()).toEqual({ top: 0, right: 0, bottom: 0, left: 0 })
9
+ expect(normalisePaddingPercent(null)).toEqual({ top: 0, right: 0, bottom: 0, left: 0 })
10
+ })
11
+
12
+ test('applies one value to every side', () => {
13
+ expect(normalisePaddingPercent(10)).toEqual({ top: 10, right: 10, bottom: 10, left: 10 })
14
+ })
15
+
16
+ test('takes each side from an object, defaulting missing sides to 0', () => {
17
+ expect(normalisePaddingPercent({ top: 5, left: 10 })).toEqual({ top: 5, right: 0, bottom: 0, left: 10 })
18
+ })
19
+
20
+ test('limits each side to 0–25', () => {
21
+ expect(normalisePaddingPercent(40)).toEqual({ top: 25, right: 25, bottom: 25, left: 25 })
22
+ expect(normalisePaddingPercent({ top: -5, right: 30 })).toEqual({ top: 0, right: 25, bottom: 0, left: 0 })
23
+ })
24
+
25
+ test('treats a value that is not a number as 0', () => {
26
+ expect(normalisePaddingPercent('lots')).toEqual({ top: 0, right: 0, bottom: 0, left: 0 })
27
+ })
28
+ })
29
+
30
+ describe('getFitMove', () => {
31
+ describe('without preferPan', () => {
32
+ test('always fits, with no margin by default', () => {
33
+ const box = { left: 200, top: 100, right: 300, bottom: 150 }
34
+ expect(getFitMove({ box, safeZone })).toEqual({ type: 'fit', margin: { top: 0, right: 0, bottom: 0, left: 0 } })
35
+ })
36
+
37
+ test('works out the margin from the safe zone width for left and right, and its height for top and bottom', () => {
38
+ expect(getFitMove({ safeZone, paddingPercent: 10 })).toEqual({ type: 'fit', margin: { top: 20, right: 40, bottom: 20, left: 40 } })
39
+ })
40
+
41
+ test('works out each side separately', () => {
42
+ expect(getFitMove({ safeZone, paddingPercent: { top: 5, left: 25 } })).toEqual({ type: 'fit', margin: { top: 10, right: 0, bottom: 0, left: 100 } })
43
+ })
44
+ })
45
+
46
+ describe('with preferPan', () => {
47
+ test('doesn\'t move a box already inside the safe zone', () => {
48
+ const box = { left: 110, top: 60, right: 200, bottom: 100 }
49
+ expect(getFitMove({ box, safeZone, preferPan: true })).toEqual({ type: 'none' })
50
+ })
51
+
52
+ test('doesn\'t move a box inside the safe zone but within its margin', () => {
53
+ const box = { left: 105, top: 55, right: 200, bottom: 100 }
54
+ expect(getFitMove({ box, safeZone, preferPan: true, paddingPercent: 10 })).toEqual({ type: 'none' })
55
+ })
56
+
57
+ test('pans a box inside the safe zone but under a panel', () => {
58
+ // A small panel the safe zone leaves out, over the box. Box centre (150, 75)
59
+ const box = { left: 140, top: 70, right: 160, bottom: 80 }
60
+ const panels = [{ left: 100, top: 50, right: 300, bottom: 120 }]
61
+ expect(getFitMove({ box, safeZone, panels, preferPan: true })).toEqual({ type: 'pan', offset: [-150, -75] })
62
+ })
63
+
64
+ test('doesn\'t move a box inside the safe zone and clear of every panel', () => {
65
+ const box = { left: 400, top: 200, right: 450, bottom: 240 }
66
+ const panels = [{ left: 100, top: 50, right: 300, bottom: 120 }]
67
+ expect(getFitMove({ box, safeZone, panels, preferPan: true })).toEqual({ type: 'none' })
68
+ })
69
+
70
+ test('pans a box that fits to the centre of the safe zone', () => {
71
+ // Box centre (550, 300), safe zone centre (300, 150)
72
+ const box = { left: 500, top: 250, right: 600, bottom: 350 }
73
+ expect(getFitMove({ box, safeZone, preferPan: true })).toEqual({ type: 'pan', offset: [250, 150] })
74
+ })
75
+
76
+ test('pans a box that is partly outside the safe zone', () => {
77
+ // Box centre (110, 150), safe zone centre (300, 150)
78
+ const box = { left: 60, top: 100, right: 160, bottom: 200 }
79
+ expect(getFitMove({ box, safeZone, preferPan: true })).toEqual({ type: 'pan', offset: [-190, 0] })
80
+ })
81
+
82
+ test('pans a point to the centre of the safe zone', () => {
83
+ const box = { left: 0, top: 0, right: 0, bottom: 0 }
84
+ expect(getFitMove({ box, safeZone, preferPan: true })).toEqual({ type: 'pan', offset: [-300, -150] })
85
+ })
86
+
87
+ test('centres in the safe zone less its margin when the sides differ', () => {
88
+ // Inner area (200, 50) to (500, 250), centre (350, 150)
89
+ const box = { left: 0, top: 100, right: 100, bottom: 200 }
90
+ expect(getFitMove({ box, safeZone, preferPan: true, paddingPercent: { left: 25 } })).toEqual({ type: 'pan', offset: [-300, 0] })
91
+ })
92
+
93
+ test('fits a box too wide for the safe zone', () => {
94
+ const box = { left: 0, top: 100, right: 450, bottom: 150 }
95
+ expect(getFitMove({ box, safeZone, preferPan: true })).toEqual({ type: 'fit', margin: { top: 0, right: 0, bottom: 0, left: 0 } })
96
+ })
97
+
98
+ test('fits a box too tall for the safe zone', () => {
99
+ const box = { left: 0, top: 0, right: 100, bottom: 250 }
100
+ expect(getFitMove({ box, safeZone, preferPan: true })).toEqual({ type: 'fit', margin: { top: 0, right: 0, bottom: 0, left: 0 } })
101
+ })
102
+
103
+ test('fits a box that fits the safe zone but not the safe zone less its margin', () => {
104
+ // 360 wide fits the 400 safe zone, not the 320 left after 10% each side
105
+ const box = { left: 0, top: 100, right: 360, bottom: 150 }
106
+ expect(getFitMove({ box, safeZone, preferPan: true, paddingPercent: 10 })).toEqual({ type: 'fit', margin: { top: 20, right: 40, bottom: 20, left: 40 } })
107
+ })
108
+
109
+ test('pans a box exactly the size of the safe zone less its margin', () => {
110
+ const box = { left: 0, top: 0, right: 320, bottom: 160 }
111
+ expect(getFitMove({ box, safeZone, preferPan: true, paddingPercent: 10 })).toEqual({ type: 'pan', offset: [-140, -70] })
112
+ })
113
+ })
114
+ })
@@ -1,19 +1,14 @@
1
+ import { getRelativeBox } from './screenBox.js'
2
+ import { topColHeight } from './renderedBoxes.js'
3
+
1
4
  /**
2
- * Calculates the safe zone inset — the unobscured region of the map viewport
3
- * not hidden behind overlay panels or structural UI (button columns, footer,
4
- * action bar). Used as padding for map operations like fitBounds or setView
5
- * so the target location or extent is fully visible.
5
+ * The safe zone inset: the part of the map not hidden by the button columns, top row, actions bar
6
+ * or open panels. Used as padding by fitBounds and setView so the target is fully visible.
6
7
  *
7
- * Each edge inset is driven by:
8
- * - A structural baseline from the button columns, footer, and action bar.
9
- * - A panel contribution once a panel (or the two panels stacked in a side's
10
- * slots) covers more than 1/RATIO of the available map height (widening
11
- * that side's left/right inset) and/or more than 1/RATIO of the available
12
- * map width (pushing the top/bottom inset down/up). Both can trigger at
13
- * once for a panel that's both tall and wide — independently, not
14
- * exclusively, since the MAX_RATIO cap below already bounds each edge.
15
- * - A cap so no single inset exceeds 1/MAX_RATIO of the map dimension,
16
- * preventing fitBounds from zooming out to a corner when panels are large.
8
+ * Each edge has a structural base: the button columns, the top row (mirrored at the bottom) and the
9
+ * actions bar. Open panels push an edge further once they cover more than 1/RATIO of the room across
10
+ * or down the map, capped at (MAX_RATIO - 1)/MAX_RATIO of it. If panels leave less than 1/MIN_RATIO
11
+ * of the room on an axis, that axis falls back to the structural insets.
17
12
  *
18
13
  * @param {Object} refs - React refs from layoutRefs.
19
14
  * @param {React.RefObject} refs.mainRef - Main map container.
@@ -21,6 +16,7 @@
21
16
  * @param {React.RefObject} refs.rightRef - Right button column.
22
17
  * @param {React.RefObject} refs.actionsRef - Bottom action bar.
23
18
  * @param {React.RefObject} refs.bottomRef - Bottom row (logo, copyright, etc).
19
+ * @param {React.RefObject} [refs.topRef] - Top row of buttons.
24
20
  * @param {React.RefObject} [refs.leftTopRef] - Top-left panel slot.
25
21
  * @param {React.RefObject} [refs.leftBottomRef] - Bottom-left panel slot.
26
22
  * @param {React.RefObject} [refs.rightTopRef] - Top-right panel slot.
@@ -30,22 +26,35 @@
30
26
  */
31
27
 
32
28
  const RATIO = 2 // panels covering more than 1/RATIO of the available height/width trigger that edge's inset
33
- const MAX_RATIO = 3 // each inset is capped at 1/MAX_RATIO of its map dimension
34
-
35
- // Query the panel element directly within its slot container.
36
- // Only the first panel matters, and only when it is also the first element in the slot
37
- // (i.e. no buttons or other elements precede it — a panel pushed down by buttons is
38
- // already within the button-column structural inset and should not add extra padding).
39
- const getPanelDimensions = (slotRef) => {
40
- if (!slotRef?.current) {
41
- return { offsetWidth: 0, offsetHeight: 0 }
42
- }
43
- const first = slotRef.current.firstElementChild
44
- if (!first?.classList.contains('im-c-panel') || first.offsetWidth === 0 || first.offsetHeight === 0) {
45
- return { offsetWidth: 0, offsetHeight: 0 }
46
- }
47
- return { offsetWidth: first.offsetWidth, offsetHeight: first.offsetHeight }
48
- }
29
+ const MAX_RATIO = 3 // each inset is capped at (MAX_RATIO - 1)/MAX_RATIO of its map dimension
30
+ // An axis with less than 1/MIN_RATIO of the structural room left falls back to the structural insets.
31
+ // A single panel at the MAX_RATIO cap still leaves more than this.
32
+ const MIN_RATIO = 4
33
+
34
+ // Closed panels stay mounted with `hidden`
35
+ const isOpenPanel = (element) => element.classList.contains('im-c-panel') && !element.hidden
36
+
37
+ /**
38
+ * Every open panel in a slot, wherever it sits among buttons and controls, as a box relative to
39
+ * `origin`.
40
+ *
41
+ * @param {React.RefObject} slotRef - A panel slot.
42
+ * @param {DOMRect} origin - The rect the boxes are measured from.
43
+ * @returns {Array<{ left: number, top: number, right: number, bottom: number }>}
44
+ */
45
+ export const getOpenPanelBoxes = (slotRef, origin) => Array.from(slotRef?.current?.children ?? [])
46
+ .filter(isOpenPanel)
47
+ .map(panel => panel.getBoundingClientRect())
48
+ .filter(rect => rect.width > 0 && rect.height > 0)
49
+ .map(rect => getRelativeBox(rect, origin))
50
+
51
+ const widthOf = (box) => box.right - box.left
52
+ const heightOf = (box) => box.bottom - box.top
53
+
54
+ // Sizes laid end to end, with the gap between each
55
+ const addUp = (sizes, gap) => sizes.length ? sizes.reduce((total, size) => total + size, 0) + gap * (sizes.length - 1) : 0
56
+
57
+ const widest = (boxes) => boxes.length ? Math.max(...boxes.map(widthOf)) : 0
49
58
 
50
59
  // Panels are in normal flow inside slot containers, expanding left/right offsetWidth.
51
60
  // Query button groups directly to recover the button-only structural column width.
@@ -54,69 +63,105 @@ const getButtonColWidth = (colRef) => {
54
63
  return group ? group.offsetWidth : 0
55
64
  }
56
65
 
57
- // Determines the column (left/right) inset from the two panels stacked in that side's
58
- // slots. Combined height exceeding the threshold triggers the inset, widened to fit
59
- // whichever panel is wider.
60
- const computeColumn = (panelA, panelB, hThreshold, columnLeft, gap) => {
61
- const coverage = panelA.offsetHeight + panelB.offsetHeight + (panelA.offsetHeight > 0 && panelB.offsetHeight > 0 ? gap : 0)
62
- const triggered = coverage > hThreshold
63
- return triggered ? columnLeft + Math.max(panelA.offsetWidth, panelB.offsetWidth) + gap : 0
66
+ // The open panels in a side's slots, stacked, trigger that side's inset once their heights added
67
+ // up pass the threshold. The inset then reaches the panels' furthest edge into the map.
68
+ const isColumnTriggered = (boxes, hThreshold, gap) => addUp(boxes.map(heightOf), gap) > hThreshold
69
+
70
+ // The open panels in the left and right near slots (both top, or both bottom) push that edge once
71
+ // the widest on each side, added up, pass the threshold. Returns the panels that push it, if any.
72
+ const rowPanels = (leftBoxes, rightBoxes, wThreshold, gap) =>
73
+ addUp([widest(leftBoxes), widest(rightBoxes)].filter(width => width > 0), gap) > wThreshold
74
+ ? [...leftBoxes, ...rightBoxes]
75
+ : []
76
+
77
+ // No edge's panel inset goes past (MAX_RATIO - 1)/MAX_RATIO of the map, less a gap each side
78
+ const maxInset = (size, gap) => (size - 2 * gap) * (MAX_RATIO - 1) / MAX_RATIO
79
+
80
+ // Width first, as panels sit in the side columns: a side whose panels are tall enough is cleared by
81
+ // its own inset, then capped, and both fall back to the structural insets if too little is left.
82
+ // A side only counts as cleared if its final inset still reaches past its panels.
83
+ const resolveWidth = ({ main, leftColumn, rightColumn, base, availableW, availableH, gap }) => {
84
+ const leftPanelInset = isColumnTriggered(leftColumn, availableH / RATIO, gap)
85
+ ? main.offsetLeft + Math.max(...leftColumn.map(box => box.right)) + gap
86
+ : 0
87
+ const rightPanelInset = isColumnTriggered(rightColumn, availableH / RATIO, gap)
88
+ ? main.offsetWidth - Math.min(...rightColumn.map(box => box.left)) + gap
89
+ : 0
90
+ const cappedLeft = Math.max(base.left, Math.min(leftPanelInset, main.offsetLeft + maxInset(main.offsetWidth, gap)))
91
+ const cappedRight = Math.max(base.right, Math.min(rightPanelInset, maxInset(main.offsetWidth, gap)))
92
+ const isTooNarrow = main.offsetWidth - (cappedLeft - main.offsetLeft) - cappedRight < availableW / MIN_RATIO
93
+ const left = isTooNarrow ? base.left : cappedLeft
94
+ const right = isTooNarrow ? base.right : cappedRight
95
+ return {
96
+ left,
97
+ right,
98
+ isLeftCleared: leftPanelInset > 0 && left >= leftPanelInset,
99
+ isRightCleared: rightPanelInset > 0 && right >= rightPanelInset
100
+ }
64
101
  }
65
102
 
66
- // Determines the row (top/bottom) inset from the panels docked in the left and right
67
- // columns' near slot (both top, or both bottom). Combined width exceeding the threshold
68
- // triggers the inset, deepened to fit whichever panel is taller. Evaluated independently
69
- // of computeColumn — a panel can widen its own column AND push top/bottom down at once.
70
- const computeRow = (leftPanel, rightPanel, wThreshold, baseInset, gap) => {
71
- const coverage = leftPanel.offsetWidth + rightPanel.offsetWidth + (leftPanel.offsetWidth > 0 && rightPanel.offsetWidth > 0 ? gap : 0)
72
- const triggered = coverage > wThreshold
73
- return triggered ? baseInset + Math.max(leftPanel.offsetHeight, rightPanel.offsetHeight) + gap : 0
103
+ // Then height: only panels on a side the final width doesn't clear still cover the rest of the map,
104
+ // so only they push the top or bottom, then capped, and both fall back if too little is left.
105
+ const resolveHeight = ({ main, panels, width, base, availableW, availableH, gap }) => {
106
+ const pushingPanels = (leftBoxes, rightBoxes) =>
107
+ rowPanels(width.isLeftCleared ? [] : leftBoxes, width.isRightCleared ? [] : rightBoxes, availableW / RATIO, gap)
108
+ const topPanels = pushingPanels(panels.topLeft, panels.topRight)
109
+ const bottomPanels = pushingPanels(panels.bottomLeft, panels.bottomRight)
110
+ const topPanelInset = topPanels.length ? Math.max(...topPanels.map(box => box.bottom)) + gap : 0
111
+ const bottomPanelInset = bottomPanels.length ? main.offsetHeight - Math.min(...bottomPanels.map(box => box.top)) + gap : 0
112
+ const cappedTop = Math.max(base.top, Math.min(topPanelInset, maxInset(main.offsetHeight, gap)))
113
+ const cappedBottom = Math.max(base.bottom, Math.min(bottomPanelInset, maxInset(main.offsetHeight, gap)))
114
+ const isTooShort = main.offsetHeight - cappedTop - cappedBottom < availableH / MIN_RATIO
115
+ return { top: isTooShort ? base.top : cappedTop, bottom: isTooShort ? base.bottom : cappedBottom }
116
+ }
117
+
118
+ // The top row's height and where it ends, counting any column that shows, as a column can wrap
119
+ const measureTopRow = (top) => {
120
+ const height = Math.max(0, ...Array.from(top?.children ?? [], topColHeight))
121
+ return { height, bottom: (top?.offsetTop ?? 0) + height }
74
122
  }
75
123
 
76
124
  export const getSafeZoneInset = ({
77
- mainRef, leftRef, rightRef, actionsRef, bottomRef,
125
+ mainRef, leftRef, rightRef, actionsRef, bottomRef, topRef,
78
126
  leftTopRef, leftBottomRef, rightTopRef, rightBottomRef
79
127
  }) => {
80
128
  if ([mainRef, leftRef, rightRef, actionsRef, bottomRef].some(ref => !ref?.current)) {
81
129
  return undefined
82
130
  }
83
131
 
84
- const main = mainRef.current; const left = leftRef.current
132
+ const main = mainRef.current; const leftButtons = leftRef.current
85
133
  const actions = actionsRef.current; const bottom = bottomRef.current
86
134
 
87
- const gap = Number.parseInt(getComputedStyle(document.documentElement).getPropertyValue('--divider-gap'), 10)
135
+ const rootStyle = getComputedStyle(document.documentElement)
136
+ const gap = Number.parseInt(rootStyle.getPropertyValue('--divider-gap'), 10)
137
+ const primaryGap = Number.parseInt(rootStyle.getPropertyValue('--primary-gap'), 10)
88
138
 
89
- const rawTL = getPanelDimensions(leftTopRef); const rawBL = getPanelDimensions(leftBottomRef)
90
- const rawTR = getPanelDimensions(rightTopRef); const rawBR = getPanelDimensions(rightBottomRef)
139
+ // Panels measured by where they really are against the main area, so one below buttons, or
140
+ // after the controls at the bottom of a slot, counts at its real position
141
+ const mainRect = main.getBoundingClientRect()
142
+ const topLeft = getOpenPanelBoxes(leftTopRef, mainRect); const bottomLeft = getOpenPanelBoxes(leftBottomRef, mainRect)
143
+ const topRight = getOpenPanelBoxes(rightTopRef, mainRect); const bottomRight = getOpenPanelBoxes(rightBottomRef, mainRect)
144
+ const leftColumn = [...topLeft, ...bottomLeft]; const rightColumn = [...topRight, ...bottomRight]
91
145
 
92
146
  // Structural base insets — always present, never capped.
93
147
  const colWidth = Math.max(getButtonColWidth(leftRef), getButtonColWidth(rightRef))
94
- const baseLeft = main.offsetLeft + left.offsetLeft + colWidth + gap
95
- const baseRight = left.offsetLeft + colWidth + gap
96
- const baseTop = left.offsetTop
97
- const bottomContainerPad = main.offsetHeight - bottom.offsetTop - bottom.offsetHeight
98
- // Minimum: primary-gap above the bottom edge. Normally: divider-gap above the top of the bottom container.
99
- const bottomInset = Math.max(bottomContainerPad, main.offsetHeight - bottom.offsetTop + gap)
100
- const baseBottom = Math.max(main.offsetHeight - actions.offsetTop + gap, bottomInset)
101
-
102
- const availableH = main.offsetHeight - baseTop - baseBottom
103
- const availableW = main.offsetWidth - (baseLeft - main.offsetLeft) - baseRight
104
-
105
- const leftPanelInset = computeColumn(rawTL, rawBL, availableH / RATIO, main.offsetLeft + left.offsetLeft, gap)
106
- const rightPanelInset = computeColumn(rawTR, rawBR, availableH / RATIO, left.offsetLeft, gap)
107
- const topPanelInset = computeRow(rawTL, rawTR, availableW / RATIO, baseTop, gap)
108
- const bottomPanelInset = computeRow(rawBL, rawBR, availableW / RATIO, bottomInset, gap)
109
-
110
- const usableW = main.offsetWidth - 2 * gap
111
- const usableH = main.offsetHeight - 2 * gap
112
- const maxL = main.offsetLeft + usableW * (MAX_RATIO - 1) / MAX_RATIO
113
- const maxR = usableW * (MAX_RATIO - 1) / MAX_RATIO
114
- const maxV = usableH * (MAX_RATIO - 1) / MAX_RATIO
115
-
116
- return {
117
- left: Math.max(baseLeft, Math.min(leftPanelInset, maxL)),
118
- right: Math.max(baseRight, Math.min(rightPanelInset, maxR)),
119
- top: Math.max(baseTop, Math.min(topPanelInset, maxV)),
120
- bottom: Math.max(baseBottom, Math.min(bottomPanelInset, maxV))
148
+ // The bottom row itself doesn't count. The bottom mirrors the top row and its margin, so the safe
149
+ // zone is centred, but still clears the actions bar and anything below the row, such as the drawer
150
+ const topRow = measureTopRow(topRef?.current)
151
+ const base = {
152
+ left: main.offsetLeft + leftButtons.offsetLeft + colWidth + gap,
153
+ right: leftButtons.offsetLeft + colWidth + gap,
154
+ top: Math.max(leftButtons.offsetTop, rightRef.current.offsetTop, topRow.bottom),
155
+ bottom: Math.max(
156
+ primaryGap + topRow.height,
157
+ main.offsetHeight - actions.offsetTop + gap,
158
+ main.offsetHeight - bottom.offsetTop - bottom.offsetHeight
159
+ )
121
160
  }
161
+ const availableH = main.offsetHeight - base.top - base.bottom
162
+ const availableW = main.offsetWidth - (base.left - main.offsetLeft) - base.right
163
+
164
+ const width = resolveWidth({ main, leftColumn, rightColumn, base, availableW, availableH, gap })
165
+ const height = resolveHeight({ main, panels: { topLeft, topRight, bottomLeft, bottomRight }, width, base, availableW, availableH, gap })
166
+ return { left: width.left, right: width.right, top: height.top, bottom: height.bottom }
122
167
  }