Skip to content

librustdoc: generalise handling of keyboard shortcuts #26881

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 12, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 48 additions & 44 deletions src/librustdoc/html/static/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,62 +76,65 @@
highlightSourceLines(null);
$(window).on('hashchange', highlightSourceLines);

// Helper function for Keyboard events,
// Get's the char from the keypress event
// Gets the human-readable string for the virtual-key code of the
// given KeyboardEvent, ev.
//
// This method is used because e.wich === x is not
// compatible with non-english keyboard layouts
// This function is meant as a polyfill for KeyboardEvent#key,
// since it is not supported in Trident. We also test for
// KeyboardEvent#keyCode because the handleShortcut handler is
// also registered for the keydown event, because Blink doesn't fire
// keypress on hitting the Escape key.
//
// Note: event.type must be keypress !
function getChar(event) {
if (event.which == null) {
return String.fromCharCode(event.keyCode) // IE
} else if (event.which!=0 && event.charCode!=0) {
return String.fromCharCode(event.which) // the rest
} else {
return null // special key
}
// So I guess you could say things are getting pretty interoperable.
function getVirtualKey(ev) {
if ("key" in ev && typeof ev.key != "undefined")
return ev.key;

var c = ev.charCode || ev.keyCode;
if (c == 27)
return "Escape";
return String.fromCharCode(c);
}

$(document).on('keypress', function handleKeyboardShortcut(e) {
if (document.activeElement.tagName === 'INPUT') {
function handleShortcut(ev) {
if (document.activeElement.tagName == "INPUT")
return;
}

if (getChar(e) === '?') {
if (e.shiftKey && $('#help').hasClass('hidden')) {
e.preventDefault();
$('#help').removeClass('hidden');
switch (getVirtualKey(ev)) {
case "Escape":
if (!$("#help").hasClass("hidden")) {
ev.preventDefault();
$("#help").addClass("hidden");
} else if (!$("#search").hasClass("hidden")) {
ev.preventDefault();
$("#search").addClass("hidden");
$("#main").removeClass("hidden");
}
} else if (getChar(e) === 's' || getChar(e) === 'S') {
e.preventDefault();
$('.search-input').focus();
}
}).on('keydown', function(e) {
// The escape key event has to be captured with the keydown event.
// Because keypressed has no keycode for the escape key
// (and other special keys in general)...
if (document.activeElement.tagName === 'INPUT') {
return;
}

if (e.keyCode === 27) { // escape key
if (!$('#help').hasClass('hidden')) {
e.preventDefault();
$('#help').addClass('hidden');
} else if (!$('#search').hasClass('hidden')) {
e.preventDefault();
$('#search').addClass('hidden');
$('#main').removeClass('hidden');
break;

case "s":
case "S":
ev.preventDefault();
$(".search-input").focus();
break;

case "?":
if (ev.shiftKey && $("#help").hasClass("hidden")) {
ev.preventDefault();
$("#help").removeClass("hidden");
}
break;
}
}).on('click', function(e) {
if (!$(e.target).closest('#help').length) {
$('#help').addClass('hidden');
}

$(document).on("keypress", handleShortcut);
$(document).on("keydown", handleShortcut);
$(document).on("click", function(ev) {
if (!$(ev.target).closest("#help").length) {
$("#help").addClass("hidden");
}
});


$('.version-selector').on('change', function() {
var i, match,
url = document.location.href,
Expand All @@ -150,6 +153,7 @@

document.location.href = url;
});

/**
* A function to compute the Levenshtein distance between two strings
* Licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported
Expand Down