Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
Trim trailing : when checking for autocompletes for emoji.
Browse files Browse the repository at this point in the history
Closes element-hq/element-web#19302

Signed-off-by: Ryan Browne <code@commonlawfeature.com>
  • Loading branch information
Ryan Browne committed Mar 27, 2022
1 parent 837f916 commit 02566d6
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/autocomplete/EmojiProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Copyright 2016 Aviral Dasgupta
Copyright 2017 Vector Creations Ltd
Copyright 2017, 2018 New Vector Ltd
Copyright 2019 The Matrix.org Foundation C.I.C.
Copyright 2022 Ryan Browne <code@commonlawfeature.com>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -62,6 +63,19 @@ function score(query, space) {
}
}

function colonsTrimmed(string: string): string {
// Trim off leading and potentially trailing `:` to correctly
// match the emoji data as they exist in emojibase.
let returned = string;
if (string[0] === ':') {
returned = returned.substring(1);
}
if (returned[returned.length - 1] === ':') {
returned = returned.slice(0, -1);
}
return returned;
}

export default class EmojiProvider extends AutocompleteProvider {
matcher: QueryMatcher<ISortedEmoji>;
nameMatcher: QueryMatcher<ISortedEmoji>;
Expand Down Expand Up @@ -109,7 +123,7 @@ export default class EmojiProvider extends AutocompleteProvider {
sorters.push(c => score(matchedString, c.emoji.shortcodes[0]));
// then sort by max score of all shortcodes, trim off the `:`
sorters.push(c => Math.min(
...c.emoji.shortcodes.map(s => score(matchedString.substring(1), s)),
...c.emoji.shortcodes.map(s => score(colonsTrimmed(matchedString), s)),
));
// If the matchedString is not empty, sort by length of shortcode. Example:
// matchedString = ":bookmark"
Expand Down

0 comments on commit 02566d6

Please sign in to comment.