@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
|
@@ -18,10 +18,8 @@ const HALF_SIZE = SIZES.touchTargetSize / 2
|
|
|
18
18
|
*/
|
|
19
19
|
function generateSvgHtml () {
|
|
20
20
|
const size = SIZES.touchTargetSize
|
|
21
|
-
const half = HALF_SIZE
|
|
22
21
|
return `
|
|
23
22
|
<svg width='${size}' height='${size}' viewBox='0 0 48 48' fill-rule='evenodd'
|
|
24
|
-
style='display:none;position:absolute;top:0;left:0;margin:${half}px 0 0 -${half}px;cursor:grab'
|
|
25
23
|
class='im-draw-touch-target' data-im-draw-touch-target>
|
|
26
24
|
<circle cx='24' cy='24' r='24' fill='var(--draw-halo, #000)'/>
|
|
27
25
|
<path d="M37.543 25H34a1 1 0 1 1 0-2h3.629l-.836-.837a1 1 0 0 1 1.414-1.414l2.5 2.501A1 1 0 0 1 41 24a1 1 0 0 1-.487.858l-2.306 2.306a1 1 0 0 1-1.414-1.414l.75-.75zM23 10.414l-.793.793a1 1 0 0 1-1.414-1.414l2.5-2.5C23.481 7.105 23.734 7 24 7s.519.105.707.293l2.5 2.5a1 1 0 0 1-1.414 1.414L25 10.414V14a1 1 0 1 1-2 0v-3.586zM7 24a1 1 0 0 1 .293-.75l2.5-2.501a1 1 0 0 1 1.414 1.414l-.836.837H14a1 1 0 1 1 0 2h-3.543l.75.75a1 1 0 0 1-1.414 1.414l-2.306-2.306A1 1 0 0 1 7 24zm16.293 16.707l-2.5-2.5a1 1 0 0 1 1.414-1.414l.793.793V34a1 1 0 1 1 2 0v3.586l.793-.793a1 1 0 0 1 1.414 1.414l-2.5 2.5c-.188.188-.441.293-.707.293s-.519-.105-.707-.293zM24 20c2.208 0 4 1.792 4 4s-1.792 4-4 4-4-1.792-4-4 1.792-4 4-4z" fill='var(--draw-bg, #fff)'/>
|
|
@@ -35,61 +33,71 @@ function generateSvgHtml () {
|
|
|
35
33
|
* @returns {SVGElement} The touch target element
|
|
36
34
|
*/
|
|
37
35
|
export const createTouchTarget = (container) => {
|
|
38
|
-
let
|
|
39
|
-
if (!
|
|
36
|
+
let element = container.querySelector('[data-im-draw-touch-target]')
|
|
37
|
+
if (!element) {
|
|
40
38
|
container.insertAdjacentHTML('beforeend', generateSvgHtml())
|
|
41
|
-
|
|
39
|
+
element = container.querySelector('[data-im-draw-touch-target]')
|
|
40
|
+
// Set through the element rather than a style attribute, which a Content Security Policy
|
|
41
|
+
// without 'unsafe-inline' blocks
|
|
42
|
+
Object.assign(element.style, {
|
|
43
|
+
display: 'none',
|
|
44
|
+
position: 'absolute',
|
|
45
|
+
top: '0',
|
|
46
|
+
left: '0',
|
|
47
|
+
margin: `${HALF_SIZE}px 0 0 -${HALF_SIZE}px`,
|
|
48
|
+
cursor: 'grab'
|
|
49
|
+
})
|
|
42
50
|
}
|
|
43
|
-
return
|
|
51
|
+
return element
|
|
44
52
|
}
|
|
45
53
|
|
|
46
54
|
/**
|
|
47
55
|
* Apply color scheme to touch target SVG
|
|
48
|
-
* @param {SVGElement}
|
|
56
|
+
* @param {SVGElement} element - Touch target element
|
|
49
57
|
* @param {object} colors - Color object with editActive, editHalo, editVertex properties
|
|
50
58
|
*/
|
|
51
|
-
export const applyTouchTargetColors = (
|
|
52
|
-
if (!
|
|
59
|
+
export const applyTouchTargetColors = (element, colors) => {
|
|
60
|
+
if (!element) {
|
|
53
61
|
return
|
|
54
62
|
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
63
|
+
element.style.setProperty('--draw-halo', colors.editActive)
|
|
64
|
+
element.style.setProperty('--draw-bg', colors.editHalo)
|
|
65
|
+
element.style.setProperty('--draw-primary', colors.editVertex)
|
|
58
66
|
}
|
|
59
67
|
|
|
60
68
|
/**
|
|
61
69
|
* Show touch target at specified pixel position
|
|
62
|
-
* @param {SVGElement}
|
|
70
|
+
* @param {SVGElement} element - Touch target element
|
|
63
71
|
* @param {object} pixel - Position object with x, y properties
|
|
64
72
|
*/
|
|
65
|
-
export const showTouchTarget = (
|
|
66
|
-
if (!pixel || !
|
|
73
|
+
export const showTouchTarget = (element, pixel) => {
|
|
74
|
+
if (!pixel || !element) {
|
|
67
75
|
return
|
|
68
76
|
}
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
77
|
+
element.style.left = `${pixel.x}px`
|
|
78
|
+
element.style.top = `${pixel.y}px`
|
|
79
|
+
element.style.display = 'block'
|
|
72
80
|
}
|
|
73
81
|
|
|
74
82
|
/**
|
|
75
83
|
* Hide touch target
|
|
76
|
-
* @param {SVGElement}
|
|
84
|
+
* @param {SVGElement} element - Touch target element
|
|
77
85
|
*/
|
|
78
|
-
export const hideTouchTarget = (
|
|
79
|
-
if (
|
|
80
|
-
|
|
86
|
+
export const hideTouchTarget = (element) => {
|
|
87
|
+
if (element) {
|
|
88
|
+
element.style.display = 'none'
|
|
81
89
|
}
|
|
82
90
|
}
|
|
83
91
|
|
|
84
92
|
/**
|
|
85
93
|
* Check if event target is part of the touch target SVG
|
|
86
|
-
* @param {HTMLElement}
|
|
94
|
+
* @param {HTMLElement} element - Element to check
|
|
87
95
|
* @returns {boolean} True if element is part of touch target
|
|
88
96
|
*/
|
|
89
|
-
export const isOnTouchTarget = (
|
|
90
|
-
if (!
|
|
97
|
+
export const isOnTouchTarget = (element) => {
|
|
98
|
+
if (!element) {
|
|
91
99
|
return false
|
|
92
100
|
}
|
|
93
|
-
const parent =
|
|
101
|
+
const parent = element.parentNode
|
|
94
102
|
return (parent instanceof globalThis.SVGElement) || (parent?.ownerSVGElement != null)
|
|
95
103
|
}
|
|
@@ -12,13 +12,25 @@ describe('createTouchTarget', () => {
|
|
|
12
12
|
test('inserts the SVG target once and returns it', () => {
|
|
13
13
|
const container = document.createElement('div')
|
|
14
14
|
|
|
15
|
-
const
|
|
15
|
+
const element = createTouchTarget(container)
|
|
16
16
|
|
|
17
|
-
expect(
|
|
18
|
-
expect(
|
|
17
|
+
expect(element).not.toBeNull()
|
|
18
|
+
expect(element.hasAttribute('data-im-draw-touch-target')).toBe(true)
|
|
19
19
|
expect(container.querySelectorAll('[data-im-draw-touch-target]')).toHaveLength(1)
|
|
20
20
|
})
|
|
21
21
|
|
|
22
|
+
test('styles the target through the element, not a style attribute in the markup', () => {
|
|
23
|
+
const container = document.createElement('div')
|
|
24
|
+
const insertAdjacentHTML = jest.spyOn(container, 'insertAdjacentHTML')
|
|
25
|
+
|
|
26
|
+
const element = createTouchTarget(container)
|
|
27
|
+
|
|
28
|
+
expect(insertAdjacentHTML.mock.calls[0][1]).not.toMatch(/style=/)
|
|
29
|
+
expect(element.style.display).toBe('none')
|
|
30
|
+
expect(element.style.position).toBe('absolute')
|
|
31
|
+
expect(element.style.margin).toBe('24px 0px 0px -24px')
|
|
32
|
+
})
|
|
33
|
+
|
|
22
34
|
test('returns the existing target without inserting a second one', () => {
|
|
23
35
|
const container = document.createElement('div')
|
|
24
36
|
const first = createTouchTarget(container)
|
|
@@ -31,13 +43,13 @@ describe('createTouchTarget', () => {
|
|
|
31
43
|
|
|
32
44
|
describe('applyTouchTargetColors', () => {
|
|
33
45
|
test('sets the CSS custom properties from the colours', () => {
|
|
34
|
-
const
|
|
46
|
+
const element = document.createElement('div')
|
|
35
47
|
|
|
36
|
-
applyTouchTargetColors(
|
|
48
|
+
applyTouchTargetColors(element, { editActive: 'a', editHalo: 'h', editVertex: 'v' })
|
|
37
49
|
|
|
38
|
-
expect(
|
|
39
|
-
expect(
|
|
40
|
-
expect(
|
|
50
|
+
expect(element.style.getPropertyValue('--draw-halo')).toBe('a')
|
|
51
|
+
expect(element.style.getPropertyValue('--draw-bg')).toBe('h')
|
|
52
|
+
expect(element.style.getPropertyValue('--draw-primary')).toBe('v')
|
|
41
53
|
})
|
|
42
54
|
|
|
43
55
|
test('does nothing for a null element', () => {
|
|
@@ -47,19 +59,19 @@ describe('applyTouchTargetColors', () => {
|
|
|
47
59
|
|
|
48
60
|
describe('showTouchTarget', () => {
|
|
49
61
|
test('positions and shows the element', () => {
|
|
50
|
-
const
|
|
62
|
+
const element = document.createElement('div')
|
|
51
63
|
|
|
52
|
-
showTouchTarget(
|
|
64
|
+
showTouchTarget(element, { x: 10, y: 20 })
|
|
53
65
|
|
|
54
|
-
expect(
|
|
55
|
-
expect(
|
|
56
|
-
expect(
|
|
66
|
+
expect(element.style.left).toBe('10px')
|
|
67
|
+
expect(element.style.top).toBe('20px')
|
|
68
|
+
expect(element.style.display).toBe('block')
|
|
57
69
|
})
|
|
58
70
|
|
|
59
71
|
test('does nothing without a pixel or an element', () => {
|
|
60
|
-
const
|
|
61
|
-
showTouchTarget(
|
|
62
|
-
expect(
|
|
72
|
+
const element = document.createElement('div')
|
|
73
|
+
showTouchTarget(element, null)
|
|
74
|
+
expect(element.style.display).toBe('')
|
|
63
75
|
|
|
64
76
|
expect(() => showTouchTarget(null, { x: 0, y: 0 })).not.toThrow()
|
|
65
77
|
})
|
|
@@ -67,12 +79,12 @@ describe('showTouchTarget', () => {
|
|
|
67
79
|
|
|
68
80
|
describe('hideTouchTarget', () => {
|
|
69
81
|
test('hides the element', () => {
|
|
70
|
-
const
|
|
71
|
-
|
|
82
|
+
const element = document.createElement('div')
|
|
83
|
+
element.style.display = 'block'
|
|
72
84
|
|
|
73
|
-
hideTouchTarget(
|
|
85
|
+
hideTouchTarget(element)
|
|
74
86
|
|
|
75
|
-
expect(
|
|
87
|
+
expect(element.style.display).toBe('none')
|
|
76
88
|
})
|
|
77
89
|
|
|
78
90
|
test('does nothing for a null element', () => {
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import { validateGeometry, normaliseResult, withSource } from './validateGeometry.js'
|
|
2
|
+
import { COMMIT_RULES } from './rules.js'
|
|
3
|
+
import { logger } from '../../../../src/services/logger.js'
|
|
4
|
+
import { stripInternalProperties } from '../utils/stripInternalProperties.js'
|
|
5
|
+
|
|
6
|
+
const IDLE = { revision: 0, status: 'idle', result: null, feature: null, context: null, slow: false, afterPending: false }
|
|
7
|
+
|
|
8
|
+
// How long an async check runs before it counts as slow: Done reads "Checking…" and the
|
|
9
|
+
// check is announced. A quicker reply settles without either.
|
|
10
|
+
export const SLOW_CHECK_MS = 300
|
|
11
|
+
|
|
12
|
+
const isThenable = (value) => typeof value?.then === 'function'
|
|
13
|
+
|
|
14
|
+
const statusFor = (result) => (result.valid ? 'valid' : 'invalid')
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Runs the commit-level check for each committed change: the built-in COMMIT_RULES first,
|
|
18
|
+
* synchronously, then the consumer's `onGeometryChange`, which may return a Promise.
|
|
19
|
+
*
|
|
20
|
+
* Every run gets a new revision and aborts the previous run's signal. A result only
|
|
21
|
+
* settles if its revision is still the latest, so a slow reply for an older shape can
|
|
22
|
+
* never overwrite the verdict for the current one. `onSettle(state)` fires for each
|
|
23
|
+
* settled state, including 'pending' as soon as an async check starts, and 'pending' again
|
|
24
|
+
* with `slow: true` once it has run for SLOW_CHECK_MS. A result that follows a slow
|
|
25
|
+
* 'pending' state for the same revision has `afterPending: true`.
|
|
26
|
+
*
|
|
27
|
+
* A finished shape waiting on the latest check can be held (`hold`), and is released when
|
|
28
|
+
* that check settles or the session ends — `reset` returns its id, so it can be discarded.
|
|
29
|
+
*
|
|
30
|
+
* Lives per draw adapter (see getCommitChecks) rather than in events.js's handlers,
|
|
31
|
+
* which are rebuilt on every plugin state change.
|
|
32
|
+
*
|
|
33
|
+
* States: 'idle' | 'pending' | 'valid' | 'invalid' | 'error'
|
|
34
|
+
*
|
|
35
|
+
* @returns {{ run: Function, reset: Function, setOnSettle: Function, hold: Function, release: Function, readonly heldId: string|undefined, readonly state: object }}
|
|
36
|
+
*/
|
|
37
|
+
export const createCommitChecks = () => {
|
|
38
|
+
let revision = 0
|
|
39
|
+
let slowRevision = null
|
|
40
|
+
let slowTimer = null
|
|
41
|
+
let abortController = null
|
|
42
|
+
let latest = IDLE
|
|
43
|
+
let onSettle = null
|
|
44
|
+
let held = null
|
|
45
|
+
|
|
46
|
+
const abortPending = () => {
|
|
47
|
+
abortController?.abort()
|
|
48
|
+
abortController = null
|
|
49
|
+
clearTimeout(slowTimer)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const settle = (rev, next) => {
|
|
53
|
+
if (rev !== revision) { return }
|
|
54
|
+
if (next.status !== 'pending') {
|
|
55
|
+
abortController = null
|
|
56
|
+
clearTimeout(slowTimer)
|
|
57
|
+
}
|
|
58
|
+
const slow = !!next.slow
|
|
59
|
+
const afterPending = next.status !== 'pending' && slowRevision === rev
|
|
60
|
+
slowRevision = slow ? rev : null
|
|
61
|
+
latest = { ...next, revision: rev, slow, afterPending }
|
|
62
|
+
onSettle?.(latest)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return {
|
|
66
|
+
get state () { return latest },
|
|
67
|
+
|
|
68
|
+
setOnSettle (fn) { onSettle = fn },
|
|
69
|
+
|
|
70
|
+
// The finished shape held for the latest check, if any
|
|
71
|
+
get heldId () { return held?.revision === revision ? held.id : undefined },
|
|
72
|
+
|
|
73
|
+
hold (featureId) { held = { id: featureId, revision } },
|
|
74
|
+
|
|
75
|
+
release () { held = null },
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* @param {object} params
|
|
79
|
+
* @param {object} params.feature - the committed GeoJSON feature
|
|
80
|
+
* @param {object} params.context - { phase, mode, vertexIndex }
|
|
81
|
+
* @param {Function} [params.onGeometryChange] - consumer callback
|
|
82
|
+
* @param {Array<Function>} [params.rules] - built-in rules, defaults to COMMIT_RULES
|
|
83
|
+
*/
|
|
84
|
+
run ({ feature, context, onGeometryChange, rules = COMMIT_RULES }) {
|
|
85
|
+
abortPending()
|
|
86
|
+
revision++
|
|
87
|
+
const rev = revision
|
|
88
|
+
const base = { feature, context }
|
|
89
|
+
|
|
90
|
+
const builtIn = validateGeometry(feature, context, { rules })
|
|
91
|
+
if (!builtIn.valid || typeof onGeometryChange !== 'function') {
|
|
92
|
+
settle(rev, { ...base, status: statusFor(builtIn), result: builtIn })
|
|
93
|
+
return
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
abortController = new AbortController()
|
|
97
|
+
const onError = (error) => {
|
|
98
|
+
// A superseded check rejecting (e.g. on its aborted signal) isn't a failure
|
|
99
|
+
if (rev !== revision) { return }
|
|
100
|
+
logger.warn('[draw] onGeometryChange failed', error)
|
|
101
|
+
settle(rev, { ...base, status: 'error', result: { valid: false, reason: null, source: 'callback' }, error })
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
let output
|
|
105
|
+
try {
|
|
106
|
+
output = onGeometryChange({ feature, ...context }, { signal: abortController.signal, revision: rev })
|
|
107
|
+
} catch (error) {
|
|
108
|
+
onError(error)
|
|
109
|
+
return
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const settleResult = (value) => {
|
|
113
|
+
const result = withSource(normaliseResult(value), 'callback')
|
|
114
|
+
settle(rev, { ...base, status: statusFor(result), result })
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if (!isThenable(output)) { settleResult(output); return }
|
|
118
|
+
|
|
119
|
+
settle(rev, { ...base, status: 'pending', result: null })
|
|
120
|
+
slowTimer = setTimeout(() => settle(rev, { ...base, status: 'pending', result: null, slow: true }), SLOW_CHECK_MS)
|
|
121
|
+
output.then(settleResult, onError)
|
|
122
|
+
},
|
|
123
|
+
|
|
124
|
+
// Ends the session: aborts any check in flight and drops whatever it returns. Returns
|
|
125
|
+
// the id of a shape that was held for it, which nothing will now create.
|
|
126
|
+
reset () {
|
|
127
|
+
const heldId = held?.revision === revision ? held.id : undefined
|
|
128
|
+
abortPending()
|
|
129
|
+
revision++
|
|
130
|
+
latest = IDLE
|
|
131
|
+
held = null
|
|
132
|
+
return heldId
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const checksByDraw = new WeakMap()
|
|
138
|
+
|
|
139
|
+
// One controller per draw adapter, so each map on a page keeps its own revisions.
|
|
140
|
+
export const getCommitChecks = (draw) => {
|
|
141
|
+
let checks = checksByDraw.get(draw)
|
|
142
|
+
if (!checks) {
|
|
143
|
+
checks = createCommitChecks()
|
|
144
|
+
checksByDraw.set(draw, checks)
|
|
145
|
+
}
|
|
146
|
+
return checks
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// Ring closure differs between a placed-vertices commit (open ring) and the finished
|
|
150
|
+
// feature (closed ring), so the closing coordinate is ignored when comparing.
|
|
151
|
+
const openRing = (ring) => {
|
|
152
|
+
const first = ring[0]
|
|
153
|
+
const last = ring[ring.length - 1]
|
|
154
|
+
return ring.length > 1 && first[0] === last[0] && first[1] === last[1] ? ring.slice(0, -1) : ring
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const comparableCoords = (geometry) =>
|
|
158
|
+
geometry.type === 'Polygon' ? geometry.coordinates.map(openRing) : geometry.coordinates
|
|
159
|
+
|
|
160
|
+
export const isSameGeometry = (a, b) => {
|
|
161
|
+
const geomA = a?.geometry
|
|
162
|
+
const geomB = b?.geometry
|
|
163
|
+
if (!geomA || !geomB || geomA.type !== geomB.type) { return false }
|
|
164
|
+
return JSON.stringify(comparableCoords(geomA)) === JSON.stringify(comparableCoords(geomB))
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Starts a draw/edit session's checks: installs the session's callbacks on the
|
|
169
|
+
* adapter and ends any previous session's check. A per-call callback replaces the
|
|
170
|
+
* plugin-level one. A shape still waiting on that check is discarded, as Cancel would.
|
|
171
|
+
*
|
|
172
|
+
* - `draw._previewValidator` — onGeometryPreview, read by the adapters for the
|
|
173
|
+
* 'preview' and 'place' phases
|
|
174
|
+
* - `draw._commitValidator` — onGeometryChange, read by events.js for committed changes
|
|
175
|
+
* - `draw._finishValidator` — onGeometryFinish, read by events.js when a shape is finished
|
|
176
|
+
*/
|
|
177
|
+
export const startCheckSession = (draw, options = {}, pluginConfig = {}, eventBus = null) => {
|
|
178
|
+
draw._previewValidator = options.onGeometryPreview ?? pluginConfig.onGeometryPreview
|
|
179
|
+
draw._commitValidator = options.onGeometryChange ?? pluginConfig.onGeometryChange
|
|
180
|
+
draw._finishValidator = options.onGeometryFinish ?? pluginConfig.onGeometryFinish
|
|
181
|
+
const heldId = getCommitChecks(draw).reset()
|
|
182
|
+
if (heldId !== undefined) {
|
|
183
|
+
const discarded = draw.get(heldId)
|
|
184
|
+
draw.delete(heldId)
|
|
185
|
+
eventBus?.emit('draw:cancelled', stripInternalProperties(discarded))
|
|
186
|
+
}
|
|
187
|
+
}
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import { createCommitChecks, getCommitChecks, isSameGeometry, startCheckSession, SLOW_CHECK_MS } from './commitChecks.js'
|
|
2
|
+
|
|
3
|
+
const square = { type: 'Feature', geometry: { type: 'Polygon', coordinates: [[[0, 0], [0, 1], [1, 1], [1, 0], [0, 0]]] } }
|
|
4
|
+
const context = { phase: 'change', mode: 'draw_polygon', vertexIndex: 3 }
|
|
5
|
+
const flush = () => Promise.resolve()
|
|
6
|
+
|
|
7
|
+
describe('createCommitChecks', () => {
|
|
8
|
+
test('starts idle', () => {
|
|
9
|
+
expect(createCommitChecks().state).toEqual(expect.objectContaining({ status: 'idle', revision: 0 }))
|
|
10
|
+
})
|
|
11
|
+
|
|
12
|
+
test('settles a passing built-in check straight away when there is no callback', () => {
|
|
13
|
+
const checks = createCommitChecks()
|
|
14
|
+
const onSettle = jest.fn()
|
|
15
|
+
checks.setOnSettle(onSettle)
|
|
16
|
+
checks.run({ feature: square, context })
|
|
17
|
+
expect(onSettle).toHaveBeenCalledWith(expect.objectContaining({ status: 'valid', feature: square, context, revision: 1 }))
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
test('passes the event and a { signal, revision } second argument to the callback', () => {
|
|
21
|
+
const checks = createCommitChecks()
|
|
22
|
+
const onGeometryChange = jest.fn(() => true)
|
|
23
|
+
checks.run({ feature: square, context, onGeometryChange })
|
|
24
|
+
expect(onGeometryChange).toHaveBeenCalledWith({ feature: square, ...context }, { signal: expect.any(AbortSignal), revision: 1 })
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
test('normalises a synchronous result', () => {
|
|
28
|
+
const checks = createCommitChecks()
|
|
29
|
+
checks.run({ feature: square, context, onGeometryChange: () => ({ valid: false, reason: 'too big' }) })
|
|
30
|
+
expect(checks.state).toEqual(expect.objectContaining({ status: 'invalid', result: { valid: false, reason: 'too big', source: 'callback' } }))
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
test('goes pending for a Promise, turns slow, then settles with its result', async () => {
|
|
34
|
+
jest.useFakeTimers()
|
|
35
|
+
const checks = createCommitChecks()
|
|
36
|
+
const onSettle = jest.fn()
|
|
37
|
+
checks.setOnSettle(onSettle)
|
|
38
|
+
let resolve
|
|
39
|
+
checks.run({ feature: square, context, onGeometryChange: () => new Promise((_resolve) => { resolve = _resolve }) })
|
|
40
|
+
expect(checks.state).toEqual(expect.objectContaining({ status: 'pending', slow: false }))
|
|
41
|
+
|
|
42
|
+
jest.advanceTimersByTime(SLOW_CHECK_MS)
|
|
43
|
+
expect(checks.state).toEqual(expect.objectContaining({ status: 'pending', slow: true }))
|
|
44
|
+
|
|
45
|
+
resolve(false)
|
|
46
|
+
await flush()
|
|
47
|
+
expect(checks.state).toEqual(expect.objectContaining({ status: 'invalid', afterPending: true }))
|
|
48
|
+
expect(onSettle.mock.calls.map(([state]) => [state.status, state.slow])).toEqual([['pending', false], ['pending', true], ['invalid', false]])
|
|
49
|
+
|
|
50
|
+
// The next check settles straight away, so it doesn't follow a pending state
|
|
51
|
+
checks.run({ feature: square, context, onGeometryChange: () => true })
|
|
52
|
+
expect(checks.state.afterPending).toBe(false)
|
|
53
|
+
jest.useRealTimers()
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
test('a quick async reply settles without turning slow', async () => {
|
|
57
|
+
jest.useFakeTimers()
|
|
58
|
+
const checks = createCommitChecks()
|
|
59
|
+
const onSettle = jest.fn()
|
|
60
|
+
checks.setOnSettle(onSettle)
|
|
61
|
+
checks.run({ feature: square, context, onGeometryChange: () => Promise.resolve(true) })
|
|
62
|
+
await flush()
|
|
63
|
+
jest.advanceTimersByTime(SLOW_CHECK_MS)
|
|
64
|
+
expect(checks.state).toEqual(expect.objectContaining({ status: 'valid', afterPending: false }))
|
|
65
|
+
expect(onSettle.mock.calls.map(([state]) => state.status)).toEqual(['pending', 'valid'])
|
|
66
|
+
jest.useRealTimers()
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
test('marks a built-in rule failure and a callback error with their source', () => {
|
|
70
|
+
const checks = createCommitChecks()
|
|
71
|
+
const partDrawn = { type: 'Feature', geometry: { type: 'Polygon', coordinates: [[[0, 0], [1, 0], [0, 0]]] } }
|
|
72
|
+
checks.run({ feature: partDrawn, context })
|
|
73
|
+
expect(checks.state.result.source).toBe('rules')
|
|
74
|
+
|
|
75
|
+
jest.spyOn(console, 'warn').mockImplementation(() => {})
|
|
76
|
+
checks.run({ feature: square, context, onGeometryChange: () => { throw new Error('503') } })
|
|
77
|
+
expect(checks.state).toEqual(expect.objectContaining({ status: 'error', result: { valid: false, reason: null, source: 'callback' } }))
|
|
78
|
+
console.warn.mockRestore()
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
test('reset aborts the check in flight, drops its reply and returns to idle', async () => {
|
|
82
|
+
const checks = createCommitChecks()
|
|
83
|
+
let resolve
|
|
84
|
+
let signal
|
|
85
|
+
checks.run({ feature: square, context, onGeometryChange: (_event, ctx) => { signal = ctx.signal; return new Promise((_resolve) => { resolve = _resolve }) } })
|
|
86
|
+
checks.reset()
|
|
87
|
+
expect(signal.aborted).toBe(true)
|
|
88
|
+
|
|
89
|
+
resolve(true)
|
|
90
|
+
await flush()
|
|
91
|
+
expect(checks.state.status).toBe('idle')
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
test('a superseded check that rejects on its aborted signal is dropped silently', async () => {
|
|
95
|
+
const checks = createCommitChecks()
|
|
96
|
+
const warn = jest.spyOn(console, 'warn').mockImplementation(() => {})
|
|
97
|
+
const onGeometryChange = (_event, { signal }) => new Promise((_resolve, reject) => {
|
|
98
|
+
signal.addEventListener('abort', () => reject(new Error('aborted')))
|
|
99
|
+
})
|
|
100
|
+
checks.run({ feature: square, context, onGeometryChange })
|
|
101
|
+
checks.run({ feature: square, context, onGeometryChange: () => true })
|
|
102
|
+
await flush()
|
|
103
|
+
expect(checks.state.status).toBe('valid')
|
|
104
|
+
expect(warn).not.toHaveBeenCalled()
|
|
105
|
+
warn.mockRestore()
|
|
106
|
+
})
|
|
107
|
+
|
|
108
|
+
test('a held shape belongs to the latest check, and is released or returned on reset', () => {
|
|
109
|
+
const checks = createCommitChecks()
|
|
110
|
+
checks.run({ feature: square, context })
|
|
111
|
+
checks.hold('a')
|
|
112
|
+
expect(checks.heldId).toBe('a')
|
|
113
|
+
checks.release()
|
|
114
|
+
expect(checks.heldId).toBeUndefined()
|
|
115
|
+
|
|
116
|
+
checks.hold('b')
|
|
117
|
+
checks.run({ feature: square, context }) // a newer check
|
|
118
|
+
expect(checks.heldId).toBeUndefined()
|
|
119
|
+
expect(checks.reset()).toBeUndefined()
|
|
120
|
+
|
|
121
|
+
checks.hold('c')
|
|
122
|
+
expect(checks.reset()).toBe('c')
|
|
123
|
+
})
|
|
124
|
+
|
|
125
|
+
test('works without an onSettle callback', () => {
|
|
126
|
+
const checks = createCommitChecks()
|
|
127
|
+
expect(() => checks.run({ feature: square, context })).not.toThrow()
|
|
128
|
+
})
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
describe('getCommitChecks', () => {
|
|
132
|
+
test('keeps one controller per draw adapter', () => {
|
|
133
|
+
const drawA = {}
|
|
134
|
+
const drawB = {}
|
|
135
|
+
expect(getCommitChecks(drawA)).toBe(getCommitChecks(drawA))
|
|
136
|
+
expect(getCommitChecks(drawA)).not.toBe(getCommitChecks(drawB))
|
|
137
|
+
})
|
|
138
|
+
})
|
|
139
|
+
|
|
140
|
+
describe('isSameGeometry', () => {
|
|
141
|
+
const ring = (coordinates) => ({ geometry: { type: 'Polygon', coordinates: [coordinates] } })
|
|
142
|
+
|
|
143
|
+
test('ignores the closing coordinate of a polygon ring', () => {
|
|
144
|
+
expect(isSameGeometry(ring([[0, 0], [0, 1], [1, 1]]), ring([[0, 0], [0, 1], [1, 1], [0, 0]]))).toBe(true)
|
|
145
|
+
})
|
|
146
|
+
|
|
147
|
+
test('tells different shapes apart', () => {
|
|
148
|
+
expect(isSameGeometry(ring([[0, 0], [0, 1], [1, 1]]), ring([[0, 0], [0, 2], [2, 2]]))).toBe(false)
|
|
149
|
+
expect(isSameGeometry(ring([[0, 0], [0, 1], [1, 1]]), { geometry: { type: 'LineString', coordinates: [[0, 0], [0, 1], [1, 1]] } })).toBe(false)
|
|
150
|
+
})
|
|
151
|
+
|
|
152
|
+
test('compares lines and points directly', () => {
|
|
153
|
+
const line = { geometry: { type: 'LineString', coordinates: [[0, 0], [1, 1]] } }
|
|
154
|
+
expect(isSameGeometry(line, { geometry: { type: 'LineString', coordinates: [[0, 0], [1, 1]] } })).toBe(true)
|
|
155
|
+
expect(isSameGeometry({ geometry: { type: 'Point', coordinates: [0, 0] } }, { geometry: { type: 'Point', coordinates: [0, 0] } })).toBe(true)
|
|
156
|
+
})
|
|
157
|
+
|
|
158
|
+
test('is false when either side has no geometry', () => {
|
|
159
|
+
expect(isSameGeometry(null, ring([[0, 0]]))).toBe(false)
|
|
160
|
+
expect(isSameGeometry(ring([[0, 0]]), { id: 'x' })).toBe(false)
|
|
161
|
+
})
|
|
162
|
+
})
|
|
163
|
+
|
|
164
|
+
describe('startCheckSession', () => {
|
|
165
|
+
test('prefers per-call callbacks over the plugin-level ones', () => {
|
|
166
|
+
const draw = {}
|
|
167
|
+
const onGeometryChange = jest.fn()
|
|
168
|
+
const onGeometryPreview = jest.fn(() => true)
|
|
169
|
+
const onGeometryFinish = jest.fn()
|
|
170
|
+
startCheckSession(draw, { onGeometryChange, onGeometryPreview, onGeometryFinish }, { onGeometryChange: jest.fn(), onGeometryPreview: jest.fn(), onGeometryFinish: jest.fn() })
|
|
171
|
+
expect(draw._commitValidator).toBe(onGeometryChange)
|
|
172
|
+
expect(draw._previewValidator).toBe(onGeometryPreview)
|
|
173
|
+
expect(draw._finishValidator).toBe(onGeometryFinish)
|
|
174
|
+
})
|
|
175
|
+
|
|
176
|
+
test('falls back to the plugin-level callbacks', () => {
|
|
177
|
+
const draw = {}
|
|
178
|
+
const onGeometryChange = jest.fn()
|
|
179
|
+
const onGeometryFinish = jest.fn()
|
|
180
|
+
startCheckSession(draw, {}, { onGeometryChange, onGeometryFinish })
|
|
181
|
+
expect(draw._commitValidator).toBe(onGeometryChange)
|
|
182
|
+
expect(draw._finishValidator).toBe(onGeometryFinish)
|
|
183
|
+
expect(draw._previewValidator).toBeUndefined()
|
|
184
|
+
})
|
|
185
|
+
|
|
186
|
+
test('discards a shape still held for the previous session\u2019s check, as Cancel would', () => {
|
|
187
|
+
const shape = { id: 'held', type: 'Feature', properties: {}, geometry: { type: 'Polygon', coordinates: [[[0, 0], [0, 1], [1, 1], [0, 0]]] } }
|
|
188
|
+
const draw = { get: jest.fn(() => shape), delete: jest.fn() }
|
|
189
|
+
const eventBus = { emit: jest.fn() }
|
|
190
|
+
const checks = getCommitChecks(draw)
|
|
191
|
+
checks.run({ feature: shape, context: { phase: 'change' }, onGeometryChange: () => new Promise(() => {}) })
|
|
192
|
+
checks.hold('held')
|
|
193
|
+
expect(checks.heldId).toBe('held')
|
|
194
|
+
|
|
195
|
+
startCheckSession(draw, {}, {}, eventBus)
|
|
196
|
+
expect(draw.delete).toHaveBeenCalledWith('held')
|
|
197
|
+
expect(eventBus.emit).toHaveBeenCalledWith('draw:cancelled', shape)
|
|
198
|
+
expect(checks.heldId).toBeUndefined()
|
|
199
|
+
})
|
|
200
|
+
|
|
201
|
+
test('ends the previous session’s check', () => {
|
|
202
|
+
const draw = {}
|
|
203
|
+
const checks = getCommitChecks(draw)
|
|
204
|
+
checks.run({ feature: { type: 'Feature', geometry: { type: 'Point', coordinates: [0, 0] } }, context: { phase: 'edit-start' } })
|
|
205
|
+
expect(checks.state.status).toBe('valid')
|
|
206
|
+
startCheckSession(draw)
|
|
207
|
+
expect(checks.state.status).toBe('idle')
|
|
208
|
+
})
|
|
209
|
+
})
|
|
@@ -1,22 +1,22 @@
|
|
|
1
1
|
import { validateGeometry } from './validateGeometry.js'
|
|
2
|
-
import {
|
|
2
|
+
import { PREVIEW_RULES, PLACE_RULES, MIN_VERTICES } from './rules.js'
|
|
3
3
|
import { requestFrame, cancelFrame } from './liveStroke.js'
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Live validation driver for DRAW mode (draw_polygon/draw_line) only — edit mode
|
|
7
7
|
* has no Add-point gate and keeps using the plain createLiveStroke (liveStroke.js).
|
|
8
8
|
*
|
|
9
|
-
* On every rubber-band move, a user `
|
|
9
|
+
* On every rubber-band move, a user `onGeometryPreview` callback is called ONCE,
|
|
10
10
|
* throttled to one call per animation frame (phase 'preview', with NO
|
|
11
11
|
* vertex-count floor — a location-based rule is meaningful even against the
|
|
12
12
|
* very first candidate point, before any vertex is committed). Its verdict is
|
|
13
13
|
* combined with two INDEPENDENTLY-floored built-in rule checks, each flip-guarded
|
|
14
14
|
* and reported through its own callback — `onChange(invalid, reason)` fires only
|
|
15
15
|
* when THAT gate's own state flips, same contract as createLiveStroke:
|
|
16
|
-
* -
|
|
16
|
+
* - PREVIEW_RULES (closed-ring self-intersect/area), floored at the geometry
|
|
17
17
|
* type's minimum vertex count — self-intersection/area are meaningless
|
|
18
18
|
* below that — reported via onStrokeChange (dashed stroke / Done gate).
|
|
19
|
-
* -
|
|
19
|
+
* - PLACE_RULES (open-path self-intersect), never floored, so it still
|
|
20
20
|
* protects placing an invalid first vertex — reported via onPlaceChange
|
|
21
21
|
* (Add-point gate).
|
|
22
22
|
* Previously each gate ran its own independently-throttled call to the same
|
|
@@ -76,15 +76,15 @@ export const createLiveDrawChecks = ({ onStrokeChange, onPlaceChange }) => {
|
|
|
76
76
|
if (!pending) { return }
|
|
77
77
|
const { feature, context } = pending
|
|
78
78
|
const min = MIN_VERTICES[feature?.geometry?.type] ?? 0
|
|
79
|
-
evaluateGate(feature, context,
|
|
80
|
-
evaluateGate(feature, context,
|
|
79
|
+
evaluateGate(feature, context, PREVIEW_RULES, min, flipStroke)
|
|
80
|
+
evaluateGate(feature, context, PLACE_RULES, 0, flipPlace)
|
|
81
81
|
}
|
|
82
82
|
|
|
83
83
|
const runUserRule = () => {
|
|
84
84
|
frame = null
|
|
85
85
|
if (!pending) { return }
|
|
86
|
-
const { feature, context,
|
|
87
|
-
const result = validateGeometry(feature, { ...context, phase: 'preview' }, { rules: [],
|
|
86
|
+
const { feature, context, onGeometryPreview } = pending
|
|
87
|
+
const result = validateGeometry(feature, { ...context, phase: 'preview' }, { rules: [], onGeometryPreview })
|
|
88
88
|
customInvalid = !result.valid
|
|
89
89
|
customReason = result.reason
|
|
90
90
|
recompute()
|
|
@@ -92,13 +92,13 @@ export const createLiveDrawChecks = ({ onStrokeChange, onPlaceChange }) => {
|
|
|
92
92
|
|
|
93
93
|
return {
|
|
94
94
|
// Re-evaluate the displayed geometry after a rubber-band move.
|
|
95
|
-
update ({ feature, context = {}, numVertices,
|
|
96
|
-
pending = { feature, context: { ...context, numVertices },
|
|
95
|
+
update ({ feature, context = {}, numVertices, onGeometryPreview }) {
|
|
96
|
+
pending = { feature, context: { ...context, numVertices }, onGeometryPreview }
|
|
97
97
|
// Built-in rules first, synchronously — immediate feedback either gate.
|
|
98
98
|
recompute()
|
|
99
99
|
// A user rule (if any) decides last, throttled to one frame; its cached
|
|
100
100
|
// verdict (customInvalid) feeds the next recompute() once it lands.
|
|
101
|
-
if (typeof
|
|
101
|
+
if (typeof onGeometryPreview === 'function' && frame == null) {
|
|
102
102
|
frame = requestFrame(runUserRule)
|
|
103
103
|
}
|
|
104
104
|
},
|