-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
unfinished-comments.tsx
49 lines (41 loc) · 1.47 KB
/
unfinished-comments.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
import select from 'select-dom';
import delegate from 'delegate-it';
import * as pageDetect from 'github-url-detection';
import features from '../feature-manager';
let documentTitle: string | undefined;
let submitting: number | undefined;
function hasDraftComments(): boolean {
// `[disabled]` excludes the PR description field that `wait-for-checks` disables while it waits
// `[id^="convert-to-issue-body"]` excludes the hidden pre-filled textareas created when opening the dropdown menu of review comments
return select.all('textarea:not([disabled], [id^="convert-to-issue-body"])').some(textarea =>
textarea.value !== textarea.textContent, // Exclude comments being edited but not yet changed (and empty comment fields)
);
}
function disableOnSubmit(): void {
clearTimeout(submitting);
submitting = window.setTimeout(() => {
submitting = undefined;
}, 2000);
}
function updateDocumentTitle(): void {
if (submitting) {
return;
}
if (document.visibilityState === 'hidden' && hasDraftComments()) {
documentTitle = document.title;
document.title = '✏️ Draft - ' + document.title;
} else if (documentTitle) {
document.title = documentTitle;
documentTitle = undefined;
}
}
function init(signal: AbortSignal): void {
delegate(document, 'form', 'submit', disableOnSubmit, {capture: true, signal});
document.addEventListener('visibilitychange', updateDocumentTitle, {signal});
}
void features.add(import.meta.url, {
include: [
pageDetect.hasRichTextEditor,
],
init,
});