Skip to content

Commit

Permalink
removed VT defs
Browse files Browse the repository at this point in the history
  • Loading branch information
oxdc committed Oct 1, 2024
1 parent 1f34703 commit 8561377
Show file tree
Hide file tree
Showing 15 changed files with 168 additions and 178 deletions.
19 changes: 10 additions & 9 deletions src/components/Group.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
import * as VT from "src/models/VTWorkspace";
import { NavigationTreeItem } from "./NavigationTreeItem";
import { Fragment, useEffect, useState } from "react";
import { IconButton } from "./IconButton";
import { DEFAULT_GROUP_TITLE, useViewState } from "src/models/ViewState";
import { useApp } from "src/models/PluginContext";
import { GroupType } from "src/models/VTWorkspace";
import { WorkspaceParent } from "obsidian";

interface GroupProps {
type: VT.GroupType;
group: VT.WorkspaceParent | null;
type: GroupType;
group: WorkspaceParent | null;
children?: React.ReactNode;
}

const titleMap: Record<VT.GroupType, string> = {
[VT.GroupType.LeftSidebar]: "Left sidebar",
[VT.GroupType.RightSidebar]: "Right sidebar",
[VT.GroupType.RootSplit]: DEFAULT_GROUP_TITLE,
const titleMap: Record<GroupType, string> = {
[GroupType.LeftSidebar]: "Left sidebar",
[GroupType.RightSidebar]: "Right sidebar",
[GroupType.RootSplit]: DEFAULT_GROUP_TITLE,
};

export const Group = ({ type, children, group }: GroupProps) => {
const app = useApp();
const workspace = app.workspace as VT.Workspace;
const workspace = app.workspace;
const isSidebar =
type === VT.GroupType.LeftSidebar || type === VT.GroupType.RightSidebar;
type === GroupType.LeftSidebar || type === GroupType.RightSidebar;
const globalCollpaseState = useViewState(
(state) => state.globalCollapseState
);
Expand Down
3 changes: 1 addition & 2 deletions src/components/NavigationContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { REFRESH_TIMEOUT, useTabCache } from "src/models/TabCache";
import { usePlugin, useSettings } from "src/models/PluginContext";
import { useEffect } from "react";
import { useViewState } from "src/models/ViewState";
import * as VT from "src/models/VTWorkspace";
import { debounce } from "obsidian";
import {
moveSelfToDefaultLocation,
Expand Down Expand Up @@ -38,7 +37,7 @@ export const NavigationContainer = () => {
};

useEffect(() => {
const workspace = plugin.app.workspace as VT.Workspace;
const workspace = plugin.app.workspace;
loadSettings(plugin);
autoRefresh();
plugin.registerEvent(workspace.on("layout-change", autoRefresh));
Expand Down
13 changes: 6 additions & 7 deletions src/components/NavigationContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import { useState } from "react";
import { CssClasses, toClassName } from "src/utils/CssClasses";
import { SortableContext } from "@dnd-kit/sortable";
import { createPortal } from "react-dom";
import * as VT from "src/models/VTWorkspace";
import { moveTab, moveTabToEnd, moveTabToNewGroup } from "src/services/MoveTab";
import { TabSlot } from "./TabSlot";
import { GroupSlot } from "./GroupSlot";
import { Identifier } from "src/models/VTWorkspace";

export const NavigationContent = () => {
const { groupIDs, content, swapGroup } = useTabCache();
Expand All @@ -45,8 +45,8 @@ export const NavigationContent = () => {
setIsDraggingGroup(false);
const { active, over } = event;
if (!over) return;
const activeID = active.id as VT.Identifier;
const overID = over.id as VT.Identifier;
const activeID = active.id as Identifier;
const overID = over.id as Identifier;
const isActiveTab = (active.data.current as any).isTab;
const isOverTab = (over.data.current as any).isTab;

Expand All @@ -68,8 +68,7 @@ export const NavigationContent = () => {
if (isOverTab) {
const leaf = app.workspace.getLeafById(overID);
if (!leaf) return;
const parent = leaf.parent as VT.WorkspaceParent;
swapGroup(activeID, parent.id);
swapGroup(activeID, leaf.parent.id);
} else {
swapGroup(activeID, overID);
}
Expand All @@ -87,12 +86,12 @@ export const NavigationContent = () => {

const getGroupIDs = () => [...groupIDs, "slot-new"];

const getLeaveIDs = (groupID: VT.Identifier) => {
const getLeaveIDs = (groupID: Identifier) => {
const group = content.get(groupID);
return [...group.leafIDs, `slot-${groupID}`];
};

const entryOf = (groupID: VT.Identifier) => {
const entryOf = (groupID: Identifier) => {
return content.get(groupID);
};

Expand Down
5 changes: 2 additions & 3 deletions src/components/NavigationHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { IconButton } from "./IconButton";
import { Menu } from "obsidian";
import { sortStrategies, useTabCache } from "src/models/TabCache";
import { useViewState } from "src/models/ViewState";
import * as VT from "../models/VTWorkspace";

export const NavigationHeader = () => {
const plugin = usePlugin();
Expand All @@ -21,7 +20,7 @@ export const NavigationHeader = () => {
);

const createAndShowNewTab = () => {
const workspace = plugin.app.workspace as VT.Workspace;
const workspace = plugin.app.workspace;
const leaf = workspace.getLeaf(true);
workspace.setActiveLeaf(leaf, { focus: true });
workspace.onLayoutChange();
Expand All @@ -30,7 +29,7 @@ export const NavigationHeader = () => {
const toggleZenModeAndLockFocus = () => {
toggleZenMode();
lockFocus(plugin);
const workspace = plugin.app.workspace as VT.Workspace;
const workspace = plugin.app.workspace;
workspace.trigger("vertical-tabs:update-toggle");
};

Expand Down
4 changes: 2 additions & 2 deletions src/components/NavigationTreeItem.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Platform, setIcon } from "obsidian";
import { useEffect, useRef, useState } from "react";
import { CssClasses, toClassName } from "src/utils/CssClasses";
import * as VT from "src/models/VTWorkspace";
import { useSortable } from "@dnd-kit/sortable";
import { IconButton } from "./IconButton";
import { Identifier } from "src/models/VTWorkspace";

interface NavigationTreeItemProps {
id: VT.Identifier | null;
id: Identifier | null;
title: string | React.ReactNode;
icon: string;
isTab: boolean;
Expand Down
7 changes: 3 additions & 4 deletions src/components/Tab.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import * as VT from "src/models/VTWorkspace";
import { NavigationTreeItem } from "./NavigationTreeItem";
import { Fragment } from "react/jsx-runtime";
import { IconButton } from "./IconButton";
import { useEffect, useState } from "react";
import { usePlugin } from "src/models/PluginContext";
import { Menu } from "obsidian";
import { Menu, WorkspaceLeaf } from "obsidian";
import {
closeOthersInGroup,
closeTabsToBottomInGroup,
Expand All @@ -15,7 +14,7 @@ import { useViewState } from "src/models/ViewState";
import { DeduplicatedTitle } from "src/services/DeduplicateTitle";

interface TabProps {
leaf: VT.WorkspaceLeaf;
leaf: WorkspaceLeaf;
}

export const Tab = ({ leaf }: TabProps) => {
Expand Down Expand Up @@ -53,7 +52,7 @@ export const Tab = ({ leaf }: TabProps) => {
};

const openTab = () => {
const workspace = plugin.app.workspace as VT.Workspace;
const workspace = plugin.app.workspace;
workspace.setActiveLeaf(leaf, { focus: true });
workspace.onLayoutChange();
toggleHiddenGroup(leaf.parent.id, false);
Expand Down
4 changes: 2 additions & 2 deletions src/components/TabSlot.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Identifier } from "src/models/VTWorkspace";
import { NavigationTreeItem } from "./NavigationTreeItem";
import * as VT from "src/models/VTWorkspace";

interface TabSlotProps {
groupID: VT.Identifier;
groupID: Identifier;
}

export const TabSlot = ({ groupID }: TabSlotProps) => {
Expand Down
22 changes: 11 additions & 11 deletions src/models/TabCache.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { App } from "obsidian";
import * as VT from "./VTWorkspace";
import { App, WorkspaceLeaf, WorkspaceParent } from "obsidian";
import { create } from "zustand";
import { DefaultRecord } from "src/utils/DefaultRecord";
import { getTabs } from "src/services/GetTabs";
Expand All @@ -10,34 +9,35 @@ import {
SortStrategy,
sortTabs,
} from "src/services/SortTabs";
import { GroupType, Identifier } from "./VTWorkspace";

export type TabCacheEntry = {
groupType: VT.GroupType;
group: VT.WorkspaceParent | null;
leaves: VT.WorkspaceLeaf[];
leafIDs: VT.Identifier[];
groupType: GroupType;
group: WorkspaceParent | null;
leaves: WorkspaceLeaf[];
leafIDs: Identifier[];
};

export const createTabCacheEntry = (): TabCacheEntry => ({
groupType: VT.GroupType.RootSplit,
groupType: GroupType.RootSplit,
group: null,
leaves: [],
leafIDs: [],
});

const factory = () => createTabCacheEntry();

export type TabCache = DefaultRecord<VT.Identifier, TabCacheEntry>;
export type TabCache = DefaultRecord<Identifier, TabCacheEntry>;
export const createNewTabCache = () => new DefaultRecord(factory) as TabCache;

interface TabCacheStore {
content: TabCache;
groupIDs: VT.Identifier[];
leaveIDs: VT.Identifier[];
groupIDs: Identifier[];
leaveIDs: Identifier[];
sortStrategy: SortStrategy | null;
clear: () => void;
refresh: (app: App) => void;
swapGroup: (source: VT.Identifier, target: VT.Identifier) => void;
swapGroup: (source: Identifier, target: Identifier) => void;
setSortStrategy: (strategy: SortStrategy | null) => void;
sort: () => void;
}
Expand Down
71 changes: 33 additions & 38 deletions src/models/VTWorkspace.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import * as Obsidian from "obsidian";

export type Identifier = string;

export enum GroupType {
Expand All @@ -10,45 +8,42 @@ export enum GroupType {

declare module "obsidian" {
interface Workspace {
iterateLeaves(
split: WorkspaceSplit,
callback: (leaf: WorkspaceLeaf) => void
): void;
onLayoutChange: () => void;
getActiveFileView: () => FileView;
leftSidebarToggleButtonEl: HTMLElement;
rightSidebarToggleButtonEl: HTMLElement;
floatingSplit: WorkspaceSplit;
on(name: "vertical-tabs:update-toggle", callback: () => void): EventRef;
}
}

export interface Workspace extends Obsidian.Workspace {
iterateLeaves(
split: Obsidian.WorkspaceSplit,
callback: (leaf: WorkspaceLeaf) => void
): void;
onLayoutChange: () => void;
getActiveFileView: () => Obsidian.FileView;
leftSidebarToggleButtonEl: HTMLElement;
rightSidebarToggleButtonEl: HTMLElement;
floatingSplit: Obsidian.WorkspaceSplit;
}

export interface WorkspaceParent extends Obsidian.WorkspaceParent {
id: Identifier;
containerEl: HTMLElement;
currentTab: number;
children: WorkspaceLeaf[];
selectTab: (leaf: WorkspaceLeaf) => void;
selectTabIndex: (index: number) => void;
recomputeChildrenDimensions: () => void;
isStacked: boolean;
setStacked: (stacked: boolean) => void;
detach: () => void;
tabHeaderContainerEl: HTMLElement;
}
interface WorkspaceParent {
id: Identifier;
containerEl: HTMLElement;
currentTab: number;
children: WorkspaceLeaf[];
selectTab: (leaf: WorkspaceLeaf) => void;
selectTabIndex: (index: number) => void;
recomputeChildrenDimensions: () => void;
isStacked: boolean;
setStacked: (stacked: boolean) => void;
detach: () => void;
tabHeaderContainerEl: HTMLElement;
}

export interface WorkspaceLeaf extends Obsidian.WorkspaceLeaf {
id: Identifier;
activeTime: number;
parent: WorkspaceParent;
setParent: (parent: WorkspaceParent) => void;
tabHeaderEl?: HTMLElement;
tabHeaderInnerTitleEl?: HTMLElement;
}
interface WorkspaceLeaf {
id: Identifier;
activeTime: number;
parent: WorkspaceTabs | WorkspaceMobileDrawer;
setParent: (parent: WorkspaceParent) => void;
tabHeaderEl?: HTMLElement;
tabHeaderInnerTitleEl?: HTMLElement;
}

export interface WorkspaceSidedock extends Obsidian.WorkspaceSidedock {
children: WorkspaceParent[];
interface WorkspaceSidedock extends WorkspaceSplit {
children: WorkspaceLeaf[];
}
}
Loading

0 comments on commit 8561377

Please sign in to comment.