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

feat(protocol-designer): implement duplicate step #2715

Merged
merged 10 commits into from
Dec 5, 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
6 changes: 6 additions & 0 deletions components/src/__tests__/__snapshots__/lists.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ exports[`ListItem renders ListItem without icon correctly 1`] = `
exports[`TitledList renders TitledList with children correctly 1`] = `
<div
className="titled_list"
onContextMenu={undefined}
onMouseEnter={undefined}
onMouseLeave={undefined}
>
Expand All @@ -137,6 +138,7 @@ exports[`TitledList renders TitledList with children correctly 1`] = `
exports[`TitledList renders TitledList with onMouseEnter & onMouseLeave correctly 1`] = `
<div
className="titled_list"
onContextMenu={undefined}
onMouseEnter={[Function]}
onMouseLeave={[Function]}
>
Expand All @@ -156,6 +158,7 @@ exports[`TitledList renders TitledList with onMouseEnter & onMouseLeave correctl
exports[`TitledList renders TitledList with optional icon correctly 1`] = `
<div
className="titled_list"
onContextMenu={undefined}
onMouseEnter={undefined}
onMouseLeave={undefined}
>
Expand All @@ -175,6 +178,7 @@ exports[`TitledList renders TitledList with optional icon correctly 1`] = `
exports[`TitledList renders TitledList without icon correctly 1`] = `
<div
className="titled_list"
onContextMenu={undefined}
onMouseEnter={undefined}
onMouseLeave={undefined}
>
Expand All @@ -194,6 +198,7 @@ exports[`TitledList renders TitledList without icon correctly 1`] = `
exports[`TitledList renders collapsed TitledList correctly 1`] = `
<div
className="titled_list"
onContextMenu={undefined}
onMouseEnter={undefined}
onMouseLeave={undefined}
>
Expand Down Expand Up @@ -233,6 +238,7 @@ exports[`TitledList renders collapsed TitledList correctly 1`] = `
exports[`TitledList renders expanded TitledList correctly 1`] = `
<div
className="titled_list"
onContextMenu={undefined}
onMouseEnter={undefined}
onMouseLeave={undefined}
>
Expand Down
6 changes: 4 additions & 2 deletions components/src/lists/TitledList.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ type ListProps = {
description?: React.Node,
/** optional click action (on title div, not children) */
onClick?: (event: SyntheticMouseEvent<>) => mixed,
/** optional right click action (on wrapping div) */
onContextMenu?: (event: SyntheticMouseEvent<>) => mixed,
/** optional mouseEnter action */
onMouseEnter?: (event: SyntheticMouseEvent<>) => mixed,
/** optional mouseLeave action */
Expand All @@ -42,7 +44,7 @@ type ListProps = {
* An ordered list with optional title, icon, and description.
*/
export default function TitledList (props: ListProps) {
const {iconName, disabled, onCollapseToggle, iconProps, onMouseEnter, onMouseLeave} = props
const {iconName, disabled, onCollapseToggle, iconProps, onMouseEnter, onMouseLeave, onContextMenu} = props
const collapsible = onCollapseToggle != null

const onClick = !disabled
Expand Down Expand Up @@ -74,7 +76,7 @@ export default function TitledList (props: ListProps) {
const iconClass = cx(styles.title_bar_icon, styles.icon_left_of_title, iconProps && iconProps.className)

return (
<div className={className} {...{onMouseEnter, onMouseLeave}}>
<div className={className} {...{onMouseEnter, onMouseLeave, onContextMenu}}>
<div onClick={onClick} className={titleBarClass}>
{iconName && (
<Icon {...iconProps} className={iconClass} name={iconName} />
Expand Down
8 changes: 4 additions & 4 deletions protocol-designer/src/components/StepEditForm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import without from 'lodash/without'
import cx from 'classnames'

import {actions, selectors} from '../../steplist'
import type {FormData, StepType, StepFieldName} from '../../form-types'
import type {FormData, StepType, StepFieldName, StepIdType} from '../../form-types'
import type {BaseState, ThunkDispatch} from '../../types'
import formStyles from '../forms.css'
import styles from './StepEditForm.css'
Expand Down Expand Up @@ -37,7 +37,7 @@ type SP = {
formData?: ?FormData,
isNewStep?: boolean,
}
type DP = { deleteStep: () => mixed }
type DP = { deleteStep: (StepIdType) => mixed }

type StepEditFormState = {
showConfirmDeleteModal: boolean,
Expand Down Expand Up @@ -101,7 +101,7 @@ class StepEditForm extends React.Component<Props, StepEditFormState> {
onCancelClick={this.toggleConfirmDeleteModal}
onContinueClick={() => {
this.toggleConfirmDeleteModal()
deleteStep()
this.props.formData && deleteStep(this.props.formData.id)
}}
/>}
<div className={cx(formStyles.form, styles[formData.stepType])}>
Expand All @@ -127,7 +127,7 @@ const mapStateToProps = (state: BaseState): SP => ({
})

const mapDispatchToProps = (dispatch: ThunkDispatch<*>): DP => ({
deleteStep: () => dispatch(actions.deleteStep()),
deleteStep: (stepId: StepIdType) => dispatch(actions.deleteStep(stepId)),
})

export default connect(mapStateToProps, mapDispatchToProps)(StepEditForm)
119 changes: 119 additions & 0 deletions protocol-designer/src/components/steplist/ContextMenu.js
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})
Copy link
Contributor

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

}

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)
22 changes: 14 additions & 8 deletions protocol-designer/src/components/steplist/DraggableStepItems.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import isEqual from 'lodash/isEqual'

import StepItem from '../../containers/ConnectedStepItem'
import type {StepIdType} from '../../form-types'
import ContextMenu from './ContextMenu'

const DND_TYPES: {STEP_ITEM: "STEP_ITEM"} = {
STEP_ITEM: 'STEP_ITEM',
Expand Down Expand Up @@ -104,14 +105,19 @@ class StepItems extends React.Component<StepItemsProps, StepItemsState> {
const currentIds = this.props.isOver ? this.state.stepIds : this.props.orderedSteps
return this.props.connectDropTarget(
<div>
{currentIds.map((stepId: StepIdType) => (
<DragDropStepItem
key={stepId}
stepId={stepId}
findStepIndex={this.findStepIndex}
onDrag={this.onDrag}
moveStep={this.moveStep} />
))}
<ContextMenu>
{({makeStepOnContextMenu}) => (
currentIds.map((stepId: StepIdType) => (
<DragDropStepItem
key={stepId}
stepId={stepId}
onStepContextMenu={makeStepOnContextMenu(stepId)}
findStepIndex={this.findStepIndex}
onDrag={this.onDrag}
moveStep={this.moveStep} />
))
)}
</ContextMenu>
</div>
)
}
Expand Down
25 changes: 25 additions & 0 deletions protocol-designer/src/components/steplist/StepItem.css
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,28 @@
.labware_display_name {
cursor: default;
}

.context_menu {
position: absolute;
color: var(--c-font-light);
background-color: var(--c-bg-dark);
box-shadow: 0 2px 6px #999;
z-index: 100000;
}

.context_menu_item {
padding: 6px 50px 5px 10px;
min-width: 160px;
cursor: default;
font-size: var(--fs-body-1);
}

.context_menu_item:hover {
background-color: color(var(--c-bg-dark) shade(30%));
color: var(--c-font-light);
}

.context_menu_item:active {
color: white;
background-color: color(var(--c-bg-dark) shade(30%));
}
3 changes: 3 additions & 0 deletions protocol-designer/src/components/steplist/StepItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type StepItemProps = {
getLabware: (labwareId: ?string) => ?Labware,
handleSubstepHover: SubstepIdentifier => mixed,
onStepClick?: (event?: SyntheticEvent<>) => mixed,
onStepContextMenu?: (event?: SyntheticEvent<>) => mixed,
onStepItemCollapseToggle?: (event?: SyntheticEvent<>) => mixed,
onStepHover?: (event?: SyntheticEvent<>) => mixed,
onStepMouseLeave?: (event?: SyntheticEvent<>) => mixed,
Expand All @@ -48,6 +49,7 @@ function StepItem (props: StepItemProps) {

onStepMouseLeave,
onStepClick,
onStepContextMenu,
onStepItemCollapseToggle,
onStepHover,
} = props
Expand All @@ -63,6 +65,7 @@ function StepItem (props: StepItemProps) {
iconProps={{className: error ? styles.error_icon : ''}}
title={title || ''}
onClick={onStepClick}
onContextMenu={onStepContextMenu}
onMouseEnter={onStepHover}
onMouseLeave={onStepMouseLeave}
onCollapseToggle={onStepItemCollapseToggle}
Expand Down
7 changes: 5 additions & 2 deletions protocol-designer/src/components/steplist/StepList.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@ export default class StepList extends React.Component<Props> {
const altIsPressed = e.getModifierState('Alt')

if (altIsPressed) {
let delta = 0
if (key === 'ArrowUp') {
reorderSelectedStep(-1)
delta = -1
} else if (key === 'ArrowDown') {
reorderSelectedStep(1)
delta = 1
}
if (!delta) return
reorderSelectedStep(delta)
}
}

Expand Down
1 change: 1 addition & 0 deletions protocol-designer/src/localization/en/alert.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
},
"window": {
"confirm_create_new": "Are you sure you want to create a new file? Any unsaved changes will be discarded.",
"confirm_delete_step": "Are you sure you want to delete this step?",
"confirm_import": "Are you sure you want to import this file? You will lose any current unsaved changes.",
"confirm_leave": "Are you sure you want to leave? You will lose any unsaved changes."
}
Expand Down
6 changes: 6 additions & 0 deletions protocol-designer/src/localization/en/context_menu.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"step": {
"duplicate": "Duplicate Step",
"delete": "Delete Step"
}
}
2 changes: 2 additions & 0 deletions protocol-designer/src/localization/en/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import alert from './alert.json'
import button from './button.json'
import card from './card.json'
import context_menu from './context_menu.json'
import deck from './deck.json'
import form from './form.json'
import modal from './modal.json'
Expand All @@ -15,6 +16,7 @@ export default {
alert,
button,
card,
context_menu,
deck,
form,
modal,
Expand Down
10 changes: 4 additions & 6 deletions protocol-designer/src/steplist/actions/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,10 @@ export type DeleteStepAction = {
payload: StepIdType,
}

export const deleteStep = () => (dispatch: Dispatch<*>, getState: GetState) => {
dispatch({
type: 'DELETE_STEP',
payload: selectors.getSelectedStepId(getState()),
})
}
export const deleteStep = (stepId: StepIdType) => ({
type: 'DELETE_STEP',
payload: stepId,
})

type ExpandAddStepButtonAction = {
type: 'EXPAND_ADD_STEP_BUTTON',
Expand Down
Loading