-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
162 lines (126 loc) · 3.67 KB
/
script.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
let allEmotes
let filteredEmotes
//#region emote processing
function normalizeEmote(raw) {
const {
text = "",
name = "",
id = "",
tags = [],
} = raw
if (!text || !name) return
return {
text: text,
name: name.toLowerCase(),
id: id.toLowerCase(),
tags,
}
}
async function loadEmotes() {
allEmotes = await fetch("emotes.json")
.then(d => d.json())
.then(d => Object.entries(d)
.map(([id, emote]) => normalizeEmote({ ...emote, id }))
.filter(emote => emote)
.sort((a, b) => a.name.localeCompare(b.name))
)
filterEmotes()
}
const filterDebounceDuration = 200
let filterDebounce
function filterEmotes() {
const filter = search.value.toLowerCase()
filteredEmotes = allEmotes.filter(e => e.name.includes(filter))
updateEmoteList()
}
function updateEmoteList() {
if (!allEmotes) return
emojicons.innerHTML = ""
if (!filterEmotes.length) {
emojicons.dataset.emptyMessage = "No match found"
}
for (const emote of filteredEmotes) {
const el = makeEmoteElement(emote)
emojicons.appendChild(el)
}
}
function makeEmoteElement(emote) {
const wrapper = document.createElement("li")
wrapper.classList.add("emote-wrapper")
const label = document.createElement("label")
label.dataset.emote = emote.text
label.textContent = emote.name
label.htmlFor = emote.id
const input = document.createElement("input")
input.value = emote.text
input.addEventListener("focus", evt => evt.target.select())
input.id = emote.id
const btn = document.createElement("button")
btn.innerHTML = `<span class="visually-hidden"> copy "${emote.name}"</span>`
btn.dataset.clipboardContent = emote.text
btn.addEventListener("click", onCopyBtnClick)
wrapper.appendChild(label)
wrapper.appendChild(input)
wrapper.appendChild(btn)
return wrapper
}
//#endregion
//#region event handlers
async function onCopyBtnClick(evt) {
const success = await copyTextToClipboard(evt.target.dataset.clipboardContent)
if (success) {
evt.target.classList.add("success")
setTimeout(() => {
evt.target.classList.remove("success")
}, 1000);
}
}
function onSearchInput(immediate = false) {
if (filterDebounce) clearTimeout(filterDebounce)
if (immediate) filterEmotes()
else filterDebounce = setTimeout(filterEmotes, filterDebounceDuration)
}
searchForm.addEventListener("input", () => onSearchInput())
searchForm.addEventListener("reset", () => onSearchInput())
function fallbackCopyTextToClipboard(text) {
const textArea = document.createElement("textarea")
textArea.value = text
// Avoid scrolling to bottom
textArea.style.top = "0"
textArea.style.left = "0"
textArea.style.position = "fixed"
document.body.appendChild(textArea)
textArea.focus()
textArea.select()
let success = false
try {
success = document.execCommand('copy')
const msg = success ? 'successful' : 'unsuccessful'
} catch (err) {
console.error('Fallback: Unable to copy:', err)
setError("Failed to set clipboard")
}
document.body.removeChild(textArea)
return success
}
async function copyTextToClipboard(text) {
if (!navigator.clipboard) {
return fallbackCopyTextToClipboard(text)
}
return navigator.clipboard.writeText(text).then(
() => true,
err => {
console.error('Async: Could not copy text: ', err)
setError("Failed to set clipboard")
return false
})
}
function setError(msg) {
error.textContent = msg
if (msg) error.parentElement.classList.remove("hidden")
else error.parentElement.classList.add("hidden")
}
clearError.addEventListener("click", () => setError(''))
//#endregion
// Finally, when everything's loaded, load and display the emotes
loadEmotes()