Skip to content

Commit

Permalink
Add support for PR review comments to quick-comment-hiding (#6320)
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante committed Feb 11, 2023
1 parent 055abfe commit 158e94f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 deletions.
43 changes: 27 additions & 16 deletions source/features/quick-comment-hiding.tsx
Expand Up @@ -5,6 +5,11 @@ import * as pageDetect from 'github-url-detection';

import features from '../feature-manager';

const formSelector = [
'form[action$="/minimize-comment"]',
'form[action$="/minimize"]', // Review thread comments
];

function generateSubmenu(hideButton: Element): void {
if (hideButton.closest('.rgh-quick-comment-hiding-details')) {
// Already generated
Expand All @@ -15,13 +20,18 @@ function generateSubmenu(hideButton: Element): void {
detailsElement.classList.add('rgh-quick-comment-hiding-details');

const comment = hideButton.closest('.unminimized-comment')!;
const hideCommentForm = select('.js-comment-minimize', comment)!;
const hideCommentForm: HTMLFormElement = select(formSelector, comment)!;

// Generate dropdown
const newForm = hideCommentForm.cloneNode();
const fields = [...hideCommentForm.elements].map(field => field.cloneNode());
newForm.append(<i hidden>{fields}</i>); // Add existing fields (comment ID, token)

hideCommentForm.classList.remove('d-flex');
// Imitate existing menu, reset classes
newForm.className = ['dropdown-menu', 'dropdown-menu-sw', 'color-fg-default', 'show-more-popover', 'anim-scale-in'].join(' ');

// Generate dropdown items
for (const reason of select.all('[name="classifier"] option:not([value=""])', comment)) {
hideCommentForm.append(
for (const reason of select.all('option:not([value=""])', hideCommentForm.elements.classifier)) {
newForm.append(
<button
type="submit"
name="classifier"
Expand All @@ -34,19 +44,12 @@ function generateSubmenu(hideButton: Element): void {
);
}

// Drop previous form controls
select('.btn', hideCommentForm)!.remove();
select('[name="classifier"]', hideCommentForm)!.remove();

// Close immediately after the clicking option
hideCommentForm.addEventListener('click', () => {
detailsElement.removeAttribute('open');
newForm.addEventListener('click', () => {
detailsElement.open = false;
});

// Imitate existing menu
hideCommentForm.classList.add('dropdown-menu', 'dropdown-menu-sw', 'color-fg-default', 'show-more-popover', 'anim-scale-in');

detailsElement.append(hideCommentForm);
detailsElement.append(newForm);
}

// Shows menu on top of mainDropdownContent when "Hide" is clicked;
Expand All @@ -59,7 +62,7 @@ function toggleSubMenu(hideButton: Element, show: boolean): void {
select('details-menu', dropdown)!.classList.toggle('v-hidden', show);

// "Hide comment" dropdown
select('form.js-comment-minimize', dropdown)!.classList.toggle('v-hidden', !show);
select(formSelector, dropdown)!.classList.toggle('v-hidden', !show);
}

function resetDropdowns(event: DelegateEvent): void {
Expand All @@ -86,3 +89,11 @@ void features.add(import.meta.url, {
],
init,
});

/*
Test URLs
https://github.com/refined-github/sandbox/pull/47
*/
2 changes: 1 addition & 1 deletion source/globals.d.ts
Expand Up @@ -65,7 +65,7 @@ interface NamedNodeMap {

// Drop after https://github.com/Microsoft/TypeScript/issues/30928
interface HTMLFormControlsCollection {
[key: string]: HTMLInputElement | HTMLTextAreaElement | HTMLButtonElement;
[key: string]: HTMLInputElement | HTMLTextAreaElement | HTMLButtonElement | HTMLSelectElement;
}

declare module 'react' {
Expand Down

0 comments on commit 158e94f

Please sign in to comment.