Skip to content

Commit f3c934e

Browse files
authored
Merge pull request #1138 from ascholerChemeketa/improve-ac-hotkeys
Update activecode keybindings
2 parents cc810b5 + f2581a2 commit f3c934e

1 file changed

Lines changed: 34 additions & 17 deletions

File tree

bases/rsptx/interactives/runestone/activecode/js/activecode.js

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -377,29 +377,46 @@ export class ActiveCode extends RunestoneBase {
377377
}
378378
// capture current this for use in event handler
379379
let acElement = this;
380-
$(window).keydown(function (e) {
381-
//Solving Keyboard Trap of ActiveCode: If user use tab for navigation outside of ActiveCode, then change tab behavior in ActiveCode to enable tab user to tab out of the textarea
382-
var code = e.keyCode ? e.keyCode : e.which;
383-
if (code == 9 && $("textarea:focus").length === 0) {
380+
381+
// document level event handler for tab key to handle context switching
382+
// detect if tab key was used to get into the editor
383+
document.addEventListener("keydown", function (e) {
384+
if (e.key === "Tab") {
385+
editor.tabDown = true;
386+
}
387+
});
388+
document.addEventListener("keyup", function (e) {
389+
if (e.key === "Tab") {
390+
editor.tabDown = false;
391+
}
392+
});
393+
editor.getInputField().addEventListener("focus", function (e) {
394+
// Solving Keyboard Trap of ActiveCode: If user uses Tab to navigate to the
395+
// activecode, let Tab get them out
396+
if (editor.tabDown) {
384397
editor.setOption("extraKeys", {
385-
Tab: function (cm) {
386-
$(document.activeElement)
387-
.closest(".tab-content")
388-
.nextSibling.focus();
389-
},
390-
"Shift-Tab": function (cm) {
391-
$(document.activeElement)
392-
.closest(".tab-content")
393-
.previousSibling.focus();
394-
},
398+
Tab: false,
399+
"Shift-Tab": false,
400+
});
401+
} else {
402+
editor.setOption("extraKeys", {
403+
Tab: "indentMore",
404+
"Shift-Tab": "indentLess",
395405
});
396406
}
397-
if ((e.originalEvent.code == "KeyS") && e.originalEvent.ctrlKey) {
407+
});
408+
// keyboard shortcuts for run (ctrl/cmd + s) and comment (ctrl/cmd + /)
409+
this.containerDiv.addEventListener("keydown", function (e) {
410+
if (e.code === "KeyS" && (e.ctrlKey || e.metaKey)) {
411+
if (acElement.runButton.disabled)
412+
return;
398413
acElement.runButton.click();
399414
e.preventDefault();
400415
}
401-
if ((e.originalEvent.code == "Slash") && e.originalEvent.ctrlKey) {
402-
editor.toggleComment();
416+
if (e.code === "Slash" && (e.ctrlKey || e.metaKey)) {
417+
if (typeof editor.toggleComment === "function") {
418+
editor.toggleComment();
419+
}
403420
e.preventDefault();
404421
}
405422
});

0 commit comments

Comments
 (0)