From e6d73be04934248906c93d8d6850878a3c121a61 Mon Sep 17 00:00:00 2001 From: tophf Date: Sun, 18 Oct 2020 16:37:42 +0300 Subject: [PATCH] option to open editor in a simple window (no omnibox) (#1067) --- _locales/en/messages.json | 4 ++++ background/background.js | 7 +++--- js/messaging.js | 50 ++++++++++++++++----------------------- js/prefs.js | 1 + manage/manage.js | 5 ++-- options.html | 7 ++++++ 6 files changed, 40 insertions(+), 34 deletions(-) diff --git a/_locales/en/messages.json b/_locales/en/messages.json index e5f505f3..39f194f3 100644 --- a/_locales/en/messages.json +++ b/_locales/en/messages.json @@ -1122,6 +1122,10 @@ "message": "Action menu", "description": "Tooltip for menu button in popup." }, + "popupOpenEditInPopup": { + "message": "Use a simple window (no omnibox)", + "description": "Label for the checkbox controlling 'edit' action behavior in the popup." + }, "popupOpenEditInWindow": { "message": "Open editor in a new window", "description": "Label for the checkbox controlling 'edit' action behavior in the popup." diff --git a/background/background.js b/background/background.js index fa54e208..0c4eaaf7 100644 --- a/background/background.js +++ b/background/background.js @@ -303,9 +303,10 @@ function openEditor(params) { u.search = new URLSearchParams(params); return openURL({ url: `${u}`, - newWindow: prefs.get('openEditInWindow'), - windowPosition: prefs.get('windowPosition'), - currentWindow: null + currentWindow: null, + newWindow: prefs.get('openEditInWindow') && Object.assign({}, + prefs.get('openEditInWindow.popup') && {type: 'popup'}, + prefs.get('windowPosition')), }); } diff --git a/js/messaging.js b/js/messaging.js index ec51267c..632b2116 100644 --- a/js/messaging.js +++ b/js/messaging.js @@ -165,47 +165,39 @@ function findExistingTab({url, currentWindow, ignoreHash = true, ignoreSearch = * @param {number} [_.openerTabId] defaults to the active tab * @param {Boolean} [_.active=true] `true` to activate the tab * @param {Boolean|null} [_.currentWindow=true] `null` to check all windows - * @param {Boolean} [_.newWindow=false] `true` to open a new window - * @param {chrome.windows.CreateData} [_.windowPosition] options for chrome.windows.create + * @param {chrome.windows.CreateData} [_.newWindow] creates a new window with these params if specified * @returns {Promise} Promise -> opened/activated tab */ -function openURL({ +async function openURL({ url, index, openerTabId, active = true, currentWindow = true, - newWindow = false, - windowPosition, + newWindow, }) { if (!url.includes('://')) { url = chrome.runtime.getURL(url); } - return findExistingTab({url, currentWindow}).then(tab => { - if (tab) { - return activateTab(tab, { - index, - openerTabId, - // when hash is different we can only set `url` if it has # otherwise the tab would reload - url: url !== (tab.pendingUrl || tab.url) && url.includes('#') ? url : undefined, - }); - } - if (newWindow && browser.windows) { - return browser.windows.create(Object.assign({url}, windowPosition)) - .then(wnd => wnd.tabs[0]); - } - return getActiveTab().then((activeTab = {url: ''}) => - isTabReplaceable(activeTab, url) ? - activateTab(activeTab, {url, openerTabId}) : // not moving the tab - createTabWithOpener(activeTab, {url, index, active})); - }); - function createTabWithOpener(openerTab, options) { - const id = openerTabId == null ? openerTab.id : openerTabId; - if (id != null && !openerTab.incognito && openerTabIdSupported) { - options.openerTabId = id; - } - return browser.tabs.create(options); + let tab = await findExistingTab({url, currentWindow}); + if (tab) { + return activateTab(tab, { + index, + openerTabId, + // when hash is different we can only set `url` if it has # otherwise the tab would reload + url: url !== (tab.pendingUrl || tab.url) && url.includes('#') ? url : undefined, + }); } + if (newWindow && browser.windows) { + return (await browser.windows.create(Object.assign({url}, newWindow)).tabs)[0]; + } + tab = await getActiveTab() || {url: ''}; + if (isTabReplaceable(tab, url)) { + return activateTab(tab, {url, openerTabId}); + } + const id = openerTabId == null ? tab.id : openerTabId; + const opener = id != null && !tab.incognito && openerTabIdSupported && {openerTabId: id}; + return browser.tabs.create(Object.assign({url, index, active}, opener)); } // replace empty tab (NTP or about:blank) diff --git a/js/prefs.js b/js/prefs.js index 3b39cf85..bb3ba650 100644 --- a/js/prefs.js +++ b/js/prefs.js @@ -4,6 +4,7 @@ self.prefs = self.INJECTED === 1 ? self.prefs : (() => { const defaults = { 'openEditInWindow': false, // new editor opens in a own browser window + 'openEditInWindow.popup': false, // new editor opens in a simplified browser window without omnibox 'windowPosition': {}, // detached window position 'show-badge': true, // display text on popup menu icon 'disableAll': false, // boss key diff --git a/manage/manage.js b/manage/manage.js index 2b856600..5bd0ca55 100644 --- a/manage/manage.js +++ b/manage/manage.js @@ -392,10 +392,11 @@ Object.assign(handleEvent, { const openWindow = left && shift && !ctrl; const openBackgroundTab = (middle && !shift) || (left && ctrl && !shift); const openForegroundTab = (middle && shift) || (left && ctrl && shift); - const url = $('[href]', event.target.closest('.entry')).href; + const entry = event.target.closest('.entry'); + const url = $('[href]', entry).href; if (openWindow || openBackgroundTab || openForegroundTab) { if (chrome.windows && openWindow) { - chrome.windows.create(Object.assign(prefs.get('windowPosition'), {url})); + API.openEditor({id: entry.styleId}); } else { getOwnTab().then(({index}) => { openURL({ diff --git a/options.html b/options.html index 718a8d31..ca5e0451 100644 --- a/options.html +++ b/options.html @@ -111,6 +111,13 @@ +