@defra/interactive-map 0.0.59-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.
- 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/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/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/umd/im-draw-ml-adapter.js +1 -1
- package/plugins/draw/dist/umd/im-draw-ol-adapter.js +1 -1
- 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/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/Panel/Panel.jsx +16 -1
- package/src/App/components/Panel/Panel.test.jsx +44 -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/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/store/AppProvider.jsx +3 -0
- package/src/App/store/appDispatchMiddleware.js +18 -10
- package/src/App/store/appDispatchMiddleware.test.js +42 -4
- 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/services/symbolRegistry.js +1 -1
- package/src/services/symbolRegistry.test.js +19 -0
- package/src/types.js +29 -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
|
@@ -66,23 +66,57 @@ const colRef = (offsetWidth, offsetLeft, offsetTop) => {
|
|
|
66
66
|
}
|
|
67
67
|
|
|
68
68
|
beforeEach(() => {
|
|
69
|
-
mainRef = {
|
|
69
|
+
mainRef = {
|
|
70
|
+
current: {
|
|
71
|
+
offsetWidth: MAIN_WIDTH,
|
|
72
|
+
offsetHeight: MAIN_HEIGHT,
|
|
73
|
+
offsetLeft: 0,
|
|
74
|
+
getBoundingClientRect: () => ({ left: 0, top: 0, right: MAIN_WIDTH, bottom: MAIN_HEIGHT, width: MAIN_WIDTH, height: MAIN_HEIGHT })
|
|
75
|
+
}
|
|
76
|
+
}
|
|
70
77
|
leftRef = colRef(LEFT_WIDTH, LEFT_LEFT, LEFT_TOP)
|
|
71
|
-
rightRef = colRef(RIGHT_WIDTH)
|
|
78
|
+
rightRef = colRef(RIGHT_WIDTH, 0, LEFT_TOP)
|
|
72
79
|
actionsRef = { current: { offsetTop: ACTIONS_TOP } }
|
|
73
80
|
bottomRef = { current: { offsetTop: BOTTOM_TOP, offsetHeight: 0 } }
|
|
74
81
|
})
|
|
75
82
|
|
|
76
83
|
const base = () => ({ mainRef, leftRef, rightRef, actionsRef, bottomRef })
|
|
77
84
|
|
|
78
|
-
//
|
|
79
|
-
const
|
|
80
|
-
|
|
81
|
-
|
|
85
|
+
// Bottom slots end just above the footer
|
|
86
|
+
const BOTTOM_EDGE = MAIN_HEIGHT - BOTTOM_INSET
|
|
87
|
+
|
|
88
|
+
// An element in a slot, positioned by the slot helpers below
|
|
89
|
+
const item = (width, height, { hidden = false, isPanel = true } = {}) => ({ width, height, hidden, isPanel })
|
|
90
|
+
const buttons = () => item(40, 40, { isPanel: false })
|
|
91
|
+
|
|
92
|
+
const toElement = ({ width, height, hidden, isPanel }, left, top) => ({
|
|
93
|
+
hidden,
|
|
94
|
+
classList: { contains: (className) => isPanel && className === 'im-c-panel' },
|
|
95
|
+
getBoundingClientRect: () => ({ left, top, right: left + width, bottom: top + height, width, height })
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
// Lays a slot's elements out as the page does: in the left or right column, top slots stacking
|
|
99
|
+
// down from the top of the column and bottom slots stacking up from above the footer. Hidden
|
|
100
|
+
// elements take no space.
|
|
101
|
+
const layOutSlot = (items, { side, end }) => {
|
|
102
|
+
const leftOf = ({ width }) => side === 'left' ? LEFT_LEFT : MAIN_WIDTH - LEFT_LEFT - width
|
|
103
|
+
const placed = []
|
|
104
|
+
let cursor = end === 'top' ? LEFT_TOP : BOTTOM_EDGE
|
|
105
|
+
const ordered = end === 'top' ? items : [...items].reverse()
|
|
106
|
+
ordered.forEach(slotItem => {
|
|
107
|
+
const top = end === 'top' ? cursor : cursor - slotItem.height
|
|
108
|
+
placed.push(toElement(slotItem, leftOf(slotItem), top))
|
|
109
|
+
if (!slotItem.hidden) {
|
|
110
|
+
cursor = end === 'top' ? cursor + slotItem.height + GAP : top - GAP
|
|
111
|
+
}
|
|
112
|
+
})
|
|
113
|
+
return { current: { children: end === 'top' ? placed : placed.reverse() } }
|
|
82
114
|
}
|
|
83
115
|
|
|
84
|
-
|
|
85
|
-
const
|
|
116
|
+
const leftTop = (...items) => layOutSlot(items, { side: 'left', end: 'top' })
|
|
117
|
+
const leftBottom = (...items) => layOutSlot(items, { side: 'left', end: 'bottom' })
|
|
118
|
+
const rightTop = (...items) => layOutSlot(items, { side: 'right', end: 'top' })
|
|
119
|
+
const rightBottom = (...items) => layOutSlot(items, { side: 'right', end: 'bottom' })
|
|
86
120
|
|
|
87
121
|
// ─── Missing refs ────────────────────────────────────────────────────────────
|
|
88
122
|
|
|
@@ -106,14 +140,15 @@ describe('getSafeZoneInset — base structural insets', () => {
|
|
|
106
140
|
left: BASE_LEFT, right: BASE_RIGHT, top: BASE_TOP, bottom: BASE_BOTTOM
|
|
107
141
|
})
|
|
108
142
|
})
|
|
109
|
-
it('
|
|
110
|
-
expect(getSafeZoneInset({ ...base(), leftTopRef:
|
|
143
|
+
it('counts a panel below buttons in its slot', () => {
|
|
144
|
+
expect(getSafeZoneInset({ ...base(), leftTopRef: leftTop(buttons(), item(PANEL_W_STANDARD, ABOVE_THRESHOLD)) }).left)
|
|
145
|
+
.toBe(leftInset(PANEL_W_STANDARD))
|
|
111
146
|
})
|
|
112
147
|
it('ignores a slot container with no children', () => {
|
|
113
|
-
expect(getSafeZoneInset({ ...base(), leftTopRef:
|
|
148
|
+
expect(getSafeZoneInset({ ...base(), leftTopRef: leftTop() }).left).toBe(BASE_LEFT)
|
|
114
149
|
})
|
|
115
150
|
it('ignores a panel with zero width', () => {
|
|
116
|
-
expect(getSafeZoneInset({ ...base(), leftTopRef:
|
|
151
|
+
expect(getSafeZoneInset({ ...base(), leftTopRef: leftTop(item(0, ABOVE_THRESHOLD)) }).left).toBe(BASE_LEFT)
|
|
117
152
|
})
|
|
118
153
|
it('uses zero button width when column ref has no button group', () => {
|
|
119
154
|
const noGroupLeft = { current: { offsetWidth: 0, offsetLeft: LEFT_LEFT, offsetTop: LEFT_TOP, querySelector: () => null } }
|
|
@@ -125,10 +160,10 @@ describe('getSafeZoneInset — base structural insets', () => {
|
|
|
125
160
|
it('returns base insets when all panel slots are empty (height 0)', () => {
|
|
126
161
|
expect(getSafeZoneInset({
|
|
127
162
|
...base(),
|
|
128
|
-
leftTopRef:
|
|
129
|
-
leftBottomRef:
|
|
130
|
-
rightTopRef:
|
|
131
|
-
rightBottomRef:
|
|
163
|
+
leftTopRef: leftTop(item(PANEL_W_STANDARD, 0)),
|
|
164
|
+
leftBottomRef: leftBottom(item(PANEL_W_STANDARD, 0)),
|
|
165
|
+
rightTopRef: rightTop(item(PANEL_W_STANDARD, 0)),
|
|
166
|
+
rightBottomRef: rightBottom(item(PANEL_W_STANDARD, 0))
|
|
132
167
|
})).toEqual({ left: BASE_LEFT, right: BASE_RIGHT, top: BASE_TOP, bottom: BASE_BOTTOM })
|
|
133
168
|
})
|
|
134
169
|
it('uses max of actions and bottom for base bottom', () => {
|
|
@@ -137,35 +172,76 @@ describe('getSafeZoneInset — base structural insets', () => {
|
|
|
137
172
|
})
|
|
138
173
|
})
|
|
139
174
|
|
|
175
|
+
// ─── Bottom row ──────────────────────────────────────────────────────────────
|
|
176
|
+
|
|
177
|
+
describe('getSafeZoneInset — bottom row', () => {
|
|
178
|
+
const TOP_ROW_HEIGHT = 100
|
|
179
|
+
const noActions = { current: { offsetTop: MAIN_HEIGHT } }
|
|
180
|
+
// A bottom row made tall by its contents, such as the scale bar and map controls
|
|
181
|
+
const tallRow = { current: { offsetTop: 300, offsetHeight: MAIN_HEIGHT - 300 - GAP } }
|
|
182
|
+
|
|
183
|
+
// A top row column, with a child that shows or one that's hidden
|
|
184
|
+
const shownChild = { hidden: false, getClientRects: () => [{}], getBoundingClientRect: () => ({}) }
|
|
185
|
+
const hiddenChild = { hidden: true }
|
|
186
|
+
const topCol = (offsetHeight, children = [shownChild]) => ({ offsetHeight, children })
|
|
187
|
+
const topRow = (...cols) => ({ current: { children: cols } })
|
|
188
|
+
|
|
189
|
+
it('mirrors the top row and its margin, ignoring the bottom row', () => {
|
|
190
|
+
expect(getSafeZoneInset({ ...base(), actionsRef: noActions, bottomRef: tallRow, topRef: topRow(topCol(TOP_ROW_HEIGHT), topCol(40)) }).bottom)
|
|
191
|
+
.toBe(GAP + TOP_ROW_HEIGHT)
|
|
192
|
+
})
|
|
193
|
+
it('ignores a top row column whose items are all hidden', () => {
|
|
194
|
+
expect(getSafeZoneInset({ ...base(), actionsRef: noActions, bottomRef: tallRow, topRef: topRow(topCol(TOP_ROW_HEIGHT, [hiddenChild]), topCol(40)) }).bottom)
|
|
195
|
+
.toBe(GAP + 40)
|
|
196
|
+
})
|
|
197
|
+
it('clears the lower of the two side columns, when the right-hand buttons wrap', () => {
|
|
198
|
+
const wrappedTop = LEFT_TOP + 50
|
|
199
|
+
expect(getSafeZoneInset({ ...base(), rightRef: colRef(RIGHT_WIDTH, 0, wrappedTop) }).top).toBe(wrappedTop)
|
|
200
|
+
})
|
|
201
|
+
it('clears a middle column that wraps taller than both side columns', () => {
|
|
202
|
+
const top = { current: { offsetTop: 10, children: [topCol(40), topCol(120), topCol(40)] } }
|
|
203
|
+
expect(getSafeZoneInset({ ...base(), topRef: top }).top).toBe(130)
|
|
204
|
+
})
|
|
205
|
+
it('collapses to the margin when there is no top row', () => {
|
|
206
|
+
expect(getSafeZoneInset({ ...base(), actionsRef: noActions, bottomRef: tallRow, topRef: topRow() }).bottom)
|
|
207
|
+
.toBe(GAP)
|
|
208
|
+
})
|
|
209
|
+
it('clears anything below the bottom row, such as the drawer', () => {
|
|
210
|
+
const rowBottom = 400
|
|
211
|
+
expect(getSafeZoneInset({ ...base(), actionsRef: noActions, bottomRef: { current: { offsetTop: rowBottom, offsetHeight: 0 } } }).bottom)
|
|
212
|
+
.toBe(MAIN_HEIGHT - rowBottom)
|
|
213
|
+
})
|
|
214
|
+
})
|
|
215
|
+
|
|
140
216
|
// ─── Left edge ───────────────────────────────────────────────────────────────
|
|
141
217
|
|
|
142
218
|
describe('getSafeZoneInset — left edge', () => {
|
|
143
219
|
it('does not trigger when single panel height is below threshold', () => {
|
|
144
|
-
expect(getSafeZoneInset({ ...base(), leftTopRef:
|
|
220
|
+
expect(getSafeZoneInset({ ...base(), leftTopRef: leftTop(item(PANEL_W_STANDARD, BELOW_THRESHOLD)) }).left).toBe(BASE_LEFT)
|
|
145
221
|
})
|
|
146
222
|
it('triggers when single panel height exceeds threshold', () => {
|
|
147
|
-
expect(getSafeZoneInset({ ...base(), leftTopRef:
|
|
223
|
+
expect(getSafeZoneInset({ ...base(), leftTopRef: leftTop(item(PANEL_W_STANDARD, ABOVE_THRESHOLD)) }).left)
|
|
148
224
|
.toBe(leftInset(PANEL_W_STANDARD))
|
|
149
225
|
})
|
|
150
226
|
it('triggers when combined height of two panels exceeds threshold', () => {
|
|
151
227
|
expect(getSafeZoneInset({
|
|
152
228
|
...base(),
|
|
153
|
-
leftTopRef:
|
|
154
|
-
leftBottomRef:
|
|
229
|
+
leftTopRef: leftTop(item(PANEL_W_STANDARD, COMBINED_ABOVE)),
|
|
230
|
+
leftBottomRef: leftBottom(item(PANEL_W_NARROW, COMBINED_ABOVE))
|
|
155
231
|
}).left).toBe(leftInset(PANEL_W_STANDARD))
|
|
156
232
|
})
|
|
157
233
|
it('does not trigger when combined height is below threshold', () => {
|
|
158
234
|
expect(getSafeZoneInset({
|
|
159
235
|
...base(),
|
|
160
|
-
leftTopRef:
|
|
161
|
-
leftBottomRef:
|
|
236
|
+
leftTopRef: leftTop(item(PANEL_W_STANDARD, COMBINED_BELOW)),
|
|
237
|
+
leftBottomRef: leftBottom(item(PANEL_W_STANDARD, COMBINED_BELOW))
|
|
162
238
|
}).left).toBe(BASE_LEFT)
|
|
163
239
|
})
|
|
164
240
|
it('uses the wider panel for the inset amount', () => {
|
|
165
241
|
expect(getSafeZoneInset({
|
|
166
242
|
...base(),
|
|
167
|
-
leftTopRef:
|
|
168
|
-
leftBottomRef:
|
|
243
|
+
leftTopRef: leftTop(item(PANEL_W_WIDE, COMBINED_ABOVE)),
|
|
244
|
+
leftBottomRef: leftBottom(item(PANEL_W_NARROW, COMBINED_ABOVE))
|
|
169
245
|
}).left).toBe(leftInset(PANEL_W_WIDE))
|
|
170
246
|
})
|
|
171
247
|
})
|
|
@@ -176,15 +252,15 @@ describe('getSafeZoneInset — right edge', () => {
|
|
|
176
252
|
it('triggers when combined height of right-column panels exceeds threshold', () => {
|
|
177
253
|
expect(getSafeZoneInset({
|
|
178
254
|
...base(),
|
|
179
|
-
rightTopRef:
|
|
180
|
-
rightBottomRef:
|
|
255
|
+
rightTopRef: rightTop(item(PANEL_W_STANDARD, COMBINED_ABOVE)),
|
|
256
|
+
rightBottomRef: rightBottom(item(PANEL_W_NARROW, COMBINED_ABOVE))
|
|
181
257
|
}).right).toBe(rightInset(PANEL_W_STANDARD))
|
|
182
258
|
})
|
|
183
259
|
it('does not trigger when combined height is below threshold', () => {
|
|
184
260
|
expect(getSafeZoneInset({
|
|
185
261
|
...base(),
|
|
186
|
-
rightTopRef:
|
|
187
|
-
rightBottomRef:
|
|
262
|
+
rightTopRef: rightTop(item(PANEL_W_STANDARD, COMBINED_BELOW)),
|
|
263
|
+
rightBottomRef: rightBottom(item(PANEL_W_STANDARD, COMBINED_BELOW))
|
|
188
264
|
}).right).toBe(BASE_RIGHT)
|
|
189
265
|
})
|
|
190
266
|
})
|
|
@@ -194,18 +270,22 @@ describe('getSafeZoneInset — right edge', () => {
|
|
|
194
270
|
|
|
195
271
|
describe('getSafeZoneInset — top edge', () => {
|
|
196
272
|
it('does not trigger when top panel is narrow, even if tall', () => {
|
|
197
|
-
expect(getSafeZoneInset({ ...base(), rightTopRef:
|
|
273
|
+
expect(getSafeZoneInset({ ...base(), rightTopRef: rightTop(item(PANEL_W_STANDARD, ABOVE_THRESHOLD)) }).top).toBe(BASE_TOP)
|
|
274
|
+
})
|
|
275
|
+
it('triggers when a top panel is wide enough but too short to clear its side', () => {
|
|
276
|
+
expect(getSafeZoneInset({ ...base(), leftTopRef: leftTop(item(ABOVE_W_THRESHOLD, BELOW_THRESHOLD)) }).top)
|
|
277
|
+
.toBe(topInset(BELOW_THRESHOLD))
|
|
198
278
|
})
|
|
199
|
-
it('triggers when a top panel is
|
|
200
|
-
const result = getSafeZoneInset({ ...base(), leftTopRef:
|
|
279
|
+
it('triggers when a top panel is wider than its side\'s capped inset can clear, capped at (MAX_RATIO-1)/MAX_RATIO of usable height', () => {
|
|
280
|
+
const result = getSafeZoneInset({ ...base(), leftTopRef: leftTop(item(PANEL_W_XLARGE, ABOVE_CAP_TOP)) })
|
|
281
|
+
expect(result.left).toBe(CAP_WIDTH)
|
|
201
282
|
expect(result.top).toBe(CAP_HEIGHT)
|
|
202
|
-
expect(result.left).toBe(leftInset(ABOVE_W_THRESHOLD))
|
|
203
283
|
})
|
|
204
284
|
it('does not trigger when combined width of two top panels is below threshold', () => {
|
|
205
285
|
expect(getSafeZoneInset({
|
|
206
286
|
...base(),
|
|
207
|
-
leftTopRef:
|
|
208
|
-
rightTopRef:
|
|
287
|
+
leftTopRef: leftTop(item(COMBINED_BELOW_W, BELOW_THRESHOLD)),
|
|
288
|
+
rightTopRef: rightTop(item(COMBINED_BELOW_W, BELOW_THRESHOLD))
|
|
209
289
|
}).top).toBe(BASE_TOP)
|
|
210
290
|
})
|
|
211
291
|
})
|
|
@@ -214,23 +294,23 @@ describe('getSafeZoneInset — top edge', () => {
|
|
|
214
294
|
|
|
215
295
|
describe('getSafeZoneInset — bottom edge', () => {
|
|
216
296
|
it('does not trigger when bottom panel is narrow, even if tall', () => {
|
|
217
|
-
expect(getSafeZoneInset({ ...base(), rightBottomRef:
|
|
297
|
+
expect(getSafeZoneInset({ ...base(), rightBottomRef: rightBottom(item(PANEL_W_STANDARD, ABOVE_THRESHOLD)) }).bottom).toBe(BASE_BOTTOM)
|
|
218
298
|
})
|
|
219
|
-
it('triggers when a bottom panel is wide enough
|
|
220
|
-
const result = getSafeZoneInset({ ...base(),
|
|
221
|
-
expect(result.bottom).toBe(
|
|
222
|
-
expect(result.
|
|
299
|
+
it('triggers when a bottom panel is wide enough but too short to clear its side', () => {
|
|
300
|
+
const result = getSafeZoneInset({ ...base(), rightBottomRef: rightBottom(item(ABOVE_W_THRESHOLD, BELOW_THRESHOLD)) })
|
|
301
|
+
expect(result.bottom).toBe(bottomInset(BELOW_THRESHOLD))
|
|
302
|
+
expect(result.right).toBe(BASE_RIGHT)
|
|
223
303
|
})
|
|
224
|
-
it('triggers when a bottom panel is
|
|
225
|
-
const result = getSafeZoneInset({ ...base(),
|
|
226
|
-
expect(result.
|
|
227
|
-
expect(result.
|
|
304
|
+
it('triggers when a bottom panel is wider than its side\'s capped inset can clear, capped at (MAX_RATIO-1)/MAX_RATIO of usable height', () => {
|
|
305
|
+
const result = getSafeZoneInset({ ...base(), leftBottomRef: leftBottom(item(PANEL_W_XLARGE, ABOVE_CAP_BOTTOM)) })
|
|
306
|
+
expect(result.left).toBe(CAP_WIDTH)
|
|
307
|
+
expect(result.bottom).toBe(CAP_HEIGHT)
|
|
228
308
|
})
|
|
229
309
|
it('does not trigger when combined width of two bottom panels is below threshold', () => {
|
|
230
310
|
expect(getSafeZoneInset({
|
|
231
311
|
...base(),
|
|
232
|
-
leftBottomRef:
|
|
233
|
-
rightBottomRef:
|
|
312
|
+
leftBottomRef: leftBottom(item(COMBINED_BELOW_W, BELOW_THRESHOLD)),
|
|
313
|
+
rightBottomRef: rightBottom(item(COMBINED_BELOW_W, BELOW_THRESHOLD))
|
|
234
314
|
}).bottom).toBe(BASE_BOTTOM)
|
|
235
315
|
})
|
|
236
316
|
})
|
|
@@ -239,10 +319,10 @@ describe('getSafeZoneInset — bottom edge', () => {
|
|
|
239
319
|
|
|
240
320
|
describe('getSafeZoneInset — MAX_RATIO cap', () => {
|
|
241
321
|
it('caps left inset at (MAX_RATIO-1)/MAX_RATIO of usable width', () => {
|
|
242
|
-
expect(getSafeZoneInset({ ...base(), leftTopRef:
|
|
322
|
+
expect(getSafeZoneInset({ ...base(), leftTopRef: leftTop(item(PANEL_W_XLARGE, PANEL_W_XLARGE)) }).left).toBe(CAP_WIDTH)
|
|
243
323
|
})
|
|
244
324
|
it('caps right inset at (MAX_RATIO-1)/MAX_RATIO of usable width', () => {
|
|
245
|
-
expect(getSafeZoneInset({ ...base(), rightTopRef:
|
|
325
|
+
expect(getSafeZoneInset({ ...base(), rightTopRef: rightTop(item(PANEL_W_XLARGE, PANEL_W_XLARGE)) }).right).toBe(CAP_WIDTH)
|
|
246
326
|
})
|
|
247
327
|
})
|
|
248
328
|
|
|
@@ -253,29 +333,136 @@ describe('getSafeZoneInset — MAX_RATIO cap', () => {
|
|
|
253
333
|
|
|
254
334
|
describe('getSafeZoneInset — row/column independence', () => {
|
|
255
335
|
it('narrow-but-tall panel triggers only its column inset, not the row', () => {
|
|
256
|
-
const result = getSafeZoneInset({ ...base(), rightTopRef:
|
|
336
|
+
const result = getSafeZoneInset({ ...base(), rightTopRef: rightTop(item(PANEL_W_STANDARD, ABOVE_THRESHOLD)) })
|
|
257
337
|
expect(result.right).toBe(rightInset(PANEL_W_STANDARD))
|
|
258
338
|
expect(result.top).toBe(BASE_TOP)
|
|
259
339
|
})
|
|
260
340
|
it('wide-but-short panel triggers only its row inset, not the column', () => {
|
|
261
|
-
const result = getSafeZoneInset({ ...base(), leftTopRef:
|
|
341
|
+
const result = getSafeZoneInset({ ...base(), leftTopRef: leftTop(item(ABOVE_W_THRESHOLD, BELOW_THRESHOLD)) })
|
|
262
342
|
expect(result.top).toBe(topInset(BELOW_THRESHOLD))
|
|
263
343
|
expect(result.left).toBe(BASE_LEFT)
|
|
264
344
|
})
|
|
265
|
-
it('wide-and-tall panel
|
|
266
|
-
const result = getSafeZoneInset({ ...base(), leftTopRef:
|
|
345
|
+
it('a wide-and-tall panel its side inset clears doesn\'t also push the top', () => {
|
|
346
|
+
const result = getSafeZoneInset({ ...base(), leftTopRef: leftTop(item(ABOVE_W_THRESHOLD, ABOVE_THRESHOLD)) })
|
|
267
347
|
expect(result.left).toBe(leftInset(ABOVE_W_THRESHOLD))
|
|
268
|
-
expect(result.top).toBe(
|
|
348
|
+
expect(result.top).toBe(BASE_TOP)
|
|
349
|
+
})
|
|
350
|
+
it('only panels on a side that isn\'t cleared count towards the top', () => {
|
|
351
|
+
// Left: two panels stacked, tall enough to clear the left. Right: one short panel. Together
|
|
352
|
+
// they'd be wide enough, but the right one alone isn't
|
|
353
|
+
const result = getSafeZoneInset({
|
|
354
|
+
...base(),
|
|
355
|
+
leftTopRef: leftTop(item(PANEL_W_WIDE, COMBINED_BELOW), item(PANEL_W_WIDE, ABOVE_THRESHOLD)),
|
|
356
|
+
rightTopRef: rightTop(item(PANEL_W_WIDE, COMBINED_BELOW))
|
|
357
|
+
})
|
|
358
|
+
expect(result.left).toBe(leftInset(PANEL_W_WIDE))
|
|
359
|
+
expect(result.top).toBe(BASE_TOP)
|
|
269
360
|
})
|
|
270
361
|
it('two narrow panels in the same column collectively trigger left, independent of row', () => {
|
|
271
362
|
// Each h=COMBINED_ABOVE (120) < hThreshold individually, combined 248 > 236 → left triggers
|
|
272
363
|
const result = getSafeZoneInset({
|
|
273
364
|
...base(),
|
|
274
|
-
leftTopRef:
|
|
275
|
-
leftBottomRef:
|
|
365
|
+
leftTopRef: leftTop(item(PANEL_W_STANDARD, COMBINED_ABOVE)),
|
|
366
|
+
leftBottomRef: leftBottom(item(PANEL_W_STANDARD, COMBINED_ABOVE))
|
|
367
|
+
})
|
|
368
|
+
expect(result.left).toBe(leftInset(PANEL_W_STANDARD))
|
|
369
|
+
expect(result.top).toBe(BASE_TOP)
|
|
370
|
+
expect(result.bottom).toBe(BASE_BOTTOM)
|
|
371
|
+
})
|
|
372
|
+
})
|
|
373
|
+
|
|
374
|
+
// ─── Every open panel in a slot ───────────────────────────────────────────────
|
|
375
|
+
|
|
376
|
+
describe('getSafeZoneInset — every open panel in a slot', () => {
|
|
377
|
+
it('counts an open panel after a closed one', () => {
|
|
378
|
+
const closed = item(PANEL_W_WIDE, ABOVE_THRESHOLD, { hidden: true })
|
|
379
|
+
expect(getSafeZoneInset({ ...base(), leftTopRef: leftTop(closed, item(PANEL_W_STANDARD, ABOVE_THRESHOLD)) }).left)
|
|
380
|
+
.toBe(leftInset(PANEL_W_STANDARD))
|
|
381
|
+
})
|
|
382
|
+
it('ignores a panel that is hidden but still has a size', () => {
|
|
383
|
+
expect(getSafeZoneInset({ ...base(), leftTopRef: leftTop(item(PANEL_W_STANDARD, ABOVE_THRESHOLD, { hidden: true })) }).left)
|
|
384
|
+
.toBe(BASE_LEFT)
|
|
385
|
+
})
|
|
386
|
+
it('stacks two open panels in one slot, with the gap between them', () => {
|
|
387
|
+
// 120 + 8 + 120 = 248 > 236
|
|
388
|
+
const result = getSafeZoneInset({
|
|
389
|
+
...base(),
|
|
390
|
+
leftTopRef: leftTop(item(PANEL_W_NARROW, COMBINED_ABOVE), item(PANEL_W_WIDE, COMBINED_ABOVE))
|
|
391
|
+
})
|
|
392
|
+
expect(result.left).toBe(leftInset(PANEL_W_WIDE))
|
|
393
|
+
})
|
|
394
|
+
it('counts panels either side of buttons in a slot', () => {
|
|
395
|
+
const result = getSafeZoneInset({
|
|
396
|
+
...base(),
|
|
397
|
+
leftTopRef: leftTop(item(PANEL_W_STANDARD, COMBINED_ABOVE), buttons(), item(PANEL_W_STANDARD, COMBINED_ABOVE))
|
|
276
398
|
})
|
|
277
399
|
expect(result.left).toBe(leftInset(PANEL_W_STANDARD))
|
|
400
|
+
})
|
|
401
|
+
it('counts a panel after the controls at the bottom of the right column', () => {
|
|
402
|
+
// A 260px panel, 300 tall, below the scale bar and move controls
|
|
403
|
+
expect(getSafeZoneInset({ ...base(), rightBottomRef: rightBottom(buttons(), buttons(), item(260, 300)) }).right)
|
|
404
|
+
.toBe(rightInset(260))
|
|
405
|
+
})
|
|
406
|
+
})
|
|
407
|
+
|
|
408
|
+
describe('getSafeZoneInset — panels measured where they are', () => {
|
|
409
|
+
it('pushes the top inset to the bottom of a wide panel below buttons', () => {
|
|
410
|
+
// Buttons 60–100, then the panel 108–208
|
|
411
|
+
expect(getSafeZoneInset({ ...base(), leftTopRef: leftTop(buttons(), item(ABOVE_W_THRESHOLD, 100)) }).top)
|
|
412
|
+
.toBe(208 + GAP)
|
|
413
|
+
})
|
|
414
|
+
it('pushes the bottom inset to the top of a wide panel above buttons in a bottom slot', () => {
|
|
415
|
+
// Buttons 512–552, then the panel 404–504 above them
|
|
416
|
+
expect(getSafeZoneInset({ ...base(), rightBottomRef: rightBottom(item(ABOVE_W_THRESHOLD, 100), buttons()) }).bottom)
|
|
417
|
+
.toBe(MAIN_HEIGHT - 404 + GAP)
|
|
418
|
+
})
|
|
419
|
+
it('matches the usual inset for a wide panel next to the footer, after buttons', () => {
|
|
420
|
+
expect(getSafeZoneInset({ ...base(), rightBottomRef: rightBottom(buttons(), item(ABOVE_W_THRESHOLD, 100)) }).bottom)
|
|
421
|
+
.toBe(bottomInset(100))
|
|
422
|
+
})
|
|
423
|
+
})
|
|
424
|
+
|
|
425
|
+
// ─── Fallback when there isn't room ───────────────────────────────────────────
|
|
426
|
+
|
|
427
|
+
describe('getSafeZoneInset — fallback when there isn\'t room', () => {
|
|
428
|
+
// Structural width 784, so the minimum is 196. Two 420px panels leave 900 - 438 - 438 = 24.
|
|
429
|
+
const PANEL_W_HALF = 420
|
|
430
|
+
// Structural height 472, so the minimum is 118. Panels 230 tall at the top and bottom leave
|
|
431
|
+
// 600 - 298 - 286 = 16.
|
|
432
|
+
const PANEL_H_HALF = 230
|
|
433
|
+
|
|
434
|
+
it('uses the structural left and right insets when panels on both sides leave too little width', () => {
|
|
435
|
+
const result = getSafeZoneInset({
|
|
436
|
+
...base(),
|
|
437
|
+
leftTopRef: leftTop(item(PANEL_W_HALF, ABOVE_THRESHOLD)),
|
|
438
|
+
rightTopRef: rightTop(item(PANEL_W_HALF, ABOVE_THRESHOLD))
|
|
439
|
+
})
|
|
440
|
+
expect(result.left).toBe(BASE_LEFT)
|
|
441
|
+
expect(result.right).toBe(BASE_RIGHT)
|
|
442
|
+
})
|
|
443
|
+
it('still counts panels for top and bottom when only the width falls back', () => {
|
|
444
|
+
// Together the two panels span the width, so they also push the top down, leaving 224 of height
|
|
445
|
+
const result = getSafeZoneInset({
|
|
446
|
+
...base(),
|
|
447
|
+
leftTopRef: leftTop(item(PANEL_W_HALF, ABOVE_THRESHOLD)),
|
|
448
|
+
rightTopRef: rightTop(item(PANEL_W_HALF, ABOVE_THRESHOLD))
|
|
449
|
+
})
|
|
450
|
+
expect(result.top).toBe(topInset(ABOVE_THRESHOLD))
|
|
451
|
+
})
|
|
452
|
+
it('uses the structural top and bottom insets when panels leave too little height', () => {
|
|
453
|
+
// Wide, short panels top left and bottom right push the top and bottom, but neither column is
|
|
454
|
+
// tall enough to clear its side. Together they leave less than 1/MIN_RATIO of the height
|
|
455
|
+
const result = getSafeZoneInset({
|
|
456
|
+
...base(),
|
|
457
|
+
leftTopRef: leftTop(item(ABOVE_W_THRESHOLD, PANEL_H_HALF)),
|
|
458
|
+
rightBottomRef: rightBottom(item(ABOVE_W_THRESHOLD, PANEL_H_HALF))
|
|
459
|
+
})
|
|
278
460
|
expect(result.top).toBe(BASE_TOP)
|
|
279
461
|
expect(result.bottom).toBe(BASE_BOTTOM)
|
|
462
|
+
expect(result.left).toBe(BASE_LEFT)
|
|
463
|
+
expect(result.right).toBe(BASE_RIGHT)
|
|
464
|
+
})
|
|
465
|
+
it('keeps a single panel at the cap, which leaves enough room', () => {
|
|
466
|
+
expect(getSafeZoneInset({ ...base(), leftTopRef: leftTop(item(PANEL_W_XLARGE, PANEL_W_XLARGE)) }).left).toBe(CAP_WIDTH)
|
|
280
467
|
})
|
|
281
468
|
})
|
|
@@ -1,14 +1,19 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Returns the appropriate color for the given mapStyleId.
|
|
2
|
+
* Returns the appropriate color or other value for the given mapStyleId.
|
|
3
3
|
* Supports:
|
|
4
4
|
* - Simple string colors (#fff, rgba(...))
|
|
5
|
-
* -
|
|
5
|
+
* - Numbers
|
|
6
|
+
* - Object maps of mapStyleId → color or number
|
|
6
7
|
*
|
|
7
|
-
* @param {string|object} colors - Color string or style-mapped
|
|
8
|
+
* @param {string|number|object} colors - Color string, number or style-mapped object of either
|
|
8
9
|
* @param {string} mapStyleId - Current style/theme identifier
|
|
9
|
-
* @returns {string|null}
|
|
10
|
+
* @returns {string|number|null}
|
|
10
11
|
*/
|
|
11
12
|
export const getValueForStyle = (colors, mapStyleId) => {
|
|
13
|
+
if (typeof colors === 'number') {
|
|
14
|
+
return colors
|
|
15
|
+
}
|
|
16
|
+
|
|
12
17
|
if (!colors) {
|
|
13
18
|
return null
|
|
14
19
|
}
|
|
@@ -21,7 +26,7 @@ export const getValueForStyle = (colors, mapStyleId) => {
|
|
|
21
26
|
// --- Case 2: mapped color object
|
|
22
27
|
if (typeof colors === 'object') {
|
|
23
28
|
// Style-specific match
|
|
24
|
-
if (mapStyleId && colors[mapStyleId]) {
|
|
29
|
+
if (mapStyleId && colors[mapStyleId] != null) {
|
|
25
30
|
return colors[mapStyleId]
|
|
26
31
|
}
|
|
27
32
|
|
|
@@ -49,9 +49,20 @@ describe('getValueForStyle', () => {
|
|
|
49
49
|
expect(getValueForStyle(colorMap)).toBe('#000')
|
|
50
50
|
})
|
|
51
51
|
|
|
52
|
-
// --- Case 8:
|
|
52
|
+
// --- Case 8: numbers
|
|
53
|
+
it('returns a number, including zero', () => {
|
|
54
|
+
expect(getValueForStyle(42, 'anyStyle')).toBe(42)
|
|
55
|
+
expect(getValueForStyle(0, 'anyStyle')).toBe(0)
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
it('returns the style-specific number, including zero', () => {
|
|
59
|
+
const widths = { outdoor: 2, dark: 0 }
|
|
60
|
+
expect(getValueForStyle(widths, 'outdoor')).toBe(2)
|
|
61
|
+
expect(getValueForStyle(widths, 'dark')).toBe(0)
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
// --- Case 9: fallback
|
|
53
65
|
it('returns null for unsupported types to hit final return null', () => {
|
|
54
|
-
expect(getValueForStyle(42, 'anyStyle')).toBeNull() // number
|
|
55
66
|
expect(getValueForStyle(true, 'anyStyle')).toBeNull() // boolean
|
|
56
67
|
expect(getValueForStyle([], 'anyStyle')).toBeNull() // array
|
|
57
68
|
expect(getValueForStyle(() => {}, 'anyStyle')).toBeNull() // function
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The boxes of what an element's children show. Plugin controls are wrapped in an element with no
|
|
3
|
+
* box of its own (display: contents), so a wrapper's boxes are those of what's shown inside it.
|
|
4
|
+
*
|
|
5
|
+
* @param {Element} [element]
|
|
6
|
+
* @returns {DOMRect[]}
|
|
7
|
+
*/
|
|
8
|
+
export const renderedBoxes = (element) => Array.from(element?.children ?? []).flatMap(child => {
|
|
9
|
+
if (child.hidden) {
|
|
10
|
+
return []
|
|
11
|
+
}
|
|
12
|
+
if (child.getClientRects().length > 0) {
|
|
13
|
+
return [child.getBoundingClientRect()]
|
|
14
|
+
}
|
|
15
|
+
return child.classList.contains('im-c-control-wrapper') ? renderedBoxes(child) : []
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* A top row column's height. One with only hidden items still has its bottom padding, which would
|
|
20
|
+
* push the side column below it down, so it counts as empty.
|
|
21
|
+
*
|
|
22
|
+
* @param {Element} col
|
|
23
|
+
* @returns {number}
|
|
24
|
+
*/
|
|
25
|
+
export const topColHeight = (col) => col.children.length > 0 && renderedBoxes(col).length === 0 ? 0 : col.offsetHeight
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { renderedBoxes, topColHeight } from './renderedBoxes.js'
|
|
2
|
+
|
|
3
|
+
const BOX = { top: 0, left: 0, right: 40, bottom: 40, width: 40, height: 40 }
|
|
4
|
+
|
|
5
|
+
// jsdom gives every element empty client rects, i.e. the same as display:none
|
|
6
|
+
const child = ({ isShown = true, hidden = false, className = '' } = {}) => {
|
|
7
|
+
const element = document.createElement('div')
|
|
8
|
+
element.hidden = hidden
|
|
9
|
+
element.className = className
|
|
10
|
+
if (isShown) {
|
|
11
|
+
element.getClientRects = () => [BOX]
|
|
12
|
+
element.getBoundingClientRect = () => BOX
|
|
13
|
+
}
|
|
14
|
+
return element
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const parentOf = (...children) => {
|
|
18
|
+
const parent = document.createElement('div')
|
|
19
|
+
children.forEach(item => parent.appendChild(item))
|
|
20
|
+
return parent
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
describe('renderedBoxes', () => {
|
|
24
|
+
it('returns the boxes of shown children', () => {
|
|
25
|
+
expect(renderedBoxes(parentOf(child(), child()))).toEqual([BOX, BOX])
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
it('skips hidden children and children with no box', () => {
|
|
29
|
+
expect(renderedBoxes(parentOf(child({ hidden: true }), child({ isShown: false })))).toEqual([])
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
it('looks inside control wrappers, which have no box of their own', () => {
|
|
33
|
+
const wrapper = parentOf(child())
|
|
34
|
+
wrapper.className = 'im-c-control-wrapper'
|
|
35
|
+
expect(renderedBoxes(parentOf(wrapper))).toEqual([BOX])
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
it('returns nothing for a missing element', () => {
|
|
39
|
+
expect(renderedBoxes(null)).toEqual([])
|
|
40
|
+
})
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
describe('topColHeight', () => {
|
|
44
|
+
const col = (...children) => {
|
|
45
|
+
const element = parentOf(...children)
|
|
46
|
+
Object.defineProperty(element, 'offsetHeight', { value: 48 })
|
|
47
|
+
return element
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
it('is the column height when anything in it shows', () => {
|
|
51
|
+
expect(topColHeight(col(child({ hidden: true }), child()))).toBe(48)
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
it('is 0 when everything in it is hidden, ignoring its padding', () => {
|
|
55
|
+
expect(topColHeight(col(child({ hidden: true })))).toBe(0)
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
it('is the column height when it has no children', () => {
|
|
59
|
+
expect(topColHeight(col())).toBe(48)
|
|
60
|
+
})
|
|
61
|
+
})
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import turfBbox from '@turf/bbox'
|
|
2
|
+
|
|
3
|
+
// A box is { left, top, right, bottom } in pixels, measured from the same origin as the boxes it's
|
|
4
|
+
// compared with.
|
|
5
|
+
|
|
6
|
+
// [west, south, east, north] from a flat or nested bounds array, or any GeoJSON object
|
|
7
|
+
const getFlatBounds = (bounds) => {
|
|
8
|
+
if (Array.isArray(bounds)) {
|
|
9
|
+
return bounds.flat()
|
|
10
|
+
}
|
|
11
|
+
return turfBbox(bounds)
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* The on-screen box that bounds a geometry, from its projected corners.
|
|
16
|
+
*
|
|
17
|
+
* @param {[number, number, number, number] | object} bounds - Bounds array or GeoJSON.
|
|
18
|
+
* @param {function([number, number]): { x: number, y: number }} mapToScreen
|
|
19
|
+
* @param {number} [scale] - Multiplies the projected pixels, e.g. to turn map pixels into CSS pixels.
|
|
20
|
+
* @returns {{ left: number, top: number, right: number, bottom: number }}
|
|
21
|
+
*/
|
|
22
|
+
export const getScreenBox = (bounds, mapToScreen, scale = 1) => {
|
|
23
|
+
const [west, south, east, north] = getFlatBounds(bounds)
|
|
24
|
+
const corners = [[west, south], [west, north], [east, south], [east, north]].map(mapToScreen)
|
|
25
|
+
const xs = corners.map(point => point.x * scale)
|
|
26
|
+
const ys = corners.map(point => point.y * scale)
|
|
27
|
+
return { left: Math.min(...xs), top: Math.min(...ys), right: Math.max(...xs), bottom: Math.max(...ys) }
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* An element's rect as a box relative to another element's rect.
|
|
32
|
+
*
|
|
33
|
+
* @param {DOMRect} rect
|
|
34
|
+
* @param {DOMRect} origin
|
|
35
|
+
* @returns {{ left: number, top: number, right: number, bottom: number }}
|
|
36
|
+
*/
|
|
37
|
+
export const getRelativeBox = (rect, origin) => ({
|
|
38
|
+
left: rect.left - origin.left,
|
|
39
|
+
top: rect.top - origin.top,
|
|
40
|
+
right: rect.right - origin.left,
|
|
41
|
+
bottom: rect.bottom - origin.top
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* The middle of a box.
|
|
46
|
+
*
|
|
47
|
+
* @param {{ left: number, top: number, right: number, bottom: number }} box
|
|
48
|
+
* @returns {[number, number]} `[x, y]`
|
|
49
|
+
*/
|
|
50
|
+
export const getBoxCenter = (box) => [(box.left + box.right) / 2, (box.top + box.bottom) / 2]
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Whether `box` lies entirely within `area`. Touching an edge counts as inside.
|
|
54
|
+
*
|
|
55
|
+
* @param {{ left: number, top: number, right: number, bottom: number }} box
|
|
56
|
+
* @param {{ left: number, top: number, right: number, bottom: number }} area
|
|
57
|
+
* @returns {boolean}
|
|
58
|
+
*/
|
|
59
|
+
export const isInside = (box, area) =>
|
|
60
|
+
box.left >= area.left && box.right <= area.right && box.top >= area.top && box.bottom <= area.bottom
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Whether two boxes overlap. Boxes that only touch at an edge don't.
|
|
64
|
+
*
|
|
65
|
+
* @param {{ left: number, top: number, right: number, bottom: number }} box
|
|
66
|
+
* @param {{ left: number, top: number, right: number, bottom: number }} area
|
|
67
|
+
* @returns {boolean}
|
|
68
|
+
*/
|
|
69
|
+
export const isOverlapping = (box, area) =>
|
|
70
|
+
box.left < area.right && box.right > area.left && box.top < area.bottom && box.bottom > area.top
|