|
3 | 3 | "use strict"; |
4 | 4 |
|
5 | 5 | (function() { |
6 | | - // Number of lines shown when code viewer is not expanded |
7 | | - const MAX_LINES = 10; |
| 6 | + // Number of lines shown when code viewer is not expanded. |
| 7 | + // DEFAULT is the first example shown by default, while HIDDEN is |
| 8 | + // the examples hidden beneath the "More examples" toggle. |
| 9 | + // |
| 10 | + // NOTE: these values MUST be synchronized with certain rules in rustdoc.css! |
| 11 | + const DEFAULT_MAX_LINES = 5; |
| 12 | + const HIDDEN_MAX_LINES = 10; |
8 | 13 |
|
9 | 14 | // Scroll code block to the given code location |
10 | | - function scrollToLoc(elt, loc) { |
| 15 | + function scrollToLoc(elt, loc, isHidden) { |
11 | 16 | const lines = elt.querySelector(".src-line-numbers"); |
12 | 17 | let scrollOffset; |
13 | 18 |
|
14 | 19 | // If the block is greater than the size of the viewer, |
15 | 20 | // then scroll to the top of the block. Otherwise scroll |
16 | 21 | // to the middle of the block. |
17 | | - if (loc[1] - loc[0] > MAX_LINES) { |
| 22 | + const maxLines = isHidden ? HIDDEN_MAX_LINES : DEFAULT_MAX_LINES; |
| 23 | + if (loc[1] - loc[0] > maxLines) { |
18 | 24 | const line = Math.max(0, loc[0] - 1); |
19 | 25 | scrollOffset = lines.children[line].offsetTop; |
20 | 26 | } else { |
21 | 27 | const wrapper = elt.querySelector(".code-wrapper"); |
22 | 28 | const halfHeight = wrapper.offsetHeight / 2; |
23 | | - const offsetMid = (lines.children[loc[0]].offsetTop |
24 | | - + lines.children[loc[1]].offsetTop) / 2; |
| 29 | + const offsetTop = lines.children[loc[0]].offsetTop; |
| 30 | + const lastLine = lines.children[loc[1]]; |
| 31 | + const offsetBot = lastLine.offsetTop + lastLine.offsetHeight; |
| 32 | + const offsetMid = (offsetTop + offsetBot) / 2; |
25 | 33 | scrollOffset = offsetMid - halfHeight; |
26 | 34 | } |
27 | 35 |
|
28 | 36 | lines.scrollTo(0, scrollOffset); |
29 | 37 | elt.querySelector(".rust").scrollTo(0, scrollOffset); |
30 | 38 | } |
31 | 39 |
|
32 | | - function updateScrapedExample(example) { |
| 40 | + function updateScrapedExample(example, isHidden) { |
33 | 41 | const locs = JSON.parse(example.attributes.getNamedItem("data-locs").textContent); |
34 | 42 | let locIndex = 0; |
35 | 43 | const highlights = Array.prototype.slice.call(example.querySelectorAll(".highlight")); |
|
40 | 48 | const onChangeLoc = changeIndex => { |
41 | 49 | removeClass(highlights[locIndex], "focus"); |
42 | 50 | changeIndex(); |
43 | | - scrollToLoc(example, locs[locIndex][0]); |
| 51 | + scrollToLoc(example, locs[locIndex][0], isHidden); |
44 | 52 | addClass(highlights[locIndex], "focus"); |
45 | 53 |
|
46 | 54 | const url = locs[locIndex][1]; |
|
70 | 78 | expandButton.addEventListener("click", () => { |
71 | 79 | if (hasClass(example, "expanded")) { |
72 | 80 | removeClass(example, "expanded"); |
73 | | - scrollToLoc(example, locs[0][0]); |
| 81 | + scrollToLoc(example, locs[0][0], isHidden); |
74 | 82 | } else { |
75 | 83 | addClass(example, "expanded"); |
76 | 84 | } |
77 | 85 | }); |
78 | 86 | } |
79 | 87 |
|
80 | 88 | // Start with the first example in view |
81 | | - scrollToLoc(example, locs[0][0]); |
| 89 | + scrollToLoc(example, locs[0][0], isHidden); |
82 | 90 | } |
83 | 91 |
|
84 | 92 | const firstExamples = document.querySelectorAll(".scraped-example-list > .scraped-example"); |
85 | | - onEachLazy(firstExamples, updateScrapedExample); |
| 93 | + onEachLazy(firstExamples, el => updateScrapedExample(el, false)); |
86 | 94 | onEachLazy(document.querySelectorAll(".more-examples-toggle"), toggle => { |
87 | 95 | // Allow users to click the left border of the <details> section to close it, |
88 | 96 | // since the section can be large and finding the [+] button is annoying. |
|
99 | 107 | // depends on offsetHeight, a property that requires an element to be visible to |
100 | 108 | // compute correctly. |
101 | 109 | setTimeout(() => { |
102 | | - onEachLazy(moreExamples, updateScrapedExample); |
| 110 | + onEachLazy(moreExamples, el => updateScrapedExample(el, true)); |
103 | 111 | }); |
104 | 112 | }, {once: true}); |
105 | 113 | }); |
|
0 commit comments