Skip to content

Commit d9476c6

Browse files
committed
fix: load existing geometry into MapboxDraw for interactive editing
Existing geometry was added as a static map source/layer, making it non-interactive: shapes couldn't be clicked, selected, or edited. Now geometry is loaded into the MapboxDraw control via draw.add(), enabling click-to-select, vertex editing, and trash deletion. Also handles draw.delete event to clear the field value.
1 parent ff70475 commit d9476c6

1 file changed

Lines changed: 32 additions & 10 deletions

File tree

spp_gis/static/src/js/widgets/gis_edit_map/field_gis_edit_map.esm.js

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,12 @@ export class FieldGisEditMap extends Component {
5151
});
5252

5353
this.renderMap();
54-
this.onLoadMap();
5554
this.addDrawInteraction();
5655
});
5756

5857
onPatched(() => {
5958
this.defaultZoom = this.map.getZoom();
6059
this.renderMap();
61-
this.onLoadMap();
6260
this.addDrawInteraction();
6361
});
6462
}
@@ -239,19 +237,24 @@ export class FieldGisEditMap extends Component {
239237
}
240238

241239
onUIChange() {
242-
this.removeSourceAndLayer(this.sourceId);
243-
this.onLoadMap();
244240
this.addDrawInteraction();
245241
}
246242

247243
addDrawInteraction() {
248244
const self = this;
245+
const hasData = Boolean(this.props.record.data[this.props.name]);
249246

250247
function updateArea(e) {
251248
var data = self.draw.getAll();
252-
self.props.record.update({
253-
[self.props.name]: JSON.stringify(data.features[0].geometry),
254-
});
249+
if (data.features.length > 0) {
250+
self.props.record.update({
251+
[self.props.name]: JSON.stringify(data.features[0].geometry),
252+
});
253+
}
254+
}
255+
256+
function deleteArea(e) {
257+
self.props.record.update({[self.props.name]: null});
255258
}
256259

257260
if (this.draw) {
@@ -261,11 +264,11 @@ export class FieldGisEditMap extends Component {
261264
this.draw = new MapboxDraw({
262265
displayControlsDefault: false,
263266
controls: {
264-
[this.drawControl]: !this.props.record.data[this.props.name],
265-
trash: Boolean(this.props.record.data[this.props.name]),
267+
[this.drawControl]: !hasData,
268+
trash: hasData,
266269
},
267270
styles: this.addDrawInteractionStyle(),
268-
defaultMode: "custom_mode",
271+
defaultMode: hasData ? "simple_select" : "custom_mode",
269272
modes: Object.assign(
270273
{
271274
custom_mode: this.addDrawCustomModes(),
@@ -282,8 +285,27 @@ export class FieldGisEditMap extends Component {
282285
elem.classList.add("maplibregl-ctrl", "maplibregl-ctrl-group");
283286
});
284287

288+
// Load existing geometry into MapboxDraw so it's interactive
289+
// (clickable, editable, deletable) instead of a static layer
290+
if (hasData) {
291+
const loadExisting = () => {
292+
const geom = JSON.parse(this.props.record.data[this.props.name]);
293+
this.draw.add({
294+
type: "Feature",
295+
geometry: geom,
296+
properties: {},
297+
});
298+
};
299+
if (this.map.loaded()) {
300+
loadExisting();
301+
} else {
302+
this.map.on("load", loadExisting);
303+
}
304+
}
305+
285306
this.map.on("draw.create", updateArea);
286307
this.map.on("draw.update", updateArea);
308+
this.map.on("draw.delete", deleteArea);
287309
}
288310

289311
addDrawInteractionStyle() {

0 commit comments

Comments
 (0)