diff --git a/pages/search/modules/actionboxHandlers.js b/pages/search/modules/actionboxHandlers.js new file mode 100644 index 0000000..1d04731 --- /dev/null +++ b/pages/search/modules/actionboxHandlers.js @@ -0,0 +1,69 @@ +export const actionboxHandlers = (commandQuery, store, flush) => { + const command = commandQuery.slice(0, 1) + const label = commandQuery.slice(1) + + const noop = (command) => () => { + if (command) { + console.log('Unsupported command:', command) + } + } + + ({ + '?': openHelp, + 'x': closeCurrentTab, + 'r': reloadCurrentTab, + /* */ + 'f': switchToTab, + 'd': closeTab, + 'c': duplicateTab, + }[command] ?? noop(command))(store, label, flush) +} + +/* */ + +function openHelp () { + +} + +function closeCurrentTab () { + window.close() +} + +function reloadCurrentTab () { + location.reload() +} + +/* */ + +const getTabByLabel = (store, label) => { + const tabs = store.getCurrentState().tabs + return tabs.find(tab => tab.label === label) +} + +function switchToTab (store, label) { + const tab = getTabByLabel(store, label) + if (tab) { + store.actions.goToTab(tab.id) + flush() + closeCurrentTab() + } +} + +function closeTab (store, label, flush) { + const tab = getTabByLabel(store, label) + if (tab) { + store.actions.closeTab(tab.id) + flush() + } +} + +function duplicateTab (store, label, flush) { + const tab = getTabByLabel(store, label) + if (tab) { + browser.tabs.create({ + active: false, + url: tab.url, + }).then(store.actions.updateState) + flush() + } +} diff --git a/pages/search/modules/inputHandlers.js b/pages/search/modules/inputHandlers.js index 5049b52..a5c8961 100644 --- a/pages/search/modules/inputHandlers.js +++ b/pages/search/modules/inputHandlers.js @@ -1,3 +1,5 @@ +import { actionboxHandlers } from './actionboxHandlers.js' + const focusButtonsWithArrows = (e, abortCallback) => { if (['ArrowUp', 'ArrowDown'].includes(e.key)) { e.preventDefault() @@ -31,7 +33,10 @@ const focusButtonsWithArrows = (e, abortCallback) => { export const attachInputHandlers = (store) => { const searchBox = document.getElementById('searchbox') + searchBox.value = '' + const actionbox = document.getElementById('actionbox') + actionbox.value = '' /* Switch to tab */ const switchToTab = (tabId) => { @@ -58,6 +63,14 @@ export const attachInputHandlers = (store) => { } }) + /* Actionbox */ + actionbox.addEventListener('input', (e) => { + const commandQuery = e.target.value + const flush = () => (actionbox.value = '') + + actionboxHandlers(commandQuery, store, flush) + }) + /* Searchbox*/ searchBox.addEventListener('input', (e) => { @@ -75,6 +88,4 @@ export const attachInputHandlers = (store) => { } } }) - - /* Actionbox */ } diff --git a/pages/search/modules/store.js b/pages/search/modules/store.js index 3f799c3..4dd1e81 100644 --- a/pages/search/modules/store.js +++ b/pages/search/modules/store.js @@ -62,6 +62,7 @@ export const init = ({ return state }, actions: { + updateState, goToTab(id) { browserTabs.update(id, { active: true }) .then(updateState)