From bd9d51308a2fef8eb37d33729e9aa5d134aed74d Mon Sep 17 00:00:00 2001 From: tophf Date: Thu, 24 Feb 2022 06:49:06 +0300 Subject: [PATCH] simplify context menu + shorten titles --- _locales/en/messages.json | 4 +-- background/context-menus.js | 60 ++++++++++++++++--------------------- js/prefs.js | 2 +- 3 files changed, 28 insertions(+), 38 deletions(-) diff --git a/_locales/en/messages.json b/_locales/en/messages.json index 1bf29a7c..1661336a 100644 --- a/_locales/en/messages.json +++ b/_locales/en/messages.json @@ -1352,8 +1352,8 @@ "message": "Reading styles..." }, "reload": { - "message": "Reload Stylus extension", - "description": "Context menu reload" + "message": "Reload", + "description": "Context menu to reload the extension when installed in developer mode" }, "replace": { "message": "Replace", diff --git a/background/context-menus.js b/background/context-menus.js index 430f6216..d0cb107b 100644 --- a/background/context-menus.js +++ b/background/context-menus.js @@ -1,34 +1,34 @@ /* global browserCommands */// background.js /* global msg */ /* global prefs */ -/* global CHROME FIREFOX URLS ignoreChromeError */// toolbox.js +/* global CHROME URLS ignoreChromeError */// toolbox.js 'use strict'; -(() => { - const contextMenus = { +chrome.management.getSelf(ext => { + const contextMenus = Object.assign({ 'show-badge': { title: 'menuShowBadge', - click: info => prefs.set(info.menuItemId, info.checked), + click: togglePref, }, 'disableAll': { title: 'disableAllStyles', click: browserCommands.styleDisableAll, }, 'open-manager': { - title: 'openStylesManager', + title: 'optionsOpenManager', click: browserCommands.openManage, }, 'open-options': { title: 'openOptions', click: browserCommands.openOptions, }, + }, ext.installType === 'development' && { 'reload': { - presentIf: async () => (await browser.management.getSelf()).installType === 'development', title: 'reload', click: browserCommands.reload, }, + }, CHROME && { 'editor.contextDelete': { - presentIf: () => !FIREFOX && prefs.get('editor.contextDelete'), title: 'editDeleteText', type: 'normal', contexts: ['editable'], @@ -38,39 +38,24 @@ .catch(msg.ignoreError); }, }, - }; - - // "Delete" item in context menu for browsers that don't have it - if (CHROME) { - prefs.__defaults['editor.contextDelete'] = true; - } - - const keys = Object.keys(contextMenus); - prefs.subscribe(keys.filter(id => typeof prefs.defaults[id] === 'boolean'), - CHROME >= 62 && CHROME <= 64 ? toggleCheckmarkBugged : toggleCheckmark); - prefs.subscribe(keys.filter(id => contextMenus[id].presentIf && prefs.knownKeys.includes(id)), - togglePresence); - - createContextMenus(keys); + }); + createContextMenus(Object.keys(contextMenus)); chrome.contextMenus.onClicked.addListener((info, tab) => contextMenus[info.menuItemId].click(info, tab)); - async function createContextMenus(ids) { + function createContextMenus(ids) { for (const id of ids) { - let item = contextMenus[id]; - if (item.presentIf && !await item.presentIf()) { - continue; - } - item = Object.assign({id}, item); - delete item.presentIf; + const item = Object.assign({id, contexts: ['browser_action']}, contextMenus[id]); item.title = chrome.i18n.getMessage(item.title); - if (!item.type && typeof prefs.defaults[id] === 'boolean') { - item.type = 'checkbox'; - item.checked = prefs.get(id); - } - if (!item.contexts) { - item.contexts = ['browser_action']; + if (typeof prefs.defaults[id] === 'boolean') { + if (item.type) { + prefs.subscribe(id, togglePresence); + } else { + item.type = 'checkbox'; + item.checked = prefs.get(id); + prefs.subscribe(id, CHROME >= 62 && CHROME <= 64 ? toggleCheckmarkBugged : toggleCheckmark); + } } delete item.click; chrome.contextMenus.create(item, ignoreChromeError); @@ -87,6 +72,11 @@ createContextMenus([id]); } + /** @param {chrome.contextMenus.OnClickData} info */ + function togglePref(info) { + prefs.set(info.menuItemId, info.checked); + } + function togglePresence(id, checked) { if (checked) { createContextMenus([id]); @@ -94,4 +84,4 @@ chrome.contextMenus.remove(id, ignoreChromeError); } } -})(); +}); diff --git a/js/prefs.js b/js/prefs.js index 276292d3..5ef033e9 100644 --- a/js/prefs.js +++ b/js/prefs.js @@ -96,7 +96,7 @@ 'editor.autoCloseBrackets': true, // auto-add a closing pair when typing an opening one of ()[]{}''"" 'editor.autocompleteOnTyping': false, // show autocomplete dropdown on typing a word token // "Delete" item in context menu for browsers that don't have it - 'editor.contextDelete': null, + 'editor.contextDelete': false, 'editor.selectByTokens': true, 'editor.appliesToLineWidget': true, // show applies-to line widget on the editor