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
return last snapTo feature #14676
return last snapTo feature #14676
Conversation
working on returning snapTo feature externally Any thoughts on best way to return the features externally ? One way is to save snapToResult internally in a variable, and add function getSnapToResult()
and have calling code checking |
e9e1742
to
9c0939f
Compare
📦 Preview the website for this branch here: https://deploy-preview-14676--ol-site.netlify.app/. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your work on this, @fmg-lydonchandra. I have added several comments. In addition, it would be great if you could add a test.
src/ol/interaction/Snap.js
Outdated
getLastSnapToResult() { | ||
if (this.lastSnapToResult_) { | ||
return this.lastSnapToResult_; | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is now unused.
getLastSnapToResult() { | |
if (this.lastSnapToResult_) { | |
return this.lastSnapToResult_; | |
} | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed unused, as per feedback.
src/ol/interaction/Snap.js
Outdated
} | ||
}); | ||
} | ||
} | ||
const result = getResult(); | ||
if (result) { | ||
this.lastSnapToResult_ = result; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No longer used.
this.lastSnapToResult_ = result; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed unused, as per feedback.
src/ol/events/SnapEvent.js
Outdated
this.vertex = options.vertex; | ||
this.vertexPixel = options.vertexPixel; | ||
this.feature = options.feature; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Types are missing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
src/ol/events/SnapEvent.js
Outdated
@@ -0,0 +1,11 @@ | |||
import BaseEvent from './Event.js'; | |||
|
|||
const SNAP = 'snap'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const SNAP = 'snap'; |
src/ol/interaction/Snap.js
Outdated
this.on; | ||
|
||
this.once; | ||
|
||
this.un; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Types are missing. I'd suggest adding a SnapOnSignature
, similar to the Modify
interaction's ModifyOnSignature
, and using it here as @type {SnapOnSignature<import("../events").EventsKey>}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added SnapOnSignature
src/ol/interaction/Snap.js
Outdated
|
||
// As you suggested in Stack Overflow you can use forEachFeatureAtPixel. | ||
// That could be done in a condition function for any interaction, as well as drawstart and drawend, and done using only documented API methods as in https://codesandbox.io/s/draw-and-modify-features-forked-enrgqv?file=/main.js | ||
// If the Snap interaction was constructed with features: snapFeatures instead of source: snapSource just replace snapSource.getFeatures().includes with snapFeatures.getArray().includes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These comments can be removed.
// As you suggested in Stack Overflow you can use forEachFeatureAtPixel. | |
// That could be done in a condition function for any interaction, as well as drawstart and drawend, and done using only documented API methods as in https://codesandbox.io/s/draw-and-modify-features-forked-enrgqv?file=/main.js | |
// If the Snap interaction was constructed with features: snapFeatures instead of source: snapSource just replace snapSource.getFeatures().includes with snapFeatures.getArray().includes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed unused comments
f5f641d
to
8e997e7
Compare
1ffbb2e
to
2f3a1ad
Compare
Re-review please @ahocevar :-) |
5922e5b
to
7efb2de
Compare
7efb2de
to
1832a0a
Compare
src/ol/interaction/Snap.js
Outdated
vertex: result.vertex.slice(0, 2), | ||
vertexPixel: result.vertexPixel, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you'd assign evt.coordinate
and evt.pixel
here instead, users would be able to modify the snap coordinate, which might be useful.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you decide not to change that, then also create a copy of the pixel so users can neither modify coordinate
nor pixel
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed to assign evt.coordinate and evt.pixel as suggested.
Curious what is the example use case for the users wanting to modify snap coordinate?
expect(snapEvent.vertexPixel).to.eql([width / 2, height / 2]); | ||
expect(snapEvent.feature).to.eql(point); | ||
done(); | ||
}); | ||
snapInteraction.handleEvent(event); | ||
// check that the coordinate is in XY and not XYZ | ||
expect(event.coordinate).to.eql([0, 0]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since events are dispatched synchronously in OpenLayers, this test won't be executed any more. I'd suggest creating a separate test for the snap
event instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved event.coordinate expect
inside eventHandler
expect
.
Keeping it in the same unit test for ease of maintenance, as we are doing snapEvent.feature expect
,
(same for all unit tests below)
and reuse event input data.
Is it still better to have separate unit tests for snapEvent @ahocevar ?
If so I'll refactor.
snapInteraction.on('snap', function (snapEvent) { | ||
expect(snapEvent.type).to.be('snap'); | ||
expect(snapEvent.vertex).to.eql([0, 0]); | ||
expect(snapEvent.vertexPixel).to.eql([width / 2, height / 2]); | ||
expect(snapEvent.feature).to.be(point); | ||
done(); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When you change the snap
event to return the event pixel and coordinate, this test won't be necessary any more. If you decide not to make that change, the same problem as above applies.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see comment above #14676 (comment)
snapInteraction.on('snap', function (snapEvent) { | ||
expect(snapEvent.type).to.be('snap'); | ||
expect(snapEvent.vertex).to.eql([7, 0]); | ||
expect(snapEvent.feature).to.be(undefined); | ||
done(); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see comment above #14676 (comment)
snapInteraction.on('snap', function (snapEvent) { | ||
expect(snapEvent.vertex[0]).to.roughlyEqual(coordinate[0], 1e-10); | ||
expect(snapEvent.vertex[1]).to.roughlyEqual(coordinate[1], 1e-10); | ||
done(); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see comment above #14676 (comment)
snapInteraction.on('snap', function (snapEvent) { | ||
expect(snapEvent.vertex[0]).to.roughlyEqual(coordinate[0], 1e-10); | ||
expect(snapEvent.vertex[1]).to.roughlyEqual(coordinate[1], 1e-10); | ||
expect(snapEvent.feature).to.be(undefined); | ||
done(); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see comment above #14676 (comment)
snapInteraction.on('snap', function (snapEvent) { | ||
expect(snapEvent.vertex).to.eql([10, 0]); | ||
expect(snapEvent.feature).to.be(feature); | ||
done(); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see comment above #14676 (comment)
snapInteraction.on('snap', function (snapEvent) { | ||
expect(snapEvent.vertex).to.eql([10, 0]); | ||
expect(snapEvent.feature).to.be(line); | ||
done(); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see comment above #14676 (comment)
snapInteraction.on('snap', function (snapEvent) { | ||
expect(snapEvent.vertex).to.eql([10, 0]); | ||
expect(snapEvent.feature).to.be(line); | ||
done(); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see comment above #14676 (comment)
snapInteraction.on('snap', function (snapEvent) { | ||
expect(snapEvent.vertex).to.eql([lon, lat]); | ||
expect(snapEvent.vertexPixel).to.eql(expectedPixel); | ||
expect(snapEvent.feature).to.be(point); | ||
done(); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see comment above #14676 (comment)
Fix snap.test
expect(snapEvent.feature).to.eql(point); | ||
|
||
// check that the coordinate is in XY and not XYZ | ||
expect(event.coordinate).to.eql([0, 0]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Moved event.coordinate check inside eventHandler check
- keeping it in the same unit test for ease of maintenance
- reuse
event
input data
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, @fmg-lydonchandra! I think this is a great addition to the API.
Fixes #14675