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

Commit

Permalink
Move all references to the emoji delimiter character to reference a c…
Browse files Browse the repository at this point in the history
…onstant.

Signed-off-by: Ryan Browne <code@commonlawfeature.com>
  • Loading branch information
Ryan Browne committed Mar 27, 2022
1 parent 02566d6 commit ac09e71
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/autocomplete/EmojiProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,13 @@ import { TimelineRenderingType } from '../contexts/RoomContext';

const LIMIT = 20;

// The delimiter used to start and end emoji shortcodes.
const EMOJI_DELIMITER = ':';

// Match for ascii-style ";-)" emoticons or ":wink:" shortcodes provided by emojibase
// anchored to only match from the start of parts otherwise it'll show emoji suggestions whilst typing matrix IDs
const EMOJI_REGEX = new RegExp('(' + EMOTICON_REGEX.source + '|(?:^|\\s):[+-\\w]*:?)$', 'g');
const EMOJI_SHORTNAME_REGEX = `(?${EMOJI_DELIMITER}^|\\s)${EMOJI_DELIMITER}[+-\\w]*${EMOJI_DELIMITER}?`;
const EMOJI_REGEX = new RegExp('(' + EMOTICON_REGEX.source + '|' + EMOJI_SHORTNAME_REGEX + ')$', 'g');

interface ISortedEmoji {
emoji: IEmoji;
Expand All @@ -63,14 +67,14 @@ function score(query, space) {
}
}

function colonsTrimmed(string: string): string {
function delimiterTrimmed(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] === ':') {
if (string[0] === EMOJI_DELIMITER) {
returned = returned.substring(1);
}
if (returned[returned.length - 1] === ':') {
if (returned[returned.length - 1] === EMOJI_DELIMITER) {
returned = returned.slice(0, -1);
}
return returned;
Expand All @@ -84,7 +88,7 @@ export default class EmojiProvider extends AutocompleteProvider {
super({ commandRegex: EMOJI_REGEX, renderingType });
this.matcher = new QueryMatcher<ISortedEmoji>(SORTED_EMOJI, {
keys: [],
funcs: [o => o.emoji.shortcodes.map(s => `:${s}:`)],
funcs: [o => o.emoji.shortcodes.map(s => `${EMOJI_DELIMITER}${s}${EMOJI_DELIMITER}`)],
// For matching against ascii equivalents
shouldMatchWordsOnly: false,
});
Expand Down Expand Up @@ -121,9 +125,9 @@ export default class EmojiProvider extends AutocompleteProvider {

// then sort by score (Infinity if matchedString not in shortcode)
sorters.push(c => score(matchedString, c.emoji.shortcodes[0]));
// then sort by max score of all shortcodes, trim off the `:`
// then sort by max score of all shortcodes, trim off the `EMOJI_DELIMITER`
sorters.push(c => Math.min(
...c.emoji.shortcodes.map(s => score(colonsTrimmed(matchedString), s)),
...c.emoji.shortcodes.map(s => score(delimiterTrimmed(matchedString), s)),
));
// If the matchedString is not empty, sort by length of shortcode. Example:
// matchedString = ":bookmark"
Expand All @@ -138,7 +142,7 @@ export default class EmojiProvider extends AutocompleteProvider {
completions = completions.map(c => ({
completion: c.emoji.unicode,
component: (
<PillCompletion title={`:${c.emoji.shortcodes[0]}:`} aria-label={c.emoji.unicode}>
<PillCompletion title={`${EMOJI_DELIMITER}${c.emoji.shortcodes[0]}${EMOJI_DELIMITER}`} aria-label={c.emoji.unicode}>
<span>{ c.emoji.unicode }</span>
</PillCompletion>
),
Expand Down

0 comments on commit ac09e71

Please sign in to comment.