Skip to content

Commit

Permalink
Refactored emoji search and implemented maoschanz#119
Browse files Browse the repository at this point in the history
  • Loading branch information
DannyGJdeJong committed Jun 29, 2020
1 parent f67019d commit 1f6e946
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 24 deletions.
33 changes: 13 additions & 20 deletions emoji-selector@maestroschan.fr/emojiCategory.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,17 @@ var EmojiCategory = class EmojiCategory {
this.emojiButtons = [];
}

searchEmoji(searchedText, neededresults, priority) {
searchEmoji(searchedText, results, recentlyUsed, neededresults, priority) {
let searchResults = [];
for (let i = 0; i < this.emojiButtons.length; i++) {
if (results.includes(this.emojiButtons[i].baseCharacter)) {
continue;
}
if (neededresults >= 0) {
let isMatching = false;
if (priority === 3) {
if (priority === 4) {
isMatching = this._searchRecentMatch(searchedText, i, recentlyUsed);
} else if (priority === 3) {
isMatching = this._searchExactMatch(searchedText, i);
} else if (priority === 2) {
isMatching = this._searchInName(searchedText, i);
Expand All @@ -126,31 +131,20 @@ var EmojiCategory = class EmojiCategory {
return searchResults
}

_searchRecentMatch(searchedText, i, recentlyUsed) {
return recentlyUsed.includes(this.emojiButtons[i].baseCharacter) && (this._searchExactMatch(searchedText, i) || this._searchInName(searchedText, i) || this._searchInKeywords(searchedText, i))
}

_searchExactMatch(searchedText, i) {
return this.emojiButtons[i].keywords[0] === searchedText;
}

_searchInName(searchedText, i) {
if (this.emojiButtons[i].keywords[0].includes(searchedText)) {
// If the name corresponds to the searched string, but it is also an
// exact match, we can assume the emoji is already in the displayed
// result.
return !this._searchExactMatch(searchedText, i);
}
return false;
return this.emojiButtons[i].keywords[0].includes(searchedText);
}

_searchInKeywords(searchedText, i) {
for (let k = 1; k < this.emojiButtons[i].keywords.length; k++) {
if (this.emojiButtons[i].keywords[k].includes(searchedText)) {
// If a keyword corresponds to the searched string, but the name
// corresponds too, we can assume the emoji is already in the
// displayed result.
return !( this._searchExactMatch(searchedText, i)
|| this._searchInName(searchedText, i) );
}
}
return false;
return this.emojiButtons[i].keywords.some((e) => e.includes(searchedText));
}

/**
Expand Down Expand Up @@ -216,4 +210,3 @@ var EmojiCategory = class EmojiCategory {
return this.categoryButton;
}
}

9 changes: 5 additions & 4 deletions emoji-selector@maestroschan.fr/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,9 @@ class EmojiSearchItem {
}

let results = [];
// First, search for an exact match with emoji names
// First, search for any match (exact, name or keyword) in recent emojis
results = this._getResults(searchedText, minCat, maxCat, recents, results, 4);
// Then, search for an exact match with emoji names
results = this._getResults(searchedText, minCat, maxCat, recents, results, 3);
// Then, search only across emoji names
results = this._getResults(searchedText, minCat, maxCat, recents, results, 2);
Expand Down Expand Up @@ -199,7 +201,7 @@ class EmojiSearchItem {
let availableSlots = recents.length - results.length;
if (availableSlots > 0) {
let catResults = GLOBAL_BUTTON.emojiCategories[cat].searchEmoji(
searchedText, availableSlots, priority
searchedText, results, SETTINGS.get_strv('recently-used'), availableSlots, priority
);
results = results.concat(catResults);
}
Expand Down Expand Up @@ -243,7 +245,7 @@ class EmojisMenu {

//creating the search entry
this.searchItem = new EmojiSearchItem();

//initializing the "recently used" buttons
let recentlyUsed = this._recentlyUsedInit();

Expand Down Expand Up @@ -494,4 +496,3 @@ function disable() {
}

//------------------------------------------------------------------------------

0 comments on commit 1f6e946

Please sign in to comment.