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

Feature/polygon selection undo functionality #3

Merged
merged 2 commits into from
Oct 31, 2018
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
5 changes: 4 additions & 1 deletion src/components/Annotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export default compose(
// For Polygon Selector
onSelectionComplete: T.func,
onSelectionClear: T.func,
onSelectionUndo: T.func,

annotations: T.arrayOf(
T.shape({
Expand Down Expand Up @@ -143,6 +144,7 @@ export default compose(
onClick = (e) => this.callSelectorMethod('onClick', e)
onSelectionComplete = () => this.callSelectorMethod('onSelectionComplete')
onSelectionClear = () => this.callSelectorMethod('onSelectionClear')
onSelectionUndo = () => this.callSelectorMethod('onSelectionUndo')

onSubmit = () => {
this.props.onSubmit(this.props.value)
Expand Down Expand Up @@ -277,7 +279,8 @@ export default compose(
renderPolygonControls({
annotation: props.value,
onSelectionComplete: this.onSelectionComplete,
onSelectionClear: this.onSelectionClear
onSelectionClear: this.onSelectionClear,
onSelectionUndo: this.onSelectionUndo
})
)
}
Expand Down
3 changes: 2 additions & 1 deletion src/components/PolygonControls/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const Button = styled.div`
function PolygonControls (props) {
const { geometry } = props.annotation
// Only show polygon controls if there are at least three points set
if (!geometry || !geometry.points || geometry.points.length < 3) return null
if (!geometry || !geometry.points || geometry.points.length === 0) return null

return (
<div
Expand All @@ -68,6 +68,7 @@ function PolygonControls (props) {
<Container
className={props.className}
>
{(geometry.points.length >= 2) && <Button onClick={props.onSelectionUndo}>Undo</Button>}
<Button onClick={props.onSelectionClear}>Clear</Button>
<Button onClick={props.onSelectionComplete}>Done</Button>
</Container>
Expand Down
3 changes: 2 additions & 1 deletion src/components/defaultProps.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,12 @@ export default {
onSubmit={onSubmit}
/>
),
renderPolygonControls: ({ annotation, onSelectionComplete, onSelectionClear }) => (
renderPolygonControls: ({ annotation, onSelectionComplete, onSelectionClear, onSelectionUndo }) => (
<PolygonControls
annotation={annotation}
onSelectionComplete={onSelectionComplete}
onSelectionClear={onSelectionClear}
onSelectionUndo={onSelectionUndo}
/>
),
renderHighlight: ({ key, annotation, active }) => {
Expand Down
10 changes: 10 additions & 0 deletions src/hocs/PolygonSelector.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,16 @@ export const methods = {
}
},

onSelectionUndo (annotation) {
return {
...annotation,
geometry: {
...annotation.geometry,
points: annotation.geometry.points.slice(0, -1)
}
}
},

onMouseUp (annotation, e) {
const coordOfClick = getCoordPercentage(e)

Expand Down