fixup 978e5ca9 and ee86ef30: editor.contextDelete option

This commit is contained in:
tophf 2017-11-26 14:20:44 +03:00
parent 74e6ea5a56
commit 288f347e40

View File

@ -100,7 +100,7 @@ browserCommands = {
// ************************************************************************* // *************************************************************************
// context menus // context menus
contextMenus = Object.assign({ contextMenus = {
'show-badge': { 'show-badge': {
title: 'menuShowBadge', title: 'menuShowBadge',
click: info => prefs.set(info.menuItemId, info.checked), click: info => prefs.set(info.menuItemId, info.checked),
@ -113,22 +113,27 @@ contextMenus = Object.assign({
title: 'openStylesManager', title: 'openStylesManager',
click: browserCommands.openManage, click: browserCommands.openManage,
}, },
}, !FIREFOX && prefs.get('editor.contextDelete') && {
'editor.contextDelete': { 'editor.contextDelete': {
presentIf: () => !FIREFOX && prefs.get('editor.contextDelete'),
title: 'editDeleteText', title: 'editDeleteText',
type: 'normal', type: 'normal',
contexts: ['editable'], contexts: ['editable'],
documentUrlPatterns: [URLS.ownOrigin + 'edit*'], documentUrlPatterns: [URLS.ownOrigin + 'edit*'],
click: (info, tab) => { click: (info, tab) => {
sendMessage(tab.id, {method: 'editDeleteText'}); sendMessage({tabId: tab.id, method: 'editDeleteText'});
}, },
} }
}); };
if (chrome.contextMenus) { if (chrome.contextMenus) {
const createContextMenus = (ids = Object.keys(contextMenus)) => { const createContextMenus = ids => {
for (const id of ids) { for (const id of ids) {
const item = Object.assign({id}, contextMenus[id]); let item = contextMenus[id];
if (item.presentIf && !item.presentIf()) {
continue;
}
item = Object.assign({id}, item);
delete item.presentIf;
const prefValue = prefs.readOnlyValues[id]; const prefValue = prefs.readOnlyValues[id];
item.title = chrome.i18n.getMessage(item.title); item.title = chrome.i18n.getMessage(item.title);
if (!item.type && typeof prefValue === 'boolean') { if (!item.type && typeof prefValue === 'boolean') {
@ -142,20 +147,20 @@ if (chrome.contextMenus) {
chrome.contextMenus.create(item, ignoreChromeError); chrome.contextMenus.create(item, ignoreChromeError);
} }
}; };
createContextMenus(); const toggleCheckmark = (id, checked) => {
const toggleableIds = Object.keys(contextMenus).filter(key => chrome.contextMenus.update(id, {checked}, ignoreChromeError);
typeof prefs.readOnlyValues[key] === 'boolean'); };
prefs.subscribe(toggleableIds, (id, checked) => { const togglePresence = (id, checked) => {
if (id === 'editor.contextDelete') { if (checked) {
if (checked) { createContextMenus([id]);
createContextMenus([id]);
} else {
chrome.contextMenus.remove(id, ignoreChromeError);
}
} else { } else {
chrome.contextMenus.update(id, {checked}, ignoreChromeError); chrome.contextMenus.remove(id, ignoreChromeError);
} }
}); };
const keys = Object.keys(contextMenus);
prefs.subscribe(keys.filter(id => typeof prefs.readOnlyValues[id] === 'boolean'), toggleCheckmark);
prefs.subscribe(keys.filter(id => contextMenus[id].presentIf), togglePresence);
createContextMenus(keys);
} }
// ************************************************************************* // *************************************************************************