Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trash correct vertices by changing sort to be numeric-aware #943

Merged
merged 1 commit into from
Dec 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/modes/direct_select.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,11 @@ DirectSelect.toDisplayFeatures = function(state, geojson, push) {
};

DirectSelect.onTrash = function(state) {
state.selectedCoordPaths.sort().reverse().forEach(id => state.feature.removeCoordinate(id));
// 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 }))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.sort((a, b) => b.localeCompare(a, 'en', { numeric: true }))
.sort((a, b) => +b - a)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't work for sort keys that are deeply nested, e.g. 0.8.9 should be sorted before 0.8.10

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested that this works with localeCompare's numeric sort:

console.warn(['11', '10', '9', '33', '8', '30', '3'].sort((a, b) => b.localeCompare(a, 'en', { numeric: true })));
console.warn(['4.11', '4.10', '40.8', '4.9', '4.33',].sort((a, b) => b.localeCompare(a, 'en', { numeric: true })));
console.warn(['4.10.34', '4.10.3', '4.10.29'].sort((a, b) => b.localeCompare(a, 'en', { numeric: true })));

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I didn't realize those weren't just numbers, which wasn't clear from the comment. 👍

.forEach(id => state.feature.removeCoordinate(id));
this.fireUpdate();
state.selectedCoordPaths = [];
this.clearSelectedCoordinates();
Expand Down
27 changes: 27 additions & 0 deletions test/direct_select.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,33 @@ test('direct_select', (t) => {
});
});

t.test('direct_select - trashing vertices should delete the correct ones', (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(() => {
// select multiple nodes at indices 9, 10, 11
click(map, makeMouseEvent(70, 10, { shiftKey: true }));
click(map, makeMouseEvent(80, 10, { shiftKey: true }));
click(map, makeMouseEvent(60, 10, { shiftKey: true }));
afterNextRender(() => {
Draw.trash();
const afterTrash = Draw.get(ids[0]);
st.deepEqual(afterTrash.geometry.coordinates, [[0, 0], [10, 0], [20, 0], [30, 0], [40, 0], [50, 0], [60, 0], [70, 0], [80, 0], [50, 10]]);
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