Skip to content

Commit

Permalink
added button for collapsing/expanding all groups: fixed #37
Browse files Browse the repository at this point in the history
  • Loading branch information
oxdc committed Sep 27, 2024
1 parent 53633bb commit 14de87c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
13 changes: 12 additions & 1 deletion src/components/Group.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,18 @@ export const Group = ({ type, children, group }: GroupProps) => {
const workspace = app.workspace as VT.Workspace;
const isSidebar =
type === VT.GroupType.LeftSidebar || type === VT.GroupType.RightSidebar;
const [isCollapsed, setIsCollapsed] = useState(isSidebar ? true : false);
const globalCollpaseState = useViewState(
(state) => state.globalCollapseState
);
const [isCollapsed, setIsCollapsed] = useState(
isSidebar ? true : globalCollpaseState
);

useEffect(() => {
if (isSidebar) return;
setIsCollapsed(globalCollpaseState);
}, [globalCollpaseState]);

const { groupTitles, setGroupTitle, toggleHiddenGroup } = useViewState();
const isHidden = useViewState((state) =>
group ? state.hiddenGroups.includes(group.id) : false
Expand Down
22 changes: 21 additions & 1 deletion src/components/NavigationHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ export const NavigationHeader = () => {
const toggleZenMode = useSettings.use.toggleZenMode();
const sortStrategy = useTabCache((state) => state.sortStrategy);
const { setSortStrategy } = useTabCache();
const { lockFocus } = useViewState();
const { lockFocus, setAllCollapsed, setAllExpanded } = useViewState();
const globalCollapseState = useViewState(
(state) => state.globalCollapseState
);

const createAndShowNewTab = () => {
const workspace = plugin.app.workspace as VT.Workspace;
Expand Down Expand Up @@ -110,6 +113,23 @@ export const NavigationHeader = () => {
isActive={zenMode}
isNavAction={true}
/>
<IconButton
icon={
globalCollapseState
? "unfold-vertical"
: "fold-vertical"
}
action="global-collapse"
tooltip={
globalCollapseState ? "Expand all" : "Collapse all"
}
onClick={() =>
globalCollapseState
? setAllExpanded()
: setAllCollapsed()
}
isNavAction={true}
/>
</div>
</div>
);
Expand Down
10 changes: 10 additions & 0 deletions src/models/ViewState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ interface ViewState {
hiddenGroups: Array<VT.Identifier>;
latestActiveLeaf: VT.WorkspaceLeaf | null;
pinningEvents: PinningEvents;
globalCollapseState: boolean;
clear: () => void;
setGroupTitle: (id: VT.Identifier, name: string) => void;
toggleHiddenGroup: (id: VT.Identifier, isHidden: boolean) => void;
Expand All @@ -59,6 +60,8 @@ interface ViewState {
callback: PinningEventCallback
) => void;
unbindPinningEvent: (leaf: VT.WorkspaceLeaf) => void;
setAllCollapsed: () => void;
setAllExpanded: () => void;
}

const saveViewState = (titles: GroupTitles) => {
Expand Down Expand Up @@ -115,6 +118,7 @@ export const useViewState = create<ViewState>()((set, get) => ({
hiddenGroups: loadHiddenGroups(),
latestActiveLeaf: null,
pinningEvents: createNewPinningEvents(),
globalCollapseState: false,
leftButtonClone: null,
rightButtonClone: null,
topLeftContainer: null,
Expand Down Expand Up @@ -290,4 +294,10 @@ export const useViewState = create<ViewState>()((set, get) => ({
set({ pinningEvents });
}
},
setAllCollapsed() {
set({ globalCollapseState: true });
},
setAllExpanded() {
set({ globalCollapseState: false });
},
}));

0 comments on commit 14de87c

Please sign in to comment.