This repository has been archived by the owner on Jun 22, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 124
/
search.js
282 lines (256 loc) · 8.68 KB
/
search.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/* global localStorage, fetch */
var emojilib = JSON.parse(localStorage.getItem('emojilib')) || require('emojilib').lib
var emojikeys = JSON.parse(localStorage.getItem('emojikeys')) || require('emojilib').ordered
var modifiers = require('emojilib').fitzpatrick_scale_modifiers
var clipboard = require('electron').clipboard
var ipc = require('electron').ipcRenderer
var index = buildIndex()
var indexKeys = Object.keys(index)
var emojikeyIndexTable = buildEmojikeyIndexTable()
var searching = false
var searchInput = document.querySelector('.js-search')
var preference = JSON.parse(localStorage.getItem('preference'))
var directions = {
37: 'left',
38: 'up',
39: 'right',
40: 'down'
}
function fetchAndUpdateLocalCache () {
if (!navigator.onLine) return
var expireTime = localStorage.getItem('emojilibExpireTime')
if (expireTime && Number(expireTime) > new Date().getTime()) return
var version = '^2.0.0'
var emojilibLib = `https://unpkg.com/emojilib@${version}/emojis.json`
var emojilibOrdered = `https://unpkg.com/emojilib@${version}/ordered.json`
fetch(emojilibLib).then(function (res) { return checkIfNewVersion(res) }).then(function (newData) {
// Fetch only once per day
localStorage.setItem('emojilibExpireTime', new Date().getTime() + 1000 * 60 * 60 * 24)
if (!newData) return
localStorage.setItem('emojilib', JSON.stringify(newData))
fetch(emojilibOrdered).then(function (res) { return res.json() }).then(function (newData) {
localStorage.setItem('emojikeys', JSON.stringify(newData))
window.location.reload()
})
})
function checkIfNewVersion (res) {
var fetchedVersion = res.url.match(/@([\d.]+)/)[1]
if (fetchedVersion !== localStorage.getItem('emojilibVersion')) {
localStorage.setItem('emojilibVersion', fetchedVersion)
return res.json()
}
}
}
searchInput.dataset.isSearchInput = true
searchInput.focus()
search('')
searchInput.addEventListener('input', function () {
search(this.value.toLowerCase())
})
ipc.on('show', function (event, message) {
searchInput.focus()
})
ipc.on('fetch', function (event, message) {
fetchAndUpdateLocalCache()
})
document.addEventListener('mousewheel', function (e) {
if (e.deltaY % 1 !== 0) {
e.preventDefault()
}
})
document.addEventListener('keydown', function (evt) {
var onSearchField = !!evt.target.dataset.isSearchInput
if (onSearchField) {
if (evt.keyCode === 40) {
// on down: focus on the first thing!
jumpto('up')
evt.preventDefault()
} else if (evt.keyCode === 13) {
copyFocusedEmoji(document.querySelector('.emoji:first-child'), evt.shiftKey)
}
} else if (evt.target.classList.contains('emoji')) {
if (evt.keyCode === 32) {
if (evt.shiftKey) {
jumpto('prev')
} else {
jumpto('next')
}
} else if (evt.keyCode === 13) {
copyFocusedEmoji(evt.target, evt.shiftKey)
} else if (Object.keys(directions).indexOf(evt.keyCode.toString()) >= 0) {
// on navigation, navigate
jumpto(directions[evt.keyCode])
}
}
if (!onSearchField && evt.keyCode === 191 && !evt.shiftKey && !evt.metaKey && !evt.ctrlKey) {
// on `/`: focus on the search field
searchInput.select()
evt.preventDefault()
} else if (evt.keyCode === 27) {
// on escape: exit
ipc.send('abort')
}
})
function copyFocusedEmoji (emoji, copyText) {
var data
// on enter: copy data and exit
if (copyText) {
data = ':' + emoji.getAttribute('aria-label') + ':'
} else {
data = emoji.innerText
}
clipboard.writeText(data)
searchInput.value = ''
search('')
ipc.send('abort')
}
document.addEventListener('keypress', function (evt) {
// if typing while navigatin, just type into the search box!
var word = isWord(evt.charCode)
if (word && evt.target.classList.contains('emoji')) {
searchInput.focus()
}
})
// if click on and emoji item, copy emoji unicode char to clipboard on click or
// copy emoji code if `shiftKey` is pressed
document.addEventListener('click', function (evt) {
if (evt.target.classList.contains('emoji')) {
copyFocusedEmoji(evt.target, evt.shiftKey)
}
})
function stringIncludes (string, search) {
if (search.length > string.length) {
return false
} else {
return string.indexOf(search) !== -1
}
}
function search (query) {
if (searching) {
clearTimeout(searching)
}
searching = setTimeout(function () {
var results
if (query.length === 0 || (query.length === 1 && query.charCodeAt() <= 255)) {
results = emojikeys.slice(0)
} else {
var resultsDict = {}
indexKeys.forEach(function matchQuery (keyword) {
if (stringIncludes(keyword, query)) {
index[keyword].forEach(function addMatchingEmoji (emoji) {
resultsDict[emoji] = true
})
}
})
results = Object.keys(resultsDict).sort(function sortResults (a, b) {
return emojikeyIndexTable[a] - emojikeyIndexTable[b]
})
}
// Put exact match first
if (results.indexOf(query) >= 0) {
results.splice(results.indexOf(query), 1)
results.unshift(query)
}
renderResults(results, document.querySelector('.js-results'))
if (document.querySelector('.emoji')) document.querySelector('.emoji').scrollIntoViewIfNeeded()
}, 80)
}
function renderResults (emojiNameArray, containerElement) {
containerElement.innerHTML = ''
var fragment = document.createDocumentFragment()
var modifierValue = preference['skin-tone-modifier']
var modifier = modifiers.indexOf(modifierValue) >= 0 ? modifierValue : null
emojiNameArray.forEach(function (name) {
var unicode = addModifier(emojilib[name], modifier) || '--'
var resultElement = document.createElement('button')
resultElement.type = 'button'
resultElement.className = 'emoji'
resultElement.setAttribute('aria-label', name)
resultElement.textContent = unicode
fragment.appendChild(resultElement)
})
containerElement.appendChild(fragment)
}
function buildEmojikeyIndexTable () {
var indexTable = {}
emojikeys.forEach(function (name, index) {
indexTable[name] = index
})
return indexTable
}
function buildIndex () {
var keywords = {}
emojikeys.forEach(function (name) {
var words = emojilib[name]['keywords']
words.push(name)
words.push(emojilib[name]['char'])
words.push(emojilib[name]['category'])
words.forEach(function (word) {
if (keywords[word] && keywords[word].indexOf(name) < 0) {
keywords[word].push(name)
} else if (!keywords[word]) {
keywords[word] = [name]
}
})
})
return keywords
}
function isWord (charCode) {
return String.fromCharCode(charCode).match(/\w/)
}
// Insert modifier in front of zwj
function addModifier (emoji, modifier) {
if (!modifier || !emoji['fitzpatrick_scale']) return emoji['char']
var zwj = new RegExp('', 'g')
return emoji['char'].match(zwj) ? emoji['char'].replace(zwj, modifier + '') : emoji['char'] + modifier
}
function jumpto (destination) {
var container = document.getElementsByClassName('js-results')[0]
var all = document.getElementsByClassName('emoji')
var focusedElement = document.querySelector('.emoji:focus')
var nodeIndex = Array.prototype.indexOf.call(all, focusedElement)
var resultPerRow = Math.floor(container.clientWidth / all[0].clientWidth)
var resultPerCol = Math.floor(container.clientHeight / all[0].clientHeight)
var newTarget
if (destination === 'up') {
newTarget = nodeIndex - resultPerRow
} else if (destination === 'down') {
newTarget = nodeIndex + resultPerRow
} else if (destination === 'left') {
if ((nodeIndex + 1) % resultPerRow === 1) {
// Wrap to previous row.
newTarget = nodeIndex + (resultPerRow - 1) // Adjust to last column.
newTarget -= resultPerRow // Up one row.
} else {
newTarget = nodeIndex - 1
}
} else if (destination === 'right') {
if ((nodeIndex + 1) % resultPerRow === 0) {
// Wrap to next row.
newTarget = nodeIndex - (resultPerRow - 1) // Adjust to first column.
newTarget += resultPerRow // Down one row.
} else {
newTarget = nodeIndex + 1
}
} else if (destination === 'next') {
newTarget = nodeIndex + resultPerRow * (resultPerCol - 1 || 1)
} else if (destination === 'prev') {
newTarget = nodeIndex - resultPerRow * (resultPerCol - 1 || 1)
}
if (newTarget < 0) {
// Allow jump back up to search field IF already at first item.
if (nodeIndex === 0) {
// Purposefully mismatch so we focus on input instead.
newTarget = -1
} else {
newTarget = 0
}
}
if (newTarget >= all.length - 1) newTarget = all.length - 1
if (all[newTarget]) {
all[newTarget].focus()
all[newTarget].scrollIntoViewIfNeeded()
} else {
searchInput.focus()
}
}