-
Notifications
You must be signed in to change notification settings - Fork 179
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
feat(protocol-designer): implement duplicate step #2715
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d906ba6
proof of concept copy steps
b-cooper 70c390c
Merge branch 'edge' into pd_copy-step
b-cooper e13e632
feat(protocol-designer): implement copy selected step
b-cooper 458ac8e
confirm delete
b-cooper d86cb78
modal stylings
b-cooper af902f0
iron out flow
b-cooper 39cbe20
fix close and make flow happy
b-cooper ce5e3d0
fix persistent menu
b-cooper f695e68
unused class
b-cooper 44bfe0a
merge conflicts dealt with
b-cooper File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
protocol-designer/src/components/steplist/ContextMenu.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
// @flow | ||
import * as React from 'react' | ||
import {connect} from 'react-redux' | ||
import type {ThunkDispatch} from '../../types' | ||
import i18n from '../../localization' | ||
import {actions as steplistActions} from '../../steplist' | ||
import {Portal} from '../portals/TopPortal' | ||
import type {StepIdType} from '../../form-types' | ||
import styles from './StepItem.css' | ||
|
||
const MENU_OFFSET_PX = 5 | ||
|
||
type DP = { | ||
deleteStep: (StepIdType) => {}, | ||
duplicateStep: (StepIdType) => {}, | ||
} | ||
type Props = { | ||
children: ({makeStepOnContextMenu: (StepIdType) => (event: SyntheticMouseEvent<>) => mixed}) => React.Node, | ||
} & DP | ||
type State = { | ||
visible: boolean, | ||
left: ?number, | ||
top: ?number, | ||
stepId: ?StepIdType, | ||
} | ||
|
||
class ContextMenu extends React.Component<Props, State> { | ||
state = { | ||
visible: false, | ||
left: null, | ||
top: null, | ||
stepId: null, | ||
} | ||
menuRoot: ?HTMLElement | ||
|
||
componentDidMount () { | ||
global.addEventListener('click', this.handleClick) | ||
} | ||
|
||
componentWillUnmount () { | ||
global.removeEventListener('click', this.handleClick) | ||
} | ||
|
||
makeHandleContextMenu = (stepId: StepIdType) => (event) => { | ||
event.preventDefault() | ||
|
||
const clickX = event.clientX | ||
const clickY = event.clientY | ||
|
||
this.setState({visible: true, stepId}, () => { | ||
const screenW = window.innerWidth | ||
const screenH = window.innerHeight | ||
const rootW = this.menuRoot ? this.menuRoot.offsetWidth : 0 | ||
const rootH = this.menuRoot ? this.menuRoot.offsetHeight : 0 | ||
|
||
const left = (screenW - clickX) > rootW ? clickX + MENU_OFFSET_PX : clickX - rootW - MENU_OFFSET_PX | ||
const top = (screenH - clickY) > rootH ? clickY + MENU_OFFSET_PX : clickY - rootH - MENU_OFFSET_PX | ||
this.setState({left, top}) | ||
}) | ||
} | ||
|
||
handleClick = (event: SyntheticMouseEvent<*>) => { | ||
const { visible } = this.state | ||
|
||
const wasOutside = !(this.menuRoot && event.target instanceof Node && this.menuRoot.contains(event.target)) | ||
|
||
if (wasOutside && visible) this.setState({visible: false, left: null, top: null}) | ||
} | ||
|
||
handleDuplicate = () => { | ||
if (this.state.stepId != null) { | ||
this.props.duplicateStep(this.state.stepId) | ||
this.setState({stepId: null, visible: false}) | ||
} | ||
} | ||
|
||
handleDelete = () => { | ||
if (this.state.stepId != null && confirm(i18n.t('alert.window.confirm_delete_step'))) { | ||
this.props.deleteStep(this.state.stepId) | ||
this.setState({stepId: null, visible: false}) | ||
} | ||
} | ||
|
||
render () { | ||
return ( | ||
<div> | ||
{this.props.children({makeStepOnContextMenu: this.makeHandleContextMenu})} | ||
{this.state.visible && | ||
<Portal> | ||
<React.Fragment> | ||
<div | ||
ref={ref => { this.menuRoot = ref }} | ||
style={{left: this.state.left, top: this.state.top}} | ||
className={styles.context_menu}> | ||
<div | ||
onClick={this.handleDuplicate} | ||
className={styles.context_menu_item}> | ||
{i18n.t('context_menu.step.duplicate')} | ||
</div> | ||
<div | ||
onClick={this.handleDelete} | ||
className={styles.context_menu_item}> | ||
{i18n.t('context_menu.step.delete')} | ||
</div> | ||
</div> | ||
</React.Fragment> | ||
</Portal> | ||
} | ||
</div> | ||
) | ||
} | ||
} | ||
|
||
const mapDispatchToProps = (dispatch: ThunkDispatch<*>) => ({ | ||
deleteStep: (stepId: StepIdType) => dispatch(steplistActions.deleteStep(stepId)), | ||
duplicateStep: (stepId: StepIdType) => dispatch(steplistActions.duplicateStep(stepId)), | ||
}) | ||
|
||
export default connect(null, mapDispatchToProps)(ContextMenu) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"step": { | ||
"duplicate": "Duplicate Step", | ||
"delete": "Delete Step" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The ContextMenu should probably close even when the click is inside. If I delete a step, that step item disappears but the ContextMenu lingers there, and I can click duplicate/delete over and over if I want though now it does nothing.
Maybe this is intended behavior with duplicate which is OK with me, but when you delete a step I think its ContextMenu should disappear