|
4 | 4 |
|
5 | 5 | (function() { |
6 | 6 | // Number of lines shown when code viewer is not expanded |
7 | | - const MAX_LINES = 10; |
| 7 | + const DEFAULT_MAX_LINES = 5; |
| 8 | + const HIDDEN_MAX_LINES = 10; |
8 | 9 |
|
9 | 10 | // Scroll code block to the given code location |
10 | | - function scrollToLoc(elt, loc) { |
| 11 | + function scrollToLoc(elt, loc, isHidden) { |
11 | 12 | const lines = elt.querySelector(".src-line-numbers"); |
12 | 13 | let scrollOffset; |
13 | 14 |
|
14 | 15 | // If the block is greater than the size of the viewer, |
15 | 16 | // then scroll to the top of the block. Otherwise scroll |
16 | 17 | // to the middle of the block. |
17 | | - if (loc[1] - loc[0] > MAX_LINES) { |
| 18 | + let maxLines = isHidden ? HIDDEN_MAX_LINES : DEFAULT_MAX_LINES; |
| 19 | + if (loc[1] - loc[0] > maxLines) { |
18 | 20 | const line = Math.max(0, loc[0] - 1); |
19 | 21 | scrollOffset = lines.children[line].offsetTop; |
20 | 22 | } else { |
|
29 | 31 | elt.querySelector(".rust").scrollTo(0, scrollOffset); |
30 | 32 | } |
31 | 33 |
|
32 | | - function updateScrapedExample(example) { |
| 34 | + function updateScrapedExample(example, isHidden) { |
33 | 35 | const locs = JSON.parse(example.attributes.getNamedItem("data-locs").textContent); |
34 | 36 | let locIndex = 0; |
35 | 37 | const highlights = Array.prototype.slice.call(example.querySelectorAll(".highlight")); |
|
40 | 42 | const onChangeLoc = changeIndex => { |
41 | 43 | removeClass(highlights[locIndex], "focus"); |
42 | 44 | changeIndex(); |
43 | | - scrollToLoc(example, locs[locIndex][0]); |
| 45 | + scrollToLoc(example, locs[locIndex][0], isHidden); |
44 | 46 | addClass(highlights[locIndex], "focus"); |
45 | 47 |
|
46 | 48 | const url = locs[locIndex][1]; |
|
70 | 72 | expandButton.addEventListener("click", () => { |
71 | 73 | if (hasClass(example, "expanded")) { |
72 | 74 | removeClass(example, "expanded"); |
73 | | - scrollToLoc(example, locs[0][0]); |
| 75 | + scrollToLoc(example, locs[0][0], isHidden); |
74 | 76 | } else { |
75 | 77 | addClass(example, "expanded"); |
76 | 78 | } |
77 | 79 | }); |
78 | 80 | } |
79 | 81 |
|
80 | 82 | // Start with the first example in view |
81 | | - scrollToLoc(example, locs[0][0]); |
| 83 | + scrollToLoc(example, locs[0][0], isHidden); |
82 | 84 | } |
83 | 85 |
|
84 | 86 | const firstExamples = document.querySelectorAll(".scraped-example-list > .scraped-example"); |
85 | | - onEachLazy(firstExamples, updateScrapedExample); |
| 87 | + onEachLazy(firstExamples, el => updateScrapedExample(el, false)); |
86 | 88 | onEachLazy(document.querySelectorAll(".more-examples-toggle"), toggle => { |
87 | 89 | // Allow users to click the left border of the <details> section to close it, |
88 | 90 | // since the section can be large and finding the [+] button is annoying. |
|
99 | 101 | // depends on offsetHeight, a property that requires an element to be visible to |
100 | 102 | // compute correctly. |
101 | 103 | setTimeout(() => { |
102 | | - onEachLazy(moreExamples, updateScrapedExample); |
| 104 | + onEachLazy(moreExamples, el => updateScrapedExample(el, true)); |
103 | 105 | }); |
104 | 106 | }, {once: true}); |
105 | 107 | }); |
|
0 commit comments