forked from refined-github/refined-github
-
Notifications
You must be signed in to change notification settings - Fork 1
/
faster-reviews.tsx
62 lines (54 loc) · 1.96 KB
/
faster-reviews.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import React from 'dom-chef';
import select from 'select-dom';
import delegate from 'delegate-it';
import elementReady from 'element-ready';
import * as pageDetect from 'github-url-detection';
import features from '.';
import onReplacedElement from '../helpers/on-replaced-element';
async function addSidebarReviewButton(): Promise<void | false> {
const reviewFormUrl = new URL(location.href);
reviewFormUrl.pathname += '/files';
reviewFormUrl.hash = 'submit-review';
const sidebarReviewsSection = await elementReady('[aria-label="Select reviewers"] .discussion-sidebar-heading');
if (select.exists('[data-hotkey="v"]', sidebarReviewsSection)) {
return false;
}
sidebarReviewsSection!.append(
<span style={{fontWeight: 'normal'}}>
– <a href={reviewFormUrl.href} className="btn-link muted-link" data-hotkey="v">review now</a>
</span>
);
}
function focusReviewTextarea({delegateTarget}: delegate.Event<Event, HTMLDetailsElement>) {
if (delegateTarget.open) {
select('textarea', delegateTarget)!.focus();
}
}
async function initReviewButtonEnhancements(): Promise<void> {
delegate(document, '.js-reviews-container > details', 'toggle', focusReviewTextarea, true);
const reviewDropdownButton = await elementReady<HTMLElement>('.js-reviews-toggle');
reviewDropdownButton!.dataset.hotkey = 'v';
}
void features.add({
id: __filebasename,
description: 'Adds a review button to the PR sidebar, autofocuses the review textarea and adds a keyboard shortcut to open the review popup: `v`.',
screenshot: 'https://user-images.githubusercontent.com/202916/83269671-bb3b2200-a1c7-11ea-90b3-b9457a454162.png',
shortcuts: {
v: 'Open PR review popup'
}
}, {
include: [
pageDetect.isPRConversation
],
additionalListeners: [
() => void onReplacedElement('#partial-discussion-sidebar', addSidebarReviewButton)
],
waitForDomReady: false,
init: addSidebarReviewButton
}, {
include: [
pageDetect.isPRFiles
],
waitForDomReady: false,
init: initReviewButtonEnhancements
});