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 106
/
Copy pathsnippets-available.js
84 lines (73 loc) · 2.22 KB
/
snippets-available.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
/** @babel */
import _ from 'underscore-plus'
import SelectListView from 'atom-select-list'
export default class SnippetsAvailable {
constructor (snippets) {
this.panel = null
this.snippets = snippets
this.selectListView = new SelectListView({
items: [],
filterKeyForItem: (snippet) => snippet.searchText,
elementForItem: (snippet) => {
const li = document.createElement('li')
li.classList.add('two-lines')
const primaryLine = document.createElement('div')
primaryLine.classList.add('primary-line')
primaryLine.textContent = snippet.prefix
li.appendChild(primaryLine)
const secondaryLine = document.createElement('div')
secondaryLine.classList.add('secondary-line')
secondaryLine.textContent = snippet.name
li.appendChild(secondaryLine)
return li
},
didConfirmSelection: (snippet) => {
for (const cursor of this.editor.getCursors()) {
this.snippets.insert(snippet.bodyText, this.editor, cursor)
}
this.cancel()
},
didConfirmEmptySelection: () => {
this.cancel()
},
didCancelSelection: () => {
this.cancel()
}
})
this.selectListView.element.classList.add('available-snippets')
this.element = this.selectListView.element
}
async toggle (editor) {
this.editor = editor
if (this.panel != null) {
this.cancel()
} else {
this.selectListView.reset()
await this.populate()
this.attach()
}
}
cancel () {
this.editor = null
if (this.panel != null) {
this.panel.destroy()
this.panel = null
}
if (this.previouslyFocusedElement) {
this.previouslyFocusedElement.focus()
this.previouslyFocusedElement = null
}
}
populate () {
const snippets = Object.values(this.snippets.getSnippets(this.editor))
for (let snippet of snippets) {
snippet.searchText = _.compact([snippet.prefix, snippet.name]).join(' ')
}
return this.selectListView.update({items: snippets})
}
attach () {
this.previouslyFocusedElement = document.activeElement
this.panel = atom.workspace.addModalPanel({item: this})
this.selectListView.focus()
}
}