Skip to content

Commit

Permalink
Allow keyboard navigation in hack tutorial
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelMarcey committed Oct 6, 2016
1 parent 5b601df commit d53874a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
27 changes: 27 additions & 0 deletions js/hack_tutorial/tutorial.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var remove_comments = function (content) {

(function () {
var current = 0,
codeset = {17: false, 66: false, 78: false}, // ctrl, b, n
passed_test = function(content) { return true; },
prev_button = document.getElementById('prev_button'),
next_button = document.getElementById('next_button'),
Expand Down Expand Up @@ -58,6 +59,32 @@ var remove_comments = function (content) {
resetState();
});

// navigating with keys: ctrl-b for back, ctrl-n for next
window.addEventListener('keydown', function(e) {
if (e.keyCode in codeset) {
codeset[e.keyCode] = true;
if (codeset[17] && codeset[66]) {
current = (current <= 0) ? 0 : current - 1;
resetState();
} else if (codeset[17] && codeset[78]) {
if (current >= ft_exercises.length - 1) {
current = ft_exercises.length;
editor.setValue("// Congratulations! You are done!");
} else {
current++;
}
resetState();
}
}
});

window.addEventListener('keyup', function(e) {
if (e.keyCode in codeset) {
codeset[e.keyCode] = false;
}
});


editor.setValue(ft_exercises[current].content);
resetState();

Expand Down
2 changes: 2 additions & 0 deletions js/hack_tutorial/tutorial_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ ft_exercises = Array(
\n\
\n\
/************ Click 'Next' to get started! *********/\n\
\n\
/************ You can use buttons OR `ctrl-b` and `ctrl-n` to navigate *********/\n\
", check:(function (x) { var content = remove_comments(x);return (true);})}, {content:"<?php\n\
// ^-- FIXME: replace <?php with <?hh\n\
\n\
Expand Down

0 comments on commit d53874a

Please sign in to comment.