2022-09-06 19:26:43 -04:00
|
|
|
let g_emojis;
|
|
|
|
|
|
|
|
function get_emojis() {
|
|
|
|
if (g_emojis) {
|
|
|
|
return Promise.resolve(g_emojis);
|
|
|
|
}
|
2024-02-23 04:35:39 -05:00
|
|
|
return fetch('emojis.json').then(function(result) {
|
2022-09-06 19:26:43 -04:00
|
|
|
g_emojis = result.json();
|
|
|
|
return g_emojis;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function picker(callback, anchor) {
|
2024-02-23 04:35:39 -05:00
|
|
|
get_emojis().then(function(json) {
|
2022-09-06 19:26:43 -04:00
|
|
|
let div = document.createElement('div');
|
|
|
|
div.id = 'emoji_picker';
|
|
|
|
div.style.color = '#000';
|
|
|
|
div.style.background = '#fff';
|
|
|
|
div.style.border = '1px solid #000';
|
|
|
|
div.style.display = 'block';
|
|
|
|
div.style.position = 'absolute';
|
2023-01-28 14:39:41 -05:00
|
|
|
div.style.minWidth = 'min(16em, 90vw)';
|
|
|
|
div.style.width = 'min(16em, 90vw)';
|
|
|
|
div.style.maxWidth = 'min(16em, 90vw)';
|
2022-09-06 19:26:43 -04:00
|
|
|
div.style.maxHeight = '16em';
|
|
|
|
div.style.overflow = 'scroll';
|
|
|
|
div.style.fontWeight = 'bold';
|
2023-01-14 17:27:35 -05:00
|
|
|
div.style.fontSize = 'xx-large';
|
2022-09-06 19:26:43 -04:00
|
|
|
let input = document.createElement('input');
|
|
|
|
input.type = 'text';
|
2023-01-14 17:27:35 -05:00
|
|
|
input.style.display = 'block';
|
|
|
|
input.style.boxSizing = 'border-box';
|
|
|
|
input.style.width = '100%';
|
|
|
|
input.style.margin = '0';
|
|
|
|
input.style.position = 'relative';
|
2022-09-06 19:26:43 -04:00
|
|
|
div.appendChild(input);
|
|
|
|
let list = document.createElement('div');
|
|
|
|
div.appendChild(list);
|
2024-02-23 04:35:39 -05:00
|
|
|
div.addEventListener('mousedown', function(event) {
|
2023-01-28 14:39:41 -05:00
|
|
|
event.stopPropagation();
|
|
|
|
});
|
2023-01-14 17:27:35 -05:00
|
|
|
|
|
|
|
function cleanup() {
|
2023-01-28 14:39:41 -05:00
|
|
|
console.log('emoji cleanup');
|
2023-01-14 17:27:35 -05:00
|
|
|
div.parentElement.removeChild(div);
|
|
|
|
window.removeEventListener('keydown', key_down);
|
2023-01-28 14:39:41 -05:00
|
|
|
console.log('removing click');
|
|
|
|
document.body.removeEventListener('mousedown', cleanup);
|
2023-01-14 17:27:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function key_down(event) {
|
|
|
|
if (event.key == 'Escape') {
|
|
|
|
cleanup();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-03 20:32:50 -04:00
|
|
|
function chosen(event) {
|
|
|
|
console.log(event.srcElement.innerText);
|
|
|
|
callback(event.srcElement.innerText);
|
|
|
|
cleanup();
|
|
|
|
}
|
|
|
|
|
2022-09-06 19:26:43 -04:00
|
|
|
function refresh() {
|
|
|
|
while (list.firstChild) {
|
|
|
|
list.removeChild(list.firstChild);
|
|
|
|
}
|
2023-09-06 18:35:20 -04:00
|
|
|
let search = input.value.toLowerCase();
|
2023-01-14 17:27:35 -05:00
|
|
|
let any_at_all = false;
|
2023-05-03 20:32:50 -04:00
|
|
|
for (let row of Object.entries(json)) {
|
2022-09-06 19:26:43 -04:00
|
|
|
let header = document.createElement('div');
|
|
|
|
header.appendChild(document.createTextNode(row[0]));
|
|
|
|
list.appendChild(header);
|
|
|
|
let any = false;
|
2023-05-03 20:32:50 -04:00
|
|
|
for (let entry of Object.entries(row[1])) {
|
2024-02-23 04:35:39 -05:00
|
|
|
if (search &&
|
2022-09-06 19:26:43 -04:00
|
|
|
search.length &&
|
2024-02-23 04:35:39 -05:00
|
|
|
entry[0].toLowerCase().indexOf(search) == -1) {
|
2022-09-06 19:26:43 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let emoji = document.createElement('span');
|
|
|
|
const k_size = '1.25em';
|
|
|
|
emoji.style.display = 'inline-block';
|
|
|
|
emoji.style.overflow = 'hidden';
|
|
|
|
emoji.style.cursor = 'pointer';
|
2023-05-03 20:32:50 -04:00
|
|
|
emoji.onclick = chosen;
|
|
|
|
emoji.title = entry[0];
|
|
|
|
emoji.appendChild(document.createTextNode(entry[1]));
|
2022-09-06 19:26:43 -04:00
|
|
|
list.appendChild(emoji);
|
|
|
|
any = true;
|
2023-01-14 17:27:35 -05:00
|
|
|
any_at_all = true;
|
2022-09-06 19:26:43 -04:00
|
|
|
}
|
|
|
|
if (!any) {
|
|
|
|
list.removeChild(header);
|
|
|
|
}
|
2023-05-03 20:32:50 -04:00
|
|
|
}
|
2023-01-14 17:27:35 -05:00
|
|
|
if (!any_at_all) {
|
|
|
|
list.appendChild(document.createTextNode('No matches found.'));
|
|
|
|
}
|
2022-09-06 19:26:43 -04:00
|
|
|
}
|
|
|
|
refresh();
|
|
|
|
input.oninput = refresh;
|
|
|
|
document.body.appendChild(div);
|
|
|
|
div.style.position = 'fixed';
|
|
|
|
div.style.top = '50%';
|
|
|
|
div.style.left = '50%';
|
|
|
|
div.style.transform = 'translate(-50%, -50%)';
|
2023-01-14 17:27:35 -05:00
|
|
|
input.focus();
|
2023-01-28 14:39:41 -05:00
|
|
|
console.log('adding click');
|
|
|
|
document.body.addEventListener('mousedown', cleanup);
|
2023-01-14 17:27:35 -05:00
|
|
|
window.addEventListener('keydown', key_down);
|
2022-09-06 19:26:43 -04:00
|
|
|
});
|
2024-02-23 04:35:39 -05:00
|
|
|
}
|