Skip to content

Commit

Permalink
feat: support selecting tabs with mod+1 ~ 9
Browse files Browse the repository at this point in the history
  • Loading branch information
pengx17 committed Apr 26, 2022
1 parent 2f18569 commit 5a939e5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ UX is mainly brought from modern browsers:
- Pin/unpin a tab: <kbd>CTRL</kbd> + <kbd>P</kbd> (macOS: <kbd>CMD</kbd> + <kbd>P</kbd>)
- Close a tab: <kbd>SHIFT</kbd> + <kbd>CTRL</kbd> + <kbd>W</kbd> (macOS: <kbd>SHIFT</kbd> + <kbd>CMD</kbd> + <kbd>W</kbd>)
- Change to next tab: <kbd>CTRL</kbd> + <kbd>TAB</kbd>
- Change to nth tab: <kbd>CTRL</kbd> + <kbd>1</kbd> ~ <kbd>9</kbd> (this is not configurable yet)

Hint: you can change them in the Settings. After change, you need to restart the app.

Expand Down
27 changes: 27 additions & 0 deletions src/PageTabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,27 @@ const useRegisterKeybindings = (
}, []);
};

const useRegisterSelectNthTabKeybindings = (cb: (nth: number) => void) => {
const cbRef = useEventCallback(cb);

React.useEffect(() => {
for (let i = 1; i <= 9; i++) {
const setting = {
key: `tabs-select-nth-tab-${i}`,
label: `Select tab ${i}`,
keybinding: {
binding: `mod+${i}`,
mode: "non-editing",
} as SimpleCommandKeybinding,
};
logseq.App.registerCommandPalette(setting, () => {
cbRef(i);
});
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
};

export function PageTabs(): JSX.Element {
const [tabs, setTabs] = useStoreTabs();
const [activeTab, setActiveTab] = useActiveTab(tabs);
Expand Down Expand Up @@ -475,6 +496,12 @@ export function PageTabs(): JSX.Element {
onChangeTab(tabs[idx]);
});

useRegisterSelectNthTabKeybindings(idx => {
if (idx > 0 && idx <= tabs.length) {
onChangeTab(tabs[idx - 1])
}
});

return (
<Tabs
ref={ref}
Expand Down

0 comments on commit 5a939e5

Please sign in to comment.