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
|
import React from 'dom-chef';
import select from 'select-dom';
import delegate from 'delegate-it';
import {AlertIcon} from '@primer/octicons-react';
import debounceFn from 'debounce-fn';
import * as pageDetect from 'github-url-detection';
import * as textFieldEdit from 'text-field-edit';
import features from '.';
import {
prCommitUrlRegex,
preventPrCommitLinkLoss,
prCompareUrlRegex,
preventPrCompareLinkLoss,
discussionUrlRegex,
preventDiscussionLinkLoss,
} from '../github-helpers/prevent-link-loss';
import {createRghIssueLink} from '../helpers/rgh-issue-link';
function handleButtonClick({delegateTarget: fixButton}: delegate.Event<MouseEvent, HTMLButtonElement>): void {
const field = fixButton.form!.querySelector('textarea')!;
textFieldEdit.replace(field, prCommitUrlRegex, preventPrCommitLinkLoss);
textFieldEdit.replace(field, prCompareUrlRegex, preventPrCompareLinkLoss);
textFieldEdit.replace(field, discussionUrlRegex, preventDiscussionLinkLoss);
fixButton.parentElement!.remove();
}
function getUI(field: HTMLTextAreaElement): HTMLElement {
return select('.rgh-prevent-link-loss-container', field.form!) ?? (
<div className="flash flash-warn mb-2 rgh-prevent-link-loss-container">
<AlertIcon/> Your link may be misinterpreted by GitHub (see {createRghIssueLink(2327)}).
<button type="button" className="btn btn-sm primary flash-action rgh-prevent-link-loss">Fix link</button>
</div>
);
}
function isVulnerableToLinkLoss(value: string): boolean {
// The replacement logic is not just in the regex, so it alone can't be used to detect the need for the replacement
return value !== value.replace(prCommitUrlRegex, preventPrCommitLinkLoss)
|| value !== value.replace(prCompareUrlRegex, preventPrCompareLinkLoss)
|| value !== value.replace(discussionUrlRegex, preventDiscussionLinkLoss);
}
const updateUI = debounceFn(({delegateTarget: field}: delegate.Event<Event, HTMLTextAreaElement>): void => {
if (!isVulnerableToLinkLoss(field.value)) {
getUI(field).remove();
} else if (pageDetect.isNewIssue() || pageDetect.isCompare()) {
select('file-attachment', field.form!)!.append(
<div className="m-2">{getUI(field)}</div>,
);
} else {
select('.form-actions', field.form!)!.prepend(getUI(field));
}
}, {
wait: 300,
});
function init(): void {
delegate(document, 'form#new_issue textarea, form.js-new-comment-form textarea, textarea.comment-form-textarea', 'input', updateUI);
delegate(document, '.rgh-prevent-link-loss', 'click', handleButtonClick);
}
void features.add(__filebasename, {
include: [
pageDetect.hasRichTextEditor,
],
deduplicate: 'has-rgh-inner',
init,
});
|