@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.
- package/dist/esm/im-core.js +1 -1
- package/dist/umd/im-core.js +1 -1
- package/dist/umd/index.js +1 -1
- package/docs/api/button-definition.md +11 -0
- package/docs/api/context.md +2 -0
- package/docs/api.md +3 -1
- package/docs/assets/css/docusaurus.scss +7 -1
- package/docs/plugins/draw.md +326 -121
- package/package.json +1 -1
- package/plugins/datasets/dist/esm/im-datasets-plugin.js +1 -1
- package/plugins/datasets/dist/umd/im-datasets-plugin.js +1 -1
- package/plugins/datasets/dist/umd/index.js +1 -1
- package/plugins/datasets/src/initialise/DatasetsInit.jsx +6 -17
- package/plugins/datasets/src/initialise/DatasetsInit.test.jsx +3 -43
- package/plugins/draw/dist/esm/im-draw-ml-adapter.js +1 -1
- package/plugins/draw/dist/esm/im-draw-ol-adapter.js +1 -1
- package/plugins/draw/dist/esm/im-draw-plugin.js +1 -1
- package/plugins/draw/dist/esm/index.js +1 -1
- package/plugins/draw/dist/esm/liveDrawChecks.js +1 -1
- package/plugins/draw/dist/umd/im-draw-ml-adapter.js +1 -1
- package/plugins/draw/dist/umd/im-draw-ol-adapter.js +1 -1
- package/plugins/draw/dist/umd/im-draw-plugin.js +1 -1
- package/plugins/draw/dist/umd/index.js +1 -1
- package/plugins/draw/src/DrawInit.jsx +9 -6
- package/plugins/draw/src/DrawInit.test.jsx +15 -0
- package/plugins/draw/src/adapterEvents.js +82 -44
- package/plugins/draw/src/adapters/adapterContract.test.js +3 -3
- package/plugins/draw/src/adapters/maplibre/MaplibreDrawAdapter.js +18 -10
- package/plugins/draw/src/adapters/maplibre/MaplibreDrawAdapter.test.js +31 -22
- package/plugins/draw/src/adapters/maplibre/drawEvents.js +2 -1
- package/plugins/draw/src/adapters/maplibre/modes/drawMode/clickHandlers.js +13 -12
- package/plugins/draw/src/adapters/maplibre/modes/drawMode/clickHandlers.test.js +5 -5
- package/plugins/draw/src/adapters/maplibre/modes/drawMode/undoHandlers.js +1 -1
- package/plugins/draw/src/adapters/maplibre/modes/drawMode/undoHandlers.test.js +2 -2
- package/plugins/draw/src/adapters/maplibre/modes/drawPointMode.js +6 -9
- package/plugins/draw/src/adapters/maplibre/modes/drawPointMode.test.js +4 -4
- package/plugins/draw/src/adapters/maplibre/modes/editPointMode/pointerHandlers.js +6 -9
- package/plugins/draw/src/adapters/maplibre/modes/editPointMode/pointerHandlers.test.js +1 -1
- package/plugins/draw/src/adapters/maplibre/modes/editPointMode/undoHandlers.js +8 -14
- package/plugins/draw/src/adapters/maplibre/modes/editPointMode/undoHandlers.test.js +37 -8
- package/plugins/draw/src/adapters/maplibre/modes/editPointMode.js +11 -1
- package/plugins/draw/src/adapters/maplibre/modes/editVertexMode/undoHandlers.js +14 -19
- package/plugins/draw/src/adapters/maplibre/modes/editVertexMode/undoHandlers.test.js +41 -4
- package/plugins/draw/src/adapters/maplibre/modes/editVertexMode.js +12 -1
- package/plugins/draw/src/adapters/maplibre/utils/editModeEvents.js +3 -1
- package/plugins/draw/src/adapters/maplibre/utils/geometryValidation.js +9 -10
- package/plugins/draw/src/adapters/maplibre/utils/geometryValidation.test.js +5 -24
- package/plugins/draw/src/adapters/openlayers/OLDrawAdapter.js +10 -7
- package/plugins/draw/src/adapters/openlayers/OLDrawAdapter.test.js +12 -7
- package/plugins/draw/src/adapters/openlayers/core/OLDrawManager.js +5 -0
- package/plugins/draw/src/adapters/openlayers/core/OLDrawManager.test.js +7 -3
- package/plugins/draw/src/adapters/openlayers/draw/DrawMode.js +11 -11
- package/plugins/draw/src/adapters/openlayers/draw/DrawMode.test.js +15 -15
- package/plugins/draw/src/adapters/openlayers/edit/EditMode.js +21 -28
- package/plugins/draw/src/adapters/openlayers/edit/EditMode.test.js +26 -5
- package/plugins/draw/src/adapters/openlayers/edit/selectionState.js +5 -6
- package/plugins/draw/src/adapters/openlayers/edit/selectionState.test.js +2 -12
- package/plugins/draw/src/adapters/openlayers/edit/undoOps.js +9 -0
- package/plugins/draw/src/adapters/openlayers/edit/undoOps.test.js +7 -0
- package/plugins/draw/src/adapters/openlayers/point/drawPointMode.js +6 -9
- package/plugins/draw/src/adapters/openlayers/point/drawPointMode.test.js +3 -3
- package/plugins/draw/src/adapters/openlayers/point/editPointMode.js +18 -9
- package/plugins/draw/src/adapters/openlayers/point/editPointMode.test.js +29 -4
- package/plugins/draw/src/adapters/openlayers/point/pointOps.js +3 -2
- package/plugins/draw/src/adapters/openlayers/point/pointOps.test.js +6 -0
- package/plugins/draw/src/adapters/openlayers/point/pointSelectionState.js +3 -4
- package/plugins/draw/src/adapters/openlayers/point/pointSelectionState.test.js +2 -12
- package/plugins/draw/src/api/createNewShape.js +4 -4
- package/plugins/draw/src/api/createNewShape.test.js +1 -1
- package/plugins/draw/src/api/editFeature.js +8 -11
- package/plugins/draw/src/api/editFeature.test.js +33 -22
- package/plugins/draw/src/api/newLine.test.js +3 -3
- package/plugins/draw/src/api/newPoint.js +4 -5
- package/plugins/draw/src/api/newPoint.test.js +11 -3
- package/plugins/draw/src/api/newPolygon.test.js +11 -3
- package/plugins/draw/src/api/recheckGeometry.js +32 -0
- package/plugins/draw/src/api/recheckGeometry.test.js +55 -0
- package/plugins/draw/src/api/setGeometry.js +33 -0
- package/plugins/draw/src/api/setGeometry.test.js +48 -0
- package/plugins/draw/src/api/split.js +26 -22
- package/plugins/draw/src/api/split.test.js +38 -18
- package/plugins/draw/src/events.js +269 -77
- package/plugins/draw/src/events.test.js +779 -34
- package/plugins/draw/src/hooks/useSpatialList.js +10 -11
- package/plugins/draw/src/hooks/useSpatialList.test.js +3 -3
- package/plugins/draw/src/manifest.js +30 -15
- package/plugins/draw/src/manifest.test.js +91 -9
- package/plugins/draw/src/reducer.js +31 -9
- package/plugins/draw/src/reducer.test.js +35 -3
- package/plugins/draw/src/utils/drawModes.js +27 -0
- package/plugins/draw/src/utils/drawModes.test.js +27 -0
- package/plugins/draw/src/utils/needsDrawTarget.js +1 -5
- package/plugins/draw/src/utils/needsDrawTarget.test.js +1 -11
- package/plugins/draw/src/utils/touchTarget.js +34 -26
- package/plugins/draw/src/utils/touchTarget.test.js +32 -20
- package/plugins/draw/src/validation/commitChecks.js +187 -0
- package/plugins/draw/src/validation/commitChecks.test.js +209 -0
- package/plugins/draw/src/validation/liveDrawChecks.js +11 -11
- package/plugins/draw/src/validation/liveDrawChecks.test.js +37 -37
- package/plugins/draw/src/validation/liveStroke.js +6 -6
- package/plugins/draw/src/validation/liveStroke.test.js +27 -27
- package/plugins/draw/src/validation/rules.js +16 -14
- package/plugins/draw/src/validation/rules.test.js +5 -5
- package/plugins/draw/src/validation/validateGeometry.js +58 -31
- package/plugins/draw/src/validation/validateGeometry.test.js +68 -44
- package/plugins/interact/dist/esm/im-interact-plugin.js +1 -1
- package/plugins/interact/dist/umd/im-interact-plugin.js +1 -1
- package/plugins/interact/dist/umd/index.js +1 -1
- package/plugins/interact/src/hooks/useInteractionHandlers.js +3 -5
- package/plugins/interact/src/hooks/useInteractionHandlers.test.js +4 -3
- package/plugins/interact/src/hooks/useSpatialList.js +16 -17
- package/plugins/interact/src/hooks/useSpatialList.test.js +9 -9
- package/plugins/map-styles/dist/esm/im-map-styles-plugin.js +1 -1
- package/plugins/map-styles/dist/umd/im-map-styles-plugin.js +1 -1
- package/plugins/map-styles/src/MapStyles.jsx +2 -2
- package/providers/maplibre/dist/esm/index.js +1 -1
- package/providers/maplibre/dist/umd/index.js +1 -1
- package/providers/maplibre/src/index.js +11 -2
- package/providers/maplibre/src/index.test.js +23 -1
- package/providers/maplibre/src/utils/detectModuleWorker.js +8 -19
- package/providers/maplibre/src/utils/detectModuleWorker.test.js +19 -23
- package/providers/maplibre/src/utils/workerUrl.js +12 -0
- package/providers/maplibre/src/utils/workerUrl.test.js +14 -1
- package/src/App/components/MapButton/MapButton.jsx +6 -0
- package/src/App/components/MapButton/MapButton.test.jsx +10 -0
- package/src/App/components/Markers/Markers.jsx +3 -5
- package/src/App/components/Markers/Markers.test.jsx +9 -11
- package/src/App/components/Panel/Panel.jsx +2 -9
- package/src/App/hooks/useInterfaceAPI.js +16 -4
- package/src/App/hooks/useInterfaceAPI.test.js +66 -1
- package/src/App/renderer/mapButtons.js +2 -1
- package/src/App/renderer/mapButtons.test.js +9 -0
- package/src/App/store/MapProvider.jsx +3 -0
- package/src/App/store/MapProvider.test.jsx +24 -0
- package/src/config/mapSizeScales.js +3 -0
- package/src/types.js +4 -0
- package/src/utils/focusAfterPanelClose.js +9 -0
- 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
|
|
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.
|
|
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
|
|
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: '
|
|
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
|
|
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
|
-
|
|
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) => (
|
|
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(
|
|
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: '
|
|
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
|
-
|
|
16
|
+
const { stroke, fill, strokeWidth, properties: customProperties, onGeometryChange, onGeometryPreview, onGeometryFinish, ...modeOptions } = options
|
|
17
|
+
startCheckSession(draw, { onGeometryChange, onGeometryPreview, onGeometryFinish }, 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 {
|
|
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
|
-
|
|
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:
|
|
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
|
|
51
|
-
//
|
|
52
|
-
|
|
53
|
-
|
|
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: {
|
|
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(
|
|
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.
|
|
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
|
-
|
|
102
|
-
|
|
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(
|
|
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,
|
|
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(
|
|
125
|
+
expect(getCommitChecks(draw).state.status).toBe('invalid')
|
|
114
126
|
})
|
|
115
127
|
|
|
116
|
-
test('
|
|
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(
|
|
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.
|
|
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.
|
|
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
|
-
|
|
13
|
+
const { properties: customProperties, onGeometryChange, onGeometryPreview, onGeometryFinish, ...modeOptions } = options
|
|
14
|
+
startCheckSession(draw, { onGeometryChange, onGeometryPreview, onGeometryFinish }, 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.
|
|
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,14 @@ 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.
|
|
115
|
+
expect(draw._commitValidator).toBe(pluginOnGeometryChange)
|
|
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')
|
|
116
124
|
})
|
|
117
125
|
})
|
|
@@ -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,14 +80,22 @@ describe('newPolygon', () => {
|
|
|
80
80
|
const { context, draw } = makeContext()
|
|
81
81
|
const onGeometryChange = jest.fn()
|
|
82
82
|
newPolygon(context, 'f1', { onGeometryChange })
|
|
83
|
-
expect(draw.
|
|
83
|
+
expect(draw._commitValidator).toBe(onGeometryChange)
|
|
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 } })
|
|
90
98
|
newPolygon(context, 'f1')
|
|
91
|
-
expect(draw.
|
|
99
|
+
expect(draw._commitValidator).toBe(pluginOnGeometryChange)
|
|
92
100
|
})
|
|
93
101
|
})
|
|
@@ -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
|
+
})
|