summaryrefslogtreecommitdiff
path: root/source/features/quick-file-edit.tsx
blob: dd3e018ae5ebac7acb419c0e6202762d5a9856e8 (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
50
51
// TODO: In 2024, drop js-navigation-item. Pre-React makeover

import './quick-file-edit.css';
import React from 'dom-chef';
import {PencilIcon} from '@primer/octicons-react';
import * as pageDetect from 'github-url-detection';

import {wrap} from '../helpers/dom-utils.js';
import features from '../feature-manager.js';
import GitHubFileURL from '../github-helpers/github-file-url.js';
import {isArchivedRepoAsync, isPermalink} from '../github-helpers/index.js';
import observe from '../helpers/selector-observer.js';
import {directoryListingFileIcon} from '../github-helpers/selectors.js';

async function linkifyIcon(fileIcon: Element): Promise<void> {
	const fileLink = fileIcon
		.closest('.js-navigation-item, .react-directory-filename-column')!
		.querySelector('a.js-navigation-open, a.Link--primary')!;

	const url = new GitHubFileURL(fileLink.href).assign({
		route: 'edit',
	});

	wrap(fileIcon, <a href={url.href} className="rgh-quick-file-edit"/>);
	fileIcon.after(<PencilIcon/>);
}

async function init(signal: AbortSignal): Promise<void | false> {
	observe(directoryListingFileIcon, linkifyIcon, {signal});
}

void features.add(import.meta.url, {
	include: [
		pageDetect.isRepoTree,
	],
	exclude: [
		pageDetect.isRepoFile404,
		isArchivedRepoAsync,
		isPermalink,
	],
	init,
});

/*

Test URLs

Legacy views: https://github.com/refined-github/refined-github
React views: https://github.com/refined-github/refined-github/tree/main/.github

*/