Skip to content

Commit cf58748

Browse files
committed
Allow deleting the whole feature from direct_select
When no vertices are selected, trash deletes the whole feature.
1 parent 405e3bf commit cf58748

File tree

2 files changed

+36
-10
lines changed

2 files changed

+36
-10
lines changed

src/modes/direct_select.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -164,18 +164,23 @@ DirectSelect.toDisplayFeatures = function(state, geojson, push) {
164164
};
165165

166166
DirectSelect.onTrash = function(state) {
167-
// Uses number-aware sorting to make sure '9' < '10'. Comparison is reversed because we want them
168-
// in reverse order so that we can remove by index safely.
169-
state.selectedCoordPaths
170-
.sort((a, b) => b.localeCompare(a, 'en', { numeric: true }))
171-
.forEach(id => state.feature.removeCoordinate(id));
172-
this.fireUpdate();
173-
state.selectedCoordPaths = [];
174-
this.clearSelectedCoordinates();
175-
this.fireActionable(state);
176-
if (state.feature.isValid() === false) {
167+
if (state.selectedCoordPaths.length === 0) {
177168
this.deleteFeature([state.featureId]);
178169
this.changeMode(Constants.modes.SIMPLE_SELECT, {});
170+
} else {
171+
// Uses number-aware sorting to make sure '9' < '10'. Comparison is reversed because we want them
172+
// in reverse order so that we can remove by index safely.
173+
state.selectedCoordPaths
174+
.sort((a, b) => b.localeCompare(a, 'en', { numeric: true }))
175+
.forEach(id => state.feature.removeCoordinate(id));
176+
this.fireUpdate();
177+
state.selectedCoordPaths = [];
178+
this.clearSelectedCoordinates();
179+
this.fireActionable(state);
180+
if (state.feature.isValid() === false) {
181+
this.deleteFeature([state.featureId]);
182+
this.changeMode(Constants.modes.SIMPLE_SELECT, {});
183+
}
179184
}
180185
};
181186

test/direct_select.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,27 @@ test('direct_select', (t) => {
148148
});
149149
});
150150

151+
t.test('direct_select - trashing with no vertices selected should delete the feature', (st) => {
152+
const longLine = {
153+
type: 'Feature',
154+
properties: {},
155+
geometry: {
156+
type: 'LineString',
157+
coordinates: [[0, 0], [10, 0], [20, 0], [30, 0], [40, 0], [50, 0], [60, 0], [70, 0], [80, 0], [80, 10], [70, 10], [60, 10], [50, 10]]
158+
}
159+
};
160+
const ids = Draw.add(longLine);
161+
Draw.changeMode(Constants.modes.DIRECT_SELECT, {
162+
featureId: ids[0]
163+
});
164+
afterNextRender(() => {
165+
Draw.trash();
166+
const afterTrash = Draw.get(ids[0]);
167+
st.deepEqual(afterTrash, undefined);
168+
cleanUp(() => st.end());
169+
});
170+
});
171+
151172
t.test('direct_select - a click on a vertex and than dragging the map shouldn\'t drag the vertex', (st) => {
152173
const ids = Draw.add(getGeoJSON('polygon'));
153174
Draw.changeMode(Constants.modes.DIRECT_SELECT, {

0 commit comments

Comments
 (0)