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 3 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
11 changes: 8 additions & 3 deletions protocol-designer/src/components/steplist/StepList.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,25 @@ import {PortalRoot} from './TooltipPortal'
type Props = {
orderedSteps: Array<StepIdType>,
reorderSelectedStep: (delta: number) => mixed,
copySelectedStep: (delta: number) => mixed,
}

export default class StepList extends React.Component<Props> {
handleKeyDown = (e: SyntheticKeyboardEvent<*>) => {
const {reorderSelectedStep} = this.props
const {reorderSelectedStep, copySelectedStep} = this.props
const key = e.key
const altIsPressed = e.getModifierState('Alt')
const ctlIsPressed = e.getModifierState('Control')

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

Expand Down
1 change: 1 addition & 0 deletions protocol-designer/src/containers/ConnectedStepList.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ function mapStateToProps (state: BaseState): SP {

function mapDispatchToProps (dispatch: ThunkDispatch<*>): DP {
return {
copySelectedStep: (delta: number) => dispatch(steplistActions.copySelectedStep(delta)),
reorderSelectedStep: (delta: number) =>
dispatch(steplistActions.reorderSelectedStep(delta)),
}
Expand Down
22 changes: 22 additions & 0 deletions protocol-designer/src/steplist/actions/thunks.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,25 @@ export const reorderSelectedStep = (delta: number) =>
})
}
}

export type CopySelectedStepAction = {
type: 'COPY_SELECTED_STEP',
payload: {
selectedStepId: StepIdType,
nextStepId: StepIdType,
delta: number,
},
}

export const copySelectedStep = (delta: number) =>
(dispatch: ThunkDispatch<CopySelectedStepAction>, getState: GetState) => {
const selectedStepId = selectors.getSelectedStepId(getState())
const nextStepId = uuid()

if (selectedStepId != null) {
dispatch({
type: 'COPY_SELECTED_STEP',
payload: { selectedStepId, nextStepId, delta },
})
}
}
27 changes: 27 additions & 0 deletions protocol-designer/src/steplist/reducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import type {
ChangeFormInputAction,
DeleteStepAction,
ReorderSelectedStepAction,
CopySelectedStepAction,
SaveStepFormAction,
SelectStepAction,
SelectTerminalItemAction,
Expand Down Expand Up @@ -127,6 +128,13 @@ const steps: Reducer<StepsState, *> = handleActions({
}
}, {...initialStepState})
},
COPY_SELECTED_STEP: (state: StepsState, action: CopySelectedStepAction): StepsState => ({
...state,
[action.payload.nextStepId]: {
...(action.payload.selectedStepId ? state[action.payload.selectedStepId] : {}),
id: action.payload.nextStepId,
},
}),
}, initialStepState)

type SavedStepFormState = {
Expand Down Expand Up @@ -173,6 +181,13 @@ const savedStepForms: Reducer<SavedStepFormState, *> = handleActions({
...action.payload.update,
},
}),
COPY_SELECTED_STEP: (state: SavedStepFormState, action: CopySelectedStepAction): SavedStepFormState => ({
...state,
[action.payload.nextStepId]: {
...(action.payload.selectedStepId ? state[action.payload.selectedStepId] : {}),
id: action.payload.nextStepId,
},
}),
}, {})

type CollapsedStepsState = {
Expand Down Expand Up @@ -221,6 +236,18 @@ const orderedSteps: Reducer<OrderedStepsState, *> = handleActions({
...stepsWithoutSelectedStep.slice(nextIndex),
]
},
COPY_SELECTED_STEP: (state: OrderedStepsState, action: CopySelectedStepAction): OrderedStepsState => {
const {selectedStepId, nextStepId, delta} = action.payload
const selectedIndex = state.findIndex(s => s === selectedStepId)

if (delta <= 0 && selectedIndex === 0) return [nextStepId, ...state]
const boundary = delta <= 0 ? selectedIndex + 1 + delta : selectedIndex + delta
return [
...state.slice(0, boundary),
nextStepId,
...state.slice(boundary, state.length),
]
},
}, [])

export type SelectableItem = {
Expand Down