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 1 commit
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
27 changes: 27 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,27 @@
import {FeatureCollection} from '../utils/geojson-types';

import {GeoJsonEditMode} from './geojson-edit-mode';
import {ClickEvent, ModeProps} from './types';
import {ImmutableFeatureCollection} from './immutable-feature-collection';

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 updatedData = new ImmutableFeatureCollection(props.data)
// in case of overlapping features, delete only the most recent feature
.deleteFeatures([selectedFeatureIndexes[0]])
moharamfatema marked this conversation as resolved.
Show resolved Hide resolved
.getObject();

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
61 changes: 61 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,61 @@
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