diff options
| author | Henry Jameson <me@hjkos.com> | 2022-10-09 18:51:42 +0300 |
|---|---|---|
| committer | Henry Jameson <me@hjkos.com> | 2022-10-09 18:51:42 +0300 |
| commit | 5fa533fbb7b3d507c0136044fd51db033c25db05 (patch) | |
| tree | fd7d50093d2bc44884cf76244d3becc2b3413c0e /src/components/emoji_input/suggestor.js | |
| parent | 0331e42ee8576893dab2c6319ee5c0d62895b8d8 (diff) | |
| parent | c807254d3eadbd57de292360c5200358261cbc0f (diff) | |
Merge remote-tracking branch 'origin/develop' into scrolltotop
* origin/develop: (89 commits)
Update dependency @vuelidate/validators to v2.0.0
Remove lolex package
Remove diff package
Pin dependencies
Update dependency sass to v1.55.0
Make suggestor suggest according to cldr annotations
Make chunks named
Use import() for emoji.json
Add regional indicators
Support filtering by keywords from cldr
Display localized unicode emoji names
Load unicode emoji annotations
Extract language list to its own file
using the half-shit approach since proper approach is full-shit
Make unicode emoji phrases match with _
Use console.info
Fix non-square emojis being truncated
Fix emoji picker lint
Fix emoji picker lint
Tweak efficiency when changing filter keywords in emoji picker
...
Diffstat (limited to 'src/components/emoji_input/suggestor.js')
| -rw-r--r-- | src/components/emoji_input/suggestor.js | 34 |
1 files changed, 17 insertions, 17 deletions
diff --git a/src/components/emoji_input/suggestor.js b/src/components/emoji_input/suggestor.js index 0ddb4d68..adaa879e 100644 --- a/src/components/emoji_input/suggestor.js +++ b/src/components/emoji_input/suggestor.js @@ -2,7 +2,7 @@ * suggest - generates a suggestor function to be used by emoji-input * data: object providing source information for specific types of suggestions: * data.emoji - optional, an array of all emoji available i.e. - * (state.instance.emoji + state.instance.customEmoji) + * (getters.standardEmojiList + state.instance.customEmoji) * data.users - optional, an array of all known users * updateUsersList - optional, a function to search and append to users * @@ -13,10 +13,10 @@ export default data => { const emojiCurry = suggestEmoji(data.emoji) const usersCurry = data.store && suggestUsers(data.store) - return input => { + return (input, nameKeywordLocalizer) => { const firstChar = input[0] if (firstChar === ':' && data.emoji) { - return emojiCurry(input) + return emojiCurry(input, nameKeywordLocalizer) } if (firstChar === '@' && usersCurry) { return usersCurry(input) @@ -25,34 +25,34 @@ export default data => { } } -export const suggestEmoji = emojis => input => { +export const suggestEmoji = emojis => (input, nameKeywordLocalizer) => { const noPrefix = input.toLowerCase().substr(1) return emojis - .filter(({ displayText }) => displayText.toLowerCase().match(noPrefix)) - .sort((a, b) => { - let aScore = 0 - let bScore = 0 + .map(emoji => ({ ...emoji, ...nameKeywordLocalizer(emoji) })) + .filter((emoji) => (emoji.names.concat(emoji.keywords)).filter(kw => kw.toLowerCase().match(noPrefix)).length) + .map(k => { + let score = 0 // An exact match always wins - aScore += a.displayText.toLowerCase() === noPrefix ? 200 : 0 - bScore += b.displayText.toLowerCase() === noPrefix ? 200 : 0 + score += Math.max(...k.names.map(name => name.toLowerCase() === noPrefix ? 200 : 0), 0) // Prioritize custom emoji a lot - aScore += a.imageUrl ? 100 : 0 - bScore += b.imageUrl ? 100 : 0 + score += k.imageUrl ? 100 : 0 // Prioritize prefix matches somewhat - aScore += a.displayText.toLowerCase().startsWith(noPrefix) ? 10 : 0 - bScore += b.displayText.toLowerCase().startsWith(noPrefix) ? 10 : 0 + score += Math.max(...k.names.map(kw => kw.toLowerCase().startsWith(noPrefix) ? 10 : 0), 0) // Sort by length - aScore -= a.displayText.length - bScore -= b.displayText.length + score -= k.displayText.length + k.score = score + return k + }) + .sort((a, b) => { // Break ties alphabetically const alphabetically = a.displayText > b.displayText ? 0.5 : -0.5 - return bScore - aScore + alphabetically + return b.score - a.score + alphabetically }) } |
