Skip to content

Commit a651106

Browse files
committed
Auto merge of #45288 - GuillaumeGomez:tab-key-binding, r=QuietMisdreavus
Save the highlighted item when switching tab To be merged after #45281. r? @rust-lang/docs
2 parents 4750c1e + f442326 commit a651106

File tree

3 files changed

+34
-17
lines changed

3 files changed

+34
-17
lines changed

src/librustdoc/html/layout.rs

+2
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ r##"<!DOCTYPE html>
101101
<dd>Move up in search results</dd>
102102
<dt>↓</dt>
103103
<dd>Move down in search results</dd>
104+
<dt>↹</dt>
105+
<dd>Switch tab</dd>
104106
<dt>&#9166;</dt>
105107
<dd>Go to active search result</dd>
106108
<dt>+</dt>

src/librustdoc/html/static/main.js

+31-16
Original file line numberDiff line numberDiff line change
@@ -712,41 +712,56 @@
712712
});
713713

714714
var search_input = document.getElementsByClassName('search-input')[0];
715-
search_input.onkeydown = null;
716715
search_input.onkeydown = function(e) {
717-
var actives = [];
716+
// "actives" references the currently highlighted item in each search tab.
717+
// Each array in "actives" represents a tab.
718+
var actives = [[], [], []];
719+
// "current" is used to know which tab we're looking into.
720+
var current = 0;
718721
onEach(document.getElementsByClassName('search-results'), function(e) {
719-
onEach(document.getElementsByClassName('highlighted'), function(e) {
720-
actives.push(e);
722+
onEach(e.getElementsByClassName('highlighted'), function(e) {
723+
actives[current].push(e);
721724
});
725+
current += 1;
722726
});
723727

724728
if (e.which === 38) { // up
725-
if (!actives.length || !actives[0].previousElementSibling) {
729+
if (!actives[currentTab].length ||
730+
!actives[currentTab][0].previousElementSibling) {
726731
return;
727732
}
728733

729-
addClass(actives[0].previousElementSibling, 'highlighted');
730-
removeClass(actives[0], 'highlighted');
734+
addClass(actives[currentTab][0].previousElementSibling, 'highlighted');
735+
removeClass(actives[currentTab][0], 'highlighted');
731736
} else if (e.which === 40) { // down
732-
if (!actives.length) {
737+
if (!actives[currentTab].length) {
733738
var results = document.getElementsByClassName('search-results');
734739
if (results.length > 0) {
735-
var res = results[0].getElementsByClassName('result');
740+
var res = results[currentTab].getElementsByClassName('result');
736741
if (res.length > 0) {
737742
addClass(res[0], 'highlighted');
738743
}
739744
}
740-
} else if (actives[0].nextElementSibling) {
741-
addClass(actives[0].nextElementSibling, 'highlighted');
742-
removeClass(actives[0], 'highlighted');
745+
} else if (actives[currentTab][0].nextElementSibling) {
746+
addClass(actives[currentTab][0].nextElementSibling, 'highlighted');
747+
removeClass(actives[currentTab][0], 'highlighted');
743748
}
744749
} else if (e.which === 13) { // return
745-
if (actives.length) {
746-
document.location.href = actives[0].getElementsByTagName('a')[0].href;
750+
if (actives[currentTab].length) {
751+
document.location.href =
752+
actives[currentTab][0].getElementsByTagName('a')[0].href;
747753
}
748-
} else if (actives.length > 0) {
749-
removeClass(actives[0], 'highlighted');
754+
} else if (e.which === 9) { // tab
755+
if (e.shiftKey) {
756+
printTab(currentTab > 0 ? currentTab - 1 : 2);
757+
} else {
758+
printTab(currentTab > 1 ? 0 : currentTab + 1);
759+
}
760+
e.preventDefault();
761+
} else if (e.which === 16) { // shift
762+
// Does nothing, it's just to avoid losing "focus" on the highlighted element.
763+
} else if (actives[currentTab].length > 0) {
764+
removeClass(actives[currentTab][0], 'highlighted');
750765
}
751766
};
752767
}

src/librustdoc/html/static/rustdoc.css

+1-1
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ body.blur > :not(#help) {
552552
flex: 0 0 auto;
553553
box-shadow: 0 0 6px rgba(0,0,0,.2);
554554
width: 550px;
555-
height: 330px;
555+
height: 354px;
556556
border: 1px solid;
557557
}
558558
#help dt {

0 commit comments

Comments
 (0)