|
1 |
| -/* global addClass, hasClass, removeClass, onEachLazy */ |
2 |
| - |
3 |
| -// Eventually fix this. |
4 |
| -// @ts-nocheck |
| 1 | + /* global addClass, hasClass, removeClass, onEachLazy, nonnull */ |
5 | 2 |
|
6 | 3 | "use strict";
|
7 | 4 |
|
|
14 | 11 | const DEFAULT_MAX_LINES = 5;
|
15 | 12 | const HIDDEN_MAX_LINES = 10;
|
16 | 13 |
|
17 |
| - // Scroll code block to the given code location |
| 14 | + /** |
| 15 | + * Scroll code block to the given code location |
| 16 | + * @param {HTMLElement} elt |
| 17 | + * @param {[number, number]} loc |
| 18 | + * @param {boolean} isHidden |
| 19 | + */ |
18 | 20 | function scrollToLoc(elt, loc, isHidden) {
|
| 21 | + /** @type {HTMLElement[]} */ |
| 22 | + // blocked on https://github.com/microsoft/TypeScript/issues/29037 |
| 23 | + // @ts-expect-error |
19 | 24 | const lines = elt.querySelectorAll("[data-nosnippet]");
|
20 | 25 | let scrollOffset;
|
21 | 26 |
|
|
35 | 40 | scrollOffset = offsetMid - halfHeight;
|
36 | 41 | }
|
37 | 42 |
|
38 |
| - lines[0].parentElement.scrollTo(0, scrollOffset); |
39 |
| - elt.querySelector(".rust").scrollTo(0, scrollOffset); |
| 43 | + nonnull(lines[0].parentElement).scrollTo(0, scrollOffset); |
| 44 | + nonnull(elt.querySelector(".rust")).scrollTo(0, scrollOffset); |
40 | 45 | }
|
41 | 46 |
|
| 47 | + /** |
| 48 | + * @param {HTMLElement} parent |
| 49 | + * @param {string} className |
| 50 | + * @param {string} content |
| 51 | + */ |
42 | 52 | function createScrapeButton(parent, className, content) {
|
43 | 53 | const button = document.createElement("button");
|
44 | 54 | button.className = className;
|
|
50 | 60 | window.updateScrapedExample = (example, buttonHolder) => {
|
51 | 61 | let locIndex = 0;
|
52 | 62 | const highlights = Array.prototype.slice.call(example.querySelectorAll(".highlight"));
|
53 |
| - const link = example.querySelector(".scraped-example-title a"); |
| 63 | + |
| 64 | + /** @type {HTMLAnchorElement} */ |
| 65 | + const link = nonnull(example.querySelector(".scraped-example-title a")); |
54 | 66 | let expandButton = null;
|
55 | 67 |
|
56 | 68 | if (!example.classList.contains("expanded")) {
|
57 | 69 | expandButton = createScrapeButton(buttonHolder, "expand", "Show all");
|
58 | 70 | }
|
59 |
| - const isHidden = example.parentElement.classList.contains("more-scraped-examples"); |
| 71 | + const isHidden = nonnull(example.parentElement).classList.contains("more-scraped-examples"); |
60 | 72 |
|
| 73 | + // @ts-expect-error |
61 | 74 | const locs = example.locs;
|
62 | 75 | if (locs.length > 1) {
|
63 | 76 | const next = createScrapeButton(buttonHolder, "next", "Next usage");
|
64 | 77 | const prev = createScrapeButton(buttonHolder, "prev", "Previous usage");
|
65 | 78 |
|
66 | 79 | // Toggle through list of examples in a given file
|
| 80 | + /** @type {function(function(): void): void} */ |
67 | 81 | const onChangeLoc = changeIndex => {
|
68 | 82 | removeClass(highlights[locIndex], "focus");
|
69 | 83 | changeIndex();
|
|
106 | 120 | }
|
107 | 121 | };
|
108 | 122 |
|
| 123 | + /** |
| 124 | + * Initialize the `locs` field |
| 125 | + * |
| 126 | + * @param {HTMLElement & {locs?: rustdoc.ScrapedLoc[]}} example |
| 127 | + * @param {boolean} isHidden |
| 128 | + */ |
109 | 129 | function setupLoc(example, isHidden) {
|
110 |
| - example.locs = JSON.parse(example.attributes.getNamedItem("data-locs").textContent); |
| 130 | + const locs_str = nonnull(example.attributes.getNamedItem("data-locs")).textContent; |
| 131 | + const locs = |
| 132 | + JSON.parse(nonnull(nonnull(locs_str))); |
| 133 | + example.locs = locs; |
111 | 134 | // Start with the first example in view
|
112 |
| - scrollToLoc(example, example.locs[0][0], isHidden); |
| 135 | + scrollToLoc(example, locs[0][0], isHidden); |
113 | 136 | }
|
114 | 137 |
|
115 | 138 | const firstExamples = document.querySelectorAll(".scraped-example-list > .scraped-example");
|
|
0 commit comments