Skip to content

Commit

Permalink
Merge pull request #5554 from nextcloud-libraries/fix/noid/emoji-inde…
Browse files Browse the repository at this point in the history
…x-singleton

fix(emoji): index emoji data once for search functions
  • Loading branch information
susnux authored May 6, 2024
2 parents bb38a7a + 24cb77b commit 902958b
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/functions/emoji/emoji.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ import data from 'emoji-mart-vue-fast/data/all.json'

const storage = getBuilder('nextcloud-vue').persist(true).build()

// Shared emoji index and skinTone for all emojiSearch function calls
// Will be initialized on the first call
let emojiIndex

/**
* Skin tones supported by Unicode Emojis (Fitzpatrick scale)
* The numeric values align with `emoji-mart-vue`
Expand All @@ -46,17 +50,20 @@ export enum EmojiSkinTone {
* @return {Array} list of found emojis
*/
export const emojiSearch = (query: string, maxResults: number = 10) => {
const index = new EmojiIndex(data)
// If this is the first call of function - initialize EmojiIndex
if (!emojiIndex) {
emojiIndex = new EmojiIndex(data)
}
const currentSkinTone = getCurrentSkinTone()

let results
if (query) {
results = index.search(`:${query}`, maxResults)
results = emojiIndex.search(`:${query}`, maxResults)
if (results.length < maxResults) {
results = results.concat(index.search(query, maxResults - results.length))
results = results.concat(emojiIndex.search(query, maxResults - results.length))
}
} else {
results = frequently.get(maxResults).map((id: number) => index.emoji(id)) || []
results = frequently.get(maxResults).map((id: number) => emojiIndex.emoji(id)) || []
}

return results.map((emoji) => emoji.getSkin(currentSkinTone))
Expand Down

0 comments on commit 902958b

Please sign in to comment.