Skip to content

Commit

Permalink
refactor(tabs): extract tab title patching to separate function
Browse files Browse the repository at this point in the history
  • Loading branch information
kdnk committed Dec 25, 2024
1 parent 19cdd71 commit 5af2014
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 58 deletions.
63 changes: 5 additions & 58 deletions src/main.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { App, Plugin, PluginManifest, Platform } from "obsidian";
import { App, Plugin, PluginManifest } from "obsidian";
import {
DEFAULT_SETTINGS,
HierarchySettings,
HierarchyPluginSettingsTab,
} from "./settings";
import { ActiveTabGroup } from "./utils/active-tab-group";
import { patchBacklinks } from "./backlinks/patch-backlinks";
import { patchTabs } from "./tabs/patch-tabs";
import { renderHierarchy } from "./hierarchy-view/render-hierarchy";

export default class HierarchyPlugin extends Plugin {
Expand All @@ -25,7 +25,7 @@ export default class HierarchyPlugin extends Plugin {

this.registerEvent(
this.app.workspace.on("file-open", async (file) => {
this.setTabTitle();
patchTabs(this);
renderHierarchy(this, file);
}),
);
Expand Down Expand Up @@ -63,7 +63,7 @@ export default class HierarchyPlugin extends Plugin {
}

async refresh() {
this.setTabTitle();
patchTabs(this);
renderHierarchy(this);
}

Expand All @@ -77,7 +77,7 @@ export default class HierarchyPlugin extends Plugin {
this.settings.hierarchyForEditors = false;

renderHierarchy(this);
this.setTabTitle();
patchTabs(this);
}

async loadSettings() {
Expand All @@ -90,57 +90,4 @@ export default class HierarchyPlugin extends Plugin {
async saveSettings() {
await this.saveData(this.settings);
}

private setTabTitle() {
if (Platform.isMobile) return;

const activeTabGroup = this.app.workspace.activeTabGroup;
if (!activeTabGroup) return;

const isActiveTabGroup = (
activeTabGroup: unknown,
): activeTabGroup is ActiveTabGroup => {
if (typeof activeTabGroup !== "object") return false;
if (activeTabGroup === null) return false;
if (!("tabHeaderEls" in activeTabGroup)) return false;
if (!("children" in activeTabGroup)) return false;
if (!Array.isArray(activeTabGroup.tabHeaderEls)) return false;
if (
!activeTabGroup.tabHeaderEls.every(
(el) => el instanceof HTMLElement,
)
)
return false;
if (!Array.isArray(activeTabGroup.children)) return false;
if (
!activeTabGroup.children.every(
(child) =>
typeof child === "object" &&
"view" in child &&
"file" in child.view,
)
)
return false;
return true;
};

if (!isActiveTabGroup(activeTabGroup)) return;
const { tabHeaderEls, children } = activeTabGroup;
for (const [index, tabHeaderEl] of tabHeaderEls.entries()) {
const child = children[index];

const titleEl = tabHeaderEl.querySelector(
".workspace-tab-header-inner-title",
) as HTMLElement;
if (titleEl) {
if (this.settings.hierarchyForTabs) {
if (!child.view.file) return;
titleEl.textContent =
children[index].view.file.path.split(".")[0];
} else {
titleEl.textContent = children[index].view.file.basename;
}
}
}
}
}
56 changes: 56 additions & 0 deletions src/tabs/patch-tabs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { Platform } from "obsidian";
import { type ActiveTabGroup } from "../utils/active-tab-group";
import type HierarchyPlugin from "../main";

export function patchTabs(plugin: HierarchyPlugin) {
if (Platform.isMobile) return;

const activeTabGroup = plugin.app.workspace.activeTabGroup;
if (!activeTabGroup) return;

const isActiveTabGroup = (
activeTabGroup: unknown,
): activeTabGroup is ActiveTabGroup => {
if (typeof activeTabGroup !== "object") return false;
if (activeTabGroup === null) return false;
if (!("tabHeaderEls" in activeTabGroup)) return false;
if (!("children" in activeTabGroup)) return false;
if (!Array.isArray(activeTabGroup.tabHeaderEls)) return false;
if (
!activeTabGroup.tabHeaderEls.every(
(el) => el instanceof HTMLElement,
)
)
return false;
if (!Array.isArray(activeTabGroup.children)) return false;
if (
!activeTabGroup.children.every(
(child) =>
typeof child === "object" &&
"view" in child &&
"file" in child.view,
)
)
return false;
return true;
};

if (!isActiveTabGroup(activeTabGroup)) return;
const { tabHeaderEls, children } = activeTabGroup;
for (const [index, tabHeaderEl] of tabHeaderEls.entries()) {
const child = children[index];

const titleEl = tabHeaderEl.querySelector(
".workspace-tab-header-inner-title",
) as HTMLElement;
if (titleEl) {
if (plugin.settings.hierarchyForTabs) {
if (!child.view.file) return;
titleEl.textContent =
children[index].view.file.path.split(".")[0];
} else {
titleEl.textContent = children[index].view.file.basename;
}
}
}
}

0 comments on commit 5af2014

Please sign in to comment.