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

Fix issue where modify mode can update the wrong vertex #198

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion modules/editable-layers/src/edit-modes/modify-mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ export class ModifyMode extends GeoJsonEditMode {

handleStopDragging(event: StopDraggingEvent, props: ModeProps<FeatureCollection>) {
const selectedFeatureIndexes = props.selectedIndexes;
const editHandle = getPickedEditHandle(event.picks);
const editHandle = getPickedEditHandle(event.pointerDownPicks);
if (selectedFeatureIndexes.length && editHandle) {
this._dragEditHandle('finishMovePosition', props, editHandle, event);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,91 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`Correct coordinate edited when stopping near another guide 1`] = `
{
"features": [
{
"geometry": {
"coordinates": [
1,
2,
],
"type": "Point",
},
"properties": {
"editHandleType": "existing",
"featureIndex": 0,
"guideType": "editHandle",
"positionIndexes": [
0,
],
},
"type": "Feature",
},
{
"geometry": {
"coordinates": [
2,
3,
],
"type": "Point",
},
"properties": {
"editHandleType": "existing",
"featureIndex": 0,
"guideType": "editHandle",
"positionIndexes": [
1,
],
},
"type": "Feature",
},
{
"geometry": {
"coordinates": [
3,
4,
],
"type": "Point",
},
"properties": {
"editHandleType": "existing",
"featureIndex": 0,
"guideType": "editHandle",
"positionIndexes": [
2,
],
},
"type": "Feature",
},
],
"type": "FeatureCollection",
}
`;

exports[`Correct coordinate edited when stopping near another guide 2`] = `
{
"geometry": {
"coordinates": [
[
3.1,
4.1,
],
[
2,
3,
],
[
3,
4,
],
],
"type": "LineString",
},
"properties": {},
"type": "Feature",
}
`;

exports[`Rectangular polygon feature preserves shape 1`] = `
{
"features": [
Expand Down
43 changes: 42 additions & 1 deletion modules/editable-layers/test/edit-modes/lib/modify-mode.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ const mockMove = (mode, picks: Pick[], props: ModeProps<FeatureCollection>) => {
const startDragEvent = createStartDraggingEvent([100, 100], [100, 100], picks);
mode.handleStartDragging(startDragEvent, props);

const stopDragEvent = createStopDraggingEvent([110, 115], [100, 100], picks);
const stopDragEvent = createStopDraggingEvent([110, 115], [100, 100], picks, picks);
mode.handleStopDragging(stopDragEvent, props);
};

Expand Down Expand Up @@ -199,6 +199,47 @@ test('Rectangular polygon feature preserves shape', () => {
expect(props.data.features[0]).not.toEqual(movedFeature);
});

test('Correct coordinate edited when stopping near another guide', () => {
const mockOnEdit = vi.fn();
const props = createFeatureCollectionProps({
data: {
type: 'FeatureCollection',
features: [lineStringFeature]
} as FeatureCollection,
selectedIndexes: [0],
onEdit: mockOnEdit
});

const mode = new ModifyMode();
const guides = mode.getGuides(props);
expect(guides).toMatchSnapshot();

const dragStart = [1, 2];
const dragEnd = [3.1, 4.1];

const pointerDownPicks = [
{ index: 0, isGuide: true, object: guides.features[0] },
{ index: 0, object: lineStringFeature }
];

const stopDragPicks = [
{ index: 2, isGuide: true, object: guides.features[2] }, // simulate another guide being picked
{ index: 0, isGuide: true, object: guides.features[0] },
{ index: 0, object: lineStringFeature }
];

const startDragEvent = createStartDraggingEvent(dragStart, dragStart, pointerDownPicks);
mode.handleStartDragging(startDragEvent, props);

const stopDragEvent = createStopDraggingEvent(dragEnd, dragStart, stopDragPicks, pointerDownPicks);
mode.handleStopDragging(stopDragEvent, props);

expect(mockOnEdit).toHaveBeenCalledTimes(1);

const editedFeature = mockOnEdit.mock.calls[0][0].updatedData.features[0];
expect(editedFeature).toMatchSnapshot();
});

describe('getGuides()', () => {
it('gets edit handles for Point', () => {
const mode = new ModifyMode();
Expand Down
5 changes: 3 additions & 2 deletions modules/editable-layers/test/edit-modes/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,15 +345,16 @@ export function createStartDraggingEvent(
export function createStopDraggingEvent(
mapCoords: Position,
pointerDownMapCoords: Position,
picks: Pick[] = []
picks: Pick[] = [],
pointerDownPicks: Pick[] | null = null
): StopDraggingEvent {
lastCoords = mapCoords;

return {
screenCoords: [-1, -1],
mapCoords,
picks,
pointerDownPicks: null,
pointerDownPicks,
pointerDownScreenCoords: [-1, -1],
pointerDownMapCoords,
sourceEvent: null
Expand Down