From cf58748e54e0ea9c89ec04de292bb3d46f8a25cb Mon Sep 17 00:00:00 2001 From: Trygve Aaberge Date: Fri, 20 Mar 2020 18:42:01 +0100 Subject: [PATCH] Allow deleting the whole feature from direct_select When no vertices are selected, trash deletes the whole feature. --- src/modes/direct_select.js | 25 +++++++++++++++---------- test/direct_select.test.js | 21 +++++++++++++++++++++ 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/src/modes/direct_select.js b/src/modes/direct_select.js index 7ba82e9d5..de1082c66 100644 --- a/src/modes/direct_select.js +++ b/src/modes/direct_select.js @@ -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, {}); + } } }; diff --git a/test/direct_select.test.js b/test/direct_select.test.js index 3758bc1e1..dd686cda5 100644 --- a/test/direct_select.test.js +++ b/test/direct_select.test.js @@ -148,6 +148,27 @@ test('direct_select', (t) => { }); }); + t.test('direct_select - trashing with no vertices selected should delete the feature', (st) => { + const longLine = { + type: 'Feature', + properties: {}, + geometry: { + type: 'LineString', + 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]] + } + }; + const ids = Draw.add(longLine); + Draw.changeMode(Constants.modes.DIRECT_SELECT, { + featureId: ids[0] + }); + afterNextRender(() => { + Draw.trash(); + const afterTrash = Draw.get(ids[0]); + st.deepEqual(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, {