Skip to content

Commit

Permalink
Fix SnapEvent types and add unit tests for SnapEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
fmg-lydonchandra committed Jul 2, 2023
1 parent 8827a91 commit 1832a0a
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 35 deletions.
3 changes: 0 additions & 3 deletions examples/snap.js
Expand Up @@ -131,7 +131,4 @@ ExampleModify.setActive(false);
const snap = new Snap({
source: vector.getSource(),
});
snap.on('snap', (e) => {
console.debug(e)
})
map.addInteraction(snap);
49 changes: 45 additions & 4 deletions src/ol/events/SnapEvent.js
@@ -1,10 +1,51 @@
import BaseEvent from './Event.js';
/**
* @module ol/events/SnapEvent
*/
import Event from './Event.js';

export class SnapEvent extends BaseEvent {
constructor(options) {
super('snap');
/**
* @enum {string}
*/
export const SnapEventType = {
/**
* Triggered upon snapping to vertex or edge
* @event SnapEvent#snap
* @api
*/
SNAP: 'snap',
};

/**
* @classdesc
* Events emitted by {@link module:ol/interaction/Snap~Snap} instances are instances of this
*/
export class SnapEvent extends Event {
/**
* @param {SnapEventType} type Type.
* @param {Object} options Options.
* @param {import("../coordinate.js").Coordinate} options.vertex The snapped vertex.
* @param {import("../coordinate.js").Coordinate} options.vertexPixel The pixel of the snapped vertex.
* @param {import("../Feature.js").default} options.feature The feature being snapped.
*/
constructor(type, options) {
super(type);
/**
* The Map coordinate of the snapped point.
* @type {import("../coordinate.js").Coordinate}
* @api
*/
this.vertex = options.vertex;
/**
* The Map pixel of the snapped point.
* @type {Array<number>&Array<number>}
* @api
*/
this.vertexPixel = options.vertexPixel;
/**
* The feature closest to the snapped point.
* @type {import("../Feature.js").default<import("../geom/Geometry.js").default>}
* @api
*/
this.feature = options.feature;
}
}
16 changes: 9 additions & 7 deletions src/ol/interaction/Snap.js
Expand Up @@ -7,7 +7,7 @@ import PointerInteraction from './Pointer.js';
import RBush from '../structs/RBush.js';
import VectorEventType from '../source/VectorEventType.js';
import {FALSE, TRUE} from '../functions.js';
import {SnapEvent} from '../events/SnapEvent.js';
import {SnapEvent, SnapEventType} from '../events/SnapEvent.js';
import {boundingExtent, buffer, createEmpty} from '../extent.js';
import {
closestOnCircle,
Expand All @@ -28,6 +28,7 @@ import {listen, unlistenByKey} from '../events.js';
* @typedef {Object} Result
* @property {import("../coordinate.js").Coordinate|null} vertex Vertex.
* @property {import("../pixel.js").Pixel|null} vertexPixel VertexPixel.
* @property {import("../Feature.js").default|null} feature Feature.
*/

/**
Expand Down Expand Up @@ -59,8 +60,8 @@ function getFeatureFromEvent(evt) {
}
if (
/** @type {import("../Collection.js").CollectionEvent<import("../Feature.js").default>} */ (
evt
).element
evt
).element
) {
return /** @type {import("../Collection.js").CollectionEvent<import("../Feature.js").default>} */ (
evt
Expand Down Expand Up @@ -102,6 +103,7 @@ const tempSegment = [];
*
* map.addInteraction(snap);
*
* @fires SnapEvent
* @api
*/
class Snap extends PointerInteraction {
Expand Down Expand Up @@ -136,7 +138,7 @@ class Snap extends PointerInteraction {
this.once;

/***
* @type {SnapOnSignature<import("../events").EventsKey>}
* @type {SnapOnSignature<void>}
*/
this.un;

Expand Down Expand Up @@ -289,16 +291,16 @@ class Snap extends PointerInteraction {
/**
* @param {import("../MapBrowserEvent.js").default} evt Map browser event.
* @return {boolean} `false` to stop event propagation.
* @api
*/
handleEvent(evt) {
const result = this.snapTo(evt.pixel, evt.coordinate, evt.map);
if (result) {
evt.coordinate = result.vertex.slice(0, 2);
evt.pixel = result.vertexPixel;
evt.feature = result.feature;
this.dispatchEvent(
new SnapEvent({
vertex: result.vertex,
new SnapEvent(SnapEventType.SNAP, {
vertex: result.vertex.slice(0, 2),
vertexPixel: result.vertexPixel,
feature: result.feature,
})
Expand Down

0 comments on commit 1832a0a

Please sign in to comment.