|
39 | 39 | const pairs = new Map(),
|
40 | 40 | ellipsis = document.createElement("span"),
|
41 | 41 | triangle = document.createElement("span");
|
| 42 | + let isMobile = false; |
42 | 43 |
|
43 | 44 | triangle.className = "collapser";
|
44 | 45 | ellipsis.className = "pl-smi ellipsis";
|
45 | 46 | ellipsis.innerHTML = "…";
|
46 | 47 |
|
47 | 48 | function countInitialWhiteSpace(arr) {
|
48 | 49 | const getWhiteSpaceIndex = i => {
|
49 |
| - if (arr[i] !== " " && arr[i] !== "\t") { |
| 50 | + if (arr[i] !== " " && arr[i] !== "\t" && |
| 51 | + (!isMobile || arr[i] !== "\xa0")) { |
50 | 52 | return i;
|
51 | 53 | }
|
52 | 54 | i++;
|
|
65 | 67 | }
|
66 | 68 |
|
67 | 69 | function getLineNumber(el) {
|
68 |
| - let elm = el.closest("td"), |
| 70 | + let elm = el.closest(isMobile ? "div" : "td"), |
69 | 71 | index = elm ? elm.id : "";
|
70 | 72 | if (index) {
|
71 | 73 | return parseInt(index.slice(2), 10);
|
72 | 74 | }
|
73 | 75 | return "";
|
74 | 76 | }
|
75 | 77 |
|
| 78 | + function getCodeLines() { |
| 79 | + return $$(isMobile ? ".blob-file-content .line" : |
| 80 | + ".file table.highlight .blob-code-inner"); |
| 81 | + } |
| 82 | + |
76 | 83 | function toggleCode(action, index, depth) {
|
77 | 84 | let els, lineNums;
|
78 |
| - const codeLines = $$(".file table.highlight .blob-code-inner"); |
| 85 | + const codeLines = getCodeLines(); |
79 | 86 | // depth is a string containing a specific depth number to toggle
|
80 | 87 | if (depth) {
|
81 | 88 | els = $$(`.collapser[data-depth="${depth}"]`);
|
|
92 | 99 | let elm,
|
93 | 100 | end = pairs.get(start - 1);
|
94 | 101 | codeLines.slice(start, end).forEach(el => {
|
95 |
| - elm = el.closest("tr"); |
| 102 | + elm = isMobile ? el : el.closest("tr"); |
96 | 103 | if (elm) {
|
97 | 104 | elm.classList.add("hidden-line");
|
98 | 105 | }
|
|
109 | 116 | lineNums.forEach(start => {
|
110 | 117 | let end = pairs.get(start - 1);
|
111 | 118 | codeLines.slice(start, end).forEach(el => {
|
112 |
| - let elm = el.closest("tr"); |
| 119 | + let elm = isMobile ? el : el.closest("tr"); |
113 | 120 | if (elm) {
|
114 | 121 | elm.classList.remove("hidden-line");
|
115 | 122 | remove(".ellipsis", elm);
|
|
172 | 179 | }
|
173 | 180 |
|
174 | 181 | function addCodeFolding() {
|
175 |
| - if ($(".file table.highlight")) { |
| 182 | + if ($(".file table.highlight") || $("div.blob-file-content")) { |
| 183 | + isMobile = !$(".file table.highlight"); |
| 184 | + |
176 | 185 | // In case this script has already been run and modified the DOM on a
|
177 | 186 | // previous page in github, make sure to reset it.
|
178 | 187 | remove("span.collapser");
|
179 | 188 | pairs.clear();
|
180 | 189 |
|
181 |
| - const codeLines = $$(".file table.highlight .blob-code-inner"), |
| 190 | + const codeLines = getCodeLines(), |
182 | 191 | spaceMap = new Map(),
|
183 | 192 | stack = [];
|
184 | 193 |
|
|
0 commit comments