forked from rDrama/rDrama
Improve inline emoji modal (with bugfixes) (#162)
- Fix/modernize keyboard navigation (making selections work and autoscroll) - Allow capital letters to be used in the inline emoji input - Fix first emoji typed not causing the picker to appear \*Fixed bug that broke emoji pickers Reviewed-on: rDrama/rDrama#162 Co-authored-by: KindaCrayCray <kindacraycray@noreply.fsdfsd.net> Co-committed-by: KindaCrayCray <kindacraycray@noreply.fsdfsd.net>master
parent
f0cfa8c916
commit
fedb366b6b
|
@ -15,7 +15,13 @@ Copyright (C) 2022 Dr Steven Transmisia, anti-evil engineer,
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// Status
|
// Status
|
||||||
let emojiEngineStarted = false;
|
/**
|
||||||
|
* inactive - user has not tried using an emoji
|
||||||
|
* loading - user has tried to use an emoji, and the engine is initializing itself
|
||||||
|
* ready - engine can handle all emoji usage
|
||||||
|
* @type {"inactive"|"loading"|"ready"}
|
||||||
|
*/
|
||||||
|
let emojiEngineState = "inactive";
|
||||||
|
|
||||||
// DOM stuff
|
// DOM stuff
|
||||||
const classesSelectorDOM = document.getElementById("emoji-modal-tabs");
|
const classesSelectorDOM = document.getElementById("emoji-modal-tabs");
|
||||||
|
@ -147,11 +153,13 @@ const emojisSearchDictionary = {
|
||||||
};
|
};
|
||||||
|
|
||||||
// get public emojis list
|
// get public emojis list
|
||||||
const emojiRequest = new XMLHttpRequest();
|
function fetchEmojis() {
|
||||||
emojiRequest.open("GET", '/emojis');
|
const headers = new Headers({xhr: "xhr"})
|
||||||
emojiRequest.setRequestHeader('xhr', 'xhr');
|
return fetch("/emojis", {
|
||||||
emojiRequest.onload = async () => {
|
headers,
|
||||||
let emojis = JSON.parse(emojiRequest.response);
|
})
|
||||||
|
.then(res => res.json())
|
||||||
|
.then(emojis => {
|
||||||
if(! (emojis instanceof Array ))
|
if(! (emojis instanceof Array ))
|
||||||
throw new TypeError("[EMOJI DIALOG] rDrama's server should have sent a JSON-coded Array!");
|
throw new TypeError("[EMOJI DIALOG] rDrama's server should have sent a JSON-coded Array!");
|
||||||
|
|
||||||
|
@ -221,7 +229,7 @@ emojiRequest.onload = async () => {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Show favorite for start.
|
// Show favorite for start.
|
||||||
await classesSelectorDOM.children[0].children[0].click();
|
classesSelectorDOM.children[0].children[0].click();
|
||||||
|
|
||||||
// Send it to the render machine!
|
// Send it to the render machine!
|
||||||
emojiResultsDOM.appendChild(bussyDOM);
|
emojiResultsDOM.appendChild(bussyDOM);
|
||||||
|
@ -229,6 +237,9 @@ emojiRequest.onload = async () => {
|
||||||
emojiResultsDOM.hidden = false;
|
emojiResultsDOM.hidden = false;
|
||||||
emojiWorkingDOM.hidden = true;
|
emojiWorkingDOM.hidden = true;
|
||||||
emojiSearchBarDOM.disabled = false;
|
emojiSearchBarDOM.disabled = false;
|
||||||
|
|
||||||
|
emojiEngineState = "ready";
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -398,7 +409,7 @@ function populate_speed_emoji_modal(results, textbox)
|
||||||
emoji_option.addEventListener('click', () => {
|
emoji_option.addEventListener('click', () => {
|
||||||
selecting = false;
|
selecting = false;
|
||||||
speed_carot_modal.style.display = "none";
|
speed_carot_modal.style.display = "none";
|
||||||
textbox.value = textbox.value.replace(new RegExp(current_word+"(?=\\s|$)", "g"), `:${name}: `)
|
textbox.value = textbox.value.replace(new RegExp(current_word+"(?=\\s|$)", "gi"), `:${name}: `)
|
||||||
textbox.focus()
|
textbox.focus()
|
||||||
if (document.location.pathname != '/chat'){
|
if (document.location.pathname != '/chat'){
|
||||||
markdown(textbox)
|
markdown(textbox)
|
||||||
|
@ -436,13 +447,13 @@ function update_speed_emoji_modal(event)
|
||||||
// Get current word at string, such as ":marse" or "word"
|
// Get current word at string, such as ":marse" or "word"
|
||||||
let coords = text.indexOf(' ',box_coords.pos);
|
let coords = text.indexOf(' ',box_coords.pos);
|
||||||
current_word = /:[!#a-zA-Z0-9_]+(?=\n|$)/.exec(text.slice(0, coords === -1 ? text.length : coords));
|
current_word = /:[!#a-zA-Z0-9_]+(?=\n|$)/.exec(text.slice(0, coords === -1 ? text.length : coords));
|
||||||
if (current_word) current_word = current_word.toString();
|
if (current_word) current_word = current_word[0].toLowerCase();
|
||||||
|
|
||||||
/* We could also check emoji_typing_state here, which is less accurate but more efficient. I've
|
/* We could also check emoji_typing_state here, which is less accurate but more efficient. I've
|
||||||
* kept it unless someone wants to provide an option to toggle it for performance */
|
* kept it unless someone wants to provide an option to toggle it for performance */
|
||||||
if (curr_word_is_emoji() && current_word != ":")
|
if (curr_word_is_emoji() && current_word != ":")
|
||||||
{
|
{
|
||||||
loadEmojis(null);
|
loadEmojis().then( () => {
|
||||||
let modal_pos = event.target.getBoundingClientRect();
|
let modal_pos = event.target.getBoundingClientRect();
|
||||||
modal_pos.x += window.scrollX;
|
modal_pos.x += window.scrollX;
|
||||||
modal_pos.y += window.scrollY;
|
modal_pos.y += window.scrollY;
|
||||||
|
@ -452,51 +463,40 @@ function update_speed_emoji_modal(event)
|
||||||
speed_carot_modal.style.top = modal_pos.y + box_coords.y + 14 + "px";
|
speed_carot_modal.style.top = modal_pos.y + box_coords.y + 14 + "px";
|
||||||
|
|
||||||
// Do the search (and do something with it)
|
// Do the search (and do something with it)
|
||||||
const resultSet = emojisSearchDictionary.completeSearch(current_word.substr(1).replace(/#/g, "").replace(/!/g, ""))
|
const resultSet = emojisSearchDictionary.completeSearch(current_word.substring(1).replace(/[#!]/g, ""));
|
||||||
|
|
||||||
const found = globalEmojis.filter(i => resultSet.has(i.name));
|
const found = globalEmojis.filter(i => resultSet.has(i.name));
|
||||||
|
|
||||||
populate_speed_emoji_modal(found, event.target);
|
populate_speed_emoji_modal(found, event.target);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
speed_carot_modal.style.display = "none";
|
speed_carot_modal.style.display = "none";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function speed_carot_navigate(e)
|
function speed_carot_navigate(event)
|
||||||
{
|
{
|
||||||
if (!selecting) return;
|
if (!selecting) return;
|
||||||
|
|
||||||
let select_items = speed_carot_modal.querySelectorAll(".speed-modal-option");
|
let select_items = speed_carot_modal.querySelectorAll(".speed-modal-option");
|
||||||
if (!select_items || !curr_word_is_emoji()) return;
|
if (!select_items || !curr_word_is_emoji()) return;
|
||||||
// Up or down arrow or enter
|
|
||||||
if (e.keyCode == 38 || e.keyCode == 40 || e.keyCode == 13)
|
|
||||||
{
|
|
||||||
if (emoji_index > select_items.length)
|
|
||||||
emoji_index = select_items;
|
|
||||||
|
|
||||||
select_items[emoji_index].classList.remove("selected");
|
const modal_keybinds = {
|
||||||
switch (e.keyCode)
|
// go up one, wrapping around to the bottom if pressed at the top
|
||||||
{
|
ArrowUp: () => emoji_index = ((emoji_index - 1) + select_items.length) % select_items.length,
|
||||||
case 38: // Up arrow
|
// go down one, wrapping around to the top if pressed at the bottom
|
||||||
if (emoji_index)
|
ArrowDown: () => emoji_index = ((emoji_index + 1) + select_items.length) % select_items.length,
|
||||||
emoji_index--;
|
// select the emoji
|
||||||
break;
|
Enter: () => select_items[emoji_index].click(),
|
||||||
|
|
||||||
case 40: // Down arrow
|
|
||||||
if (emoji_index < select_items.length-1) emoji_index++;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 13:
|
|
||||||
select_items[emoji_index].click();
|
|
||||||
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
if (event.key in modal_keybinds)
|
||||||
|
{
|
||||||
|
select_items[emoji_index].classList.remove("selected");
|
||||||
|
modal_keybinds[event.key]();
|
||||||
select_items[emoji_index].classList.add("selected");
|
select_items[emoji_index].classList.add("selected");
|
||||||
e.preventDefault();
|
select_items[emoji_index].scrollIntoView({inline: "end", block: "nearest"});
|
||||||
|
event.preventDefault();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -516,14 +516,20 @@ function loadEmojis(inputTargetIDName)
|
||||||
selecting = false;
|
selecting = false;
|
||||||
speed_carot_modal.style.display = "none";
|
speed_carot_modal.style.display = "none";
|
||||||
|
|
||||||
if(!emojiEngineStarted)
|
if (inputTargetIDName) emojiInputTargetDOM = document.getElementById(inputTargetIDName);
|
||||||
{
|
|
||||||
emojiEngineStarted = true;
|
|
||||||
emojiRequest.send();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (inputTargetIDName)
|
switch (emojiEngineState) {
|
||||||
emojiInputTargetDOM = document.getElementById(inputTargetIDName);
|
case "inactive":
|
||||||
|
emojiEngineState = "loading"
|
||||||
|
return fetchEmojis();
|
||||||
|
case "loading":
|
||||||
|
// this works because once the fetch completes, the first keystroke callback will fire and use the current value
|
||||||
|
return Promise.reject();
|
||||||
|
case "ready":
|
||||||
|
return Promise.resolve();
|
||||||
|
default:
|
||||||
|
throw Error("Unknown emoji engine state");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
document.getElementById('emojiModal').addEventListener('shown.bs.modal', function () {
|
document.getElementById('emojiModal').addEventListener('shown.bs.modal', function () {
|
||||||
|
|
Loading…
Reference in New Issue