From 5bffa80be9eb01d22cd4106d896a8e892e764e43 Mon Sep 17 00:00:00 2001 From: Peng Xiao Date: Fri, 11 Mar 2022 17:30:54 +0800 Subject: [PATCH] feat: add two keyboard shortcuts - mod+shift+w: to close current tab - mod+p: to toggle pin status --- src/PageTabs.tsx | 53 ++++++++++++++++++++++++++++++++++++++++++++++-- src/main.tsx | 20 ++++++++---------- 2 files changed, 59 insertions(+), 14 deletions(-) diff --git a/src/PageTabs.tsx b/src/PageTabs.tsx index 9f07298..8612e34 100644 --- a/src/PageTabs.tsx +++ b/src/PageTabs.tsx @@ -1,4 +1,7 @@ -import type { BlockEntity } from "@logseq/libs/dist/LSPlugin"; +import type { + BlockEntity, + SimpleCommandKeybinding, +} from "@logseq/libs/dist/LSPlugin"; import produce from "immer"; import React from "react"; import { useDeepCompareEffect, useLatest } from "react-use"; @@ -82,7 +85,9 @@ const Tabs = React.forwardRef( className={`flex items-center h-full px-1`} style={{ width: "fit-content" }} // By default middle button click will enter the horizontal scroll mode - onMouseDown={e => { if (e.button === 1) e.preventDefault(); }} + onMouseDown={(e) => { + if (e.button === 1) e.preventDefault(); + }} > {tabs.map((tab) => { const isActive = isTabEqual(tab, activePage); @@ -287,6 +292,18 @@ const sortTabs = (tabs: ITabInfo[]) => { }); }; +const useRegisterKeybindings = ( + opts: { key: string; keybinding: SimpleCommandKeybinding; label: string }, + cb: () => void +) => { + const cbRef = useEventCallback(cb); + + React.useEffect(() => { + logseq.App.registerCommandPalette(opts, cbRef); + // eslint-disable-next-line react-hooks/exhaustive-deps + }, []); +}; + export function PageTabs(): JSX.Element { const [tabs, setTabs] = useStoreTabs(); const [activePage, setActivePage] = useActivePage(tabs); @@ -425,6 +442,38 @@ export function PageTabs(): JSX.Element { } }, [activePage, ref]); + useRegisterKeybindings( + { + key: "logseq-tab-toggle-pin", + label: "Tabs: Toggle Tab Pin Status", + keybinding: { + mode: "global", + binding: "mod+p", + }, + }, + () => { + if (currActivePageRef.current) { + onPinTab(currActivePageRef.current); + } + } + ); + + useRegisterKeybindings( + { + key: "logseq-tab-close", + label: "Tabs: Close Current Tab", + keybinding: { + mode: "global", + binding: "mod+shift+w", + }, + }, + () => { + if (currActivePageRef.current) { + onCloseTab(currActivePageRef.current); + } + } + ); + return ( @@ -21,14 +24,7 @@ function main() { document.getElementById("app") ); - // @ts-expect-error - top[magicKey] = true; console.info(`#${pluginId}: MAIN DONE`); } -// @ts-expect-error -if (top[magicKey]) { - logseq.App.relaunch().then(main).catch(console.error); -} else { - logseq.ready(main).catch(console.error); -} +logseq.ready(main).catch(console.error);