Skip to content

Commit

Permalink
Make github-code-folding.user.js work on mobile layout (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
zb3 authored and Mottie committed May 28, 2018
1 parent fca41d2 commit 0f99a65
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions github-code-folding.user.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,16 @@
const pairs = new Map(),
ellipsis = document.createElement("span"),
triangle = document.createElement("span");
let isMobile = false;

triangle.className = "collapser";
ellipsis.className = "pl-smi ellipsis";
ellipsis.innerHTML = "…";

function countInitialWhiteSpace(arr) {
const getWhiteSpaceIndex = i => {
if (arr[i] !== " " && arr[i] !== "\t") {
if (arr[i] !== " " && arr[i] !== "\t" &&
(!isMobile || arr[i] !== "\xa0")) {
return i;
}
i++;
Expand All @@ -65,17 +67,22 @@
}

function getLineNumber(el) {
let elm = el.closest("td"),
let elm = el.closest(isMobile ? "div" : "td"),
index = elm ? elm.id : "";
if (index) {
return parseInt(index.slice(2), 10);
}
return "";
}

function getCodeLines() {
return $$(isMobile ? ".blob-file-content .line" :
".file table.highlight .blob-code-inner");
}

function toggleCode(action, index, depth) {
let els, lineNums;
const codeLines = $$(".file table.highlight .blob-code-inner");
const codeLines = getCodeLines();
// depth is a string containing a specific depth number to toggle
if (depth) {
els = $$(`.collapser[data-depth="${depth}"]`);
Expand All @@ -92,7 +99,7 @@
let elm,
end = pairs.get(start - 1);
codeLines.slice(start, end).forEach(el => {
elm = el.closest("tr");
elm = isMobile ? el : el.closest("tr");
if (elm) {
elm.classList.add("hidden-line");
}
Expand All @@ -109,7 +116,7 @@
lineNums.forEach(start => {
let end = pairs.get(start - 1);
codeLines.slice(start, end).forEach(el => {
let elm = el.closest("tr");
let elm = isMobile ? el : el.closest("tr");
if (elm) {
elm.classList.remove("hidden-line");
remove(".ellipsis", elm);
Expand Down Expand Up @@ -172,13 +179,15 @@
}

function addCodeFolding() {
if ($(".file table.highlight")) {
if ($(".file table.highlight") || $("div.blob-file-content")) {
isMobile = !$(".file table.highlight");

// In case this script has already been run and modified the DOM on a
// previous page in github, make sure to reset it.
remove("span.collapser");
pairs.clear();

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

Expand Down

0 comments on commit 0f99a65

Please # to comment.