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
/
Copy pathpackage-snippets-view.js
236 lines (199 loc) · 7.1 KB
/
package-snippets-view.js
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/** @babel */
/** @jsx etch.dom */
import path from 'path'
import _ from 'underscore-plus'
import etch from 'etch'
import {CompositeDisposable, Disposable} from 'atom'
// View to display the snippets that a package has registered.
export default class PackageSnippetsView {
constructor (pack, snippetsProvider) {
this.pack = pack
this.namespace = this.pack.name
this.snippetsProvider = snippetsProvider
this.packagePath = path.join(pack.path, path.sep)
etch.initialize(this)
this.disposables = new CompositeDisposable()
this.updateSnippetsView()
const packagesWithSnippetsDisabled = atom.config.get('core.packagesWithSnippetsDisabled') || []
this.refs.snippetToggle.checked = !packagesWithSnippetsDisabled.includes(this.namespace)
const changeHandler = (event) => {
event.stopPropagation()
const value = this.refs.snippetToggle.checked
if (value) {
atom.config.removeAtKeyPath('core.packagesWithSnippetsDisabled', this.namespace)
} else {
atom.config.pushAtKeyPath('core.packagesWithSnippetsDisabled', this.namespace)
}
this.updateSnippetsView()
}
this.refs.snippetToggle.addEventListener('change', changeHandler)
this.disposables.add(new Disposable(() => { this.refs.snippetToggle.removeEventListener('change', changeHandler) }))
}
destroy () {
this.disposables.dispose()
return etch.destroy(this)
}
update () {}
render () {
return (
<section className='section'>
<div className='section-heading icon icon-code'>Snippets</div>
<div className='checkbox'>
<label for='toggleSnippets'>
<input id='toggleSnippets' className='input-checkbox' type='checkbox' ref='snippetToggle' />
<div className='setting-title'>Enable</div>
</label>
<div className='setting-description'>
{'Disable this if you want to prevent this package’s snippets from appearing as suggestions or if you want to customize them in your snippets file.'}
</div>
</div>
<table className='package-snippets-table table native-key-bindings text' tabIndex={-1}>
<thead>
<tr>
<th>Trigger</th>
<th>Name</th>
<th>Scope</th>
<th>Body</th>
</tr>
</thead>
<tbody ref='snippets' />
</table>
</section>
)
}
getSnippetProperties () {
const packageProperties = {}
for (const {name, properties, selectorString} of this.snippetsProvider.getSnippets()) {
if (name && name.indexOf && name.indexOf(this.packagePath) === 0) {
const object = properties.snippets != null ? properties.snippets : {}
for (let key in object) {
const snippet = object[key]
if (snippet != null) {
snippet.selectorString = selectorString
if (packageProperties[key] == null) {
packageProperties[key] = snippet
}
}
}
}
}
return _.values(packageProperties).sort((snippet1, snippet2) => {
const prefix1 = snippet1.prefix != null ? snippet1.prefix : ''
const prefix2 = snippet2.prefix != null ? snippet2.prefix : ''
return prefix1.localeCompare(prefix2)
})
}
getSnippets (callback) {
const snippetsPackage = atom.packages.getLoadedPackage('snippets')
const snippetsModule = snippetsPackage ? snippetsPackage.mainModule : null
if (snippetsModule) {
if (snippetsModule.loaded) {
callback(this.getSnippetProperties())
} else {
snippetsModule.onDidLoadSnippets(() => callback(this.getSnippetProperties()))
}
} else {
callback([]) // eslint-disable-line standard/no-callback-literal
}
}
updateSnippetsView () {
const packagesWithSnippetsDisabled = atom.config.get('core.packagesWithSnippetsDisabled') || []
const snippetsDisabled = packagesWithSnippetsDisabled.includes(this.namespace)
this.getSnippets((snippets) => {
this.refs.snippets.innerHTML = ''
if (snippetsDisabled) {
this.refs.snippets.classList.add('text-subtle')
} else {
this.refs.snippets.classList.remove('text-subtle')
}
for (let {body, bodyText, name, prefix, selectorString} of snippets) {
if (name == null) {
name = ''
}
if (prefix == null) {
prefix = ''
}
if (body == null) {
body = bodyText || ''
}
if (selectorString == null) {
selectorString = ''
}
const row = document.createElement('tr')
const prefixTd = document.createElement('td')
prefixTd.classList.add('snippet-prefix')
prefixTd.textContent = prefix
row.appendChild(prefixTd)
const nameTd = document.createElement('td')
nameTd.textContent = name
row.appendChild(nameTd)
const scopeTd = document.createElement('td')
scopeTd.classList.add('snippet-scope-name')
scopeTd.textContent = selectorString
row.appendChild(scopeTd)
const bodyTd = document.createElement('td')
bodyTd.classList.add('snippet-body')
row.appendChild(bodyTd)
this.refs.snippets.appendChild(row)
this.createButtonsForSnippetRow(bodyTd, {body, prefix, scope: selectorString, name})
}
if (this.refs.snippets.children.length > 0) {
this.element.style.display = ''
} else {
this.element.style.display = 'none'
}
})
}
createButtonsForSnippetRow (td, {scope, body, name, prefix}) {
let buttonContainer = document.createElement('div')
buttonContainer.classList.add('btn-group', 'btn-group-xs')
let viewButton = document.createElement('button')
let copyButton = document.createElement('button')
viewButton.setAttribute('type', 'button')
viewButton.textContent = 'View'
viewButton.classList.add('btn', 'snippet-view-btn')
let tooltip = atom.tooltips.add(viewButton, {
title: body,
html: false,
trigger: 'click',
placement: 'auto left',
'class': 'snippet-body-tooltip'
})
this.disposables.add(tooltip)
copyButton.setAttribute('type', 'button')
copyButton.textContent = 'Copy'
copyButton.classList.add('btn', 'snippet-copy-btn')
copyButton.addEventListener('click', (event) => {
event.preventDefault()
return this.writeSnippetToClipboard({scope, body, name, prefix})
})
buttonContainer.appendChild(viewButton)
buttonContainer.appendChild(copyButton)
td.appendChild(buttonContainer)
}
writeSnippetToClipboard ({scope, body, name, prefix}) {
let content
const extension = path.extname(this.snippetsProvider.getUserSnippetsPath())
body = body.replace(/\n/g, '\\n').replace(/\t/g, '\\t')
if (extension === '.cson') {
body = body.replace(/'/g, `\\'`)
content = `
'${scope}':
'${name}':
'prefix': '${prefix}'
'body': '${body}'
`
} else {
body = body.replace(/"/g, `\\"`)
content = `
"${scope}": {
"${name}": {
"prefix": "${prefix}",
"body": "${body}"
}
}
`
}
atom.clipboard.write(content)
}
}