Skip to content

Commit

Permalink
fix(editor): improve pattern shape editing
Browse files Browse the repository at this point in the history
  • Loading branch information
evansiroky authored and landonreed committed Sep 22, 2017
1 parent 3c99fc5 commit cf0fbda
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 33 deletions.
42 changes: 29 additions & 13 deletions lib/editor/actions/map/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,18 @@ export function handleControlPointDragEnd (index, controlPoint, evt, pattern) {
}
}

export function handleControlPointDrag (index, latlng, previous, next, pattern) {
export function handleControlPointDrag (controlPoints, index, latlng, patternShape) {
return function (dispatch, getState) {
const { followStreets, patternCoordinates } = getState().editor.editSettings
const defaultToStraightLine = false
recalculatePatternCoordinates(latlng, previous, next, pattern, followStreets, patternCoordinates, defaultToStraightLine)
const {followStreets} = getState().editor.editSettings
recalculatePatternCoordinates(
controlPoints,
'update',
index,
followStreets,
latlng,
patternShape,
false
)
.then(coords => {
if (coords) {
dispatch(updatePatternCoordinates(coords))
Expand All @@ -50,13 +57,11 @@ export function handleControlPointDrag (index, latlng, previous, next, pattern)
}
}

export function removingControlPoint (pattern, index, begin, end) {
export function removingControlPoint (pattern, index) {
return {
type: 'REMOVE_CONTROL_POINT',
pattern,
index,
begin,
end
index
}
}

Expand Down Expand Up @@ -93,14 +98,25 @@ export function updatePatternCoordinates (coordinates) {
}
}

export function removeControlPoint (pattern, index, begin, end) {
export function removeControlPoint (controlPoints, index, pattern) {
return async function (dispatch, getState) {
const { followStreets, patternCoordinates } = getState().editor.editSettings
const coordinates = await recalculatePatternCoordinates(null, begin, end, pattern, followStreets, patternCoordinates)
const {followStreets} = getState().editor.editSettings
const coordinates = await recalculatePatternCoordinates(
controlPoints,
'delete',
index,
followStreets,
null,
pattern.shape
)
// update pattern
dispatch(updateActiveGtfsEntity(pattern, 'trippattern', {shape: {type: 'LineString', coordinates}}))
dispatch(
updateActiveGtfsEntity(pattern, 'trippattern', {
shape: {type: 'LineString', coordinates}
})
)
// remove controlPoint
dispatch(removingControlPoint(pattern, index, begin, end))
dispatch(removingControlPoint(pattern, index))
}
}

Expand Down
21 changes: 7 additions & 14 deletions lib/editor/actions/map/stopStrategies.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,25 +238,18 @@ export function removeStopFromPattern (pattern, stop, index, controlPoints) {
controlPointStopIdx === index
)
})
// if not the first stop, use the previous control point as beginning of slice
const begin = controlPoints[cpIndex - 2]
? controlPoints[cpIndex - 1].point
: null
// if not last stop, use previous control point as end of slice
const end = controlPoints[cpIndex + 2]
? controlPoints[cpIndex + 1].point
: null
const {followStreets, patternCoordinates} = getState().editor.editSettings

const {followStreets} = getState().editor.editSettings

let coordinates
try {
coordinates = await recalculatePatternCoordinates(
null,
begin,
end,
pattern,
controlPoints,
'delete',
cpIndex,
followStreets,
patternCoordinates
null,
pattern.shape
)
} catch (err) {
console.log(err)
Expand Down
9 changes: 5 additions & 4 deletions lib/editor/components/map/ControlPoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export default class ControlPoint extends Component {
static propTypes = {
activePattern: PropTypes.object,
controlPoint: PropTypes.object,
controlPoints: PropTypes.array,
editSettings: PropTypes.object,
icon: PropTypes.object,
index: PropTypes.number,
Expand Down Expand Up @@ -34,18 +35,18 @@ export default class ControlPoint extends Component {
}

_onClick = (e) => {
const {permanent, removeControlPoint, activePattern, index, previous, next} = this.props
const {controlPoints, permanent, removeControlPoint, activePattern, index} = this.props
// only remove controlPoint if it's not based on pattern stop (i.e., has permanent prop)
if (!permanent) {
removeControlPoint(activePattern, index, previous, next)
removeControlPoint(controlPoints, index, activePattern)
}
}

handleDrag = (e) => {
const {activePattern, handleControlPointDrag, index, next, previous} = this.props
const {activePattern, controlPoints, handleControlPointDrag, index} = this.props
const latlng = e.latlng
this.setState({latlng})
handleControlPointDrag(index, latlng, previous, next, activePattern)
handleControlPointDrag(controlPoints, index, latlng, activePattern.shape)
}

_onDragEnd = (e) => {
Expand Down
4 changes: 2 additions & 2 deletions lib/editor/containers/ActiveGtfsEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,11 @@ const mapDispatchToProps = (dispatch, ownProps) => {
addStopAtIntersection: (latlng, activePattern) => dispatch(addStopAtIntersection(latlng, activePattern)),
addControlPoint: (controlPoint, index) => dispatch(addControlPoint(controlPoint, index)),
setActiveStop: ({id, index}) => dispatch(setActiveStop({id, index})),
removeControlPoint: (pattern, index, begin, end) => dispatch(removeControlPoint(pattern, index, begin, end)),
removeControlPoint: (controlPoints, index, pattern) => dispatch(removeControlPoint(controlPoints, index, pattern)),
updateControlPoint: (index, point, distance) => dispatch(updateControlPoint(index, point, distance)),
constructControlPoint: (pattern, latlng, controlPoints) => dispatch(constructControlPoint(pattern, latlng, controlPoints)),
handleControlPointDragEnd: (index, controlPoint, evt, pattern) => dispatch(handleControlPointDragEnd(index, controlPoint, evt, pattern)),
handleControlPointDrag: (index, latlng, previous, next, pattern) => dispatch(handleControlPointDrag(index, latlng, previous, next, pattern)),
handleControlPointDrag: (controlPoints, index, latlng, patternShape) => dispatch(handleControlPointDrag(controlPoints, index, latlng, patternShape)),

// EDITOR UI
setTutorialHidden: (value) => dispatch(setTutorialHidden(value))
Expand Down
1 change: 1 addition & 0 deletions lib/editor/util/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ export async function recalculatePatternCoordinates (

/**
* Convenience function to return the coordinates from a lineSliceAlong operation
*
* @param {Number} startDistance
* @param {Number} endDistance
* @return {Coordinate[]}
Expand Down

0 comments on commit cf0fbda

Please sign in to comment.