Skip to content

Commit 7a2f68e

Browse files
committed
Cosmetic changes to keybindings code
1 parent 8fd08cb commit 7a2f68e

File tree

3 files changed

+49
-37
lines changed

3 files changed

+49
-37
lines changed

src/web/BindingsWaiter.js

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -23,36 +23,40 @@ const BindingsWaiter = function (app, manager) {
2323
* @param {event} e
2424
*/
2525
BindingsWaiter.prototype.parseInput = function(e) {
26-
let modKey = e.altKey;
27-
if (this.app.options.useMetaKey) modKey = e.metaKey;
26+
const modKey = this.app.options.useMetaKey ? e.metaKey : e.altKey;
27+
2828
if (e.ctrlKey && modKey) {
2929
let elem;
3030
switch (e.code) {
31-
case "KeyF":
31+
case "KeyF": // Focus search
3232
e.preventDefault();
33-
document.getElementById("search").focus(); // Focus search
33+
document.getElementById("search").focus();
3434
break;
35-
case "KeyI":
35+
case "KeyI": // Focus input
3636
e.preventDefault();
37-
document.getElementById("input-text").focus(); // Focus input
37+
document.getElementById("input-text").focus();
3838
break;
39-
case "KeyO":
39+
case "KeyO": // Focus output
4040
e.preventDefault();
41-
document.getElementById("output-text").focus(); // Focus output
41+
document.getElementById("output-text").focus();
4242
break;
43-
case "Period":
43+
case "Period": // Focus next operation
44+
e.preventDefault();
4445
try {
45-
elem = document.activeElement.closest(".operation");
46+
elem = document.activeElement.closest(".operation") || document.querySelector("#rec-list .operation");
4647
if (elem.parentNode.lastChild === elem) {
47-
elem.parentNode.firstChild.querySelectorAll(".arg")[0].focus(); // if operation is last in recipe, loop around to the top operation's first argument
48+
// If operation is last in recipe, loop around to the top operation's first argument
49+
elem.parentNode.firstChild.querySelectorAll(".arg")[0].focus();
4850
} else {
49-
elem.nextSibling.querySelectorAll(".arg")[0].focus(); //focus first argument of next operation
51+
// Focus first argument of next operation
52+
elem.nextSibling.querySelectorAll(".arg")[0].focus();
5053
}
5154
} catch (e) {
5255
// do nothing, just don't throw an error
5356
}
5457
break;
55-
case "KeyB":
58+
case "KeyB": // Set breakpoint
59+
e.preventDefault();
5660
try {
5761
elem = document.activeElement.closest(".operation").querySelectorAll(".breakpoint")[0];
5862
if (elem.getAttribute("break") === "false") {
@@ -67,7 +71,8 @@ BindingsWaiter.prototype.parseInput = function(e) {
6771
// do nothing, just don't throw an error
6872
}
6973
break;
70-
case "KeyD":
74+
case "KeyD": // Disable operation
75+
e.preventDefault();
7176
try {
7277
elem = document.activeElement.closest(".operation").querySelectorAll(".disable-icon")[0];
7378
if (elem.getAttribute("disabled") === "false") {
@@ -85,29 +90,36 @@ BindingsWaiter.prototype.parseInput = function(e) {
8590
// do nothing, just don't throw an error
8691
}
8792
break;
88-
case "Space":
89-
this.app.bake(); // bake the recipe
93+
case "Space": // Bake
94+
e.preventDefault();
95+
this.app.bake();
9096
break;
91-
case "Quote":
92-
this.app.bake(true); // step through the recipe
97+
case "Quote": // Step through
98+
e.preventDefault();
99+
this.app.bake(true);
93100
break;
94-
case "KeyC":
95-
this.manager.recipe.clearRecipe(); // clear recipe
101+
case "KeyC": // Clear recipe
102+
e.preventDefault();
103+
this.manager.recipe.clearRecipe();
96104
break;
97-
case "KeyS":
98-
this.manager.output.saveClick(); // save output to file
105+
case "KeyS": // Save output to file
106+
e.preventDefault();
107+
this.manager.output.saveClick();
99108
break;
100-
case "KeyL":
101-
this.manager.controls.loadClick(); // load a recipe
109+
case "KeyL": // Load recipe
110+
e.preventDefault();
111+
this.manager.controls.loadClick();
102112
break;
103-
case "KeyM":
104-
this.manager.controls.switchClick(); // switch input & output
113+
case "KeyM": // Switch input and output
114+
e.preventDefault();
115+
this.manager.output.switchClick();
105116
break;
106117
default:
107-
if (e.code.match(/Digit[0-9]/g)) {
118+
if (e.code.match(/Digit[0-9]/g)) { // Select nth operation
108119
e.preventDefault();
109120
try {
110-
document.querySelector(`li:nth-child(${e.code.substr(-1)}) .arg`).focus(); // select the first argument of the operation corresponding to the number pressed
121+
// Select the first argument of the operation corresponding to the number pressed
122+
document.querySelector(`li:nth-child(${e.code.substr(-1)}) .arg`).focus();
111123
} catch (e) {
112124
// do nothing, just don't throw an error
113125
}
@@ -117,6 +129,7 @@ BindingsWaiter.prototype.parseInput = function(e) {
117129
}
118130
};
119131

132+
120133
/**
121134
* Updates keybinding list when metaKey option is toggled
122135
*
@@ -149,12 +162,12 @@ BindingsWaiter.prototype.updateKeybList = function() {
149162
<td>Ctrl+${modMac}+o</td>
150163
</tr>
151164
<tr>
152-
<td>Place cursor in first argument field<br>of the next operation in the recipe</td>
165+
<td>Place cursor in first argument field of the next operation in the recipe</td>
153166
<td>Ctrl+${modWinLin}+.</td>
154167
<td>Ctrl+${modMac}+.</td>
155168
</tr>
156169
<tr>
157-
<td>Place cursor in first argument field<br>of the nth operation in the recipe</td>
170+
<td>Place cursor in first argument field of the nth operation in the recipe</td>
158171
<td>Ctrl+${modWinLin}+[1-9]</td>
159172
<td>Ctrl+${modMac}+[1-9]</td>
160173
</tr>

src/web/Manager.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ Manager.prototype.setup = function() {
7777
this.worker.registerChefWorker();
7878
this.recipe.initialiseOperationDragNDrop();
7979
this.controls.autoBakeChange();
80-
this.seasonal.load();
8180
this.bindings.updateKeybList();
81+
this.seasonal.load();
8282
};
8383

8484

@@ -92,6 +92,7 @@ Manager.prototype.initialiseEventListeners = function() {
9292
window.addEventListener("focus", this.window.windowFocus.bind(this.window));
9393
window.addEventListener("statechange", this.app.stateChange.bind(this.app));
9494
window.addEventListener("popstate", this.app.popState.bind(this.app));
95+
9596
// Controls
9697
document.getElementById("bake").addEventListener("click", this.controls.bakeClick.bind(this.controls));
9798
document.getElementById("auto-bake").addEventListener("change", this.controls.autoBakeChange.bind(this.controls));
@@ -162,16 +163,14 @@ Manager.prototype.initialiseEventListeners = function() {
162163
document.getElementById("reset-options").addEventListener("click", this.options.resetOptionsClick.bind(this.options));
163164
$(document).on("switchChange.bootstrapSwitch", ".option-item input:checkbox", this.options.switchChange.bind(this.options));
164165
$(document).on("switchChange.bootstrapSwitch", ".option-item input:checkbox", this.options.setWordWrap.bind(this.options));
166+
$(document).on("switchChange.bootstrapSwitch", ".option-item input:checkbox#useMetaKey", this.bindings.updateKeybList.bind(this.bindings));
165167
this.addDynamicListener(".option-item input[type=number]", "keyup", this.options.numberChange, this.options);
166168
this.addDynamicListener(".option-item input[type=number]", "change", this.options.numberChange, this.options);
167169
this.addDynamicListener(".option-item select", "change", this.options.selectChange, this.options);
168170
document.getElementById("theme").addEventListener("change", this.options.themeChange.bind(this.options));
169171

170-
//Keybindings
171-
window.addEventListener("keydown", this.bindings.parseInput.bind(this.bindings));
172-
$(document).on("switchChange.bootstrapSwitch", ".option-item input:checkbox#useMetaKey", this.bindings.updateKeybList.bind(this.bindings));
173-
174172
// Misc
173+
window.addEventListener("keydown", this.bindings.parseInput.bind(this.bindings));
175174
document.getElementById("alert-close").addEventListener("click", this.app.alertCloseClick.bind(this.app));
176175
};
177176

src/web/html/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ <h4 class="modal-title">Options</h4>
328328
</div>
329329
<div class="option-item">
330330
<input type="checkbox" option="useMetaKey" id="useMetaKey" />
331-
<label for="errorTimeout"> Use meta (Windows/Command ⌘) key for keybindings </label>
331+
<label for="useMetaKey"> Use meta key for keybindings (Windows/Command ⌘) </label>
332332
</div>
333333
<div class="option-item">
334334
<input type="number" option="errorTimeout" id="errorTimeout" />
@@ -486,7 +486,7 @@ <h5><strong>Aim</strong></h5>
486486
<p>It’s the Cyber Swiss Army Knife.</p>
487487
</div>
488488
<div role="tabpanel" class="tab-pane" id="keybindings" style="padding: 20px;">
489-
<table class="table table-condensed table-bordered" id="keybList"></table>
489+
<table class="table table-condensed table-bordered table-hover" id="keybList"></table>
490490
</div>
491491
</div>
492492
</div>

0 commit comments

Comments
 (0)