@defra/interactive-map 0.0.56-alpha → 0.0.58-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 (138) 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/assets/css/docusaurus.scss +7 -1
  8. package/docs/plugins/draw.md +326 -121
  9. package/package.json +1 -1
  10. package/plugins/datasets/dist/esm/im-datasets-plugin.js +1 -1
  11. package/plugins/datasets/dist/umd/im-datasets-plugin.js +1 -1
  12. package/plugins/datasets/dist/umd/index.js +1 -1
  13. package/plugins/datasets/src/initialise/DatasetsInit.jsx +6 -17
  14. package/plugins/datasets/src/initialise/DatasetsInit.test.jsx +3 -43
  15. package/plugins/draw/dist/esm/im-draw-ml-adapter.js +1 -1
  16. package/plugins/draw/dist/esm/im-draw-ol-adapter.js +1 -1
  17. package/plugins/draw/dist/esm/im-draw-plugin.js +1 -1
  18. package/plugins/draw/dist/esm/index.js +1 -1
  19. package/plugins/draw/dist/esm/liveDrawChecks.js +1 -1
  20. package/plugins/draw/dist/umd/im-draw-ml-adapter.js +1 -1
  21. package/plugins/draw/dist/umd/im-draw-ol-adapter.js +1 -1
  22. package/plugins/draw/dist/umd/im-draw-plugin.js +1 -1
  23. package/plugins/draw/dist/umd/index.js +1 -1
  24. package/plugins/draw/src/DrawInit.jsx +9 -6
  25. package/plugins/draw/src/DrawInit.test.jsx +15 -0
  26. package/plugins/draw/src/adapterEvents.js +82 -44
  27. package/plugins/draw/src/adapters/adapterContract.test.js +3 -3
  28. package/plugins/draw/src/adapters/maplibre/MaplibreDrawAdapter.js +18 -10
  29. package/plugins/draw/src/adapters/maplibre/MaplibreDrawAdapter.test.js +31 -22
  30. package/plugins/draw/src/adapters/maplibre/drawEvents.js +2 -1
  31. package/plugins/draw/src/adapters/maplibre/modes/drawMode/clickHandlers.js +13 -12
  32. package/plugins/draw/src/adapters/maplibre/modes/drawMode/clickHandlers.test.js +5 -5
  33. package/plugins/draw/src/adapters/maplibre/modes/drawMode/undoHandlers.js +1 -1
  34. package/plugins/draw/src/adapters/maplibre/modes/drawMode/undoHandlers.test.js +2 -2
  35. package/plugins/draw/src/adapters/maplibre/modes/drawPointMode.js +6 -9
  36. package/plugins/draw/src/adapters/maplibre/modes/drawPointMode.test.js +4 -4
  37. package/plugins/draw/src/adapters/maplibre/modes/editPointMode/pointerHandlers.js +6 -9
  38. package/plugins/draw/src/adapters/maplibre/modes/editPointMode/pointerHandlers.test.js +1 -1
  39. package/plugins/draw/src/adapters/maplibre/modes/editPointMode/undoHandlers.js +8 -14
  40. package/plugins/draw/src/adapters/maplibre/modes/editPointMode/undoHandlers.test.js +37 -8
  41. package/plugins/draw/src/adapters/maplibre/modes/editPointMode.js +11 -1
  42. package/plugins/draw/src/adapters/maplibre/modes/editVertexMode/undoHandlers.js +14 -19
  43. package/plugins/draw/src/adapters/maplibre/modes/editVertexMode/undoHandlers.test.js +41 -4
  44. package/plugins/draw/src/adapters/maplibre/modes/editVertexMode.js +12 -1
  45. package/plugins/draw/src/adapters/maplibre/utils/editModeEvents.js +3 -1
  46. package/plugins/draw/src/adapters/maplibre/utils/geometryValidation.js +9 -10
  47. package/plugins/draw/src/adapters/maplibre/utils/geometryValidation.test.js +5 -24
  48. package/plugins/draw/src/adapters/openlayers/OLDrawAdapter.js +10 -7
  49. package/plugins/draw/src/adapters/openlayers/OLDrawAdapter.test.js +12 -7
  50. package/plugins/draw/src/adapters/openlayers/core/OLDrawManager.js +5 -0
  51. package/plugins/draw/src/adapters/openlayers/core/OLDrawManager.test.js +7 -3
  52. package/plugins/draw/src/adapters/openlayers/draw/DrawMode.js +11 -11
  53. package/plugins/draw/src/adapters/openlayers/draw/DrawMode.test.js +15 -15
  54. package/plugins/draw/src/adapters/openlayers/edit/EditMode.js +21 -28
  55. package/plugins/draw/src/adapters/openlayers/edit/EditMode.test.js +26 -5
  56. package/plugins/draw/src/adapters/openlayers/edit/selectionState.js +5 -6
  57. package/plugins/draw/src/adapters/openlayers/edit/selectionState.test.js +2 -12
  58. package/plugins/draw/src/adapters/openlayers/edit/undoOps.js +9 -0
  59. package/plugins/draw/src/adapters/openlayers/edit/undoOps.test.js +7 -0
  60. package/plugins/draw/src/adapters/openlayers/point/drawPointMode.js +6 -9
  61. package/plugins/draw/src/adapters/openlayers/point/drawPointMode.test.js +3 -3
  62. package/plugins/draw/src/adapters/openlayers/point/editPointMode.js +18 -9
  63. package/plugins/draw/src/adapters/openlayers/point/editPointMode.test.js +29 -4
  64. package/plugins/draw/src/adapters/openlayers/point/pointOps.js +3 -2
  65. package/plugins/draw/src/adapters/openlayers/point/pointOps.test.js +6 -0
  66. package/plugins/draw/src/adapters/openlayers/point/pointSelectionState.js +3 -4
  67. package/plugins/draw/src/adapters/openlayers/point/pointSelectionState.test.js +2 -12
  68. package/plugins/draw/src/api/createNewShape.js +4 -4
  69. package/plugins/draw/src/api/createNewShape.test.js +1 -1
  70. package/plugins/draw/src/api/editFeature.js +8 -11
  71. package/plugins/draw/src/api/editFeature.test.js +33 -22
  72. package/plugins/draw/src/api/newLine.test.js +3 -3
  73. package/plugins/draw/src/api/newPoint.js +4 -5
  74. package/plugins/draw/src/api/newPoint.test.js +11 -3
  75. package/plugins/draw/src/api/newPolygon.test.js +11 -3
  76. package/plugins/draw/src/api/recheckGeometry.js +32 -0
  77. package/plugins/draw/src/api/recheckGeometry.test.js +55 -0
  78. package/plugins/draw/src/api/setGeometry.js +33 -0
  79. package/plugins/draw/src/api/setGeometry.test.js +48 -0
  80. package/plugins/draw/src/api/split.js +26 -22
  81. package/plugins/draw/src/api/split.test.js +38 -18
  82. package/plugins/draw/src/events.js +269 -77
  83. package/plugins/draw/src/events.test.js +779 -34
  84. package/plugins/draw/src/hooks/useSpatialList.js +10 -11
  85. package/plugins/draw/src/hooks/useSpatialList.test.js +3 -3
  86. package/plugins/draw/src/manifest.js +30 -15
  87. package/plugins/draw/src/manifest.test.js +91 -9
  88. package/plugins/draw/src/reducer.js +31 -9
  89. package/plugins/draw/src/reducer.test.js +35 -3
  90. package/plugins/draw/src/utils/drawModes.js +27 -0
  91. package/plugins/draw/src/utils/drawModes.test.js +27 -0
  92. package/plugins/draw/src/utils/needsDrawTarget.js +1 -5
  93. package/plugins/draw/src/utils/needsDrawTarget.test.js +1 -11
  94. package/plugins/draw/src/utils/touchTarget.js +34 -26
  95. package/plugins/draw/src/utils/touchTarget.test.js +32 -20
  96. package/plugins/draw/src/validation/commitChecks.js +187 -0
  97. package/plugins/draw/src/validation/commitChecks.test.js +209 -0
  98. package/plugins/draw/src/validation/liveDrawChecks.js +11 -11
  99. package/plugins/draw/src/validation/liveDrawChecks.test.js +37 -37
  100. package/plugins/draw/src/validation/liveStroke.js +6 -6
  101. package/plugins/draw/src/validation/liveStroke.test.js +27 -27
  102. package/plugins/draw/src/validation/rules.js +16 -14
  103. package/plugins/draw/src/validation/rules.test.js +5 -5
  104. package/plugins/draw/src/validation/validateGeometry.js +58 -31
  105. package/plugins/draw/src/validation/validateGeometry.test.js +68 -44
  106. package/plugins/interact/dist/esm/im-interact-plugin.js +1 -1
  107. package/plugins/interact/dist/umd/im-interact-plugin.js +1 -1
  108. package/plugins/interact/dist/umd/index.js +1 -1
  109. package/plugins/interact/src/hooks/useInteractionHandlers.js +3 -5
  110. package/plugins/interact/src/hooks/useInteractionHandlers.test.js +4 -3
  111. package/plugins/interact/src/hooks/useSpatialList.js +16 -17
  112. package/plugins/interact/src/hooks/useSpatialList.test.js +9 -9
  113. package/plugins/map-styles/dist/esm/im-map-styles-plugin.js +1 -1
  114. package/plugins/map-styles/dist/umd/im-map-styles-plugin.js +1 -1
  115. package/plugins/map-styles/src/MapStyles.jsx +2 -2
  116. package/providers/maplibre/dist/esm/index.js +1 -1
  117. package/providers/maplibre/dist/umd/index.js +1 -1
  118. package/providers/maplibre/src/index.js +11 -2
  119. package/providers/maplibre/src/index.test.js +23 -1
  120. package/providers/maplibre/src/utils/detectModuleWorker.js +8 -19
  121. package/providers/maplibre/src/utils/detectModuleWorker.test.js +19 -23
  122. package/providers/maplibre/src/utils/workerUrl.js +12 -0
  123. package/providers/maplibre/src/utils/workerUrl.test.js +14 -1
  124. package/src/App/components/MapButton/MapButton.jsx +6 -0
  125. package/src/App/components/MapButton/MapButton.test.jsx +10 -0
  126. package/src/App/components/Markers/Markers.jsx +3 -5
  127. package/src/App/components/Markers/Markers.test.jsx +9 -11
  128. package/src/App/components/Panel/Panel.jsx +2 -9
  129. package/src/App/hooks/useInterfaceAPI.js +16 -4
  130. package/src/App/hooks/useInterfaceAPI.test.js +66 -1
  131. package/src/App/renderer/mapButtons.js +2 -1
  132. package/src/App/renderer/mapButtons.test.js +9 -0
  133. package/src/App/store/MapProvider.jsx +3 -0
  134. package/src/App/store/MapProvider.test.jsx +24 -0
  135. package/src/config/mapSizeScales.js +3 -0
  136. package/src/types.js +4 -0
  137. package/src/utils/focusAfterPanelClose.js +9 -0
  138. package/src/utils/focusAfterPanelClose.test.js +40 -0
@@ -6,23 +6,36 @@
6
6
  * All MapLibre / OL specifics live in the adapter.
7
7
  */
8
8
  import { ADAPTER_EVENTS } from './adapterEvents.js'
9
- import { validateGeometry } from './validation/validateGeometry.js'
9
+ import { getCommitChecks, isSameGeometry } from './validation/commitChecks.js'
10
10
  import { MIN_VERTICES_REASONS } from './validation/rules.js'
11
- import { MAP_SIZE_SCALES } from './defaults.js'
12
11
  import { stripInternalProperties } from './utils/stripInternalProperties.js'
12
+ import { isEditMode, isCheckedMode } from './utils/drawModes.js'
13
13
 
14
14
  const EDIT_VERTEX_MODE = 'edit_vertex'
15
15
  const GEOMETRY_INVALID_EVENT = 'draw:geometryinvalid'
16
+ const CREATED_EVENT = 'draw:created'
16
17
 
17
- // edit_point behaves like edit_vertex for the two checks below (restore-on-cancel, live
18
- // invalid-stroke gating) — a relocate session, just with one coordinate instead of a ring.
19
- // enterEditVertexMode (below) stays edit_vertex-only — it's only ever reached for a shape
20
- // that finished invalid, and Point geometry has no such failure mode (see rules.js).
21
- const EDIT_MODES = new Set([EDIT_VERTEX_MODE, 'edit_point'])
18
+ // A shape that finished without a passing check is reopened in edit mode, and its
19
+ // eventual edit-finish must report as a creation. Kept per draw adapter, not in
20
+ // createHandlers, which is rebuilt on every plugin state change.
21
+ const pendingCreateIds = new WeakMap()
22
+
23
+ // While editing, Done pressed during a pending check waits for that check (by revision): the
24
+ // edit finishes if it passes, and stays open if it fails or the shape changes again. Kept
25
+ // apart from a held new shape, which a new session discards — an edited feature must stay.
26
+ const editFinishRevisions = new WeakMap()
27
+
28
+ // A finished shape is checked again, rather than reusing the latest result, when there's an
29
+ // onGeometryFinish, or the result asked for it (recheckOnFinish, deprecated). Never on that
30
+ // finishing check itself. onGeometryFinish runs the finishing check; without it, onGeometryChange.
31
+ const FINISH_PHASES = new Set(['create', 'edit-finish'])
32
+ const needsFinishCheck = (draw, { result, context }) =>
33
+ !FINISH_PHASES.has(context?.phase) && (typeof draw._finishValidator === 'function' || !!result?.recheckOnFinish)
34
+ const finishValidatorFor = (draw) => draw._finishValidator ?? draw._commitValidator
22
35
 
23
36
  // A shape that simply hasn't reached its minimum vertex count yet isn't a mistake —
24
37
  // it's the normal, expected state of "still being drawn" — so it's excluded below
25
- // even though it's a genuine SOFT_RULES failure like any other.
38
+ // even though it's a genuine COMMIT_RULES failure like any other.
26
39
  const isIncompleteShape = (reason) => Object.values(MIN_VERTICES_REASONS).includes(reason)
27
40
 
28
41
  // A discrete, one-shot invalid result (a rejected placement, a finished-but-invalid
@@ -31,9 +44,10 @@ const isIncompleteShape = (reason) => Object.values(MIN_VERTICES_REASONS).includ
31
44
  // alongside the existing public event, so the reason is visible without every
32
45
  // consumer having to wire up their own listener for it. The public event still
33
46
  // fires either way — only the hint is skipped for an incomplete shape.
34
- const createGeometryInvalidEmitter = (eventBus, hints) => (payload) => {
47
+ // A result with its own `announcement` is presented by the consumer, so it gets no hint.
48
+ const createGeometryInvalidEmitter = (eventBus, hints) => ({ announcement, ...payload }) => {
35
49
  eventBus.emit(GEOMETRY_INVALID_EVENT, payload)
36
- if (payload.reason && !isIncompleteShape(payload.reason)) { hints.show(payload.reason) }
50
+ if (!announcement && payload.reason && !isIncompleteShape(payload.reason)) { hints.show(payload.reason) }
37
51
  }
38
52
 
39
53
  // Claims MapControls' D-pad for the currently selected vertex — see the generic
@@ -58,44 +72,237 @@ const createVertexSelectionHandlers = ({ draw, pluginState, mapProvider, eventBu
58
72
  }
59
73
  })
60
74
 
61
- // Re-open a feature in vertex-edit mode (used when a drawn shape finishes invalid).
62
- // Mirrors the options built by api/editFeature.js. Edit-mode Done is gated by the
63
- // same validity, so the shape can't be finished until it's fixed.
64
- const enterEditVertexMode = ({ draw, appState, appConfig, mapState, dispatch }, featureId) => {
65
- draw.changeMode(EDIT_VERTEX_MODE, {
75
+ // Re-open a feature in edit mode (used when a drawn shape finishes and its check is
76
+ // invalid or errored). Mirrors the options built by api/editFeature.js. Edit-mode Done is
77
+ // gated by the same check, so the shape can't be finished until it passes.
78
+ // Emits draw:reopened so a consumer knows to show the shape's issues.
79
+ const enterEditMode = ({ draw, appState, appConfig, mapState, dispatch, eventBus }, featureId, geometryType) => {
80
+ const mode = geometryType === 'Point' ? 'edit_point' : EDIT_VERTEX_MODE
81
+ draw.changeMode(mode, {
66
82
  container: appState?.layoutRefs?.viewportRef?.current,
67
83
  deleteVertexButtonId: `${appConfig?.id}-draw-delete-point`,
68
84
  undoButtonId: `${appConfig?.id}-draw-undo`,
69
85
  isPanEnabled: appState?.interfaceType !== 'keyboard',
70
86
  interfaceType: appState?.interfaceType,
71
- scale: MAP_SIZE_SCALES[mapState?.mapSize],
87
+ scale: mapState?.mapSizeScale,
72
88
  featureId,
73
89
  getSnapEnabled: () => draw.isSnapEnabled()
74
90
  })
75
91
  const editing = draw.get(featureId)
92
+ const { status, result } = getCommitChecks(draw).state
76
93
  dispatch({ type: 'SET_FEATURE', payload: { feature: editing, tempFeature: editing } })
77
- dispatch({ type: 'SET_MODE', payload: EDIT_VERTEX_MODE })
78
- dispatch({ type: 'SET_GEOMETRY_VALID', payload: false })
79
- draw.setGeometryValid?.(false)
80
- draw.setInvalid?.(true)
94
+ dispatch({ type: 'SET_MODE', payload: mode })
95
+ dispatch({ type: 'SET_VALIDATION_STATUS', payload: status })
96
+ draw.setCanFinish?.(status === 'valid' || status === 'pending')
97
+ draw.setInvalid?.(status === 'invalid')
98
+ eventBus.emit('draw:reopened', { feature: stripInternalProperties(editing), status, reason: result?.reason ?? null })
99
+ }
100
+
101
+ // Screen reader messages for a slow check (see SLOW_CHECK_MS). A check that settles quickly
102
+ // says nothing new — Done enabling, or the hint for a failure, is enough. A result's own
103
+ // `announcement` replaces these, however quickly it settles.
104
+ const CHECK_MESSAGES = {
105
+ pending: 'Checking shape',
106
+ valid: 'Shape check passed',
107
+ invalid: 'Shape check failed',
108
+ error: 'Shape could not be checked'
109
+ }
110
+
111
+ // Announced as an 'action' so a run of quick changes collapses to the latest message,
112
+ // and so the failure hint's own ambient announcement yields rather than repeating it.
113
+ const announceCheck = (announce, { status, result, slow, afterPending }) => {
114
+ if (result?.announcement) {
115
+ announce?.(result.announcement, 'action')
116
+ } else if (status === 'pending') {
117
+ if (slow) { announce?.(CHECK_MESSAGES.pending, 'action') }
118
+ } else if (afterPending) {
119
+ announce?.((status === 'invalid' && result.reason) || CHECK_MESSAGES[status], 'action')
120
+ } else {
121
+ // Settled straight away
122
+ }
123
+ }
124
+
125
+ // Applies each settled commit-level check (validation/commitChecks.js). Only the
126
+ // latest revision ever reaches here, so a stale reply can't change the Done gate.
127
+ const createCheckStateHandler = ({ draw, dispatch, emitGeometryInvalid, announce }) => (state) => {
128
+ const { status, result, context, feature } = state
129
+ const valid = status === 'valid'
130
+ // A slow check is the same pending check, now shown as "Checking…"
131
+ dispatch(state.slow ? { type: 'SET_CHECK_SLOW' } : { type: 'SET_VALIDATION_STATUS', payload: status })
132
+ 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')
137
+ if (status === 'pending' || status === 'error') { return }
138
+ // The invalid stroke is committed-validity-driven in edit mode only; in draw mode the
139
+ // adapters drive it live from the displayed geometry (placed vertices + cursor).
140
+ if (isEditMode(draw.getMode())) {
141
+ draw.setInvalid?.(!valid)
142
+ }
143
+ // edit-start is the baseline check on opening a feature, not a change the user made
144
+ if (!valid && context?.phase !== 'edit-start') {
145
+ const { reason, source, announcement } = result
146
+ emitGeometryInvalid({ reason, source, announcement, ...context, feature: stripInternalProperties(feature) })
147
+ }
148
+ }
149
+
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.
153
+ function createFinishHandlers ({ appState, appConfig, mapState, draw, dispatch, eventBus, resetState }) {
154
+ const checks = getCommitChecks(draw)
155
+
156
+ const create = (f) => {
157
+ resetState()
158
+ setTimeout(() => draw.changeMode('disabled'), 0)
159
+ eventBus.emit(CREATED_EVENT, stripInternalProperties(f))
160
+ }
161
+
162
+ const reopen = (featureId, geometryType) => {
163
+ pendingCreateIds.set(draw, featureId)
164
+ setTimeout(() => enterEditMode({ draw, appState, appConfig, mapState, dispatch, eventBus }, featureId, geometryType), 0)
165
+ }
166
+
167
+ // The map leaves draw mode; Done stays, disabled and reading "Checking…" once the check is slow
168
+ const hold = (featureId) => {
169
+ checks.hold(featureId)
170
+ dispatch({ type: 'SET_HOLDING', payload: true })
171
+ setTimeout(() => draw.changeMode('disabled'), 0)
172
+ }
173
+
174
+ // A shape can be finished by the Done button OR a map gesture (double-click, clicking
175
+ // the first vertex, Enter), so the latest check is re-applied here — a shape without a
176
+ // passing check must never be finalised. A passing or still-pending result for this
177
+ // same geometry is reused, so finishing doesn't repeat a server call; otherwise (a
178
+ // point, which has no commits before it's created, a failed result, or a finishing check
179
+ // — see needsFinishCheck) the shape is checked now with phase 'create'.
180
+ const checkFinished = (f) => {
181
+ const { status, feature: checkedFeature } = checks.state
182
+ const isReusable = (status === 'valid' || status === 'pending') && isSameGeometry(checkedFeature, f) && !needsFinishCheck(draw, checks.state)
183
+ if (!isReusable) {
184
+ checks.run({ feature: f, context: { phase: 'create', mode: draw.getMode() }, onGeometryChange: finishValidatorFor(draw) })
185
+ }
186
+ return checks.state
187
+ }
188
+
189
+ const finish = (f) => {
190
+ const state = checkFinished(f)
191
+ if (state.status === 'valid') {
192
+ create(f)
193
+ } else if (state.status === 'pending') {
194
+ hold(f.id)
195
+ } else {
196
+ reopen(f.id, f.geometry?.type)
197
+ }
198
+ }
199
+
200
+ return {
201
+ onCreate: finish,
202
+ // A held shape is created once its check passes, or reopened in edit mode. A result that
203
+ // needs a finishing check, unknown until the pending check settled, is checked again.
204
+ onCheckSettled: (state) => {
205
+ const heldId = checks.heldId
206
+ if (heldId === undefined || state.status === 'pending') { return }
207
+ checks.release()
208
+ const finished = draw.get(heldId)
209
+ if (needsFinishCheck(draw, state)) {
210
+ finish(finished)
211
+ } else if (state.status === 'valid') {
212
+ create(finished)
213
+ } else {
214
+ reopen(heldId, finished?.geometry?.type)
215
+ }
216
+ },
217
+ onEditFinish: (f) => {
218
+ resetState()
219
+ setTimeout(() => draw.changeMode('disabled'), 0)
220
+ // A shape that was drawn-then-fixed reports as a creation, not an edit.
221
+ if (pendingCreateIds.get(draw) === f.id) {
222
+ pendingCreateIds.delete(draw)
223
+ eventBus.emit(CREATED_EVENT, stripInternalProperties(f))
224
+ } else {
225
+ eventBus.emit('draw:edited', stripInternalProperties(f))
226
+ }
227
+ }
228
+ }
81
229
  }
82
230
 
83
- function createHandlers ({ appState, appConfig, mapState, pluginState, mapProvider, eventBus, hints, resetState }) {
231
+ // Done, and finishing an edit: pressed while editing with a check pending, Done waits for
232
+ // that check (see editFinishRevisions above) instead of finishing straight away. With an
233
+ // onGeometryFinish, or a result asking for recheckOnFinish, the shape is checked again first,
234
+ // with phase 'edit-finish' — straight away for onGeometryFinish, which decides on its own.
235
+ function createEditFinishHandlers ({ draw, dispatch }) {
236
+ const checks = getCommitChecks(draw)
237
+
238
+ const waitFor = (revision) => {
239
+ editFinishRevisions.set(draw, revision)
240
+ dispatch({ type: 'SET_HOLDING', payload: true })
241
+ }
242
+
243
+ const finishEdit = (state) => {
244
+ if (state.status === 'pending' && !draw._finishValidator) {
245
+ waitFor(state.revision)
246
+ return
247
+ }
248
+ if (!needsFinishCheck(draw, state)) {
249
+ draw.done()
250
+ return
251
+ }
252
+ checks.run({ feature: state.feature, context: { phase: 'edit-finish', mode: draw.getMode() }, onGeometryChange: finishValidatorFor(draw) })
253
+ const next = checks.state
254
+ if (next.status === 'pending') {
255
+ waitFor(next.revision)
256
+ } else if (next.status === 'valid') {
257
+ draw.done()
258
+ } else {
259
+ dispatch({ type: 'SET_HOLDING', payload: false })
260
+ }
261
+ }
262
+
263
+ return {
264
+ handleDone: () => {
265
+ if (isEditMode(draw.getMode())) {
266
+ finishEdit(checks.state)
267
+ return
268
+ }
269
+ draw.done()
270
+ },
271
+ onEditCheckSettled: (state) => {
272
+ if (!editFinishRevisions.has(draw)) { return }
273
+ const isAwaitedCheck = editFinishRevisions.get(draw) === state.revision
274
+ if (isAwaitedCheck && state.status === 'pending') { return }
275
+ editFinishRevisions.delete(draw)
276
+ if (isAwaitedCheck && state.status === 'valid') {
277
+ finishEdit(state)
278
+ return
279
+ }
280
+ dispatch({ type: 'SET_HOLDING', payload: false })
281
+ }
282
+ }
283
+ }
284
+
285
+ function createHandlers ({ appState, appConfig, mapState, pluginState, mapProvider, eventBus, emitGeometryInvalid, announce, resetState }) {
84
286
  const { draw } = mapProvider
85
- const emitGeometryInvalid = createGeometryInvalidEmitter(eventBus, hints)
86
287
  const { feature, tempFeature } = pluginState
87
288
  const { dispatch } = pluginState
88
- // A shape that finished invalid and was re-opened in edit mode: its eventual
89
- // edit-finish must report as a creation, not an edit.
90
- let pendingCreateId = null
289
+ const checks = getCommitChecks(draw)
91
290
 
92
291
  return {
93
- handleDone: () => { draw.done() },
292
+ ...createEditFinishHandlers({ draw, dispatch }),
94
293
  handleCancel: () => {
95
294
  const mode = draw.getMode()
96
- if (EDIT_MODES.has(mode) && tempFeature?.id) { draw.add(feature) }
97
- pendingCreateId = null
98
- draw.cancel(); resetState()
295
+ const isEditing = isEditMode(mode) && !!tempFeature?.id
296
+ const isReopened = isEditing && pendingCreateIds.get(draw) === tempFeature.id
297
+ // A new shape that's finished but not yet created (held, or reopened for fixing) has
298
+ // no earlier version to restore, so it's discarded
299
+ const unfinishedId = checks.heldId ?? (isReopened ? tempFeature.id : undefined)
300
+ if (isEditing && !isReopened) { draw.add(feature) }
301
+ pendingCreateIds.delete(draw)
302
+ checks.release()
303
+ draw.cancel()
304
+ if (unfinishedId !== undefined) { draw.delete(unfinishedId) }
305
+ resetState()
99
306
  eventBus.emit('draw:cancelled', stripInternalProperties(feature))
100
307
  },
101
308
  handleUndo: () => draw.undo(),
@@ -104,68 +311,41 @@ function createHandlers ({ appState, appConfig, mapState, pluginState, mapProvid
104
311
  pluginState.dispatch({ type: 'TOGGLE_SNAP' })
105
312
  draw.setSnapEnabled(!pluginState.snap)
106
313
  },
107
- onCreate: (f) => {
108
- // A shape can be finished by the Done button OR a map gesture (double-click,
109
- // clicking the first vertex, Enter). Only the button is gated, so re-validate
110
- // here to catch the gesture paths — an invalid shape must never be finalised.
111
- const { valid, reason } = validateGeometry(f, { phase: 'create', mode: draw.getMode() }, { onGeometryChange: draw._geometryValidator })
112
- if (!valid) {
113
- pendingCreateId = f.id
114
- emitGeometryInvalid({ feature: stripInternalProperties(f), reason, phase: 'create', mode: EDIT_VERTEX_MODE })
115
- setTimeout(() => enterEditVertexMode({ draw, appState, appConfig, mapState, dispatch }, f.id), 0)
116
- return
117
- }
118
- resetState(); setTimeout(() => draw.changeMode('disabled'), 0); eventBus.emit('draw:created', stripInternalProperties(f))
119
- },
120
- onEditFinish: (f) => {
121
- resetState()
122
- setTimeout(() => draw.changeMode('disabled'), 0)
123
- // A shape that was drawn-then-fixed reports as a creation, not an edit.
124
- if (pendingCreateId && f.id === pendingCreateId) {
125
- pendingCreateId = null
126
- eventBus.emit('draw:created', stripInternalProperties(f))
127
- } else {
128
- eventBus.emit('draw:edited', stripInternalProperties(f))
129
- }
130
- },
131
- onCancel: () => {},
314
+ onCancel: () => { checks.reset() },
315
+ ...createFinishHandlers({ appState, appConfig, mapState, draw, dispatch, eventBus, resetState }),
132
316
  ...createVertexSelectionHandlers({ draw, pluginState, mapProvider, eventBus }),
133
317
  onUndoChange: (l) => { pluginState.dispatch({ type: 'SET_UNDO_STACK_LENGTH', payload: l }) },
134
318
  onUpdate: (f) => { eventBus.emit('draw:updated', stripInternalProperties(f)) },
135
319
  onGeometryChange: (e) => {
136
- // Only commit-level changes (commit-add/move/insert/delete) carry a `phase`.
320
+ // Only commit-level changes carry a `phase` ('change').
137
321
  // Preview events (e.g. split's live preview) have none and are ignored.
138
322
  if (!e?.phase) { return }
139
323
 
324
+ // A commit that lands after the session has ended (the adapters defer them a tick)
325
+ // is dropped rather than calling onGeometryChange.
140
326
  const mode = draw.getMode()
141
- const context = { phase: e.phase, vertexIndex: e.vertexIndex, mode }
142
- const { valid, reason } = validateGeometry(e.feature, context, { onGeometryChange: draw._geometryValidator })
327
+ if (!isCheckedMode(mode)) { return }
143
328
 
144
329
  // Rules only gate the Done button — never revert. A shape can pass through
145
- // interim invalid states while being built or reshaped.
146
- pluginState.dispatch({ type: 'SET_GEOMETRY_VALID', payload: valid })
147
- draw.setGeometryValid?.(valid)
148
- // The invalid stroke is committed-validity-driven in edit mode only; in draw
149
- // mode the adapters drive it live from the displayed geometry (placed
150
- // vertices + cursor) on every rubber-band move.
151
- if (EDIT_MODES.has(mode)) {
152
- draw.setInvalid?.(!valid)
153
- }
154
- if (!valid) {
155
- emitGeometryInvalid({ reason, ...context, feature: stripInternalProperties(e.feature) })
156
- }
330
+ // interim invalid states while being built or reshaped. The result is applied
331
+ // by the check-state handler once it settles.
332
+ checks.run({
333
+ feature: e.feature,
334
+ context: { phase: e.phase, vertexIndex: e.vertexIndex, mode },
335
+ onGeometryChange: draw._commitValidator
336
+ })
157
337
  },
158
- // A vertex placement was rejected (hard rule or user callback veto). Surface it
338
+ // A vertex placement was rejected (placement rule or user callback veto). Surface it
159
339
  // on the public bus with phase 'place', and as a hint toast, so the reason is visible.
160
340
  onPlacementBlocked: (e) => {
341
+ if (e.announcement) { announce?.(e.announcement, 'action') }
161
342
  emitGeometryInvalid({ ...e, feature: stripInternalProperties(e.feature) })
162
343
  },
163
344
  // Live (mid-drag) validity flip while editing — the displayed shape is exactly
164
- // what Done finishes there, so it gates the Done button in real time. Emitted
165
- // by the adapters' edit wiring only, on flips only.
345
+ // what Done finishes there, so it gates the Done button in real time alongside
346
+ // the commit-level check. Emitted by the adapters' edit wiring only, on flips only.
166
347
  onValidityChange: (e) => {
167
- pluginState.dispatch({ type: 'SET_GEOMETRY_VALID', payload: e.valid })
168
- draw.setGeometryValid?.(e.valid)
348
+ pluginState.dispatch({ type: 'SET_PREVIEW_VALID', payload: e.valid })
169
349
  },
170
350
  // Live placement-veto flip while drawing — gates the Add point button so it
171
351
  // never looks active when a tap would be rejected.
@@ -228,7 +408,7 @@ function detachDrawEvents (draw, handlers) {
228
408
  draw.off(ADAPTER_EVENTS.INTERFACE_TYPE_CHANGE, handlers.onInterfaceTypeChange)
229
409
  }
230
410
 
231
- export function attachEvents ({ appState, appConfig, mapState, pluginState, mapProvider, buttonConfig, eventBus, hints }) {
411
+ export function attachEvents ({ appState, appConfig, mapState, pluginState, mapProvider, buttonConfig, eventBus, hints, announce }) {
232
412
  const { draw } = mapProvider
233
413
  const resetState = () => {
234
414
  pluginState.dispatch({ type: 'SET_MODE', payload: null })
@@ -236,9 +416,21 @@ export function attachEvents ({ appState, appConfig, mapState, pluginState, mapP
236
416
  // Release MapControls' D-pad back to panning the map whenever a draw/edit
237
417
  // session ends — a stale claim here would silently hijack it for good.
238
418
  mapProvider.activeMoveTarget = null
419
+ // Ends the check session too, so a reply still in flight is dropped, along with any
420
+ // Done that was waiting on it.
421
+ getCommitChecks(draw).reset()
422
+ editFinishRevisions.delete(draw)
239
423
  }
240
- const handlers = createHandlers({ appState, appConfig, mapState, pluginState, mapProvider, eventBus, hints, resetState })
424
+ const emitGeometryInvalid = createGeometryInvalidEmitter(eventBus, hints)
425
+ const handlers = createHandlers({ appState, appConfig, mapState, pluginState, mapProvider, eventBus, emitGeometryInvalid, announce, resetState })
426
+ const checks = getCommitChecks(draw)
427
+ const applyCheckState = createCheckStateHandler({ draw, dispatch: pluginState.dispatch, emitGeometryInvalid, announce })
428
+ checks.setOnSettle((state) => {
429
+ applyCheckState(state)
430
+ handlers.onCheckSettled(state)
431
+ handlers.onEditCheckSettled(state)
432
+ })
241
433
  attachButtonHandlers(buttonConfig, handlers)
242
434
  attachDrawEvents(draw, handlers)
243
- return () => { detachButtonHandlers(buttonConfig); detachDrawEvents(draw, handlers) }
435
+ return () => { checks.setOnSettle(null); detachButtonHandlers(buttonConfig); detachDrawEvents(draw, handlers) }
244
436
  }