GitHub Toggle Diff Comments

A userscript that toggles diff/PR and commit comments

目前為 2020-09-30 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name GitHub Toggle Diff Comments
  3. // @version 0.3.0
  4. // @description A userscript that toggles diff/PR and commit comments
  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 GM.addStyle
  11. // @grant GM_addStyle
  12. // @require https://greasemonkey.github.io/gm4-polyfill/gm4-polyfill.js?updated=20180103
  13. // @require https://greasyfork.org/scripts/28721-mutations/code/mutations.js?version=666427
  14. // @require https://greasyfork.org/scripts/398877-utils-js/code/utilsjs.js?version=785415
  15. // @icon https://github.githubassets.com/pinned-octocat.svg
  16. // ==/UserScript==
  17. /* global GM $ $$ on debounce make */
  18. (() => {
  19. "use strict";
  20.  
  21. const selectors = {
  22. // PR has notes (added to <div id="diff-00" class="file ...">)
  23. headerHasNotes: ".has-inline-notes:not(.hide-file-notes-toggle)",
  24. // show comments wrapper for each file
  25. headerComment: ".has-inline-notes .file-actions > .d-flex",
  26. // show comments checkbox
  27. headerCheckbox: "js-toggle-file-notes",
  28. // button active
  29. activeClass: "ghtc-active",
  30. // td wrapper
  31. tdWrapper: "js-quote-selection-container",
  32. // first div inside td wrapper
  33. tdDiv: "js-resolvable-timeline-thread-container",
  34. // has row comments
  35. rowComment: "tr.inline-comments",
  36. // button class names
  37. button: "btn btn-sm BtnGroup-item ghtc-toggle tooltipped tooltipped-s"
  38. };
  39.  
  40. const actions = {
  41. // Show or hide all comments on the page
  42. toggleAllShowComments: {
  43. check: (event, el) => {
  44. const button = el.matches(`button.${selectors.headerCheckbox}`);
  45. return el.id === "ghtc-show-toggle-all" || event.shiftKey && button;
  46. },
  47. exec: el => {
  48. const state = getState(el);
  49. $$(`#ghtc-show-toggle-all, button.${selectors.headerCheckbox}`).forEach(
  50. el => el.classList.toggle(selectors.activeClass, state)
  51. );
  52. // Use built-in "Show comments" checkbox
  53. $$(`#files input.${selectors.headerCheckbox}`).forEach(el => {
  54. el.checked = state;
  55. el.dispatchEvent(new Event("change", {bubbles: true}));
  56. });
  57. }
  58. },
  59. // Show or hide all comments in a file
  60. toggleFileShowComments: {
  61. check: (_, el) => {
  62. return el.matches(`button.${selectors.headerCheckbox}`);
  63. },
  64. exec: el => {
  65. const state = getState(el);
  66. const box = $(`input.${selectors.headerCheckbox}`, el.closest(".d-flex"));
  67. if (box) {
  68. box.checked = state;
  69. box.dispatchEvent(new Event("change", {bubbles: true}));
  70. }
  71. }
  72. },
  73. // Collapse or expand all comments on the page
  74. collapsePageComments: {
  75. check: (event, el) => {
  76. const toggleAll = el.id === "ghtc-collapse-toggle-all";
  77. const toggle = el.classList.contains("ghtc-collapse-toggle-file");
  78. return toggleAll || (event.shiftKey && toggle);
  79. },
  80. exec: el => {
  81. const state = getState(el);
  82. $("#ghtc-collapse-toggle-all").classList.toggle(selectors.activeClass, state);
  83. toggleMultipleComments(el.closest("#files_bucket"), state);
  84. $$(".ghtc-collapse-toggle-file").forEach(el => {
  85. el.classList.toggle(selectors.activeClass, state);
  86. });
  87. }
  88. },
  89. // Collapse or expand all comments within a file
  90. collapseFileComments: {
  91. check: (event, el) => {
  92. const toggle = el.classList.contains("ghtc-collapse-toggle-file");
  93. const container = el.classList.contains(selectors.tdDiv);
  94. return toggle || (event.shiftKey && container);
  95. },
  96. exec: el => {
  97. const state = getState(el);
  98. toggleMultipleComments(el.closest(".file"), state);
  99. }
  100. },
  101. // Collapse or expand single comment
  102. collapseComment: {
  103. check: (_, el) => el.classList.contains(selectors.tdDiv),
  104. exec: el => el.closest("tr").classList.toggle("ghtc-collapsed"),
  105. }
  106. };
  107.  
  108. const icons = {
  109. "show": `<svg xmlns="http://www.w3.org/2000/svg" class="octicon ghtc-primary ghtc-comment-hide" width="16" height="16" viewBox="0 0 16 16" aria-hidden="true"><g fill="#777"><path d="M6 9h1L6 8 5 7v1c0 .6.5 1 1 1z"/><path d="M9 11H4.5L3 12.5V11H1V5h2L2 4H1a1 1 0 0 0-1 1v6c0 .6.5 1 1 1h1v3l3-3h4c.3 0 .6-.1.7-.3l-.7-.8v.1zM15 1H6a1 1 0 0 0-1 1v.3l1 1V2h9v6h-2v1.5L11.5 8h-.8l3.3 3.2V9h1c.6 0 1-.5 1-1V2c0-.6-.4-1-1-1z"/></g><path d="M.4.9L13.7 14h1.7L2 .9z"/></svg>
  110. <svg xmlns="http://www.w3.org/2000/svg" class="octicon ghtc-secondary ghtc-comment-show" width="16" height="16" viewBox="0 0 16 16" aria-hidden="true"><path fill-rule="evenodd" d="M15 1H6c-.55 0-1 .45-1 1v2H1c-.55 0-1 .45-1 1v6c0 .55.45 1 1 1h1v3l3-3h4c.55 0 1-.45 1-1V9h1l3 3V9h1c.55 0 1-.45 1-1V2c0-.55-.45-1-1-1zM9 11H4.5L3 12.5V11H1V5h4v3c0 .55.45 1 1 1h3v2zm6-3h-2v1.5L11.5 8H6V2h9v6z"></path></svg>`,
  111. "collapse": `<svg xmlns="http://www.w3.org/2000/svg" class="octicon ghtc-primary ghtc-collapse" width="16" height="16" viewBox="0 0 16 16" aria-hidden="true"><g fill="#777"><path d="M4.2 12.8L5.6 11H4.5L3 12.5V11H1V5h4v3c0 .6.5 1 1 1h1.2L8 8H6V5.4L5 4H1a1 1 0 0 0-1 1v6c0 .6.5 1 1 1h1v3l2.2-2.2zM6 2.2V2h.6V1H6a1 1 0 0 0-1 1v.2h1zM15 1h-4.6v1H15v6h-2v1.5L11.5 8H9.1l.9 1.2V9h1l3 3V9h1c.6 0 1-.5 1-1V2c0-.6-.4-1-1-1z"/></g><path d="M11.5 3h-2V1h-2v2h-2l3 4zM5.5 13h2v2h2v-2h2l-3-4z"/></svg><svg xmlns="http://www.w3.org/2000/svg" class="octicon ghtc-secondary ghtc-expand" width="16" height="16" viewBox="0 0 16 16" aria-hidden="true"><g fill="#777"><path d="M6 5.8H5V8c0 .6.5 1 1 1h.8V8H6V5.8z"/><path d="M4.6 11h-.1L3 12.5V11H1V5h3.3L6 2.9V2h.7l.8-1H6a1 1 0 0 0-1 1v2H1a1 1 0 0 0-1 1v6c0 .6.5 1 1 1h1v3l3-3h.3l-.7-1zM15 1h-5l.7 1H15v6h-2v1.5L11.5 8h-1v1h.5l.8.8h1.8l-.8 1L14 12V9h1c.6 0 1-.5 1-1V2c0-.6-.4-1-1-1z"/></g><path d="M11.7 11h-2V9h-2v2h-2l3 4zM11.7 5h-2v2h-2V5h-2l3-4z"/></svg>`
  112. };
  113.  
  114. // Using small black triangles because Windows doesn't
  115. // replace them with ugly emoji images
  116. GM.addStyle(`
  117. td.${selectors.tdWrapper} {
  118. position: relative;
  119. }
  120. td .${selectors.tdDiv} {
  121. cursor: pointer;
  122. }
  123. .js-resolvable-thread-contents {
  124. cursor: default;
  125. }
  126. td .${selectors.tdDiv}:before {
  127. content: "\\25be";
  128. font-size: 40px;
  129. position: absolute;
  130. right: 10px;
  131. top: -10px;
  132. pointer-events: none;
  133. }
  134. .ghtc-collapsed .${selectors.tdDiv}:before {
  135. content: "\\25c2";
  136. top: -20px;
  137. }
  138. .ghtc-collapsed .${selectors.tdDiv} {
  139. padding: 10px;
  140. border: 0;
  141. min-height: 26px;
  142. }
  143. .ghtc-collapsed .${selectors.tdDiv}:last-child {
  144. margin-bottom: 16px;
  145. }
  146. .ghtc-toggle .ghtc-secondary,
  147. .ghtc-toggle.${selectors.activeClass} .ghtc-primary,
  148. .ghtc-toggle input ~ .ghtc-secondary,
  149. .ghtc-toggle input:checked ~ .ghtc-primary,
  150. .ghtc-collapsed .${selectors.tdDiv} > *,
  151. .ghtc-collapsed .last-${selectors.tdDiv},
  152. .ghtc-collapsed .inline-comment-form-container:not(.open) {
  153. display: none;
  154. }
  155. .diff-table .ghtc-collapsed td.line-comments {
  156. padding: 0;
  157. cursor: pointer;
  158. }
  159. .pr-toolbar .pr-review-tools.float-right .diffbar-item + .diffbar-item {
  160. margin-left: 10px;
  161. }
  162. .ghtc-toggle {
  163. height: 28px;
  164. }
  165. .ghtc-toggle svg {
  166. display: inline-block;
  167. max-height: 16px;
  168. pointer-events: none;
  169. vertical-align: baseline !important;
  170. }
  171. .ghtc-toggle.${selectors.activeClass} .ghtc-secondary,
  172. .ghtc-toggle input:checked ~ .ghtc-secondary {
  173. display: block;
  174. }`
  175. );
  176.  
  177. function toggleMultipleComments(wrapper, state) {
  178. $(".ghtc-collapse-toggle-file", wrapper).classList.toggle(
  179. selectors.activeClass, state
  180. );
  181. $$(selectors.rowComment, wrapper).forEach(el => {
  182. el.classList.toggle("ghtc-collapsed", state);
  183. });
  184. }
  185.  
  186. function getState(el) {
  187. el.classList.toggle(selectors.activeClass);
  188. return el.classList.contains(selectors.activeClass);
  189. }
  190.  
  191. function addListeners() {
  192. const mainContent = $(".repository-content");
  193. if (!mainContent.classList.contains("ghtc-listeners")) {
  194. mainContent.classList.add("ghtc-listeners");
  195. on(mainContent, "change", debounce(event => {
  196. const el = event.target;
  197. if (el && el.classList.contains(selectors.headerCheckbox)) {
  198. const button = $(selectors.headerCheckbox, el.closest(".d-flex"));
  199. if (button) {
  200. button.classList.toggle(selectors.activeClass, el.checked);
  201. }
  202. }
  203. }));
  204. on(mainContent, "click", debounce(event => {
  205. const el = event.target;
  206. if (el) {
  207. Object.keys(actions).some(action => {
  208. if (actions[action].check(event, el)) {
  209. event.stopPropagation();
  210. event.preventDefault();
  211. actions[action].exec(el);
  212. return true;
  213. }
  214. return false;
  215. });
  216. }
  217. }));
  218. }
  219. }
  220.  
  221. function addButtons() {
  222. $$(selectors.headerComment).forEach(wrapper => {
  223. if (!wrapper.classList.contains("ghtc-show-comments")) {
  224. wrapper.classList.add("ghtc-show-comments", "BtnGroup");
  225. // Add a "Show Comments" button outside the dropdown
  226. const show = make({
  227. el: "button",
  228. className: `${selectors.button} ${selectors.headerCheckbox} ${selectors.activeClass}`,
  229. html: icons.show,
  230. attrs: {
  231. "aria-label": "Show or hide all comments in this file"
  232. }
  233. });
  234. wrapper.prepend(show);
  235. // Add collapse all file comments button before label
  236. const collapse = make({
  237. el: "button",
  238. className: `${selectors.button} ghtc-collapse-toggle-file`,
  239. html: icons.collapse,
  240. attrs: {
  241. type: "button",
  242. "aria-label": "Expand or collapse all comments in this file"
  243. },
  244. });
  245. wrapper.prepend(collapse);
  246. }
  247. });
  248. // Add collapse all comments on the page - test adding global toggle on
  249. // https://github.com/openstyles/stylus/pull/150/files (edit.js)
  250. if (!$("#ghtc-collapse-toggle-all")) {
  251. // insert before Unified/Split button group
  252. const diffmode = $(".pr-review-tools .diffbar-item, #toc .toc-diff-stats");
  253. const wrapper = make({
  254. className: "BtnGroup " +
  255. // PR: diffbar-item; commit: toc-diff-stats
  256. (diffmode.classList.contains("diffbar-item")
  257. ? "diffbar-item"
  258. : "float-right pr-2"
  259. )
  260. }, [
  261. // collapse/expand all comments
  262. make({
  263. html: icons.collapse,
  264. className: selectors.button,
  265. attrs: {
  266. id: "ghtc-collapse-toggle-all",
  267. type: "button",
  268. "aria-label": "Expand or collapse all comments"
  269. }
  270. }),
  271. // show/hide all comments
  272. make({
  273. className: `${selectors.button} ${selectors.activeClass}`,
  274. html: icons.show,
  275. attrs: {
  276. id: "ghtc-show-toggle-all",
  277. type: "button",
  278. "aria-label": "Show or hide all comments"
  279. }
  280. })
  281. ]);
  282. diffmode.parentNode.insertBefore(wrapper, diffmode);
  283. }
  284. }
  285.  
  286. function init() {
  287. if ($("#files") && $(selectors.headerHasNotes)) {
  288. addListeners();
  289. addButtons();
  290. }
  291. }
  292.  
  293. on(document, "ghmo:container", init);
  294. on(document, "ghmo:diff", init);
  295. init();
  296.  
  297. })();