@defra/interactive-map 0.0.58-alpha → 0.0.60-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/assets/templates/add-polygons.njk +2 -2
- package/assets/templates/add-symbols.njk +2 -2
- package/dist/css/index.css +1 -1
- package/dist/esm/im-core.js +1 -1
- package/dist/esm/im-shell.js +1 -1
- package/dist/umd/im-core.js +1 -1
- package/dist/umd/index.js +1 -1
- package/docs/api/panel-definition.md +30 -0
- package/docs/api/symbol-config.md +12 -0
- package/docs/api.md +21 -2
- package/docs/assets/images/slot-map.svg +3 -3
- package/docs/plugins/datasets.md +11 -1
- package/docs/plugins/draw.md +95 -13
- package/package.json +1 -1
- package/plugins/datasets/dist/esm/index.js +1 -1
- package/plugins/datasets/dist/esm/layerAdapter.js +1 -1
- package/plugins/datasets/dist/umd/im-datasets-ml-adapter.js +1 -1
- package/plugins/datasets/dist/umd/im-datasets-ol-adapter.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/index.js +0 -1
- package/plugins/datasets/src/index.test.js +2 -8
- package/plugins/datasets/src/registry/dataset.test.js +9 -0
- 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/adapterEvents.js +2 -0
- package/plugins/draw/src/adapters/adapterContract.test.js +1 -0
- package/plugins/draw/src/adapters/maplibre/MaplibreDrawAdapter.js +15 -3
- package/plugins/draw/src/adapters/maplibre/MaplibreDrawAdapter.test.js +27 -0
- package/plugins/draw/src/adapters/maplibre/modes/editPointMode/pointerHandlers.js +8 -19
- package/plugins/draw/src/adapters/maplibre/modes/editPointMode/pointerHandlers.test.js +20 -0
- package/plugins/draw/src/adapters/maplibre/modes/editVertexMode/pointerHandlers.js +6 -17
- package/plugins/draw/src/adapters/maplibre/modes/editVertexMode/pointerHandlers.test.js +19 -0
- package/plugins/draw/src/adapters/maplibre/utils/editModeEvents.js +29 -1
- package/plugins/draw/src/adapters/openlayers/OLDrawAdapter.js +13 -3
- package/plugins/draw/src/adapters/openlayers/OLDrawAdapter.test.js +28 -0
- package/plugins/draw/src/api/addFeature.js +9 -6
- package/plugins/draw/src/api/addFeature.test.js +37 -10
- package/plugins/draw/src/api/editFeature.js +1 -1
- package/plugins/draw/src/api/recheckGeometry.js +1 -1
- package/plugins/draw/src/api/recheckGeometry.test.js +2 -2
- package/plugins/draw/src/api/setProperties.js +39 -0
- package/plugins/draw/src/api/setProperties.test.js +74 -0
- package/plugins/draw/src/api/setStyle.js +16 -19
- package/plugins/draw/src/api/setStyle.test.js +17 -2
- package/plugins/draw/src/api/split.js +7 -4
- package/plugins/draw/src/api/split.test.js +25 -2
- package/plugins/draw/src/events.js +95 -53
- package/plugins/draw/src/events.test.js +425 -6
- package/plugins/draw/src/manifest.js +6 -4
- package/plugins/draw/src/manifest.test.js +20 -26
- package/plugins/draw/src/reducer.js +7 -0
- package/plugins/draw/src/reducer.test.js +11 -0
- package/plugins/draw/src/utils/flattenStyleProperties.js +54 -2
- package/plugins/draw/src/utils/flattenStyleProperties.test.js +74 -1
- package/plugins/draw/src/utils/isPlainObject.js +14 -0
- package/plugins/draw/src/utils/isPlainObject.test.js +24 -0
- package/plugins/draw/src/utils/mergeFeatureProperties.js +45 -0
- package/plugins/draw/src/utils/mergeFeatureProperties.test.js +48 -0
- package/plugins/draw/src/utils/stripInternalProperties.js +3 -2
- package/plugins/draw/src/validation/commitChecks.js +29 -11
- package/plugins/draw/src/validation/commitChecks.test.js +67 -11
- package/plugins/draw/src/validation/validateGeometry.js +12 -5
- package/plugins/draw/src/validation/validateGeometry.test.js +18 -0
- package/plugins/interact/dist/esm/im-interact-plugin.js +1 -1
- package/plugins/interact/dist/umd/im-interact-plugin.js +1 -1
- package/plugins/map-key/dist/esm/im-map-key-plugin.js +1 -1
- package/plugins/map-key/dist/umd/im-map-key-plugin.js +1 -1
- package/plugins/search/dist/css/index.css +1 -1
- package/plugins/search/src/components/Form/Form.module.scss +1 -0
- package/providers/esri/dist/esm/im-esri-provider.js +1 -1
- package/providers/esri/src/esriProvider.js +10 -21
- package/providers/esri/src/utils/coords.js +37 -7
- package/providers/esri/src/utils/coords.test.js +22 -1
- package/providers/esri/src/utils/spatial.js +1 -47
- package/providers/maplibre/dist/esm/im-maplibre-provider.js +1 -1
- package/providers/maplibre/dist/umd/im-maplibre-provider.js +1 -1
- package/providers/maplibre/src/maplibreProvider.js +5 -14
- package/providers/maplibre/src/maplibreProvider.test.js +143 -146
- package/providers/maplibre/src/utils/spatial.js +0 -40
- package/providers/maplibre/src/utils/spatial.test.js +0 -35
- package/providers/openlayers/dist/esm/im-openlayers-provider.js +1 -1
- package/providers/openlayers/dist/umd/im-openlayers-provider.js +1 -1
- package/providers/openlayers/src/openlayersProvider.js +5 -14
- package/providers/openlayers/src/openlayersProvider.test.js +12 -10
- package/providers/openlayers/src/utils/spatial.js +0 -39
- package/providers/openlayers/src/utils/spatial.test.js +1 -26
- package/src/App/components/Hints/Hints.jsx +9 -21
- package/src/App/components/Hints/Hints.test.jsx +20 -33
- package/src/App/components/Panel/Panel.jsx +16 -1
- package/src/App/components/Panel/Panel.test.jsx +44 -0
- package/src/App/hooks/useCrossHairAPI.js +27 -28
- package/src/App/hooks/useCrossHairAPI.test.js +64 -7
- package/src/App/hooks/useLayoutMeasurements.js +175 -131
- package/src/App/hooks/useLayoutMeasurements.test.js +227 -122
- package/src/App/hooks/useMapProviderOverrides.js +114 -35
- package/src/App/hooks/useMapProviderOverrides.test.js +188 -5
- package/src/App/hooks/useModalPanelBehaviour.js +18 -32
- package/src/App/hooks/useModalPanelBehaviour.test.js +45 -33
- package/src/App/hooks/useVisibleGeometry.js +35 -45
- package/src/App/hooks/useVisibleGeometry.test.js +149 -121
- package/src/App/layout/Layout.jsx +4 -4
- package/src/App/layout/layout.module.scss +124 -61
- package/src/App/store/AppProvider.jsx +3 -0
- package/src/App/store/MapProvider.jsx +1 -0
- package/src/App/store/MapProvider.test.jsx +25 -0
- package/src/App/store/appDispatchMiddleware.js +18 -10
- package/src/App/store/appDispatchMiddleware.test.js +42 -4
- package/src/App/store/mapActionsMap.js +6 -0
- package/src/App/store/mapActionsMap.test.js +7 -0
- package/src/App/store/mapReducer.js +2 -0
- package/src/App/store/mapReducer.test.js +2 -0
- package/src/InteractiveMap/InteractiveMap.js +3 -2
- package/src/InteractiveMap/InteractiveMap.test.js +2 -2
- package/src/config/events.js +1 -1
- package/src/config/mapPanelSlots.js +8 -0
- package/src/config/symbolConfig.js +1 -1
- package/src/services/symbolRegistry.js +1 -1
- package/src/services/symbolRegistry.test.js +19 -0
- package/src/types.js +29 -8
- package/src/utils/getFitMove.js +73 -0
- package/src/utils/getFitMove.test.js +114 -0
- package/src/utils/getSafeZoneInset.js +122 -77
- package/src/utils/getSafeZoneInset.test.js +241 -54
- package/src/utils/getValueForStyle.js +10 -5
- package/src/utils/getValueForStyle.test.js +13 -2
- package/src/utils/renderedBoxes.js +25 -0
- package/src/utils/renderedBoxes.test.js +61 -0
- package/src/utils/screenBox.js +70 -0
- package/src/utils/screenBox.test.js +69 -0
- package/src/utils/symbolUtils.js +9 -9
- package/src/utils/symbolUtils.test.js +21 -0
- package/providers/esri/src/utils/spatial.test.js +0 -58
|
@@ -1,5 +1,10 @@
|
|
|
1
|
+
import { SYMBOL_KEYS } from './symbolKeys.js'
|
|
2
|
+
import { isPlainObject } from './isPlainObject.js'
|
|
3
|
+
|
|
1
4
|
const STYLE_PROPS = new Set(['stroke', 'fill', 'strokeWidth'])
|
|
2
5
|
|
|
6
|
+
const capitalise = (id) => id.charAt(0).toUpperCase() + id.slice(1)
|
|
7
|
+
|
|
3
8
|
export const flattenStyleProperties = (props) => {
|
|
4
9
|
if (!props) {
|
|
5
10
|
return {}
|
|
@@ -8,13 +13,13 @@ export const flattenStyleProperties = (props) => {
|
|
|
8
13
|
const result = {}
|
|
9
14
|
|
|
10
15
|
for (const [key, value] of Object.entries(props)) {
|
|
11
|
-
if (STYLE_PROPS.has(key) &&
|
|
16
|
+
if (STYLE_PROPS.has(key) && isPlainObject(value)) {
|
|
12
17
|
const entries = Object.entries(value)
|
|
13
18
|
if (entries.length > 0) {
|
|
14
19
|
result[key] = entries[0][1]
|
|
15
20
|
}
|
|
16
21
|
for (const [styleId, styleValue] of entries) {
|
|
17
|
-
result[`${key}${
|
|
22
|
+
result[`${key}${capitalise(styleId)}`] = styleValue
|
|
18
23
|
}
|
|
19
24
|
} else {
|
|
20
25
|
result[key] = value
|
|
@@ -23,3 +28,50 @@ export const flattenStyleProperties = (props) => {
|
|
|
23
28
|
|
|
24
29
|
return result
|
|
25
30
|
}
|
|
31
|
+
|
|
32
|
+
// Drops keys whose value is undefined — a key left out of a patch must not blank out
|
|
33
|
+
// whatever the feature already has.
|
|
34
|
+
export const definedOnly = (properties = {}) => {
|
|
35
|
+
const result = {}
|
|
36
|
+
for (const [key, value] of Object.entries(properties)) {
|
|
37
|
+
if (value !== undefined) {
|
|
38
|
+
result[key] = value
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return result
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// The IDs of the map styles a per-style value can be for (mapState.mapStyles)
|
|
45
|
+
export const mapStyleIds = (mapState) => mapState?.mapStyles?.map((mapStyle) => mapStyle.id) ?? []
|
|
46
|
+
|
|
47
|
+
// A style property, set through setStyle: stroke/fill/strokeWidth, one of their keys for a
|
|
48
|
+
// known map style (see mapStyleIds), or a symbol-family key. A data property such as
|
|
49
|
+
// fillType never is.
|
|
50
|
+
export const isStyleProperty = (key, styleIds = []) =>
|
|
51
|
+
STYLE_PROPS.has(key) || SYMBOL_KEYS.has(key) ||
|
|
52
|
+
[...STYLE_PROPS].some((prop) => styleIds.some((styleId) => key === `${prop}${capitalise(styleId)}`))
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* The flattened patch for changing a feature's style props. Setting a style prop replaces
|
|
56
|
+
* all of its values: the feature's existing keys for that prop in each map style come back
|
|
57
|
+
* as undefined, meaning remove, so an old strokeOutdoor can't outrank a new single stroke.
|
|
58
|
+
* Only keys for known map styles are removed, so a data property such as fillType is never
|
|
59
|
+
* touched. Props left out or undefined in `changes` are untouched.
|
|
60
|
+
*
|
|
61
|
+
* @param {object} existing - the feature's current (flattened) properties
|
|
62
|
+
* @param {object} changes - stroke/fill/strokeWidth, each a value or a per-style object
|
|
63
|
+
* @param {string[]} styleIds - see mapStyleIds
|
|
64
|
+
* @returns {object}
|
|
65
|
+
*/
|
|
66
|
+
export const toStyleChanges = (existing = {}, changes = {}, styleIds = []) => {
|
|
67
|
+
const defined = definedOnly(changes)
|
|
68
|
+
const removed = {}
|
|
69
|
+
for (const prop of Object.keys(defined)) {
|
|
70
|
+
if (!STYLE_PROPS.has(prop)) { continue }
|
|
71
|
+
for (const styleId of styleIds) {
|
|
72
|
+
const key = `${prop}${capitalise(styleId)}`
|
|
73
|
+
if (key in existing) { removed[key] = undefined } // NOSONAR — undefined means remove (see setStyle in both adapters); null is a real value to keep
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return { ...removed, ...flattenStyleProperties(defined) }
|
|
77
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { flattenStyleProperties } from './flattenStyleProperties.js'
|
|
1
|
+
import { flattenStyleProperties, definedOnly, mapStyleIds, toStyleChanges, isStyleProperty } from './flattenStyleProperties.js'
|
|
2
2
|
|
|
3
3
|
describe('flattenStyleProperties', () => {
|
|
4
4
|
test('returns an empty object for nullish input', () => {
|
|
@@ -31,3 +31,76 @@ describe('flattenStyleProperties', () => {
|
|
|
31
31
|
expect(flattenStyleProperties({ fill: {} })).toEqual({})
|
|
32
32
|
})
|
|
33
33
|
})
|
|
34
|
+
|
|
35
|
+
describe('definedOnly', () => {
|
|
36
|
+
test('drops undefined values and keeps null', () => {
|
|
37
|
+
expect(definedOnly({ a: 1, b: undefined, c: null })).toEqual({ a: 1, c: null })
|
|
38
|
+
expect(definedOnly()).toEqual({})
|
|
39
|
+
})
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
describe('mapStyleIds', () => {
|
|
43
|
+
test('lists the ids of the map styles in map state', () => {
|
|
44
|
+
expect(mapStyleIds({ mapStyles: [{ id: 'outdoor' }, { id: 'dark' }] })).toEqual(['outdoor', 'dark'])
|
|
45
|
+
expect(mapStyleIds({})).toEqual([])
|
|
46
|
+
expect(mapStyleIds(undefined)).toEqual([])
|
|
47
|
+
})
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
describe('toStyleChanges', () => {
|
|
51
|
+
const styleIds = ['outdoor', 'dark']
|
|
52
|
+
const existing = {
|
|
53
|
+
stroke: 'brown',
|
|
54
|
+
strokeOutdoor: 'brown',
|
|
55
|
+
strokeDark: 'tan',
|
|
56
|
+
strokeAerial: 'grey', // a style that isn't configured
|
|
57
|
+
strokeWidth: 4,
|
|
58
|
+
strokeWidthDark: 6,
|
|
59
|
+
fill: 'green',
|
|
60
|
+
fillType: 'arable', // page data
|
|
61
|
+
strokeNote: 'cut in March' // page data
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
test('a single value removes that prop\'s keys for each known map style', () => {
|
|
65
|
+
expect(toStyleChanges(existing, { stroke: 'red' }, styleIds)).toEqual({ strokeOutdoor: undefined, strokeDark: undefined, stroke: 'red' })
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
test('a per-style object replaces the old keys, including styles it leaves out', () => {
|
|
69
|
+
expect(toStyleChanges(existing, { stroke: { outdoor: 'red' } }, styleIds)).toEqual({ strokeOutdoor: 'red', strokeDark: undefined, stroke: 'red' })
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
test('strokeWidth\'s per-style keys belong to strokeWidth, not stroke', () => {
|
|
73
|
+
expect(toStyleChanges(existing, { strokeWidth: 2 }, styleIds)).toEqual({ strokeWidthDark: undefined, strokeWidth: 2 })
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
test('never touches data properties, or keys for a style that isn\'t configured', () => {
|
|
77
|
+
const changes = toStyleChanges(existing, { stroke: 'red', fill: 'blue' }, styleIds)
|
|
78
|
+
expect(changes).not.toHaveProperty('fillType')
|
|
79
|
+
expect(changes).not.toHaveProperty('strokeNote')
|
|
80
|
+
expect(changes).not.toHaveProperty('strokeAerial')
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
test('removes nothing without known map styles', () => {
|
|
84
|
+
expect(toStyleChanges(existing, { stroke: 'red' })).toEqual({ stroke: 'red' })
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
test('props left out, undefined or not style props are passed through', () => {
|
|
88
|
+
expect(toStyleChanges(existing, { fill: undefined }, styleIds)).toEqual({})
|
|
89
|
+
expect(toStyleChanges(existing, { name: 'hedge' }, styleIds)).toEqual({ name: 'hedge' })
|
|
90
|
+
expect(toStyleChanges(undefined, undefined, undefined)).toEqual({})
|
|
91
|
+
})
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
describe('isStyleProperty', () => {
|
|
95
|
+
test('is true for stroke/fill/strokeWidth, their keys for a known map style, and symbol-family keys', () => {
|
|
96
|
+
const styleIds = ['outdoor', 'dark']
|
|
97
|
+
const styleKeys = ['stroke', 'fill', 'strokeWidth', 'strokeDark', 'fillOutdoor', 'strokeWidthDark', 'symbol', 'symbolBackgroundColor']
|
|
98
|
+
expect(styleKeys.every((key) => isStyleProperty(key, styleIds))).toBe(true)
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
test('is false for data properties and keys for a style that isn\'t configured', () => {
|
|
102
|
+
const styleIds = ['outdoor', 'dark']
|
|
103
|
+
expect(['fillType', 'strokeNote', 'strokeAerial', 'name', 'symbolImageId'].some((key) => isStyleProperty(key, styleIds))).toBe(false)
|
|
104
|
+
expect(isStyleProperty('strokeDark')).toBe(false)
|
|
105
|
+
})
|
|
106
|
+
})
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns true for an object literal or Object.create(null). An array, Map, Set, Date, URL or
|
|
3
|
+
* class instance is an object too, but not a plain one.
|
|
4
|
+
*
|
|
5
|
+
* @param {*} value
|
|
6
|
+
* @returns {boolean}
|
|
7
|
+
*/
|
|
8
|
+
export const isPlainObject = (value) => {
|
|
9
|
+
if (value === null || typeof value !== 'object') {
|
|
10
|
+
return false
|
|
11
|
+
}
|
|
12
|
+
const prototype = Object.getPrototypeOf(value)
|
|
13
|
+
return prototype === null || prototype === Object.prototype
|
|
14
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { isPlainObject } from './isPlainObject.js'
|
|
2
|
+
|
|
3
|
+
describe('isPlainObject', () => {
|
|
4
|
+
test('is true for an object literal and Object.create(null)', () => {
|
|
5
|
+
expect(isPlainObject({})).toBe(true)
|
|
6
|
+
expect(isPlainObject({ name: 'North field' })).toBe(true)
|
|
7
|
+
expect(isPlainObject(Object.create(null))).toBe(true)
|
|
8
|
+
})
|
|
9
|
+
|
|
10
|
+
test('is false for other objects', () => {
|
|
11
|
+
class Properties {}
|
|
12
|
+
const others = [[], new Map(), new Set(), new Date(), new URL('http://localhost'), new Properties()]
|
|
13
|
+
for (const other of others) {
|
|
14
|
+
expect(isPlainObject(other)).toBe(false)
|
|
15
|
+
}
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
test('is false for null and primitives', () => {
|
|
19
|
+
const primitives = [null, undefined, 'text', 3, true]
|
|
20
|
+
for (const primitive of primitives) {
|
|
21
|
+
expect(isPlainObject(primitive)).toBe(false)
|
|
22
|
+
}
|
|
23
|
+
})
|
|
24
|
+
})
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { logger } from '../../../../src/services/logger.js'
|
|
2
|
+
import { definedOnly, isStyleProperty } from './flattenStyleProperties.js'
|
|
3
|
+
import { isInternalProperty } from './stripInternalProperties.js'
|
|
4
|
+
import { isPlainObject } from './isPlainObject.js'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Shallow-merges data properties into a feature in draw's store, with the new values
|
|
8
|
+
* winning. A property whose value is undefined is left out; null is kept, so a value can
|
|
9
|
+
* be cleared. Draw's internal properties, and style properties (set through setStyle,
|
|
10
|
+
* which restyles the shape), are refused with a warning. Properties that aren't a plain
|
|
11
|
+
* object, such as an array or a Map, are refused whole with a warning.
|
|
12
|
+
*
|
|
13
|
+
* @param {object} draw - the draw adapter
|
|
14
|
+
* @param {string} featureId
|
|
15
|
+
* @param {object} properties
|
|
16
|
+
* @param {object} options
|
|
17
|
+
* @param {string} options.caller - named in the warning, e.g. 'setProperties'
|
|
18
|
+
* @param {string[]} [options.styleIds] - the known map style ids, see mapStyleIds
|
|
19
|
+
* @returns {object} the properties that were applied
|
|
20
|
+
*/
|
|
21
|
+
export const mergeFeatureProperties = (draw, featureId, properties, { caller, styleIds = [] }) => {
|
|
22
|
+
if (properties == null) {
|
|
23
|
+
return {}
|
|
24
|
+
}
|
|
25
|
+
if (!isPlainObject(properties)) {
|
|
26
|
+
logger.warn(`${caller}: ignoring properties that aren't a plain object`, properties)
|
|
27
|
+
return {}
|
|
28
|
+
}
|
|
29
|
+
const applied = {}
|
|
30
|
+
const refused = []
|
|
31
|
+
for (const [key, value] of Object.entries(definedOnly(properties))) {
|
|
32
|
+
if (isInternalProperty(key) || isStyleProperty(key, styleIds)) {
|
|
33
|
+
refused.push(key)
|
|
34
|
+
} else {
|
|
35
|
+
applied[key] = value
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
if (refused.length) {
|
|
39
|
+
logger.warn(`${caller}: ignoring internal and style properties — use setStyle for style`, refused)
|
|
40
|
+
}
|
|
41
|
+
if (Object.keys(applied).length) {
|
|
42
|
+
draw.setProperties(featureId, applied)
|
|
43
|
+
}
|
|
44
|
+
return applied
|
|
45
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { mergeFeatureProperties } from './mergeFeatureProperties.js'
|
|
2
|
+
import { logger } from '../../../../src/services/logger.js'
|
|
3
|
+
|
|
4
|
+
jest.mock('../../../../src/services/logger.js', () => ({
|
|
5
|
+
logger: { warn: jest.fn(), error: jest.fn() }
|
|
6
|
+
}))
|
|
7
|
+
|
|
8
|
+
beforeEach(() => jest.clearAllMocks())
|
|
9
|
+
|
|
10
|
+
describe('mergeFeatureProperties', () => {
|
|
11
|
+
test('writes the properties to the feature, leaving out undefined and keeping null', () => {
|
|
12
|
+
const draw = { setProperties: jest.fn() }
|
|
13
|
+
const applied = mergeFeatureProperties(draw, 'a', { name: 'North field', ref: null, note: undefined }, { caller: 'setProperties' })
|
|
14
|
+
expect(draw.setProperties).toHaveBeenCalledWith('a', { name: 'North field', ref: null })
|
|
15
|
+
expect(applied).toEqual({ name: 'North field', ref: null })
|
|
16
|
+
expect(logger.warn).not.toHaveBeenCalled()
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
test('refuses internal and style properties with a warning, applying the rest', () => {
|
|
20
|
+
const draw = { setProperties: jest.fn() }
|
|
21
|
+
const applied = mergeFeatureProperties(draw, 'a', {
|
|
22
|
+
name: 'x', fillType: 'arable', sortKey: 3, symbolImageId: 'img', stroke: 'red', strokeDark: 'tan', strokeWidth: 2, symbol: 'pin'
|
|
23
|
+
}, { caller: 'onGeometryFinish', styleIds: ['outdoor', 'dark'] })
|
|
24
|
+
expect(applied).toEqual({ name: 'x', fillType: 'arable' })
|
|
25
|
+
expect(draw.setProperties).toHaveBeenCalledWith('a', { name: 'x', fillType: 'arable' })
|
|
26
|
+
expect(logger.warn).toHaveBeenCalledWith(
|
|
27
|
+
'onGeometryFinish: ignoring internal and style properties — use setStyle for style',
|
|
28
|
+
['sortKey', 'symbolImageId', 'stroke', 'strokeDark', 'strokeWidth', 'symbol']
|
|
29
|
+
)
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
test('writes nothing when no property is left to apply', () => {
|
|
33
|
+
const draw = { setProperties: jest.fn() }
|
|
34
|
+
expect(mergeFeatureProperties(draw, 'a', { stroke: 'red' }, { caller: 'setProperties' })).toEqual({})
|
|
35
|
+
expect(mergeFeatureProperties(draw, 'a', undefined, { caller: 'setProperties' })).toEqual({})
|
|
36
|
+
expect(draw.setProperties).not.toHaveBeenCalled()
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
test('refuses properties that aren\'t a plain object with a warning', () => {
|
|
40
|
+
const draw = { setProperties: jest.fn() }
|
|
41
|
+
const notPlain = [['North field'], new Map([['name', 'North field']]), new Date()]
|
|
42
|
+
for (const properties of notPlain) {
|
|
43
|
+
expect(mergeFeatureProperties(draw, 'a', properties, { caller: 'setProperties' })).toEqual({})
|
|
44
|
+
expect(logger.warn).toHaveBeenLastCalledWith('setProperties: ignoring properties that aren\'t a plain object', properties)
|
|
45
|
+
}
|
|
46
|
+
expect(draw.setProperties).not.toHaveBeenCalled()
|
|
47
|
+
})
|
|
48
|
+
})
|
|
@@ -4,14 +4,15 @@ import { SYMBOL_KEYS } from './symbolKeys.js'
|
|
|
4
4
|
// symbol* keys not in SYMBOL_KEYS (e.g. symbolImageId, symbolPixelRatio), and `sortKey`
|
|
5
5
|
// (drives fill/line/symbol-sort-key render order — see MaplibreDrawAdapter.js — never
|
|
6
6
|
// caller-supplied or meaningful application data).
|
|
7
|
+
export const isInternalProperty = (key) => key === 'sortKey' || (key.startsWith('symbol') && !SYMBOL_KEYS.has(key))
|
|
8
|
+
|
|
7
9
|
export const stripInternalProperties = (feature) => {
|
|
8
10
|
if (!feature?.properties) {
|
|
9
11
|
return feature
|
|
10
12
|
}
|
|
11
13
|
const properties = {}
|
|
12
14
|
for (const [key, value] of Object.entries(feature.properties)) {
|
|
13
|
-
|
|
14
|
-
if (!isInternal) {
|
|
15
|
+
if (!isInternalProperty(key)) {
|
|
15
16
|
properties[key] = value
|
|
16
17
|
}
|
|
17
18
|
}
|
|
@@ -3,7 +3,7 @@ import { COMMIT_RULES } from './rules.js'
|
|
|
3
3
|
import { logger } from '../../../../src/services/logger.js'
|
|
4
4
|
import { stripInternalProperties } from '../utils/stripInternalProperties.js'
|
|
5
5
|
|
|
6
|
-
const IDLE = { revision: 0, status: 'idle', result: null, feature: null, context: null, slow: false, afterPending: false }
|
|
6
|
+
const IDLE = { revision: 0, status: 'idle', result: null, feature: null, context: null, slow: false, afterPending: false, lastResultStatus: null }
|
|
7
7
|
|
|
8
8
|
// How long an async check runs before it counts as slow: Done reads "Checking…" and the
|
|
9
9
|
// check is announced. A quicker reply settles without either.
|
|
@@ -13,9 +13,21 @@ const isThenable = (value) => typeof value?.then === 'function'
|
|
|
13
13
|
|
|
14
14
|
const statusFor = (result) => (result.valid ? 'valid' : 'invalid')
|
|
15
15
|
|
|
16
|
+
const BLOCKING_STATUSES = new Set(['invalid', 'error'])
|
|
17
|
+
|
|
18
|
+
// Whether the shape can be finished (the Done button and the map's finish gestures). While a
|
|
19
|
+
// check is pending, Done keeps the state from the last callback result, so a shape that
|
|
20
|
+
// failed can't be finished in the gap before the next result. With no earlier result, it can
|
|
21
|
+
// — except an existing feature opened by editFeature, which may already be invalid, so it
|
|
22
|
+
// waits for its 'edit-start' check rather than showing Done enabled and then disabled.
|
|
23
|
+
export const canFinishCheck = ({ status, lastResultStatus, context }) =>
|
|
24
|
+
status === 'valid' ||
|
|
25
|
+
(status === 'pending' && context?.phase !== 'edit-start' && !BLOCKING_STATUSES.has(lastResultStatus))
|
|
26
|
+
|
|
16
27
|
/**
|
|
17
28
|
* Runs the commit-level check for each committed change: the built-in COMMIT_RULES first,
|
|
18
|
-
* synchronously, then the consumer's
|
|
29
|
+
* synchronously, then the consumer's callback, which may return a Promise: `onGeometryChange`,
|
|
30
|
+
* or `onGeometryFinish` when a shape is finished.
|
|
19
31
|
*
|
|
20
32
|
* Every run gets a new revision and aborts the previous run's signal. A result only
|
|
21
33
|
* settles if its revision is still the latest, so a slow reply for an older shape can
|
|
@@ -24,6 +36,11 @@ const statusFor = (result) => (result.valid ? 'valid' : 'invalid')
|
|
|
24
36
|
* with `slow: true` once it has run for SLOW_CHECK_MS. A result that follows a slow
|
|
25
37
|
* 'pending' state for the same revision has `afterPending: true`.
|
|
26
38
|
*
|
|
39
|
+
* Every state carries `lastResultStatus`: the status of the last result from the consumer's
|
|
40
|
+
* callback (onGeometryChange, or onGeometryFinish, which runs through here too), whatever its
|
|
41
|
+
* revision. The built-in rules' results don't count, so a shape that has just reached its
|
|
42
|
+
* minimum vertices isn't held back by them. A superseded check never settles, so never counts.
|
|
43
|
+
*
|
|
27
44
|
* A finished shape waiting on the latest check can be held (`hold`), and is released when
|
|
28
45
|
* that check settles or the session ends — `reset` returns its id, so it can be discarded.
|
|
29
46
|
*
|
|
@@ -49,8 +66,9 @@ export const createCommitChecks = () => {
|
|
|
49
66
|
clearTimeout(slowTimer)
|
|
50
67
|
}
|
|
51
68
|
|
|
52
|
-
const settle = (rev, next) => {
|
|
69
|
+
const settle = (rev, next, { fromCallback = false } = {}) => {
|
|
53
70
|
if (rev !== revision) { return }
|
|
71
|
+
const lastResultStatus = fromCallback ? next.status : latest.lastResultStatus
|
|
54
72
|
if (next.status !== 'pending') {
|
|
55
73
|
abortController = null
|
|
56
74
|
clearTimeout(slowTimer)
|
|
@@ -58,7 +76,7 @@ export const createCommitChecks = () => {
|
|
|
58
76
|
const slow = !!next.slow
|
|
59
77
|
const afterPending = next.status !== 'pending' && slowRevision === rev
|
|
60
78
|
slowRevision = slow ? rev : null
|
|
61
|
-
latest = { ...next, revision: rev, slow, afterPending }
|
|
79
|
+
latest = { ...next, revision: rev, slow, afterPending, lastResultStatus }
|
|
62
80
|
onSettle?.(latest)
|
|
63
81
|
}
|
|
64
82
|
|
|
@@ -78,17 +96,17 @@ export const createCommitChecks = () => {
|
|
|
78
96
|
* @param {object} params
|
|
79
97
|
* @param {object} params.feature - the committed GeoJSON feature
|
|
80
98
|
* @param {object} params.context - { phase, mode, vertexIndex }
|
|
81
|
-
* @param {Function} [params.
|
|
99
|
+
* @param {Function} [params.consumerCheck] - the consumer's onGeometryChange or onGeometryFinish
|
|
82
100
|
* @param {Array<Function>} [params.rules] - built-in rules, defaults to COMMIT_RULES
|
|
83
101
|
*/
|
|
84
|
-
run ({ feature, context,
|
|
102
|
+
run ({ feature, context, consumerCheck, rules = COMMIT_RULES }) {
|
|
85
103
|
abortPending()
|
|
86
104
|
revision++
|
|
87
105
|
const rev = revision
|
|
88
106
|
const base = { feature, context }
|
|
89
107
|
|
|
90
108
|
const builtIn = validateGeometry(feature, context, { rules })
|
|
91
|
-
if (!builtIn.valid || typeof
|
|
109
|
+
if (!builtIn.valid || typeof consumerCheck !== 'function') {
|
|
92
110
|
settle(rev, { ...base, status: statusFor(builtIn), result: builtIn })
|
|
93
111
|
return
|
|
94
112
|
}
|
|
@@ -97,13 +115,13 @@ export const createCommitChecks = () => {
|
|
|
97
115
|
const onError = (error) => {
|
|
98
116
|
// A superseded check rejecting (e.g. on its aborted signal) isn't a failure
|
|
99
117
|
if (rev !== revision) { return }
|
|
100
|
-
logger.warn(
|
|
101
|
-
settle(rev, { ...base, status: 'error', result: { valid: false, reason: null, source: 'callback' }, error })
|
|
118
|
+
logger.warn(`[draw] geometry check failed (${context?.phase})`, error)
|
|
119
|
+
settle(rev, { ...base, status: 'error', result: { valid: false, reason: null, source: 'callback' }, error }, { fromCallback: true })
|
|
102
120
|
}
|
|
103
121
|
|
|
104
122
|
let output
|
|
105
123
|
try {
|
|
106
|
-
output =
|
|
124
|
+
output = consumerCheck({ feature, ...context }, { signal: abortController.signal, revision: rev })
|
|
107
125
|
} catch (error) {
|
|
108
126
|
onError(error)
|
|
109
127
|
return
|
|
@@ -111,7 +129,7 @@ export const createCommitChecks = () => {
|
|
|
111
129
|
|
|
112
130
|
const settleResult = (value) => {
|
|
113
131
|
const result = withSource(normaliseResult(value), 'callback')
|
|
114
|
-
settle(rev, { ...base, status: statusFor(result), result })
|
|
132
|
+
settle(rev, { ...base, status: statusFor(result), result }, { fromCallback: true })
|
|
115
133
|
}
|
|
116
134
|
|
|
117
135
|
if (!isThenable(output)) { settleResult(output); return }
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { createCommitChecks, getCommitChecks, isSameGeometry, startCheckSession, SLOW_CHECK_MS } from './commitChecks.js'
|
|
1
|
+
import { createCommitChecks, getCommitChecks, isSameGeometry, startCheckSession, canFinishCheck, SLOW_CHECK_MS } from './commitChecks.js'
|
|
2
2
|
|
|
3
3
|
const square = { type: 'Feature', geometry: { type: 'Polygon', coordinates: [[[0, 0], [0, 1], [1, 1], [1, 0], [0, 0]]] } }
|
|
4
4
|
const context = { phase: 'change', mode: 'draw_polygon', vertexIndex: 3 }
|
|
@@ -20,13 +20,13 @@ describe('createCommitChecks', () => {
|
|
|
20
20
|
test('passes the event and a { signal, revision } second argument to the callback', () => {
|
|
21
21
|
const checks = createCommitChecks()
|
|
22
22
|
const onGeometryChange = jest.fn(() => true)
|
|
23
|
-
checks.run({ feature: square, context, onGeometryChange })
|
|
23
|
+
checks.run({ feature: square, context, consumerCheck: onGeometryChange })
|
|
24
24
|
expect(onGeometryChange).toHaveBeenCalledWith({ feature: square, ...context }, { signal: expect.any(AbortSignal), revision: 1 })
|
|
25
25
|
})
|
|
26
26
|
|
|
27
27
|
test('normalises a synchronous result', () => {
|
|
28
28
|
const checks = createCommitChecks()
|
|
29
|
-
checks.run({ feature: square, context,
|
|
29
|
+
checks.run({ feature: square, context, consumerCheck: () => ({ valid: false, reason: 'too big' }) })
|
|
30
30
|
expect(checks.state).toEqual(expect.objectContaining({ status: 'invalid', result: { valid: false, reason: 'too big', source: 'callback' } }))
|
|
31
31
|
})
|
|
32
32
|
|
|
@@ -36,7 +36,7 @@ describe('createCommitChecks', () => {
|
|
|
36
36
|
const onSettle = jest.fn()
|
|
37
37
|
checks.setOnSettle(onSettle)
|
|
38
38
|
let resolve
|
|
39
|
-
checks.run({ feature: square, context,
|
|
39
|
+
checks.run({ feature: square, context, consumerCheck: () => new Promise((_resolve) => { resolve = _resolve }) })
|
|
40
40
|
expect(checks.state).toEqual(expect.objectContaining({ status: 'pending', slow: false }))
|
|
41
41
|
|
|
42
42
|
jest.advanceTimersByTime(SLOW_CHECK_MS)
|
|
@@ -48,7 +48,7 @@ describe('createCommitChecks', () => {
|
|
|
48
48
|
expect(onSettle.mock.calls.map(([state]) => [state.status, state.slow])).toEqual([['pending', false], ['pending', true], ['invalid', false]])
|
|
49
49
|
|
|
50
50
|
// The next check settles straight away, so it doesn't follow a pending state
|
|
51
|
-
checks.run({ feature: square, context,
|
|
51
|
+
checks.run({ feature: square, context, consumerCheck: () => true })
|
|
52
52
|
expect(checks.state.afterPending).toBe(false)
|
|
53
53
|
jest.useRealTimers()
|
|
54
54
|
})
|
|
@@ -58,7 +58,7 @@ describe('createCommitChecks', () => {
|
|
|
58
58
|
const checks = createCommitChecks()
|
|
59
59
|
const onSettle = jest.fn()
|
|
60
60
|
checks.setOnSettle(onSettle)
|
|
61
|
-
checks.run({ feature: square, context,
|
|
61
|
+
checks.run({ feature: square, context, consumerCheck: () => Promise.resolve(true) })
|
|
62
62
|
await flush()
|
|
63
63
|
jest.advanceTimersByTime(SLOW_CHECK_MS)
|
|
64
64
|
expect(checks.state).toEqual(expect.objectContaining({ status: 'valid', afterPending: false }))
|
|
@@ -73,16 +73,25 @@ describe('createCommitChecks', () => {
|
|
|
73
73
|
expect(checks.state.result.source).toBe('rules')
|
|
74
74
|
|
|
75
75
|
jest.spyOn(console, 'warn').mockImplementation(() => {})
|
|
76
|
-
checks.run({ feature: square, context,
|
|
76
|
+
checks.run({ feature: square, context, consumerCheck: () => { throw new Error('503') } })
|
|
77
77
|
expect(checks.state).toEqual(expect.objectContaining({ status: 'error', result: { valid: false, reason: null, source: 'callback' } }))
|
|
78
78
|
console.warn.mockRestore()
|
|
79
79
|
})
|
|
80
80
|
|
|
81
|
+
test('logs a callback error with the phase, so it names the finishing check too', () => {
|
|
82
|
+
const checks = createCommitChecks()
|
|
83
|
+
jest.spyOn(console, 'warn').mockImplementation(() => {})
|
|
84
|
+
const error = new Error('503')
|
|
85
|
+
checks.run({ feature: square, context: { phase: 'edit-finish' }, consumerCheck: () => { throw error } })
|
|
86
|
+
expect(console.warn).toHaveBeenCalledWith('[interactive-map]', '[draw] geometry check failed (edit-finish)', error)
|
|
87
|
+
console.warn.mockRestore()
|
|
88
|
+
})
|
|
89
|
+
|
|
81
90
|
test('reset aborts the check in flight, drops its reply and returns to idle', async () => {
|
|
82
91
|
const checks = createCommitChecks()
|
|
83
92
|
let resolve
|
|
84
93
|
let signal
|
|
85
|
-
checks.run({ feature: square, context,
|
|
94
|
+
checks.run({ feature: square, context, consumerCheck: (_event, ctx) => { signal = ctx.signal; return new Promise((_resolve) => { resolve = _resolve }) } })
|
|
86
95
|
checks.reset()
|
|
87
96
|
expect(signal.aborted).toBe(true)
|
|
88
97
|
|
|
@@ -97,8 +106,8 @@ describe('createCommitChecks', () => {
|
|
|
97
106
|
const onGeometryChange = (_event, { signal }) => new Promise((_resolve, reject) => {
|
|
98
107
|
signal.addEventListener('abort', () => reject(new Error('aborted')))
|
|
99
108
|
})
|
|
100
|
-
checks.run({ feature: square, context, onGeometryChange })
|
|
101
|
-
checks.run({ feature: square, context,
|
|
109
|
+
checks.run({ feature: square, context, consumerCheck: onGeometryChange })
|
|
110
|
+
checks.run({ feature: square, context, consumerCheck: () => true })
|
|
102
111
|
await flush()
|
|
103
112
|
expect(checks.state.status).toBe('valid')
|
|
104
113
|
expect(warn).not.toHaveBeenCalled()
|
|
@@ -122,12 +131,59 @@ describe('createCommitChecks', () => {
|
|
|
122
131
|
expect(checks.reset()).toBe('c')
|
|
123
132
|
})
|
|
124
133
|
|
|
134
|
+
test('carries the last callback result into later states, ignoring the built-in rules', () => {
|
|
135
|
+
const checks = createCommitChecks()
|
|
136
|
+
const partDrawn = { type: 'Feature', geometry: { type: 'Polygon', coordinates: [[[0, 0], [1, 0], [0, 0]]] } }
|
|
137
|
+
checks.run({ feature: square, context, consumerCheck: () => false })
|
|
138
|
+
expect(checks.state.lastResultStatus).toBe('invalid')
|
|
139
|
+
|
|
140
|
+
checks.run({ feature: partDrawn, context, consumerCheck: () => true })
|
|
141
|
+
expect(checks.state).toEqual(expect.objectContaining({ status: 'invalid', lastResultStatus: 'invalid' }))
|
|
142
|
+
|
|
143
|
+
checks.run({ feature: square, context, consumerCheck: () => new Promise(() => {}) })
|
|
144
|
+
checks.run({ feature: square, context, consumerCheck: () => new Promise(() => {}) })
|
|
145
|
+
expect(checks.state).toEqual(expect.objectContaining({ status: 'pending', lastResultStatus: 'invalid' }))
|
|
146
|
+
|
|
147
|
+
checks.reset()
|
|
148
|
+
expect(checks.state.lastResultStatus).toBeNull()
|
|
149
|
+
})
|
|
150
|
+
|
|
151
|
+
test('counts a callback error as its last result', () => {
|
|
152
|
+
const checks = createCommitChecks()
|
|
153
|
+
jest.spyOn(console, 'warn').mockImplementation(() => {})
|
|
154
|
+
checks.run({ feature: square, context, consumerCheck: () => { throw new Error('503') } })
|
|
155
|
+
expect(checks.state.lastResultStatus).toBe('error')
|
|
156
|
+
console.warn.mockRestore()
|
|
157
|
+
})
|
|
158
|
+
|
|
125
159
|
test('works without an onSettle callback', () => {
|
|
126
160
|
const checks = createCommitChecks()
|
|
127
161
|
expect(() => checks.run({ feature: square, context })).not.toThrow()
|
|
128
162
|
})
|
|
129
163
|
})
|
|
130
164
|
|
|
165
|
+
describe('canFinishCheck', () => {
|
|
166
|
+
test('is true for a passing check, and false for a failed one or none', () => {
|
|
167
|
+
expect(canFinishCheck({ status: 'valid' })).toBe(true)
|
|
168
|
+
expect(canFinishCheck({ status: 'invalid' })).toBe(false)
|
|
169
|
+
expect(canFinishCheck({ status: 'error' })).toBe(false)
|
|
170
|
+
expect(canFinishCheck({ status: 'idle' })).toBe(false)
|
|
171
|
+
})
|
|
172
|
+
|
|
173
|
+
test('keeps the last callback result while a check is pending', () => {
|
|
174
|
+
expect(canFinishCheck({ status: 'pending', lastResultStatus: null })).toBe(true)
|
|
175
|
+
expect(canFinishCheck({ status: 'pending', lastResultStatus: 'valid' })).toBe(true)
|
|
176
|
+
expect(canFinishCheck({ status: 'pending', lastResultStatus: 'invalid' })).toBe(false)
|
|
177
|
+
expect(canFinishCheck({ status: 'pending', lastResultStatus: 'error' })).toBe(false)
|
|
178
|
+
})
|
|
179
|
+
|
|
180
|
+
test('is false while an existing feature’s edit-start check is pending', () => {
|
|
181
|
+
expect(canFinishCheck({ status: 'pending', lastResultStatus: null, context: { phase: 'edit-start' } })).toBe(false)
|
|
182
|
+
expect(canFinishCheck({ status: 'valid', lastResultStatus: 'valid', context: { phase: 'edit-start' } })).toBe(true)
|
|
183
|
+
expect(canFinishCheck({ status: 'pending', lastResultStatus: null, context: { phase: 'change' } })).toBe(true)
|
|
184
|
+
})
|
|
185
|
+
})
|
|
186
|
+
|
|
131
187
|
describe('getCommitChecks', () => {
|
|
132
188
|
test('keeps one controller per draw adapter', () => {
|
|
133
189
|
const drawA = {}
|
|
@@ -188,7 +244,7 @@ describe('startCheckSession', () => {
|
|
|
188
244
|
const draw = { get: jest.fn(() => shape), delete: jest.fn() }
|
|
189
245
|
const eventBus = { emit: jest.fn() }
|
|
190
246
|
const checks = getCommitChecks(draw)
|
|
191
|
-
checks.run({ feature: shape, context: { phase: 'change' },
|
|
247
|
+
checks.run({ feature: shape, context: { phase: 'change' }, consumerCheck: () => new Promise(() => {}) })
|
|
192
248
|
checks.hold('held')
|
|
193
249
|
expect(checks.heldId).toBe('held')
|
|
194
250
|
|
|
@@ -1,23 +1,30 @@
|
|
|
1
1
|
import { COMMIT_RULES, PLACE_RULES, PREVIEW_RULES, MIN_VERTICES } from './rules.js'
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
* Normalise a rule / callback result into `{ valid, reason, announcement?, recheckOnFinish? }`.
|
|
4
|
+
* Normalise a rule / callback result into `{ valid, reason, announcement?, recheckOnFinish?, reopen?, properties? }`.
|
|
5
5
|
*
|
|
6
6
|
* Supports three return shapes so a user `onGeometryPreview`/`onGeometryChange` callback can stay
|
|
7
7
|
* terse:
|
|
8
8
|
* - `true` / `undefined` → valid
|
|
9
9
|
* - `false` → invalid (no reason)
|
|
10
|
-
* - `{ valid, reason, announcement, recheckOnFinish }` → passed through
|
|
10
|
+
* - `{ valid, reason, announcement, recheckOnFinish, reopen, properties }` → passed through
|
|
11
11
|
*/
|
|
12
12
|
export const normaliseResult = (result) => {
|
|
13
13
|
if (result === undefined || result === true) { return { valid: true } }
|
|
14
14
|
if (result === false) { return { valid: false, reason: null } }
|
|
15
|
-
const { valid, reason, announcement, recheckOnFinish } = result
|
|
15
|
+
const { valid, reason, announcement, recheckOnFinish, reopen, properties } = result
|
|
16
|
+
const isValid = valid !== false
|
|
16
17
|
return {
|
|
17
|
-
valid:
|
|
18
|
+
valid: isValid,
|
|
18
19
|
reason,
|
|
19
20
|
...(announcement && { announcement }),
|
|
20
|
-
...(recheckOnFinish && { recheckOnFinish: true })
|
|
21
|
+
...(recheckOnFinish && { recheckOnFinish: true }),
|
|
22
|
+
// Only a passing result can ask to reopen: a failing new shape reopens anyway. Acted on
|
|
23
|
+
// only when a new shape is finished (see createFinishHandlers in events.js).
|
|
24
|
+
...(isValid && reopen && { reopen: true }),
|
|
25
|
+
// Only a passing result can add properties: an invalid shape isn't finished. Their shape
|
|
26
|
+
// is checked when they're merged (see mergeFeatureProperties), which warns if it's wrong.
|
|
27
|
+
...(isValid && properties != null && { properties })
|
|
21
28
|
}
|
|
22
29
|
}
|
|
23
30
|
|
|
@@ -211,6 +211,24 @@ describe('normaliseResult', () => {
|
|
|
211
211
|
.toEqual({ valid: true, reason: undefined, announcement: 'Area 2 hectares', recheckOnFinish: true })
|
|
212
212
|
expect(normaliseResult({ valid: false, reason: 'too big' })).not.toHaveProperty('announcement')
|
|
213
213
|
})
|
|
214
|
+
|
|
215
|
+
test('keeps properties on a passing result only', () => {
|
|
216
|
+
const properties = { drawRules: { checked: false } }
|
|
217
|
+
expect(normaliseResult({ valid: true, properties })).toEqual({ valid: true, reason: undefined, properties })
|
|
218
|
+
expect(normaliseResult({ properties })).toHaveProperty('properties', properties)
|
|
219
|
+
expect(normaliseResult({ valid: false, reason: 'overlap', properties })).not.toHaveProperty('properties')
|
|
220
|
+
expect(normaliseResult({ valid: true, properties: null })).not.toHaveProperty('properties')
|
|
221
|
+
})
|
|
222
|
+
|
|
223
|
+
test('keeps reopen on a passing result only', () => {
|
|
224
|
+
expect(normaliseResult({ valid: true, reopen: true })).toEqual({ valid: true, reason: undefined, reopen: true })
|
|
225
|
+
expect(normaliseResult({ valid: false, reason: 'overlap', reopen: true })).not.toHaveProperty('reopen')
|
|
226
|
+
expect(normaliseResult({ valid: true, reopen: false })).not.toHaveProperty('reopen')
|
|
227
|
+
})
|
|
228
|
+
|
|
229
|
+
test('passes properties of the wrong shape on, for mergeFeatureProperties to refuse with a warning', () => {
|
|
230
|
+
expect(normaliseResult({ valid: true, properties: ['a'] })).toHaveProperty('properties', ['a'])
|
|
231
|
+
})
|
|
214
232
|
})
|
|
215
233
|
|
|
216
234
|
describe('toPlacementBlocked', () => {
|