Skip to content

Commit 072aa68

Browse files
committed
Indent-comments: Update styling & code
1 parent 1191cfb commit 072aa68

File tree

1 file changed

+30
-51
lines changed

1 file changed

+30
-51
lines changed

github-indent-comments.user.js

Lines changed: 30 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// ==UserScript==
22
// @name GitHub Indent Comments
3-
// @version 1.0.16
3+
// @version 1.0.17
44
// @description A userscript that allows you to indent & outdent blocks in the comment editor
55
// @license MIT
66
// @author Rob Garrison
@@ -14,12 +14,14 @@
1414
// @grant GM_registerMenuCommand
1515
// @connect github.com
1616
// @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
1718
// @require https://greasyfork.org/scripts/28239-rangy-inputs-mod-js/code/rangy-inputs-modjs.js?version=181769
1819
// @icon https://github.githubassets.com/pinned-octocat.svg
1920
// @updateURL https://raw.githubusercontent.com/Mottie/GitHub-userscripts/master/github-indent-comments.user.js
2021
// @downloadURL https://raw.githubusercontent.com/Mottie/GitHub-userscripts/master/github-indent-comments.user.js
2122
// @supportURL https://github.com/Mottie/GitHub-userscripts/issues
2223
// ==/UserScript==
24+
/* global $ $$ on make */
2325
(() => {
2426
"use strict";
2527

@@ -45,38 +47,35 @@
4547
}
4648

4749
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]);
6463
}
65-
}
64+
});
6665
}
6766

6867
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(" ");
7170
(text || "").split(/\r*\n/).forEach(line => {
7271
result.push(block + line);
7372
});
7473
return result.join("\n");
7574
}
7675

7776
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 = [];
8079
(text || "").split(/\r*\n/).forEach(line => {
8180
result.push(line.replace(regex, ""));
8281
});
@@ -86,12 +85,10 @@
8685
function addBindings() {
8786
window.rangyInput.init();
8887
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);
9592
textarea.focus();
9693
setTimeout(() => {
9794
window.rangyInput.indentSelectedText(
@@ -103,10 +100,10 @@
103100
}
104101
});
105102
// 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")) {
110107
event.preventDefault();
111108
target.focus();
112109
setTimeout(() => {
@@ -132,24 +129,6 @@
132129
$el.innerHTML = `.comment-form-textarea { tab-size:${spaceSize}; }`;
133130
}
134131

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-
153132
// Add GM options
154133
GM_registerMenuCommand(
155134
"Indent or outdent size",
@@ -164,7 +143,7 @@
164143
}
165144
);
166145

167-
document.addEventListener("ghmo:container", addButtons);
146+
on(document, "ghmo:container", addButtons);
168147
addBindings();
169148
addButtons();
170149
})();

0 commit comments

Comments
 (0)