Return null when not snapped
This commit is contained in:
@@ -26,7 +26,6 @@ import {listen, unlistenByKey} from '../events.js';
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @typedef {Object} Result
|
* @typedef {Object} Result
|
||||||
* @property {boolean} snapped Snapped.
|
|
||||||
* @property {import("../coordinate.js").Coordinate|null} vertex Vertex.
|
* @property {import("../coordinate.js").Coordinate|null} vertex Vertex.
|
||||||
* @property {import("../pixel.js").Pixel|null} vertexPixel VertexPixel.
|
* @property {import("../pixel.js").Pixel|null} vertexPixel VertexPixel.
|
||||||
*/
|
*/
|
||||||
@@ -66,11 +65,7 @@ function getFeatureFromEvent(evt) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const NOT_SNAPPED = {
|
const tempSegment = [];
|
||||||
snapped: false,
|
|
||||||
vertex: null,
|
|
||||||
vertexPixel: null,
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @classdesc
|
* @classdesc
|
||||||
@@ -268,7 +263,7 @@ class Snap extends PointerInteraction {
|
|||||||
*/
|
*/
|
||||||
handleEvent(evt) {
|
handleEvent(evt) {
|
||||||
const result = this.snapTo(evt.pixel, evt.coordinate, evt.map);
|
const result = this.snapTo(evt.pixel, evt.coordinate, evt.map);
|
||||||
if (result.snapped) {
|
if (result) {
|
||||||
evt.coordinate = result.vertex.slice(0, 2);
|
evt.coordinate = result.vertex.slice(0, 2);
|
||||||
evt.pixel = result.vertexPixel;
|
evt.pixel = result.vertexPixel;
|
||||||
}
|
}
|
||||||
@@ -413,7 +408,7 @@ class Snap extends PointerInteraction {
|
|||||||
* @param {import("../pixel.js").Pixel} pixel Pixel
|
* @param {import("../pixel.js").Pixel} pixel Pixel
|
||||||
* @param {import("../coordinate.js").Coordinate} pixelCoordinate Coordinate
|
* @param {import("../coordinate.js").Coordinate} pixelCoordinate Coordinate
|
||||||
* @param {import("../PluggableMap.js").default} map Map.
|
* @param {import("../PluggableMap.js").default} map Map.
|
||||||
* @return {Result} Snap result
|
* @return {Result|null} Snap result
|
||||||
*/
|
*/
|
||||||
snapTo(pixel, pixelCoordinate, map) {
|
snapTo(pixel, pixelCoordinate, map) {
|
||||||
const lowerLeft = map.getCoordinateFromPixel([
|
const lowerLeft = map.getCoordinateFromPixel([
|
||||||
@@ -430,7 +425,7 @@ class Snap extends PointerInteraction {
|
|||||||
|
|
||||||
const segmentsLength = segments.length;
|
const segmentsLength = segments.length;
|
||||||
if (segmentsLength === 0) {
|
if (segmentsLength === 0) {
|
||||||
return NOT_SNAPPED;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const projection = map.getView().getProjection();
|
const projection = map.getView().getProjection();
|
||||||
@@ -446,7 +441,6 @@ class Snap extends PointerInteraction {
|
|||||||
const squaredPixelDistance = squaredDistance(pixel, vertexPixel);
|
const squaredPixelDistance = squaredDistance(pixel, vertexPixel);
|
||||||
if (squaredPixelDistance <= squaredPixelTolerance) {
|
if (squaredPixelDistance <= squaredPixelTolerance) {
|
||||||
return {
|
return {
|
||||||
snapped: true,
|
|
||||||
vertex: closestVertex,
|
vertex: closestVertex,
|
||||||
vertexPixel: [
|
vertexPixel: [
|
||||||
Math.round(vertexPixel[0]),
|
Math.round(vertexPixel[0]),
|
||||||
@@ -455,7 +449,7 @@ class Snap extends PointerInteraction {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return NOT_SNAPPED;
|
return null;
|
||||||
};
|
};
|
||||||
|
|
||||||
if (this.vertex_) {
|
if (this.vertex_) {
|
||||||
@@ -475,13 +469,12 @@ class Snap extends PointerInteraction {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
const result = getResult();
|
const result = getResult();
|
||||||
if (result.snapped) {
|
if (result) {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.edge_) {
|
if (this.edge_) {
|
||||||
const tempSegment = [];
|
|
||||||
for (let i = 0; i < segmentsLength; ++i) {
|
for (let i = 0; i < segmentsLength; ++i) {
|
||||||
let vertex = null;
|
let vertex = null;
|
||||||
const segmentData = segments[i];
|
const segmentData = segments[i];
|
||||||
@@ -523,12 +516,12 @@ class Snap extends PointerInteraction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const result = getResult();
|
const result = getResult();
|
||||||
if (result.snapped) {
|
if (result) {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return NOT_SNAPPED;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user