From 991deab91dac4851adf81956da453b39ff0e6270 Mon Sep 17 00:00:00 2001 From: Peng Xiao Date: Tue, 10 May 2022 10:27:09 +0800 Subject: [PATCH] fix: add close other tabs command --- src/PageTabs.tsx | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/src/PageTabs.tsx b/src/PageTabs.tsx index 3714d5e..e710752 100644 --- a/src/PageTabs.tsx +++ b/src/PageTabs.tsx @@ -337,7 +337,7 @@ const useRegisterSelectNthTabKeybindings = (cb: (nth: number) => void) => { }, []); }; -const useRegisterCloseAllButPins = (cb: () => void) => { +const useRegisterCloseAllButPins = (cb: (b: boolean) => void) => { const cbRef = useEventCallback(cb); React.useEffect(() => { @@ -348,7 +348,21 @@ const useRegisterCloseAllButPins = (cb: () => void) => { // no keybindings yet }, () => { - cbRef(); + cbRef(false); + } + ); + // eslint-disable-next-line react-hooks/exhaustive-deps + }, []); + + React.useEffect(() => { + logseq.App.registerCommandPalette( + { + key: `tabs-close-all`, + label: `Close other tabs`, + // no keybindings yet + }, + () => { + cbRef(true); } ); // eslint-disable-next-line react-hooks/exhaustive-deps @@ -381,17 +395,19 @@ export function PageTabs(): JSX.Element { } }); - const onCloseAllTabs = useEventCallback(() => { - const pinnedTabs = tabs.filter((t) => t.pinned); - const newTabs = [...pinnedTabs]; - setTabs(newTabs); - logseq.App.pushState("home"); - }); - const getCurrentActiveIndex = () => { return tabs.findIndex((ct) => isTabEqual(ct, currActiveTabRef.current)); }; + const onCloseAllTabs = useEventCallback((excludeActive: boolean) => { + const newTabs = tabs.filter( + (t) => + t.pinned || (excludeActive && isTabEqual(t, currActiveTabRef.current)) + ); + setTabs(newTabs); + logseq.App.pushState("home"); + }); + const onChangeTab = useEventCallback(async (t: ITabInfo) => { setActiveTab(t); const idx = getCurrentActiveIndex();