diff options
| author | Will Crichton <wcrichto@cs.stanford.edu> | 2022-12-07 10:24:16 -0800 |
|---|---|---|
| committer | Will Crichton <wcrichto@cs.stanford.edu> | 2022-12-07 10:42:09 -0800 |
| commit | 9499d2cce3bebef96d8ae64442602f87726a875a (patch) | |
| tree | b4ffbfdb3873f76cb44e8dd69e06b3bc51919632 | |
| parent | 8a459384ad02d120f1d1cc81166f95262c1d4fac (diff) | |
| download | rust-9499d2cce3bebef96d8ae64442602f87726a875a.tar.gz rust-9499d2cce3bebef96d8ae64442602f87726a875a.zip | |
Improve calculation of scraped example minimized height
| -rw-r--r-- | src/librustdoc/html/static/css/rustdoc.css | 12 | ||||
| -rw-r--r-- | src/librustdoc/html/static/js/scrape-examples.js | 8 |
2 files changed, 15 insertions, 5 deletions
diff --git a/src/librustdoc/html/static/css/rustdoc.css b/src/librustdoc/html/static/css/rustdoc.css index 6e5e293780d..afed3da9e91 100644 --- a/src/librustdoc/html/static/css/rustdoc.css +++ b/src/librustdoc/html/static/css/rustdoc.css @@ -1931,18 +1931,24 @@ in storage.js } .scraped-example:not(.expanded) .code-wrapper { - max-height: 120px; + /* scrape-examples.js has a constant DEFAULT_MAX_LINES (call it N) for the number + * of lines shown in the un-expanded example code viewer. This pre needs to have + * a max-height equal to line-height * N. The line-height is currently 1.5em, + * and we include additional 10px for padding. */ + max-height: calc(1.5em * 5 + 10px); } .scraped-example:not(.expanded) .code-wrapper pre { overflow-y: hidden; - max-height: 120px; padding-bottom: 0; + /* See above comment, should be the same max-height. */ + max-height: calc(1.5em * 5 + 10px); } .more-scraped-examples .scraped-example:not(.expanded) .code-wrapper, .more-scraped-examples .scraped-example:not(.expanded) .code-wrapper pre { - max-height: 240px; + /* See above comment, except this height is based on HIDDEN_MAX_LINES. */ + max-height: calc(1.5em * 10 + 10px); } .scraped-example .code-wrapper .next, diff --git a/src/librustdoc/html/static/js/scrape-examples.js b/src/librustdoc/html/static/js/scrape-examples.js index 830b44d6bc0..7a3a9c5f340 100644 --- a/src/librustdoc/html/static/js/scrape-examples.js +++ b/src/librustdoc/html/static/js/scrape-examples.js @@ -6,6 +6,8 @@ // Number of lines shown when code viewer is not expanded. // DEFAULT is the first example shown by default, while HIDDEN is // the examples hidden beneath the "More examples" toggle. + // + // NOTE: these values MUST be synchronized with certain rules in rustdoc.css! const DEFAULT_MAX_LINES = 5; const HIDDEN_MAX_LINES = 10; @@ -24,8 +26,10 @@ } else { const wrapper = elt.querySelector(".code-wrapper"); const halfHeight = wrapper.offsetHeight / 2; - const offsetMid = (lines.children[loc[0]].offsetTop - + lines.children[loc[1]].offsetTop) / 2; + const offsetTop = lines.children[loc[0]].offsetTop; + const lastLine = lines.children[loc[1]]; + const offsetBot = lastLine.offsetTop + lastLine.offsetHeight; + const offsetMid = (offsetTop + offsetBot) / 2; scrollOffset = offsetMid - halfHeight; } |
