|
1 | 1 | // ==UserScript==
|
2 | 2 | // @name GitHub Issue Add Details
|
3 |
| -// @version 1.0.10 |
| 3 | +// @version 1.0.11 |
4 | 4 | // @description A userscript that adds a button to insert a details block into comments
|
5 | 5 | // @license MIT
|
6 | 6 | // @author Rob Garrison
|
|
9 | 9 | // @run-at document-idle
|
10 | 10 | // @grant none
|
11 | 11 | // @require https://greasyfork.org/scripts/28721-mutations/code/mutations.js?version=952601
|
| 12 | +// @require https://greasyfork.org/scripts/398877-utils-js/code/utilsjs.js?version=952600 |
12 | 13 | // @require https://greasyfork.org/scripts/28239-rangy-inputs-mod-js/code/rangy-inputs-modjs.js?version=181769
|
13 | 14 | // @icon https://github.githubassets.com/pinned-octocat.svg
|
14 | 15 | // @updateURL https://raw.githubusercontent.com/Mottie/Github-userscripts/master/github-issue-add-details.user.js
|
15 | 16 | // @downloadURL https://raw.githubusercontent.com/Mottie/Github-userscripts/master/github-issue-add-details.user.js
|
16 | 17 | // @supportURL https://github.com/Mottie/GitHub-userscripts/issues
|
17 | 18 | // ==/UserScript==
|
| 19 | +/* global $ $$ on make */ |
18 | 20 | (() => {
|
19 | 21 | "use strict";
|
20 | 22 |
|
|
31 | 33 | ];
|
32 | 34 |
|
33 | 35 | // Add insert details button
|
34 |
| - function addDetailsButton() { |
35 |
| - const button = document.createElement("button"); |
36 |
| - button.type = "button"; |
37 |
| - button.className = "ghad-details toolbar-item tooltipped tooltipped-n"; |
38 |
| - button.setAttribute("aria-label", "Add a details/summary block"); |
39 |
| - button.setAttribute("tabindex", "-1"); |
40 |
| - button.innerHTML = icon; |
41 |
| - [...document.querySelectorAll(".toolbar-commenting")].forEach(el => { |
| 36 | + const addDetailsButton = () => { |
| 37 | + const button = make({ |
| 38 | + el: "button", |
| 39 | + className: "ghad-details btn-link toolbar-item btn-octicon no-underline tooltipped tooltipped-n", |
| 40 | + attrs: { |
| 41 | + "aria-label": "Add a details/summary block", |
| 42 | + tabindex: "-1", |
| 43 | + type: "button" |
| 44 | + }, |
| 45 | + html: icon |
| 46 | + }); |
| 47 | + $$(".toolbar-commenting").forEach(el => { |
42 | 48 | if (el && !$(".ghad-details", el)) {
|
43 |
| - const btn = $("[aria-label*='Add a task list']", el); |
| 49 | + const btn = $("md-quote", el); |
44 | 50 | btn.before(button.cloneNode(true));
|
45 | 51 | }
|
46 | 52 | });
|
47 |
| - } |
| 53 | + }; |
48 | 54 |
|
49 |
| - function addBindings() { |
| 55 | + const addBindings = () => { |
50 | 56 | window.rangyInput.init();
|
51 |
| - $("body").addEventListener("click", event => { |
52 |
| - const target = event.target; |
53 |
| - if (target && target.classList.contains("ghad-details")) { |
54 |
| - let textarea = target.closest(".previewable-comment-form"); |
55 |
| - textarea = $(".comment-form-textarea", textarea); |
56 |
| - textarea.focus(); |
57 |
| - window.rangyInput.surroundSelectedText( |
58 |
| - textarea, |
59 |
| - detailsBlock[0], // prefix |
60 |
| - detailsBlock[1] // suffix |
61 |
| - ); |
| 57 | + on($("body"), "click", event => { |
| 58 | + const { target } = event; |
| 59 | + if (target?.classList.contains("ghad-details")) { |
| 60 | + event.preventDefault(); |
| 61 | + const form = target.closest(".previewable-comment-form"); |
| 62 | + const textarea = $(".comment-form-textarea", form); |
| 63 | + setTimeout(() => { |
| 64 | + textarea.focus(); |
| 65 | + window.rangyInput.surroundSelectedText( |
| 66 | + textarea, |
| 67 | + detailsBlock[0], // prefix |
| 68 | + detailsBlock[1] // suffix |
| 69 | + ); |
| 70 | + }, 100); |
62 | 71 | return false;
|
63 | 72 | }
|
64 | 73 | });
|
65 |
| - } |
66 |
| - |
67 |
| - function $(str, el) { |
68 |
| - return (el || document).querySelector(str); |
69 |
| - } |
| 74 | + }; |
70 | 75 |
|
71 |
| - document.addEventListener("ghmo:container", addDetailsButton); |
72 |
| - document.addEventListener("ghmo:comments", addDetailsButton); |
| 76 | + on(document, "ghmo:container", addDetailsButton); |
| 77 | + on(document, "ghmo:comments", addDetailsButton); |
73 | 78 |
|
74 | 79 | addDetailsButton();
|
75 | 80 | addBindings();
|
|
0 commit comments