import React from 'dom-chef'; import select from 'select-dom'; import delegate from 'delegate-it'; import AlertIcon from 'octicon/alert.svg'; 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} from '../github-helpers'; function handleButtonClick({delegateTarget: fixButton}: delegate.Event): void { const field = fixButton.form!.querySelector('textarea')!; textFieldEdit.replace(field, prCommitUrlRegex, preventPrCommitLinkLoss); fixButton.parentElement!.remove(); } function getUI(field: HTMLTextAreaElement): HTMLElement { return select('.rgh-fix-pr-commit-links-container', field.form!) ?? (
Your PR Commit link may be misinterpreted by GitHub.
); } const updateUI = debounceFn(({delegateTarget: field}: delegate.Event): void => { // The replacement logic is not just in the regex, so it alone can't be used to detect the need for the replacement if (field.value === field.value.replace(prCommitUrlRegex, preventPrCommitLinkLoss)) { getUI(field).remove(); } else if (pageDetect.isNewIssue() || pageDetect.isCompare()) { select('file-attachment', field.form!)!.append(
{getUI(field)}
); } 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-fix-pr-commit-links', 'click', handleButtonClick); } void features.add({ id: __filebasename, description: 'Suggests fixing your PR Commit links before commenting. GitHub has a bug that causes these link to appear as plain commit links, without association to the PR.', screenshot: 'https://user-images.githubusercontent.com/1402241/82131169-93fd5180-97d2-11ea-9695-97051c55091f.gif' }, { include: [ pageDetect.hasRichTextEditor ], init });