|
| 1 | +// ==UserScript== |
| 2 | +// @name GitHub Diff File Toggle |
| 3 | +// @version 0.1.1 |
| 4 | +// @description A userscript that adds global diff file toggles |
| 5 | +// @license MIT |
| 6 | +// @author Rob Garrison |
| 7 | +// @namespace https://github.com/Mottie |
| 8 | +// @include https://github.com/* |
| 9 | +// @run-at document-idle |
| 10 | +// @grant none |
| 11 | +// @require https://greasyfork.org/scripts/28721-mutations/code/mutations.js?version=882023 |
| 12 | +// @require https://greasyfork.org/scripts/398877-utils-js/code/utilsjs.js?version=785415 |
| 13 | +// @icon https://github.githubassets.com/pinned-octocat.svg |
| 14 | +// ==/UserScript== |
| 15 | +/* global $ $$ on debounce make */ |
| 16 | +(() => { |
| 17 | + "use strict"; |
| 18 | + |
| 19 | + let timer; |
| 20 | + let busy = false; |
| 21 | + |
| 22 | + const setToggleStyle = state => { |
| 23 | + const mainToggle = $(".ghdt-toggle"); |
| 24 | + mainToggle.classList.toggle("ghdt-selected", state); |
| 25 | + mainToggle.style = state |
| 26 | + ? "background-color: var(--color-btn-selected-bg);" |
| 27 | + : ""; |
| 28 | + }; |
| 29 | + |
| 30 | + const buildButton = () => { |
| 31 | + if (!$(".ghdt-toggle")) { |
| 32 | + const button = make({ |
| 33 | + el: "button", |
| 34 | + className: "btn btn-sm ghdt-toggle tooltipped tooltipped-s float-right", |
| 35 | + text: "Toggle viewed", |
| 36 | + attrs: { |
| 37 | + "aria-label": "Toggle all viewed files" |
| 38 | + } |
| 39 | + }); |
| 40 | + on(button, "click", event => { |
| 41 | + toggle(document, !event.target.classList.contains("ghdt-selected")); |
| 42 | + }); |
| 43 | + $("#files.diff-view")?.prepend(button); |
| 44 | + } |
| 45 | + // Update toggle button state after initialized; timer for progressive |
| 46 | + // loading |
| 47 | + clearTimeout(timer); |
| 48 | + timer = setTimeout(() => { |
| 49 | + if ($$(".js-reviewed-checkbox").every(el => el.checked)) { |
| 50 | + setToggleStyle(true); |
| 51 | + } |
| 52 | + }, 1000); |
| 53 | + }; |
| 54 | + |
| 55 | + const toggle = (target, state) => { |
| 56 | + $$(".js-reviewed-checkbox").forEach(checkbox => { |
| 57 | + if (target !== checkbox && checkbox.checked !== state) { |
| 58 | + checkbox.click(); |
| 59 | + } |
| 60 | + }); |
| 61 | + setToggleStyle(state); |
| 62 | + }; |
| 63 | + |
| 64 | + const handleChange = event => { |
| 65 | + const { target, altKey, shiftKey } = event; |
| 66 | + const anyModifier = altKey || shiftKey; |
| 67 | + if (!busy && anyModifier && target.matches(".js-reviewed-checkbox")) { |
| 68 | + busy = true; |
| 69 | + toggle(target, target.checked); |
| 70 | + setTimeout(() => { |
| 71 | + busy = false; |
| 72 | + }); |
| 73 | + } |
| 74 | + }; |
| 75 | + |
| 76 | + const init = () => { |
| 77 | + if ($("#files.diff-view") || $(".pr-toolbar")) { |
| 78 | + buildButton(); |
| 79 | + } |
| 80 | + }; |
| 81 | + |
| 82 | + on(document, "ghmo:container ghmo:diff", init); |
| 83 | + on(document, "click", debounce(handleChange)); |
| 84 | + on(document, "keydown", debounce(handleChange)); |
| 85 | + init(); |
| 86 | +})(); |
0 commit comments