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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
import './hidden-review-comments-indicator.css';
import mem from 'mem';
import React from 'dom-chef';
import select from 'select-dom';
import delegate from 'delegate-it';
import CommentIcon from 'octicon/comment.svg';
import * as pageDetect from 'github-url-detection';
import features from '.';
import onPrFileLoad from '../github-events/on-pr-file-load';
import preserveScroll from '../helpers/preserve-scroll';
// When an indicator is clicked, this will show comments on the current file
const handleIndicatorClick = ({delegateTarget}: delegate.Event): void => {
const commentedLine = delegateTarget.closest('tr')!.previousElementSibling!;
const resetScroll = preserveScroll(commentedLine);
delegateTarget
.closest('.file.js-file')!
.querySelector<HTMLInputElement>('.js-toggle-file-notes')!
.click();
resetScroll();
};
// `mem` avoids adding the indicator twice to the same thread
const addIndicator = mem((commentThread: HTMLElement): void => {
const commentCount = commentThread.querySelectorAll('.review-comment .js-comment').length;
commentThread.before(
<tr>
<td className="rgh-comments-indicator blob-num" colSpan={2}>
<button type="button" className="btn-link">
<CommentIcon/>
<span>{commentCount}</span>
</button>
</td>
</tr>
);
});
// Add indicator when the `show-inline-notes` class is removed (i.e. the comments are hidden)
const observer = new MutationObserver(mutations => {
for (const mutation of mutations) {
const file = mutation.target as HTMLElement;
const wasVisible = mutation.oldValue!.includes('show-inline-notes');
const isHidden = !file.classList.contains('show-inline-notes');
if (wasVisible && isHidden) {
for (const thread of select.all('tr.inline-comments', file)) {
addIndicator(thread);
}
}
}
});
function observeFiles(): void {
for (const element of select.all('.file.js-file')) {
// #observe won't observe the same element twice
observer.observe(element, {
attributes: true,
attributeOldValue: true,
attributeFilter: ['class']
});
}
}
function init(): void {
observeFiles();
onPrFileLoad(observeFiles);
delegate(document, '.rgh-comments-indicator', 'click', handleIndicatorClick);
}
void features.add({
id: __filebasename,
description: 'Adds comment indicators when comments are hidden in PR review.',
screenshot:
'https://user-images.githubusercontent.com/1402241/63112671-011d5580-bfbb-11e9-9e19-53e11641990e.gif'
}, {
include: [
pageDetect.isPRFiles,
pageDetect.isPRCommit
],
init
});
|