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

Adds onCancelPan optional prop and adds more calls to cancelPan #166

Merged
merged 1 commit into from
Dec 9, 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 @@ -145,6 +145,10 @@ The `onEdit` event is the core event provided by this layer and must be handled

* `editContext` (Object): `null` or an object containing additional context about the edit. This is populated by the active mode.

#### `onCancelPan` (Function, optional)

The `onCancelPan` event is called when map panning should be cancelled to enable feature dragging interactions while editing.

### Guide style properties and data getters

#### `editHandleType`: (String, optional)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ export class ExtendLineStringMode extends GeoJsonEditMode {

const mapCoords = props.lastPointerMoveEvent && props.lastPointerMoveEvent.mapCoords;

if (!mapCoords) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe a comment that explains when this happens?

return guides;
}

// Draw an extension line starting from one end of the selected LineString to the cursor
let startPosition: Position | null | undefined = null;
const {modeConfig} = props;
Expand Down
7 changes: 6 additions & 1 deletion modules/editable-layers/src/edit-modes/extrude-mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import bearing from '@turf/bearing';
import {
generatePointsParallelToLinePoints,
getPickedEditHandle,
getPickedIntermediateEditHandle
getPickedIntermediateEditHandle,
shouldCancelPan
} from './utils';
import {FeatureCollection} from '../utils/geojson-types';
import {ModeProps, StartDraggingEvent, StopDraggingEvent, DraggingEvent} from './types';
Expand Down Expand Up @@ -61,6 +62,10 @@ export class ExtrudeMode extends ModifyMode {
}

handleStartDragging(event: StartDraggingEvent, props: ModeProps<FeatureCollection>) {
if (shouldCancelPan(event)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this need to be implemented by every mode? Could it be called by the framework before invoking handleStartDragging?

Do we need a mode writer's guide page in the docs that explains this?

event.cancelPan();
}

const selectedFeatureIndexes = props.selectedIndexes;

const editHandle = getPickedIntermediateEditHandle(event.picks);
Expand Down
7 changes: 6 additions & 1 deletion modules/editable-layers/src/edit-modes/modify-mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import {
getPickedExistingEditHandle,
getPickedIntermediateEditHandle,
updateRectanglePosition,
NearestPointType
NearestPointType,
shouldCancelPan
} from './utils';
import {LineString, Point, Polygon, FeatureCollection, FeatureOf} from '../utils/geojson-types';
import {
Expand Down Expand Up @@ -251,6 +252,10 @@ export class ModifyMode extends GeoJsonEditMode {
}

handleStartDragging(event: StartDraggingEvent, props: ModeProps<FeatureCollection>) {
if (shouldCancelPan(event)) {
event.cancelPan();
}

const selectedFeatureIndexes = props.selectedIndexes;

const editHandle = getPickedIntermediateEditHandle(event.picks);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ export class ResizeCircleMode extends GeoJsonEditMode {

handleStartDragging(event: StartDraggingEvent, props: ModeProps<FeatureCollection>) {
if (this._selectedEditHandle) {
event.cancelPan();
this._isResizing = true;
}
}
Expand Down
1 change: 1 addition & 0 deletions modules/editable-layers/src/edit-modes/rotate-mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import {getGeom} from '@turf/invariant';
import {point, featureCollection, lineString} from '@turf/helpers';
import turfTransformRotate from '@turf/transform-rotate';
import polygonToLine from '@turf/polygon-to-line';

Check warning on line 14 in modules/editable-layers/src/edit-modes/rotate-mode.ts

View workflow job for this annotation

GitHub Actions / test-node

Using exported name 'polygonToLine' as identifier for default export
import {
PointerMoveEvent,
StartDraggingEvent,
Expand Down Expand Up @@ -128,6 +128,7 @@

handleStartDragging(event: StartDraggingEvent, props: ModeProps<FeatureCollection>) {
if (this._selectedEditHandle) {
event.cancelPan();
this._isRotating = true;
this._geometryBeingRotated = this.getSelectedFeaturesAsFeatureCollection(props);
}
Expand Down
1 change: 1 addition & 0 deletions modules/editable-layers/src/edit-modes/scale-mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import turfBearing from '@turf/bearing';
import bboxPolygon from '@turf/bbox-polygon';
import {point, featureCollection} from '@turf/helpers';
import polygonToLine from '@turf/polygon-to-line';

Check warning on line 10 in modules/editable-layers/src/edit-modes/scale-mode.ts

View workflow job for this annotation

GitHub Actions / test-node

Using exported name 'polygonToLine' as identifier for default export
import {coordEach} from '@turf/meta';
import turfDistance from '@turf/distance';
import turfTransformScale from '@turf/transform-scale';
Expand Down Expand Up @@ -152,6 +152,7 @@

handleStartDragging(event: StartDraggingEvent, props: ModeProps<FeatureCollection>) {
if (this._selectedEditHandle) {
event.cancelPan();
this._isScaling = true;
this._geometryBeingScaled = this.getSelectedFeaturesAsFeatureCollection(props);
}
Expand Down
4 changes: 4 additions & 0 deletions modules/editable-layers/src/edit-modes/transform-mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ export class TransformMode extends CompositeMode {
let translateMode: TranslateMode | null = null;
const filteredModes: GeoJsonEditMode[] = [];

if (event.picks.length) {
event.cancelPan();
}

// If the user selects a scaling edit handle that overlaps with part of the selected feature,
// it is possible for both scale and translate actions to be triggered. This logic prevents
// this simultaneous action trigger from happening by putting a higher priority on scaling
Expand Down
1 change: 1 addition & 0 deletions modules/editable-layers/src/edit-modes/translate-mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import turfBearing from '@turf/bearing';
import turfDistance from '@turf/distance';
import clone from '@turf/clone';
import {point, Feature as TurfFeature, Geometry as TurfGeometry} from '@turf/helpers';

Check warning on line 8 in modules/editable-layers/src/edit-modes/translate-mode.ts

View workflow job for this annotation

GitHub Actions / test-node

Feature not found in '@turf/helpers'

Check warning on line 8 in modules/editable-layers/src/edit-modes/translate-mode.ts

View workflow job for this annotation

GitHub Actions / test-node

Geometry not found in '@turf/helpers'
import WebMercatorViewport from 'viewport-mercator-project';

Check warning on line 9 in modules/editable-layers/src/edit-modes/translate-mode.ts

View workflow job for this annotation

GitHub Actions / test-node

Using exported name 'WebMercatorViewport' as identifier for default export
import {FeatureCollection, Position, Geometry} from '../utils/geojson-types';
import {
PointerMoveEvent,
Expand Down Expand Up @@ -59,6 +59,7 @@
return;
}

event.cancelPan();
this._geometryBeforeTranslate = this.getSelectedFeaturesAsFeatureCollection(props);
}

Expand Down
1 change: 1 addition & 0 deletions modules/editable-layers/src/edit-modes/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export type Pick = {
index: number;
object?: any;
isGuide?: boolean;
featureType?: string;
featureIndex?: number;
type?: string;
isEditingHandle?: boolean | null;
Expand Down
6 changes: 5 additions & 1 deletion modules/editable-layers/src/edit-modes/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
import bearing from '@turf/bearing';
import pointToLineDistance from '@turf/point-to-line-distance';
import {flattenEach} from '@turf/meta';
import {point, MultiLineString} from '@turf/helpers';

Check warning on line 11 in modules/editable-layers/src/edit-modes/utils.ts

View workflow job for this annotation

GitHub Actions / test-node

MultiLineString not found in '@turf/helpers'
import {getCoords} from '@turf/invariant';
import WebMercatorViewport from 'viewport-mercator-project';
import {Viewport, Pick, EditHandleFeature, EditHandleType} from './types';
import {Viewport, Pick, EditHandleFeature, EditHandleType, StartDraggingEvent} from './types';
import {
Geometry,
Position,
Expand Down Expand Up @@ -516,3 +516,7 @@
})
.filter(Boolean);
}

export function shouldCancelPan(event: StartDraggingEvent) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Some TSDoc here would be helpful explaining when to call and why this criteria is used.

return event.picks.length && event.picks.find((p) => p.featureType === 'points');
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const EVENT_TYPES = ['anyclick', 'pointermove', 'panstart', 'panmove', 'panend',
export type EditableLayerProps<DataType = any> = CompositeLayerProps & {
pickingRadius?: number;
pickingDepth?: number;
onCancelPan?: () => void;
};

export abstract class EditableLayer<
Expand Down Expand Up @@ -154,7 +155,13 @@ export abstract class EditableLayer<
mapCoords,
pointerDownScreenCoords: screenCoords,
pointerDownMapCoords: mapCoords,
cancelPan: event.stopImmediatePropagation,
cancelPan: () => {
if (this.props.onCancelPan) {
this.props.onCancelPan();
}
Comment on lines +159 to +161
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (this.props.onCancelPan) {
this.props.onCancelPan();
}
this.props.onCancelPan?.();

Or make the default prop an empty function?


event.stopImmediatePropagation();
},
sourceEvent: event.srcEvent
});
}
Expand Down
Loading