@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
@@ -66,10 +66,10 @@ test('destroy restores the feature\'s pre-edit style', () => {
66
66
  expect(olFeature.getStyle()).not.toBe(selectedStyleFn)
67
67
  })
68
68
 
69
- test('validity flips (user callback only — a Point has no built-in HARD_RULES violation) gate the Done button', () => {
69
+ test('validity flips (user callback only — a Point has no built-in PLACE_RULES violation) gate the Done button', () => {
70
70
  jest.useFakeTimers()
71
71
  const { manager, olFeature } = setup()
72
- manager._geometryValidator = jest.fn(() => ({ valid: false, reason: 'outside region' }))
72
+ manager._previewValidator = jest.fn(() => ({ valid: false, reason: 'outside region' }))
73
73
  olFeature.getGeometry().setCoordinates([9, 9])
74
74
  jest.runAllTimers()
75
75
  expect(manager.emit).toHaveBeenCalledWith(ADAPTER_EVENTS.VALIDITY_CHANGE,
@@ -219,7 +219,7 @@ test('nudgeSelectedVertex (MapControls D-pad) moves the point and is undoable',
219
219
  mode.undo() // empty stack — no-op
220
220
  })
221
221
 
222
- test('undo re-validates with the commit-move phase', () => {
222
+ test('undo re-validates with the change phase', () => {
223
223
  jest.useFakeTimers()
224
224
  const { manager, mode } = setup()
225
225
  mode.nudgeSelectedVertex(1, 0, true)
@@ -228,7 +228,7 @@ test('undo re-validates with the commit-move phase', () => {
228
228
  mode.undo()
229
229
  jest.runAllTimers()
230
230
  expect(manager.emit).toHaveBeenCalledWith(ADAPTER_EVENTS.GEOMETRY_CHANGE, expect.objectContaining({
231
- phase: 'commit-move',
231
+ phase: 'change',
232
232
  vertexIndex: 0,
233
233
  feature: expect.any(Object)
234
234
  }))
@@ -341,3 +341,28 @@ test('destroy removes the point drag interaction and listeners', () => {
341
341
  expect(map.removeInteraction).toHaveBeenCalled()
342
342
  expect(map.interactions.length).toBeLessThan(interactionsBefore)
343
343
  })
344
+
345
+ test('setGeometry moves the point as one undoable change', () => {
346
+ jest.useFakeTimers()
347
+ const { manager, mode, coord } = setup([5, 5])
348
+ manager.emit.mockClear()
349
+
350
+ mode.setGeometry({ type: 'Point', coordinates: [9, 9] })
351
+ expect(coord()).toEqual([9, 9])
352
+ jest.runAllTimers()
353
+ expect(manager.emit).toHaveBeenCalledWith(ADAPTER_EVENTS.GEOMETRY_CHANGE, expect.objectContaining({ phase: 'change', vertexIndex: 0 }))
354
+
355
+ manager.emit.mockClear()
356
+ mode.undo()
357
+ expect(coord()).toEqual([5, 5])
358
+ jest.runAllTimers()
359
+ expect(manager.emit).toHaveBeenCalledWith(ADAPTER_EVENTS.GEOMETRY_CHANGE, expect.objectContaining({ phase: 'change' }))
360
+ jest.useRealTimers()
361
+ })
362
+
363
+ test('setGeometry repositions the touch target in touch mode', () => {
364
+ const { mode, coord } = setup([5, 5])
365
+ mode.setInterfaceType('touch')
366
+ expect(() => mode.setGeometry({ type: 'Point', coordinates: [9, 9] })).not.toThrow()
367
+ expect(coord()).toEqual([9, 9])
368
+ })
@@ -23,11 +23,12 @@ export const undoMovePoint = (olFeature, op) => {
23
23
 
24
24
  /**
25
25
  * Dispatch the correct undo handler based on operation type — mirrors edit/undoOps.js's
26
- * applyUndo, but with the single op type a Point ever produces.
26
+ * applyUndo, but with the two op types a Point produces (a move, or a setGeometry).
27
27
  * @returns {number} vertex index to re-select after undo (always 0), or -1 for none
28
28
  */
29
29
  export const applyPointUndo = (olFeature, op) => {
30
- if (op.type === 'move_vertex') {
30
+ // A replace_geometry op (setGeometry) stores the previous coordinate the same way
31
+ if (op.type === 'move_vertex' || op.type === 'replace_geometry') {
31
32
  return undoMovePoint(olFeature, op)
32
33
  }
33
34
  return -1
@@ -24,6 +24,12 @@ describe('applyPointUndo', () => {
24
24
  expect(feature.getGeometry().getCoordinates()).toEqual([3, 4])
25
25
  })
26
26
 
27
+ test('dispatches replace_geometry (a setGeometry) the same way', () => {
28
+ const feature = pointFeature([5, 5])
29
+ expect(applyPointUndo(feature, { type: 'replace_geometry', previousCoord: [1, 2] })).toBe(0)
30
+ expect(feature.getGeometry().getCoordinates()).toEqual([1, 2])
31
+ })
32
+
27
33
  test('returns -1 for an unknown operation type', () => {
28
34
  const feature = pointFeature([5, 5])
29
35
  expect(applyPointUndo(feature, { type: 'unknown' })).toBe(-1)
@@ -1,12 +1,11 @@
1
- import { ADAPTER_EVENTS } from '../../../adapterEvents.js'
1
+ import { ADAPTER_EVENTS, CHANGE_PHASE } from '../../../adapterEvents.js'
2
2
 
3
3
  // Deferred commit-level geometrychange emitter (feature + change phase + vertex index)
4
4
  // consumed by the validation layer — copied from edit/selectionState.js's own (identical
5
5
  // shape, no ring dependency to strip).
6
- const createGeometryValidationEmitter = (manager, store, olFeature) => (phase, vertexIndex) => {
7
- if (!phase) { return }
6
+ const createGeometryValidationEmitter = (manager, store, olFeature) => (vertexIndex) => {
8
7
  setTimeout(() => {
9
- manager.emit(ADAPTER_EVENTS.GEOMETRY_CHANGE, { feature: store.toGeoJSON(olFeature), phase, vertexIndex })
8
+ manager.emit(ADAPTER_EVENTS.GEOMETRY_CHANGE, { feature: store.toGeoJSON(olFeature), phase: CHANGE_PHASE, vertexIndex })
10
9
  }, 0)
11
10
  }
12
11
 
@@ -75,28 +75,18 @@ test('emitGeometryValidation emits a deferred commit-level geometrychange with t
75
75
  const { selection, manager, store } = setup()
76
76
  manager.emit.mockClear()
77
77
 
78
- selection.emitGeometryValidation('commit-move', 0)
78
+ selection.emitGeometryValidation(0)
79
79
  expect(manager.emit).not.toHaveBeenCalledWith(ADAPTER_EVENTS.GEOMETRY_CHANGE, expect.anything())
80
80
 
81
81
  jest.runAllTimers()
82
82
  expect(manager.emit).toHaveBeenCalledWith(ADAPTER_EVENTS.GEOMETRY_CHANGE, {
83
83
  feature: store.toGeoJSON(),
84
- phase: 'commit-move',
84
+ phase: 'change',
85
85
  vertexIndex: 0
86
86
  })
87
87
  jest.useRealTimers()
88
88
  })
89
89
 
90
- test('emitGeometryValidation is a no-op without a change phase', () => {
91
- jest.useFakeTimers()
92
- const { selection, manager } = setup()
93
- manager.emit.mockClear()
94
- selection.emitGeometryValidation(undefined, 0)
95
- jest.runAllTimers()
96
- expect(manager.emit).not.toHaveBeenCalledWith(ADAPTER_EVENTS.GEOMETRY_CHANGE, expect.anything())
97
- jest.useRealTimers()
98
- })
99
-
100
90
  test('geometry changes refresh the cached coordinate until destroy unbinds the listener', () => {
101
91
  const { selection, olFeature } = setup()
102
92
  olFeature.getGeometry().setCoordinates([20, 20])
@@ -1,4 +1,5 @@
1
1
  import { flattenStyleProperties } from '../utils/flattenStyleProperties.js'
2
+ import { startCheckSession } from '../validation/commitChecks.js'
2
3
 
3
4
  // Shared body for newLine/newPolygon — the two are identical except which draw_* mode they
4
5
  // enter, so that's the only thing parameterised here. Kept out of newPoint.js: a Point also
@@ -12,15 +13,14 @@ export const createNewShape = (mode) => ({ appState, appConfig, pluginConfig, pl
12
13
  return
13
14
  }
14
15
 
15
- eventBus.emit('draw:started', { mode })
16
+ const { stroke, fill, strokeWidth, properties: customProperties, onGeometryChange, onGeometryPreview, ...modeOptions } = options
17
+ startCheckSession(draw, { onGeometryChange, onGeometryPreview }, pluginConfig, eventBus)
18
+ eventBus.emit('draw:started', { mode, featureId })
16
19
 
17
20
  const snapLayers = options.snapLayers === undefined ? (pluginConfig.snapLayers ?? null) : options.snapLayers
18
21
  draw.setSnapLayers(snapLayers)
19
22
  dispatch({ type: 'SET_HAS_SNAP_LAYERS', payload: snapLayers?.length > 0 })
20
23
 
21
- const { stroke, fill, strokeWidth, properties: customProperties, onGeometryChange, ...modeOptions } = options
22
- // Per-call callback overrides the plugin-level one; events.js reads this on every commit.
23
- draw._geometryValidator = onGeometryChange ?? pluginConfig.onGeometryChange
24
24
  const properties = {
25
25
  ...customProperties,
26
26
  ...flattenStyleProperties({ stroke, fill, strokeWidth })
@@ -37,7 +37,7 @@ describe('createNewShape', () => {
37
37
  const { context, dispatch, eventBus, draw } = makeContext()
38
38
  createNewShape('draw_custom')(context, 'f1')
39
39
 
40
- expect(eventBus.emit).toHaveBeenCalledWith('draw:started', { mode: 'draw_custom' })
40
+ expect(eventBus.emit).toHaveBeenCalledWith('draw:started', { mode: 'draw_custom', featureId: 'f1' })
41
41
  expect(draw.changeMode).toHaveBeenCalledWith('draw_custom', expect.objectContaining({ featureId: 'f1' }))
42
42
  expect(dispatch).toHaveBeenCalledWith({ type: 'SET_MODE', payload: 'draw_custom' })
43
43
  })
@@ -1,5 +1,4 @@
1
- import { MAP_SIZE_SCALES } from '../defaults.js'
2
- import { validateGeometry } from '../validation/validateGeometry.js'
1
+ import { startCheckSession, getCommitChecks } from '../validation/commitChecks.js'
3
2
 
4
3
  export const editFeature = ({ appState, appConfig, mapState, pluginConfig, pluginState, mapProvider, services }, featureId, options = {}) => {
5
4
  const { dispatch } = pluginState
@@ -15,14 +14,13 @@ export const editFeature = ({ appState, appConfig, mapState, pluginConfig, plugi
15
14
  return false
16
15
  }
17
16
 
18
- // Per-call callback overrides the plugin-level one; events.js reads this on every commit.
19
- draw._geometryValidator = options.onGeometryChange ?? pluginConfig.onGeometryChange
17
+ startCheckSession(draw, options, pluginConfig, eventBus)
20
18
 
21
19
  // A Point has no ring of vertices to edit — edit_point relocates the single coordinate
22
20
  // instead of edit_vertex's ring-of-vertices machinery.
23
21
  const editModeMap = { LineString: 'edit_line', Polygon: 'edit_polygon', Point: 'edit_point' }
24
22
  const mode = existingFeature.geometry.type === 'Point' ? 'edit_point' : 'edit_vertex'
25
- eventBus.emit('draw:editstart', { mode: editModeMap[existingFeature.geometry.type] })
23
+ eventBus.emit('draw:editstart', { mode: editModeMap[existingFeature.geometry.type], featureId })
26
24
 
27
25
  const snapLayers = options.snapLayers === undefined ? (pluginConfig.snapLayers ?? null) : options.snapLayers
28
26
  draw.setSnapLayers(snapLayers)
@@ -34,7 +32,7 @@ export const editFeature = ({ appState, appConfig, mapState, pluginConfig, plugi
34
32
  undoButtonId: `${appConfig.id}-draw-undo`,
35
33
  isPanEnabled: appState.interfaceType !== 'keyboard',
36
34
  interfaceType: appState.interfaceType,
37
- scale: MAP_SIZE_SCALES[mapState.mapSize],
35
+ scale: mapState.mapSizeScale,
38
36
  featureId,
39
37
  getSnapEnabled: () => draw.isSnapEnabled()
40
38
  })
@@ -47,11 +45,10 @@ export const editFeature = ({ appState, appConfig, mapState, pluginConfig, plugi
47
45
 
48
46
  dispatch({ type: 'SET_MODE', payload: mode })
49
47
 
50
- // Seed the Done-button gate and the stroke from the feature's starting validity
51
- // so an already invalid feature opens dashed and cannot be "finished" until fixed.
52
- const { valid } = validateGeometry(feature, { phase: 'edit-start', mode }, { onGeometryChange: draw._geometryValidator })
53
- dispatch({ type: 'SET_GEOMETRY_VALID', payload: valid })
54
- draw.setInvalid?.(!valid)
48
+ // Seed the Done-button gate and the stroke from the feature's starting check, so an
49
+ // already invalid feature opens dashed and cannot be "finished" until fixed. events.js
50
+ // applies the result once it settles (straight away unless onGeometryChange is async).
51
+ getCommitChecks(draw).run({ feature, context: { phase: 'edit-start', mode }, onGeometryChange: draw._commitValidator })
55
52
 
56
53
  return true
57
54
  }
@@ -1,6 +1,5 @@
1
1
  import { editFeature } from './editFeature.js'
2
-
3
- jest.mock('../defaults.js', () => ({ MAP_SIZE_SCALES: { medium: 1.5, large: 2 } }))
2
+ import { getCommitChecks } from '../validation/commitChecks.js'
4
3
 
5
4
  const makeContext = (overrides = {}) => {
6
5
  const dispatch = jest.fn()
@@ -14,7 +13,7 @@ const makeContext = (overrides = {}) => {
14
13
  const context = {
15
14
  appState: { layoutRefs: { viewportRef: { current: 'viewport' } }, interfaceType: 'mouse' },
16
15
  appConfig: { id: 'app' },
17
- mapState: { mapSize: 'medium' },
16
+ mapState: { mapSizeScale: 1.5 },
18
17
  pluginConfig: { snapLayers: ['pc-layer'] },
19
18
  pluginState: { dispatch },
20
19
  mapProvider: { draw },
@@ -45,7 +44,7 @@ describe('editFeature', () => {
45
44
  const result = editFeature(context, 'f1')
46
45
 
47
46
  expect(result).toBe(true)
48
- expect(eventBus.emit).toHaveBeenCalledWith('draw:editstart', { mode: 'edit_polygon' })
47
+ expect(eventBus.emit).toHaveBeenCalledWith('draw:editstart', { mode: 'edit_polygon', featureId: 'f1' })
49
48
  expect(draw.setSnapLayers).toHaveBeenCalledWith(['pc-layer'])
50
49
  expect(dispatch).toHaveBeenCalledWith({ type: 'SET_HAS_SNAP_LAYERS', payload: true })
51
50
 
@@ -71,7 +70,7 @@ describe('editFeature', () => {
71
70
  const { context, eventBus, draw } = makeContext()
72
71
  draw.get.mockReturnValue({ id: 'f1', geometry: { type: 'LineString' } })
73
72
  editFeature(context, 'f1')
74
- expect(eventBus.emit).toHaveBeenCalledWith('draw:editstart', { mode: 'edit_line' })
73
+ expect(eventBus.emit).toHaveBeenCalledWith('draw:editstart', { mode: 'edit_line', featureId: 'f1' })
75
74
  })
76
75
 
77
76
  // A Point has no ring of vertices — editing one goes through edit_point (relocate the
@@ -82,12 +81,12 @@ describe('editFeature', () => {
82
81
 
83
82
  editFeature(context, 'f1')
84
83
 
85
- expect(eventBus.emit).toHaveBeenCalledWith('draw:editstart', { mode: 'edit_point' })
84
+ expect(eventBus.emit).toHaveBeenCalledWith('draw:editstart', { mode: 'edit_point', featureId: 'f1' })
86
85
  expect(draw.changeMode).toHaveBeenCalledWith('edit_point', expect.objectContaining({ featureId: 'f1' }))
87
86
  expect(dispatch).toHaveBeenCalledWith({ type: 'SET_MODE', payload: 'edit_point' })
88
87
  // Point geometry is always valid (validation/rules.js has no Point branch) — the gate
89
88
  // opens immediately, unlike a self-intersecting/zero-area polygon.
90
- expect(dispatch).toHaveBeenCalledWith({ type: 'SET_GEOMETRY_VALID', payload: true })
89
+ expect(getCommitChecks(draw).state.status).toBe('valid')
91
90
  })
92
91
 
93
92
  test('stores a per-call onGeometryChange validator, overriding the plugin-level one', () => {
@@ -95,35 +94,47 @@ describe('editFeature', () => {
95
94
  const onGeometryChange = jest.fn()
96
95
  const { context, draw } = makeContext({ pluginConfig: { snapLayers: ['pc-layer'], onGeometryChange: pluginOnGeometryChange } })
97
96
  editFeature(context, 'f1', { onGeometryChange })
98
- expect(draw._geometryValidator).toBe(onGeometryChange)
97
+ expect(draw._commitValidator).toBe(onGeometryChange)
98
+ })
99
+
100
+ test('stores a per-call onGeometryPreview validator, overriding the plugin-level one', () => {
101
+ const onGeometryPreview = jest.fn(() => true)
102
+ const { context, draw } = makeContext({ pluginConfig: { onGeometryPreview: jest.fn() } })
103
+ editFeature(context, 'f1', { onGeometryPreview })
104
+ draw._previewValidator({ phase: 'preview' })
105
+ expect(onGeometryPreview).toHaveBeenCalledWith({ phase: 'preview' })
99
106
  })
100
107
 
101
- test('seeds the geometry-valid gate from the feature starting validity', () => {
102
- const { context, dispatch, draw } = makeContext()
108
+ // The check's result is applied by events.js once it settles — here, its state is
109
+ // what seeds the Done gate and the stroke.
110
+ test('seeds the gate from the feature starting check (phase: edit-start)', () => {
111
+ const { context, draw } = makeContext()
103
112
  // A valid square → gate opens.
104
113
  draw.get.mockReturnValue({ id: 'f1', geometry: { type: 'Polygon', coordinates: [[[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]] } })
105
114
  editFeature(context, 'f1')
106
- expect(dispatch).toHaveBeenCalledWith({ type: 'SET_GEOMETRY_VALID', payload: true })
115
+ expect(getCommitChecks(draw).state).toEqual(expect.objectContaining({
116
+ status: 'valid',
117
+ context: { phase: 'edit-start', mode: 'edit_vertex' }
118
+ }))
107
119
  })
108
120
 
109
121
  test('seeds the gate closed for an already self-intersecting feature', () => {
110
- const { context, dispatch, draw } = makeContext()
122
+ const { context, draw } = makeContext()
111
123
  draw.get.mockReturnValue({ id: 'f1', geometry: { type: 'Polygon', coordinates: [[[0, 0], [1, 1], [1, 0], [0, 1], [0, 0]]] } })
112
124
  editFeature(context, 'f1')
113
- expect(dispatch).toHaveBeenCalledWith({ type: 'SET_GEOMETRY_VALID', payload: false })
125
+ expect(getCommitChecks(draw).state.status).toBe('invalid')
114
126
  })
115
127
 
116
- test('seeds the stroke alongside the gate — an invalid feature opens edit mode dashed', () => {
128
+ test('an async starting check leaves the gate pending until it settles', () => {
129
+ const onGeometryChange = jest.fn(() => new Promise(() => {}))
117
130
  const { context, draw } = makeContext()
118
- draw.setInvalid = jest.fn()
119
- draw.get.mockReturnValue({ id: 'f1', geometry: { type: 'Polygon', coordinates: [[[0, 0], [1, 1], [1, 0], [0, 1], [0, 0]]] } })
120
- editFeature(context, 'f1')
121
- expect(draw.setInvalid).toHaveBeenCalledWith(true)
122
-
123
- draw.setInvalid.mockClear()
124
131
  draw.get.mockReturnValue({ id: 'f1', geometry: { type: 'Polygon', coordinates: [[[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]] } })
125
- editFeature(context, 'f1')
126
- expect(draw.setInvalid).toHaveBeenCalledWith(false)
132
+ editFeature(context, 'f1', { onGeometryChange })
133
+ expect(onGeometryChange).toHaveBeenCalledWith(
134
+ expect.objectContaining({ phase: 'edit-start', mode: 'edit_vertex' }),
135
+ expect.objectContaining({ signal: expect.any(AbortSignal) })
136
+ )
137
+ expect(getCommitChecks(draw).state.status).toBe('pending')
127
138
  })
128
139
 
129
140
  test('disables panning for the keyboard interface', () => {
@@ -36,7 +36,7 @@ describe('newLine', () => {
36
36
 
37
37
  newLine(context, 'f1', { stroke: 'red', fill: 'blue', strokeWidth: 3, properties: { name: 'x' }, extra: 'opt' })
38
38
 
39
- expect(eventBus.emit).toHaveBeenCalledWith('draw:started', { mode: 'draw_line' })
39
+ expect(eventBus.emit).toHaveBeenCalledWith('draw:started', { mode: 'draw_line', featureId: 'f1' })
40
40
  expect(flattenStyleProperties).toHaveBeenCalledWith({ stroke: 'red', fill: 'blue', strokeWidth: 3 })
41
41
 
42
42
  expect(draw.changeMode).toHaveBeenCalledWith('draw_line', expect.objectContaining({
@@ -80,7 +80,7 @@ describe('newLine', () => {
80
80
  const { context, draw } = makeContext()
81
81
  const onGeometryChange = jest.fn()
82
82
  newLine(context, 'f1', { onGeometryChange })
83
- expect(draw._geometryValidator).toBe(onGeometryChange)
83
+ expect(draw._commitValidator).toBe(onGeometryChange)
84
84
  expect(draw.changeMode.mock.calls[0][1]).not.toHaveProperty('onGeometryChange')
85
85
  })
86
86
 
@@ -88,6 +88,6 @@ describe('newLine', () => {
88
88
  const pluginOnGeometryChange = jest.fn()
89
89
  const { context, draw } = makeContext({ pluginConfig: { onGeometryChange: pluginOnGeometryChange } })
90
90
  newLine(context, 'f1')
91
- expect(draw._geometryValidator).toBe(pluginOnGeometryChange)
91
+ expect(draw._commitValidator).toBe(pluginOnGeometryChange)
92
92
  })
93
93
  })
@@ -1,4 +1,5 @@
1
1
  import { SYMBOL_KEYS } from '../utils/symbolKeys.js'
2
+ import { startCheckSession } from '../validation/commitChecks.js'
2
3
 
3
4
  export const newPoint = ({ appState, appConfig, pluginConfig, pluginState, mapState, mapProvider, services }, featureId, options = {}) => {
4
5
  const { dispatch } = pluginState
@@ -9,16 +10,14 @@ export const newPoint = ({ appState, appConfig, pluginConfig, pluginState, mapSt
9
10
  return
10
11
  }
11
12
 
12
- eventBus.emit('draw:started', { mode: 'draw_point' })
13
+ const { properties: customProperties, onGeometryChange, onGeometryPreview, ...modeOptions } = options
14
+ startCheckSession(draw, { onGeometryChange, onGeometryPreview }, pluginConfig, eventBus)
15
+ eventBus.emit('draw:started', { mode: 'draw_point', featureId })
13
16
 
14
17
  const snapLayers = options.snapLayers === undefined ? (pluginConfig.snapLayers ?? null) : options.snapLayers
15
18
  draw.setSnapLayers(snapLayers)
16
19
  dispatch({ type: 'SET_HAS_SNAP_LAYERS', payload: snapLayers?.length > 0 })
17
20
 
18
- const { properties: customProperties, onGeometryChange, ...modeOptions } = options
19
- // Per-call callback overrides the plugin-level one; events.js reads this on every commit.
20
- draw._geometryValidator = onGeometryChange ?? pluginConfig.onGeometryChange
21
-
22
21
  const symbolStyle = {}
23
22
  SYMBOL_KEYS.forEach((key) => {
24
23
  if (modeOptions[key] !== undefined) {
@@ -36,7 +36,7 @@ describe('newPoint', () => {
36
36
  extra: 'opt'
37
37
  })
38
38
 
39
- expect(eventBus.emit).toHaveBeenCalledWith('draw:started', { mode: 'draw_point' })
39
+ expect(eventBus.emit).toHaveBeenCalledWith('draw:started', { mode: 'draw_point', featureId: 'f1' })
40
40
 
41
41
  expect(draw.changeMode).toHaveBeenCalledWith('draw_point', expect.objectContaining({
42
42
  container: 'viewport',
@@ -104,7 +104,7 @@ describe('newPoint', () => {
104
104
  const { context, draw } = makeContext()
105
105
  const onGeometryChange = jest.fn()
106
106
  newPoint(context, 'f1', { onGeometryChange })
107
- expect(draw._geometryValidator).toBe(onGeometryChange)
107
+ expect(draw._commitValidator).toBe(onGeometryChange)
108
108
  expect(draw.changeMode.mock.calls[0][1]).not.toHaveProperty('onGeometryChange')
109
109
  })
110
110
 
@@ -112,6 +112,6 @@ describe('newPoint', () => {
112
112
  const pluginOnGeometryChange = jest.fn()
113
113
  const { context, draw } = makeContext({ pluginConfig: { onGeometryChange: pluginOnGeometryChange } })
114
114
  newPoint(context, 'f1')
115
- expect(draw._geometryValidator).toBe(pluginOnGeometryChange)
115
+ expect(draw._commitValidator).toBe(pluginOnGeometryChange)
116
116
  })
117
117
  })
@@ -36,7 +36,7 @@ describe('newPolygon', () => {
36
36
 
37
37
  newPolygon(context, 'f1', { stroke: 'red', fill: 'blue', strokeWidth: 3, properties: { name: 'x' }, extra: 'opt' })
38
38
 
39
- expect(eventBus.emit).toHaveBeenCalledWith('draw:started', { mode: 'draw_polygon' })
39
+ expect(eventBus.emit).toHaveBeenCalledWith('draw:started', { mode: 'draw_polygon', featureId: 'f1' })
40
40
  expect(flattenStyleProperties).toHaveBeenCalledWith({ stroke: 'red', fill: 'blue', strokeWidth: 3 })
41
41
 
42
42
  expect(draw.changeMode).toHaveBeenCalledWith('draw_polygon', expect.objectContaining({
@@ -80,7 +80,7 @@ describe('newPolygon', () => {
80
80
  const { context, draw } = makeContext()
81
81
  const onGeometryChange = jest.fn()
82
82
  newPolygon(context, 'f1', { onGeometryChange })
83
- expect(draw._geometryValidator).toBe(onGeometryChange)
83
+ expect(draw._commitValidator).toBe(onGeometryChange)
84
84
  expect(draw.changeMode.mock.calls[0][1]).not.toHaveProperty('onGeometryChange')
85
85
  })
86
86
 
@@ -88,6 +88,6 @@ describe('newPolygon', () => {
88
88
  const pluginOnGeometryChange = jest.fn()
89
89
  const { context, draw } = makeContext({ pluginConfig: { onGeometryChange: pluginOnGeometryChange } })
90
90
  newPolygon(context, 'f1')
91
- expect(draw._geometryValidator).toBe(pluginOnGeometryChange)
91
+ expect(draw._commitValidator).toBe(pluginOnGeometryChange)
92
92
  })
93
93
  })
@@ -0,0 +1,32 @@
1
+ import { getCommitChecks } from '../validation/commitChecks.js'
2
+ import { isCheckedMode } from '../utils/drawModes.js'
3
+
4
+ /**
5
+ * Check the current shape again, unchanged, by calling `onGeometryChange` with
6
+ * `phase: 'recheck'`. Use it when the answer may have changed without the shape
7
+ * changing (e.g. the user ignored a warning), or to retry a check that failed.
8
+ *
9
+ * Does nothing outside a draw/edit session, or before the shape has been checked.
10
+ *
11
+ * @param {object} context - plugin context
12
+ * @param {*} [detail] - passed to `onGeometryChange` as `event.detail`, e.g. to say why
13
+ * @returns {boolean} whether a check was started
14
+ */
15
+ export const recheckGeometry = ({ mapProvider }, detail) => {
16
+ const { draw } = mapProvider
17
+
18
+ if (!draw) {
19
+ return false
20
+ }
21
+
22
+ const mode = draw.getMode()
23
+ const checks = getCommitChecks(draw)
24
+ const { feature } = checks.state
25
+ if (!isCheckedMode(mode) || !feature) {
26
+ return false
27
+ }
28
+
29
+ const context = { phase: 'recheck', mode, ...(detail !== undefined && { detail }) }
30
+ checks.run({ feature, context, onGeometryChange: draw._commitValidator })
31
+ return true
32
+ }
@@ -0,0 +1,55 @@
1
+ import { recheckGeometry } from './recheckGeometry.js'
2
+ import { getCommitChecks } from '../validation/commitChecks.js'
3
+
4
+ const square = { type: 'Feature', geometry: { type: 'Polygon', coordinates: [[[0, 0], [0, 1], [1, 1], [1, 0], [0, 0]]] } }
5
+
6
+ const makeDraw = (mode = 'edit_vertex') => ({ getMode: jest.fn(() => mode) })
7
+
8
+ describe('recheckGeometry', () => {
9
+ test('checks the last checked shape again with phase recheck', () => {
10
+ const draw = makeDraw()
11
+ draw._commitValidator = jest.fn()
12
+ .mockReturnValueOnce({ valid: false, reason: 'near a parcel' })
13
+ .mockReturnValueOnce(true)
14
+ const checks = getCommitChecks(draw)
15
+ checks.run({ feature: square, context: { phase: 'change', mode: 'edit_vertex' }, onGeometryChange: draw._commitValidator })
16
+ expect(checks.state.status).toBe('invalid')
17
+
18
+ expect(recheckGeometry({ mapProvider: { draw } })).toBe(true)
19
+ expect(draw._commitValidator).toHaveBeenLastCalledWith(
20
+ { feature: square, phase: 'recheck', mode: 'edit_vertex' },
21
+ expect.objectContaining({ signal: expect.any(AbortSignal) })
22
+ )
23
+ expect(checks.state.status).toBe('valid')
24
+ })
25
+
26
+ test('passes a detail through to onGeometryChange', () => {
27
+ const draw = makeDraw()
28
+ draw._commitValidator = jest.fn(() => true)
29
+ getCommitChecks(draw).run({ feature: square, context: { phase: 'change', mode: 'edit_vertex' }, onGeometryChange: draw._commitValidator })
30
+ recheckGeometry({ mapProvider: { draw } }, { refresh: false })
31
+ expect(draw._commitValidator).toHaveBeenLastCalledWith(
32
+ { feature: square, phase: 'recheck', mode: 'edit_vertex', detail: { refresh: false } },
33
+ expect.anything()
34
+ )
35
+ })
36
+
37
+ test('does nothing before the shape has been checked', () => {
38
+ const draw = makeDraw()
39
+ draw._commitValidator = jest.fn()
40
+ expect(recheckGeometry({ mapProvider: { draw } })).toBe(false)
41
+ expect(draw._commitValidator).not.toHaveBeenCalled()
42
+ })
43
+
44
+ test('does nothing outside a draw/edit session', () => {
45
+ const draw = makeDraw('disabled')
46
+ draw._commitValidator = jest.fn(() => true)
47
+ getCommitChecks(draw).run({ feature: square, context: { phase: 'change' } })
48
+ expect(recheckGeometry({ mapProvider: { draw } })).toBe(false)
49
+ expect(draw._commitValidator).not.toHaveBeenCalled()
50
+ })
51
+
52
+ test('does nothing without a draw instance', () => {
53
+ expect(recheckGeometry({ mapProvider: { draw: null } })).toBe(false)
54
+ })
55
+ })
@@ -0,0 +1,33 @@
1
+ import { isEditMode } from '../utils/drawModes.js'
2
+
3
+ // Not structuredClone: it's missing from older browsers we still support, and coordinates
4
+ // are plain arrays of numbers, which JSON copies exactly
5
+ const copyCoordinates = (coordinates) => JSON.parse(JSON.stringify(coordinates)) // NOSONAR
6
+
7
+ /**
8
+ * Replace the shape being edited, e.g. with a corrected boundary. The change can be
9
+ * undone and is checked like any other, with `phase: 'change'`.
10
+ *
11
+ * Does nothing outside an edit session, or when the geometry type differs from the
12
+ * shape being edited.
13
+ *
14
+ * @param {object} context - plugin context
15
+ * @param {object} geometry - GeoJSON geometry of the same type as the edited shape
16
+ * @returns {boolean} whether the shape was replaced
17
+ */
18
+ export const setGeometry = ({ mapProvider, pluginState }, geometry) => {
19
+ const { draw } = mapProvider
20
+
21
+ if (!draw || !isEditMode(draw.getMode())) {
22
+ return false
23
+ }
24
+
25
+ const current = draw.get(pluginState.feature?.id)
26
+ if (!current || current.geometry?.type !== geometry?.type) {
27
+ return false
28
+ }
29
+
30
+ // Copied so later changes to the caller's object can't reach the edited shape
31
+ draw.setGeometry({ type: geometry.type, coordinates: copyCoordinates(geometry.coordinates) })
32
+ return true
33
+ }
@@ -0,0 +1,48 @@
1
+ import { setGeometry } from './setGeometry.js'
2
+
3
+ const polygon = { type: 'Polygon', coordinates: [[[0, 0], [0, 1], [1, 1], [0, 0]]] }
4
+
5
+ const makeContext = ({ mode = 'edit_vertex', current = { id: 'f1', geometry: { type: 'Polygon' } } } = {}) => {
6
+ const draw = {
7
+ getMode: jest.fn(() => mode),
8
+ get: jest.fn(() => current),
9
+ setGeometry: jest.fn()
10
+ }
11
+ return { context: { mapProvider: { draw }, pluginState: { feature: { id: 'f1' } } }, draw }
12
+ }
13
+
14
+ describe('setGeometry', () => {
15
+ test('replaces the edited shape with a copy of the geometry', () => {
16
+ const { context, draw } = makeContext()
17
+ expect(setGeometry(context, polygon)).toBe(true)
18
+ expect(draw.get).toHaveBeenCalledWith('f1')
19
+ const [passed] = draw.setGeometry.mock.calls[0]
20
+ expect(passed).toEqual(polygon)
21
+ expect(passed.coordinates).not.toBe(polygon.coordinates)
22
+ })
23
+
24
+ test('works in a point edit too', () => {
25
+ const { context, draw } = makeContext({ mode: 'edit_point', current: { id: 'f1', geometry: { type: 'Point' } } })
26
+ expect(setGeometry(context, { type: 'Point', coordinates: [1, 2] })).toBe(true)
27
+ expect(draw.setGeometry).toHaveBeenCalledWith({ type: 'Point', coordinates: [1, 2] })
28
+ })
29
+
30
+ test('does nothing outside an edit session', () => {
31
+ const { context, draw } = makeContext({ mode: 'draw_polygon' })
32
+ expect(setGeometry(context, polygon)).toBe(false)
33
+ expect(draw.setGeometry).not.toHaveBeenCalled()
34
+ })
35
+
36
+ test('does nothing when the geometry type differs', () => {
37
+ const { context, draw } = makeContext()
38
+ expect(setGeometry(context, { type: 'LineString', coordinates: [[0, 0], [1, 1]] })).toBe(false)
39
+ expect(draw.setGeometry).not.toHaveBeenCalled()
40
+ })
41
+
42
+ test('does nothing without a draw instance or an edited feature', () => {
43
+ expect(setGeometry({ mapProvider: { draw: null }, pluginState: {} }, polygon)).toBe(false)
44
+ const { context, draw } = makeContext({ current: null })
45
+ expect(setGeometry(context, polygon)).toBe(false)
46
+ expect(draw.setGeometry).not.toHaveBeenCalled()
47
+ })
48
+ })