optionsUI: add 'editor.contextDelete'

This commit is contained in:
tophf 2017-04-29 19:54:16 +03:00
parent 0e1124b8bb
commit ee86ef3037
5 changed files with 56 additions and 29 deletions

View File

@ -683,6 +683,9 @@
"optionsAdvancedExposeIframesNote": {
"message": "Enables writing iframe-specific CSS like 'html[stylus-iframe] h1 { display:none }'"
},
"optionsAdvancedContextDelete": {
"message": "Add 'Delete' in editor context menu"
},
"optionsActions": {
"message": "Actions"
},

View File

@ -99,16 +99,10 @@ contextMenus = Object.assign({
title: 'openStylesManager',
click: browserCommands.openManage,
},
},
// detect browsers without Delete by looking at the end of UA string
/Vivaldi\/[\d.]+$/.test(navigator.userAgent) ||
// Chrome and co.
/Safari\/[\d.]+$/.test(navigator.userAgent) &&
// skip forks with Flash as those are likely to have the menu e.g. CentBrowser
!Array.from(navigator.plugins).some(p => p.name == 'Shockwave Flash')
&& {
'editDeleteText': {
}, prefs.get('editor.contextDelete') && {
'editor.contextDelete': {
title: 'editDeleteText',
type: 'normal',
contexts: ['editable'],
documentUrlPatterns: [URLS.ownOrigin + 'edit*'],
click: (info, tab) => {
@ -117,26 +111,37 @@ contextMenus = Object.assign({
}
});
for (const id of Object.keys(contextMenus)) {
const item = Object.assign({id}, contextMenus[id]);
const prefValue = prefs.readOnlyValues[id];
const isBoolean = typeof prefValue == 'boolean';
item.title = chrome.i18n.getMessage(item.title);
if (isBoolean) {
item.type = 'checkbox';
item.checked = prefValue;
}
if (!item.contexts) {
item.contexts = ['browser_action'];
}
delete item.click;
chrome.contextMenus.create(item, ignoreChromeError);
{
const createContextMenus = (ids = Object.keys(contextMenus)) => {
for (const id of ids) {
const item = Object.assign({id}, contextMenus[id]);
const prefValue = prefs.readOnlyValues[id];
item.title = chrome.i18n.getMessage(item.title);
if (!item.type && typeof prefValue == 'boolean') {
item.type = 'checkbox';
item.checked = prefValue;
}
if (!item.contexts) {
item.contexts = ['browser_action'];
}
delete item.click;
chrome.contextMenus.create(item, ignoreChromeError);
}
};
createContextMenus();
prefs.subscribe((id, checked) => {
if (id == 'editor.contextDelete') {
if (checked) {
createContextMenus([id]);
} else {
chrome.contextMenus.remove(id, ignoreChromeError);
}
} else {
chrome.contextMenus.update(id, {checked}, ignoreChromeError);
}
}, Object.keys(contextMenus).filter(key => typeof prefs.readOnlyValues[key] == 'boolean'));
}
prefs.subscribe((id, checked) => {
chrome.contextMenus.update(id, {checked}, ignoreChromeError);
}, Object.keys(contextMenus));
// *************************************************************************
// [re]inject content scripts
{

View File

@ -1234,8 +1234,8 @@ function initHooks() {
function toggleContextMenuDelete(event) {
if (event.button == 2) {
chrome.contextMenus.update('editDeleteText', {
if (event.button == 2 && prefs.get('editor.contextDelete')) {
chrome.contextMenus.update('editor.contextDelete', {
enabled: Boolean(
this.selectionStart != this.selectionEnd ||
this.somethingSelected && this.somethingSelected()

View File

@ -71,6 +71,13 @@
<span class="onoffswitch-label" for="exposeIframes"></span>
</span>
</label>
<label>
<span i18n-text="optionsAdvancedContextDelete"></span>
<span class="onoffswitch">
<input type="checkbox" class="onoffswitch-checkbox" id="editor.contextDelete">
<span class="onoffswitch-label" for="editor.contextDelete"></span>
</span>
</label>
</div>
</div>

View File

@ -44,6 +44,7 @@ var prefs = new function Prefs() {
'editor.matchHighlight': 'token', // token = token/word under cursor even if nothing is selected
// selection = only when something is selected
// '' (empty string) = disabled
'editor.contextDelete': contextDeleteMissing(), // "Delete" item in context menu
'badgeDisabled': '#8B0000', // badge background color when disabled
'badgeNormal': '#006666', // badge background color
@ -301,6 +302,17 @@ var prefs = new function Prefs() {
}
return true;
}
function contextDeleteMissing() {
return (
// detect browsers without Delete by looking at the end of UA string
/Vivaldi\/[\d.]+$/.test(navigator.userAgent) ||
// Chrome and co.
/Safari\/[\d.]+$/.test(navigator.userAgent) &&
// skip forks with Flash as those are likely to have the menu e.g. CentBrowser
!Array.from(navigator.plugins).some(p => p.name == 'Shockwave Flash')
);
}
}();