@@ -27,6 +27,10 @@ import data from 'emoji-mart-vue-fast/data/all.json'
2727
2828const storage = getBuilder ( 'nextcloud-vue' ) . persist ( true ) . build ( )
2929
30+ // Shared emoji index and skinTone for all emojiSearch function calls
31+ // Will be initialized on the first call
32+ let emojiIndex
33+
3034/**
3135 * Skin tones supported by Unicode Emojis (Fitzpatrick scale)
3236 * The numeric values align with `emoji-mart-vue`
@@ -46,17 +50,20 @@ export enum EmojiSkinTone {
4650 * @return {Array } list of found emojis
4751 */
4852export const emojiSearch = ( query : string , maxResults : number = 10 ) => {
49- const index = new EmojiIndex ( data )
53+ // If this is the first call of function - initialize EmojiIndex
54+ if ( ! emojiIndex ) {
55+ emojiIndex = new EmojiIndex ( data )
56+ }
5057 const currentSkinTone = getCurrentSkinTone ( )
5158
5259 let results
5360 if ( query ) {
54- results = index . search ( `:${ query } ` , maxResults )
61+ results = emojiIndex . search ( `:${ query } ` , maxResults )
5562 if ( results . length < maxResults ) {
56- results = results . concat ( index . search ( query , maxResults - results . length ) )
63+ results = results . concat ( emojiIndex . search ( query , maxResults - results . length ) )
5764 }
5865 } else {
59- results = frequently . get ( maxResults ) . map ( ( id : number ) => index . emoji ( id ) ) || [ ]
66+ results = frequently . get ( maxResults ) . map ( ( id : number ) => emojiIndex . emoji ( id ) ) || [ ]
6067 }
6168
6269 return results . map ( ( emoji ) => emoji . getSkin ( currentSkinTone ) )
0 commit comments