Skip to content

Commit

Permalink
Merge pull request #10 from tomershvueli/recents
Browse files Browse the repository at this point in the history
Add recently used emoji feature, search through emoji names for better search results and enter selects first emoji in list
  • Loading branch information
virejdasani committed Jul 20, 2021
2 parents e516222 + fff3c68 commit 9ca9aac
Show file tree
Hide file tree
Showing 14 changed files with 23,536 additions and 23,138 deletions.
51 changes: 51 additions & 0 deletions app/macos/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ const {
Menu,
ipcMain,
} = require("electron");
const { LRUMap } = require("lru_map");

// Create store to save user's recents
const Store = require('electron-store');
const store = new Store();

const robot = require("robotjs");

Expand All @@ -16,9 +21,22 @@ const path = require("path");

const assetsDirectory = path.join(__dirname, "assets");

// JSON list of emojis
const emojis = require("./src/emojis");

let tray = undefined;
let window = undefined;

// Let's fetch our previous LRU Map, or set it
let lruMap;
if (store.has("lruMap")) {
lruMap = new LRUMap(10, store.get("lruMap").map(it => {
return [it.key, it.value];
}));
} else {
lruMap = new LRUMap(10);
}

// Hide the menu and dev tools (for production build)
Menu.setApplicationMenu(null);

Expand Down Expand Up @@ -105,6 +123,39 @@ ipcMain.on("typeEmoji", (_event, arg) => {
robot.typeString(arg);
});

// Return filtered and sorted emojis based on a search query
ipcMain.handle("getEmojisForSearchString", (_event, arg) => {
const recents = Array.from(lruMap.keys());

// For each emoji in the emojis.js file, this will search
// through emoji.keywords and emoji.name (from emojis.js) if it contains the word from the user input
return emojis
.filter((item) => item.keywords.includes(arg) || item.name.toLowerCase().includes(arg))
.sort((a, b) => {
if (lruMap.has(a.char) && !lruMap.has(b.char)) {
// A is in recently used and B is not
return -1;
} else if (!lruMap.has(a.char) && lruMap.has(b.char)) {
// B is in recently used and A is not
return 1;
} else if (!lruMap.has(a.char) && !lruMap.has(b.char)) {
// Neither A nor B is in recently used
return a.no - b.no;
} else {
// Both A and B are in recently used
return recents.indexOf(b.char) - recents.indexOf(a.char);
}
})
});

// When we get a signal to select an emoji, update our LRU Map
ipcMain.on("selectEmoji", (_event, arg) => {
lruMap.set(arg, "");

// Save our current lruMap's JSON representation to the store
store.set("lruMap", lruMap.toJSON());
});

const toggleWindow = () => {
if (window.isVisible()) {
hideWindow();
Expand Down

0 comments on commit 9ca9aac

Please sign in to comment.