@defra/interactive-map 0.0.57-alpha → 0.0.59-alpha
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/esm/im-core.js +1 -1
- package/dist/umd/im-core.js +1 -1
- package/docs/assets/css/docusaurus.scss +7 -1
- package/docs/plugins/draw.md +317 -186
- package/package.json +1 -1
- 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 +15 -9
- 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/createNewShape.js +2 -2
- package/plugins/draw/src/api/editFeature.js +1 -1
- package/plugins/draw/src/api/newPoint.js +2 -2
- package/plugins/draw/src/api/newPoint.test.js +8 -0
- package/plugins/draw/src/api/newPolygon.test.js +8 -0
- 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 +10 -4
- package/plugins/draw/src/api/split.test.js +30 -2
- package/plugins/draw/src/events.js +110 -64
- package/plugins/draw/src/events.test.js +494 -4
- 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 +32 -12
- package/plugins/draw/src/validation/commitChecks.test.js +73 -13
- package/plugins/draw/src/validation/validateGeometry.js +12 -5
- package/plugins/draw/src/validation/validateGeometry.test.js +18 -0
- package/src/App/store/MapProvider.jsx +1 -0
- package/src/App/store/MapProvider.test.jsx +25 -0
- 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
|
@@ -13,6 +13,9 @@ const initialState = {
|
|
|
13
13
|
// else should set them.
|
|
14
14
|
// Status of the latest check: 'idle' | 'pending' | 'valid' | 'invalid' | 'error'
|
|
15
15
|
validationStatus: 'idle',
|
|
16
|
+
// Whether the shape can be finished: the latest check passed, or it's pending and the last
|
|
17
|
+
// callback result didn't fail (canFinishCheck)
|
|
18
|
+
canFinish: false,
|
|
16
19
|
// Whether a pending check has run long enough to show as "Checking…" (SLOW_CHECK_MS)
|
|
17
20
|
checkSlow: false,
|
|
18
21
|
// A finished shape is waiting on its pending check before it's created
|
|
@@ -32,6 +35,7 @@ const setMode = (state, payload) => ({
|
|
|
32
35
|
// A new/empty shape has no passing check yet; its first commit (or edit mode's
|
|
33
36
|
// edit-start check in api/editFeature) settles it.
|
|
34
37
|
validationStatus: 'idle',
|
|
38
|
+
canFinish: false,
|
|
35
39
|
checkSlow: false,
|
|
36
40
|
holding: false,
|
|
37
41
|
previewValid: true,
|
|
@@ -65,6 +69,8 @@ const setUndoStackLength = (state, payload) => ({ ...state, undoStackLength: pay
|
|
|
65
69
|
|
|
66
70
|
const setValidationStatus = (state, payload) => ({ ...state, validationStatus: payload, checkSlow: false })
|
|
67
71
|
|
|
72
|
+
const setCanFinish = (state, payload) => ({ ...state, canFinish: !!payload })
|
|
73
|
+
|
|
68
74
|
const setCheckSlow = (state) => ({ ...state, checkSlow: true })
|
|
69
75
|
|
|
70
76
|
const setHolding = (state, payload) => ({ ...state, holding: !!payload })
|
|
@@ -82,6 +88,7 @@ const actions = {
|
|
|
82
88
|
SET_HAS_SNAP_LAYERS: setHasSnapLayers,
|
|
83
89
|
SET_UNDO_STACK_LENGTH: setUndoStackLength,
|
|
84
90
|
SET_VALIDATION_STATUS: setValidationStatus,
|
|
91
|
+
SET_CAN_FINISH: setCanFinish,
|
|
85
92
|
SET_CHECK_SLOW: setCheckSlow,
|
|
86
93
|
SET_HOLDING: setHolding,
|
|
87
94
|
SET_PREVIEW_VALID: setPreviewValid,
|
|
@@ -93,6 +93,17 @@ describe('SET_VALIDATION_STATUS', () => {
|
|
|
93
93
|
})
|
|
94
94
|
})
|
|
95
95
|
|
|
96
|
+
describe('SET_CAN_FINISH', () => {
|
|
97
|
+
test('stores whether the shape can be finished', () => {
|
|
98
|
+
expect(actions.SET_CAN_FINISH(initialState, true).canFinish).toBe(true)
|
|
99
|
+
expect(actions.SET_CAN_FINISH({ ...initialState, canFinish: true }, false).canFinish).toBe(false)
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
test('a new mode can\u2019t be finished until its first check', () => {
|
|
103
|
+
expect(actions.SET_MODE({ ...initialState, canFinish: true }, 'draw_polygon').canFinish).toBe(false)
|
|
104
|
+
})
|
|
105
|
+
})
|
|
106
|
+
|
|
96
107
|
describe('SET_HOLDING', () => {
|
|
97
108
|
test('marks a finished shape as waiting on its check', () => {
|
|
98
109
|
expect(actions.SET_HOLDING(initialState, true).holding).toBe(true)
|
|
@@ -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 }
|
|
@@ -165,17 +183,19 @@ export const isSameGeometry = (a, b) => {
|
|
|
165
183
|
}
|
|
166
184
|
|
|
167
185
|
/**
|
|
168
|
-
* Starts a draw/edit session's checks: installs the session's
|
|
186
|
+
* Starts a draw/edit session's checks: installs the session's callbacks on the
|
|
169
187
|
* adapter and ends any previous session's check. A per-call callback replaces the
|
|
170
188
|
* plugin-level one. A shape still waiting on that check is discarded, as Cancel would.
|
|
171
189
|
*
|
|
172
190
|
* - `draw._previewValidator` — onGeometryPreview, read by the adapters for the
|
|
173
191
|
* 'preview' and 'place' phases
|
|
174
192
|
* - `draw._commitValidator` — onGeometryChange, read by events.js for committed changes
|
|
193
|
+
* - `draw._finishValidator` — onGeometryFinish, read by events.js when a shape is finished
|
|
175
194
|
*/
|
|
176
195
|
export const startCheckSession = (draw, options = {}, pluginConfig = {}, eventBus = null) => {
|
|
177
196
|
draw._previewValidator = options.onGeometryPreview ?? pluginConfig.onGeometryPreview
|
|
178
197
|
draw._commitValidator = options.onGeometryChange ?? pluginConfig.onGeometryChange
|
|
198
|
+
draw._finishValidator = options.onGeometryFinish ?? pluginConfig.onGeometryFinish
|
|
179
199
|
const heldId = getCommitChecks(draw).reset()
|
|
180
200
|
if (heldId !== undefined) {
|
|
181
201
|
const discarded = draw.get(heldId)
|