|
38 | 38 | toggleSource(toggleSourceButton); |
39 | 39 | }); |
40 | 40 | }); |
| 41 | + |
| 42 | + // This code has been adapted from the rustdoc implementation here: |
| 43 | + // https://github.com/rust-lang/rust/blob/5c848860/src/librustdoc/html/static/js/src-script.js#L152-L204 |
| 44 | + function highlightLineNumbers() { |
| 45 | + const match = window.location.hash.match(/^#?(\d+)(?:-(\d+))?$/); |
| 46 | + if (!match) { |
| 47 | + return; |
| 48 | + } |
| 49 | + let from = parseInt(match[1], 10); |
| 50 | + let to = from; |
| 51 | + if (typeof match[2] !== "undefined") { |
| 52 | + to = parseInt(match[2], 10); |
| 53 | + } |
| 54 | + if (to < from) { |
| 55 | + const tmp = to; |
| 56 | + to = from; |
| 57 | + from = tmp; |
| 58 | + } |
| 59 | + let elem = document.getElementById(from); |
| 60 | + if (!elem) { |
| 61 | + return; |
| 62 | + } |
| 63 | + const x = document.getElementById(from); |
| 64 | + if (x) { |
| 65 | + x.scrollIntoView(); |
| 66 | + } |
| 67 | + Array.from(document.getElementsByClassName("line-number-highlighted")).forEach(e => { |
| 68 | + e.classList.remove("line-number-highlighted"); |
| 69 | + }); |
| 70 | + for (let i = from; i <= to; ++i) { |
| 71 | + elem = document.getElementById(i); |
| 72 | + if (!elem) { |
| 73 | + break; |
| 74 | + } |
| 75 | + elem.classList.add("line-number-highlighted"); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + const handleLineNumbers = (function () { |
| 80 | + let prev_line_id = 0; |
| 81 | + |
| 82 | + const set_fragment = name => { |
| 83 | + const x = window.scrollX, |
| 84 | + y = window.scrollY; |
| 85 | + if (window.history && typeof window.history.pushState === "function") { |
| 86 | + history.replaceState(null, null, "#" + name); |
| 87 | + highlightLineNumbers(); |
| 88 | + } else { |
| 89 | + location.replace("#" + name); |
| 90 | + } |
| 91 | + // Prevent jumps when selecting one or many lines |
| 92 | + window.scrollTo(x, y); |
| 93 | + }; |
| 94 | + |
| 95 | + return ev => { |
| 96 | + let cur_line_id = parseInt(ev.target.id, 10); |
| 97 | + // This event handler is attached to the entire line number column, but it should only |
| 98 | + // be run if one of the anchors is clicked. It also shouldn't do anything if the anchor |
| 99 | + // is clicked with a modifier key (to open a new browser tab). |
| 100 | + if (isNaN(cur_line_id) || |
| 101 | + ev.ctrlKey || |
| 102 | + ev.altKey || |
| 103 | + ev.metaKey) { |
| 104 | + return; |
| 105 | + } |
| 106 | + ev.preventDefault(); |
| 107 | + |
| 108 | + if (ev.shiftKey && prev_line_id) { |
| 109 | + // Swap selection if needed |
| 110 | + if (prev_line_id > cur_line_id) { |
| 111 | + const tmp = prev_line_id; |
| 112 | + prev_line_id = cur_line_id; |
| 113 | + cur_line_id = tmp; |
| 114 | + } |
| 115 | + |
| 116 | + set_fragment(prev_line_id + "-" + cur_line_id); |
| 117 | + } else { |
| 118 | + prev_line_id = cur_line_id; |
| 119 | + |
| 120 | + set_fragment(cur_line_id); |
| 121 | + } |
| 122 | + }; |
| 123 | + }()); |
| 124 | + |
| 125 | + window.addEventListener("hashchange", highlightLineNumbers) |
| 126 | + |
| 127 | + Array.from(document.getElementById("line-numbers").children[0].children).forEach(el => { |
| 128 | + el.addEventListener("click", handleLineNumbers); |
| 129 | + }); |
| 130 | + |
| 131 | + highlightLineNumbers(); |
41 | 132 | })(); |
0 commit comments