summaryrefslogtreecommitdiff
path: root/source/features/link-to-file-in-file-history.tsx
diff options
context:
space:
mode:
authorGravatar Hardik Modha <hardikmodha22@gmail.com> 2019-05-05 23:49:45 +0530
committerGravatar Federico Brigante <github@bfred.it> 2019-05-06 02:19:45 +0800
commitad17f062d53fa911c131ce1be964e1d20d5e6f72 (patch)
tree564f83615126e26684eded98b5985cef57fd199d /source/features/link-to-file-in-file-history.tsx
parent3150af62d16f6aac7bce36cec00a83a876df357d (diff)
downloadrefined-github-ad17f062d53fa911c131ce1be964e1d20d5e6f72.tar.gz
refined-github-ad17f062d53fa911c131ce1be964e1d20d5e6f72.tar.zst
refined-github-ad17f062d53fa911c131ce1be964e1d20d5e6f72.zip
Add `link-to-file-in-file-history` feature (#2000)
Co-authored-by: Federico Brigante <github@bfred.it>
Diffstat (limited to 'source/features/link-to-file-in-file-history.tsx')
-rw-r--r--source/features/link-to-file-in-file-history.tsx47
1 files changed, 47 insertions, 0 deletions
diff --git a/source/features/link-to-file-in-file-history.tsx b/source/features/link-to-file-in-file-history.tsx
new file mode 100644
index 00000000..ef55a483
--- /dev/null
+++ b/source/features/link-to-file-in-file-history.tsx
@@ -0,0 +1,47 @@
+/**
+Adds direct link to file/directory when viewing the history.
+See it in action at https://github.com/sindresorhus/refined-github/commits/master/readme.md
+*/
+
+import React from 'dom-chef';
+import select from 'select-dom';
+import features from '../libs/features';
+import {file} from '../libs/icons';
+import {getRepoPath} from '../libs/utils';
+import {groupSiblings} from '../libs/group-buttons';
+
+function init(): void | false {
+ // /user/repo/commits/master/readme.md -> 'readme.md'
+ // /user/repo/commits/master/ -> ''
+ const path = getRepoPath()!.replace(/^commits\/[^/]+\//, '');
+ if (!path) {
+ return false;
+ }
+
+ for (const rootLink of select.all<HTMLAnchorElement>('[aria-label="Browse the repository at this point in the history"]')) {
+ // `rootLink.pathname` points to /tree/ but GitHub automatically redirects to /blob/ when the path is of a file
+ rootLink.before(
+ <a
+ href={rootLink.pathname + '/' + path}
+ className="btn btn-outline tooltipped tooltipped-sw"
+ aria-label="See object at this point in the history"
+ >
+ {file()}
+ </a>
+ );
+
+ // TODO: drop `as` after https://github.com/Microsoft/TSJS-lib-generator/pull/697
+ (rootLink.closest('.commit-links-cell') as HTMLElement).style.width = 'auto';
+
+ groupSiblings(rootLink);
+ }
+}
+
+features.add({
+ id: 'link-to-file-in-file-history',
+ include: [
+ features.isCommitList
+ ],
+ load: features.onAjaxedPages,
+ init
+});