|
1 | 1 | // ==UserScript==
|
2 | 2 | // @name GitHub Indent Comments
|
3 |
| -// @version 1.0.16 |
| 3 | +// @version 1.0.17 |
4 | 4 | // @description A userscript that allows you to indent & outdent blocks in the comment editor
|
5 | 5 | // @license MIT
|
6 | 6 | // @author Rob Garrison
|
|
14 | 14 | // @grant GM_registerMenuCommand
|
15 | 15 | // @connect github.com
|
16 | 16 | // @require https://greasyfork.org/scripts/28721-mutations/code/mutations.js?version=952601
|
| 17 | +// @require https://greasyfork.org/scripts/398877-utils-js/code/utilsjs.js?version=952600 |
17 | 18 | // @require https://greasyfork.org/scripts/28239-rangy-inputs-mod-js/code/rangy-inputs-modjs.js?version=181769
|
18 | 19 | // @icon https://github.githubassets.com/pinned-octocat.svg
|
19 | 20 | // @updateURL https://raw.githubusercontent.com/Mottie/GitHub-userscripts/master/github-indent-comments.user.js
|
20 | 21 | // @downloadURL https://raw.githubusercontent.com/Mottie/GitHub-userscripts/master/github-indent-comments.user.js
|
21 | 22 | // @supportURL https://github.com/Mottie/GitHub-userscripts/issues
|
22 | 23 | // ==/UserScript==
|
| 24 | +/* global $ $$ on make */ |
23 | 25 | (() => {
|
24 | 26 | "use strict";
|
25 | 27 |
|
|
45 | 47 | }
|
46 | 48 |
|
47 | 49 | function createButton(name) {
|
48 |
| - const toolbars = $$(".toolbar-commenting"), |
49 |
| - nam = name.toLowerCase(), |
50 |
| - button = document.createElement("button"); |
51 |
| - let el, |
52 |
| - indx = toolbars.length; |
53 |
| - if (indx) { |
54 |
| - button.type = "button"; |
55 |
| - button.className = `ghio-${nam.toLowerCase()} ghio-in-outdent toolbar-item tooltipped tooltipped-n`; |
56 |
| - button.setAttribute("aria-label", `${name} Selected Text`); |
57 |
| - button.setAttribute("tabindex", "-1"); |
58 |
| - button.innerHTML = icons[nam.toLowerCase()]; |
59 |
| - while (indx--) { |
60 |
| - el = toolbars[indx]; |
61 |
| - if (!$(`.ghio-${nam.toLowerCase()}`, el)) { |
62 |
| - el.insertBefore(button.cloneNode(true), el.childNodes[0]); |
63 |
| - } |
| 50 | + const nam = name.toLowerCase(); |
| 51 | + const button = make({ |
| 52 | + className: `ghio-${nam.toLowerCase()} ghio-in-outdent btn-link toolbar-item btn-octicon no-underline tooltipped tooltipped-n`, |
| 53 | + attrs: { |
| 54 | + "aria-label": `${name} Selected Text`, |
| 55 | + tabindex: "-1", |
| 56 | + type: "button" |
| 57 | + }, |
| 58 | + html: icons[nam.toLowerCase()] |
| 59 | + }); |
| 60 | + $$(".toolbar-commenting").forEach(el => { |
| 61 | + if (el && !$(`.ghio-${nam.toLowerCase()}`, el)) { |
| 62 | + el.insertBefore(button.cloneNode(true), el.childNodes[0]); |
64 | 63 | }
|
65 |
| - } |
| 64 | + }); |
66 | 65 | }
|
67 | 66 |
|
68 | 67 | function indent(text) {
|
69 |
| - let result = [], |
70 |
| - block = new Array(parseInt(spaceSize, 10) + 1).join(" "); |
| 68 | + let result = []; |
| 69 | + let block = new Array(parseInt(spaceSize, 10) + 1).join(" "); |
71 | 70 | (text || "").split(/\r*\n/).forEach(line => {
|
72 | 71 | result.push(block + line);
|
73 | 72 | });
|
74 | 73 | return result.join("\n");
|
75 | 74 | }
|
76 | 75 |
|
77 | 76 | function outdent(text) {
|
78 |
| - let regex = new RegExp(`^(\x20{1,${spaceSize}}|\xA0{1,${spaceSize}}|\x09)`), |
79 |
| - result = []; |
| 77 | + let regex = new RegExp(`^(\x20{1,${spaceSize}}|\xA0{1,${spaceSize}}|\x09)`); |
| 78 | + let result = []; |
80 | 79 | (text || "").split(/\r*\n/).forEach(line => {
|
81 | 80 | result.push(line.replace(regex, ""));
|
82 | 81 | });
|
|
86 | 85 | function addBindings() {
|
87 | 86 | window.rangyInput.init();
|
88 | 87 | saveTabSize();
|
89 |
| - $("body").addEventListener("click", event => { |
90 |
| - let textarea, |
91 |
| - target = event.target; |
92 |
| - if (target && target.classList.contains("ghio-in-outdent")) { |
93 |
| - textarea = closest(".previewable-comment-form", target); |
94 |
| - textarea = $(".comment-form-textarea", textarea); |
| 88 | + on($("body"), "click", ({ target }) => { |
| 89 | + if (target?.classList.contains("ghio-in-outdent")) { |
| 90 | + const form = target.closest(".previewable-comment-form"); |
| 91 | + const textarea = $(".comment-form-textarea", form); |
95 | 92 | textarea.focus();
|
96 | 93 | setTimeout(() => {
|
97 | 94 | window.rangyInput.indentSelectedText(
|
|
103 | 100 | }
|
104 | 101 | });
|
105 | 102 | // Add Tab & Shift + Tab
|
106 |
| - $("body").addEventListener("keydown", event => { |
107 |
| - if (event.key === "Tab") { |
108 |
| - let target = event.target; |
109 |
| - if (target && target.classList.contains("comment-form-textarea")) { |
| 103 | + on($("body"), "keydown", event => { |
| 104 | + const { target, key } = event; |
| 105 | + if (key === "Tab") { |
| 106 | + if (target?.classList.contains("comment-form-textarea")) { |
110 | 107 | event.preventDefault();
|
111 | 108 | target.focus();
|
112 | 109 | setTimeout(() => {
|
|
132 | 129 | $el.innerHTML = `.comment-form-textarea { tab-size:${spaceSize}; }`;
|
133 | 130 | }
|
134 | 131 |
|
135 |
| - function $(selector, el) { |
136 |
| - return (el || document).querySelector(selector); |
137 |
| - } |
138 |
| - |
139 |
| - function $$(selector, el) { |
140 |
| - return Array.from((el || document).querySelectorAll(selector)); |
141 |
| - } |
142 |
| - |
143 |
| - function closest(selector, el) { |
144 |
| - while (el && el.nodeType === 1) { |
145 |
| - if (el.matches(selector)) { |
146 |
| - return el; |
147 |
| - } |
148 |
| - el = el.parentNode; |
149 |
| - } |
150 |
| - return null; |
151 |
| - } |
152 |
| - |
153 | 132 | // Add GM options
|
154 | 133 | GM_registerMenuCommand(
|
155 | 134 | "Indent or outdent size",
|
|
164 | 143 | }
|
165 | 144 | );
|
166 | 145 |
|
167 |
| - document.addEventListener("ghmo:container", addButtons); |
| 146 | + on(document, "ghmo:container", addButtons); |
168 | 147 | addBindings();
|
169 | 148 | addButtons();
|
170 | 149 | })();
|
0 commit comments