GitHub Indent Comments

A userscript that allows you to indent & outdent blocks in the comment editor

  1. // ==UserScript==
  2. // @name GitHub Indent Comments
  3. // @version 1.0.18
  4. // @description A userscript that allows you to indent & outdent blocks in the comment editor
  5. // @license MIT
  6. // @author Rob Garrison
  7. // @namespace https://github.com/Mottie
  8. // @match https://github.com/*
  9. // @match https://gist.github.com/*
  10. // @run-at document-idle
  11. // @grant GM_addStyle
  12. // @grant GM_getValue
  13. // @grant GM_setValue
  14. // @grant GM_registerMenuCommand
  15. // @connect github.com
  16. // @require https://greasyfork.org/scripts/28721-mutations/code/mutations.js?version=1108163
  17. // @require https://greasyfork.org/scripts/398877-utils-js/code/utilsjs.js?version=1079637
  18. // @require https://greasyfork.org/scripts/28239-rangy-inputs-mod-js/code/rangy-inputs-modjs.js?version=181769
  19. // @icon https://github.githubassets.com/pinned-octocat.svg
  20. // @supportURL https://github.com/Mottie/GitHub-userscripts/issues
  21. // ==/UserScript==
  22.  
  23. /* global $ $$ on make */
  24. (() => {
  25. "use strict";
  26.  
  27. let spaceSize = GM_getValue("space-size", 2);
  28.  
  29. const icons = {
  30. indent: `
  31. <svg class="octicon" xmlns="http://www.w3.org/2000/svg" width="12" height="16" viewBox="0 0 12 16">
  32. <path d="M12 13c0 .6 0 1-.9 1H.9c-.9 0-.9-.4-.9-1s0-1 .9-1h10.2c.88 0 .88.4.88 1zM.92 4h10.2C12 4 12 3.6 12 3s0-1-.9-1H.92c-.9 0-.9.4-.9 1s0 1 .9 1zM11.5 7h-5C6 7 6 7.4 6 8s0 1 .5 1h5c.5 0 .5-.4.5-1s0-1-.5-1zm-7 1L0 5v6z"/>
  33. </svg>`,
  34. outdent: `
  35. <svg class="octicon" xmlns="http://www.w3.org/2000/svg" width="12" height="16" viewBox="0 0 12 16">
  36. <path d="M12 13c0 .6 0 1-.9 1H.9c-.9 0-.9-.4-.9-1s0-1 .9-1h10.2c.88 0 .88.4.88 1zM.92 4h10.2C12 4 12 3.6 12 3s0-1-.9-1H.92c-.9 0-.9.4-.9 1s0 1 .9 1zm10.7 3H6.4c-.46 0-.4.4-.4 1s-.06 1 .4 1h5.2c.47 0 .4-.4.4-1s.07-1-.4-1zM0 8l4.5-3v6z"/>
  37. </svg>`
  38. };
  39.  
  40. GM_addStyle(".ghio-in-outdent * { pointer-events:none; }");
  41.  
  42. // Add indent & outdent buttons
  43. function addButtons() {
  44. createButton("Outdent");
  45. createButton("Indent");
  46. }
  47.  
  48. function createButton(name) {
  49. const nam = name.toLowerCase();
  50. const button = make({
  51. className: `ghio-${nam.toLowerCase()} ghio-in-outdent btn-link toolbar-item btn-octicon no-underline tooltipped tooltipped-n`,
  52. attrs: {
  53. "aria-label": `${name} Selected Text`,
  54. tabindex: "-1",
  55. type: "button"
  56. },
  57. html: icons[nam.toLowerCase()]
  58. });
  59. $$(".toolbar-commenting").forEach(el => {
  60. if (el && !$(`.ghio-${nam.toLowerCase()}`, el)) {
  61. el.insertBefore(button.cloneNode(true), el.childNodes[0]);
  62. }
  63. });
  64. }
  65.  
  66. function indent(text) {
  67. let result = [];
  68. let block = new Array(parseInt(spaceSize, 10) + 1).join(" ");
  69. (text || "").split(/\r*\n/).forEach(line => {
  70. result.push(block + line);
  71. });
  72. return result.join("\n");
  73. }
  74.  
  75. function outdent(text) {
  76. let regex = new RegExp(`^(\x20{1,${spaceSize}}|\xA0{1,${spaceSize}}|\x09)`);
  77. let result = [];
  78. (text || "").split(/\r*\n/).forEach(line => {
  79. result.push(line.replace(regex, ""));
  80. });
  81. return result.join("\n");
  82. }
  83.  
  84. function addBindings() {
  85. window.rangyInput.init();
  86. saveTabSize();
  87. on($("body"), "click", ({ target }) => {
  88. if (target?.classList.contains("ghio-in-outdent")) {
  89. const form = target.closest(".previewable-comment-form");
  90. const textarea = $(".comment-form-textarea", form);
  91. textarea.focus();
  92. setTimeout(() => {
  93. window.rangyInput.indentSelectedText(
  94. textarea,
  95. target.classList.contains("ghio-indent") ? indent : outdent
  96. );
  97. }, 100);
  98. return false;
  99. }
  100. });
  101. // Add Tab & Shift + Tab
  102. on($("body"), "keydown", event => {
  103. const { target, key } = event;
  104. if (key === "Tab") {
  105. if (target?.classList.contains("comment-form-textarea")) {
  106. event.preventDefault();
  107. target.focus();
  108. setTimeout(() => {
  109. window.rangyInput.indentSelectedText(
  110. target,
  111. // shift + tab = outdent
  112. event.getModifierState("Shift") ? outdent : indent
  113. );
  114. }, 100);
  115. }
  116. }
  117. });
  118. }
  119.  
  120. function saveTabSize() {
  121. let $el = $(".gh-indent-size");
  122. if (!$el) {
  123. $el = document.createElement("style");
  124. $el.setAttribute("rel", "stylesheet");
  125. $el.className = "gh-indent-size";
  126. document.querySelector("head").appendChild($el);
  127. }
  128. $el.innerHTML = `.comment-form-textarea { tab-size:${spaceSize}; }`;
  129. }
  130.  
  131. // Add GM options
  132. GM_registerMenuCommand(
  133. "Indent or outdent size",
  134. () => {
  135. const spaces = GM_getValue("indentOutdentSize", spaceSize);
  136. let val = prompt("Enter number of spaces to indent or outdent:", spaces);
  137. if (val !== null && typeof val === "string") {
  138. spaceSize = val;
  139. GM_setValue("space-size", val);
  140. saveTabSize();
  141. }
  142. }
  143. );
  144.  
  145. on(document, "ghmo:container", addButtons);
  146. addBindings();
  147. addButtons();
  148. })();