Skip to content

Commit

Permalink
fix: add close other tabs command
Browse files Browse the repository at this point in the history
  • Loading branch information
pengx17 committed May 10, 2022
1 parent b1232f5 commit 991deab
Showing 1 changed file with 25 additions and 9 deletions.
34 changes: 25 additions & 9 deletions src/PageTabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand All @@ -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
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 991deab

Please sign in to comment.