From c2d2f895a5e65cc7a42da39e7ab8acf73cb03fc8 Mon Sep 17 00:00:00 2001 From: Jiabo Hou Date: Tue, 31 Oct 2017 19:37:27 -0400 Subject: [PATCH 1/2] added support for pinning/unpinning tabs features: * pin tab * unpin tab * toggle pin/unpin tab --- README.md | 1 + src/background/actions/operation.js | 6 ++++++ src/background/tabs.js | 14 +++++++++++++- src/shared/default-settings.js | 1 + src/shared/operations.js | 3 +++ 5 files changed, 24 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 04e6e9e..d53a89e 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,7 @@ The default mappings are as follows: - g0, g$: select first or last tab - r: reload current tab - R: reload current tab without cache +- p: toggle pin/unpin current tab ### Navigation - f: start following links in the page diff --git a/src/background/actions/operation.js b/src/background/actions/operation.js index a5094c0..c45eabd 100644 --- a/src/background/actions/operation.js +++ b/src/background/actions/operation.js @@ -26,6 +26,12 @@ const exec = (operation, tab) => { return tabs.selectLastTab(); case operations.TAB_RELOAD: return tabs.reload(tab, operation.cache); + case operations.TAB_PIN: + return tabs.updateTabPinned(tab, true); + case operations.TAB_UNPIN: + return tabs.updateTabPinned(tab, false); + case operations.TAB_TOGGLE_PINNED: + return tabs.updateTabPinned(tab); case operations.ZOOM_IN: return zooms.zoomIn(); case operations.ZOOM_OUT: diff --git a/src/background/tabs.js b/src/background/tabs.js index 020c826..38b2ed9 100644 --- a/src/background/tabs.js +++ b/src/background/tabs.js @@ -100,7 +100,19 @@ const reload = (current, cache) => { ); }; +const updateTabPinned = (current, pinned) => { + return browser.tabs.query({ currentWindow: true, active: true }) + .then(() => { + let newPinned = pinned; + if (newPinned !== true && newPinned !== false) { + newPinned = !current.pinned; + } + return browser.tabs.update(current.id, { pinned: newPinned }); + }); +}; + export { closeTab, reopenTab, selectAt, selectByKeyword, getCompletions, - selectPrevTab, selectNextTab, selectFirstTab, selectLastTab, reload + selectPrevTab, selectNextTab, selectFirstTab, selectLastTab, reload, + updateTabPinned }; diff --git a/src/shared/default-settings.js b/src/shared/default-settings.js index 49eee37..0127933 100644 --- a/src/shared/default-settings.js +++ b/src/shared/default-settings.js @@ -32,6 +32,7 @@ export default { "g$": { "type": "tabs.last" }, "r": { "type": "tabs.reload", "cache": false }, "R": { "type": "tabs.reload", "cache": true }, + "p": { "type": "tabs.pin.toggle" }, "zi": { "type": "zoom.in" }, "zo": { "type": "zoom.out" }, "zz": { "type": "zoom.neutral" }, diff --git a/src/shared/operations.js b/src/shared/operations.js index 1519e90..1aa4227 100644 --- a/src/shared/operations.js +++ b/src/shared/operations.js @@ -39,6 +39,9 @@ export default { TAB_FIRST: 'tabs.first', TAB_LAST: 'tabs.last', TAB_RELOAD: 'tabs.reload', + TAB_PIN: 'tabs.pin', + TAB_UNPIN: 'tabs.unpin', + TAB_TOGGLE_PINNED: 'tabs.pin.toggle', // Zooms ZOOM_IN: 'zoom.in', From 87b8280d4bdcaddeac1aff95d9354e1954b7c25a Mon Sep 17 00:00:00 2001 From: Jiabo Hou Date: Sun, 5 Nov 2017 17:59:46 -0500 Subject: [PATCH 2/2] code review * change default keybinding for pin/unpin tab from 'p' to 'zp' * use toggleTabPinned to toggle the tab pinned state, instead of updateTabPinned --- README.md | 2 +- src/background/actions/operation.js | 6 +++++- src/background/tabs.js | 12 ++++++------ src/shared/default-settings.js | 2 +- 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index d53a89e..2d34a06 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ The default mappings are as follows: - g0, g$: select first or last tab - r: reload current tab - R: reload current tab without cache -- p: toggle pin/unpin current tab +- zp: toggle pin/unpin current tab ### Navigation - f: start following links in the page diff --git a/src/background/actions/operation.js b/src/background/actions/operation.js index c45eabd..d0caf80 100644 --- a/src/background/actions/operation.js +++ b/src/background/actions/operation.js @@ -10,6 +10,9 @@ const sendConsoleShowCommand = (tab, command) => { }); }; +// This switch statement is only gonna get longer as more +// features are added, so disable complexity check +/* eslint-disable complexity */ const exec = (operation, tab) => { switch (operation.type) { case operations.TAB_CLOSE: @@ -31,7 +34,7 @@ const exec = (operation, tab) => { case operations.TAB_UNPIN: return tabs.updateTabPinned(tab, false); case operations.TAB_TOGGLE_PINNED: - return tabs.updateTabPinned(tab); + return tabs.toggleTabPinned(tab); case operations.ZOOM_IN: return zooms.zoomIn(); case operations.ZOOM_OUT: @@ -64,5 +67,6 @@ const exec = (operation, tab) => { return Promise.resolve(); } }; +/* eslint-enable complexity */ export { exec }; diff --git a/src/background/tabs.js b/src/background/tabs.js index 38b2ed9..23b3b7b 100644 --- a/src/background/tabs.js +++ b/src/background/tabs.js @@ -103,16 +103,16 @@ const reload = (current, cache) => { const updateTabPinned = (current, pinned) => { return browser.tabs.query({ currentWindow: true, active: true }) .then(() => { - let newPinned = pinned; - if (newPinned !== true && newPinned !== false) { - newPinned = !current.pinned; - } - return browser.tabs.update(current.id, { pinned: newPinned }); + return browser.tabs.update(current.id, { pinned: pinned }); }); }; +const toggleTabPinned = (current) => { + updateTabPinned(current, !current.pinned); +}; + export { closeTab, reopenTab, selectAt, selectByKeyword, getCompletions, selectPrevTab, selectNextTab, selectFirstTab, selectLastTab, reload, - updateTabPinned + updateTabPinned, toggleTabPinned }; diff --git a/src/shared/default-settings.js b/src/shared/default-settings.js index 0127933..e45bee6 100644 --- a/src/shared/default-settings.js +++ b/src/shared/default-settings.js @@ -32,7 +32,7 @@ export default { "g$": { "type": "tabs.last" }, "r": { "type": "tabs.reload", "cache": false }, "R": { "type": "tabs.reload", "cache": true }, - "p": { "type": "tabs.pin.toggle" }, + "zp": { "type": "tabs.pin.toggle" }, "zi": { "type": "zoom.in" }, "zo": { "type": "zoom.out" }, "zz": { "type": "zoom.neutral" },