|
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) {
|
19 | 21 | const lines = elt.querySelectorAll("[data-nosnippet]");
|
20 | 22 | let scrollOffset;
|
|
35 | 37 | scrollOffset = offsetMid - halfHeight;
|
36 | 38 | }
|
37 | 39 |
|
38 |
| - lines[0].parentElement.scrollTo(0, scrollOffset); |
39 |
| - elt.querySelector(".rust").scrollTo(0, scrollOffset); |
| 40 | + nonnull(lines[0].parentElement).scrollTo(0, scrollOffset); |
| 41 | + nonnull(elt.querySelector(".rust")).scrollTo(0, scrollOffset); |
40 | 42 | }
|
41 | 43 |
|
| 44 | + /** |
| 45 | + * @param {HTMLElement} parent |
| 46 | + * @param {string} className |
| 47 | + * @param {string} content |
| 48 | + */ |
42 | 49 | function createScrapeButton(parent, className, content) {
|
43 | 50 | const button = document.createElement("button");
|
44 | 51 | button.className = className;
|
|
50 | 57 | window.updateScrapedExample = (example, buttonHolder) => {
|
51 | 58 | let locIndex = 0;
|
52 | 59 | const highlights = Array.prototype.slice.call(example.querySelectorAll(".highlight"));
|
53 |
| - const link = example.querySelector(".scraped-example-title a"); |
| 60 | + const link = nonnull(example.querySelector(".scraped-example-title a")); |
54 | 61 | let expandButton = null;
|
55 | 62 |
|
56 | 63 | if (!example.classList.contains("expanded")) {
|
57 | 64 | expandButton = createScrapeButton(buttonHolder, "expand", "Show all");
|
58 | 65 | }
|
59 |
| - const isHidden = example.parentElement.classList.contains("more-scraped-examples"); |
| 66 | + const isHidden = nonnull(example.parentElement).classList.contains("more-scraped-examples"); |
60 | 67 |
|
| 68 | + // @ts-expect-error |
61 | 69 | const locs = example.locs;
|
62 | 70 | if (locs.length > 1) {
|
63 | 71 | const next = createScrapeButton(buttonHolder, "next", "Next usage");
|
|
106 | 114 | }
|
107 | 115 | };
|
108 | 116 |
|
| 117 | + /** |
| 118 | + * Intitialize the `locs` field |
| 119 | + * |
| 120 | + * @param {HTMLElement} example |
| 121 | + * @param {boolean} isHidden |
| 122 | + */ |
109 | 123 | function setupLoc(example, isHidden) {
|
| 124 | + // @ts-expect-error |
110 | 125 | example.locs = JSON.parse(example.attributes.getNamedItem("data-locs").textContent);
|
111 | 126 | // Start with the first example in view
|
112 | 127 | scrollToLoc(example, example.locs[0][0], isHidden);
|
|
0 commit comments