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 './quick-mention.css';
import React from 'dom-chef';
import select from 'select-dom';
import delegate from 'delegate-it';
import {ReplyIcon} from '@primer/octicons-react';
import * as pageDetect from 'github-url-detection';
import * as textFieldEdit from 'text-field-edit';
import features from '.';
import {getUsername} from '../github-helpers';
import onNewComments from '../github-events/on-new-comments';
function mentionUser({delegateTarget: button}: delegate.Event): void {
const userMention = button.parentElement!.querySelector('img')!.alt;
const newComment = select('textarea#new_comment_field')!;
newComment.focus();
// If the new comment field has selected text, don’t replace it
newComment.selectionStart = newComment.selectionEnd;
// If the cursor is preceded by a space (or is at place 0), don't add a space before the mention
const precedingCharacter = newComment.value.slice(newComment.selectionStart - 1, newComment.selectionStart);
const spacer = /\s|^$/.test(precedingCharacter) ? '' : ' ';
// The space after closes the autocomplete box and places the cursor where the user would start typing
textFieldEdit.insert(newComment, `${spacer}${userMention} `);
}
function init(): void {
// `:first-child` avoids app badges #2630
// The hovercard attribute avoids `highest-rated-comment`
const avatars = select.all(`.TimelineItem-avatar > [data-hovercard-type="user"]:first-child:not([href="/${getUsername()!}"], .rgh-quick-mention)`);
for (const avatar of avatars) {
const userMention = select('img', avatar)!.alt;
avatar.classList.add('rgh-quick-mention');
avatar.after(
<button
type="button"
className="rgh-quick-mention tooltipped tooltipped-e btn-link"
aria-label={`Mention ${userMention} in a new comment`}
>
<ReplyIcon/>
</button>,
);
}
delegate(document, 'button.rgh-quick-mention', 'click', mentionUser);
}
void features.add(__filebasename, {
include: [
pageDetect.isConversation,
],
exclude: [
() => select.exists('.conversation-limited'), // Conversation is locked
],
additionalListeners: [
onNewComments,
],
deduplicate: 'has-rgh-inner',
init,
});
|