@defra/interactive-map 0.0.59-alpha → 0.0.61-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.
- package/assets/templates/add-polygons.njk +2 -2
- package/assets/templates/add-symbols.njk +2 -2
- package/dist/css/index.css +1 -1
- package/dist/esm/im-core.js +1 -1
- package/dist/esm/im-shell.js +1 -1
- package/dist/umd/im-core.js +1 -1
- package/dist/umd/index.js +1 -1
- package/docs/api/button-definition.md +11 -0
- package/docs/api/context.md +2 -0
- package/docs/api/panel-definition.md +30 -0
- package/docs/api/symbol-config.md +12 -0
- package/docs/api.md +21 -2
- package/docs/assets/images/slot-map.svg +3 -3
- package/docs/plugins/datasets.md +11 -1
- package/docs/plugins/draw.md +26 -0
- package/package.json +1 -1
- package/plugins/datasets/dist/esm/index.js +1 -1
- package/plugins/datasets/dist/esm/layerAdapter.js +1 -1
- package/plugins/datasets/dist/umd/im-datasets-ml-adapter.js +1 -1
- package/plugins/datasets/dist/umd/im-datasets-ol-adapter.js +1 -1
- package/plugins/datasets/dist/umd/im-datasets-plugin.js +1 -1
- package/plugins/datasets/dist/umd/index.js +1 -1
- package/plugins/datasets/src/index.js +0 -1
- package/plugins/datasets/src/index.test.js +2 -8
- package/plugins/datasets/src/registry/dataset.test.js +9 -0
- package/plugins/draw/dist/esm/im-draw-ml-adapter.js +1 -1
- package/plugins/draw/dist/esm/im-draw-ol-adapter.js +1 -1
- package/plugins/draw/dist/esm/im-draw-plugin.js +1 -1
- package/plugins/draw/dist/umd/im-draw-ml-adapter.js +1 -1
- package/plugins/draw/dist/umd/im-draw-ol-adapter.js +1 -1
- package/plugins/draw/dist/umd/im-draw-plugin.js +1 -1
- package/plugins/draw/dist/umd/index.js +1 -1
- package/plugins/draw/src/DrawInit.jsx +3 -0
- package/plugins/draw/src/DrawInit.test.jsx +16 -0
- package/plugins/draw/src/adapters/maplibre/MaplibreDrawAdapter.js +19 -3
- package/plugins/draw/src/adapters/maplibre/MaplibreDrawAdapter.test.js +27 -0
- package/plugins/draw/src/adapters/openlayers/OLDrawAdapter.js +10 -2
- package/plugins/draw/src/adapters/openlayers/OLDrawAdapter.test.js +14 -0
- package/plugins/draw/src/defaults.js +4 -0
- package/plugins/draw/src/hooks/useSnapMinZoom.js +29 -0
- package/plugins/draw/src/hooks/useSnapMinZoom.test.js +82 -0
- package/plugins/draw/src/manifest.js +4 -1
- package/plugins/draw/src/manifest.test.js +15 -2
- package/plugins/draw/src/utils/snapMinZoom.js +8 -0
- package/plugins/draw/src/utils/snapMinZoom.test.js +22 -0
- package/plugins/interact/dist/esm/im-interact-plugin.js +1 -1
- package/plugins/interact/dist/umd/im-interact-plugin.js +1 -1
- package/plugins/map-key/dist/esm/im-map-key-plugin.js +1 -1
- package/plugins/map-key/dist/umd/im-map-key-plugin.js +1 -1
- package/plugins/map-key/src/manifest.js +1 -1
- package/plugins/search/dist/css/index.css +1 -1
- package/plugins/search/src/components/Form/Form.module.scss +1 -0
- package/providers/esri/dist/esm/im-esri-provider.js +1 -1
- package/providers/esri/src/esriProvider.js +10 -21
- package/providers/esri/src/utils/coords.js +37 -7
- package/providers/esri/src/utils/coords.test.js +22 -1
- package/providers/esri/src/utils/spatial.js +1 -47
- package/providers/maplibre/dist/esm/im-maplibre-provider.js +1 -1
- package/providers/maplibre/dist/umd/im-maplibre-provider.js +1 -1
- package/providers/maplibre/src/maplibreProvider.js +5 -14
- package/providers/maplibre/src/maplibreProvider.test.js +143 -146
- package/providers/maplibre/src/utils/spatial.js +0 -40
- package/providers/maplibre/src/utils/spatial.test.js +0 -35
- package/providers/openlayers/dist/esm/im-openlayers-provider.js +1 -1
- package/providers/openlayers/dist/umd/im-openlayers-provider.js +1 -1
- package/providers/openlayers/src/openlayersProvider.js +5 -14
- package/providers/openlayers/src/openlayersProvider.test.js +12 -10
- package/providers/openlayers/src/utils/spatial.js +0 -39
- package/providers/openlayers/src/utils/spatial.test.js +1 -26
- package/src/App/components/Hints/Hints.jsx +9 -21
- package/src/App/components/Hints/Hints.test.jsx +20 -33
- package/src/App/components/MapButton/MapButton.jsx +20 -2
- package/src/App/components/MapButton/MapButton.module.scss +15 -1
- package/src/App/components/MapButton/MapButton.test.jsx +21 -3
- package/src/App/components/Panel/Panel.jsx +16 -1
- package/src/App/components/Panel/Panel.test.jsx +44 -0
- package/src/App/components/Tooltip/Tooltip.jsx +29 -11
- package/src/App/components/Tooltip/Tooltip.test.jsx +45 -3
- package/src/App/components/Viewport/MapController.jsx +2 -0
- package/src/App/components/Viewport/MapController.test.jsx +3 -0
- package/src/App/hooks/useCrossHairAPI.js +27 -28
- package/src/App/hooks/useCrossHairAPI.test.js +64 -7
- package/src/App/hooks/useLayoutMeasurements.js +175 -131
- package/src/App/hooks/useLayoutMeasurements.test.js +227 -122
- package/src/App/hooks/useMapProviderOverrides.js +114 -35
- package/src/App/hooks/useMapProviderOverrides.test.js +188 -5
- package/src/App/hooks/useModalPanelBehaviour.js +18 -32
- package/src/App/hooks/useModalPanelBehaviour.test.js +45 -33
- package/src/App/hooks/useUserMoveTracking.js +65 -0
- package/src/App/hooks/useUserMoveTracking.test.js +99 -0
- package/src/App/hooks/useVisibleGeometry.js +35 -45
- package/src/App/hooks/useVisibleGeometry.test.js +149 -121
- package/src/App/layout/Layout.jsx +4 -4
- package/src/App/layout/layout.module.scss +124 -61
- package/src/App/renderer/mapButtons.js +1 -0
- package/src/App/renderer/mapButtons.test.js +8 -0
- package/src/App/store/AppProvider.jsx +3 -0
- package/src/App/store/appDispatchMiddleware.js +18 -10
- package/src/App/store/appDispatchMiddleware.test.js +42 -4
- package/src/App/store/mapActionsMap.js +2 -1
- package/src/App/store/mapActionsMap.test.js +6 -0
- package/src/App/store/mapReducer.js +2 -0
- package/src/App/store/mapReducer.test.js +1 -0
- package/src/InteractiveMap/InteractiveMap.js +3 -2
- package/src/InteractiveMap/InteractiveMap.test.js +2 -2
- package/src/config/events.js +1 -1
- package/src/config/mapPanelSlots.js +8 -0
- package/src/config/symbolConfig.js +1 -1
- package/src/scss/settings/_colors.scss +4 -0
- package/src/services/symbolRegistry.js +1 -1
- package/src/services/symbolRegistry.test.js +19 -0
- package/src/types.js +33 -8
- package/src/utils/getFitMove.js +73 -0
- package/src/utils/getFitMove.test.js +114 -0
- package/src/utils/getSafeZoneInset.js +122 -77
- package/src/utils/getSafeZoneInset.test.js +241 -54
- package/src/utils/getValueForStyle.js +10 -5
- package/src/utils/getValueForStyle.test.js +13 -2
- package/src/utils/renderedBoxes.js +25 -0
- package/src/utils/renderedBoxes.test.js +61 -0
- package/src/utils/screenBox.js +70 -0
- package/src/utils/screenBox.test.js +69 -0
- package/src/utils/symbolUtils.js +9 -9
- package/src/utils/symbolUtils.test.js +21 -0
- package/providers/esri/src/utils/spatial.test.js +0 -58
package/src/types.js
CHANGED
|
@@ -194,6 +194,10 @@
|
|
|
194
194
|
* opens or expands a custom control rendered elsewhere by the plugin. Text or a function
|
|
195
195
|
* returning the id.
|
|
196
196
|
*
|
|
197
|
+
* @property {string | ((context: PluginContext) => string | undefined)} [description]
|
|
198
|
+
* Extra information read by screen readers after the label, set with aria-describedby. Text or a
|
|
199
|
+
* function returning the text; no description when it returns nothing.
|
|
200
|
+
*
|
|
197
201
|
* @property {ButtonBreakpointConfig} [desktop]
|
|
198
202
|
* Desktop breakpoint configuration.
|
|
199
203
|
*
|
|
@@ -318,8 +322,9 @@
|
|
|
318
322
|
* @property {(offset: [number, number]) => void} panBy
|
|
319
323
|
* Pan map by pixel offset [x, y]. Positive x pans right, positive y pans down.
|
|
320
324
|
*
|
|
321
|
-
* @property {(bounds: [number, number, number, number] | object) => void} fitToBounds
|
|
325
|
+
* @property {(bounds: [number, number, number, number] | object, options?: { padding?: { top: number, right: number, bottom: number, left: number } }) => void} fitToBounds
|
|
322
326
|
* 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.
|
|
327
|
+
* `padding` is extra space in pixels kept around the bounds, inside the map's padding.
|
|
323
328
|
*
|
|
324
329
|
* @property {(padding: { top?: number, bottom?: number, left?: number, right?: number }) => void} setPadding
|
|
325
330
|
* Set map padding as pixel insets from the top, bottom, left and right edges of the map.
|
|
@@ -366,10 +371,21 @@
|
|
|
366
371
|
*
|
|
367
372
|
* @property {() => void} [clearHighlightedLabel]
|
|
368
373
|
* @experimental Clear any highlighted label.
|
|
374
|
+
*/
|
|
375
|
+
|
|
376
|
+
/**
|
|
377
|
+
* Options for `interactiveMap.fitToBounds()`.
|
|
369
378
|
*
|
|
370
|
-
* @
|
|
371
|
-
*
|
|
372
|
-
*
|
|
379
|
+
* @typedef {Object} FitToBoundsOptions
|
|
380
|
+
*
|
|
381
|
+
* @property {boolean} [preferPan]
|
|
382
|
+
* Leave the map where it is if the bounds are already inside the safe zone and not under an open
|
|
383
|
+
* panel. Otherwise pan to centre
|
|
384
|
+
* them in the safe zone, keeping the current zoom, and only zoom out if they don't fit.
|
|
385
|
+
*
|
|
386
|
+
* @property {number | { top?: number, right?: number, bottom?: number, left?: number }} [paddingPercent]
|
|
387
|
+
* Space kept clear inside the safe zone, as a percentage of its size: left and right use its width,
|
|
388
|
+
* top and bottom its height. One value for every side, or set per side. Limited to 0–25. Defaults to 0.
|
|
373
389
|
*/
|
|
374
390
|
|
|
375
391
|
/**
|
|
@@ -657,16 +673,25 @@
|
|
|
657
673
|
* @property {PanelBreakpointConfig} mobile
|
|
658
674
|
* Mobile breakpoint configuration.
|
|
659
675
|
*
|
|
676
|
+
* @property {number | { top?: number, right?: number, bottom?: number, left?: number }} [paddingPercent=0]
|
|
677
|
+
* Space to keep around `visibleGeometry`, as a percentage of the safe zone's size. See `FitToBoundsOptions`.
|
|
678
|
+
*
|
|
679
|
+
* @property {boolean} [preferPan=true]
|
|
680
|
+
* Whether to pan rather than zoom to show `visibleGeometry`. With `false`, the map fits the geometry when the panel opens over it.
|
|
681
|
+
*
|
|
660
682
|
* @property {ComponentType} [render]
|
|
661
683
|
* Render component.
|
|
662
684
|
*
|
|
663
685
|
* @property {PanelBreakpointConfig} tablet
|
|
664
686
|
* Tablet breakpoint configuration.
|
|
665
687
|
*
|
|
666
|
-
* @property {object} [visibleGeometry]
|
|
667
|
-
* GeoJSON Feature, FeatureCollection, or geometry to keep visible when this panel opens
|
|
668
|
-
*
|
|
669
|
-
* the
|
|
688
|
+
* @property {object | ((context: object) => object)} [visibleGeometry]
|
|
689
|
+
* GeoJSON Feature, FeatureCollection, or geometry to keep visible when this panel opens, or a function
|
|
690
|
+
* that receives the context and returns it, called each time the panel opens.
|
|
691
|
+
* 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
|
|
692
|
+
* 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
|
|
693
|
+
* map doesn't move.
|
|
694
|
+
* Applies to panels in the left, right and drawer slots. Panels in other slots, and modal panels, don't move the map.
|
|
670
695
|
*/
|
|
671
696
|
|
|
672
697
|
/**
|
|
@@ -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
|
-
*
|
|
3
|
-
*
|
|
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
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
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
|
-
//
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
//
|
|
39
|
-
const
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
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
|
-
//
|
|
58
|
-
//
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
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
|
-
//
|
|
67
|
-
//
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
const
|
|
72
|
-
const
|
|
73
|
-
|
|
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
|
|
132
|
+
const main = mainRef.current; const leftButtons = leftRef.current
|
|
85
133
|
const actions = actionsRef.current; const bottom = bottomRef.current
|
|
86
134
|
|
|
87
|
-
const
|
|
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
|
-
|
|
90
|
-
|
|
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
|
-
|
|
95
|
-
|
|
96
|
-
const
|
|
97
|
-
const
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
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
|
}
|