diff options
| author | Will Crichton <wcrichto@cs.stanford.edu> | 2022-11-27 13:11:21 -0600 |
|---|---|---|
| committer | Will Crichton <wcrichto@cs.stanford.edu> | 2022-12-07 09:08:43 -0800 |
| commit | 6ccd14a782420d27aef9d9fcd9196949a3276427 (patch) | |
| tree | 56608f31f109012632cad07581c812b5ba15592b /src/librustdoc/html/static | |
| parent | 01fbc5ae789fc0c7a2da71d3cd908451f175e4eb (diff) | |
| download | rust-6ccd14a782420d27aef9d9fcd9196949a3276427.tar.gz rust-6ccd14a782420d27aef9d9fcd9196949a3276427.zip | |
Improve several aspects of the Rustdoc scrape-examples UI.
* Examples take up less screen height. * Snippets from binary crates are prioritized. * toggle-all-docs does not expand "More examples" sections.
Diffstat (limited to 'src/librustdoc/html/static')
| -rw-r--r-- | src/librustdoc/html/static/css/rustdoc.css | 23 | ||||
| -rw-r--r-- | src/librustdoc/html/static/js/main.js | 2 | ||||
| -rw-r--r-- | src/librustdoc/html/static/js/scrape-examples.js | 20 |
3 files changed, 33 insertions, 12 deletions
diff --git a/src/librustdoc/html/static/css/rustdoc.css b/src/librustdoc/html/static/css/rustdoc.css index 5ebc545d10c..4a8babaef75 100644 --- a/src/librustdoc/html/static/css/rustdoc.css +++ b/src/librustdoc/html/static/css/rustdoc.css @@ -1901,6 +1901,10 @@ in storage.js border-radius: 50px; } +.scraped-example { + position: relative; +} + .scraped-example .code-wrapper { position: relative; display: flex; @@ -1909,16 +1913,31 @@ in storage.js width: 100%; } +.scraped-example-title { + position: absolute; + z-index: 1000; + background: white; + bottom: 8px; + right: 5px; + padding: 2px 4px; + box-shadow: 0 0 4px white; +} + .scraped-example:not(.expanded) .code-wrapper { - max-height: 240px; + max-height: 120px; } .scraped-example:not(.expanded) .code-wrapper pre { overflow-y: hidden; - max-height: 240px; + max-height: 120px; padding-bottom: 0; } +.more-scraped-examples .scraped-example:not(.expanded) .code-wrapper, +.more-scraped-examples .scraped-example:not(.expanded) .code-wrapper pre { + max-height: 240px; +} + .scraped-example .code-wrapper .next, .scraped-example .code-wrapper .prev, .scraped-example .code-wrapper .expand { diff --git a/src/librustdoc/html/static/js/main.js b/src/librustdoc/html/static/js/main.js index 623f46b1096..152116089c7 100644 --- a/src/librustdoc/html/static/js/main.js +++ b/src/librustdoc/html/static/js/main.js @@ -622,7 +622,7 @@ function loadCss(cssUrl) { const innerToggle = document.getElementById(toggleAllDocsId); removeClass(innerToggle, "will-expand"); onEachLazy(document.getElementsByClassName("rustdoc-toggle"), e => { - if (!hasClass(e, "type-contents-toggle")) { + if (!hasClass(e, "type-contents-toggle") && !hasClass(e, "more-examples-toggle")) { e.open = true; } }); diff --git a/src/librustdoc/html/static/js/scrape-examples.js b/src/librustdoc/html/static/js/scrape-examples.js index e328e656edd..82b71ad0fca 100644 --- a/src/librustdoc/html/static/js/scrape-examples.js +++ b/src/librustdoc/html/static/js/scrape-examples.js @@ -4,17 +4,19 @@ (function() { // Number of lines shown when code viewer is not expanded - const MAX_LINES = 10; + const DEFAULT_MAX_LINES = 5; + const HIDDEN_MAX_LINES = 10; // Scroll code block to the given code location - function scrollToLoc(elt, loc) { + function scrollToLoc(elt, loc, isHidden) { const lines = elt.querySelector(".src-line-numbers"); let scrollOffset; // If the block is greater than the size of the viewer, // then scroll to the top of the block. Otherwise scroll // to the middle of the block. - if (loc[1] - loc[0] > MAX_LINES) { + let maxLines = isHidden ? HIDDEN_MAX_LINES : DEFAULT_MAX_LINES; + if (loc[1] - loc[0] > maxLines) { const line = Math.max(0, loc[0] - 1); scrollOffset = lines.children[line].offsetTop; } else { @@ -29,7 +31,7 @@ elt.querySelector(".rust").scrollTo(0, scrollOffset); } - function updateScrapedExample(example) { + function updateScrapedExample(example, isHidden) { const locs = JSON.parse(example.attributes.getNamedItem("data-locs").textContent); let locIndex = 0; const highlights = Array.prototype.slice.call(example.querySelectorAll(".highlight")); @@ -40,7 +42,7 @@ const onChangeLoc = changeIndex => { removeClass(highlights[locIndex], "focus"); changeIndex(); - scrollToLoc(example, locs[locIndex][0]); + scrollToLoc(example, locs[locIndex][0], isHidden); addClass(highlights[locIndex], "focus"); const url = locs[locIndex][1]; @@ -70,7 +72,7 @@ expandButton.addEventListener("click", () => { if (hasClass(example, "expanded")) { removeClass(example, "expanded"); - scrollToLoc(example, locs[0][0]); + scrollToLoc(example, locs[0][0], isHidden); } else { addClass(example, "expanded"); } @@ -78,11 +80,11 @@ } // Start with the first example in view - scrollToLoc(example, locs[0][0]); + scrollToLoc(example, locs[0][0], isHidden); } const firstExamples = document.querySelectorAll(".scraped-example-list > .scraped-example"); - onEachLazy(firstExamples, updateScrapedExample); + onEachLazy(firstExamples, el => updateScrapedExample(el, false)); onEachLazy(document.querySelectorAll(".more-examples-toggle"), toggle => { // Allow users to click the left border of the <details> section to close it, // since the section can be large and finding the [+] button is annoying. @@ -99,7 +101,7 @@ // depends on offsetHeight, a property that requires an element to be visible to // compute correctly. setTimeout(() => { - onEachLazy(moreExamples, updateScrapedExample); + onEachLazy(moreExamples, el => updateScrapedExample(el, true)); }); }, {once: true}); }); |
