Skip to content

Commit

Permalink
Allow deleting the whole feature from direct_select
Browse files Browse the repository at this point in the history
When no vertices are selected, trash deletes the whole feature.
  • Loading branch information
trygveaa committed Mar 20, 2020
1 parent 405e3bf commit a4c3ab8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
25 changes: 15 additions & 10 deletions src/modes/direct_select.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,18 +164,23 @@ DirectSelect.toDisplayFeatures = function(state, geojson, push) {
};

DirectSelect.onTrash = function(state) {
// Uses number-aware sorting to make sure '9' < '10'. Comparison is reversed because we want them
// in reverse order so that we can remove by index safely.
state.selectedCoordPaths
.sort((a, b) => b.localeCompare(a, 'en', { numeric: true }))
.forEach(id => state.feature.removeCoordinate(id));
this.fireUpdate();
state.selectedCoordPaths = [];
this.clearSelectedCoordinates();
this.fireActionable(state);
if (state.feature.isValid() === false) {
if (state.selectedCoordPaths.length === 0) {
this.deleteFeature([state.featureId]);
this.changeMode(Constants.modes.SIMPLE_SELECT, {});
} else {
// Uses number-aware sorting to make sure '9' < '10'. Comparison is reversed because we want them
// in reverse order so that we can remove by index safely.
state.selectedCoordPaths
.sort((a, b) => b.localeCompare(a, 'en', { numeric: true }))
.forEach(id => state.feature.removeCoordinate(id));
this.fireUpdate();
state.selectedCoordPaths = [];
this.clearSelectedCoordinates();
this.fireActionable(state);
if (state.feature.isValid() === false) {
this.deleteFeature([state.featureId]);
this.changeMode(Constants.modes.SIMPLE_SELECT, {});
}
}
};

Expand Down
13 changes: 13 additions & 0 deletions test/direct_select.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,19 @@ test('direct_select', (t) => {
});
});

t.test('direct_select - trashing with no vertices selected should delete the feature', (st) => {
const ids = Draw.add(getGeoJSON('polygon'));
Draw.changeMode(Constants.modes.DIRECT_SELECT, {
featureId: ids[0]
});
afterNextRender(() => {
Draw.trash();
const afterTrash = Draw.get(ids[0]);
st.equal(afterTrash, undefined);
cleanUp(() => st.end());
});
});

t.test('direct_select - a click on a vertex and than dragging the map shouldn\'t drag the vertex', (st) => {
const ids = Draw.add(getGeoJSON('polygon'));
Draw.changeMode(Constants.modes.DIRECT_SELECT, {
Expand Down

0 comments on commit a4c3ab8

Please sign in to comment.