Skip to content

Commit

Permalink
implemented #25
Browse files Browse the repository at this point in the history
  • Loading branch information
oxdc committed Sep 1, 2024
1 parent 78c8825 commit bbee520
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 20 deletions.
8 changes: 3 additions & 5 deletions src/components/Group.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,15 @@ export const Group = ({ type, children, group }: GroupProps) => {
const isSidebar =
type === VT.GroupType.LeftSidebar || type === VT.GroupType.RightSidebar;
const [isCollapsed, setIsCollapsed] = useState(isSidebar ? true : false);
const { groupTitles, setGroupTitle, hiddenGroups, toggleHiddenGroup } =
useViewState();
const [isHidden, setIsHidden] = useState(
group ? hiddenGroups.has(group.id) : false
const { groupTitles, setGroupTitle, toggleHiddenGroup } = useViewState();
const isHidden = useViewState((state) =>
group ? state.hiddenGroups.includes(group.id) : false
);
const [isEditing, setIsEditing] = useState(false);
const toggleCollapsed = () => setIsCollapsed(!isCollapsed);
const toggleHidden = () => {
if (isSidebar) return;
if (group) toggleHiddenGroup(group.id, !isHidden);
setIsHidden(!isHidden);
};
useEffect(() => {
if (!group) return;
Expand Down
3 changes: 2 additions & 1 deletion src/components/Tab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const Tab = ({ leaf }: TabProps) => {
const { bindPinningEvent } = useViewState();
const [isPinned, setIsPinned] = useState(leaf.getViewState().pinned);
const { sort } = useTabCache();
const { lockFocusOnLeaf } = useViewState();
const { lockFocusOnLeaf, toggleHiddenGroup } = useViewState();
const lastActiveLeaf = useViewState((state) => state.latestActiveLeaf);

useEffect(() => {
Expand Down Expand Up @@ -55,6 +55,7 @@ export const Tab = ({ leaf }: TabProps) => {
const workspace = plugin.app.workspace as VT.Workspace;
workspace.setActiveLeaf(leaf, { focus: true });
workspace.onLayoutChange();
toggleHiddenGroup(leaf.parent.id, false);
lockFocusOnLeaf(plugin.app, leaf);
};

Expand Down
30 changes: 16 additions & 14 deletions src/models/ViewState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const createNewPinningEvents = () =>

interface ViewState {
groupTitles: GroupTitles;
hiddenGroups: Set<VT.Identifier>;
hiddenGroups: Array<VT.Identifier>;
latestActiveLeaf: VT.WorkspaceLeaf | null;
pinningEvents: PinningEvents;
clear: () => void;
Expand Down Expand Up @@ -51,15 +51,14 @@ const loadViewState = (): GroupTitles | null => {
return new DefaultRecord(factory, entries);
};

const saveHiddenGroups = (hiddenGroups: Set<VT.Identifier>) => {
const data = Array.from(hiddenGroups);
localStorage.setItem("hidden-groups", JSON.stringify(data));
const saveHiddenGroups = (hiddenGroups: Array<VT.Identifier>) => {
localStorage.setItem("hidden-groups", JSON.stringify(hiddenGroups));
};

const loadHiddenGroups = (): Set<VT.Identifier> => {
const loadHiddenGroups = (): Array<VT.Identifier> => {
const data = localStorage.getItem("hidden-groups");
if (!data) return new Set();
return new Set(JSON.parse(data));
if (!data) return [];
return JSON.parse(data);
};

export const useViewState = create<ViewState>()((set, get) => ({
Expand All @@ -74,13 +73,16 @@ export const useViewState = create<ViewState>()((set, get) => ({
saveViewState(state.groupTitles);
return state;
}),
toggleHiddenGroup: (id: VT.Identifier, isHidden: boolean) =>
set((state) => {
if (isHidden) state.hiddenGroups.add(id);
else state.hiddenGroups.delete(id);
saveHiddenGroups(state.hiddenGroups);
return state;
}),
toggleHiddenGroup: (id: VT.Identifier, isHidden: boolean) => {
if (isHidden) {
set((state) => ({ hiddenGroups: [...state.hiddenGroups, id] }));
} else {
set((state) => ({
hiddenGroups: state.hiddenGroups.filter((gid) => gid !== id),
}));
}
saveHiddenGroups(get().hiddenGroups);
},
setLatestActiveLeaf(plugin: ObsidianVerticalTabs) {
const workspace = plugin.app.workspace as VT.Workspace;
const activeView = workspace.getActiveViewOfType(ItemView);
Expand Down

0 comments on commit bbee520

Please sign in to comment.