This repository was archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 408
Expand file tree
/
Copy pathissueish-link.js
More file actions
79 lines (66 loc) · 2.28 KB
/
issueish-link.js
File metadata and controls
79 lines (66 loc) · 2.28 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import url from 'url';
import {shell} from 'electron';
import React from 'react';
import PropTypes from 'prop-types';
import IssueishDetailItem from '../items/issueish-detail-item';
import {addEvent} from '../reporter-proxy';
// eslint-disable-next-line no-shadow
export default function IssueishLink({url, children, ...others}) {
function clickHandler(event) {
handleClickEvent(event, url);
}
return <a {...others} onClick={clickHandler}>{children}</a>;
}
IssueishLink.propTypes = {
url: PropTypes.string.isRequired,
children: PropTypes.node,
};
// eslint-disable-next-line no-shadow
export function handleClickEvent(event, url) {
event.preventDefault();
event.stopPropagation();
if (!event.shiftKey) {
return openIssueishLinkInNewTab(url, {activate: !(event.metaKey || event.ctrlKey)});
} else {
// Open in browser if shift key held
return openLinkInBrowser(url);
}
}
// eslint-disable-next-line no-shadow
export function openIssueishLinkInNewTab(url, options = {}) {
const uri = getAtomUriForGithubUrl(url);
if (uri) {
return openInNewTab(uri, options);
} else {
return null;
}
}
export async function openLinkInBrowser(uri) {
await shell.openExternal(uri);
addEvent('open-issueish-in-browser', {package: 'github', from: 'issueish-link'});
}
function getAtomUriForGithubUrl(githubUrl) {
return getUriForData(getDataFromGithubUrl(githubUrl));
}
export function getDataFromGithubUrl(githubUrl) {
const {hostname, pathname} = url.parse(githubUrl);
const [repoOwner, repoName, type, issueishNumber] = pathname.split('/').filter(s => s);
return {hostname, repoOwner, repoName, type, issueishNumber: parseInt(issueishNumber, 10)};
}
function getUriForData({hostname, repoOwner, repoName, type, issueishNumber}) {
if (hostname !== 'github.com' || !['pull', 'issues'].includes(type) || !issueishNumber || isNaN(issueishNumber)) {
return null;
} else {
return IssueishDetailItem.buildURI({
host: 'github.com',
owner: repoOwner,
repo: repoName,
number: issueishNumber,
});
}
}
function openInNewTab(uri, {activate} = {activate: true}) {
return atom.workspace.open(uri, {activateItem: activate}).then(() => {
addEvent('open-issueish-in-pane', {package: 'github', from: 'issueish-link', target: 'new-tab'});
});
}