1
1
const { Disposable} = require ( 'atom' )
2
2
const GitHubFile = require ( './github-file' )
3
3
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
+ }
6
28
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
+ }
9
49
}
10
50
}
11
51
@@ -21,77 +61,41 @@ module.exports = {
21
61
activate ( ) {
22
62
this . commandsSubscription = new Disposable ( )
23
63
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
+ } ) ,
63
67
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
+ } ) ,
66
71
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
+ } ) ,
71
75
72
- 'open-on-github:copy-url' : ( ) => {
73
- const itemPath = getActivePath ( )
76
+ 'open-on-github:history' : pathCommand ( ( itemPath ) => {
77
+ GitHubFile . fromPath ( itemPath ) . history ( )
78
+ } ) ,
74
79
75
- if ( itemPath ) {
76
- GitHubFile . fromPath ( itemPath ) . copyURL ( getSelectedRange ( ) )
77
- }
78
- } ,
80
+ 'open-on-github:issues' : pathCommand ( ( itemPath ) => {
81
+ GitHubFile . fromPath ( itemPath ) . openIssues ( )
82
+ } ) ,
79
83
80
- 'open-on-github:branch-compare' : ( ) => {
81
- const itemPath = getActivePath ( )
84
+ 'open-on-github:pull-requests' : pathCommand ( ( itemPath ) => {
85
+ GitHubFile . fromPath ( itemPath ) . openPullRequests ( )
86
+ } ) ,
82
87
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
+ } ) ,
87
91
88
- 'open-on-github:repository' : ( ) => {
89
- const itemPath = getActivePath ( )
92
+ 'open-on-github:branch-compare' : pathCommand ( ( itemPath ) => {
93
+ GitHubFile . fromPath ( itemPath ) . openBranchCompare ( )
94
+ } ) ,
90
95
91
- if ( itemPath ) {
92
- GitHubFile . fromPath ( itemPath ) . openRepository ( )
93
- }
94
- }
96
+ 'open-on-github:repository' : pathCommand ( ( itemPath ) => {
97
+ GitHubFile . fromPath ( itemPath ) . openRepository ( )
98
+ } )
95
99
} )
96
100
} ,
97
101
0 commit comments