@trackunit/react-map-draw 0.0.3 → 0.0.5
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/index.cjs.js +167 -52
- package/index.esm.js +168 -53
- package/package.json +3 -3
- package/src/DrawController.d.ts +46 -18
- package/src/drawEngine.d.ts +12 -3
- package/src/drawValue.d.ts +5 -3
- package/src/index.d.ts +1 -1
- package/src/testing/drawPortContract.d.ts +4 -1
- package/src/testing/index.d.ts +7 -0
- package/terraDrawEngine.cjs.js +59 -44
- package/terraDrawEngine.esm.js +60 -45
package/index.cjs.js
CHANGED
|
@@ -18,18 +18,13 @@ const isDrawablePolygonFeature = (feature) => {
|
|
|
18
18
|
return geoJsonUtils.geoJsonPolygonSchema.safeParse(geometry).success;
|
|
19
19
|
};
|
|
20
20
|
/**
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
21
|
+
* Canonical form of a committed value: an empty collection becomes the shared
|
|
22
|
+
* `EMPTY_DRAW_VALUE`, anything else is returned by reference so controlled-input
|
|
23
|
+
* round trips keep identity. Features the engine cannot render are kept, not
|
|
24
|
+
* dropped: the engine reports them as `value-rejected` and the committed value
|
|
25
|
+
* stays what the consumer set (ADR 0030, decided 19 Oct 2026).
|
|
24
26
|
*/
|
|
25
|
-
const normalizeDrawValue = (value) =>
|
|
26
|
-
const features = value.features.filter(isDrawablePolygonFeature);
|
|
27
|
-
if (features.length === 0)
|
|
28
|
-
return reactMapAdapterShared.EMPTY_DRAW_VALUE;
|
|
29
|
-
if (features.length === value.features.length)
|
|
30
|
-
return value;
|
|
31
|
-
return { type: "FeatureCollection", features };
|
|
32
|
-
};
|
|
27
|
+
const normalizeDrawValue = (value) => value.features.length === 0 ? reactMapAdapterShared.EMPTY_DRAW_VALUE : value;
|
|
33
28
|
/** Full feature equality (ids, properties and geometry). Used for controlled-input updates. */
|
|
34
29
|
const drawValuesIdentical = (a, b) => a === b || esToolkit.isEqual(a.features, b.features);
|
|
35
30
|
/**
|
|
@@ -45,21 +40,33 @@ const drawValuesEqual = (a, b) => {
|
|
|
45
40
|
return esToolkit.isEqual(a.features.map(feature => feature.geometry), b.features.map(feature => feature.geometry));
|
|
46
41
|
};
|
|
47
42
|
|
|
43
|
+
const sameIndices = (a, b) => a.length === b.length && a.every((index, position) => index === b[position]);
|
|
48
44
|
/**
|
|
49
45
|
* Provider-agnostic `DrawPort` implementation (ADR 0030).
|
|
50
46
|
*
|
|
51
|
-
* The controller is the source of truth for `mode` and `
|
|
52
|
-
* resources itself: adapters call `connect(loader)` whenever they have a
|
|
53
|
-
* to draw on (first connect, and again after every recreation or style
|
|
54
|
-
* `disconnect()` when the native map goes away. The engine is created
|
|
55
|
-
* once the drawing needs one (non-idle mode or a non-empty value), so
|
|
56
|
-
* draw never load Terra Draw.
|
|
47
|
+
* The controller is the source of truth for `mode`, `value` and `errors`. It owns no
|
|
48
|
+
* native resources itself: adapters call `connect(loader)` whenever they have a
|
|
49
|
+
* native map to draw on (first connect, and again after every recreation or style
|
|
50
|
+
* reload) and `disconnect()` when the native map goes away. The engine is created
|
|
51
|
+
* lazily, only once the drawing needs one (non-idle mode or a non-empty value), so
|
|
52
|
+
* maps that never draw never load Terra Draw.
|
|
53
|
+
*
|
|
54
|
+
* The committed value is never rewritten from the map: features the engine cannot
|
|
55
|
+
* render stay in `value` and surface as a `value-rejected` error, so a stored
|
|
56
|
+
* boundary opened for editing is never silently replaced by an empty one. A user
|
|
57
|
+
* edit keeps those features too, ahead of whatever the user drew.
|
|
57
58
|
*/
|
|
58
59
|
class DrawController {
|
|
59
|
-
constructor(
|
|
60
|
-
this.options = options;
|
|
60
|
+
constructor() {
|
|
61
61
|
this.mode = "idle";
|
|
62
62
|
this.value = reactMapAdapterShared.EMPTY_DRAW_VALUE;
|
|
63
|
+
/**
|
|
64
|
+
* Two error slots, so a transient engine failure never hides (or, on recovery,
|
|
65
|
+
* silently drops) the verdict on the committed value. The snapshot object is
|
|
66
|
+
* replaced only when a slot changes, so `useSyncExternalStore` sees one change
|
|
67
|
+
* per real change.
|
|
68
|
+
*/
|
|
69
|
+
this.errors = reactMapAdapterShared.NO_DRAW_ERRORS;
|
|
63
70
|
this.listeners = new Set();
|
|
64
71
|
this.loader = null;
|
|
65
72
|
this.engine = null;
|
|
@@ -67,6 +74,9 @@ class DrawController {
|
|
|
67
74
|
/** Incremented on every connect/disconnect so a slow loader cannot attach a stale engine. */
|
|
68
75
|
this.generation = 0;
|
|
69
76
|
this.loading = false;
|
|
77
|
+
/** While > 0, `notify` only records that something changed; the outermost batch notifies once. */
|
|
78
|
+
this.batchDepth = 0;
|
|
79
|
+
this.batchDirty = false;
|
|
70
80
|
}
|
|
71
81
|
// ---- Adapter lifecycle ----
|
|
72
82
|
/**
|
|
@@ -78,7 +88,7 @@ class DrawController {
|
|
|
78
88
|
this.loader = loader;
|
|
79
89
|
this.ensureEngine();
|
|
80
90
|
}
|
|
81
|
-
/** Unbind from the native map. Mode and
|
|
91
|
+
/** Unbind from the native map. Mode, value and errors are kept for the next `connect`. */
|
|
82
92
|
disconnect() {
|
|
83
93
|
this.releaseEngine();
|
|
84
94
|
this.loader = null;
|
|
@@ -91,13 +101,15 @@ class DrawController {
|
|
|
91
101
|
setMode(mode) {
|
|
92
102
|
if (this.mode === mode)
|
|
93
103
|
return;
|
|
94
|
-
this.
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
this.engine
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
104
|
+
this.batch(() => {
|
|
105
|
+
this.mode = mode;
|
|
106
|
+
this.notify();
|
|
107
|
+
if (this.engine !== null) {
|
|
108
|
+
this.applyToEngine(this.engine, engine => engine.setMode(mode));
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
this.ensureEngine();
|
|
112
|
+
});
|
|
101
113
|
}
|
|
102
114
|
setValue(value) {
|
|
103
115
|
const normalized = normalizeDrawValue(value);
|
|
@@ -106,18 +118,23 @@ class DrawController {
|
|
|
106
118
|
if (drawValuesIdentical(this.value, normalized))
|
|
107
119
|
return;
|
|
108
120
|
const sameGeometry = drawValuesEqual(this.value, normalized);
|
|
109
|
-
this.
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
+
this.batch(() => {
|
|
122
|
+
this.value = normalized;
|
|
123
|
+
this.notify();
|
|
124
|
+
// The engine renders geometry only. A value that merely re-labels ids or
|
|
125
|
+
// properties (a consumer round-tripping through form state) must not clear and
|
|
126
|
+
// redraw the store, which would drop the selection or interrupt a drag.
|
|
127
|
+
if (sameGeometry)
|
|
128
|
+
return;
|
|
129
|
+
// A new geometry gets a fresh verdict: the engine reports again if it still
|
|
130
|
+
// cannot render part of it.
|
|
131
|
+
this.setValueError(null);
|
|
132
|
+
if (this.engine !== null) {
|
|
133
|
+
this.applyToEngine(this.engine, engine => engine.setValue(normalized));
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
this.ensureEngine();
|
|
137
|
+
});
|
|
121
138
|
}
|
|
122
139
|
getMode() {
|
|
123
140
|
return this.mode;
|
|
@@ -125,6 +142,9 @@ class DrawController {
|
|
|
125
142
|
getValue() {
|
|
126
143
|
return this.value;
|
|
127
144
|
}
|
|
145
|
+
getErrors() {
|
|
146
|
+
return this.errors;
|
|
147
|
+
}
|
|
128
148
|
subscribe(listener) {
|
|
129
149
|
this.listeners.add(listener);
|
|
130
150
|
return () => {
|
|
@@ -153,29 +173,84 @@ class DrawController {
|
|
|
153
173
|
}
|
|
154
174
|
this.loading = false;
|
|
155
175
|
this.attachEngine(engine);
|
|
156
|
-
}, (
|
|
176
|
+
}, (cause) => {
|
|
157
177
|
if (generation !== this.generation)
|
|
158
178
|
return;
|
|
159
179
|
this.loading = false;
|
|
160
|
-
this.
|
|
180
|
+
this.setEngineError({ code: "engine-load-failed", cause });
|
|
161
181
|
});
|
|
162
182
|
}
|
|
163
183
|
attachEngine(engine) {
|
|
164
184
|
this.engine = engine;
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
185
|
+
const unsubscribeChange = engine.onChange(rendered => this.adoptEngineValue(engine, rendered));
|
|
186
|
+
const unsubscribeRejection = engine.onValueRejected(rejection => this.setValueError(rejection));
|
|
187
|
+
this.engineUnsubscribe = () => {
|
|
188
|
+
unsubscribeChange();
|
|
189
|
+
unsubscribeRejection();
|
|
190
|
+
};
|
|
191
|
+
this.batch(() => {
|
|
192
|
+
this.setEngineError(null);
|
|
193
|
+
this.applyToEngine(engine, current => {
|
|
194
|
+
current.setValue(this.value);
|
|
195
|
+
current.setMode(this.mode);
|
|
196
|
+
});
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* A user edit arrived from the map. Features the engine could not render are not
|
|
201
|
+
* on the map, so they cannot be part of what the user edited: they stay in the
|
|
202
|
+
* value, ahead of the rendered polygons, and the verdict on them stands.
|
|
203
|
+
*/
|
|
204
|
+
adoptEngineValue(engine, rendered) {
|
|
205
|
+
const kept = this.rejectedFeatures();
|
|
206
|
+
const next = kept.length === 0 ? rendered : { type: "FeatureCollection", features: [...kept, ...rendered.features] };
|
|
207
|
+
if (drawValuesEqual(this.value, next))
|
|
208
|
+
return;
|
|
209
|
+
this.batch(() => {
|
|
210
|
+
this.value = next;
|
|
211
|
+
this.notify();
|
|
212
|
+
const verdict = this.errors.value;
|
|
213
|
+
if (kept.length > 0 && verdict !== null) {
|
|
214
|
+
// Same verdict, same object: a consumer's onError must not re-fire on every edit.
|
|
215
|
+
const indices = kept.map((_, index) => index);
|
|
216
|
+
if (!sameIndices(verdict.rejectedFeatureIndices, indices)) {
|
|
217
|
+
this.errors = { engine: this.errors.engine, value: { ...verdict, rejectedFeatureIndices: indices } };
|
|
218
|
+
this.notify();
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
// Select mode has nothing left to select once the last rendered polygon is
|
|
222
|
+
// deleted, so drop back to idle instead of leaving the map in a dead mode.
|
|
223
|
+
if (rendered.features.length === 0 && this.mode === "select") {
|
|
172
224
|
this.mode = "idle";
|
|
173
|
-
|
|
225
|
+
this.notify();
|
|
226
|
+
this.applyToEngine(engine, current => current.setMode("idle"));
|
|
174
227
|
}
|
|
175
|
-
this.notify();
|
|
176
228
|
});
|
|
177
|
-
|
|
178
|
-
|
|
229
|
+
}
|
|
230
|
+
rejectedFeatures() {
|
|
231
|
+
const rejection = this.errors.value;
|
|
232
|
+
if (rejection === null)
|
|
233
|
+
return [];
|
|
234
|
+
return rejection.rejectedFeatureIndices.flatMap(index => {
|
|
235
|
+
const feature = this.value.features[index];
|
|
236
|
+
return feature === undefined ? [] : [feature];
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Runs an engine operation. A throwing engine (Terra Draw with a map that is mid
|
|
241
|
+
* teardown, a provider adapter that lost its style) is reported as
|
|
242
|
+
* `engine-update-failed` instead of propagating into React state updates; the
|
|
243
|
+
* committed mode and value are untouched and apply again on the next reconnect.
|
|
244
|
+
*/
|
|
245
|
+
applyToEngine(engine, operation) {
|
|
246
|
+
try {
|
|
247
|
+
operation(engine);
|
|
248
|
+
if (this.errors.engine?.code === "engine-update-failed")
|
|
249
|
+
this.setEngineError(null);
|
|
250
|
+
}
|
|
251
|
+
catch (cause) {
|
|
252
|
+
this.setEngineError({ code: "engine-update-failed", cause });
|
|
253
|
+
}
|
|
179
254
|
}
|
|
180
255
|
releaseEngine() {
|
|
181
256
|
this.generation += 1;
|
|
@@ -185,7 +260,47 @@ class DrawController {
|
|
|
185
260
|
this.engine?.destroy();
|
|
186
261
|
this.engine = null;
|
|
187
262
|
}
|
|
263
|
+
/**
|
|
264
|
+
* Replaces the verdict on the committed value. The same verdict again (a rebuilt
|
|
265
|
+
* engine re-applying the same value after a reconnect) keeps the existing object,
|
|
266
|
+
* so subscribers are not told about a change that did not happen.
|
|
267
|
+
*/
|
|
268
|
+
setValueError(rejection) {
|
|
269
|
+
const current = this.errors.value;
|
|
270
|
+
if (current === rejection)
|
|
271
|
+
return;
|
|
272
|
+
if (current !== null &&
|
|
273
|
+
rejection !== null &&
|
|
274
|
+
sameIndices(current.rejectedFeatureIndices, rejection.rejectedFeatureIndices)) {
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
277
|
+
this.errors = { engine: this.errors.engine, value: rejection };
|
|
278
|
+
this.notify();
|
|
279
|
+
}
|
|
280
|
+
setEngineError(error) {
|
|
281
|
+
if (this.errors.engine === error)
|
|
282
|
+
return;
|
|
283
|
+
this.errors = { engine: error, value: this.errors.value };
|
|
284
|
+
this.notify();
|
|
285
|
+
}
|
|
286
|
+
batch(operation) {
|
|
287
|
+
this.batchDepth += 1;
|
|
288
|
+
try {
|
|
289
|
+
operation();
|
|
290
|
+
}
|
|
291
|
+
finally {
|
|
292
|
+
this.batchDepth -= 1;
|
|
293
|
+
if (this.batchDepth === 0 && this.batchDirty) {
|
|
294
|
+
this.batchDirty = false;
|
|
295
|
+
this.listeners.forEach(listener => listener());
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
}
|
|
188
299
|
notify() {
|
|
300
|
+
if (this.batchDepth > 0) {
|
|
301
|
+
this.batchDirty = true;
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
189
304
|
this.listeners.forEach(listener => listener());
|
|
190
305
|
}
|
|
191
306
|
}
|
package/index.esm.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { EMPTY_DRAW_VALUE } from '@trackunit/react-map-adapter-shared';
|
|
1
|
+
import { EMPTY_DRAW_VALUE, NO_DRAW_ERRORS } from '@trackunit/react-map-adapter-shared';
|
|
2
2
|
import { geoJsonPolygonSchema } from '@trackunit/geo-json-utils';
|
|
3
3
|
import { isEqual } from 'es-toolkit';
|
|
4
4
|
|
|
@@ -16,18 +16,13 @@ const isDrawablePolygonFeature = (feature) => {
|
|
|
16
16
|
return geoJsonPolygonSchema.safeParse(geometry).success;
|
|
17
17
|
};
|
|
18
18
|
/**
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
19
|
+
* Canonical form of a committed value: an empty collection becomes the shared
|
|
20
|
+
* `EMPTY_DRAW_VALUE`, anything else is returned by reference so controlled-input
|
|
21
|
+
* round trips keep identity. Features the engine cannot render are kept, not
|
|
22
|
+
* dropped: the engine reports them as `value-rejected` and the committed value
|
|
23
|
+
* stays what the consumer set (ADR 0030, decided 19 Oct 2026).
|
|
22
24
|
*/
|
|
23
|
-
const normalizeDrawValue = (value) =>
|
|
24
|
-
const features = value.features.filter(isDrawablePolygonFeature);
|
|
25
|
-
if (features.length === 0)
|
|
26
|
-
return EMPTY_DRAW_VALUE;
|
|
27
|
-
if (features.length === value.features.length)
|
|
28
|
-
return value;
|
|
29
|
-
return { type: "FeatureCollection", features };
|
|
30
|
-
};
|
|
25
|
+
const normalizeDrawValue = (value) => value.features.length === 0 ? EMPTY_DRAW_VALUE : value;
|
|
31
26
|
/** Full feature equality (ids, properties and geometry). Used for controlled-input updates. */
|
|
32
27
|
const drawValuesIdentical = (a, b) => a === b || isEqual(a.features, b.features);
|
|
33
28
|
/**
|
|
@@ -43,21 +38,33 @@ const drawValuesEqual = (a, b) => {
|
|
|
43
38
|
return isEqual(a.features.map(feature => feature.geometry), b.features.map(feature => feature.geometry));
|
|
44
39
|
};
|
|
45
40
|
|
|
41
|
+
const sameIndices = (a, b) => a.length === b.length && a.every((index, position) => index === b[position]);
|
|
46
42
|
/**
|
|
47
43
|
* Provider-agnostic `DrawPort` implementation (ADR 0030).
|
|
48
44
|
*
|
|
49
|
-
* The controller is the source of truth for `mode` and `
|
|
50
|
-
* resources itself: adapters call `connect(loader)` whenever they have a
|
|
51
|
-
* to draw on (first connect, and again after every recreation or style
|
|
52
|
-
* `disconnect()` when the native map goes away. The engine is created
|
|
53
|
-
* once the drawing needs one (non-idle mode or a non-empty value), so
|
|
54
|
-
* draw never load Terra Draw.
|
|
45
|
+
* The controller is the source of truth for `mode`, `value` and `errors`. It owns no
|
|
46
|
+
* native resources itself: adapters call `connect(loader)` whenever they have a
|
|
47
|
+
* native map to draw on (first connect, and again after every recreation or style
|
|
48
|
+
* reload) and `disconnect()` when the native map goes away. The engine is created
|
|
49
|
+
* lazily, only once the drawing needs one (non-idle mode or a non-empty value), so
|
|
50
|
+
* maps that never draw never load Terra Draw.
|
|
51
|
+
*
|
|
52
|
+
* The committed value is never rewritten from the map: features the engine cannot
|
|
53
|
+
* render stay in `value` and surface as a `value-rejected` error, so a stored
|
|
54
|
+
* boundary opened for editing is never silently replaced by an empty one. A user
|
|
55
|
+
* edit keeps those features too, ahead of whatever the user drew.
|
|
55
56
|
*/
|
|
56
57
|
class DrawController {
|
|
57
|
-
constructor(
|
|
58
|
-
this.options = options;
|
|
58
|
+
constructor() {
|
|
59
59
|
this.mode = "idle";
|
|
60
60
|
this.value = EMPTY_DRAW_VALUE;
|
|
61
|
+
/**
|
|
62
|
+
* Two error slots, so a transient engine failure never hides (or, on recovery,
|
|
63
|
+
* silently drops) the verdict on the committed value. The snapshot object is
|
|
64
|
+
* replaced only when a slot changes, so `useSyncExternalStore` sees one change
|
|
65
|
+
* per real change.
|
|
66
|
+
*/
|
|
67
|
+
this.errors = NO_DRAW_ERRORS;
|
|
61
68
|
this.listeners = new Set();
|
|
62
69
|
this.loader = null;
|
|
63
70
|
this.engine = null;
|
|
@@ -65,6 +72,9 @@ class DrawController {
|
|
|
65
72
|
/** Incremented on every connect/disconnect so a slow loader cannot attach a stale engine. */
|
|
66
73
|
this.generation = 0;
|
|
67
74
|
this.loading = false;
|
|
75
|
+
/** While > 0, `notify` only records that something changed; the outermost batch notifies once. */
|
|
76
|
+
this.batchDepth = 0;
|
|
77
|
+
this.batchDirty = false;
|
|
68
78
|
}
|
|
69
79
|
// ---- Adapter lifecycle ----
|
|
70
80
|
/**
|
|
@@ -76,7 +86,7 @@ class DrawController {
|
|
|
76
86
|
this.loader = loader;
|
|
77
87
|
this.ensureEngine();
|
|
78
88
|
}
|
|
79
|
-
/** Unbind from the native map. Mode and
|
|
89
|
+
/** Unbind from the native map. Mode, value and errors are kept for the next `connect`. */
|
|
80
90
|
disconnect() {
|
|
81
91
|
this.releaseEngine();
|
|
82
92
|
this.loader = null;
|
|
@@ -89,13 +99,15 @@ class DrawController {
|
|
|
89
99
|
setMode(mode) {
|
|
90
100
|
if (this.mode === mode)
|
|
91
101
|
return;
|
|
92
|
-
this.
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
this.engine
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
102
|
+
this.batch(() => {
|
|
103
|
+
this.mode = mode;
|
|
104
|
+
this.notify();
|
|
105
|
+
if (this.engine !== null) {
|
|
106
|
+
this.applyToEngine(this.engine, engine => engine.setMode(mode));
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
this.ensureEngine();
|
|
110
|
+
});
|
|
99
111
|
}
|
|
100
112
|
setValue(value) {
|
|
101
113
|
const normalized = normalizeDrawValue(value);
|
|
@@ -104,18 +116,23 @@ class DrawController {
|
|
|
104
116
|
if (drawValuesIdentical(this.value, normalized))
|
|
105
117
|
return;
|
|
106
118
|
const sameGeometry = drawValuesEqual(this.value, normalized);
|
|
107
|
-
this.
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
+
this.batch(() => {
|
|
120
|
+
this.value = normalized;
|
|
121
|
+
this.notify();
|
|
122
|
+
// The engine renders geometry only. A value that merely re-labels ids or
|
|
123
|
+
// properties (a consumer round-tripping through form state) must not clear and
|
|
124
|
+
// redraw the store, which would drop the selection or interrupt a drag.
|
|
125
|
+
if (sameGeometry)
|
|
126
|
+
return;
|
|
127
|
+
// A new geometry gets a fresh verdict: the engine reports again if it still
|
|
128
|
+
// cannot render part of it.
|
|
129
|
+
this.setValueError(null);
|
|
130
|
+
if (this.engine !== null) {
|
|
131
|
+
this.applyToEngine(this.engine, engine => engine.setValue(normalized));
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
this.ensureEngine();
|
|
135
|
+
});
|
|
119
136
|
}
|
|
120
137
|
getMode() {
|
|
121
138
|
return this.mode;
|
|
@@ -123,6 +140,9 @@ class DrawController {
|
|
|
123
140
|
getValue() {
|
|
124
141
|
return this.value;
|
|
125
142
|
}
|
|
143
|
+
getErrors() {
|
|
144
|
+
return this.errors;
|
|
145
|
+
}
|
|
126
146
|
subscribe(listener) {
|
|
127
147
|
this.listeners.add(listener);
|
|
128
148
|
return () => {
|
|
@@ -151,29 +171,84 @@ class DrawController {
|
|
|
151
171
|
}
|
|
152
172
|
this.loading = false;
|
|
153
173
|
this.attachEngine(engine);
|
|
154
|
-
}, (
|
|
174
|
+
}, (cause) => {
|
|
155
175
|
if (generation !== this.generation)
|
|
156
176
|
return;
|
|
157
177
|
this.loading = false;
|
|
158
|
-
this.
|
|
178
|
+
this.setEngineError({ code: "engine-load-failed", cause });
|
|
159
179
|
});
|
|
160
180
|
}
|
|
161
181
|
attachEngine(engine) {
|
|
162
182
|
this.engine = engine;
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
183
|
+
const unsubscribeChange = engine.onChange(rendered => this.adoptEngineValue(engine, rendered));
|
|
184
|
+
const unsubscribeRejection = engine.onValueRejected(rejection => this.setValueError(rejection));
|
|
185
|
+
this.engineUnsubscribe = () => {
|
|
186
|
+
unsubscribeChange();
|
|
187
|
+
unsubscribeRejection();
|
|
188
|
+
};
|
|
189
|
+
this.batch(() => {
|
|
190
|
+
this.setEngineError(null);
|
|
191
|
+
this.applyToEngine(engine, current => {
|
|
192
|
+
current.setValue(this.value);
|
|
193
|
+
current.setMode(this.mode);
|
|
194
|
+
});
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* A user edit arrived from the map. Features the engine could not render are not
|
|
199
|
+
* on the map, so they cannot be part of what the user edited: they stay in the
|
|
200
|
+
* value, ahead of the rendered polygons, and the verdict on them stands.
|
|
201
|
+
*/
|
|
202
|
+
adoptEngineValue(engine, rendered) {
|
|
203
|
+
const kept = this.rejectedFeatures();
|
|
204
|
+
const next = kept.length === 0 ? rendered : { type: "FeatureCollection", features: [...kept, ...rendered.features] };
|
|
205
|
+
if (drawValuesEqual(this.value, next))
|
|
206
|
+
return;
|
|
207
|
+
this.batch(() => {
|
|
208
|
+
this.value = next;
|
|
209
|
+
this.notify();
|
|
210
|
+
const verdict = this.errors.value;
|
|
211
|
+
if (kept.length > 0 && verdict !== null) {
|
|
212
|
+
// Same verdict, same object: a consumer's onError must not re-fire on every edit.
|
|
213
|
+
const indices = kept.map((_, index) => index);
|
|
214
|
+
if (!sameIndices(verdict.rejectedFeatureIndices, indices)) {
|
|
215
|
+
this.errors = { engine: this.errors.engine, value: { ...verdict, rejectedFeatureIndices: indices } };
|
|
216
|
+
this.notify();
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
// Select mode has nothing left to select once the last rendered polygon is
|
|
220
|
+
// deleted, so drop back to idle instead of leaving the map in a dead mode.
|
|
221
|
+
if (rendered.features.length === 0 && this.mode === "select") {
|
|
170
222
|
this.mode = "idle";
|
|
171
|
-
|
|
223
|
+
this.notify();
|
|
224
|
+
this.applyToEngine(engine, current => current.setMode("idle"));
|
|
172
225
|
}
|
|
173
|
-
this.notify();
|
|
174
226
|
});
|
|
175
|
-
|
|
176
|
-
|
|
227
|
+
}
|
|
228
|
+
rejectedFeatures() {
|
|
229
|
+
const rejection = this.errors.value;
|
|
230
|
+
if (rejection === null)
|
|
231
|
+
return [];
|
|
232
|
+
return rejection.rejectedFeatureIndices.flatMap(index => {
|
|
233
|
+
const feature = this.value.features[index];
|
|
234
|
+
return feature === undefined ? [] : [feature];
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* Runs an engine operation. A throwing engine (Terra Draw with a map that is mid
|
|
239
|
+
* teardown, a provider adapter that lost its style) is reported as
|
|
240
|
+
* `engine-update-failed` instead of propagating into React state updates; the
|
|
241
|
+
* committed mode and value are untouched and apply again on the next reconnect.
|
|
242
|
+
*/
|
|
243
|
+
applyToEngine(engine, operation) {
|
|
244
|
+
try {
|
|
245
|
+
operation(engine);
|
|
246
|
+
if (this.errors.engine?.code === "engine-update-failed")
|
|
247
|
+
this.setEngineError(null);
|
|
248
|
+
}
|
|
249
|
+
catch (cause) {
|
|
250
|
+
this.setEngineError({ code: "engine-update-failed", cause });
|
|
251
|
+
}
|
|
177
252
|
}
|
|
178
253
|
releaseEngine() {
|
|
179
254
|
this.generation += 1;
|
|
@@ -183,7 +258,47 @@ class DrawController {
|
|
|
183
258
|
this.engine?.destroy();
|
|
184
259
|
this.engine = null;
|
|
185
260
|
}
|
|
261
|
+
/**
|
|
262
|
+
* Replaces the verdict on the committed value. The same verdict again (a rebuilt
|
|
263
|
+
* engine re-applying the same value after a reconnect) keeps the existing object,
|
|
264
|
+
* so subscribers are not told about a change that did not happen.
|
|
265
|
+
*/
|
|
266
|
+
setValueError(rejection) {
|
|
267
|
+
const current = this.errors.value;
|
|
268
|
+
if (current === rejection)
|
|
269
|
+
return;
|
|
270
|
+
if (current !== null &&
|
|
271
|
+
rejection !== null &&
|
|
272
|
+
sameIndices(current.rejectedFeatureIndices, rejection.rejectedFeatureIndices)) {
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
this.errors = { engine: this.errors.engine, value: rejection };
|
|
276
|
+
this.notify();
|
|
277
|
+
}
|
|
278
|
+
setEngineError(error) {
|
|
279
|
+
if (this.errors.engine === error)
|
|
280
|
+
return;
|
|
281
|
+
this.errors = { engine: error, value: this.errors.value };
|
|
282
|
+
this.notify();
|
|
283
|
+
}
|
|
284
|
+
batch(operation) {
|
|
285
|
+
this.batchDepth += 1;
|
|
286
|
+
try {
|
|
287
|
+
operation();
|
|
288
|
+
}
|
|
289
|
+
finally {
|
|
290
|
+
this.batchDepth -= 1;
|
|
291
|
+
if (this.batchDepth === 0 && this.batchDirty) {
|
|
292
|
+
this.batchDirty = false;
|
|
293
|
+
this.listeners.forEach(listener => listener());
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
}
|
|
186
297
|
notify() {
|
|
298
|
+
if (this.batchDepth > 0) {
|
|
299
|
+
this.batchDirty = true;
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
187
302
|
this.listeners.forEach(listener => listener());
|
|
188
303
|
}
|
|
189
304
|
}
|
package/package.json
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trackunit/react-map-draw",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.5",
|
|
4
4
|
"repository": "https://github.com/Trackunit/manager",
|
|
5
5
|
"license": "SEE LICENSE IN LICENSE.txt",
|
|
6
6
|
"engines": {
|
|
7
7
|
"node": ">=24.x"
|
|
8
8
|
},
|
|
9
9
|
"dependencies": {
|
|
10
|
-
"@trackunit/react-map-adapter-shared": "0.0.
|
|
11
|
-
"@trackunit/geo-json-utils": "1.
|
|
10
|
+
"@trackunit/react-map-adapter-shared": "0.0.167",
|
|
11
|
+
"@trackunit/geo-json-utils": "1.21.0",
|
|
12
12
|
"es-toolkit": "^1.52.0",
|
|
13
13
|
"terra-draw": "^1.36.0"
|
|
14
14
|
},
|
package/src/DrawController.d.ts
CHANGED
|
@@ -1,27 +1,30 @@
|
|
|
1
|
-
import { type DrawMode, type DrawPort, type DrawValue } from "@trackunit/react-map-adapter-shared";
|
|
1
|
+
import { type DrawErrors, type DrawMode, type DrawPort, type DrawValue } from "@trackunit/react-map-adapter-shared";
|
|
2
2
|
import type { DrawEngineLoader } from "./drawEngine";
|
|
3
|
-
export type DrawControllerOptions = Readonly<{
|
|
4
|
-
/**
|
|
5
|
-
* Called when the engine loader rejects (for example the dynamic import failed).
|
|
6
|
-
* The controller keeps its mode and value and loads again on the next `connect`,
|
|
7
|
-
* `setMode` or `setValue` that needs an engine.
|
|
8
|
-
*/
|
|
9
|
-
onEngineError?: (error: unknown) => void;
|
|
10
|
-
}>;
|
|
11
3
|
/**
|
|
12
4
|
* Provider-agnostic `DrawPort` implementation (ADR 0030).
|
|
13
5
|
*
|
|
14
|
-
* The controller is the source of truth for `mode` and `
|
|
15
|
-
* resources itself: adapters call `connect(loader)` whenever they have a
|
|
16
|
-
* to draw on (first connect, and again after every recreation or style
|
|
17
|
-
* `disconnect()` when the native map goes away. The engine is created
|
|
18
|
-
* once the drawing needs one (non-idle mode or a non-empty value), so
|
|
19
|
-
* draw never load Terra Draw.
|
|
6
|
+
* The controller is the source of truth for `mode`, `value` and `errors`. It owns no
|
|
7
|
+
* native resources itself: adapters call `connect(loader)` whenever they have a
|
|
8
|
+
* native map to draw on (first connect, and again after every recreation or style
|
|
9
|
+
* reload) and `disconnect()` when the native map goes away. The engine is created
|
|
10
|
+
* lazily, only once the drawing needs one (non-idle mode or a non-empty value), so
|
|
11
|
+
* maps that never draw never load Terra Draw.
|
|
12
|
+
*
|
|
13
|
+
* The committed value is never rewritten from the map: features the engine cannot
|
|
14
|
+
* render stay in `value` and surface as a `value-rejected` error, so a stored
|
|
15
|
+
* boundary opened for editing is never silently replaced by an empty one. A user
|
|
16
|
+
* edit keeps those features too, ahead of whatever the user drew.
|
|
20
17
|
*/
|
|
21
18
|
export declare class DrawController implements DrawPort {
|
|
22
|
-
private readonly options;
|
|
23
19
|
private mode;
|
|
24
20
|
private value;
|
|
21
|
+
/**
|
|
22
|
+
* Two error slots, so a transient engine failure never hides (or, on recovery,
|
|
23
|
+
* silently drops) the verdict on the committed value. The snapshot object is
|
|
24
|
+
* replaced only when a slot changes, so `useSyncExternalStore` sees one change
|
|
25
|
+
* per real change.
|
|
26
|
+
*/
|
|
27
|
+
private errors;
|
|
25
28
|
private readonly listeners;
|
|
26
29
|
private loader;
|
|
27
30
|
private engine;
|
|
@@ -29,13 +32,15 @@ export declare class DrawController implements DrawPort {
|
|
|
29
32
|
/** Incremented on every connect/disconnect so a slow loader cannot attach a stale engine. */
|
|
30
33
|
private generation;
|
|
31
34
|
private loading;
|
|
32
|
-
|
|
35
|
+
/** While > 0, `notify` only records that something changed; the outermost batch notifies once. */
|
|
36
|
+
private batchDepth;
|
|
37
|
+
private batchDirty;
|
|
33
38
|
/**
|
|
34
39
|
* Bind to a native map. Destroys any previous engine and, if the current mode or
|
|
35
40
|
* value needs one, builds a fresh engine from the committed value.
|
|
36
41
|
*/
|
|
37
42
|
connect(loader: DrawEngineLoader): void;
|
|
38
|
-
/** Unbind from the native map. Mode and
|
|
43
|
+
/** Unbind from the native map. Mode, value and errors are kept for the next `connect`. */
|
|
39
44
|
disconnect(): void;
|
|
40
45
|
/** True between `connect` and `disconnect`, whether or not an engine has been loaded yet. */
|
|
41
46
|
isConnected(): boolean;
|
|
@@ -43,10 +48,33 @@ export declare class DrawController implements DrawPort {
|
|
|
43
48
|
setValue(value: DrawValue): void;
|
|
44
49
|
getMode(): DrawMode;
|
|
45
50
|
getValue(): DrawValue;
|
|
51
|
+
getErrors(): DrawErrors;
|
|
46
52
|
subscribe(listener: () => void): () => void;
|
|
47
53
|
private needsEngine;
|
|
48
54
|
private ensureEngine;
|
|
49
55
|
private attachEngine;
|
|
56
|
+
/**
|
|
57
|
+
* A user edit arrived from the map. Features the engine could not render are not
|
|
58
|
+
* on the map, so they cannot be part of what the user edited: they stay in the
|
|
59
|
+
* value, ahead of the rendered polygons, and the verdict on them stands.
|
|
60
|
+
*/
|
|
61
|
+
private adoptEngineValue;
|
|
62
|
+
private rejectedFeatures;
|
|
63
|
+
/**
|
|
64
|
+
* Runs an engine operation. A throwing engine (Terra Draw with a map that is mid
|
|
65
|
+
* teardown, a provider adapter that lost its style) is reported as
|
|
66
|
+
* `engine-update-failed` instead of propagating into React state updates; the
|
|
67
|
+
* committed mode and value are untouched and apply again on the next reconnect.
|
|
68
|
+
*/
|
|
69
|
+
private applyToEngine;
|
|
50
70
|
private releaseEngine;
|
|
71
|
+
/**
|
|
72
|
+
* Replaces the verdict on the committed value. The same verdict again (a rebuilt
|
|
73
|
+
* engine re-applying the same value after a reconnect) keeps the existing object,
|
|
74
|
+
* so subscribers are not told about a change that did not happen.
|
|
75
|
+
*/
|
|
76
|
+
private setValueError;
|
|
77
|
+
private setEngineError;
|
|
78
|
+
private batch;
|
|
51
79
|
private notify;
|
|
52
80
|
}
|
package/src/drawEngine.d.ts
CHANGED
|
@@ -1,19 +1,28 @@
|
|
|
1
|
-
import type { DrawMode, DrawValue } from "@trackunit/react-map-adapter-shared";
|
|
1
|
+
import type { DrawMode, DrawValue, DrawValueRejection } from "@trackunit/react-map-adapter-shared";
|
|
2
2
|
/**
|
|
3
3
|
* The provider-bound half of the drawing capability. One engine instance is bound to
|
|
4
4
|
* one native map instance (or one Mapbox style); the `DrawController` creates a new
|
|
5
5
|
* engine through the loader on every reconnect and destroys the previous one.
|
|
6
6
|
*/
|
|
7
7
|
export type DrawEngine = Readonly<{
|
|
8
|
-
/** Apply a mode. Called after `setValue` on a fresh engine. */
|
|
8
|
+
/** Apply a mode. Called after `setValue` on a fresh engine. May throw; the controller reports it. */
|
|
9
9
|
setMode: (mode: DrawMode) => void;
|
|
10
|
-
/**
|
|
10
|
+
/**
|
|
11
|
+
* Replace all rendered polygons with the given committed value. Features the engine
|
|
12
|
+
* cannot render are skipped and reported through `onValueRejected`; the rendered
|
|
13
|
+
* remainder is never echoed through `onChange`.
|
|
14
|
+
*/
|
|
11
15
|
setValue: (value: DrawValue) => void;
|
|
12
16
|
/**
|
|
13
17
|
* Called when the user finishes, edits or deletes a polygon. The engine must not
|
|
14
18
|
* echo values it received through `setValue`.
|
|
15
19
|
*/
|
|
16
20
|
onChange: (listener: (value: DrawValue) => void) => () => void;
|
|
21
|
+
/**
|
|
22
|
+
* Called after a `setValue` that could not render every feature, with the indices
|
|
23
|
+
* of the skipped features in the given value (ADR 0030, decided 19 Oct 2026).
|
|
24
|
+
*/
|
|
25
|
+
onValueRejected: (listener: (rejection: DrawValueRejection) => void) => () => void;
|
|
17
26
|
/** Release the engine's native resources. Safe to call when the native map is gone. */
|
|
18
27
|
destroy: () => void;
|
|
19
28
|
}>;
|
package/src/drawValue.d.ts
CHANGED
|
@@ -7,9 +7,11 @@ import { type DrawValue } from "@trackunit/react-map-adapter-shared";
|
|
|
7
7
|
*/
|
|
8
8
|
export declare const isDrawablePolygonFeature: (feature: GeoJsonFeature) => boolean;
|
|
9
9
|
/**
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
10
|
+
* Canonical form of a committed value: an empty collection becomes the shared
|
|
11
|
+
* `EMPTY_DRAW_VALUE`, anything else is returned by reference so controlled-input
|
|
12
|
+
* round trips keep identity. Features the engine cannot render are kept, not
|
|
13
|
+
* dropped: the engine reports them as `value-rejected` and the committed value
|
|
14
|
+
* stays what the consumer set (ADR 0030, decided 19 Oct 2026).
|
|
13
15
|
*/
|
|
14
16
|
export declare const normalizeDrawValue: (value: DrawValue) => DrawValue;
|
|
15
17
|
/** Full feature equality (ids, properties and geometry). Used for controlled-input updates. */
|
package/src/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { DrawController
|
|
1
|
+
export { DrawController } from "./DrawController";
|
|
2
2
|
export type { DrawEngine, DrawEngineLoader } from "./drawEngine";
|
|
3
3
|
export { DEFAULT_DRAW_STYLE, DRAW_COORDINATE_PRECISION, type DrawStyle } from "./drawStyle";
|
|
4
4
|
export { drawValuesEqual, isDrawablePolygonFeature, normalizeDrawValue } from "./drawValue";
|
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import { type DrawPort, type DrawValue } from "@trackunit/react-map-adapter-shared";
|
|
2
2
|
export declare const SQUARE: DrawValue;
|
|
3
3
|
export declare const TRIANGLE: DrawValue;
|
|
4
|
-
/**
|
|
4
|
+
/**
|
|
5
|
+
* A polygon with a hole. The engine cannot render it (sites have no holes), so the
|
|
6
|
+
* port keeps it in the value and reports `value-rejected` once an engine applies it.
|
|
7
|
+
*/
|
|
5
8
|
export declare const HOLED: DrawValue;
|
|
6
9
|
/**
|
|
7
10
|
* Behavioural contract every `DrawPort` implementation must satisfy, independent of
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Test-only entry point (`@trackunit/react-map-draw/testing`): the `DrawPort`
|
|
3
|
+
* behavioural contract and fixtures, plus the fake Terra Draw adapter. Kept out of
|
|
4
|
+
* the package index because it imports vitest.
|
|
5
|
+
*/
|
|
6
|
+
export { HOLED, SQUARE, TRIANGLE, describeDrawPortContract } from "./drawPortContract";
|
|
7
|
+
export { FakeTerraDrawAdapter } from "./FakeTerraDrawAdapter";
|
package/terraDrawEngine.cjs.js
CHANGED
|
@@ -27,42 +27,25 @@ const roundCoordinate = (value) => {
|
|
|
27
27
|
return Math.round(value * factor) / factor;
|
|
28
28
|
};
|
|
29
29
|
/**
|
|
30
|
-
* Converts committed
|
|
31
|
-
*
|
|
32
|
-
*
|
|
30
|
+
* Converts one committed polygon into a Terra Draw store feature, or null when the
|
|
31
|
+
* feature is not a single-ring polygon (holes, points, open rings). Ids are dropped
|
|
32
|
+
* so the store assigns its own (Terra Draw validates ids against its id strategy),
|
|
33
|
+
* and coordinates are rounded to the store precision so validation does not reject
|
|
33
34
|
* high-precision input.
|
|
34
35
|
*/
|
|
35
|
-
const
|
|
36
|
+
const toStoreFeature = (feature) => {
|
|
36
37
|
const geometry = feature.geometry;
|
|
37
|
-
if (geometry === null || geometry.type !== "Polygon")
|
|
38
|
-
return
|
|
39
|
-
return
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
coordinates: geometry.coordinates.map(ring => ring.map(([lng, lat]) => [roundCoordinate(lng), roundCoordinate(lat)])),
|
|
45
|
-
},
|
|
46
|
-
properties: { mode: POLYGON_MODE },
|
|
38
|
+
if (geometry === null || geometry.type !== "Polygon" || !index.isDrawablePolygonFeature(feature))
|
|
39
|
+
return null;
|
|
40
|
+
return {
|
|
41
|
+
type: "Feature",
|
|
42
|
+
geometry: {
|
|
43
|
+
type: "Polygon",
|
|
44
|
+
coordinates: geometry.coordinates.map(ring => ring.map(([lng, lat]) => [roundCoordinate(lng), roundCoordinate(lat)])),
|
|
47
45
|
},
|
|
48
|
-
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
const roundDrawValue = (value) => ({
|
|
52
|
-
type: "FeatureCollection",
|
|
53
|
-
features: value.features.map(feature => {
|
|
54
|
-
const geometry = feature.geometry;
|
|
55
|
-
if (geometry === null || geometry.type !== "Polygon")
|
|
56
|
-
return feature;
|
|
57
|
-
return {
|
|
58
|
-
...feature,
|
|
59
|
-
geometry: {
|
|
60
|
-
type: "Polygon",
|
|
61
|
-
coordinates: geometry.coordinates.map(ring => ring.map(([lng, lat]) => [roundCoordinate(lng), roundCoordinate(lat)])),
|
|
62
|
-
},
|
|
63
|
-
};
|
|
64
|
-
}),
|
|
65
|
-
});
|
|
46
|
+
properties: { mode: POLYGON_MODE },
|
|
47
|
+
};
|
|
48
|
+
};
|
|
66
49
|
const toPosition = (position) => {
|
|
67
50
|
const lng = position[0];
|
|
68
51
|
const lat = position[1];
|
|
@@ -169,28 +152,53 @@ const createTerraDrawEngine = ({ adapter, style }) => {
|
|
|
169
152
|
let desiredValue = reactMapAdapterShared.EMPTY_DRAW_VALUE;
|
|
170
153
|
let lastEmitted = reactMapAdapterShared.EMPTY_DRAW_VALUE;
|
|
171
154
|
const listeners = new Set();
|
|
155
|
+
const rejectionListeners = new Set();
|
|
172
156
|
/**
|
|
173
|
-
* Replaces the store with the desired value.
|
|
174
|
-
*
|
|
175
|
-
*
|
|
176
|
-
*
|
|
157
|
+
* Replaces the store with the desired value. Features that are not single-ring
|
|
158
|
+
* polygons never reach the store, and Terra Draw validates the rest on its own
|
|
159
|
+
* terms (a self-intersecting ring fails `ValidateNotSelfIntersecting`). Whatever
|
|
160
|
+
* was skipped is reported by index through `onValueRejected`; the committed value
|
|
161
|
+
* is never rewritten from the map (ADR 0030, decided 19 Oct 2026).
|
|
177
162
|
*/
|
|
178
163
|
const applyValue = () => {
|
|
164
|
+
const rejectedFeatureIndices = [];
|
|
165
|
+
const reasons = [];
|
|
166
|
+
const storeFeatures = [];
|
|
167
|
+
const valueIndexByStoreIndex = [];
|
|
168
|
+
desiredValue.features.forEach((feature, index) => {
|
|
169
|
+
const storeFeature = toStoreFeature(feature);
|
|
170
|
+
if (storeFeature === null) {
|
|
171
|
+
rejectedFeatureIndices.push(index);
|
|
172
|
+
reasons.push(`feature ${index}: not a closed single-ring polygon`);
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
storeFeatures.push(storeFeature);
|
|
176
|
+
valueIndexByStoreIndex.push(index);
|
|
177
|
+
});
|
|
179
178
|
applying = true;
|
|
179
|
+
let validations = [];
|
|
180
180
|
try {
|
|
181
181
|
draw.clear();
|
|
182
|
-
draw.addFeatures(
|
|
182
|
+
validations = draw.addFeatures(storeFeatures);
|
|
183
183
|
}
|
|
184
184
|
finally {
|
|
185
185
|
applying = false;
|
|
186
186
|
}
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
187
|
+
validations.forEach((validation, storeIndex) => {
|
|
188
|
+
if (validation.valid)
|
|
189
|
+
return;
|
|
190
|
+
const index = valueIndexByStoreIndex[storeIndex];
|
|
191
|
+
if (index === undefined)
|
|
192
|
+
return;
|
|
193
|
+
rejectedFeatureIndices.push(index);
|
|
194
|
+
reasons.push(`feature ${index}: ${validation.reason ?? "rejected by the engine"}`);
|
|
195
|
+
});
|
|
196
|
+
lastEmitted = toDrawValue(draw.getSnapshot());
|
|
197
|
+
if (rejectedFeatureIndices.length === 0)
|
|
198
|
+
return;
|
|
199
|
+
rejectedFeatureIndices.sort((a, b) => a - b);
|
|
200
|
+
const rejection = { code: "value-rejected", reasons, rejectedFeatureIndices };
|
|
201
|
+
rejectionListeners.forEach(listener => listener(rejection));
|
|
194
202
|
};
|
|
195
203
|
const applyMode = () => {
|
|
196
204
|
const target = toTerraDrawMode(desiredMode);
|
|
@@ -254,11 +262,18 @@ const createTerraDrawEngine = ({ adapter, style }) => {
|
|
|
254
262
|
listeners.delete(listener);
|
|
255
263
|
};
|
|
256
264
|
},
|
|
265
|
+
onValueRejected: listener => {
|
|
266
|
+
rejectionListeners.add(listener);
|
|
267
|
+
return () => {
|
|
268
|
+
rejectionListeners.delete(listener);
|
|
269
|
+
};
|
|
270
|
+
},
|
|
257
271
|
destroy: () => {
|
|
258
272
|
if (destroyed)
|
|
259
273
|
return;
|
|
260
274
|
destroyed = true;
|
|
261
275
|
listeners.clear();
|
|
276
|
+
rejectionListeners.clear();
|
|
262
277
|
draw.off("change", handleChange);
|
|
263
278
|
draw.off("finish", handleFinish);
|
|
264
279
|
draw.off("ready", handleReady);
|
package/terraDrawEngine.esm.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { EMPTY_DRAW_VALUE } from '@trackunit/react-map-adapter-shared';
|
|
2
2
|
import { TerraDrawPolygonMode, ValidateNotSelfIntersecting, TerraDrawSelectMode, TerraDraw } from 'terra-draw';
|
|
3
|
-
import { DEFAULT_DRAW_STYLE,
|
|
3
|
+
import { DEFAULT_DRAW_STYLE, isDrawablePolygonFeature, DRAW_COORDINATE_PRECISION, drawValuesEqual } from './index.esm.js';
|
|
4
4
|
import '@trackunit/geo-json-utils';
|
|
5
5
|
import 'es-toolkit';
|
|
6
6
|
|
|
@@ -25,42 +25,25 @@ const roundCoordinate = (value) => {
|
|
|
25
25
|
return Math.round(value * factor) / factor;
|
|
26
26
|
};
|
|
27
27
|
/**
|
|
28
|
-
* Converts committed
|
|
29
|
-
*
|
|
30
|
-
*
|
|
28
|
+
* Converts one committed polygon into a Terra Draw store feature, or null when the
|
|
29
|
+
* feature is not a single-ring polygon (holes, points, open rings). Ids are dropped
|
|
30
|
+
* so the store assigns its own (Terra Draw validates ids against its id strategy),
|
|
31
|
+
* and coordinates are rounded to the store precision so validation does not reject
|
|
31
32
|
* high-precision input.
|
|
32
33
|
*/
|
|
33
|
-
const
|
|
34
|
+
const toStoreFeature = (feature) => {
|
|
34
35
|
const geometry = feature.geometry;
|
|
35
|
-
if (geometry === null || geometry.type !== "Polygon")
|
|
36
|
-
return
|
|
37
|
-
return
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
coordinates: geometry.coordinates.map(ring => ring.map(([lng, lat]) => [roundCoordinate(lng), roundCoordinate(lat)])),
|
|
43
|
-
},
|
|
44
|
-
properties: { mode: POLYGON_MODE },
|
|
36
|
+
if (geometry === null || geometry.type !== "Polygon" || !isDrawablePolygonFeature(feature))
|
|
37
|
+
return null;
|
|
38
|
+
return {
|
|
39
|
+
type: "Feature",
|
|
40
|
+
geometry: {
|
|
41
|
+
type: "Polygon",
|
|
42
|
+
coordinates: geometry.coordinates.map(ring => ring.map(([lng, lat]) => [roundCoordinate(lng), roundCoordinate(lat)])),
|
|
45
43
|
},
|
|
46
|
-
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
const roundDrawValue = (value) => ({
|
|
50
|
-
type: "FeatureCollection",
|
|
51
|
-
features: value.features.map(feature => {
|
|
52
|
-
const geometry = feature.geometry;
|
|
53
|
-
if (geometry === null || geometry.type !== "Polygon")
|
|
54
|
-
return feature;
|
|
55
|
-
return {
|
|
56
|
-
...feature,
|
|
57
|
-
geometry: {
|
|
58
|
-
type: "Polygon",
|
|
59
|
-
coordinates: geometry.coordinates.map(ring => ring.map(([lng, lat]) => [roundCoordinate(lng), roundCoordinate(lat)])),
|
|
60
|
-
},
|
|
61
|
-
};
|
|
62
|
-
}),
|
|
63
|
-
});
|
|
44
|
+
properties: { mode: POLYGON_MODE },
|
|
45
|
+
};
|
|
46
|
+
};
|
|
64
47
|
const toPosition = (position) => {
|
|
65
48
|
const lng = position[0];
|
|
66
49
|
const lat = position[1];
|
|
@@ -167,28 +150,53 @@ const createTerraDrawEngine = ({ adapter, style }) => {
|
|
|
167
150
|
let desiredValue = EMPTY_DRAW_VALUE;
|
|
168
151
|
let lastEmitted = EMPTY_DRAW_VALUE;
|
|
169
152
|
const listeners = new Set();
|
|
153
|
+
const rejectionListeners = new Set();
|
|
170
154
|
/**
|
|
171
|
-
* Replaces the store with the desired value.
|
|
172
|
-
*
|
|
173
|
-
*
|
|
174
|
-
*
|
|
155
|
+
* Replaces the store with the desired value. Features that are not single-ring
|
|
156
|
+
* polygons never reach the store, and Terra Draw validates the rest on its own
|
|
157
|
+
* terms (a self-intersecting ring fails `ValidateNotSelfIntersecting`). Whatever
|
|
158
|
+
* was skipped is reported by index through `onValueRejected`; the committed value
|
|
159
|
+
* is never rewritten from the map (ADR 0030, decided 19 Oct 2026).
|
|
175
160
|
*/
|
|
176
161
|
const applyValue = () => {
|
|
162
|
+
const rejectedFeatureIndices = [];
|
|
163
|
+
const reasons = [];
|
|
164
|
+
const storeFeatures = [];
|
|
165
|
+
const valueIndexByStoreIndex = [];
|
|
166
|
+
desiredValue.features.forEach((feature, index) => {
|
|
167
|
+
const storeFeature = toStoreFeature(feature);
|
|
168
|
+
if (storeFeature === null) {
|
|
169
|
+
rejectedFeatureIndices.push(index);
|
|
170
|
+
reasons.push(`feature ${index}: not a closed single-ring polygon`);
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
storeFeatures.push(storeFeature);
|
|
174
|
+
valueIndexByStoreIndex.push(index);
|
|
175
|
+
});
|
|
177
176
|
applying = true;
|
|
177
|
+
let validations = [];
|
|
178
178
|
try {
|
|
179
179
|
draw.clear();
|
|
180
|
-
draw.addFeatures(
|
|
180
|
+
validations = draw.addFeatures(storeFeatures);
|
|
181
181
|
}
|
|
182
182
|
finally {
|
|
183
183
|
applying = false;
|
|
184
184
|
}
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
185
|
+
validations.forEach((validation, storeIndex) => {
|
|
186
|
+
if (validation.valid)
|
|
187
|
+
return;
|
|
188
|
+
const index = valueIndexByStoreIndex[storeIndex];
|
|
189
|
+
if (index === undefined)
|
|
190
|
+
return;
|
|
191
|
+
rejectedFeatureIndices.push(index);
|
|
192
|
+
reasons.push(`feature ${index}: ${validation.reason ?? "rejected by the engine"}`);
|
|
193
|
+
});
|
|
194
|
+
lastEmitted = toDrawValue(draw.getSnapshot());
|
|
195
|
+
if (rejectedFeatureIndices.length === 0)
|
|
196
|
+
return;
|
|
197
|
+
rejectedFeatureIndices.sort((a, b) => a - b);
|
|
198
|
+
const rejection = { code: "value-rejected", reasons, rejectedFeatureIndices };
|
|
199
|
+
rejectionListeners.forEach(listener => listener(rejection));
|
|
192
200
|
};
|
|
193
201
|
const applyMode = () => {
|
|
194
202
|
const target = toTerraDrawMode(desiredMode);
|
|
@@ -252,11 +260,18 @@ const createTerraDrawEngine = ({ adapter, style }) => {
|
|
|
252
260
|
listeners.delete(listener);
|
|
253
261
|
};
|
|
254
262
|
},
|
|
263
|
+
onValueRejected: listener => {
|
|
264
|
+
rejectionListeners.add(listener);
|
|
265
|
+
return () => {
|
|
266
|
+
rejectionListeners.delete(listener);
|
|
267
|
+
};
|
|
268
|
+
},
|
|
255
269
|
destroy: () => {
|
|
256
270
|
if (destroyed)
|
|
257
271
|
return;
|
|
258
272
|
destroyed = true;
|
|
259
273
|
listeners.clear();
|
|
274
|
+
rejectionListeners.clear();
|
|
260
275
|
draw.off("change", handleChange);
|
|
261
276
|
draw.off("finish", handleFinish);
|
|
262
277
|
draw.off("ready", handleReady);
|