@defra/interactive-map 0.0.57-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.
@@ -3,6 +3,7 @@
3
3
  * `context.phase` through validateGeometry (validation/validateGeometry.js) to the
4
4
  * default rules (validation/rules.js) and to the user's callbacks:
5
5
  * - 'preview' and 'place' go to `onGeometryPreview`, which must answer synchronously
6
+ * - 'create' and 'edit-finish' go to `onGeometryFinish` when it's set
6
7
  * - every other phase goes to `onGeometryChange`, which may return a Promise
7
8
  *
8
9
  * @typedef {'preview' | 'place' | 'change' | 'create' | 'edit-start' | 'edit-finish' | 'recheck'} GeometryChangePhase
@@ -17,13 +18,15 @@
17
18
  * synchronously; a PLACE_RULES failure vetoes it outright
18
19
  * and it never appears. Never fires continuously — that's
19
20
  * 'preview''s job (see above).
20
- * 'create' - a whole new feature finished without a passing or pending
21
- * result for its final geometry (e.g. a new point), or its
22
- * last result asked for recheckOnFinish, so it's checked now.
21
+ * 'create' - a whole new feature finished. Always checked with
22
+ * onGeometryFinish; otherwise only without a passing or pending
23
+ * result for its final geometry (e.g. a new point), or when its
24
+ * last result asked for recheckOnFinish.
23
25
  * 'edit-start' - an existing feature was just loaded into an edit session;
24
26
  * nothing has changed yet, this is the baseline check.
25
- * 'edit-finish' - Done was pressed on an edit whose last result asked for
26
- * recheckOnFinish, so it's checked now.
27
+ * 'edit-finish' - Done was pressed on an edit. Always checked with
28
+ * onGeometryFinish; otherwise only when its last result asked
29
+ * for recheckOnFinish.
27
30
  * 'recheck' - the unchanged shape is checked again (recheckGeometry), with
28
31
  * any detail it was given as `event.detail`.
29
32
  * 'change' - the shape changed: a vertex was added, moved, inserted or
@@ -73,7 +76,7 @@ export const CHANGE_PHASE = 'change'
73
76
  */
74
77
 
75
78
  /**
76
- * What a user's `onGeometryPreview` or `onGeometryChange` callback returns, after
79
+ * What a user's `onGeometryPreview`, `onGeometryChange` or `onGeometryFinish` callback returns, after
77
80
  * normaliseResult (validation/validateGeometry.js). `true`/`undefined` and `false`
78
81
  * are shorthands for valid and invalid.
79
82
  *
@@ -82,12 +85,13 @@ export const CHANGE_PHASE = 'change'
82
85
  * @property {string} [reason] - shown as a hint toast and sent with draw:geometryinvalid
83
86
  * @property {string} [announcement] - announced to screen readers in place of draw's own
84
87
  * message; the reason then isn't shown as a hint, as the consumer presents it
85
- * @property {boolean} [recheckOnFinish] - `onGeometryChange` only: don't reuse this
86
- * result when the shape is finished, check it again with phase 'create'
88
+ * @property {boolean} [recheckOnFinish] - deprecated, use onGeometryFinish.
89
+ * `onGeometryChange` only: don't reuse this result when the shape is finished, check
90
+ * it again with phase 'create' or 'edit-finish'
87
91
  */
88
92
 
89
93
  /**
90
- * The second argument passed to a user's `onGeometryChange` callback —
94
+ * The second argument passed to a user's `onGeometryChange` or `onGeometryFinish` callback —
91
95
  * `onGeometryChange(event, { signal, revision })` (validation/commitChecks.js).
92
96
  *
93
97
  * @typedef {object} GeometryCheckContext
@@ -13,8 +13,8 @@ export const createNewShape = (mode) => ({ appState, appConfig, pluginConfig, pl
13
13
  return
14
14
  }
15
15
 
16
- const { stroke, fill, strokeWidth, properties: customProperties, onGeometryChange, onGeometryPreview, ...modeOptions } = options
17
- startCheckSession(draw, { onGeometryChange, onGeometryPreview }, pluginConfig, eventBus)
16
+ const { stroke, fill, strokeWidth, properties: customProperties, onGeometryChange, onGeometryPreview, onGeometryFinish, ...modeOptions } = options
17
+ startCheckSession(draw, { onGeometryChange, onGeometryPreview, onGeometryFinish }, pluginConfig, eventBus)
18
18
  eventBus.emit('draw:started', { mode, featureId })
19
19
 
20
20
  const snapLayers = options.snapLayers === undefined ? (pluginConfig.snapLayers ?? null) : options.snapLayers
@@ -10,8 +10,8 @@ export const newPoint = ({ appState, appConfig, pluginConfig, pluginState, mapSt
10
10
  return
11
11
  }
12
12
 
13
- const { properties: customProperties, onGeometryChange, onGeometryPreview, ...modeOptions } = options
14
- startCheckSession(draw, { onGeometryChange, onGeometryPreview }, pluginConfig, eventBus)
13
+ const { properties: customProperties, onGeometryChange, onGeometryPreview, onGeometryFinish, ...modeOptions } = options
14
+ startCheckSession(draw, { onGeometryChange, onGeometryPreview, onGeometryFinish }, pluginConfig, eventBus)
15
15
  eventBus.emit('draw:started', { mode: 'draw_point', featureId })
16
16
 
17
17
  const snapLayers = options.snapLayers === undefined ? (pluginConfig.snapLayers ?? null) : options.snapLayers
@@ -114,4 +114,12 @@ describe('newPoint', () => {
114
114
  newPoint(context, 'f1')
115
115
  expect(draw._commitValidator).toBe(pluginOnGeometryChange)
116
116
  })
117
+
118
+ test('stores a per-call onGeometryFinish without leaking it into mode options', () => {
119
+ const { context, draw } = makeContext()
120
+ const onGeometryFinish = jest.fn()
121
+ newPoint(context, 'f1', { onGeometryFinish })
122
+ expect(draw._finishValidator).toBe(onGeometryFinish)
123
+ expect(draw.changeMode.mock.calls[0][1]).not.toHaveProperty('onGeometryFinish')
124
+ })
117
125
  })
@@ -84,6 +84,14 @@ describe('newPolygon', () => {
84
84
  expect(draw.changeMode.mock.calls[0][1]).not.toHaveProperty('onGeometryChange')
85
85
  })
86
86
 
87
+ test('stores a per-call onGeometryFinish without leaking it into mode options', () => {
88
+ const { context, draw } = makeContext()
89
+ const onGeometryFinish = jest.fn()
90
+ newPolygon(context, 'f1', { onGeometryFinish })
91
+ expect(draw._finishValidator).toBe(onGeometryFinish)
92
+ expect(draw.changeMode.mock.calls[0][1]).not.toHaveProperty('onGeometryFinish')
93
+ })
94
+
87
95
  test('falls back to the plugin-level onGeometryChange validator', () => {
88
96
  const pluginOnGeometryChange = jest.fn()
89
97
  const { context, draw } = makeContext({ pluginConfig: { onGeometryChange: pluginOnGeometryChange } })
@@ -63,8 +63,10 @@ export const split = ({ appState, appConfig, pluginState, mapState, mapProvider,
63
63
  // Swap in split's own rules; restored in stopListening.
64
64
  const previousPreviewValidator = draw._previewValidator
65
65
  const previousValidator = draw._commitValidator
66
+ const previousFinishValidator = draw._finishValidator
66
67
  draw._previewValidator = createSplitPreviewValidator({ draw, polygonFeature })
67
68
  draw._commitValidator = createSplitCommitValidator({ draw, dispatch, polygonFeature })
69
+ draw._finishValidator = null
68
70
  getCommitChecks(draw).reset()
69
71
 
70
72
  // Always include the draw outline layer so the split line snaps to it
@@ -86,6 +88,7 @@ export const split = ({ appState, appConfig, pluginState, mapState, mapProvider,
86
88
  const stopListening = () => {
87
89
  draw._previewValidator = previousPreviewValidator
88
90
  draw._commitValidator = previousValidator
91
+ draw._finishValidator = previousFinishValidator
89
92
  draw.off(ADAPTER_EVENTS.CREATE, onSplitCreate)
90
93
  draw.off(ADAPTER_EVENTS.CANCEL, onSplitCancel)
91
94
  }
@@ -79,12 +79,16 @@ describe('split', () => {
79
79
  const featureCollection = { type: 'FeatureCollection' }
80
80
  const previousValidator = jest.fn()
81
81
  const previousPreviewValidator = jest.fn()
82
+ const previousFinishValidator = jest.fn()
82
83
  draw._commitValidator = previousValidator
83
84
  draw._previewValidator = previousPreviewValidator
85
+ draw._finishValidator = previousFinishValidator
84
86
  draw.get.mockReturnValue(polygonFeature)
85
87
  splitPolygon.mockReturnValue(featureCollection)
86
88
 
87
89
  split(context, 'poly')
90
+ // The split line is never passed to the consumer's onGeometryFinish
91
+ expect(draw._finishValidator).toBeNull()
88
92
  const onCreate = handlerFor(draw, 'create')
89
93
  const geojson = { id: 'line' }
90
94
  onCreate(geojson)
@@ -96,6 +100,7 @@ describe('split', () => {
96
100
  expect(eventBus.emit).toHaveBeenCalledWith('draw:split', { originalFeatureId: 'poly', featureCollection })
97
101
  expect(draw._commitValidator).toBe(previousValidator)
98
102
  expect(draw._previewValidator).toBe(previousPreviewValidator)
103
+ expect(draw._finishValidator).toBe(previousFinishValidator)
99
104
  // The splitter line only ever existed to compute the split — it must not
100
105
  // linger in the draw store afterwards.
101
106
  expect(draw.delete).toHaveBeenCalledWith('_splitter')
@@ -25,10 +25,13 @@ const pendingCreateIds = new WeakMap()
25
25
  // apart from a held new shape, which a new session discards — an edited feature must stay.
26
26
  const editFinishRevisions = new WeakMap()
27
27
 
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.
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.
30
31
  const FINISH_PHASES = new Set(['create', 'edit-finish'])
31
- const wantsRecheckOnFinish = ({ result, context }) => !!result?.recheckOnFinish && !FINISH_PHASES.has(context?.phase)
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
32
35
 
33
36
  // A shape that simply hasn't reached its minimum vertex count yet isn't a mistake —
34
37
  // it's the normal, expected state of "still being drawn" — so it's excluded below
@@ -172,13 +175,13 @@ function createFinishHandlers ({ appState, appConfig, mapState, draw, dispatch,
172
175
  // the first vertex, Enter), so the latest check is re-applied here — a shape without a
173
176
  // passing check must never be finalised. A passing or still-pending result for this
174
177
  // 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'.
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'.
177
180
  const checkFinished = (f) => {
178
181
  const { status, feature: checkedFeature } = checks.state
179
- const isReusable = (status === 'valid' || status === 'pending') && isSameGeometry(checkedFeature, f) && !wantsRecheckOnFinish(checks.state)
182
+ const isReusable = (status === 'valid' || status === 'pending') && isSameGeometry(checkedFeature, f) && !needsFinishCheck(draw, checks.state)
180
183
  if (!isReusable) {
181
- checks.run({ feature: f, context: { phase: 'create', mode: draw.getMode() }, onGeometryChange: draw._commitValidator })
184
+ checks.run({ feature: f, context: { phase: 'create', mode: draw.getMode() }, onGeometryChange: finishValidatorFor(draw) })
182
185
  }
183
186
  return checks.state
184
187
  }
@@ -197,13 +200,13 @@ function createFinishHandlers ({ appState, appConfig, mapState, draw, dispatch,
197
200
  return {
198
201
  onCreate: finish,
199
202
  // 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.
203
+ // needs a finishing check, unknown until the pending check settled, is checked again.
201
204
  onCheckSettled: (state) => {
202
205
  const heldId = checks.heldId
203
206
  if (heldId === undefined || state.status === 'pending') { return }
204
207
  checks.release()
205
208
  const finished = draw.get(heldId)
206
- if (wantsRecheckOnFinish(state)) {
209
+ if (needsFinishCheck(draw, state)) {
207
210
  finish(finished)
208
211
  } else if (state.status === 'valid') {
209
212
  create(finished)
@@ -226,8 +229,9 @@ function createFinishHandlers ({ appState, appConfig, mapState, draw, dispatch,
226
229
  }
227
230
 
228
231
  // 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'.
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.
231
235
  function createEditFinishHandlers ({ draw, dispatch }) {
232
236
  const checks = getCommitChecks(draw)
233
237
 
@@ -237,15 +241,15 @@ function createEditFinishHandlers ({ draw, dispatch }) {
237
241
  }
238
242
 
239
243
  const finishEdit = (state) => {
240
- if (state.status === 'pending') {
244
+ if (state.status === 'pending' && !draw._finishValidator) {
241
245
  waitFor(state.revision)
242
246
  return
243
247
  }
244
- if (!wantsRecheckOnFinish(state)) {
248
+ if (!needsFinishCheck(draw, state)) {
245
249
  draw.done()
246
250
  return
247
251
  }
248
- checks.run({ feature: state.feature, context: { phase: 'edit-finish', mode: draw.getMode() }, onGeometryChange: draw._commitValidator })
252
+ checks.run({ feature: state.feature, context: { phase: 'edit-finish', mode: draw.getMode() }, onGeometryChange: finishValidatorFor(draw) })
249
253
  const next = checks.state
250
254
  if (next.status === 'pending') {
251
255
  waitFor(next.revision)
@@ -1007,6 +1007,77 @@ describe('async geometry checks', () => {
1007
1007
  })
1008
1008
  })
1009
1009
 
1010
+ describe('onGeometryFinish', () => {
1011
+ const drawing = () => {
1012
+ const context = setup()
1013
+ context.draw.get.mockReturnValue({ id: 'new', ...square })
1014
+ context.draw._commitValidator = jest.fn(() => undefined)
1015
+ return context
1016
+ }
1017
+
1018
+ test('checks a finished new shape with phase create, instead of reusing onGeometryChange', () => {
1019
+ const { draw, eventBus } = drawing()
1020
+ draw._finishValidator = jest.fn(() => true)
1021
+ commit(draw, square, 'change')
1022
+ drawHandler(draw, 'create')({ id: 'new', ...square })
1023
+ expect(draw._commitValidator).toHaveBeenCalledTimes(1)
1024
+ expect(draw._finishValidator).toHaveBeenCalledWith(expect.objectContaining({ phase: 'create', feature: { id: 'new', ...square } }), expect.anything())
1025
+ expect(eventBus.emit).toHaveBeenCalledWith('draw:created', expect.objectContaining({ id: 'new' }))
1026
+ })
1027
+
1028
+ test('holds the shape while it runs, then reopens it if it fails', async () => {
1029
+ const { draw, eventBus } = drawing()
1030
+ const reply = deferred()
1031
+ draw._finishValidator = () => reply.promise
1032
+ commit(draw, square, 'change')
1033
+ drawHandler(draw, 'create')({ id: 'new', ...square })
1034
+ jest.runAllTimers()
1035
+ expect(eventBus.emit).not.toHaveBeenCalledWith('draw:created', expect.anything())
1036
+
1037
+ reply.resolve({ valid: false, reason: 'overlap' })
1038
+ await flush()
1039
+ jest.runAllTimers()
1040
+ expect(eventBus.emit).toHaveBeenCalledWith('draw:reopened', expect.objectContaining({ status: 'invalid', reason: 'overlap' }))
1041
+ })
1042
+
1043
+ test('runs on Done while editing with phase edit-finish, then finishes the edit if it passes', async () => {
1044
+ const { draw, buttonConfig } = drawing()
1045
+ draw.getMode.mockReturnValue('edit_vertex')
1046
+ const reply = deferred()
1047
+ draw._finishValidator = jest.fn(() => reply.promise)
1048
+ commit(draw, square, 'change')
1049
+
1050
+ buttonConfig.drawDone.onClick()
1051
+ expect(draw._finishValidator).toHaveBeenCalledWith(expect.objectContaining({ phase: 'edit-finish', mode: 'edit_vertex' }), expect.anything())
1052
+ expect(draw.done).not.toHaveBeenCalled()
1053
+
1054
+ reply.resolve(true)
1055
+ await flush()
1056
+ expect(draw._finishValidator).toHaveBeenCalledTimes(1)
1057
+ expect(draw.done).toHaveBeenCalledTimes(1)
1058
+ })
1059
+
1060
+ test('keeps an edit open if it fails', () => {
1061
+ const { draw, buttonConfig } = drawing()
1062
+ draw.getMode.mockReturnValue('edit_vertex')
1063
+ draw._finishValidator = () => false
1064
+ commit(draw, square, 'change')
1065
+ buttonConfig.drawDone.onClick()
1066
+ expect(draw.done).not.toHaveBeenCalled()
1067
+ })
1068
+
1069
+ test('doesn’t wait for a pending onGeometryChange before running', () => {
1070
+ const { draw, buttonConfig } = drawing()
1071
+ draw.getMode.mockReturnValue('edit_vertex')
1072
+ draw._commitValidator = () => new Promise(() => {})
1073
+ draw._finishValidator = jest.fn(() => true)
1074
+ commit(draw, square, 'change')
1075
+ buttonConfig.drawDone.onClick()
1076
+ expect(draw._finishValidator).toHaveBeenCalledTimes(1)
1077
+ expect(draw.done).toHaveBeenCalledTimes(1)
1078
+ })
1079
+ })
1080
+
1010
1081
  test('survives the handlers being rebuilt mid-check', async () => {
1011
1082
  const first = setup()
1012
1083
  const { draw, eventBus } = first
@@ -165,17 +165,19 @@ export const isSameGeometry = (a, b) => {
165
165
  }
166
166
 
167
167
  /**
168
- * Starts a draw/edit session's checks: installs the session's two callbacks on the
168
+ * Starts a draw/edit session's checks: installs the session's callbacks on the
169
169
  * adapter and ends any previous session's check. A per-call callback replaces the
170
170
  * plugin-level one. A shape still waiting on that check is discarded, as Cancel would.
171
171
  *
172
172
  * - `draw._previewValidator` — onGeometryPreview, read by the adapters for the
173
173
  * 'preview' and 'place' phases
174
174
  * - `draw._commitValidator` — onGeometryChange, read by events.js for committed changes
175
+ * - `draw._finishValidator` — onGeometryFinish, read by events.js when a shape is finished
175
176
  */
176
177
  export const startCheckSession = (draw, options = {}, pluginConfig = {}, eventBus = null) => {
177
178
  draw._previewValidator = options.onGeometryPreview ?? pluginConfig.onGeometryPreview
178
179
  draw._commitValidator = options.onGeometryChange ?? pluginConfig.onGeometryChange
180
+ draw._finishValidator = options.onGeometryFinish ?? pluginConfig.onGeometryFinish
179
181
  const heldId = getCommitChecks(draw).reset()
180
182
  if (heldId !== undefined) {
181
183
  const discarded = draw.get(heldId)
@@ -166,16 +166,20 @@ describe('startCheckSession', () => {
166
166
  const draw = {}
167
167
  const onGeometryChange = jest.fn()
168
168
  const onGeometryPreview = jest.fn(() => true)
169
- startCheckSession(draw, { onGeometryChange, onGeometryPreview }, { onGeometryChange: jest.fn(), onGeometryPreview: jest.fn() })
169
+ const onGeometryFinish = jest.fn()
170
+ startCheckSession(draw, { onGeometryChange, onGeometryPreview, onGeometryFinish }, { onGeometryChange: jest.fn(), onGeometryPreview: jest.fn(), onGeometryFinish: jest.fn() })
170
171
  expect(draw._commitValidator).toBe(onGeometryChange)
171
172
  expect(draw._previewValidator).toBe(onGeometryPreview)
173
+ expect(draw._finishValidator).toBe(onGeometryFinish)
172
174
  })
173
175
 
174
176
  test('falls back to the plugin-level callbacks', () => {
175
177
  const draw = {}
176
178
  const onGeometryChange = jest.fn()
177
- startCheckSession(draw, {}, { onGeometryChange })
179
+ const onGeometryFinish = jest.fn()
180
+ startCheckSession(draw, {}, { onGeometryChange, onGeometryFinish })
178
181
  expect(draw._commitValidator).toBe(onGeometryChange)
182
+ expect(draw._finishValidator).toBe(onGeometryFinish)
179
183
  expect(draw._previewValidator).toBeUndefined()
180
184
  })
181
185