Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make code folding work on mobile layout #47

Merged
merged 1 commit into from
May 28, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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