-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add first initial implementation of Emoji search in address ba
(called omnibar)
- Loading branch information
Showing
13 changed files
with
235 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,9 @@ | ||
import * as IconHandler from "/common/modules/IconHandler.js"; | ||
import * as AddonSettings from "/common/modules/AddonSettings/AddonSettings.js"; | ||
|
||
import * as OmniboxSearch from "./modules/OmniboxSearch.js"; | ||
|
||
// init modules | ||
IconHandler.init(); | ||
const emojiSearch = AddonSettings.get("emojiSearch"); | ||
if (emojiSearch) { | ||
OmniboxSearch.init(); | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import * as EmojiInteraction from "/common/modules/EmojiInteraction.js"; | ||
|
||
/** | ||
* Lazy-load the emoji-mart library, . | ||
* | ||
* This consumes some memory (RAM), up-to 10MB, as remount and other things are loaded. | ||
* | ||
* @public | ||
* @returns {void} | ||
*/ | ||
function loadEmojiMart() { | ||
const emojiMartLoader = document.createElement("script"); | ||
emojiMartLoader.setAttribute("async", true); | ||
emojiMartLoader.setAttribute("src", "/common/lib/emoji-mart-embed/dist/emoji-mart.js"); | ||
document.querySelector("head").appendChild(emojiMartLoader); | ||
} | ||
|
||
/** | ||
* Trigger the evaluation for the search for emojis. | ||
* | ||
* @public | ||
* @param {string} text the string the user entered | ||
* @param {function} suggest function to call to add suggestions | ||
* @returns {void} | ||
* @see {@link https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/omnibox/onInputChanged} | ||
*/ | ||
export function triggerOmnixboxSuggestion(text, suggest) { | ||
const suggestions = window.emojiMart.emojiIndex.search(text).map((emoji) => { | ||
return { | ||
description: browser.i18n.getMessage("searchResultDescription", [ | ||
emoji.native, | ||
emoji.name, | ||
emoji.colons | ||
]), | ||
content: emoji.native | ||
}; | ||
}); | ||
|
||
suggest(suggestions); | ||
} | ||
|
||
/** | ||
* Triggered when the search is actually executed,. | ||
* | ||
* @public | ||
* @param {string} text the string the user entered or selected | ||
* @param {string} disposition how the result should be possible | ||
* @returns {void} | ||
* @see {@link https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/omnibox/onInputEntered} | ||
*/ | ||
export function triggerOmnixboxSearch(text) { | ||
const searchResult = window.emojiMart.emojiIndex.search(text); | ||
|
||
// if a single emoji is selected or searched for, detect this and return | ||
// emoji data | ||
try { | ||
searchResult.push( | ||
window.emojiMart.getEmojiDataFromNative(text) | ||
); | ||
} catch (e) { | ||
// ignore errors, as we usually expect text strings there and these are | ||
// totally fine, too; search may find something here | ||
} | ||
|
||
// emoji itself copied or found | ||
if (searchResult.length === 1) { | ||
// if result is only one emoji, also instantly copy it | ||
EmojiInteraction.copyEmoji(searchResult[0]); | ||
} else { | ||
// otherwise open popup to show all emoji choices | ||
browser.browserAction.openPopup(); | ||
} | ||
|
||
} | ||
|
||
/** | ||
* Init omnibox search. | ||
* | ||
* @public | ||
* @returns {void} | ||
*/ | ||
export function init() { | ||
// lazy-load emoji-mart | ||
loadEmojiMart(); | ||
|
||
browser.omnibox.onInputChanged.addListener(triggerOmnixboxSuggestion); | ||
browser.omnibox.onInputEntered.addListener(triggerOmnixboxSearch); | ||
|
||
browser.omnibox.setDefaultSuggestion({ | ||
description: browser.i18n.getMessage("searchTipDescription", [ | ||
browser.i18n.getMessage("extensionName") | ||
]) | ||
}); | ||
} |
Submodule emoji-mart-embed
updated
from 000000 to eb65bd
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import * as AddonSettings from "/common/modules/AddonSettings/AddonSettings.js"; | ||
|
||
const POPUP_ICON_OPTION = "popupIconColored"; | ||
|
||
/** | ||
* Sets a popup icon variant. | ||
* | ||
* @private | ||
* @param {string} icon version or "null"/"undefined" to reset to default | ||
* @returns {Promise} | ||
*/ | ||
function setPopupIcon(icon) { | ||
// verify parameter | ||
switch (icon) { | ||
case "dark": // fall through | ||
case "light": | ||
case "colored": | ||
case null: | ||
// ok | ||
break; | ||
default: | ||
throw new TypeError(`invalid parameter: ${icon}`); | ||
} | ||
|
||
// ignore request if API is not available | ||
if (browser.browserAction.setIcon === undefined) { | ||
return Promise.resolve(); | ||
} | ||
|
||
if (icon === null || icon === undefined) { | ||
return browser.browserAction.setIcon({path: null}); | ||
} | ||
|
||
// set colored icon | ||
if (icon === "colored") { | ||
// WTF: For whatever reason, these paths need to be absolute... | ||
return browser.browserAction.setIcon({path: { | ||
"16": "/icons/icon_32.png", | ||
"32": "/icons/icon_32.png", | ||
"64": "/icons/icon_64.png", | ||
"128": "/icons/icon_128.png" | ||
}}); | ||
} | ||
|
||
return browser.browserAction.setIcon({path: `/icons/fa-grin-${icon}.svg`}); | ||
} | ||
|
||
/** | ||
* Set icon depending on whether it should be colored, or not. | ||
* | ||
* @public | ||
* @param {boolean} popupIconColored if popupIconColored is colored or not | ||
* @returns {Promise} | ||
*/ | ||
export function changeIconIfColored(popupIconColored) { | ||
if (popupIconColored === true) { | ||
return setPopupIcon("colored"); | ||
} else { | ||
// reset icon | ||
return setPopupIcon(null); | ||
} | ||
} | ||
|
||
/** | ||
* Init icon module. | ||
* | ||
* @public | ||
* @returns {void} | ||
*/ | ||
export function init() { | ||
return AddonSettings.get(POPUP_ICON_OPTION).then((popupIconColored) => changeIconIfColored(popupIconColored)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,22 @@ | ||
import * as AddonSettings from "/common/modules/AddonSettings/AddonSettings.js"; | ||
|
||
const POPUP_ICON_OPTION = "popupIconColored"; | ||
|
||
/** | ||
* Sets a popup icon variant. | ||
* Copy the Emoji to clipboard, once it has been selected. | ||
* | ||
* @private | ||
* @param {string} icon version or "null"/"undefined" to reset to default | ||
* @returns {Promise} | ||
* @param {Object} emoji | ||
* @returns {void} | ||
*/ | ||
function setPopupIcon(icon) { | ||
// verify parameter | ||
switch (icon) { | ||
case "dark": // fall through | ||
case "light": | ||
case "colored": | ||
case null: | ||
// ok | ||
export async function copyEmoji(emoji) { | ||
const emojiCopyOption = await AddonSettings.get("copyEmoji"); | ||
switch (emojiCopyOption) { | ||
case "native": | ||
navigator.clipboard.writeText(emoji.native); | ||
break; | ||
case "colons": | ||
navigator.clipboard.writeText(emoji.colons); | ||
break; | ||
default: | ||
throw new TypeError(`invalid parameter: ${icon}`); | ||
} | ||
|
||
// ignore request if API is not available | ||
if (browser.browserAction.setIcon === undefined) { | ||
return Promise.resolve(); | ||
} | ||
|
||
if (icon === null || icon === undefined) { | ||
return browser.browserAction.setIcon({path: null}); | ||
} | ||
|
||
// set colored icon | ||
if (icon === "colored") { | ||
// WTF: For whatever reason, these paths need to be absolute... | ||
return browser.browserAction.setIcon({path: { | ||
"16": "/icons/icon_32.png", | ||
"32": "/icons/icon_32.png", | ||
"64": "/icons/icon_64.png", | ||
"128": "/icons/icon_128.png" | ||
}}); | ||
} | ||
|
||
return browser.browserAction.setIcon({path: `/icons/fa-grin-${icon}.svg`}); | ||
} | ||
|
||
/** | ||
* Set icon depending on whether it should be colored, or not. | ||
* | ||
* @public | ||
* @param {boolean} popupIconColored if popupIconColored is colored or not | ||
* @returns {Promise} | ||
*/ | ||
export function changeIconIfColored(popupIconColored) { | ||
if (popupIconColored === true) { | ||
return setPopupIcon("colored"); | ||
} else { | ||
// reset icon | ||
return setPopupIcon(null); | ||
throw new Error("invalid option:", "copyEmoji", emojiCopyOption); | ||
} | ||
} | ||
|
||
/** | ||
* Init icon module. | ||
* | ||
* @public | ||
* @returns {void} | ||
*/ | ||
export function init() { | ||
return AddonSettings.get(POPUP_ICON_OPTION).then((popupIconColored) => changeIconIfColored(popupIconColored)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.