Skip to content

Commit 8792c5c

Browse files
committed
librustdoc: generalise handling of keyboard shortcuts
1 parent fd8e175 commit 8792c5c

File tree

1 file changed

+48
-44
lines changed

1 file changed

+48
-44
lines changed

src/librustdoc/html/static/main.js

Lines changed: 48 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -76,62 +76,65 @@
7676
highlightSourceLines(null);
7777
$(window).on('hashchange', highlightSourceLines);
7878

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

96-
$(document).on('keypress', function handleKeyboardShortcut(e) {
97-
if (document.activeElement.tagName === 'INPUT') {
99+
function handleShortcut(ev) {
100+
if (document.activeElement.tagName == "INPUT")
98101
return;
99-
}
100102

101-
if (getChar(e) === '?') {
102-
if (e.shiftKey && $('#help').hasClass('hidden')) {
103-
e.preventDefault();
104-
$('#help').removeClass('hidden');
103+
switch (getVirtualKey(ev)) {
104+
case "Escape":
105+
if (!$("#help").hasClass("hidden")) {
106+
ev.preventDefault();
107+
$("#help").addClass("hidden");
108+
} else if (!$("#search").hasClass("hidden")) {
109+
ev.preventDefault();
110+
$("#search").addClass("hidden");
111+
$("#main").removeClass("hidden");
105112
}
106-
} else if (getChar(e) === 's' || getChar(e) === 'S') {
107-
e.preventDefault();
108-
$('.search-input').focus();
109-
}
110-
}).on('keydown', function(e) {
111-
// The escape key event has to be captured with the keydown event.
112-
// Because keypressed has no keycode for the escape key
113-
// (and other special keys in general)...
114-
if (document.activeElement.tagName === 'INPUT') {
115-
return;
116-
}
117-
118-
if (e.keyCode === 27) { // escape key
119-
if (!$('#help').hasClass('hidden')) {
120-
e.preventDefault();
121-
$('#help').addClass('hidden');
122-
} else if (!$('#search').hasClass('hidden')) {
123-
e.preventDefault();
124-
$('#search').addClass('hidden');
125-
$('#main').removeClass('hidden');
113+
break;
114+
115+
case "s":
116+
case "S":
117+
ev.preventDefault();
118+
$(".search-input").focus();
119+
break;
120+
121+
case "?":
122+
if (ev.shiftKey && $("#help").hasClass("hidden")) {
123+
ev.preventDefault();
124+
$("#help").removeClass("hidden");
126125
}
126+
break;
127127
}
128-
}).on('click', function(e) {
129-
if (!$(e.target).closest('#help').length) {
130-
$('#help').addClass('hidden');
128+
}
129+
130+
$(document).on("keypress", handleShortcut);
131+
$(document).on("keydown", handleShortcut);
132+
$(document).on("click", function(ev) {
133+
if (!$(ev.target).closest("#help").length) {
134+
$("#help").addClass("hidden");
131135
}
132136
});
133137

134-
135138
$('.version-selector').on('change', function() {
136139
var i, match,
137140
url = document.location.href,
@@ -150,6 +153,7 @@
150153

151154
document.location.href = url;
152155
});
156+
153157
/**
154158
* A function to compute the Levenshtein distance between two strings
155159
* Licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported

0 commit comments

Comments
 (0)