pages/search: add actionbox handlers

This commit is contained in:
He4eT 2024-01-21 03:33:43 +01:00
commit 45cf56e255
3 changed files with 83 additions and 2 deletions

View file

@ -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()
}
}

View file

@ -1,3 +1,5 @@
import { actionboxHandlers } from './actionboxHandlers.js'
const focusButtonsWithArrows = (e, abortCallback) => { const focusButtonsWithArrows = (e, abortCallback) => {
if (['ArrowUp', 'ArrowDown'].includes(e.key)) { if (['ArrowUp', 'ArrowDown'].includes(e.key)) {
e.preventDefault() e.preventDefault()
@ -31,7 +33,10 @@ const focusButtonsWithArrows = (e, abortCallback) => {
export const attachInputHandlers = (store) => { export const attachInputHandlers = (store) => {
const searchBox = document.getElementById('searchbox') const searchBox = document.getElementById('searchbox')
searchBox.value = ''
const actionbox = document.getElementById('actionbox') const actionbox = document.getElementById('actionbox')
actionbox.value = ''
/* Switch to tab */ /* Switch to tab */
const switchToTab = (tabId) => { 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*/
searchBox.addEventListener('input', (e) => { searchBox.addEventListener('input', (e) => {
@ -75,6 +88,4 @@ export const attachInputHandlers = (store) => {
} }
} }
}) })
/* Actionbox */
} }

View file

@ -62,6 +62,7 @@ export const init = ({
return state return state
}, },
actions: { actions: {
updateState,
goToTab(id) { goToTab(id) {
browserTabs.update(id, { active: true }) browserTabs.update(id, { active: true })
.then(updateState) .then(updateState)