Skip to content

Commit

Permalink
Merge branch 'omnibox' of https://github.com/rugk/awesome-emoji-picker
Browse files Browse the repository at this point in the history
…into omnibox
  • Loading branch information
rugk committed Sep 13, 2019
2 parents 59f2e60 + 5b21c0f commit b06ae00
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 47 deletions.
76 changes: 30 additions & 46 deletions src/options/modules/CustomOptionTriggers.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,9 +274,11 @@ function updateEmojiPerLineMaxViaEmojiSize(optionValue, option, event) {
*
* @private
* @param {Object} optionValue
* @param {string} [option]
* @param {Object} [event]
* @returns {Promise}
*/
function applyEmojiSearch(optionValue) {
function applyEmojiSearch(optionValue, option, event = {}) {
// switch status of dependent settings
if (optionValue.enabled) {
document.getElementById("searchCopyAction").disabled = false;
Expand All @@ -294,6 +296,25 @@ function applyEmojiSearch(optionValue) {
toEnable: optionValue.enabled
});

const reloadEmojiSearchStatus = () => {
// get new settings, because they could have been changed
// TODO: generalize in AutomaticSettings
const isEnabled = document.getElementById("omnibarIntegration").checked;

const newOptionValue = {
enabled: isEnabled
};

if (document.getElementById("searchAction").checked) {
newOptionValue.action = document.getElementById("searchAction").value;
} else if (document.getElementById("emojipediaAction").checked) {
newOptionValue.action = document.getElementById("emojipediaAction").value;
}

// we can only all hope, this won't end in an inifnitive loop
applyEmojiSearch(newOptionValue);
}

// request permission from user
if (optionValue.enabled && // only if actually enabled
optionValue.action === "copy" && // if we require a permission for copying
Expand All @@ -302,53 +323,16 @@ function applyEmojiSearch(optionValue) {
return PermissionRequest.requestPermission(
CLIPBOARD_WRITE_PERMISSION,
MESSAGE_EMOJI_COPY_PERMISSION_SEARCH,
event
).then(() => {
// also trigger update when permission is granted
browser.runtime.sendMessage({
type: COMMUNICATION_MESSAGE_TYPE.OMNIBAR_TOGGLE,
toEnable: optionValue.enabled
});
}).catch(async () => {
// only rejects in case of fatal error
// AutomaticSettings.setOption("enabled", lastEmojiSearchSettings, option);

// special handling, if it has been enabled for the first time
if (!lastEmojiSearchSettings.enabled &&
lastEmojiSearchSettings.action === "copy") {
document.getElementById("emojipediaAction").checked = true;

lastEmojiSearchSettings.enabled = true;
lastEmojiSearchSettings.action = "emojipedia";
}

if (!lastEmojiSearchSettings.enabled) {
document.getElementById("omnibarIntegration").checked = false;
}

// revert settings to last state
switch (lastEmojiSearchSettings.action) {
case "copy":
document.getElementById("searchCopyAction").checked = true;
break;
case "emojipedia":
document.getElementById("emojipediaAction").checked = true;
break;
default:
throw new TypeError(`lastEmojiSearchSettings.action has invalid value: ${lastEmojiSearchSettings.action}`);
}
debugger;

// re-apply own stuff (e.g. to get disabled status)
// NOTE: This won't end in an endless loop, because our reversion above
// _must not_ set it to a state where a permission is requested.
await applyEmojiSearch(lastEmojiSearchSettings);

debugger;
// TODO: revert setting to previous state
event,
{retry: true}
).finally(() => {
// Note: Error (rejection) will never happen, because we have infinite retries enabled
// So this is equivalent to a "then".
reloadEmojiSearchStatus();
});
} else {
debugger; // TODO. cancel permission prompt already shown
// debugger;
PermissionRequest.cancelPermissionPrompt(CLIPBOARD_WRITE_PERMISSION);
}

lastEmojiSearchSettings = optionValue;
Expand Down
23 changes: 22 additions & 1 deletion src/options/modules/PermissionRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ export function requestPermission(permissions, messageId, event, options = {}) {
}

// validate parameters
if (options.retry !== false && options.retry < 1) {
if (options.retry !== true && options.retry !== false && options.retry < 1) {
throw new TypeError(`invalid options.retry value of ${options.retry} passed.`);
}

Expand Down Expand Up @@ -371,6 +371,27 @@ export function requestPermission(permissions, messageId, event, options = {}) {
return returnPermission;
}

/**
* Cancels the permission prompt.
*
* Due to technical limitations, it cannot actually close the permission prompt. It can just hide the own
* Thus, if a permission is currently being requested, this may lead to strange side-effects if the permission
* is granted anyway, because the old Promise will still be fullfilled then.
*
* @public
* @param {browser.permissions.Permissions} permissions the permission request to close,
* see https://developer.mozilla.org/docs/Mozilla/Add-ons/WebExtensions/API/permissions/Permissions
* @returns {void}
*/
export function cancelPermissionPrompt(permissions) {
// we cannot actually really close the permission prompt, see:
// https://discourse.mozilla.org/t/can-browser-extension-permission-requests-be-cancelled/44734?u=rugkx

const thisPermission = getInternalPermissionData(permissions);

thisPermission.messageBoxes.forEach(hideMessageBox);
}

/**
* **Syncronously** checks whether the permission has is granted.
*
Expand Down
1 change: 1 addition & 0 deletions src/popup/lib/emoji-mart-embed
Submodule emoji-mart-embed added at 2b7852

0 comments on commit b06ae00

Please sign in to comment.