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

Feat: delete mode for geojson editable layers #136

Merged
merged 3 commits into from
Sep 17, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,8 @@ A single mode that provides translating, rotating, and scaling capabilities. Tra

[Source code](https://github.com/visgl/deck.gl-community/blob/master/modules/editable-layers/src/edit-modes/transform-mode.ts)

## DeleteMode

User can delete features by clicking on them. Only the most recently added feature will be deleted if multiple features overlap.

[Source code](https://github.com/visgl/deck.gl-community/blob/master/modules/editable-layers/src/edit-modes/delete-mode.ts)
28 changes: 28 additions & 0 deletions modules/editable-layers/src/edit-modes/delete-mode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {FeatureCollection} from '../utils/geojson-types';

import {GeoJsonEditMode} from './geojson-edit-mode';
import {ClickEvent, ModeProps} from './types';
export class DeleteMode extends GeoJsonEditMode {
handleClick(_event: ClickEvent, props: ModeProps<FeatureCollection>): void {
const selectedFeatureIndexes = props.lastPointerMoveEvent.picks.map((pick) => pick.index);
if (selectedFeatureIndexes.length > 0) {
const indexToDelete = selectedFeatureIndexes[0];

const features = props.data.features.filter((_, index) => index !== indexToDelete);
const updatedData = {
...props.data,
features
};

const editAction = {
updatedData,
editType: 'deleteFeature',
editContext: {
featureIndexes: selectedFeatureIndexes
}
};

props.onEdit(editAction);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {Draw90DegreePolygonMode} from '../edit-modes/draw-90degree-polygon-mode'
import {DrawPolygonByDraggingMode} from '../edit-modes/draw-polygon-by-dragging-mode';
import {SnappableMode} from '../edit-modes/snappable-mode';
import {TransformMode} from '../edit-modes/transform-mode';
import {DeleteMode} from '../edit-modes/delete-mode';
import {GeoJsonEditModeType} from '../edit-modes/geojson-edit-mode';

import {Color} from '../utils/types';
Expand Down Expand Up @@ -250,6 +251,7 @@ const modeNameMapping = {
split: SplitPolygonMode,
extrude: ExtrudeMode,
elevation: ElevationMode,
delete: DeleteMode,

// Draw modes
drawPoint: DrawPointMode,
Expand Down
72 changes: 72 additions & 0 deletions modules/editable-layers/test/edit-modes/lib/delete-mode.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import {describe, it, expect, vi} from 'vitest';
import {DeleteMode} from '../../../src/edit-modes/delete-mode';
import {ClickEvent, ModeProps} from '../../../src/edit-modes/types';
import {FeatureCollection} from '../../../src/utils/geojson-types';

describe('DeleteMode', () => {
it('should not call onEdit when no features are selected', () => {
const deleteMode = new DeleteMode();
const props: ModeProps<FeatureCollection> = {
data: {type: 'FeatureCollection', features: []},
selectedIndexes: [],
lastPointerMoveEvent: {picks: []},
onEdit: vi.fn()
};

deleteMode.handleClick({} as ClickEvent, props);

expect(props.onEdit).not.toHaveBeenCalled();
});

it('should call onEdit with correct parameters when one feature is selected', () => {
const deleteMode = new DeleteMode();
const props: ModeProps<FeatureCollection> = {
data: {
type: 'FeatureCollection',
features: [
{type: 'Feature', geometry: {type: 'Point', coordinates: [0, 0]}, properties: {}}
]
},
selectedIndexes: [],
lastPointerMoveEvent: {picks: [{index: 0}]},
onEdit: vi.fn()
};

deleteMode.handleClick({} as ClickEvent, props);

expect(props.onEdit).toHaveBeenCalledWith({
updatedData: {type: 'FeatureCollection', features: []},
editType: 'deleteFeature',
editContext: {featureIndexes: [0]}
});
});

it('should call onEdit with correct parameters when multiple features are selected', () => {
const deleteMode = new DeleteMode();
const props: ModeProps<FeatureCollection> = {
data: {
type: 'FeatureCollection',
features: [
{type: 'Feature', geometry: {type: 'Point', coordinates: [0, 0]}, properties: {}},
{type: 'Feature', geometry: {type: 'Point', coordinates: [1, 1]}, properties: {}}
]
},
selectedIndexes: [],
lastPointerMoveEvent: {picks: [{index: 0}, {index: 1}]},
onEdit: vi.fn()
};

deleteMode.handleClick({} as ClickEvent, props);

expect(props.onEdit).toHaveBeenCalledWith({
updatedData: {
type: 'FeatureCollection',
features: [
{type: 'Feature', geometry: {type: 'Point', coordinates: [1, 1]}, properties: {}}
]
},
editType: 'deleteFeature',
editContext: {featureIndexes: [0, 1]}
});
});
});
Loading