GitHub Indent Comments

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

当前为 2021-02-21 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name GitHub Indent Comments
  3. // @version 1.0.15
  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. // @include https://github.com/*
  9. // @include 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=882023
  17. // @require https://greasyfork.org/scripts/28239-rangy-inputs-mod-js/code/rangy-inputs-modjs.js?version=181769
  18. // @icon https://github.githubassets.com/pinned-octocat.svg
  19. // @supportURL https://github.com/Mottie/GitHub-userscripts/issues
  20. // ==/UserScript==
  21. (() => {
  22. "use strict";
  23.  
  24. let spaceSize = GM_getValue("space-size", 2);
  25.  
  26. const icons = {
  27. indent: `
  28. <svg class="octicon" xmlns="http://www.w3.org/2000/svg" width="12" height="16" viewBox="0 0 12 16">
  29. <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"/>
  30. </svg>`,
  31. outdent: `
  32. <svg class="octicon" xmlns="http://www.w3.org/2000/svg" width="12" height="16" viewBox="0 0 12 16">
  33. <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"/>
  34. </svg>`
  35. };
  36.  
  37. GM_addStyle(".ghio-in-outdent * { pointer-events:none; }");
  38.  
  39. // Add indent & outdent buttons
  40. function addButtons() {
  41. createButton("Outdent");
  42. createButton("Indent");
  43. }
  44.  
  45. function createButton(name) {
  46. const toolbars = $$(".toolbar-commenting"),
  47. nam = name.toLowerCase(),
  48. button = document.createElement("button");
  49. let el,
  50. indx = toolbars.length;
  51. if (indx) {
  52. button.type = "button";
  53. button.className = `ghio-${nam.toLowerCase()} ghio-in-outdent toolbar-item tooltipped tooltipped-n`;
  54. button.setAttribute("aria-label", `${name} Selected Text`);
  55. button.setAttribute("tabindex", "-1");
  56. button.innerHTML = icons[nam.toLowerCase()];
  57. while (indx--) {
  58. el = toolbars[indx];
  59. if (!$(`.ghio-${nam.toLowerCase()}`, el)) {
  60. el.insertBefore(button.cloneNode(true), el.childNodes[0]);
  61. }
  62. }
  63. }
  64. }
  65.  
  66. function indent(text) {
  67. let result = [],
  68. 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. 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. $("body").addEventListener("click", event => {
  88. let textarea,
  89. target = event.target;
  90. if (target && target.classList.contains("ghio-in-outdent")) {
  91. textarea = closest(".previewable-comment-form", target);
  92. textarea = $(".comment-form-textarea", textarea);
  93. textarea.focus();
  94. setTimeout(() => {
  95. window.rangyInput.indentSelectedText(
  96. textarea,
  97. target.classList.contains("ghio-indent") ? indent : outdent
  98. );
  99. }, 100);
  100. return false;
  101. }
  102. });
  103. // Add Tab & Shift + Tab
  104. $("body").addEventListener("keydown", event => {
  105. if (event.key === "Tab") {
  106. let target = event.target;
  107. if (target && target.classList.contains("comment-form-textarea")) {
  108. event.preventDefault();
  109. target.focus();
  110. setTimeout(() => {
  111. window.rangyInput.indentSelectedText(
  112. target,
  113. // shift + tab = outdent
  114. event.getModifierState("Shift") ? outdent : indent
  115. );
  116. }, 100);
  117. }
  118. }
  119. });
  120. }
  121.  
  122. function saveTabSize() {
  123. let $el = $(".gh-indent-size");
  124. if (!$el) {
  125. $el = document.createElement("style");
  126. $el.setAttribute("rel", "stylesheet");
  127. $el.className = "gh-indent-size";
  128. document.querySelector("head").appendChild($el);
  129. }
  130. $el.innerHTML = `.comment-form-textarea { tab-size:${spaceSize}; }`;
  131. }
  132.  
  133. function $(selector, el) {
  134. return (el || document).querySelector(selector);
  135. }
  136.  
  137. function $$(selector, el) {
  138. return Array.from((el || document).querySelectorAll(selector));
  139. }
  140.  
  141. function closest(selector, el) {
  142. while (el && el.nodeType === 1) {
  143. if (el.matches(selector)) {
  144. return el;
  145. }
  146. el = el.parentNode;
  147. }
  148. return null;
  149. }
  150.  
  151. // Add GM options
  152. GM_registerMenuCommand(
  153. "Indent or outdent size",
  154. () => {
  155. const spaces = GM_getValue("indentOutdentSize", spaceSize);
  156. let val = prompt("Enter number of spaces to indent or outdent:", spaces);
  157. if (val !== null && typeof val === "string") {
  158. spaceSize = val;
  159. GM_setValue("space-size", val);
  160. saveTabSize();
  161. }
  162. }
  163. );
  164.  
  165. document.addEventListener("ghmo:container", addButtons);
  166. addBindings();
  167. addButtons();
  168. })();