2020-07-01 21:45:18 +02:00
|
|
|
import "./emoji-picker-content.js";
|
2021-06-28 10:55:42 +02:00
|
|
|
import './emoji-dropdown.js';
|
2021-02-22 20:20:19 +01:00
|
|
|
import DOMNavigator from "shared/dom-navigator";
|
2021-05-12 12:14:28 +02:00
|
|
|
import debounce from 'lodash-es/debounce';
|
2021-03-24 11:59:09 +01:00
|
|
|
import { CustomElement } from 'shared/components/element.js';
|
2022-03-09 12:24:26 +01:00
|
|
|
import { KEYCODES } from '@converse/headless/shared/constants.js';
|
2020-12-03 13:40:30 +01:00
|
|
|
import { _converse, api, converse } from "@converse/headless/core";
|
2022-04-06 11:18:12 +02:00
|
|
|
import { getTonedEmojis } from './utils.js';
|
2023-02-15 14:19:48 +01:00
|
|
|
import { tplEmojiPicker } from "./templates/emoji-picker.js";
|
2020-06-23 11:44:22 +02:00
|
|
|
|
2021-03-19 15:05:35 +01:00
|
|
|
import './styles/emoji.scss';
|
2021-03-19 11:28:35 +01:00
|
|
|
|
2020-06-23 11:44:22 +02:00
|
|
|
const u = converse.env.utils;
|
|
|
|
|
|
|
|
|
2020-07-01 21:45:18 +02:00
|
|
|
export default class EmojiPicker extends CustomElement {
|
2020-06-23 11:44:22 +02:00
|
|
|
|
|
|
|
static get properties () {
|
|
|
|
return {
|
|
|
|
'chatview': { type: Object },
|
2020-06-08 12:12:49 +02:00
|
|
|
'current_category': { type: String, 'reflect': true },
|
|
|
|
'current_skintone': { type: String, 'reflect': true },
|
2020-06-23 11:44:22 +02:00
|
|
|
'model': { type: Object },
|
2022-02-09 21:07:03 +01:00
|
|
|
'query': { type: String, 'reflect': true },
|
2020-06-08 12:12:49 +02:00
|
|
|
// This is an optimization, we lazily render the emoji picker, otherwise tests slow to a crawl.
|
|
|
|
'render_emojis': { type: Boolean },
|
2020-06-23 11:44:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-08 12:12:49 +02:00
|
|
|
firstUpdated () {
|
2021-06-28 10:55:42 +02:00
|
|
|
super.firstUpdated();
|
2020-06-08 12:12:49 +02:00
|
|
|
this.listenTo(this.model, 'change', o => this.onModelChanged(o.changed));
|
|
|
|
this.initArrowNavigation();
|
|
|
|
}
|
|
|
|
|
2020-06-23 11:44:22 +02:00
|
|
|
constructor () {
|
|
|
|
super();
|
2022-02-09 21:07:03 +01:00
|
|
|
this.query = '';
|
2020-07-13 09:55:01 +02:00
|
|
|
this._search_results = [];
|
2020-06-25 11:18:13 +02:00
|
|
|
this.debouncedFilter = debounce(input => this.model.set({'query': input.value}), 250);
|
2020-06-23 11:44:22 +02:00
|
|
|
}
|
|
|
|
|
2020-07-13 09:55:01 +02:00
|
|
|
get search_results () {
|
|
|
|
return this._search_results;
|
|
|
|
}
|
|
|
|
|
|
|
|
set search_results (value) {
|
|
|
|
this._search_results = value;
|
|
|
|
this.requestUpdate();
|
|
|
|
}
|
|
|
|
|
2020-06-23 11:44:22 +02:00
|
|
|
render () {
|
2023-02-15 14:19:48 +01:00
|
|
|
return tplEmojiPicker({
|
2020-06-25 11:18:13 +02:00
|
|
|
'chatview': this.chatview,
|
2020-06-23 11:44:22 +02:00
|
|
|
'current_category': this.current_category,
|
|
|
|
'current_skintone': this.current_skintone,
|
2020-06-25 11:18:13 +02:00
|
|
|
'model': this.model,
|
2020-06-23 11:44:22 +02:00
|
|
|
'onCategoryPicked': ev => this.chooseCategory(ev),
|
|
|
|
'onSearchInputBlurred': ev => this.chatview.emitFocused(ev),
|
|
|
|
'onSearchInputFocus': ev => this.onSearchInputFocus(ev),
|
2022-03-09 12:24:26 +01:00
|
|
|
'onSearchInputKeyDown': ev => this.onSearchInputKeyDown(ev),
|
2020-06-23 11:44:22 +02:00
|
|
|
'onSkintonePicked': ev => this.chooseSkinTone(ev),
|
|
|
|
'query': this.query,
|
|
|
|
'search_results': this.search_results,
|
2020-06-08 12:12:49 +02:00
|
|
|
'render_emojis': this.render_emojis,
|
2020-11-26 14:19:30 +01:00
|
|
|
'sn2Emoji': shortname => u.shortnamesToEmojis(this.getTonedShortname(shortname))
|
2020-06-23 11:44:22 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
updated (changed) {
|
2020-07-13 09:55:01 +02:00
|
|
|
changed.has('query') && this.updateSearchResults(changed);
|
2020-06-25 11:18:13 +02:00
|
|
|
changed.has('current_category') && this.setScrollPosition();
|
|
|
|
}
|
|
|
|
|
2020-06-08 12:12:49 +02:00
|
|
|
onModelChanged (changed) {
|
|
|
|
if ('current_category' in changed) this.current_category = changed.current_category;
|
|
|
|
if ('current_skintone' in changed) this.current_skintone = changed.current_skintone;
|
|
|
|
if ('query' in changed) this.query = changed.query;
|
|
|
|
}
|
|
|
|
|
2020-06-25 11:18:13 +02:00
|
|
|
setScrollPosition () {
|
|
|
|
if (this.preserve_scroll) {
|
|
|
|
this.preserve_scroll = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const el = this.querySelector('.emoji-lists__container--browse');
|
|
|
|
const heading = this.querySelector(`#emoji-picker-${this.current_category}`);
|
|
|
|
if (heading) {
|
|
|
|
// +4 due to 2px padding on list elements
|
|
|
|
el.scrollTop = heading.offsetTop - heading.offsetHeight*3 + 4;
|
2020-06-23 11:44:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-13 09:55:01 +02:00
|
|
|
updateSearchResults (changed) {
|
|
|
|
const old_query = changed.get('query');
|
2020-06-23 11:44:22 +02:00
|
|
|
const contains = _converse.FILTER_CONTAINS;
|
|
|
|
if (this.query) {
|
2020-07-13 09:55:01 +02:00
|
|
|
if (this.query === old_query) {
|
2020-06-25 11:18:13 +02:00
|
|
|
return this.search_results;
|
2020-07-13 09:55:01 +02:00
|
|
|
} else if (old_query && this.query.includes(old_query)) {
|
2020-06-25 11:18:13 +02:00
|
|
|
this.search_results = this.search_results.filter(e => contains(e.sn, this.query));
|
2020-06-23 11:44:22 +02:00
|
|
|
} else {
|
2020-06-08 12:12:49 +02:00
|
|
|
this.search_results = converse.emojis.list.filter(e => contains(e.sn, this.query));
|
2020-06-23 11:44:22 +02:00
|
|
|
}
|
2020-06-25 11:18:13 +02:00
|
|
|
} else if (this.search_results.length) {
|
|
|
|
// Avoid re-rendering by only setting to new empty array if it wasn't empty before
|
|
|
|
this.search_results = [];
|
2020-06-23 11:44:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-14 15:07:15 +02:00
|
|
|
registerEvents () {
|
|
|
|
this.onGlobalKeyDown = ev => this._onGlobalKeyDown(ev);
|
|
|
|
const body = document.querySelector('body');
|
|
|
|
body.addEventListener('keydown', this.onGlobalKeyDown);
|
|
|
|
}
|
|
|
|
|
|
|
|
connectedCallback () {
|
|
|
|
super.connectedCallback();
|
|
|
|
this.registerEvents();
|
|
|
|
}
|
|
|
|
|
2020-06-23 11:44:22 +02:00
|
|
|
disconnectedCallback() {
|
|
|
|
const body = document.querySelector('body');
|
|
|
|
body.removeEventListener('keydown', this.onGlobalKeyDown);
|
2022-03-09 12:24:26 +01:00
|
|
|
this.disableArrowNavigation();
|
2021-06-25 14:50:48 +02:00
|
|
|
super.disconnectedCallback();
|
2020-06-23 11:44:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_onGlobalKeyDown (ev) {
|
|
|
|
if (!this.navigator) {
|
|
|
|
return;
|
|
|
|
}
|
2022-03-09 12:24:26 +01:00
|
|
|
if (ev.keyCode === KEYCODES.ENTER && u.isVisible(this)) {
|
2020-06-23 11:44:22 +02:00
|
|
|
this.onEnterPressed(ev);
|
2022-03-09 12:24:26 +01:00
|
|
|
} else if (ev.keyCode === KEYCODES.DOWN_ARROW &&
|
2020-06-23 11:44:22 +02:00
|
|
|
!this.navigator.enabled &&
|
|
|
|
u.isVisible(this)) {
|
|
|
|
this.enableArrowNavigation(ev);
|
2022-03-09 12:24:26 +01:00
|
|
|
} else if (ev.keyCode === KEYCODES.ESCAPE) {
|
|
|
|
this.disableArrowNavigation();
|
|
|
|
setTimeout(() => this.chatview.querySelector('.chat-textarea').focus(), 50);
|
2020-06-23 11:44:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-25 11:18:13 +02:00
|
|
|
setCategoryForElement (el) {
|
2020-06-23 11:44:22 +02:00
|
|
|
const old_category = this.current_category;
|
2020-06-08 12:12:49 +02:00
|
|
|
const category = el?.getAttribute('data-category') || old_category;
|
2020-06-23 11:44:22 +02:00
|
|
|
if (old_category !== category) {
|
|
|
|
this.model.save({'current_category': category});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
insertIntoTextArea (value) {
|
2021-03-04 15:53:04 +01:00
|
|
|
const autocompleting = this.model.get('autocompleting');
|
|
|
|
const ac_position = this.model.get('ac_position');
|
2020-07-13 10:10:58 +02:00
|
|
|
this.model.set({'autocompleting': null, 'query': '', 'ac_position': null});
|
2022-03-09 12:24:26 +01:00
|
|
|
this.disableArrowNavigation();
|
2022-04-04 11:53:50 +02:00
|
|
|
const jid = this.chatview.model.get('jid');
|
2022-03-09 12:24:26 +01:00
|
|
|
const options = {
|
|
|
|
'bubbles': true,
|
2022-04-04 11:53:50 +02:00
|
|
|
'detail': { value, autocompleting, ac_position, jid }
|
2022-03-09 12:24:26 +01:00
|
|
|
};
|
|
|
|
this.dispatchEvent(new CustomEvent("emojiSelected", options));
|
2020-06-23 11:44:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
chooseSkinTone (ev) {
|
|
|
|
ev.preventDefault();
|
|
|
|
ev.stopPropagation();
|
|
|
|
const target = ev.target.nodeName === 'IMG' ? ev.target.parentElement : ev.target;
|
|
|
|
const skintone = target.getAttribute("data-skintone").trim();
|
|
|
|
if (this.current_skintone === skintone) {
|
|
|
|
this.model.save({'current_skintone': ''});
|
|
|
|
} else {
|
|
|
|
this.model.save({'current_skintone': skintone});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
chooseCategory (ev) {
|
|
|
|
ev.preventDefault && ev.preventDefault();
|
|
|
|
ev.stopPropagation && ev.stopPropagation();
|
|
|
|
const el = ev.target.matches('li') ? ev.target : u.ancestor(ev.target, 'li');
|
|
|
|
this.setCategoryForElement(el);
|
|
|
|
this.navigator.select(el);
|
|
|
|
!this.navigator.enabled && this.navigator.enable();
|
|
|
|
}
|
|
|
|
|
2022-03-09 12:24:26 +01:00
|
|
|
onSearchInputKeyDown (ev) {
|
|
|
|
if (ev.keyCode === KEYCODES.TAB) {
|
2020-06-23 11:44:22 +02:00
|
|
|
if (ev.target.value) {
|
|
|
|
ev.preventDefault();
|
2020-10-01 11:13:13 +02:00
|
|
|
const match = converse.emojis.shortnames.find(sn => _converse.FILTER_CONTAINS(sn, ev.target.value));
|
2020-06-23 11:44:22 +02:00
|
|
|
match && this.model.set({'query': match});
|
|
|
|
} else if (!this.navigator.enabled) {
|
|
|
|
this.enableArrowNavigation(ev);
|
|
|
|
}
|
2022-03-09 12:24:26 +01:00
|
|
|
} else if (ev.keyCode === KEYCODES.DOWN_ARROW && !this.navigator.enabled) {
|
2020-06-23 11:44:22 +02:00
|
|
|
this.enableArrowNavigation(ev);
|
|
|
|
} else if (
|
2022-03-09 12:24:26 +01:00
|
|
|
ev.keyCode !== KEYCODES.ENTER &&
|
|
|
|
ev.keyCode !== KEYCODES.DOWN_ARROW
|
2020-06-23 11:44:22 +02:00
|
|
|
) {
|
|
|
|
this.debouncedFilter(ev.target);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onEnterPressed (ev) {
|
|
|
|
ev.preventDefault();
|
|
|
|
ev.stopPropagation();
|
2020-06-08 12:12:49 +02:00
|
|
|
if (converse.emojis.shortnames.includes(ev.target.value)) {
|
2020-06-23 11:44:22 +02:00
|
|
|
this.insertIntoTextArea(ev.target.value);
|
|
|
|
} else if (this.search_results.length === 1) {
|
|
|
|
this.insertIntoTextArea(this.search_results[0].sn);
|
|
|
|
} else if (this.navigator.selected && this.navigator.selected.matches('.insert-emoji')) {
|
|
|
|
this.insertIntoTextArea(this.navigator.selected.getAttribute('data-emoji'));
|
|
|
|
} else if (this.navigator.selected && this.navigator.selected.matches('.emoji-category')) {
|
|
|
|
this.chooseCategory({'target': this.navigator.selected});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onSearchInputFocus (ev) {
|
|
|
|
this.chatview.emitBlurred(ev);
|
|
|
|
this.disableArrowNavigation();
|
|
|
|
}
|
|
|
|
|
|
|
|
getTonedShortname (shortname) {
|
2022-04-06 11:18:12 +02:00
|
|
|
if (getTonedEmojis().includes(shortname) && this.current_skintone) {
|
2020-06-23 11:44:22 +02:00
|
|
|
return `${shortname.slice(0, shortname.length-1)}_${this.current_skintone}:`
|
|
|
|
}
|
|
|
|
return shortname;
|
|
|
|
}
|
|
|
|
|
|
|
|
initArrowNavigation () {
|
|
|
|
if (!this.navigator) {
|
|
|
|
const default_selector = 'li:not(.hidden):not(.emoji-skintone), .emoji-search';
|
|
|
|
const options = {
|
|
|
|
'jump_to_picked': '.emoji-category',
|
|
|
|
'jump_to_picked_selector': '.emoji-category.picked',
|
|
|
|
'jump_to_picked_direction': DOMNavigator.DIRECTION.down,
|
|
|
|
'picked_selector': '.picked',
|
|
|
|
'scroll_container': this.querySelector('.emoji-picker__lists'),
|
|
|
|
'getSelector': direction => {
|
|
|
|
if (direction === DOMNavigator.DIRECTION.down) {
|
|
|
|
const c = this.navigator.selected && this.navigator.selected.getAttribute('data-category');
|
|
|
|
return c ? `ul[data-category="${c}"] li:not(.hidden):not(.emoji-skintone), .emoji-search` : default_selector;
|
|
|
|
} else {
|
|
|
|
return default_selector;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
'onSelected': el => {
|
|
|
|
el.matches('.insert-emoji') && this.setCategoryForElement(el.parentElement);
|
|
|
|
el.matches('.insert-emoji, .emoji-category') && el.firstElementChild.focus();
|
|
|
|
el.matches('.emoji-search') && el.focus();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
this.navigator = new DOMNavigator(this, options);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
disableArrowNavigation () {
|
2022-03-09 12:24:26 +01:00
|
|
|
this.navigator?.disable();
|
2020-06-23 11:44:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
enableArrowNavigation (ev) {
|
2020-07-09 21:14:13 +02:00
|
|
|
ev?.preventDefault?.();
|
|
|
|
ev?.stopPropagation?.();
|
2020-06-23 11:44:22 +02:00
|
|
|
this.disableArrowNavigation();
|
|
|
|
this.navigator.enable();
|
|
|
|
this.navigator.handleKeydown(ev);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-01 21:45:18 +02:00
|
|
|
api.elements.define('converse-emoji-picker', EmojiPicker);
|