Skip to content

Commit

Permalink
feat: improve emoji autocomplete (#3923)
Browse files Browse the repository at this point in the history
* feat: improve emoji autocomplete
* chore: improve dropdown header
  • Loading branch information
SychO9 authored Nov 11, 2023
1 parent 60ffa78 commit 1c0e093
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 34 deletions.
6 changes: 0 additions & 6 deletions extensions/emoji/js/.prettierrc.json

This file was deleted.

44 changes: 30 additions & 14 deletions extensions/emoji/js/src/forum/addComposerAutocomplete.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { extend } from 'flarum/common/extend';
import TextEditorButton from 'flarum/common/components/TextEditorButton';
import KeyboardNavigatable from 'flarum/common/utils/KeyboardNavigatable';
import Tooltip from 'flarum/common/components/Tooltip';

import AutocompleteDropdown from './fragments/AutocompleteDropdown';
import getEmojiIconCode from './helpers/getEmojiIconCode';
Expand All @@ -11,6 +12,13 @@ export default function addComposerAutocomplete() {

extend('flarum/common/components/TextEditor', 'oninit', function () {
this._loaders.push(async () => await import('./emojiMap').then((m) => (emojiMap = m.default)));
// prettier-ignore
this.commonEmoji = [
'😀', '😁', '😂', '😃', '😄', '😅', '😆', '😇', '😈', '😉', '😊', '😋', '😌', '😍', '😎', '😏', '😐️', '😑', '😒',
'😓', '😔', '😕', '😖', '😗', '😘', '😙', '😚', '😛', '😜', '😝', '😞', '😟', '😠', '😡', '😢', '😣', '😤', '😥',
'😦', '😧', '😨', '😩', '😪', '😫', '😬', '😭', '😮', '😮‍💨', '😯', '😰', '😱', '😲', '😳', '😴', '😵', '😵‍💫',
'😶', '😶‍🌫️', '😷', '😸', '😹', '😺', '😻', '😼', '😽', '😾', '😿', '🙀', '🙁', '🙂', '🙃', '🙄',
];
});

extend('flarum/common/components/TextEditor', 'onbuild', function () {
Expand Down Expand Up @@ -75,16 +83,17 @@ export default function addComposerAutocomplete() {

const makeSuggestion = function ({ emoji, name, code }) {
return (
<button
key={emoji}
onclick={() => applySuggestion(emoji)}
onmouseenter={function () {
this.emojiDropdown.setIndex($(this).parent().index() - 1);
}}
>
<img alt={emoji} className="emoji" draggable="false" loading="lazy" src={`${cdn}72x72/${code}.png`} />
{name}
</button>
<Tooltip text={name}>
<button
key={emoji}
onclick={() => applySuggestion(emoji)}
onmouseenter={function () {
this.emojiDropdown.setIndex($(this).parent().index() - 1);
}}
>
<img alt={emoji} className="emoji" draggable="false" loading="lazy" src={`${cdn}72x72/${code}.png`} title={name} />
</button>
</Tooltip>
);
};

Expand All @@ -98,7 +107,7 @@ export default function addComposerAutocomplete() {
};
const regTyped = fuzzyRegexp(typed);

let maxSuggestions = 7;
let maxSuggestions = 40;

const findMatchingEmojis = (matcher) => {
for (let i = 0; i < emojiKeys.length && maxSuggestions > 0; i++) {
Expand All @@ -107,7 +116,7 @@ export default function addComposerAutocomplete() {
if (similarEmoji.indexOf(curEmoji) === -1) {
const names = emojiMap[curEmoji];
for (let name of names) {
if (matcher(name)) {
if (matcher(name, curEmoji)) {
--maxSuggestions;
similarEmoji.push(curEmoji);
break;
Expand All @@ -118,10 +127,17 @@ export default function addComposerAutocomplete() {
};

// First, try to find all emojis starting with the given string
findMatchingEmojis((emoji) => emoji.indexOf(typed) === 0);
findMatchingEmojis((emojiName, emoji) => {
// If no input is provided yet, match the most common emojis.
if (!typed) {
return this.commonEmoji?.includes(emoji);
}

return emojiName.indexOf(typed) === 0;
});

// If there are still suggestions left, try for some fuzzy matches
findMatchingEmojis((emoji) => regTyped.test(emoji));
findMatchingEmojis((emojiName) => regTyped.test(emojiName));

const suggestions = similarEmoji
.map((emoji) => ({
Expand Down
27 changes: 13 additions & 14 deletions extensions/emoji/less/forum.less
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,28 @@ img.emoji {
.EmojiDropdown {
max-width: 500px;
max-height: 200px;
overflow: auto;
overflow: visible;
position: absolute;
margin: 5px 0 !important;
padding: 8px;

> li > button {
color: var(--text-color);
font-weight: bold;
padding-top: 6px;
padding-bottom: 6px;
padding-left: 45px;
> li {
display: inline-block;

.emoji {
float: left;
margin-left: -30px;
> button {
color: var(--text-color);
font-weight: bold;
padding: 8px;
border-radius: var(--border-radius);
}
}

.Dropdown-header {
> .Dropdown-header {
display: block;
color: var(--muted-more-color);
text-transform: none;
font-weight: normal;
padding-bottom: 5px;
font-size: 11px;
padding: 4px 8px;
margin: 0 0 4px 0;
}
}

Expand Down

0 comments on commit 1c0e093

Please sign in to comment.