import React from 'dom-chef'; import select from 'select-dom'; import * as pageDetect from 'github-url-detection'; import {BookIcon, CheckIcon, DiffIcon, DiffModifiedIcon} from '@primer/octicons-react'; import features from '.'; import selectHas from '../helpers/select-has'; import {onDiffFileLoad} from '../github-events/on-fragment-load'; function makeLink(type: string, icon: Element, selected: boolean): JSX.Element { const url = new URL(location.href); url.searchParams.set('diff', type); const classes = pageDetect.isPR() ? 'tooltipped tooltipped-s d-none d-lg-block ml-2 color-icon-secondary color-fg-muted' : 'tooltipped tooltipped-s btn btn-sm BtnGroup-item ' + (selected ? 'selected' : ''); return ( {icon} ); } function createDiffStyleToggle(): DocumentFragment { const isUnified = select.exists([ '[value="unified"][checked]', // Form in PR '.table-of-contents .selected[href*="diff=unified"]', // Link in single commit ]); if (pageDetect.isPR()) { return isUnified ? makeLink('split', , false) : makeLink('unified', , false); } return ( <> {makeLink('unified', , isUnified)} {makeLink('split', , !isUnified)} ); } function isHidingWhitespace(): boolean { // The selector is the native button return new URL(location.href).searchParams.get('w') === '1' || select.exists('button[name="w"][value="0"]:not([hidden])'); } function createWhitespaceButton(): HTMLElement { const url = new URL(location.href); if (isHidingWhitespace()) { url.searchParams.delete('w'); } else { url.searchParams.set('w', '1'); } const classes = pageDetect.isPR() ? 'tooltipped tooltipped-s d-none d-lg-block color-icon-secondary color-fg-muted' : 'tooltipped tooltipped-s btn btn-sm tooltipped ' + (isHidingWhitespace() ? 'color-text-tertiary color-fg-subtle' : ''); return ( {pageDetect.isPR() ? : <>{isHidingWhitespace() && } No Whitespace} ); } function initPR(): false | void { const originalToggle = selectHas('details:has([aria-label="Diff settings"])')!.parentElement!; if (!isHidingWhitespace()) { originalToggle.after(
{createWhitespaceButton()}
, ); } originalToggle.after(
{createDiffStyleToggle()}
, ); // Trim title const prTitle = select('.pr-toolbar .js-issue-title'); if (prTitle && select.exists('.pr-toolbar progress-bar')) { // Only review view has progress-bar prTitle.style.maxWidth = '24em'; prTitle.title = prTitle.textContent!; } originalToggle.classList.add('d-lg-none'); // Make space for the new button by removing "Changes from" #655 select('[data-hotkey="c"] strong')!.previousSibling!.remove(); // Remove extraneous padding around "Clear filters" button select('.subset-files-tab')?.classList.replace('px-sm-3', 'ml-sm-2'); } function initCommitAndCompare(): false | void { select('#toc')!.prepend(
{createWhitespaceButton()}
, ); } const shortcuts = { 'd w': 'Show/hide whitespaces in diffs', }; void features.add(import.meta.url, { shortcuts, include: [ pageDetect.isPRFiles, pageDetect.isPRCommit, ], exclude: [ pageDetect.isPRFile404, ], deduplicate: 'has-rgh-inner', init: initPR, }, { shortcuts, include: [ pageDetect.isSingleCommit, ], init: initCommitAndCompare, }, { shortcuts, include: [ pageDetect.isCompare, ], additionalListeners: [ onDiffFileLoad, ], onlyAdditionalListeners: true, deduplicate: false, init: initCommitAndCompare, });