forked from AkkomaGang/akkoma-fe
Merge branch 'feature-emoji-length-sorting' into 'develop'
Allow emoji suggestions based on a match anywhere in the emoji name, but improve sorting Closes #135 See merge request pleroma/pleroma-fe!1061
This commit is contained in:
commit
fe16b6259e
1 changed files with 18 additions and 6 deletions
|
@ -29,17 +29,29 @@ export default data => input => {
|
||||||
export const suggestEmoji = emojis => input => {
|
export const suggestEmoji = emojis => input => {
|
||||||
const noPrefix = input.toLowerCase().substr(1)
|
const noPrefix = input.toLowerCase().substr(1)
|
||||||
return emojis
|
return emojis
|
||||||
.filter(({ displayText }) => displayText.toLowerCase().startsWith(noPrefix))
|
.filter(({ displayText }) => displayText.toLowerCase().match(noPrefix))
|
||||||
.sort((a, b) => {
|
.sort((a, b) => {
|
||||||
let aScore = 0
|
let aScore = 0
|
||||||
let bScore = 0
|
let bScore = 0
|
||||||
|
|
||||||
// Make custom emojis a priority
|
// An exact match always wins
|
||||||
aScore += a.imageUrl ? 10 : 0
|
aScore += a.displayText.toLowerCase() === noPrefix ? 200 : 0
|
||||||
bScore += b.imageUrl ? 10 : 0
|
bScore += b.displayText.toLowerCase() === noPrefix ? 200 : 0
|
||||||
|
|
||||||
// Sort alphabetically
|
// Prioritize custom emoji a lot
|
||||||
const alphabetically = a.displayText > b.displayText ? 1 : -1
|
aScore += a.imageUrl ? 100 : 0
|
||||||
|
bScore += b.imageUrl ? 100 : 0
|
||||||
|
|
||||||
|
// Prioritize prefix matches somewhat
|
||||||
|
aScore += a.displayText.toLowerCase().startsWith(noPrefix) ? 10 : 0
|
||||||
|
bScore += b.displayText.toLowerCase().startsWith(noPrefix) ? 10 : 0
|
||||||
|
|
||||||
|
// Sort by length
|
||||||
|
aScore -= a.displayText.length
|
||||||
|
bScore -= b.displayText.length
|
||||||
|
|
||||||
|
// Break ties alphabetically
|
||||||
|
const alphabetically = a.displayText > b.displayText ? 0.5 : -0.5
|
||||||
|
|
||||||
return bScore - aScore + alphabetically
|
return bScore - aScore + alphabetically
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in a new issue