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 '../feature-manager.js';
import observe from '../helpers/selector-observer.js';
import {removeTextNodeContaining} from '../helpers/dom-utils.js';
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');
}
return (
{isHidingWhitespace() && } No Whitespace
);
}
function attachPRButtons(dropdownIcon: SVGElement): void {
// TODO: Replace with :has selector
const dropdown = dropdownIcon.closest('details.diffbar-item')!;
const diffSettingsForm = select('form[action$="/diffview"]', dropdown)!;
// Preserve data before emption the form
const isUnified = new FormData(diffSettingsForm).get('diff') === 'unified';
const token = select('[name="authenticity_token"]', diffSettingsForm)!;
// Empty form except the token field
diffSettingsForm.replaceChildren(token);
const type = isUnified ? 'split' : 'unified';
const Icon = isUnified ? BookIcon : DiffIcon;
diffSettingsForm.append(
,
);
if (!isHidingWhitespace()) {
diffSettingsForm.append(
,
);
}
dropdown.replaceWith(diffSettingsForm);
// 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!;
}
// Make space for the new button #655
removeTextNodeContaining(
select('[data-hotkey="c"] strong')!.previousSibling!,
'Changes from',
);
// Remove extraneous padding around "Clear filters" button
select('.subset-files-tab')?.classList.replace('px-sm-3', 'ml-sm-2');
}
function initPR(signal: AbortSignal): void {
// There are two "diff settings" element, one for mobile and one for the desktop. We only replace the one for the desktop
observe('.hide-sm.hide-md details.diffbar-item svg.octicon-gear', attachPRButtons, {signal});
}
function attachButtons(nativeDiffButtons: HTMLElement): void {
// TODO: Replace with :has()
const anchor = nativeDiffButtons.parentElement!;
// `usesFloats` is necessary to ensure the order and spacing as seen in #5958
const usesFloats = anchor?.classList.contains('float-right');
if (usesFloats) {
anchor.after(