@defra/interactive-map 0.0.58-alpha → 0.0.60-alpha

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (140) hide show
  1. package/assets/templates/add-polygons.njk +2 -2
  2. package/assets/templates/add-symbols.njk +2 -2
  3. package/dist/css/index.css +1 -1
  4. package/dist/esm/im-core.js +1 -1
  5. package/dist/esm/im-shell.js +1 -1
  6. package/dist/umd/im-core.js +1 -1
  7. package/dist/umd/index.js +1 -1
  8. package/docs/api/panel-definition.md +30 -0
  9. package/docs/api/symbol-config.md +12 -0
  10. package/docs/api.md +21 -2
  11. package/docs/assets/images/slot-map.svg +3 -3
  12. package/docs/plugins/datasets.md +11 -1
  13. package/docs/plugins/draw.md +95 -13
  14. package/package.json +1 -1
  15. package/plugins/datasets/dist/esm/index.js +1 -1
  16. package/plugins/datasets/dist/esm/layerAdapter.js +1 -1
  17. package/plugins/datasets/dist/umd/im-datasets-ml-adapter.js +1 -1
  18. package/plugins/datasets/dist/umd/im-datasets-ol-adapter.js +1 -1
  19. package/plugins/datasets/dist/umd/im-datasets-plugin.js +1 -1
  20. package/plugins/datasets/dist/umd/index.js +1 -1
  21. package/plugins/datasets/src/index.js +0 -1
  22. package/plugins/datasets/src/index.test.js +2 -8
  23. package/plugins/datasets/src/registry/dataset.test.js +9 -0
  24. package/plugins/draw/dist/esm/im-draw-ml-adapter.js +1 -1
  25. package/plugins/draw/dist/esm/im-draw-ol-adapter.js +1 -1
  26. package/plugins/draw/dist/esm/im-draw-plugin.js +1 -1
  27. package/plugins/draw/dist/esm/index.js +1 -1
  28. package/plugins/draw/dist/esm/liveDrawChecks.js +1 -1
  29. package/plugins/draw/dist/umd/im-draw-ml-adapter.js +1 -1
  30. package/plugins/draw/dist/umd/im-draw-ol-adapter.js +1 -1
  31. package/plugins/draw/dist/umd/im-draw-plugin.js +1 -1
  32. package/plugins/draw/dist/umd/index.js +1 -1
  33. package/plugins/draw/src/adapterEvents.js +2 -0
  34. package/plugins/draw/src/adapters/adapterContract.test.js +1 -0
  35. package/plugins/draw/src/adapters/maplibre/MaplibreDrawAdapter.js +15 -3
  36. package/plugins/draw/src/adapters/maplibre/MaplibreDrawAdapter.test.js +27 -0
  37. package/plugins/draw/src/adapters/maplibre/modes/editPointMode/pointerHandlers.js +8 -19
  38. package/plugins/draw/src/adapters/maplibre/modes/editPointMode/pointerHandlers.test.js +20 -0
  39. package/plugins/draw/src/adapters/maplibre/modes/editVertexMode/pointerHandlers.js +6 -17
  40. package/plugins/draw/src/adapters/maplibre/modes/editVertexMode/pointerHandlers.test.js +19 -0
  41. package/plugins/draw/src/adapters/maplibre/utils/editModeEvents.js +29 -1
  42. package/plugins/draw/src/adapters/openlayers/OLDrawAdapter.js +13 -3
  43. package/plugins/draw/src/adapters/openlayers/OLDrawAdapter.test.js +28 -0
  44. package/plugins/draw/src/api/addFeature.js +9 -6
  45. package/plugins/draw/src/api/addFeature.test.js +37 -10
  46. package/plugins/draw/src/api/editFeature.js +1 -1
  47. package/plugins/draw/src/api/recheckGeometry.js +1 -1
  48. package/plugins/draw/src/api/recheckGeometry.test.js +2 -2
  49. package/plugins/draw/src/api/setProperties.js +39 -0
  50. package/plugins/draw/src/api/setProperties.test.js +74 -0
  51. package/plugins/draw/src/api/setStyle.js +16 -19
  52. package/plugins/draw/src/api/setStyle.test.js +17 -2
  53. package/plugins/draw/src/api/split.js +7 -4
  54. package/plugins/draw/src/api/split.test.js +25 -2
  55. package/plugins/draw/src/events.js +95 -53
  56. package/plugins/draw/src/events.test.js +425 -6
  57. package/plugins/draw/src/manifest.js +6 -4
  58. package/plugins/draw/src/manifest.test.js +20 -26
  59. package/plugins/draw/src/reducer.js +7 -0
  60. package/plugins/draw/src/reducer.test.js +11 -0
  61. package/plugins/draw/src/utils/flattenStyleProperties.js +54 -2
  62. package/plugins/draw/src/utils/flattenStyleProperties.test.js +74 -1
  63. package/plugins/draw/src/utils/isPlainObject.js +14 -0
  64. package/plugins/draw/src/utils/isPlainObject.test.js +24 -0
  65. package/plugins/draw/src/utils/mergeFeatureProperties.js +45 -0
  66. package/plugins/draw/src/utils/mergeFeatureProperties.test.js +48 -0
  67. package/plugins/draw/src/utils/stripInternalProperties.js +3 -2
  68. package/plugins/draw/src/validation/commitChecks.js +29 -11
  69. package/plugins/draw/src/validation/commitChecks.test.js +67 -11
  70. package/plugins/draw/src/validation/validateGeometry.js +12 -5
  71. package/plugins/draw/src/validation/validateGeometry.test.js +18 -0
  72. package/plugins/interact/dist/esm/im-interact-plugin.js +1 -1
  73. package/plugins/interact/dist/umd/im-interact-plugin.js +1 -1
  74. package/plugins/map-key/dist/esm/im-map-key-plugin.js +1 -1
  75. package/plugins/map-key/dist/umd/im-map-key-plugin.js +1 -1
  76. package/plugins/search/dist/css/index.css +1 -1
  77. package/plugins/search/src/components/Form/Form.module.scss +1 -0
  78. package/providers/esri/dist/esm/im-esri-provider.js +1 -1
  79. package/providers/esri/src/esriProvider.js +10 -21
  80. package/providers/esri/src/utils/coords.js +37 -7
  81. package/providers/esri/src/utils/coords.test.js +22 -1
  82. package/providers/esri/src/utils/spatial.js +1 -47
  83. package/providers/maplibre/dist/esm/im-maplibre-provider.js +1 -1
  84. package/providers/maplibre/dist/umd/im-maplibre-provider.js +1 -1
  85. package/providers/maplibre/src/maplibreProvider.js +5 -14
  86. package/providers/maplibre/src/maplibreProvider.test.js +143 -146
  87. package/providers/maplibre/src/utils/spatial.js +0 -40
  88. package/providers/maplibre/src/utils/spatial.test.js +0 -35
  89. package/providers/openlayers/dist/esm/im-openlayers-provider.js +1 -1
  90. package/providers/openlayers/dist/umd/im-openlayers-provider.js +1 -1
  91. package/providers/openlayers/src/openlayersProvider.js +5 -14
  92. package/providers/openlayers/src/openlayersProvider.test.js +12 -10
  93. package/providers/openlayers/src/utils/spatial.js +0 -39
  94. package/providers/openlayers/src/utils/spatial.test.js +1 -26
  95. package/src/App/components/Hints/Hints.jsx +9 -21
  96. package/src/App/components/Hints/Hints.test.jsx +20 -33
  97. package/src/App/components/Panel/Panel.jsx +16 -1
  98. package/src/App/components/Panel/Panel.test.jsx +44 -0
  99. package/src/App/hooks/useCrossHairAPI.js +27 -28
  100. package/src/App/hooks/useCrossHairAPI.test.js +64 -7
  101. package/src/App/hooks/useLayoutMeasurements.js +175 -131
  102. package/src/App/hooks/useLayoutMeasurements.test.js +227 -122
  103. package/src/App/hooks/useMapProviderOverrides.js +114 -35
  104. package/src/App/hooks/useMapProviderOverrides.test.js +188 -5
  105. package/src/App/hooks/useModalPanelBehaviour.js +18 -32
  106. package/src/App/hooks/useModalPanelBehaviour.test.js +45 -33
  107. package/src/App/hooks/useVisibleGeometry.js +35 -45
  108. package/src/App/hooks/useVisibleGeometry.test.js +149 -121
  109. package/src/App/layout/Layout.jsx +4 -4
  110. package/src/App/layout/layout.module.scss +124 -61
  111. package/src/App/store/AppProvider.jsx +3 -0
  112. package/src/App/store/MapProvider.jsx +1 -0
  113. package/src/App/store/MapProvider.test.jsx +25 -0
  114. package/src/App/store/appDispatchMiddleware.js +18 -10
  115. package/src/App/store/appDispatchMiddleware.test.js +42 -4
  116. package/src/App/store/mapActionsMap.js +6 -0
  117. package/src/App/store/mapActionsMap.test.js +7 -0
  118. package/src/App/store/mapReducer.js +2 -0
  119. package/src/App/store/mapReducer.test.js +2 -0
  120. package/src/InteractiveMap/InteractiveMap.js +3 -2
  121. package/src/InteractiveMap/InteractiveMap.test.js +2 -2
  122. package/src/config/events.js +1 -1
  123. package/src/config/mapPanelSlots.js +8 -0
  124. package/src/config/symbolConfig.js +1 -1
  125. package/src/services/symbolRegistry.js +1 -1
  126. package/src/services/symbolRegistry.test.js +19 -0
  127. package/src/types.js +29 -8
  128. package/src/utils/getFitMove.js +73 -0
  129. package/src/utils/getFitMove.test.js +114 -0
  130. package/src/utils/getSafeZoneInset.js +122 -77
  131. package/src/utils/getSafeZoneInset.test.js +241 -54
  132. package/src/utils/getValueForStyle.js +10 -5
  133. package/src/utils/getValueForStyle.test.js +13 -2
  134. package/src/utils/renderedBoxes.js +25 -0
  135. package/src/utils/renderedBoxes.test.js +61 -0
  136. package/src/utils/screenBox.js +70 -0
  137. package/src/utils/screenBox.test.js +69 -0
  138. package/src/utils/symbolUtils.js +9 -9
  139. package/src/utils/symbolUtils.test.js +21 -0
  140. package/providers/esri/src/utils/spatial.test.js +0 -58
@@ -0,0 +1,74 @@
1
+ import { setProperties } from './setProperties.js'
2
+ import { logger } from '../../../../src/services/logger.js'
3
+
4
+ jest.mock('../../../../src/services/logger.js', () => ({
5
+ logger: { warn: jest.fn(), error: jest.fn() }
6
+ }))
7
+
8
+ const setup = ({ draw, mode = 'disabled', feature = null } = {}) => {
9
+ const eventBus = { emit: jest.fn() }
10
+ const dispatch = jest.fn()
11
+ if (draw) { draw.getMode = jest.fn(() => mode) }
12
+ return {
13
+ context: { pluginState: { feature, dispatch }, mapState: { mapStyles: [{ id: 'outdoor' }, { id: 'dark' }] }, mapProvider: { draw }, services: { eventBus } },
14
+ eventBus,
15
+ dispatch
16
+ }
17
+ }
18
+
19
+ const makeDraw = () => ({ get: jest.fn((id) => ({ id, properties: {} })), setProperties: jest.fn() })
20
+
21
+ beforeEach(() => jest.clearAllMocks())
22
+
23
+ describe('setProperties', () => {
24
+ test('does nothing when there is no draw instance', () => {
25
+ const { context, eventBus } = setup()
26
+ expect(setProperties(context, 'a', { name: 'x' })).toBe(false)
27
+ expect(eventBus.emit).not.toHaveBeenCalled()
28
+ })
29
+
30
+ test('warns and does nothing for an id with no existing feature', () => {
31
+ const draw = makeDraw()
32
+ draw.get.mockReturnValue(undefined)
33
+ const { context, eventBus } = setup({ draw })
34
+ expect(setProperties(context, 'missing', { name: 'x' })).toBe(false)
35
+ expect(draw.setProperties).not.toHaveBeenCalled()
36
+ expect(eventBus.emit).not.toHaveBeenCalled()
37
+ expect(logger.warn).toHaveBeenCalledWith('setProperties: no feature found for id', 'missing')
38
+ })
39
+
40
+ test('merges the properties and reports what was applied', () => {
41
+ const draw = makeDraw()
42
+ const { context, eventBus, dispatch } = setup({ draw })
43
+ expect(setProperties(context, 'a', { name: 'North field', fillType: 'arable', stroke: 'red', strokeDark: 'tan' })).toBe(true)
44
+ expect(draw.setProperties).toHaveBeenCalledWith('a', { name: 'North field', fillType: 'arable' })
45
+ expect(eventBus.emit).toHaveBeenCalledWith('draw:propertieschange', { featureId: 'a', properties: { name: 'North field', fillType: 'arable' } })
46
+ expect(dispatch).not.toHaveBeenCalled()
47
+ })
48
+
49
+ test('defaults properties to {}', () => {
50
+ const draw = makeDraw()
51
+ const { context, eventBus } = setup({ draw })
52
+ expect(setProperties(context, 'a')).toBe(true)
53
+ expect(eventBus.emit).toHaveBeenCalledWith('draw:propertieschange', { featureId: 'a', properties: {} })
54
+ })
55
+
56
+ // Otherwise Cancel would put back the edit-start copy without them
57
+ test('also updates the edit-start copy of the feature being edited', () => {
58
+ const draw = makeDraw()
59
+ const editStart = { id: 'a', geometry: {}, properties: { landUse: 'arable', sortKey: 2 } }
60
+ const { context, dispatch } = setup({ draw, mode: 'edit_vertex', feature: editStart })
61
+ setProperties(context, 'a', { name: 'North field' })
62
+ expect(dispatch).toHaveBeenCalledWith({
63
+ type: 'SET_FEATURE',
64
+ payload: { feature: { id: 'a', geometry: {}, properties: { landUse: 'arable', sortKey: 2, name: 'North field' } } }
65
+ })
66
+ })
67
+
68
+ test('leaves the edit-start copy alone when a different feature is changed', () => {
69
+ const draw = makeDraw()
70
+ const { context, dispatch } = setup({ draw, mode: 'edit_vertex', feature: { id: 'b', properties: {} } })
71
+ setProperties(context, 'a', { name: 'x' })
72
+ expect(dispatch).not.toHaveBeenCalled()
73
+ })
74
+ })
@@ -1,23 +1,14 @@
1
- import { flattenStyleProperties } from '../utils/flattenStyleProperties.js'
1
+ import { toStyleChanges, definedOnly, mapStyleIds } from '../utils/flattenStyleProperties.js'
2
2
  import { logger } from '../../../../src/services/logger.js'
3
3
 
4
4
  const STYLE_KEYS = new Set(['stroke', 'fill', 'strokeWidth'])
5
5
 
6
- // A partial patch — unlike addFeature.js building a whole new feature from scratch,
7
- // flattenStyleProperties would otherwise write stroke/fill/strokeWidth as explicit
8
- // `undefined` for any of the three the caller didn't mention, blanking out whatever that
9
- // feature already had.
10
- const definedOnly = (properties) => {
11
- const result = {}
12
- for (const [key, value] of Object.entries(properties)) {
13
- if (value !== undefined) {
14
- result[key] = value
15
- }
16
- }
17
- return result
18
- }
6
+ // A removed key is reported as null, so a page that merges the payload into its saved copy
7
+ // of the feature clears it too
8
+ const toPayload = (changes) =>
9
+ Object.fromEntries(Object.entries(changes).map(([key, value]) => [key, value === undefined ? null : value]))
19
10
 
20
- export const setStyle = ({ mapProvider, services }, featureId, styleChanges = {}) => {
11
+ export const setStyle = ({ mapState, mapProvider, services }, featureId, styleChanges = {}) => {
21
12
  const { draw } = mapProvider
22
13
  const { eventBus } = services
23
14
 
@@ -25,7 +16,8 @@ export const setStyle = ({ mapProvider, services }, featureId, styleChanges = {}
25
16
  return false
26
17
  }
27
18
 
28
- if (!draw.get(featureId)) {
19
+ const feature = draw.get(featureId)
20
+ if (!feature) {
29
21
  logger.warn('setStyle: no feature found for id', featureId)
30
22
  return false
31
23
  }
@@ -35,9 +27,14 @@ export const setStyle = ({ mapProvider, services }, featureId, styleChanges = {}
35
27
  for (const [key, value] of Object.entries(styleChanges)) {
36
28
  (STYLE_KEYS.has(key) ? styleProps : symbolProps)[key] = value
37
29
  }
38
- const properties = definedOnly({ ...flattenStyleProperties(styleProps), ...symbolProps })
30
+ // A partial patch: style keys left out stay as they are. Setting one replaces all of its
31
+ // per-style values, which reach the adapter as undefined, meaning remove.
32
+ const changes = {
33
+ ...toStyleChanges(feature.properties, styleProps, mapStyleIds(mapState)),
34
+ ...definedOnly(symbolProps)
35
+ }
39
36
 
40
- draw.setStyle(featureId, properties)
41
- eventBus.emit('draw:stylechange', { featureId, properties })
37
+ draw.setStyle(featureId, changes)
38
+ eventBus.emit('draw:stylechange', { featureId, properties: toPayload(changes) })
42
39
  return true
43
40
  }
@@ -5,9 +5,9 @@ jest.mock('../../../../src/services/logger.js', () => ({
5
5
  logger: { warn: jest.fn(), error: jest.fn() }
6
6
  }))
7
7
 
8
- const setup = (draw) => {
8
+ const setup = (draw, mapStyles = []) => {
9
9
  const eventBus = { emit: jest.fn() }
10
- return { context: { mapProvider: { draw }, services: { eventBus } }, eventBus }
10
+ return { context: { mapState: { mapStyles }, mapProvider: { draw }, services: { eventBus } }, eventBus }
11
11
  }
12
12
 
13
13
  beforeEach(() => jest.clearAllMocks())
@@ -66,6 +66,21 @@ describe('setStyle', () => {
66
66
  expect(draw.setStyle).toHaveBeenCalledWith('a', { stroke: 'red', symbol: 'pin' })
67
67
  })
68
68
 
69
+ // Otherwise the renderers would keep reading the old strokeOutdoor over the new stroke
70
+ test('setting a style prop removes its values for each map style, and the event reports them as null', () => {
71
+ const draw = {
72
+ get: jest.fn(() => ({ id: 'a', properties: { stroke: 'brown', strokeOutdoor: 'brown', strokeDark: 'tan', fillType: 'arable' } })),
73
+ setStyle: jest.fn()
74
+ }
75
+ const { context, eventBus } = setup(draw, [{ id: 'outdoor' }, { id: 'dark' }])
76
+ setStyle(context, 'a', { stroke: 'red' })
77
+ expect(draw.setStyle).toHaveBeenCalledWith('a', { strokeOutdoor: undefined, strokeDark: undefined, stroke: 'red' })
78
+ expect(eventBus.emit).toHaveBeenCalledWith('draw:stylechange', {
79
+ featureId: 'a',
80
+ properties: { strokeOutdoor: null, strokeDark: null, stroke: 'red' }
81
+ })
82
+ })
83
+
69
84
  test('defaults styleChanges to {} and still resolves cleanly', () => {
70
85
  const draw = { get: jest.fn(() => ({ id: 'a' })), setStyle: jest.fn() }
71
86
  const { context } = setup(draw)
@@ -32,10 +32,13 @@ const applySplitCommit = ({ draw, dispatch, polygonFeature, feature }) => {
32
32
 
33
33
  const toResult = (isValid) => (isValid ? { valid: true } : { valid: false, reason: INVALID_REASON })
34
34
 
35
- // Installed as draw._previewValidator. 'place' is a no-op — a split isn't complete
36
- // until its last vertex, so checking it would block placement.
37
- const createSplitPreviewValidator = ({ draw, polygonFeature }) => ({ feature, phase }) =>
38
- phase === 'preview' ? toResult(applySplitPreview({ draw, polygonFeature, feature })) : { valid: true }
35
+ // Installed as draw._previewValidator. 'preview' only colours the line and 'place' is a
36
+ // no-op — a split isn't complete until its last vertex, so an invalid verdict would
37
+ // disable the Add point button and block placement.
38
+ const createSplitPreviewValidator = ({ draw, polygonFeature }) => ({ feature, phase }) => {
39
+ if (phase === 'preview') { applySplitPreview({ draw, polygonFeature, feature }) }
40
+ return { valid: true }
41
+ }
39
42
 
40
43
  // Installed as draw._commitValidator. 'create' is a no-op — checking it would
41
44
  // hijack an early finish into edit mode.
@@ -199,6 +199,18 @@ describe('split', () => {
199
199
  expect(result).toEqual({ valid: false, reason: expect.any(String) })
200
200
  })
201
201
 
202
+ test('a committed vertex that completes the split returns a passing verdict', () => {
203
+ const { context, dispatch, draw } = makeContext()
204
+ splitPolygon.mockReturnValue({ type: 'FeatureCollection' })
205
+ split(context, 'poly')
206
+
207
+ const result = draw._commitValidator({ feature: lineFeature([[0, 0], [1, 1]]), phase: 'change', mode: 'draw_line', vertexIndex: 1 })
208
+
209
+ expect(draw.setDrawingPreviewProperty).toHaveBeenCalledWith('splitter', 'valid')
210
+ expect(dispatch).toHaveBeenCalledWith({ type: 'SET_ACTION', payload: { name: 'split', isValid: true } })
211
+ expect(result).toEqual({ valid: true })
212
+ })
213
+
202
214
  test('the commit validator ignores the live preview phase', () => {
203
215
  const { context, draw } = makeContext()
204
216
  split(context, 'poly')
@@ -209,7 +221,7 @@ describe('split', () => {
209
221
  expect(splitPolygon).not.toHaveBeenCalled()
210
222
  })
211
223
 
212
- test('a line with fewer than two coordinates is treated as invalid without calling splitPolygon', () => {
224
+ test('a line with fewer than two coordinates is coloured invalid without calling splitPolygon', () => {
213
225
  const { context, draw } = makeContext()
214
226
  split(context, 'poly')
215
227
 
@@ -218,7 +230,18 @@ describe('split', () => {
218
230
 
219
231
  expect(splitPolygon).not.toHaveBeenCalled()
220
232
  expect(draw.setDrawingPreviewProperty).toHaveBeenCalledWith('splitter', 'invalid')
221
- expect(result).toEqual({ valid: false, reason: expect.any(String) })
233
+ expect(result).toEqual({ valid: true })
234
+ })
235
+
236
+ test('the live preview of an incomplete split colours it invalid but never vetoes, so Add point stays enabled', () => {
237
+ const { context, draw } = makeContext()
238
+ splitPolygon.mockReturnValue(null)
239
+ split(context, 'poly')
240
+
241
+ const result = draw._previewValidator({ feature: lineFeature([[0, 0], [1, 1]]), phase: 'preview', mode: 'draw_line', numVertices: 1 })
242
+
243
+ expect(draw.setDrawingPreviewProperty).toHaveBeenCalledWith('splitter', 'invalid')
244
+ expect(result).toEqual({ valid: true })
222
245
  })
223
246
  })
224
247
  })
@@ -6,9 +6,11 @@
6
6
  * All MapLibre / OL specifics live in the adapter.
7
7
  */
8
8
  import { ADAPTER_EVENTS } from './adapterEvents.js'
9
- import { getCommitChecks, isSameGeometry } from './validation/commitChecks.js'
9
+ import { getCommitChecks, isSameGeometry, canFinishCheck } from './validation/commitChecks.js'
10
10
  import { MIN_VERTICES_REASONS } from './validation/rules.js'
11
11
  import { stripInternalProperties } from './utils/stripInternalProperties.js'
12
+ import { mergeFeatureProperties } from './utils/mergeFeatureProperties.js'
13
+ import { mapStyleIds } from './utils/flattenStyleProperties.js'
12
14
  import { isEditMode, isCheckedMode } from './utils/drawModes.js'
13
15
 
14
16
  const EDIT_VERTEX_MODE = 'edit_vertex'
@@ -33,6 +35,32 @@ const needsFinishCheck = (draw, { result, context }) =>
33
35
  !FINISH_PHASES.has(context?.phase) && (typeof draw._finishValidator === 'function' || !!result?.recheckOnFinish)
34
36
  const finishValidatorFor = (draw) => draw._finishValidator ?? draw._commitValidator
35
37
 
38
+ // The latest onGeometryFinish result, if it passed for this shape; otherwise null.
39
+ // onGeometryChange standing in for it, or a result for different geometry, doesn't count.
40
+ const getPassingFinishResult = (draw, { status, result, context, feature: checkedFeature }, feature) => {
41
+ const isPassingFinishResult = typeof draw._finishValidator === 'function' && FINISH_PHASES.has(context?.phase) &&
42
+ status === 'valid' && isSameGeometry(checkedFeature, feature)
43
+ return isPassingFinishResult ? result : null
44
+ }
45
+
46
+ // A passing onGeometryFinish result's properties are merged into the finished shape, in
47
+ // draw's store, before any listener sees it. Called before resetState(), which ends the
48
+ // check session. Returns the shape to emit.
49
+ const applyFinishProperties = (draw, feature, mapState) => {
50
+ const properties = getPassingFinishResult(draw, getCommitChecks(draw).state, feature)?.properties
51
+ if (!properties) { return feature }
52
+ mergeFeatureProperties(draw, feature.id, properties, { caller: 'onGeometryFinish', styleIds: mapStyleIds(mapState) })
53
+ return draw.get(feature.id)
54
+ }
55
+
56
+ // The Done button and the map's finish gestures (double-click, clicking the first vertex)
57
+ // follow the same check state
58
+ const applyCanFinish = (draw, dispatch, state) => {
59
+ const canFinish = canFinishCheck(state)
60
+ draw.setCanFinish?.(canFinish)
61
+ dispatch({ type: 'SET_CAN_FINISH', payload: canFinish })
62
+ }
63
+
36
64
  // A shape that simply hasn't reached its minimum vertex count yet isn't a mistake —
37
65
  // it's the normal, expected state of "still being drawn" — so it's excluded below
38
66
  // even though it's a genuine COMMIT_RULES failure like any other.
@@ -61,13 +89,13 @@ const buildVertexMoveTarget = (draw) => ({
61
89
  // Vertex-selection handlers: sync pluginState.selectedVertexIndex, and claim/release
62
90
  // MapControls' D-pad via buildVertexMoveTarget above.
63
91
  const createVertexSelectionHandlers = ({ draw, pluginState, mapProvider, eventBus }) => ({
64
- onVertexSelection: (e) => {
65
- pluginState.dispatch({ type: 'SET_SELECTED_VERTEX_INDEX', payload: e })
66
- mapProvider.activeMoveTarget = e.index >= 0 ? buildVertexMoveTarget(draw) : null
67
- eventBus.emit('draw:vertexselection', e)
92
+ onVertexSelection: (event) => {
93
+ pluginState.dispatch({ type: 'SET_SELECTED_VERTEX_INDEX', payload: event })
94
+ mapProvider.activeMoveTarget = event.index >= 0 ? buildVertexMoveTarget(draw) : null
95
+ eventBus.emit('draw:vertexselection', event)
68
96
  },
69
- onVertexChange: (e) => {
70
- pluginState.dispatch({ type: 'SET_SELECTED_VERTEX_INDEX', payload: { index: -1, numVertices: e.numVertices } })
97
+ onVertexChange: (event) => {
98
+ pluginState.dispatch({ type: 'SET_SELECTED_VERTEX_INDEX', payload: { index: -1, numVertices: event.numVertices } })
71
99
  mapProvider.activeMoveTarget = null
72
100
  }
73
101
  })
@@ -89,11 +117,12 @@ const enterEditMode = ({ draw, appState, appConfig, mapState, dispatch, eventBus
89
117
  getSnapEnabled: () => draw.isSnapEnabled()
90
118
  })
91
119
  const editing = draw.get(featureId)
92
- const { status, result } = getCommitChecks(draw).state
120
+ const state = getCommitChecks(draw).state
121
+ const { status, result } = state
93
122
  dispatch({ type: 'SET_FEATURE', payload: { feature: editing, tempFeature: editing } })
94
123
  dispatch({ type: 'SET_MODE', payload: mode })
95
124
  dispatch({ type: 'SET_VALIDATION_STATUS', payload: status })
96
- draw.setCanFinish?.(status === 'valid' || status === 'pending')
125
+ applyCanFinish(draw, dispatch, state)
97
126
  draw.setInvalid?.(status === 'invalid')
98
127
  eventBus.emit('draw:reopened', { feature: stripInternalProperties(editing), status, reason: result?.reason ?? null })
99
128
  }
@@ -130,10 +159,9 @@ const createCheckStateHandler = ({ draw, dispatch, emitGeometryInvalid, announce
130
159
  // A slow check is the same pending check, now shown as "Checking…"
131
160
  dispatch(state.slow ? { type: 'SET_CHECK_SLOW' } : { type: 'SET_VALIDATION_STATUS', payload: status })
132
161
  announceCheck(announce, state)
133
- // Also blocks the adapters' finish gestures (double-click, clicking the first vertex)
134
- // while the check is failing. A shape finished while its check is pending is held
135
- // until it settles (see createFinishHandlers).
136
- draw.setCanFinish?.(valid || status === 'pending')
162
+ // A shape finished while its check is pending is held until it settles (see
163
+ // createFinishHandlers).
164
+ applyCanFinish(draw, dispatch, state)
137
165
  if (status === 'pending' || status === 'error') { return }
138
166
  // The invalid stroke is committed-validity-driven in edit mode only; in draw mode the
139
167
  // adapters drive it live from the displayed geometry (placed vertices + cursor).
@@ -147,25 +175,38 @@ const createCheckStateHandler = ({ draw, dispatch, emitGeometryInvalid, announce
147
175
  }
148
176
  }
149
177
 
150
- // Finishing a new shape: created when its check passes, otherwise reopened in edit mode.
151
- // A shape finished while its check is pending is held until it settles, with Done reading
152
- // "Checking…" — for a point, Done shows just for that.
178
+ // Finishing a new shape: created when its check passes, otherwise reopened in edit mode —
179
+ // as is a shape whose passing onGeometryFinish result asks to reopen it. A shape finished
180
+ // while its check is pending is held until it settles, with Done reading "Checking…" — for
181
+ // a point, Done shows just for that.
153
182
  function createFinishHandlers ({ appState, appConfig, mapState, draw, dispatch, eventBus, resetState }) {
154
183
  const checks = getCommitChecks(draw)
155
184
 
156
- const create = (f) => {
185
+ const createFeature = (feature) => {
186
+ const finished = applyFinishProperties(draw, feature, mapState)
157
187
  resetState()
158
188
  setTimeout(() => draw.changeMode('disabled'), 0)
159
- eventBus.emit(CREATED_EVENT, stripInternalProperties(f))
189
+ eventBus.emit(CREATED_EVENT, stripInternalProperties(finished))
160
190
  }
161
191
 
162
- const reopen = (featureId, geometryType) => {
192
+ const reopenFeature = (featureId, geometryType) => {
163
193
  pendingCreateIds.set(draw, featureId)
164
194
  setTimeout(() => enterEditMode({ draw, appState, appConfig, mapState, dispatch, eventBus }, featureId, geometryType), 0)
165
195
  }
166
196
 
197
+ // A passing result creates the shape, unless its onGeometryFinish result asked to reopen it,
198
+ // valid. Finishing it again, unchanged, creates it from that same result.
199
+ const createOrReopenFeature = (feature, state) => {
200
+ const shouldReopen = state.context?.phase === 'create' && !!getPassingFinishResult(draw, state, feature)?.reopen
201
+ if (shouldReopen) {
202
+ reopenFeature(feature.id, feature.geometry?.type)
203
+ } else {
204
+ createFeature(feature)
205
+ }
206
+ }
207
+
167
208
  // The map leaves draw mode; Done stays, disabled and reading "Checking…" once the check is slow
168
- const hold = (featureId) => {
209
+ const holdFeature = (featureId) => {
169
210
  checks.hold(featureId)
170
211
  dispatch({ type: 'SET_HOLDING', payload: true })
171
212
  setTimeout(() => draw.changeMode('disabled'), 0)
@@ -177,28 +218,28 @@ function createFinishHandlers ({ appState, appConfig, mapState, draw, dispatch,
177
218
  // same geometry is reused, so finishing doesn't repeat a server call; otherwise (a
178
219
  // point, which has no commits before it's created, a failed result, or a finishing check
179
220
  // — see needsFinishCheck) the shape is checked now with phase 'create'.
180
- const checkFinished = (f) => {
221
+ const checkFinishedFeature = (feature) => {
181
222
  const { status, feature: checkedFeature } = checks.state
182
- const isReusable = (status === 'valid' || status === 'pending') && isSameGeometry(checkedFeature, f) && !needsFinishCheck(draw, checks.state)
223
+ const isReusable = (status === 'valid' || status === 'pending') && isSameGeometry(checkedFeature, feature) && !needsFinishCheck(draw, checks.state)
183
224
  if (!isReusable) {
184
- checks.run({ feature: f, context: { phase: 'create', mode: draw.getMode() }, onGeometryChange: finishValidatorFor(draw) })
225
+ checks.run({ feature, context: { phase: 'create', mode: draw.getMode() }, consumerCheck: finishValidatorFor(draw) })
185
226
  }
186
227
  return checks.state
187
228
  }
188
229
 
189
- const finish = (f) => {
190
- const state = checkFinished(f)
230
+ const finishFeature = (feature) => {
231
+ const state = checkFinishedFeature(feature)
191
232
  if (state.status === 'valid') {
192
- create(f)
233
+ createOrReopenFeature(feature, state)
193
234
  } else if (state.status === 'pending') {
194
- hold(f.id)
235
+ holdFeature(feature.id)
195
236
  } else {
196
- reopen(f.id, f.geometry?.type)
237
+ reopenFeature(feature.id, feature.geometry?.type)
197
238
  }
198
239
  }
199
240
 
200
241
  return {
201
- onCreate: finish,
242
+ onCreate: finishFeature,
202
243
  // A held shape is created once its check passes, or reopened in edit mode. A result that
203
244
  // needs a finishing check, unknown until the pending check settled, is checked again.
204
245
  onCheckSettled: (state) => {
@@ -207,22 +248,23 @@ function createFinishHandlers ({ appState, appConfig, mapState, draw, dispatch,
207
248
  checks.release()
208
249
  const finished = draw.get(heldId)
209
250
  if (needsFinishCheck(draw, state)) {
210
- finish(finished)
251
+ finishFeature(finished)
211
252
  } else if (state.status === 'valid') {
212
- create(finished)
253
+ createOrReopenFeature(finished, state)
213
254
  } else {
214
- reopen(heldId, finished?.geometry?.type)
255
+ reopenFeature(heldId, finished?.geometry?.type)
215
256
  }
216
257
  },
217
- onEditFinish: (f) => {
258
+ onEditFinish: (feature) => {
259
+ const finished = applyFinishProperties(draw, feature, mapState)
218
260
  resetState()
219
261
  setTimeout(() => draw.changeMode('disabled'), 0)
220
262
  // A shape that was drawn-then-fixed reports as a creation, not an edit.
221
- if (pendingCreateIds.get(draw) === f.id) {
263
+ if (pendingCreateIds.get(draw) === feature.id) {
222
264
  pendingCreateIds.delete(draw)
223
- eventBus.emit(CREATED_EVENT, stripInternalProperties(f))
265
+ eventBus.emit(CREATED_EVENT, stripInternalProperties(finished))
224
266
  } else {
225
- eventBus.emit('draw:edited', stripInternalProperties(f))
267
+ eventBus.emit('draw:edited', stripInternalProperties(finished))
226
268
  }
227
269
  }
228
270
  }
@@ -249,7 +291,7 @@ function createEditFinishHandlers ({ draw, dispatch }) {
249
291
  draw.done()
250
292
  return
251
293
  }
252
- checks.run({ feature: state.feature, context: { phase: 'edit-finish', mode: draw.getMode() }, onGeometryChange: finishValidatorFor(draw) })
294
+ checks.run({ feature: state.feature, context: { phase: 'edit-finish', mode: draw.getMode() }, consumerCheck: finishValidatorFor(draw) })
253
295
  const next = checks.state
254
296
  if (next.status === 'pending') {
255
297
  waitFor(next.revision)
@@ -314,12 +356,12 @@ function createHandlers ({ appState, appConfig, mapState, pluginState, mapProvid
314
356
  onCancel: () => { checks.reset() },
315
357
  ...createFinishHandlers({ appState, appConfig, mapState, draw, dispatch, eventBus, resetState }),
316
358
  ...createVertexSelectionHandlers({ draw, pluginState, mapProvider, eventBus }),
317
- onUndoChange: (l) => { pluginState.dispatch({ type: 'SET_UNDO_STACK_LENGTH', payload: l }) },
318
- onUpdate: (f) => { eventBus.emit('draw:updated', stripInternalProperties(f)) },
319
- onGeometryChange: (e) => {
359
+ onUndoChange: (undoStackLength) => { pluginState.dispatch({ type: 'SET_UNDO_STACK_LENGTH', payload: undoStackLength }) },
360
+ onUpdate: (updatedFeature) => { eventBus.emit('draw:updated', stripInternalProperties(updatedFeature)) },
361
+ onGeometryChange: (event) => {
320
362
  // Only commit-level changes carry a `phase` ('change').
321
363
  // Preview events (e.g. split's live preview) have none and are ignored.
322
- if (!e?.phase) { return }
364
+ if (!event?.phase) { return }
323
365
 
324
366
  // A commit that lands after the session has ended (the adapters defer them a tick)
325
367
  // is dropped rather than calling onGeometryChange.
@@ -330,32 +372,32 @@ function createHandlers ({ appState, appConfig, mapState, pluginState, mapProvid
330
372
  // interim invalid states while being built or reshaped. The result is applied
331
373
  // by the check-state handler once it settles.
332
374
  checks.run({
333
- feature: e.feature,
334
- context: { phase: e.phase, vertexIndex: e.vertexIndex, mode },
335
- onGeometryChange: draw._commitValidator
375
+ feature: event.feature,
376
+ context: { phase: event.phase, vertexIndex: event.vertexIndex, mode },
377
+ consumerCheck: draw._commitValidator
336
378
  })
337
379
  },
338
380
  // A vertex placement was rejected (placement rule or user callback veto). Surface it
339
381
  // on the public bus with phase 'place', and as a hint toast, so the reason is visible.
340
- onPlacementBlocked: (e) => {
341
- if (e.announcement) { announce?.(e.announcement, 'action') }
342
- emitGeometryInvalid({ ...e, feature: stripInternalProperties(e.feature) })
382
+ onPlacementBlocked: (event) => {
383
+ if (event.announcement) { announce?.(event.announcement, 'action') }
384
+ emitGeometryInvalid({ ...event, feature: stripInternalProperties(event.feature) })
343
385
  },
344
386
  // Live (mid-drag) validity flip while editing — the displayed shape is exactly
345
387
  // what Done finishes there, so it gates the Done button in real time alongside
346
388
  // the commit-level check. Emitted by the adapters' edit wiring only, on flips only.
347
- onValidityChange: (e) => {
348
- pluginState.dispatch({ type: 'SET_PREVIEW_VALID', payload: e.valid })
389
+ onValidityChange: (event) => {
390
+ pluginState.dispatch({ type: 'SET_PREVIEW_VALID', payload: event.valid })
349
391
  },
350
392
  // Live placement-veto flip while drawing — gates the Add point button so it
351
393
  // never looks active when a tap would be rejected.
352
- onCanPlaceChange: (e) => {
353
- pluginState.dispatch({ type: 'SET_CAN_ADD_POINT', payload: e.canPlace })
394
+ onCanPlaceChange: (event) => {
395
+ pluginState.dispatch({ type: 'SET_CAN_ADD_POINT', payload: event.canPlace })
354
396
  },
355
397
  // The mode's interface type changed (device switch mid-draw, or the final
356
398
  // value on mode exit) — relay so the app can sync appState.interfaceType.
357
- onInterfaceTypeChange: (e) => {
358
- eventBus.emit('draw:interfacetypechange', { interfaceType: e.interfaceType })
399
+ onInterfaceTypeChange: (event) => {
400
+ eventBus.emit('draw:interfacetypechange', { interfaceType: event.interfaceType })
359
401
  }
360
402
  }
361
403
  }