-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathKeyboardShortcut.js
More file actions
189 lines (180 loc) · 5.14 KB
/
KeyboardShortcut.js
File metadata and controls
189 lines (180 loc) · 5.14 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import React from "react";
import {
Button,
ButtonBase,
Icon,
Tooltip,
Popover,
} from "@material-ui/core";
import "../../css/KeyboardShortcut.css";
const general = [
{
shortcut: ["Ctrl/⌘", "S"],
description: "Save scene"
},
{
shortcut: ["Ctrl/⌘", "Shift", "S"],
description: "Save scene as"
},
{
shortcut: ["Ctrl/⌘", "Enter"],
description: "Render scene"
},
];
const editor = [
{
shortcut: ["Ctrl/⌘", "/"],
description: "Comment current or selected line of code"
},
{
shortcut: ["Alt/Option", "Up"],
description: "Move current line of code up 1 line"
},
{
shortcut: ["Alt/Option", "Down"],
description: "Move current line of code down 1 line"
},
{
shortcut: ["Alt/⌘", "D"],
description: "Delete current line of code"
}
];
const scene = [
{
shortcut: ["W"],
description: "Move forwards"
},
{
shortcut: ["S"],
description: "Move backwards"
},
{
shortcut: ["A"],
description: "Move left"
},
{
shortcut: ["D"],
description: "Move right"
},
{
shortcut: ["F"],
description: "Enter full screen"
},
{
shortcut: ["Space"],
description: "Move up"
},
{
shortcut: ["Shift"],
description: "Move down"
},
];
/**
* React components create the button with KeyboardShortcut window
*/
class KeyboardShortcut extends React.Component {
constructor(props){
super(props);
this.state = {
open: false,
anchorEl: null
};
}
/**
* Helper function to convert the shrotcuts to an equivalent DOM elements
*
* @param {array} data
*/
shortcutHelper = (data) => {
let shortcuts = [];
data.shortcut.forEach((key,i)=>{
shortcuts.push(<kbd>{key}</kbd>);
if(i < data.shortcut.length-1){
shortcuts.push(" + ");
}
});
return (<p>{shortcuts} {data.description}</p>);
};
/**
* Update the state when the button is clicked
*
* @param {object} event target to anchor the window
*/
handleClick = (event) =>{
this.setState({
open: true,
anchorEl: event.target});
};
/**
* Update the state when the window is closed
*
* @param {object} event target to anchor the window
*/
handleClose = () => {
this.setState({
open: false,
anchorEl: null});
};
/**
* Creates the keyboard shortcut in the DOM
*/
render(){
return(
<div className="whole-keyboard"
spacing={10}>
<Tooltip title="Keyboard Shortcut">
<Button
id = "keyboard-tour"
className="shortcut-button"
variant="contained"
size="small"
color="primary"
onClick={this.handleClick}>
<Icon className="material-icons">keyboard</Icon>
</Button>
</Tooltip>
<Popover
id="simple-popover"
anchorEl={this.state.anchorEl}
anchorOrigin={{
vertical:"top",
horizontal: "left",
}}
transformOrigin={{
vertical: "bottom",
hotizontal: "left"
}}
open={this.state.open}
onClose={this.handleClose}>
<div className="keyboard-shortcut">
<ButtonBase
style={{ position: "absolute", right: 15, top: 15 }}
onClick={this.handleClose} >
<Icon className="material-icons">clear</Icon>
</ButtonBase >
<section className="right">
<p className="title">General Commands</p>
{
general.map(e => {return this.shortcutHelper(e);})
}
</section>
<section className="right">
<p className="title">Editor Commands</p>
{
editor.map(e => {return this.shortcutHelper(e);})
}
</section>
<section className="right">
<p className="title">Scene Controls</p>
{
scene.map(e => {return this.shortcutHelper(e);})
}
</section>
</div>
<p className="note">⌘: Command key for macOS user</p>
</Popover>
</div>
);
}
}
export default KeyboardShortcut;