librustdoc: generalise handling of keyboard shortcuts
This commit is contained in:
parent
fd8e175c4e
commit
8792c5c040
1 changed files with 47 additions and 43 deletions
|
@ -76,62 +76,65 @@
|
||||||
highlightSourceLines(null);
|
highlightSourceLines(null);
|
||||||
$(window).on('hashchange', highlightSourceLines);
|
$(window).on('hashchange', highlightSourceLines);
|
||||||
|
|
||||||
// Helper function for Keyboard events,
|
// Gets the human-readable string for the virtual-key code of the
|
||||||
// Get's the char from the keypress event
|
// given KeyboardEvent, ev.
|
||||||
//
|
//
|
||||||
// This method is used because e.wich === x is not
|
// This function is meant as a polyfill for KeyboardEvent#key,
|
||||||
// compatible with non-english keyboard layouts
|
// 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 !
|
// So I guess you could say things are getting pretty interoperable.
|
||||||
function getChar(event) {
|
function getVirtualKey(ev) {
|
||||||
if (event.which == null) {
|
if ("key" in ev && typeof ev.key != "undefined")
|
||||||
return String.fromCharCode(event.keyCode) // IE
|
return ev.key;
|
||||||
} else if (event.which!=0 && event.charCode!=0) {
|
|
||||||
return String.fromCharCode(event.which) // the rest
|
var c = ev.charCode || ev.keyCode;
|
||||||
} else {
|
if (c == 27)
|
||||||
return null // special key
|
return "Escape";
|
||||||
}
|
return String.fromCharCode(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
$(document).on('keypress', function handleKeyboardShortcut(e) {
|
function handleShortcut(ev) {
|
||||||
if (document.activeElement.tagName === 'INPUT') {
|
if (document.activeElement.tagName == "INPUT")
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
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");
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "s":
|
||||||
|
case "S":
|
||||||
|
ev.preventDefault();
|
||||||
|
$(".search-input").focus();
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "?":
|
||||||
|
if (ev.shiftKey && $("#help").hasClass("hidden")) {
|
||||||
|
ev.preventDefault();
|
||||||
|
$("#help").removeClass("hidden");
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (getChar(e) === '?') {
|
$(document).on("keypress", handleShortcut);
|
||||||
if (e.shiftKey && $('#help').hasClass('hidden')) {
|
$(document).on("keydown", handleShortcut);
|
||||||
e.preventDefault();
|
$(document).on("click", function(ev) {
|
||||||
$('#help').removeClass('hidden');
|
if (!$(ev.target).closest("#help").length) {
|
||||||
}
|
$("#help").addClass("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');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}).on('click', function(e) {
|
|
||||||
if (!$(e.target).closest('#help').length) {
|
|
||||||
$('#help').addClass('hidden');
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
$('.version-selector').on('change', function() {
|
$('.version-selector').on('change', function() {
|
||||||
var i, match,
|
var i, match,
|
||||||
url = document.location.href,
|
url = document.location.href,
|
||||||
|
@ -150,6 +153,7 @@
|
||||||
|
|
||||||
document.location.href = url;
|
document.location.href = url;
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A function to compute the Levenshtein distance between two strings
|
* A function to compute the Levenshtein distance between two strings
|
||||||
* Licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported
|
* Licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue