Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

Commit 45c098c

Browse files
committed
get target from command event
1 parent 139dd0e commit 45c098c

File tree

1 file changed

+71
-67
lines changed

1 file changed

+71
-67
lines changed

lib/main.js

Lines changed: 71 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,51 @@
11
const {Disposable} = require('atom')
22
const GitHubFile = require('./github-file')
33

4-
function getActivePath () {
5-
const activePaneItem = atom.workspace.getActivePaneItem()
4+
function getActivePath (target) {
5+
if (!target) {
6+
return atom.project.getPaths()[0];
7+
}
8+
9+
const treeView = target.closest(".tree-view");
10+
if (treeView) {
11+
// called from treeview
12+
const selected = treeView.querySelector(".selected > .list-item > .name, .selected > .name");
13+
if (selected) {
14+
return selected.dataset.path;
15+
}
16+
return;
17+
}
18+
19+
const tab = target.closest(".tab-bar > .tab");
20+
if (tab) {
21+
// called from tab
22+
const title = tab.querySelector(".title");
23+
if (title && title.dataset.path) {
24+
return title.dataset.path;
25+
}
26+
return;
27+
}
628

7-
if (activePaneItem && typeof activePaneItem.getPath === 'function') {
8-
return activePaneItem.getPath()
29+
const paneItem = atom.workspace.getActivePaneItem();
30+
if (paneItem && typeof paneItem.getPath === "function") {
31+
// called from active pane
32+
return paneItem.getPath();
33+
}
34+
35+
const textEditor = atom.workspace.getActiveTextEditor();
36+
if (textEditor && typeof textEditor.getPath === "function") {
37+
// fallback to activeTextEditor if activePaneItem is not a file
38+
return textEditor.getPath();
39+
}
40+
}
41+
42+
function pathCommand(func) {
43+
return function (e) {
44+
const itemPath = getActivePath(e.target)
45+
46+
if (itemPath) {
47+
func(itemPath);
48+
}
949
}
1050
}
1151

@@ -21,77 +61,41 @@ module.exports = {
2161
activate () {
2262
this.commandsSubscription = new Disposable()
2363
this.commandsSubscription = atom.commands.add('atom-pane', {
24-
'open-on-github:file': () => {
25-
const itemPath = getActivePath()
26-
27-
if (itemPath) {
28-
GitHubFile.fromPath(itemPath).open(getSelectedRange())
29-
}
30-
},
31-
32-
'open-on-github:file-on-master': () => {
33-
const itemPath = getActivePath()
34-
35-
if (itemPath) {
36-
GitHubFile.fromPath(itemPath).openOnMaster(getSelectedRange())
37-
}
38-
},
39-
40-
'open-on-github:blame': () => {
41-
const itemPath = getActivePath()
42-
43-
if (itemPath) {
44-
GitHubFile.fromPath(itemPath).blame(getSelectedRange())
45-
}
46-
},
47-
48-
'open-on-github:history': () => {
49-
const itemPath = getActivePath()
50-
51-
if (itemPath) {
52-
GitHubFile.fromPath(itemPath).history()
53-
}
54-
},
55-
56-
'open-on-github:issues': () => {
57-
const itemPath = getActivePath()
58-
59-
if (itemPath) {
60-
GitHubFile.fromPath(itemPath).openIssues()
61-
}
62-
},
64+
'open-on-github:file': pathCommand((itemPath) => {
65+
GitHubFile.fromPath(itemPath).open(getSelectedRange())
66+
}),
6367

64-
'open-on-github:pull-requests': () => {
65-
const itemPath = getActivePath()
68+
'open-on-github:file-on-master': pathCommand((itemPath) => {
69+
GitHubFile.fromPath(itemPath).openOnMaster(getSelectedRange())
70+
}),
6671

67-
if (itemPath) {
68-
return GitHubFile.fromPath(itemPath).openPullRequests()
69-
}
70-
},
72+
'open-on-github:blame': pathCommand((itemPath) => {
73+
GitHubFile.fromPath(itemPath).blame(getSelectedRange())
74+
}),
7175

72-
'open-on-github:copy-url': () => {
73-
const itemPath = getActivePath()
76+
'open-on-github:history': pathCommand((itemPath) => {
77+
GitHubFile.fromPath(itemPath).history()
78+
}),
7479

75-
if (itemPath) {
76-
GitHubFile.fromPath(itemPath).copyURL(getSelectedRange())
77-
}
78-
},
80+
'open-on-github:issues': pathCommand((itemPath) => {
81+
GitHubFile.fromPath(itemPath).openIssues()
82+
}),
7983

80-
'open-on-github:branch-compare': () => {
81-
const itemPath = getActivePath()
84+
'open-on-github:pull-requests': pathCommand((itemPath) => {
85+
GitHubFile.fromPath(itemPath).openPullRequests()
86+
}),
8287

83-
if (itemPath) {
84-
GitHubFile.fromPath(itemPath).openBranchCompare()
85-
}
86-
},
88+
'open-on-github:copy-url': pathCommand((itemPath) => {
89+
GitHubFile.fromPath(itemPath).copyURL(getSelectedRange())
90+
}),
8791

88-
'open-on-github:repository': () => {
89-
const itemPath = getActivePath()
92+
'open-on-github:branch-compare': pathCommand((itemPath) => {
93+
GitHubFile.fromPath(itemPath).openBranchCompare()
94+
}),
9095

91-
if (itemPath) {
92-
GitHubFile.fromPath(itemPath).openRepository()
93-
}
94-
}
96+
'open-on-github:repository': pathCommand((itemPath) => {
97+
GitHubFile.fromPath(itemPath).openRepository()
98+
})
9599
})
96100
},
97101

0 commit comments

Comments
 (0)