You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
devdocs/assets/javascripts/app/shortcuts.js

316 lines
7.2 KiB

// TODO: This file was created by bulk-decaffeinate.
// Sanity-check the conversion and remove this comment.
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* DS103: Rewrite code to no longer use __guard__, or convert again using --optional-chaining
* DS205: Consider reworking code to avoid use of IIFEs
* DS206: Consider reworking classes to avoid initClass
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/main/docs/suggestions.md
*/
app.Shortcuts = class Shortcuts {
static initClass() {
$.extend(this.prototype, Events);
}
constructor() {
this.onKeydown = this.onKeydown.bind(this);
this.onKeypress = this.onKeypress.bind(this);
this.isMac = $.isMac();
this.start();
}
start() {
1 year ago
$.on(document, "keydown", this.onKeydown);
$.on(document, "keypress", this.onKeypress);
}
stop() {
1 year ago
$.off(document, "keydown", this.onKeydown);
$.off(document, "keypress", this.onKeypress);
}
swapArrowKeysBehavior() {
1 year ago
return app.settings.get("arrowScroll");
}
spaceScroll() {
1 year ago
return app.settings.get("spaceScroll");
}
showTip() {
1 year ago
app.showTip("KeyNav");
return (this.showTip = null);
}
spaceTimeout() {
1 year ago
return app.settings.get("spaceTimeout");
}
onKeydown(event) {
1 year ago
if (this.buggyEvent(event)) {
return;
}
const result = (() => {
if (event.ctrlKey || event.metaKey) {
1 year ago
if (!event.altKey && !event.shiftKey) {
return this.handleKeydownSuperEvent(event);
}
} else if (event.shiftKey) {
if (!event.altKey) {
return this.handleKeydownShiftEvent(event);
}
} else if (event.altKey) {
return this.handleKeydownAltEvent(event);
} else {
return this.handleKeydownEvent(event);
}
})();
1 year ago
if (result === false) {
event.preventDefault();
}
}
onKeypress(event) {
1 year ago
if (
this.buggyEvent(event) ||
(event.charCode === 63 && document.activeElement.tagName === "INPUT")
) {
return;
}
if (!event.ctrlKey && !event.metaKey) {
const result = this.handleKeypressEvent(event);
1 year ago
if (result === false) {
event.preventDefault();
}
}
}
handleKeydownEvent(event, _force) {
1 year ago
if (
!_force &&
[37, 38, 39, 40].includes(event.which) &&
this.swapArrowKeysBehavior()
) {
return this.handleKeydownAltEvent(event, true);
}
1 year ago
if (
!event.target.form &&
((48 <= event.which && event.which <= 57) ||
(65 <= event.which && event.which <= 90))
) {
this.trigger("typing");
return;
}
switch (event.which) {
case 8:
1 year ago
if (!event.target.form) {
return this.trigger("typing");
}
break;
case 13:
1 year ago
return this.trigger("enter");
case 27:
1 year ago
this.trigger("escape");
return false;
case 32:
1 year ago
if (
event.target.type === "search" &&
this.spaceScroll() &&
(!this.lastKeypress ||
this.lastKeypress < Date.now() - this.spaceTimeout() * 1000)
) {
this.trigger("pageDown");
return false;
}
break;
case 33:
1 year ago
return this.trigger("pageUp");
case 34:
1 year ago
return this.trigger("pageDown");
case 35:
1 year ago
if (!event.target.form) {
return this.trigger("pageBottom");
}
break;
case 36:
1 year ago
if (!event.target.form) {
return this.trigger("pageTop");
}
break;
case 37:
1 year ago
if (!event.target.value) {
return this.trigger("left");
}
break;
case 38:
1 year ago
this.trigger("up");
if (typeof this.showTip === "function") {
this.showTip();
}
return false;
case 39:
1 year ago
if (!event.target.value) {
return this.trigger("right");
}
break;
case 40:
1 year ago
this.trigger("down");
if (typeof this.showTip === "function") {
this.showTip();
}
return false;
case 191:
if (!event.target.form) {
1 year ago
this.trigger("typing");
return false;
}
break;
}
}
handleKeydownSuperEvent(event) {
switch (event.which) {
case 13:
1 year ago
return this.trigger("superEnter");
case 37:
if (this.isMac) {
1 year ago
this.trigger("superLeft");
return false;
}
break;
case 38:
1 year ago
this.trigger("pageTop");
return false;
case 39:
if (this.isMac) {
1 year ago
this.trigger("superRight");
return false;
}
break;
case 40:
1 year ago
this.trigger("pageBottom");
return false;
case 188:
1 year ago
this.trigger("preferences");
return false;
}
}
handleKeydownShiftEvent(event, _force) {
1 year ago
if (
!_force &&
[37, 38, 39, 40].includes(event.which) &&
this.swapArrowKeysBehavior()
) {
return this.handleKeydownEvent(event, true);
}
1 year ago
if (!event.target.form && 65 <= event.which && event.which <= 90) {
this.trigger("typing");
return;
}
switch (event.which) {
case 32:
1 year ago
this.trigger("pageUp");
return false;
case 38:
1 year ago
if (!__guard__(getSelection(), (x) => x.toString())) {
this.trigger("altUp");
return false;
}
break;
case 40:
1 year ago
if (!__guard__(getSelection(), (x1) => x1.toString())) {
this.trigger("altDown");
return false;
}
break;
}
}
handleKeydownAltEvent(event, _force) {
1 year ago
if (
!_force &&
[37, 38, 39, 40].includes(event.which) &&
this.swapArrowKeysBehavior()
) {
return this.handleKeydownEvent(event, true);
}
switch (event.which) {
case 9:
1 year ago
return this.trigger("altRight", event);
case 37:
if (!this.isMac) {
1 year ago
this.trigger("superLeft");
return false;
}
break;
case 38:
1 year ago
this.trigger("altUp");
return false;
case 39:
if (!this.isMac) {
1 year ago
this.trigger("superRight");
return false;
}
break;
case 40:
1 year ago
this.trigger("altDown");
return false;
case 67:
1 year ago
this.trigger("altC");
return false;
case 68:
1 year ago
this.trigger("altD");
return false;
case 70:
1 year ago
return this.trigger("altF", event);
case 71:
1 year ago
this.trigger("altG");
return false;
case 79:
1 year ago
this.trigger("altO");
return false;
case 82:
1 year ago
this.trigger("altR");
return false;
case 83:
1 year ago
this.trigger("altS");
return false;
}
}
handleKeypressEvent(event) {
1 year ago
if (event.which === 63 && !event.target.value) {
this.trigger("help");
return false;
} else {
1 year ago
return (this.lastKeypress = Date.now());
}
}
buggyEvent(event) {
try {
event.target;
event.ctrlKey;
event.which;
return false;
} catch (error) {
return true;
}
}
};
app.Shortcuts.initClass();
function __guard__(value, transform) {
1 year ago
return typeof value !== "undefined" && value !== null
? transform(value)
: undefined;
}