@defra/interactive-map 0.0.57-alpha → 0.0.59-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 (63) hide show
  1. package/dist/esm/im-core.js +1 -1
  2. package/dist/umd/im-core.js +1 -1
  3. package/docs/assets/css/docusaurus.scss +7 -1
  4. package/docs/plugins/draw.md +317 -186
  5. package/package.json +1 -1
  6. package/plugins/draw/dist/esm/im-draw-ml-adapter.js +1 -1
  7. package/plugins/draw/dist/esm/im-draw-ol-adapter.js +1 -1
  8. package/plugins/draw/dist/esm/im-draw-plugin.js +1 -1
  9. package/plugins/draw/dist/esm/index.js +1 -1
  10. package/plugins/draw/dist/esm/liveDrawChecks.js +1 -1
  11. package/plugins/draw/dist/umd/im-draw-ml-adapter.js +1 -1
  12. package/plugins/draw/dist/umd/im-draw-ol-adapter.js +1 -1
  13. package/plugins/draw/dist/umd/im-draw-plugin.js +1 -1
  14. package/plugins/draw/dist/umd/index.js +1 -1
  15. package/plugins/draw/src/adapterEvents.js +15 -9
  16. package/plugins/draw/src/adapters/adapterContract.test.js +1 -0
  17. package/plugins/draw/src/adapters/maplibre/MaplibreDrawAdapter.js +15 -3
  18. package/plugins/draw/src/adapters/maplibre/MaplibreDrawAdapter.test.js +27 -0
  19. package/plugins/draw/src/adapters/maplibre/modes/editPointMode/pointerHandlers.js +8 -19
  20. package/plugins/draw/src/adapters/maplibre/modes/editPointMode/pointerHandlers.test.js +20 -0
  21. package/plugins/draw/src/adapters/maplibre/modes/editVertexMode/pointerHandlers.js +6 -17
  22. package/plugins/draw/src/adapters/maplibre/modes/editVertexMode/pointerHandlers.test.js +19 -0
  23. package/plugins/draw/src/adapters/maplibre/utils/editModeEvents.js +29 -1
  24. package/plugins/draw/src/adapters/openlayers/OLDrawAdapter.js +13 -3
  25. package/plugins/draw/src/adapters/openlayers/OLDrawAdapter.test.js +28 -0
  26. package/plugins/draw/src/api/addFeature.js +9 -6
  27. package/plugins/draw/src/api/addFeature.test.js +37 -10
  28. package/plugins/draw/src/api/createNewShape.js +2 -2
  29. package/plugins/draw/src/api/editFeature.js +1 -1
  30. package/plugins/draw/src/api/newPoint.js +2 -2
  31. package/plugins/draw/src/api/newPoint.test.js +8 -0
  32. package/plugins/draw/src/api/newPolygon.test.js +8 -0
  33. package/plugins/draw/src/api/recheckGeometry.js +1 -1
  34. package/plugins/draw/src/api/recheckGeometry.test.js +2 -2
  35. package/plugins/draw/src/api/setProperties.js +39 -0
  36. package/plugins/draw/src/api/setProperties.test.js +74 -0
  37. package/plugins/draw/src/api/setStyle.js +16 -19
  38. package/plugins/draw/src/api/setStyle.test.js +17 -2
  39. package/plugins/draw/src/api/split.js +10 -4
  40. package/plugins/draw/src/api/split.test.js +30 -2
  41. package/plugins/draw/src/events.js +110 -64
  42. package/plugins/draw/src/events.test.js +494 -4
  43. package/plugins/draw/src/manifest.js +6 -4
  44. package/plugins/draw/src/manifest.test.js +20 -26
  45. package/plugins/draw/src/reducer.js +7 -0
  46. package/plugins/draw/src/reducer.test.js +11 -0
  47. package/plugins/draw/src/utils/flattenStyleProperties.js +54 -2
  48. package/plugins/draw/src/utils/flattenStyleProperties.test.js +74 -1
  49. package/plugins/draw/src/utils/isPlainObject.js +14 -0
  50. package/plugins/draw/src/utils/isPlainObject.test.js +24 -0
  51. package/plugins/draw/src/utils/mergeFeatureProperties.js +45 -0
  52. package/plugins/draw/src/utils/mergeFeatureProperties.test.js +48 -0
  53. package/plugins/draw/src/utils/stripInternalProperties.js +3 -2
  54. package/plugins/draw/src/validation/commitChecks.js +32 -12
  55. package/plugins/draw/src/validation/commitChecks.test.js +73 -13
  56. package/plugins/draw/src/validation/validateGeometry.js +12 -5
  57. package/plugins/draw/src/validation/validateGeometry.test.js +18 -0
  58. package/src/App/store/MapProvider.jsx +1 -0
  59. package/src/App/store/MapProvider.test.jsx +25 -0
  60. package/src/App/store/mapActionsMap.js +6 -0
  61. package/src/App/store/mapActionsMap.test.js +7 -0
  62. package/src/App/store/mapReducer.js +2 -0
  63. package/src/App/store/mapReducer.test.js +2 -0
@@ -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'
@@ -25,10 +27,39 @@ const pendingCreateIds = new WeakMap()
25
27
  // apart from a held new shape, which a new session discards — an edited feature must stay.
26
28
  const editFinishRevisions = new WeakMap()
27
29
 
28
- // A result can ask for the shape to be checked again once it's finished (recheckOnFinish),
29
- // e.g. when the consumer skips its own check while drawing. Ignored on that finishing check.
30
+ // A finished shape is checked again, rather than reusing the latest result, when there's an
31
+ // onGeometryFinish, or the result asked for it (recheckOnFinish, deprecated). Never on that
32
+ // finishing check itself. onGeometryFinish runs the finishing check; without it, onGeometryChange.
30
33
  const FINISH_PHASES = new Set(['create', 'edit-finish'])
31
- const wantsRecheckOnFinish = ({ result, context }) => !!result?.recheckOnFinish && !FINISH_PHASES.has(context?.phase)
34
+ const needsFinishCheck = (draw, { result, context }) =>
35
+ !FINISH_PHASES.has(context?.phase) && (typeof draw._finishValidator === 'function' || !!result?.recheckOnFinish)
36
+ const finishValidatorFor = (draw) => draw._finishValidator ?? draw._commitValidator
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
+ }
32
63
 
33
64
  // A shape that simply hasn't reached its minimum vertex count yet isn't a mistake —
34
65
  // it's the normal, expected state of "still being drawn" — so it's excluded below
@@ -58,13 +89,13 @@ const buildVertexMoveTarget = (draw) => ({
58
89
  // Vertex-selection handlers: sync pluginState.selectedVertexIndex, and claim/release
59
90
  // MapControls' D-pad via buildVertexMoveTarget above.
60
91
  const createVertexSelectionHandlers = ({ draw, pluginState, mapProvider, eventBus }) => ({
61
- onVertexSelection: (e) => {
62
- pluginState.dispatch({ type: 'SET_SELECTED_VERTEX_INDEX', payload: e })
63
- mapProvider.activeMoveTarget = e.index >= 0 ? buildVertexMoveTarget(draw) : null
64
- 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)
65
96
  },
66
- onVertexChange: (e) => {
67
- 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 } })
68
99
  mapProvider.activeMoveTarget = null
69
100
  }
70
101
  })
@@ -86,11 +117,12 @@ const enterEditMode = ({ draw, appState, appConfig, mapState, dispatch, eventBus
86
117
  getSnapEnabled: () => draw.isSnapEnabled()
87
118
  })
88
119
  const editing = draw.get(featureId)
89
- const { status, result } = getCommitChecks(draw).state
120
+ const state = getCommitChecks(draw).state
121
+ const { status, result } = state
90
122
  dispatch({ type: 'SET_FEATURE', payload: { feature: editing, tempFeature: editing } })
91
123
  dispatch({ type: 'SET_MODE', payload: mode })
92
124
  dispatch({ type: 'SET_VALIDATION_STATUS', payload: status })
93
- draw.setCanFinish?.(status === 'valid' || status === 'pending')
125
+ applyCanFinish(draw, dispatch, state)
94
126
  draw.setInvalid?.(status === 'invalid')
95
127
  eventBus.emit('draw:reopened', { feature: stripInternalProperties(editing), status, reason: result?.reason ?? null })
96
128
  }
@@ -127,10 +159,9 @@ const createCheckStateHandler = ({ draw, dispatch, emitGeometryInvalid, announce
127
159
  // A slow check is the same pending check, now shown as "Checking…"
128
160
  dispatch(state.slow ? { type: 'SET_CHECK_SLOW' } : { type: 'SET_VALIDATION_STATUS', payload: status })
129
161
  announceCheck(announce, state)
130
- // Also blocks the adapters' finish gestures (double-click, clicking the first vertex)
131
- // while the check is failing. A shape finished while its check is pending is held
132
- // until it settles (see createFinishHandlers).
133
- 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)
134
165
  if (status === 'pending' || status === 'error') { return }
135
166
  // The invalid stroke is committed-validity-driven in edit mode only; in draw mode the
136
167
  // adapters drive it live from the displayed geometry (placed vertices + cursor).
@@ -144,25 +175,38 @@ const createCheckStateHandler = ({ draw, dispatch, emitGeometryInvalid, announce
144
175
  }
145
176
  }
146
177
 
147
- // Finishing a new shape: created when its check passes, otherwise reopened in edit mode.
148
- // A shape finished while its check is pending is held until it settles, with Done reading
149
- // "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.
150
182
  function createFinishHandlers ({ appState, appConfig, mapState, draw, dispatch, eventBus, resetState }) {
151
183
  const checks = getCommitChecks(draw)
152
184
 
153
- const create = (f) => {
185
+ const createFeature = (feature) => {
186
+ const finished = applyFinishProperties(draw, feature, mapState)
154
187
  resetState()
155
188
  setTimeout(() => draw.changeMode('disabled'), 0)
156
- eventBus.emit(CREATED_EVENT, stripInternalProperties(f))
189
+ eventBus.emit(CREATED_EVENT, stripInternalProperties(finished))
157
190
  }
158
191
 
159
- const reopen = (featureId, geometryType) => {
192
+ const reopenFeature = (featureId, geometryType) => {
160
193
  pendingCreateIds.set(draw, featureId)
161
194
  setTimeout(() => enterEditMode({ draw, appState, appConfig, mapState, dispatch, eventBus }, featureId, geometryType), 0)
162
195
  }
163
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
+
164
208
  // The map leaves draw mode; Done stays, disabled and reading "Checking…" once the check is slow
165
- const hold = (featureId) => {
209
+ const holdFeature = (featureId) => {
166
210
  checks.hold(featureId)
167
211
  dispatch({ type: 'SET_HOLDING', payload: true })
168
212
  setTimeout(() => draw.changeMode('disabled'), 0)
@@ -172,62 +216,64 @@ function createFinishHandlers ({ appState, appConfig, mapState, draw, dispatch,
172
216
  // the first vertex, Enter), so the latest check is re-applied here — a shape without a
173
217
  // passing check must never be finalised. A passing or still-pending result for this
174
218
  // same geometry is reused, so finishing doesn't repeat a server call; otherwise (a
175
- // point, which has no commits before it's created, a failed result, or a result asking
176
- // for recheckOnFinish) the shape is checked now with phase 'create'.
177
- const checkFinished = (f) => {
219
+ // point, which has no commits before it's created, a failed result, or a finishing check
220
+ // — see needsFinishCheck) the shape is checked now with phase 'create'.
221
+ const checkFinishedFeature = (feature) => {
178
222
  const { status, feature: checkedFeature } = checks.state
179
- const isReusable = (status === 'valid' || status === 'pending') && isSameGeometry(checkedFeature, f) && !wantsRecheckOnFinish(checks.state)
223
+ const isReusable = (status === 'valid' || status === 'pending') && isSameGeometry(checkedFeature, feature) && !needsFinishCheck(draw, checks.state)
180
224
  if (!isReusable) {
181
- checks.run({ feature: f, context: { phase: 'create', mode: draw.getMode() }, onGeometryChange: draw._commitValidator })
225
+ checks.run({ feature, context: { phase: 'create', mode: draw.getMode() }, consumerCheck: finishValidatorFor(draw) })
182
226
  }
183
227
  return checks.state
184
228
  }
185
229
 
186
- const finish = (f) => {
187
- const state = checkFinished(f)
230
+ const finishFeature = (feature) => {
231
+ const state = checkFinishedFeature(feature)
188
232
  if (state.status === 'valid') {
189
- create(f)
233
+ createOrReopenFeature(feature, state)
190
234
  } else if (state.status === 'pending') {
191
- hold(f.id)
235
+ holdFeature(feature.id)
192
236
  } else {
193
- reopen(f.id, f.geometry?.type)
237
+ reopenFeature(feature.id, feature.geometry?.type)
194
238
  }
195
239
  }
196
240
 
197
241
  return {
198
- onCreate: finish,
242
+ onCreate: finishFeature,
199
243
  // A held shape is created once its check passes, or reopened in edit mode. A result that
200
- // asks for recheckOnFinish, unknown until the pending check settled, is checked again.
244
+ // needs a finishing check, unknown until the pending check settled, is checked again.
201
245
  onCheckSettled: (state) => {
202
246
  const heldId = checks.heldId
203
247
  if (heldId === undefined || state.status === 'pending') { return }
204
248
  checks.release()
205
249
  const finished = draw.get(heldId)
206
- if (wantsRecheckOnFinish(state)) {
207
- finish(finished)
250
+ if (needsFinishCheck(draw, state)) {
251
+ finishFeature(finished)
208
252
  } else if (state.status === 'valid') {
209
- create(finished)
253
+ createOrReopenFeature(finished, state)
210
254
  } else {
211
- reopen(heldId, finished?.geometry?.type)
255
+ reopenFeature(heldId, finished?.geometry?.type)
212
256
  }
213
257
  },
214
- onEditFinish: (f) => {
258
+ onEditFinish: (feature) => {
259
+ const finished = applyFinishProperties(draw, feature, mapState)
215
260
  resetState()
216
261
  setTimeout(() => draw.changeMode('disabled'), 0)
217
262
  // A shape that was drawn-then-fixed reports as a creation, not an edit.
218
- if (pendingCreateIds.get(draw) === f.id) {
263
+ if (pendingCreateIds.get(draw) === feature.id) {
219
264
  pendingCreateIds.delete(draw)
220
- eventBus.emit(CREATED_EVENT, stripInternalProperties(f))
265
+ eventBus.emit(CREATED_EVENT, stripInternalProperties(finished))
221
266
  } else {
222
- eventBus.emit('draw:edited', stripInternalProperties(f))
267
+ eventBus.emit('draw:edited', stripInternalProperties(finished))
223
268
  }
224
269
  }
225
270
  }
226
271
  }
227
272
 
228
273
  // Done, and finishing an edit: pressed while editing with a check pending, Done waits for
229
- // that check (see editFinishRevisions above) instead of finishing straight away. A result
230
- // that asks for recheckOnFinish is checked again first, with phase 'edit-finish'.
274
+ // that check (see editFinishRevisions above) instead of finishing straight away. With an
275
+ // onGeometryFinish, or a result asking for recheckOnFinish, the shape is checked again first,
276
+ // with phase 'edit-finish' — straight away for onGeometryFinish, which decides on its own.
231
277
  function createEditFinishHandlers ({ draw, dispatch }) {
232
278
  const checks = getCommitChecks(draw)
233
279
 
@@ -237,15 +283,15 @@ function createEditFinishHandlers ({ draw, dispatch }) {
237
283
  }
238
284
 
239
285
  const finishEdit = (state) => {
240
- if (state.status === 'pending') {
286
+ if (state.status === 'pending' && !draw._finishValidator) {
241
287
  waitFor(state.revision)
242
288
  return
243
289
  }
244
- if (!wantsRecheckOnFinish(state)) {
290
+ if (!needsFinishCheck(draw, state)) {
245
291
  draw.done()
246
292
  return
247
293
  }
248
- checks.run({ feature: state.feature, context: { phase: 'edit-finish', mode: draw.getMode() }, onGeometryChange: draw._commitValidator })
294
+ checks.run({ feature: state.feature, context: { phase: 'edit-finish', mode: draw.getMode() }, consumerCheck: finishValidatorFor(draw) })
249
295
  const next = checks.state
250
296
  if (next.status === 'pending') {
251
297
  waitFor(next.revision)
@@ -310,12 +356,12 @@ function createHandlers ({ appState, appConfig, mapState, pluginState, mapProvid
310
356
  onCancel: () => { checks.reset() },
311
357
  ...createFinishHandlers({ appState, appConfig, mapState, draw, dispatch, eventBus, resetState }),
312
358
  ...createVertexSelectionHandlers({ draw, pluginState, mapProvider, eventBus }),
313
- onUndoChange: (l) => { pluginState.dispatch({ type: 'SET_UNDO_STACK_LENGTH', payload: l }) },
314
- onUpdate: (f) => { eventBus.emit('draw:updated', stripInternalProperties(f)) },
315
- 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) => {
316
362
  // Only commit-level changes carry a `phase` ('change').
317
363
  // Preview events (e.g. split's live preview) have none and are ignored.
318
- if (!e?.phase) { return }
364
+ if (!event?.phase) { return }
319
365
 
320
366
  // A commit that lands after the session has ended (the adapters defer them a tick)
321
367
  // is dropped rather than calling onGeometryChange.
@@ -326,32 +372,32 @@ function createHandlers ({ appState, appConfig, mapState, pluginState, mapProvid
326
372
  // interim invalid states while being built or reshaped. The result is applied
327
373
  // by the check-state handler once it settles.
328
374
  checks.run({
329
- feature: e.feature,
330
- context: { phase: e.phase, vertexIndex: e.vertexIndex, mode },
331
- onGeometryChange: draw._commitValidator
375
+ feature: event.feature,
376
+ context: { phase: event.phase, vertexIndex: event.vertexIndex, mode },
377
+ consumerCheck: draw._commitValidator
332
378
  })
333
379
  },
334
380
  // A vertex placement was rejected (placement rule or user callback veto). Surface it
335
381
  // on the public bus with phase 'place', and as a hint toast, so the reason is visible.
336
- onPlacementBlocked: (e) => {
337
- if (e.announcement) { announce?.(e.announcement, 'action') }
338
- emitGeometryInvalid({ ...e, feature: stripInternalProperties(e.feature) })
382
+ onPlacementBlocked: (event) => {
383
+ if (event.announcement) { announce?.(event.announcement, 'action') }
384
+ emitGeometryInvalid({ ...event, feature: stripInternalProperties(event.feature) })
339
385
  },
340
386
  // Live (mid-drag) validity flip while editing — the displayed shape is exactly
341
387
  // what Done finishes there, so it gates the Done button in real time alongside
342
388
  // the commit-level check. Emitted by the adapters' edit wiring only, on flips only.
343
- onValidityChange: (e) => {
344
- pluginState.dispatch({ type: 'SET_PREVIEW_VALID', payload: e.valid })
389
+ onValidityChange: (event) => {
390
+ pluginState.dispatch({ type: 'SET_PREVIEW_VALID', payload: event.valid })
345
391
  },
346
392
  // Live placement-veto flip while drawing — gates the Add point button so it
347
393
  // never looks active when a tap would be rejected.
348
- onCanPlaceChange: (e) => {
349
- pluginState.dispatch({ type: 'SET_CAN_ADD_POINT', payload: e.canPlace })
394
+ onCanPlaceChange: (event) => {
395
+ pluginState.dispatch({ type: 'SET_CAN_ADD_POINT', payload: event.canPlace })
350
396
  },
351
397
  // The mode's interface type changed (device switch mid-draw, or the final
352
398
  // value on mode exit) — relay so the app can sync appState.interfaceType.
353
- onInterfaceTypeChange: (e) => {
354
- eventBus.emit('draw:interfacetypechange', { interfaceType: e.interfaceType })
399
+ onInterfaceTypeChange: (event) => {
400
+ eventBus.emit('draw:interfacetypechange', { interfaceType: event.interfaceType })
355
401
  }
356
402
  }
357
403
  }