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 276
Expand file tree
/
Copy pathuri-handler-panel.js
More file actions
210 lines (184 loc) · 6.54 KB
/
uri-handler-panel.js
File metadata and controls
210 lines (184 loc) · 6.54 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/** @babel */
/** @jsx etch.dom */
import {CompositeDisposable} from 'atom'
import etch from 'etch'
function isSupported () {
return ['win32', 'darwin'].includes(process.platform)
}
function isDefaultProtocolClient () {
return require('electron').remote.app.isDefaultProtocolClient('atom', process.execPath, ['--uri-handler', '--'])
}
function setAsDefaultProtocolClient () {
// This Electron API is only available on Windows and macOS. There might be some
// hacks to make it work on Linux; see https://github.com/electron/electron/issues/6440
return isSupported() && require('electron').remote.app.setAsDefaultProtocolClient('atom', process.execPath, ['--uri-handler', '--'])
}
export default class UriHandlerPanel {
constructor () {
this.handleChange = this.handleChange.bind(this)
this.handleBecomeProtocolClient = this.handleBecomeProtocolClient.bind(this)
this.isDefaultProtocolClient = isDefaultProtocolClient()
this.uriHistory = []
etch.initialize(this)
this.subscriptions = new CompositeDisposable()
this.subscriptions.add(
atom.commands.add(this.element, {
'core:move-up': () => { this.scrollUp() },
'core:move-down': () => { this.scrollDown() },
'core:page-up': () => { this.pageUp() },
'core:page-down': () => { this.pageDown() },
'core:move-to-top': () => { this.scrollToTop() },
'core:move-to-bottom': () => { this.scrollToBottom() }
}),
atom.uriHandlerRegistry.onHistoryChange(() => {
this.uriHistory = atom.uriHandlerRegistry.getRecentlyHandledURIs()
etch.update(this)
})
)
}
destroy () {
this.subscriptions.dispose()
return etch.destroy(this)
}
update () {}
render () {
const schema = atom.config.getSchema('core.uriHandlerRegistration')
return (
<div className='panels-item' tabIndex='0'>
<form className='general-panel section'>
<div className='settings-panel'>
<div className='section-container'>
<div className='block section-heading icon icon-device-desktop'>URI Handling</div>
<div className='text icon icon-question'>These settings determine how Atom handles atom:// URIs.</div>
<div className='section-body'>
<div className='control-group'>
<div className='controls'>
<label className='control-label'>
<div className='setting-title'>URI Handler Registration</div>
<div className='setting-description'>
{this.renderRegistrationDescription()}
</div>
</label>
<button
className='btn btn-primary'
disabled={!isSupported() || this.isDefaultProtocolClient}
style={{fontSize: '1.25em', display: 'block'}}
onClick={this.handleBecomeProtocolClient}
>
Register as default atom:// protocol handler
</button>
</div>
</div>
<div className='control-group'>
<div className='controls'>
<label className='control-label'>
<div className='setting-title'>Default Registration</div>
<div className='setting-description'>
{schema.description}
</div>
</label>
<select
id='core.uriHandlerRegistration'
className='form-control'
onChange={this.handleChange}
value={atom.config.get('core.uriHandlerRegistration')}
>
{schema.enum.map(({description, value}) => (
<option value={value}>{description}</option>
))}
</select>
</div>
</div>
<div className='control-group'>
<div className='controls'>
<label className='controls-label'>
<div className='setting-title'>Recent URIs</div>
</label>
<table className='uri-history'>
<tr>
<th>URI</th>
<th>Handled By</th>
</tr>
{this.uriHistory.map(this.renderHistoryRow.bind(this))}
</table>
</div>
</div>
</div>
</div>
</div>
</form>
</div>
)
}
renderHistoryRow (item, idx) {
return (
<tr
key={item.id}
className=''
>
<td>{item.uri}</td>
<td>
{item.handled
? this.renderItem(item)
: <em>not handled</em>
}</td>
</tr>
)
}
renderItem (item) {
if (item.host === 'core') {
return <em>core</em>
} else {
return <a href={`atom://config/packages/${item.host}`} onClick={this.handlePackageLinkClicked}>{item.host}</a>
}
}
handlePackageLinkClicked (evt) {
evt.preventDefault()
atom.workspace.open(evt.target.getAttribute('href'))
}
renderRegistrationDescription () {
if (this.isDefaultProtocolClient) {
return 'Atom is already the default handler for atom:// URIs.'
} else if (isSupported()) {
return 'Register Atom as the default handler for atom:// URIs.'
} else {
return 'Registration as the default handler for atom:// URIs is only supported on Windows and macOS.'
}
}
handleChange (evt) {
atom.config.set('core.uriHandlerRegistration', evt.target.value)
}
handleBecomeProtocolClient (evt) {
evt.preventDefault()
if (setAsDefaultProtocolClient()) {
this.isDefaultProtocolClient = isDefaultProtocolClient()
etch.update(this)
} else {
atom.notifications.addError('Could not become default protocol client')
}
}
focus () {
this.element.focus()
}
show () {
this.element.style.display = ''
}
scrollUp () {
this.element.scrollTop -= document.body.offsetHeight / 20
}
scrollDown () {
this.element.scrollTop += document.body.offsetHeight / 20
}
pageUp () {
this.element.scrollTop -= this.element.offsetHeight
}
pageDown () {
this.element.scrollTop += this.element.offsetHeight
}
scrollToTop () {
this.element.scrollTop = 0
}
scrollToBottom () {
this.element.scrollTop = this.element.scrollHeight
}
}