Skip to content

Commit 0f99a65

Browse files
zb3Mottie
authored andcommitted
Make github-code-folding.user.js work on mobile layout (#47)
1 parent fca41d2 commit 0f99a65

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

github-code-folding.user.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,16 @@
3939
const pairs = new Map(),
4040
ellipsis = document.createElement("span"),
4141
triangle = document.createElement("span");
42+
let isMobile = false;
4243

4344
triangle.className = "collapser";
4445
ellipsis.className = "pl-smi ellipsis";
4546
ellipsis.innerHTML = "…";
4647

4748
function countInitialWhiteSpace(arr) {
4849
const getWhiteSpaceIndex = i => {
49-
if (arr[i] !== " " && arr[i] !== "\t") {
50+
if (arr[i] !== " " && arr[i] !== "\t" &&
51+
(!isMobile || arr[i] !== "\xa0")) {
5052
return i;
5153
}
5254
i++;
@@ -65,17 +67,22 @@
6567
}
6668

6769
function getLineNumber(el) {
68-
let elm = el.closest("td"),
70+
let elm = el.closest(isMobile ? "div" : "td"),
6971
index = elm ? elm.id : "";
7072
if (index) {
7173
return parseInt(index.slice(2), 10);
7274
}
7375
return "";
7476
}
7577

78+
function getCodeLines() {
79+
return $$(isMobile ? ".blob-file-content .line" :
80+
".file table.highlight .blob-code-inner");
81+
}
82+
7683
function toggleCode(action, index, depth) {
7784
let els, lineNums;
78-
const codeLines = $$(".file table.highlight .blob-code-inner");
85+
const codeLines = getCodeLines();
7986
// depth is a string containing a specific depth number to toggle
8087
if (depth) {
8188
els = $$(`.collapser[data-depth="${depth}"]`);
@@ -92,7 +99,7 @@
9299
let elm,
93100
end = pairs.get(start - 1);
94101
codeLines.slice(start, end).forEach(el => {
95-
elm = el.closest("tr");
102+
elm = isMobile ? el : el.closest("tr");
96103
if (elm) {
97104
elm.classList.add("hidden-line");
98105
}
@@ -109,7 +116,7 @@
109116
lineNums.forEach(start => {
110117
let end = pairs.get(start - 1);
111118
codeLines.slice(start, end).forEach(el => {
112-
let elm = el.closest("tr");
119+
let elm = isMobile ? el : el.closest("tr");
113120
if (elm) {
114121
elm.classList.remove("hidden-line");
115122
remove(".ellipsis", elm);
@@ -172,13 +179,15 @@
172179
}
173180

174181
function addCodeFolding() {
175-
if ($(".file table.highlight")) {
182+
if ($(".file table.highlight") || $("div.blob-file-content")) {
183+
isMobile = !$(".file table.highlight");
184+
176185
// In case this script has already been run and modified the DOM on a
177186
// previous page in github, make sure to reset it.
178187
remove("span.collapser");
179188
pairs.clear();
180189

181-
const codeLines = $$(".file table.highlight .blob-code-inner"),
190+
const codeLines = getCodeLines(),
182191
spaceMap = new Map(),
183192
stack = [];
184193

0 commit comments

Comments
 (0)