summaryrefslogtreecommitdiff
path: root/source/features/default-to-rich-diff.tsx
blob: f2c5a7b87ca17b1f8d4f78458ce498a9905ab753 (plain) (blame)
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
import select from 'select-dom';
import features from '../libs/features';

function run(): void {
	const buttons = select.all<HTMLButtonElement>(`
		[data-file-type=".svg"]
		[aria-label="Display the rich diff"]:not(.rgh-rich-diff)
	`);
	for (const button of buttons) {
		button.classList.add('rgh-rich-diff');

		// The ajax form handler might not be ready yet, so without this the page would change
		button.form!.addEventListener('submit', event => event.preventDefault());

		button.click();

		const interval = setInterval(() => {
			// Either button is selected (the last one to be clicked on) or rich diff container is being rendered
			if (button.classList.contains('selected') || button.closest('.js-details-container')!.querySelector('.render-wrapper')) {
				clearInterval(interval);
			} else {
				button.disabled = false;
				button.click();
			}
		}, 300);
	}
}

function init(): void {
	run();

	// Some files are loaded progressively later. On load, look for more buttons and more fragments
	for (const fragment of select.all('include-fragment.diff-progressive-loader')) {
		fragment.addEventListener('load', init);
	}
}

features.add({
	disabled: '#2041',
	id: __featureName__,
	description: 'Renders the rich diff by default in SVG files’ diffs.',
	screenshot: 'https://user-images.githubusercontent.com/5243867/57125552-c08a2b00-6d81-11e9-9b84-cdb535baa98e.png',
	include: [
		features.isCommit,
		features.isPRFiles
	],
	load: features.onAjaxedPages,
	init
});