Collapsable Diffs and Linked Branches (GitHub)

Adds a toggle to collapse diffs in GitHub's pull request and commit diff interfaces

2016-10-29 يوللانغان نەشرى. ئەڭ يېڭى نەشرىنى كۆرۈش.

  1. // ==UserScript==
  2. // @name Collapsable Diffs and Linked Branches (GitHub)
  3. // @namespace chriskim06
  4. // @description Adds a toggle to collapse diffs in GitHub's pull request and commit diff interfaces
  5. // @include https://github.com/*
  6. // @require http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js
  7. // @require https://greatest.deepsurf.us/scripts/5392-waitforkeyelements/code/WaitForKeyElements.js?version=19641
  8. // @version 1.5.0
  9. // @grant none
  10. // @locale en
  11. // ==/UserScript==
  12.  
  13. this.$ = this.jQuery = jQuery.noConflict(true);
  14.  
  15. $(function() {
  16.  
  17. waitForKeyElements('#files > div[id^="diff-"]', addDiffCollapseButtons);
  18. waitForKeyElements('.pr-review-tools > .diffbar-item:nth-child(1)', addCollapseAllButton);
  19. waitForKeyElements('#partial-discussion-header .commit-ref', makeLinks);
  20.  
  21. function addDiffCollapseButtons(jNode) {
  22. // Add buttons in each header to allow folding the diff
  23. var info = jNode.find('.file-info');
  24. if (!info.children().first().is('a')) {
  25. // If the button isn't there add it and add the event handler
  26. var expanded = 'M0 5l6 6 6-6H0z';
  27. var collapsed = 'M0 14l6-6L0 2v12z';
  28. info.prepend(
  29. '<a class="octicon-btn custom-collapsable" href="javascript:void(0)">' +
  30. '<svg height="16" width="12" xmlns="http://www.w3.org/2000/svg">' +
  31. '<path d="' + expanded + '" />' +
  32. '</svg>' +
  33. '</a>'
  34. );
  35. jNode.find('.octicon-btn.custom-collapsable').on('click', function() {
  36. // Toggle the visibility of the diff and the direction of the arrow
  37. var icon = $(this).find('path');
  38. jNode.children('.data.highlight.blob-wrapper, .data.highlight.empty, .render-wrapper').toggle();
  39. icon.attr('d', (icon.attr('d') === collapsed) ? expanded : collapsed);
  40. var collapseAllButton = $('#diff-collapse-button');
  41. if (collapseAllButton.length) {
  42. var state = $('#files').find('div[id^="diff-"]').children('.data.highlight.blob-wrapper').is(':visible');
  43. collapseAllButton.attr('data-toggle-state', state ? 'expanded' : 'collapsed');
  44. collapseAllButton.text(state ? 'Fold All' : 'Show All');
  45. }
  46. });
  47. }
  48. }
  49.  
  50. function addCollapseAllButton(jNode) {
  51. // Add a Show/Fold All button if its not there
  52. if (!$('#diff-collapse-button').length) {
  53. var blobs = $('#files').find('div[id^="diff-"]').children('.data.highlight.blob-wrapper, .data.highlight.empty, .render-wrapper');
  54. jNode.after(
  55. '<div class="diffbar-item">' +
  56. '<button id="diff-collapse-button"' +
  57. 'class="btn btn-sm btn-outline BtnGroup-item" style="width: 75px"' +
  58. 'data-toggle-state="' + (blobs.is(':visible') ? 'expanded' : 'collapsed') + '">' +
  59. (blobs.is(':visible') ? 'Fold All' : 'Show All') +
  60. '</button>' +
  61. '</div>'
  62. );
  63. $('#diff-collapse-button').on('click', function() {
  64. // Toggle the visibility of all diffs, directions of arrows, and the button
  65. var state = ($(this).attr('data-toggle-state') === 'expanded');
  66. if (state) {
  67. blobs.hide();
  68. } else {
  69. blobs.show();
  70. }
  71. var expanded = 'M0 5l6 6 6-6H0z';
  72. var collapsed = 'M0 14l6-6L0 2v12z';
  73. $('.octicon-btn.custom-collapsable').find('path').attr('d', state ? collapsed : expanded);
  74. $(this).attr('data-toggle-state', state ? 'collapsed' : 'expanded');
  75. $(this).text(state ? 'Show All' : 'Fold All');
  76. });
  77. }
  78. }
  79.  
  80. function makeLinks(jNode) {
  81. if (!jNode.children(':first-child').is('a')) {
  82. // Turn the branches being compared into links if they aren't already
  83. var url = window.location.href.match(/(https:\/\/github\.com\/)[A-Za-z0-9_-]+(\/[A-Za-z0-9_-]+)/);
  84. if (url) {
  85. var branch = jNode.contents().text();
  86. if (!branch.includes(':')) {
  87. jNode.contents().wrapAll('<a href="' + url[0] + '/tree/' + branch + '"></a>');
  88. } else {
  89. jNode.contents().wrapAll('<a href="' + url[1] + branch.replace(':', url[2] + '/tree/') + '"></a>');
  90. }
  91. }
  92. }
  93. }
  94.  
  95. });