@defra/interactive-map 0.0.56-alpha → 0.0.57-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 (137) hide show
  1. package/dist/esm/im-core.js +1 -1
  2. package/dist/umd/im-core.js +1 -1
  3. package/dist/umd/index.js +1 -1
  4. package/docs/api/button-definition.md +11 -0
  5. package/docs/api/context.md +2 -0
  6. package/docs/api.md +3 -1
  7. package/docs/plugins/draw.md +189 -33
  8. package/package.json +1 -1
  9. package/plugins/datasets/dist/esm/im-datasets-plugin.js +1 -1
  10. package/plugins/datasets/dist/umd/im-datasets-plugin.js +1 -1
  11. package/plugins/datasets/dist/umd/index.js +1 -1
  12. package/plugins/datasets/src/initialise/DatasetsInit.jsx +6 -17
  13. package/plugins/datasets/src/initialise/DatasetsInit.test.jsx +3 -43
  14. package/plugins/draw/dist/esm/im-draw-ml-adapter.js +1 -1
  15. package/plugins/draw/dist/esm/im-draw-ol-adapter.js +1 -1
  16. package/plugins/draw/dist/esm/im-draw-plugin.js +1 -1
  17. package/plugins/draw/dist/esm/index.js +1 -1
  18. package/plugins/draw/dist/esm/liveDrawChecks.js +1 -1
  19. package/plugins/draw/dist/umd/im-draw-ml-adapter.js +1 -1
  20. package/plugins/draw/dist/umd/im-draw-ol-adapter.js +1 -1
  21. package/plugins/draw/dist/umd/im-draw-plugin.js +1 -1
  22. package/plugins/draw/dist/umd/index.js +1 -1
  23. package/plugins/draw/src/DrawInit.jsx +9 -6
  24. package/plugins/draw/src/DrawInit.test.jsx +15 -0
  25. package/plugins/draw/src/adapterEvents.js +78 -44
  26. package/plugins/draw/src/adapters/adapterContract.test.js +3 -3
  27. package/plugins/draw/src/adapters/maplibre/MaplibreDrawAdapter.js +18 -10
  28. package/plugins/draw/src/adapters/maplibre/MaplibreDrawAdapter.test.js +31 -22
  29. package/plugins/draw/src/adapters/maplibre/drawEvents.js +2 -1
  30. package/plugins/draw/src/adapters/maplibre/modes/drawMode/clickHandlers.js +13 -12
  31. package/plugins/draw/src/adapters/maplibre/modes/drawMode/clickHandlers.test.js +5 -5
  32. package/plugins/draw/src/adapters/maplibre/modes/drawMode/undoHandlers.js +1 -1
  33. package/plugins/draw/src/adapters/maplibre/modes/drawMode/undoHandlers.test.js +2 -2
  34. package/plugins/draw/src/adapters/maplibre/modes/drawPointMode.js +6 -9
  35. package/plugins/draw/src/adapters/maplibre/modes/drawPointMode.test.js +4 -4
  36. package/plugins/draw/src/adapters/maplibre/modes/editPointMode/pointerHandlers.js +6 -9
  37. package/plugins/draw/src/adapters/maplibre/modes/editPointMode/pointerHandlers.test.js +1 -1
  38. package/plugins/draw/src/adapters/maplibre/modes/editPointMode/undoHandlers.js +8 -14
  39. package/plugins/draw/src/adapters/maplibre/modes/editPointMode/undoHandlers.test.js +37 -8
  40. package/plugins/draw/src/adapters/maplibre/modes/editPointMode.js +11 -1
  41. package/plugins/draw/src/adapters/maplibre/modes/editVertexMode/undoHandlers.js +14 -19
  42. package/plugins/draw/src/adapters/maplibre/modes/editVertexMode/undoHandlers.test.js +41 -4
  43. package/plugins/draw/src/adapters/maplibre/modes/editVertexMode.js +12 -1
  44. package/plugins/draw/src/adapters/maplibre/utils/editModeEvents.js +3 -1
  45. package/plugins/draw/src/adapters/maplibre/utils/geometryValidation.js +9 -10
  46. package/plugins/draw/src/adapters/maplibre/utils/geometryValidation.test.js +5 -24
  47. package/plugins/draw/src/adapters/openlayers/OLDrawAdapter.js +10 -7
  48. package/plugins/draw/src/adapters/openlayers/OLDrawAdapter.test.js +12 -7
  49. package/plugins/draw/src/adapters/openlayers/core/OLDrawManager.js +5 -0
  50. package/plugins/draw/src/adapters/openlayers/core/OLDrawManager.test.js +7 -3
  51. package/plugins/draw/src/adapters/openlayers/draw/DrawMode.js +11 -11
  52. package/plugins/draw/src/adapters/openlayers/draw/DrawMode.test.js +15 -15
  53. package/plugins/draw/src/adapters/openlayers/edit/EditMode.js +21 -28
  54. package/plugins/draw/src/adapters/openlayers/edit/EditMode.test.js +26 -5
  55. package/plugins/draw/src/adapters/openlayers/edit/selectionState.js +5 -6
  56. package/plugins/draw/src/adapters/openlayers/edit/selectionState.test.js +2 -12
  57. package/plugins/draw/src/adapters/openlayers/edit/undoOps.js +9 -0
  58. package/plugins/draw/src/adapters/openlayers/edit/undoOps.test.js +7 -0
  59. package/plugins/draw/src/adapters/openlayers/point/drawPointMode.js +6 -9
  60. package/plugins/draw/src/adapters/openlayers/point/drawPointMode.test.js +3 -3
  61. package/plugins/draw/src/adapters/openlayers/point/editPointMode.js +18 -9
  62. package/plugins/draw/src/adapters/openlayers/point/editPointMode.test.js +29 -4
  63. package/plugins/draw/src/adapters/openlayers/point/pointOps.js +3 -2
  64. package/plugins/draw/src/adapters/openlayers/point/pointOps.test.js +6 -0
  65. package/plugins/draw/src/adapters/openlayers/point/pointSelectionState.js +3 -4
  66. package/plugins/draw/src/adapters/openlayers/point/pointSelectionState.test.js +2 -12
  67. package/plugins/draw/src/api/createNewShape.js +4 -4
  68. package/plugins/draw/src/api/createNewShape.test.js +1 -1
  69. package/plugins/draw/src/api/editFeature.js +8 -11
  70. package/plugins/draw/src/api/editFeature.test.js +33 -22
  71. package/plugins/draw/src/api/newLine.test.js +3 -3
  72. package/plugins/draw/src/api/newPoint.js +4 -5
  73. package/plugins/draw/src/api/newPoint.test.js +3 -3
  74. package/plugins/draw/src/api/newPolygon.test.js +3 -3
  75. package/plugins/draw/src/api/recheckGeometry.js +32 -0
  76. package/plugins/draw/src/api/recheckGeometry.test.js +55 -0
  77. package/plugins/draw/src/api/setGeometry.js +33 -0
  78. package/plugins/draw/src/api/setGeometry.test.js +48 -0
  79. package/plugins/draw/src/api/split.js +23 -22
  80. package/plugins/draw/src/api/split.test.js +33 -18
  81. package/plugins/draw/src/events.js +265 -77
  82. package/plugins/draw/src/events.test.js +708 -34
  83. package/plugins/draw/src/hooks/useSpatialList.js +10 -11
  84. package/plugins/draw/src/hooks/useSpatialList.test.js +3 -3
  85. package/plugins/draw/src/manifest.js +30 -15
  86. package/plugins/draw/src/manifest.test.js +91 -9
  87. package/plugins/draw/src/reducer.js +31 -9
  88. package/plugins/draw/src/reducer.test.js +35 -3
  89. package/plugins/draw/src/utils/drawModes.js +27 -0
  90. package/plugins/draw/src/utils/drawModes.test.js +27 -0
  91. package/plugins/draw/src/utils/needsDrawTarget.js +1 -5
  92. package/plugins/draw/src/utils/needsDrawTarget.test.js +1 -11
  93. package/plugins/draw/src/utils/touchTarget.js +34 -26
  94. package/plugins/draw/src/utils/touchTarget.test.js +32 -20
  95. package/plugins/draw/src/validation/commitChecks.js +185 -0
  96. package/plugins/draw/src/validation/commitChecks.test.js +205 -0
  97. package/plugins/draw/src/validation/liveDrawChecks.js +11 -11
  98. package/plugins/draw/src/validation/liveDrawChecks.test.js +37 -37
  99. package/plugins/draw/src/validation/liveStroke.js +6 -6
  100. package/plugins/draw/src/validation/liveStroke.test.js +27 -27
  101. package/plugins/draw/src/validation/rules.js +16 -14
  102. package/plugins/draw/src/validation/rules.test.js +5 -5
  103. package/plugins/draw/src/validation/validateGeometry.js +58 -31
  104. package/plugins/draw/src/validation/validateGeometry.test.js +68 -44
  105. package/plugins/interact/dist/esm/im-interact-plugin.js +1 -1
  106. package/plugins/interact/dist/umd/im-interact-plugin.js +1 -1
  107. package/plugins/interact/dist/umd/index.js +1 -1
  108. package/plugins/interact/src/hooks/useInteractionHandlers.js +3 -5
  109. package/plugins/interact/src/hooks/useInteractionHandlers.test.js +4 -3
  110. package/plugins/interact/src/hooks/useSpatialList.js +16 -17
  111. package/plugins/interact/src/hooks/useSpatialList.test.js +9 -9
  112. package/plugins/map-styles/dist/esm/im-map-styles-plugin.js +1 -1
  113. package/plugins/map-styles/dist/umd/im-map-styles-plugin.js +1 -1
  114. package/plugins/map-styles/src/MapStyles.jsx +2 -2
  115. package/providers/maplibre/dist/esm/index.js +1 -1
  116. package/providers/maplibre/dist/umd/index.js +1 -1
  117. package/providers/maplibre/src/index.js +11 -2
  118. package/providers/maplibre/src/index.test.js +23 -1
  119. package/providers/maplibre/src/utils/detectModuleWorker.js +8 -19
  120. package/providers/maplibre/src/utils/detectModuleWorker.test.js +19 -23
  121. package/providers/maplibre/src/utils/workerUrl.js +12 -0
  122. package/providers/maplibre/src/utils/workerUrl.test.js +14 -1
  123. package/src/App/components/MapButton/MapButton.jsx +6 -0
  124. package/src/App/components/MapButton/MapButton.test.jsx +10 -0
  125. package/src/App/components/Markers/Markers.jsx +3 -5
  126. package/src/App/components/Markers/Markers.test.jsx +9 -11
  127. package/src/App/components/Panel/Panel.jsx +2 -9
  128. package/src/App/hooks/useInterfaceAPI.js +16 -4
  129. package/src/App/hooks/useInterfaceAPI.test.js +66 -1
  130. package/src/App/renderer/mapButtons.js +2 -1
  131. package/src/App/renderer/mapButtons.test.js +9 -0
  132. package/src/App/store/MapProvider.jsx +3 -0
  133. package/src/App/store/MapProvider.test.jsx +24 -0
  134. package/src/config/mapSizeScales.js +3 -0
  135. package/src/types.js +4 -0
  136. package/src/utils/focusAfterPanelClose.js +9 -0
  137. package/src/utils/focusAfterPanelClose.test.js +40 -0
@@ -2,9 +2,11 @@ import { renderHook, act } from '@testing-library/react'
2
2
  import { useInterfaceAPI } from './useInterfaceAPI.js'
3
3
  import { useApp } from '../store/appContext.js'
4
4
  import { useService } from '../store/serviceContext.js'
5
+ import { useConfig } from '../store/configContext.js'
5
6
 
6
7
  jest.mock('../store/appContext.js')
7
8
  jest.mock('../store/serviceContext.js')
9
+ jest.mock('../store/configContext.js')
8
10
 
9
11
  const makeEventBus = () => {
10
12
  const handlers = {}
@@ -26,11 +28,15 @@ describe('useInterfaceAPI', () => {
26
28
  hiddenButtons: new Set(),
27
29
  disabledButtons: new Set(),
28
30
  pressedButtons: new Set(),
29
- expandedButtons: new Set()
31
+ expandedButtons: new Set(),
32
+ openPanels: {},
33
+ layoutRefs: { viewportRef: { current: { focus: jest.fn() } } },
34
+ interfaceType: 'keyboard'
30
35
  }
31
36
 
32
37
  useApp.mockReturnValue({ dispatch: mockDispatch, ...mockState })
33
38
  useService.mockReturnValue({ eventBus: mockEventBus })
39
+ useConfig.mockReturnValue({ id: 'map' })
34
40
  })
35
41
 
36
42
  it('sets a mode on app:setapplicationmode', () => {
@@ -122,6 +128,65 @@ describe('useInterfaceAPI', () => {
122
128
  expect(mockDispatch).toHaveBeenCalledWith({ type: 'CLOSE_PANEL', payload: 'panel1' })
123
129
  })
124
130
 
131
+ describe('focus on app:hidepanel', () => {
132
+ let panelEl, panelButton, triggeringElement, outsideButton
133
+
134
+ beforeEach(() => {
135
+ jest.spyOn(window, 'requestAnimationFrame').mockImplementation((cb) => cb())
136
+ panelEl = document.createElement('div')
137
+ panelEl.id = 'map-panel-panel1'
138
+ panelButton = panelEl.appendChild(document.createElement('button'))
139
+ triggeringElement = document.createElement('button')
140
+ outsideButton = document.createElement('button')
141
+ document.body.append(panelEl, triggeringElement, outsideButton)
142
+ jest.spyOn(triggeringElement, 'focus')
143
+ })
144
+
145
+ afterEach(() => {
146
+ window.requestAnimationFrame.mockRestore()
147
+ document.body.innerHTML = ''
148
+ })
149
+
150
+ const renderWithOpenPanel = (props) => {
151
+ useApp.mockReturnValue({ dispatch: mockDispatch, ...mockState, openPanels: { panel1: { props } } })
152
+ renderHook(() => useInterfaceAPI())
153
+ }
154
+
155
+ it('focuses the triggering element when focus is inside the panel', () => {
156
+ renderWithOpenPanel({ triggeringElement })
157
+ panelButton.focus()
158
+ act(() => mockEventBus.emit('app:hidepanel', 'panel1'))
159
+ expect(triggeringElement.focus).toHaveBeenCalledWith({ preventScroll: false })
160
+ expect(mockDispatch).toHaveBeenCalledWith({ type: 'CLOSE_PANEL', payload: 'panel1' })
161
+ })
162
+
163
+ it('focuses the map when focus is inside the panel and the triggering element has gone', () => {
164
+ triggeringElement.remove()
165
+ renderWithOpenPanel({ triggeringElement })
166
+ panelButton.focus()
167
+ act(() => mockEventBus.emit('app:hidepanel', 'panel1'))
168
+ expect(triggeringElement.focus).not.toHaveBeenCalled()
169
+ expect(mockState.layoutRefs.viewportRef.current.focus).toHaveBeenCalled()
170
+ })
171
+
172
+ it('focuses the map when focus is inside a panel opened without a triggering element', () => {
173
+ renderWithOpenPanel({})
174
+ panelButton.focus()
175
+ act(() => mockEventBus.emit('app:hidepanel', 'panel1'))
176
+ expect(mockState.layoutRefs.viewportRef.current.focus).toHaveBeenCalled()
177
+ })
178
+
179
+ it('leaves focus where it is when focus is outside the panel', () => {
180
+ renderWithOpenPanel({ triggeringElement })
181
+ outsideButton.focus()
182
+ act(() => mockEventBus.emit('app:hidepanel', 'panel1'))
183
+ expect(triggeringElement.focus).not.toHaveBeenCalled()
184
+ expect(mockState.layoutRefs.viewportRef.current.focus).not.toHaveBeenCalled()
185
+ expect(document.activeElement).toBe(outsideButton)
186
+ expect(mockDispatch).toHaveBeenCalledWith({ type: 'CLOSE_PANEL', payload: 'panel1' })
187
+ })
188
+ })
189
+
125
190
  it('dispatches ADD_CONTROL on app:addcontrol', () => {
126
191
  renderHook(() => useInterfaceAPI())
127
192
  act(() => mockEventBus.emit('app:addcontrol', { id: 'ctrl1', config: { position: 'top-left' } }))
@@ -116,7 +116,7 @@ const slotButtonProps = ({ buttonId, config, isHidden, appState, appConfig, eval
116
116
  * Renders a single button's MapButton element for its slot, computing its click handler
117
117
  * and derived state (panel-open, showLabel fallback, etc.) from its config and appState.
118
118
  */
119
- function SlotButton ({ buttonId, config, isHidden = false, appState, appConfig, evaluateProp }) {
119
+ function SlotButton ({ buttonId, config, isHidden = false, appState, appConfig, evaluateProp }) { // NOSONAR, the inline props read more clearly than a helper would
120
120
  const bpConfig = config[appState.breakpoint] ?? {}
121
121
  const handleClick = createButtonClickHandler(config, appState, evaluateProp)
122
122
  const isPanelOpen = !!(config.panelId && appState.openPanels[config.panelId])
@@ -136,6 +136,7 @@ function SlotButton ({ buttonId, config, isHidden = false, appState, appConfig,
136
136
  isHidden={isHidden}
137
137
  isPressed={(config.isPressed !== undefined || config.pressedWhen) ? appState.pressedButtons.has(buttonId) : undefined}
138
138
  isExpanded={(config.isExpanded !== undefined || config.expandedWhen) ? appState.expandedButtons.has(buttonId) : undefined}
139
+ isBusy={config.busyWhen ? !!evaluateProp(config.busyWhen, config.pluginId) : undefined}
139
140
  isPanelOpen={isPanelOpen}
140
141
  panelRole={panelRole}
141
142
  onClick={handleClick}
@@ -165,6 +165,15 @@ describe('mapButtons module', () => {
165
165
  expect(result.props).toMatchObject({ isDisabled: true, isHidden: true, isPressed: true, isExpanded: true })
166
166
  })
167
167
 
168
+ it('evaluates busyWhen on render, and leaves isBusy undefined without it', () => {
169
+ const busyEvaluate = jest.fn((prop) => (typeof prop === 'function' ? prop() : prop))
170
+ const busy = SlotButton({ buttonId: 'id', config: { ...baseBtn, busyWhen: () => true }, appState, appConfig, evaluateProp: busyEvaluate })
171
+ expect(busy.props.isBusy).toBe(true)
172
+ const idle = SlotButton({ buttonId: 'id', config: { ...baseBtn, busyWhen: () => false }, appState, appConfig, evaluateProp: busyEvaluate })
173
+ expect(idle.props.isBusy).toBe(false)
174
+ expect(render(baseBtn).props.isBusy).toBeUndefined()
175
+ })
176
+
168
177
  it.each([
169
178
  [{ slot: 'right-top', open: false }, 'dialog'],
170
179
  [{ slot: 'right-top', open: false, dismissible: false }, 'region'],
@@ -2,6 +2,7 @@
2
2
  import React, { createContext, useEffect, useReducer, useMemo, useRef } from 'react'
3
3
  import { initialState, reducer } from './mapReducer.js'
4
4
  import { EVENTS as events } from '../../config/events.js'
5
+ import { getMapSizeScale } from '../../config/mapSizeScales.js'
5
6
 
6
7
  export const MapContext = createContext(null)
7
8
 
@@ -68,8 +69,10 @@ export const MapProvider = ({ options, children }) => {
68
69
  localStorage.setItem(`${options.id}:mapSize`, state.mapSize)
69
70
  }, [state.mapStyle, state.mapSize])
70
71
 
72
+ // mapSizeScale is worked out from mapSize here rather than stored, so the two can't drift apart
71
73
  const mapStore = useMemo(() => ({
72
74
  ...state,
75
+ mapSizeScale: getMapSizeScale(state.mapSize),
73
76
  dispatch
74
77
  }), [state])
75
78
 
@@ -125,6 +125,30 @@ describe('MapProvider', () => {
125
125
  expect(contextValue.isMapReady).toBe(true)
126
126
  })
127
127
 
128
+ test.each([
129
+ ['small', 1],
130
+ ['medium', 1.5],
131
+ ['large', 2],
132
+ ['unknown', 1]
133
+ ])('provides mapSizeScale for map size %s', (mapSize, expected) => {
134
+ let contextValue
135
+ const Child = () => {
136
+ contextValue = React.useContext(MapContext)
137
+ return null
138
+ }
139
+ render(
140
+ <MapProvider options={{ id: 'map1', mapSize: 'small', eventBus: mockEventBus }}>
141
+ <Child />
142
+ </MapProvider>
143
+ )
144
+
145
+ act(() => {
146
+ capturedHandlers['map:setsize'](mapSize)
147
+ })
148
+
149
+ expect(contextValue.mapSizeScale).toBe(expected)
150
+ })
151
+
128
152
  test('emits map:sizechange when mapSize changes after initial value', () => {
129
153
  render(
130
154
  <MapProvider options={{ id: 'map1', mapSize: '100x100', eventBus: mockEventBus }}>
@@ -6,3 +6,6 @@ export const scaleFactor = {
6
6
  medium: 1.5,
7
7
  large: 2
8
8
  }
9
+
10
+ // Scale for a map size, falling back to 1 for an unknown or unset size
11
+ export const getMapSizeScale = (mapSize) => scaleFactor[mapSize] ?? 1
package/src/types.js CHANGED
@@ -257,6 +257,10 @@
257
257
  * Callback to determine if the button should appear pressed. Sets aria-pressed accordingly.
258
258
  * Only evaluated for plugin-defined buttons; use toggleButtonState() for host buttons.
259
259
  *
260
+ * @property {(context: PluginContext) => boolean} [busyWhen]
261
+ * Callback to determine if the action behind the button is in progress. Sets aria-busy
262
+ * accordingly. Evaluated on every render, like a function label.
263
+ *
260
264
  * @property {ButtonBreakpointConfig} tablet
261
265
  * Tablet breakpoint configuration.
262
266
  */
@@ -0,0 +1,9 @@
1
+ // Moves focus to the element that opened a closed panel, or to the map viewport if that element
2
+ // has since left the page (e.g. a Features-list item that dropped out of the list). Runs in the
3
+ // next frame so a modal panel has released its focus trap and inert state first.
4
+ export const focusAfterPanelClose = ({ triggeringElement, viewportRef, interfaceType }) => {
5
+ requestAnimationFrame(() => {
6
+ const focusTarget = triggeringElement && document.contains(triggeringElement) ? triggeringElement : viewportRef.current
7
+ focusTarget?.focus?.({ preventScroll: interfaceType !== 'keyboard' })
8
+ })
9
+ }
@@ -0,0 +1,40 @@
1
+ import { focusAfterPanelClose } from './focusAfterPanelClose.js'
2
+
3
+ describe('focusAfterPanelClose', () => {
4
+ let viewportRef
5
+
6
+ beforeEach(() => {
7
+ jest.spyOn(window, 'requestAnimationFrame').mockImplementation((cb) => cb())
8
+ viewportRef = { current: { focus: jest.fn() } }
9
+ })
10
+
11
+ afterEach(() => {
12
+ window.requestAnimationFrame.mockRestore()
13
+ document.body.innerHTML = ''
14
+ })
15
+
16
+ it('focuses the triggering element when it is still on the page', () => {
17
+ const triggeringElement = document.body.appendChild(document.createElement('button'))
18
+ const focusSpy = jest.spyOn(triggeringElement, 'focus')
19
+ focusAfterPanelClose({ triggeringElement, viewportRef, interfaceType: 'keyboard' })
20
+ expect(focusSpy).toHaveBeenCalledWith({ preventScroll: false })
21
+ expect(viewportRef.current.focus).not.toHaveBeenCalled()
22
+ })
23
+
24
+ it('focuses the viewport when the triggering element has left the page', () => {
25
+ const triggeringElement = document.createElement('button')
26
+ focusAfterPanelClose({ triggeringElement, viewportRef, interfaceType: 'pointer' })
27
+ expect(viewportRef.current.focus).toHaveBeenCalledWith({ preventScroll: true })
28
+ })
29
+
30
+ it('focuses the viewport when there is no triggering element', () => {
31
+ focusAfterPanelClose({ viewportRef, interfaceType: 'keyboard' })
32
+ expect(viewportRef.current.focus).toHaveBeenCalledWith({ preventScroll: false })
33
+ })
34
+
35
+ it('waits for the next frame before moving focus', () => {
36
+ window.requestAnimationFrame.mockImplementation(() => 1)
37
+ focusAfterPanelClose({ viewportRef, interfaceType: 'keyboard' })
38
+ expect(viewportRef.current.focus).not.toHaveBeenCalled()
39
+ })
40
+ })