This repository has been archived by the owner on Jun 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathgithub-script-diff-toggle.user.js
140 lines (130 loc) · 4.38 KB
/
github-script-diff-toggle.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// ==UserScript==
// @name GitHub Diff File Toggle
// @version 1.2.6
// @description A userscript that adds a toggle to show or hide diff files
// @license MIT
// @author StylishThemes
// @namespace https://github.com/StylishThemes
// @include https://github.com/*
// @run-at document-end
// @grant GM_addStyle
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_registerMenuCommand
// @require https://greasyfork.org/scripts/28721-mutations/code/mutations.js?version=634242
// @icon https://avatars3.githubusercontent.com/u/6145677?v=3&s=200
// @updateURL https://raw.githubusercontent.com/StylishThemes/GitHub-Dark-Script/master/github-script-diff-toggle.user.js
// @downloadURL https://raw.githubusercontent.com/StylishThemes/GitHub-Dark-Script/master/github-script-diff-toggle.user.js
// @homepageURL https://github.com/StylishThemes/GitHub-Dark-Script
// ==/UserScript==
(() => {
"use strict";
// This code is also part of the GitHub-Dark Script
// (https://github.com/StylishThemes/GitHub-Dark-Script)
// Extracted out into a separate userscript in case users only want to add this
// functionality
const icon =
`<svg class="octicon" xmlns="http://www.w3.org/2000/svg" width="10" height="6.5" viewBox="0 0 10 6.5">
<path d="M0 1.5L1.5 0l3.5 3.7L8.5.0 10 1.5 5 6.5 0 1.5z"/>
</svg>`;
// Add file diffs toggle
function addFileToggle() {
const files = $$("#files .file-actions");
const button = document.createElement("button");
let updated = false;
button.type = "button";
button.className = "ghd-file-toggle btn btn-sm tooltipped tooltipped-n";
button.setAttribute("aria-label", "Click to Expand or Collapse file");
button.setAttribute("tabindex", "-1");
button.innerHTML = icon;
files.forEach(el => {
if (!$(".ghd-file-toggle", el)) {
// hide GitHub toggle view button
el.querySelector(".js-details-target").style.display = "none";
el.appendChild(button.cloneNode(true));
updated = true;
}
});
// start with all but first entry collapsed
if (updated && files.length) {
if ((GM_getValue("accordion") || "").startsWith("t")) {
toggleFile({
target: $(".ghd-file-toggle")
}, "init");
}
}
}
function toggleSibs(target, state) {
const isCollapsed = state,
toggles = $$(".file");
let el,
indx = toggles.length;
while (indx--) {
el = toggles[indx];
if (el !== target) {
el.classList.toggle("Details--on", isCollapsed);
}
}
}
function toggleFile(event, init) {
const accordion = GM_getValue("accordion"),
el = event.target.closest(".file");
if (el && accordion) {
if (!init) {
el.classList.toggle("Details--on");
}
toggleSibs(el, false);
} else if (el) {
el.classList.toggle("Details--on");
// shift+click toggle all files!
if (event.shiftKey) {
toggleSibs(el, el.classList.contains("Details--on"));
}
}
document.activeElement.blur();
// move current open panel to the top
if (el.classList.contains("Details--on")) {
location.hash = el.id;
}
}
function addBindings() {
$("body").addEventListener("click", event => {
const target = event.target;
if (target && target.classList.contains("ghd-file-toggle")) {
toggleFile(event);
return false;
}
});
}
function $(str, el) {
return (el || document).querySelector(str);
}
function $$(str, el) {
return [...(el || document).querySelectorAll(str)];
}
// Don't initialize if GitHub Dark Script is active
if (!$("#ghd-menu")) {
GM_addStyle(`
.Details--on .ghd-file-toggle svg {
-webkit-transform:rotate(90deg); transform:rotate(90deg);
}
.ghd-file-toggle svg.octicon {
pointer-events: none;
vertical-align: middle;
}
`);
document.addEventListener("ghmo:container", addFileToggle);
document.addEventListener("ghmo:diff", addFileToggle);
// Add GM options
GM_registerMenuCommand("GitHub Diff File Toggle", () => {
let result = `${GM_getValue("accordion") || false}`;
const val = prompt("Accordion Mode? (true/false):", result);
if (val) {
result = val.startsWith("t");
GM_setValue("accordion", result);
}
});
addBindings();
addFileToggle();
}
})();