Skip to content

Commit 8eefa46

Browse files
committed
control tabs with keyboard
1 parent caeef94 commit 8eefa46

File tree

6 files changed

+137
-60
lines changed

6 files changed

+137
-60
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"package": "ts-node ./.erb/scripts/clean.js dist && npm run build && electron-builder build --publish never",
1616
"package:win": "ts-node ./.erb/scripts/clean.js dist && npm run build && electron-builder build --publish never --win",
1717
"package:linux": "ts-node ./.erb/scripts/clean.js dist && npm run build && electron-builder build --publish never --linux",
18+
"deploy": "ts-node ./.erb/scripts/clean.js dist && npm run build && electron-builder build --publish always",
1819
"prepare": "husky install",
1920
"rebuild": "electron-rebuild --parallel --types prod,dev,optional --module-dir release/app",
2021
"start": "ts-node ./.erb/scripts/check-port-in-use.js && npm run start:renderer",

src/renderer/App.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import List from './components/List';
2929
import Searchbar from './components/Searchbar';
3030
import Webview from './components/Webview';
3131
import {SearchContextProvider } from './context/SearchContext';
32+
import { TabContextProvider } from './context/TabContext';
3233

3334

3435
const Hello = () => {
@@ -38,8 +39,10 @@ const Hello = () => {
3839
{/* Searchbar */}
3940
<SearchContextProvider>
4041
<Container centerContent paddingTop='10px'>
41-
<Searchbar />
42-
<List />
42+
<TabContextProvider>
43+
<Searchbar />
44+
<List />
45+
</TabContextProvider>
4346
</Container>
4447
</SearchContextProvider>
4548
</div>

src/renderer/components/BrowserCollapse.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,16 @@ import { SearchContext } from 'renderer/context/SearchContext';
2121
import { AiOutlineLeft, AiOutlineRight, AiOutlineClose } from 'react-icons/ai';
2222
import Webview from './Webview';
2323

24-
const BrowserCollapse = ({ name, tabUrl, tabId }) => {
24+
const BrowserCollapse = ({ name, tabUrl, tabId, onOpen, index }) => {
2525
const webviewRef = useRef();
2626

2727
const [collapseName, setCollapseName] = useState(name);
2828

2929
const { closeTab } = useContext(SearchContext);
3030

31+
32+
33+
3134
const close = (id: any) => closeTab(id);
3235

3336
const back = (e: MouseEvent<HTMLButtonElement, MouseEvent>) => {
@@ -55,9 +58,9 @@ const BrowserCollapse = ({ name, tabUrl, tabId }) => {
5558
return webviewRef.current.getTitle();
5659
};
5760
return (
58-
<AccordionItem border="none">
61+
<AccordionItem border="none" >
5962
<h2>
60-
<AccordionButton _expanded={{ bg: '#03c9d7', color: 'white' }}>
63+
<AccordionButton _expanded={{ bg: '#03c9d7', color: 'white' }} onClick={()=> onOpen(index)} >
6164
<IconButton
6265
backgroundColor="#32363e"
6366
_hover={{ bg: '#32363e' }}

src/renderer/components/List.tsx

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,36 @@
11
import { Accordion } from "@chakra-ui/react";
2-
import { useContext } from "react";
2+
import { useCallback, useContext, useEffect, useState } from "react";
33
import { SearchContext } from "renderer/context/SearchContext";
4+
import { TabContext } from "renderer/context/TabContext";
45
import BrowserCollapse from "./BrowserCollapse";
56

67
const List = () => {
78

89
const {tabs} = useContext(SearchContext);
910

11+
const {
12+
currentTabIndex,
13+
setTabIndex
14+
} = useContext(TabContext);
15+
16+
const onOpen = (index) => {
17+
18+
if(currentTabIndex == index) {
19+
setTabIndex(null)
20+
}else{
21+
setTabIndex(index)
22+
}
23+
24+
console.log(tabs);
25+
}
26+
27+
1028
return (
1129
<div>
12-
<Accordion width='95vw' allowToggle _hover={{}}
30+
<Accordion width='95vw' allowToggle _hover={{}} index={currentTabIndex}
1331
border='none' backgroundColor='#32363e' margin='10px' overflowY='scroll' >
1432
{tabs.map((tab,index)=>(
15-
<BrowserCollapse key={index} name={tab.keyword} tabUrl={tab.url} tabId={tab.tabId}/>
33+
<BrowserCollapse key={index} name={tab.keyword} tabUrl={tab.url} tabId={tab.tabId} onOpen={onOpen} index={index}/>
1634
))}
1735
</Accordion>
1836
</div>

src/renderer/components/Searchbar.tsx

Lines changed: 61 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -6,94 +6,103 @@ import {
66
InputLeftAddon,
77
InputRightElement,
88
HStack,
9-
Select
9+
Select,
1010
} from '@chakra-ui/react';
11-
import {useCallback, useContext, useEffect,useState} from 'react';
11+
import { useCallback, useContext, useEffect, useState } from 'react';
1212
import { SearchContext } from 'renderer/context/SearchContext';
13-
import {FcGoogle} from 'react-icons/fc';
14-
import {SiDuckduckgo} from 'react-icons/si';
15-
import {FaYandexInternational} from 'react-icons/fa';
13+
import { FcGoogle } from 'react-icons/fc';
14+
import { SiDuckduckgo } from 'react-icons/si';
15+
import { FaYandexInternational } from 'react-icons/fa';
1616
import '../App.css';
17+
import { TabContext } from 'renderer/context/TabContext';
1718
import SearchEngineModal from './Settings/SearchEngineModal';
1819

1920
const Searchbar = ({}) => {
20-
2121
const [isModalOpen, setModal] = useState(false);
2222

2323
const onClose = () => setModal(!isModalOpen);
2424

25-
const {
26-
url,
27-
setUrl,
28-
keyword,
29-
setKeyword,
30-
onChange,
31-
search,
32-
searchEngine
33-
} = useContext(SearchContext);
25+
const { url, setUrl, keyword, setKeyword, onChange, search, searchEngine } =
26+
useContext(SearchContext);
3427

28+
const { nexTab,setTabIndex } = useContext(TabContext);
3529

36-
const handleSetSearchEngineShortcut = useCallback((event)=>{
37-
if(event.ctrlKey && (event.key === 'E' || event.key === 'e')){
30+
const handleSetSearchEngineShortcut = useCallback((event) => {
31+
if (event.ctrlKey && (event.key === 'E' || event.key === 'e')) {
3832
onClose();
3933
}
40-
})
4134

42-
useEffect(()=>{
43-
document.addEventListener('keydown',handleSetSearchEngineShortcut);
35+
nexTab(event);
36+
37+
38+
39+
});
40+
41+
useEffect(() => {
42+
document.addEventListener('keydown', handleSetSearchEngineShortcut);
4443

4544
return () => {
46-
document.removeEventListener('keydown',handleSetSearchEngineShortcut);
47-
}
48-
},[])
45+
document.removeEventListener('keydown', handleSetSearchEngineShortcut);
46+
};
47+
}, []);
4948

5049
const getEngineIcon = () => {
51-
switch(searchEngine){
52-
case "https://www.google.com/search?q=":
53-
return <FcGoogle />
54-
case "https://yandex.com.tr/search/?text=":
55-
return <FaYandexInternational/>
56-
case "https://duckduckgo.com/?q=":
57-
return <SiDuckduckgo />
58-
}
59-
}
50+
switch (searchEngine) {
51+
case 'https://www.google.com/search?q=':
52+
return <FcGoogle />;
53+
case 'https://yandex.com.tr/search/?text=':
54+
return <FaYandexInternational />;
55+
case 'https://duckduckgo.com/?q=':
56+
return <SiDuckduckgo />;
57+
}
58+
};
6059

6160
return (
6261
<>
63-
<HStack w='95vw' >
64-
<InputGroup size='md' h='40px' id='search-bar-container'
65-
>
66-
<InputLeftAddon h='40px' children={getEngineIcon()} color='white' backgroundColor='#32363e' border='none' onClick={onClose} />
67-
<Input variant='filled' placeholder='Search' autoFocus
68-
onKeyDown={e => e.key === 'Enter' && search()}
62+
<HStack w="95vw">
63+
<InputGroup size="md" h="40px" id="search-bar-container">
64+
<InputLeftAddon
65+
h="40px"
66+
children={getEngineIcon()}
67+
color="white"
68+
backgroundColor="#32363e"
69+
border="none"
70+
onClick={onClose}
71+
/>
72+
<Input
73+
variant="filled"
74+
placeholder="Search"
75+
autoFocus
76+
onKeyDown={(e) => e.key === 'Enter' && search()}
6977
onChange={onChange}
70-
backgroundColor='#32363e'
71-
_focus={{backgroundColor:'#32363e'}}
72-
_hover={{backgroundColor:'#32363e'}}
73-
/>
78+
backgroundColor="#32363e"
79+
_focus={{ backgroundColor: '#32363e' }}
80+
_hover={{ backgroundColor: '#32363e' }}
81+
/>
7482

7583
<InputRightElement>
76-
<Button h='40px' w='40px' size='sm'
84+
<Button
85+
h="40px"
86+
w="40px"
87+
size="sm"
7788
leftIcon={<SearchIcon />}
78-
backgroundColor='#32363e'
79-
variant='solid'
89+
backgroundColor="#32363e"
90+
variant="solid"
8091
onClick={search}
8192
_hover={{
82-
backgroundColor:'#32363e',
93+
backgroundColor: '#32363e',
8394
}}
8495
_focus={{
85-
backgroundColor:'#32363e',
86-
outline:'none'
96+
backgroundColor: '#32363e',
97+
outline: 'none',
8798
}}
88-
>
89-
</Button>
99+
/>
90100
</InputRightElement>
91101
</InputGroup>
92-
93102
</HStack>
94103
<SearchEngineModal isOpen={isModalOpen} onClose={onClose} />
95104
</>
96105
);
97-
}
106+
};
98107

99108
export default Searchbar;
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { createContext, useCallback, useContext, useState } from 'react';
2+
import { SearchContext } from './SearchContext';
3+
4+
export const TabContext = createContext();
5+
6+
export const TabContextProvider = (props) => {
7+
const [currentTabIndex, setTabIndex] = useState(0);
8+
9+
const { closeTab, tabs } = useContext(SearchContext);
10+
11+
const nexTab = (event) => {
12+
13+
if (event.ctrlKey && isFinite(event.key)) {
14+
// if (Number(event.key) - 1 == currentTabIndex) {
15+
// setTabIndex(null);
16+
// } else {
17+
// setTabIndex(Number(event.key) - 1);
18+
// }
19+
20+
21+
22+
setTabIndex(Number(event.key) - 1);
23+
24+
}
25+
26+
if (event.altKey && (event.key === 'X' || event.key === 'x')) {
27+
console.log('tab id', tabs);
28+
//closeTab(tabs)
29+
}
30+
};
31+
32+
return (
33+
<TabContext.Provider
34+
value={{
35+
currentTabIndex,
36+
setTabIndex,
37+
nexTab,
38+
}}
39+
>
40+
{props.children}
41+
</TabContext.Provider>
42+
);
43+
};

0 commit comments

Comments
 (0)