-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathCommandBlock.js
More file actions
146 lines (139 loc) · 5.46 KB
/
CommandBlock.js
File metadata and controls
146 lines (139 loc) · 5.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// @flow
import * as React from 'react';
import AriaDisablingButton from './AriaDisablingButton';
import LoopIterationsInput from './LoopIterationsInput';
import { Button } from 'react-bootstrap';
import classNames from 'classnames';
import type { KeyboardInputSchemeName } from './KeyboardInputSchemes';
import type { RunningState } from './types';
import { ReactComponent as Forward1 } from './svg/Forward1.svg';
import { ReactComponent as Forward2 } from './svg/Forward2.svg';
import { ReactComponent as Forward3 } from './svg/Forward3.svg';
import { ReactComponent as Backward1 } from './svg/Backward1.svg';
import { ReactComponent as Backward2 } from './svg/Backward2.svg';
import { ReactComponent as Backward3 } from './svg/Backward3.svg';
import { ReactComponent as Left45 } from './svg/Left45.svg';
import { ReactComponent as Left90 } from './svg/Left90.svg';
import { ReactComponent as Left180 } from './svg/Left180.svg';
import { ReactComponent as Right45 } from './svg/Right45.svg';
import { ReactComponent as Right90 } from './svg/Right90.svg';
import { ReactComponent as Right180 } from './svg/Right180.svg';
import { ReactComponent as Loop } from './svg/Loop.svg';
import { ReactComponent as LoopWithShadow } from './svg/LoopWithShadow.svg'
import { ReactComponent as LoopStart } from './svg/LoopStart.svg';
import { ReactComponent as LoopEnd } from './svg/LoopEnd.svg';
type CommandBlockProps = {
commandName: string,
disabled: boolean,
loopLabel?: string,
loopIterations?: ?number,
loopIterationsAriaLabel?: ?string,
stepNumber?: number,
className?: string,
runningState?: RunningState,
keyboardInputSchemeName?: KeyboardInputSchemeName,
onClick: (evt: SyntheticEvent<HTMLButtonElement>) => void,
onChangeLoopIterations?: (stepNumber: number, loopLabel: string, loopIterations: number) => void
};
// TODO: Revise this once there is a proper strategy for typing SVG-backed
// components.
export const commandBlockIconTypes = new Map<string, any>([
['forward1', Forward1],
['forward2', Forward2],
['forward3', Forward3],
['backward1', Backward1],
['backward2', Backward2],
['backward3', Backward3],
['left45', Left45],
['left90', Left90],
['left180', Left180],
['right45', Right45],
['right90', Right90],
['right180', Right180],
['loop', Loop],
['loopWithShadow', LoopWithShadow],
['startLoop', LoopStart],
['endLoop', LoopEnd]
]);
export default React.forwardRef<CommandBlockProps, Button>(
(props, ref) => {
const {
commandName,
disabled,
loopLabel,
loopIterations,
loopIterationsAriaLabel,
stepNumber,
className,
runningState,
keyboardInputSchemeName,
onClick,
onChangeLoopIterations,
...otherProps
} = props;
let children = null;
if (commandName === 'startLoop' || commandName === 'endLoop') {
children =
<div className='command-block-loop-block-container'>
<div className='command-block-loop-label-container'>
{loopLabel}
</div>
{commandName === 'startLoop' && disabled &&
<input
aria-disabled={disabled}
aria-label={loopIterationsAriaLabel}
className='command-block-loop-iterations'
maxLength='2'
size='2'
type='text'
value={loopIterations}
readOnly={true}
/>
}
{commandName === 'startLoop'
&& !disabled
&& loopLabel != null
&& stepNumber != null
&& runningState != null
&& keyboardInputSchemeName != null
&& onChangeLoopIterations != null
&&
<LoopIterationsInput
loopIterationsAriaLabel={loopIterationsAriaLabel}
loopIterationsStr={loopIterations != null ? loopIterations.toString() : ''}
loopLabel={loopLabel}
stepNumber={stepNumber}
runningState={runningState}
keyboardInputSchemeName={keyboardInputSchemeName}
onChangeLoopIterations={onChangeLoopIterations}
/>
}
</div>
} else {
const iconType = commandBlockIconTypes.get(commandName);
if (iconType) {
children = React.createElement(
iconType,
{
className: 'command-block-svg'
}
);
}
}
const classes = classNames(
'command-block',
className
);
return React.createElement(
AriaDisablingButton,
Object.assign({
'variant': `command-block--${commandName}`,
'className': classes,
'onClick': onClick,
'disabled': disabled,
'ref': ref
}, otherProps),
children
);
}
);