forked from refined-github/refined-github
-
Notifications
You must be signed in to change notification settings - Fork 1
/
toggle-files-button.tsx
61 lines (52 loc) · 1.62 KB
/
toggle-files-button.tsx
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
52
53
54
55
56
57
58
59
60
61
import './toggle-files-button.css';
import cache from 'webext-storage-cache';
import React from 'dom-chef';
import select from 'select-dom';
import delegate from 'delegate-it';
import elementReady from 'element-ready';
import * as pageDetect from 'github-url-detection';
import FoldIcon from 'octicon/fold.svg';
import UnfoldIcon from 'octicon/unfold.svg';
import features from '.';
import observeElement from '../helpers/simplified-element-observer';
const cacheKey = 'files-hidden';
function addButton(): void {
// `div` excludes `include-fragment`, which means the list is still loading. #2160
const filesHeader = select([
'div.commit-tease',
'.Box-header--blue .Details > :last-child > ul' // "Repository refresh" layout
]);
if (!filesHeader || select.exists('.rgh-toggle-files')) {
return;
}
filesHeader.append(
<button
type="button"
className="btn-octicon rgh-toggle-files"
aria-label="Toggle files section"
>
<FoldIcon/>
<UnfoldIcon/>
</button>
);
}
async function toggleHandler(): Promise<void> {
const isHidden = select('.repository-content')!.classList.toggle('rgh-files-hidden');
await (isHidden ? cache.set(cacheKey, true) : cache.delete(cacheKey));
}
async function init(): Promise<void> {
const repoContent = (await elementReady('.repository-content'))!;
observeElement(repoContent, addButton);
delegate(document, '.rgh-toggle-files', 'click', toggleHandler);
if (await cache.get<boolean>(cacheKey)) {
repoContent.classList.add('rgh-files-hidden');
}
}
void features.add(__filebasename, {
include: [
pageDetect.isRepoTree
],
awaitDomReady: false,
repeatOnBackButton: true,
init
});