Skip to content

Commit

Permalink
auto refresh web viewer title
Browse files Browse the repository at this point in the history
  • Loading branch information
oxdc committed Dec 23, 2024
1 parent 4dd702e commit d608d38
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
21 changes: 17 additions & 4 deletions src/components/Tab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Fragment } from "react/jsx-runtime";
import { IconButton } from "./IconButton";
import { useEffect, useState } from "react";
import { usePlugin, useSettings } from "src/models/PluginContext";
import { Menu, Platform, Webview, WorkspaceLeaf } from "obsidian";
import { Menu, Platform, WebviewView, WorkspaceLeaf } from "obsidian";
import {
closeOthersInGroup,
closeTabsToBottomInGroup,
Expand Down Expand Up @@ -47,6 +47,7 @@ export const Tab = ({ leaf }: TabProps) => {
const lastActiveLeaf = useViewState((state) => state.latestActiveLeaf);
const enableTabZoom = useSettings((state) => state.enableTabZoom);
const alwaysOpenInNewTab = useSettings((state) => state.alwaysOpenInNewTab);
const [title, setTitle] = useState(DeduplicatedTitle(app, leaf));
const isWebViewer = leaf.view.getViewType() === "webviewer";

const changePinnedState = (pinned: boolean) => {
Expand Down Expand Up @@ -309,14 +310,26 @@ export const Tab = ({ leaf }: TabProps) => {
});
});
}
if (isWebViewer) {
const webview = leaf.view as Webview;
if (Platform.isDesktop && isWebViewer) {
const webview = leaf.view as WebviewView;
webview.webview.addEventListener(
"page-title-updated",
(title: { title: string }) => setTitle(title.title)
);
menu.addSeparator();
menu.addItem((item) => {
item.setSection("webview")
.setTitle("Toggle reader mode")
.onClick(() => webview.toggleReaderMode());
});
menu.addItem((item) => {
item.setSection("webview")
.setTitle("Save to vault")
.onClick(async () => {
const file = await webview.saveAsMarkdown();
if (file) workspace.getLeaf("tab").openFile(file);
});
});
menu.addSeparator();
menu.addItem((item) => {
item.setSection("zoom").setTitle("Zoom");
Expand Down Expand Up @@ -370,7 +383,6 @@ export const Tab = ({ leaf }: TabProps) => {
);

const props = {
title: DeduplicatedTitle(app, leaf),
icon: leaf.getIcon(),
isActive: leaf.tabHeaderEl?.classList.contains("is-active"),
};
Expand All @@ -385,6 +397,7 @@ export const Tab = ({ leaf }: TabProps) => {
return (
<NavigationTreeItem
id={leaf.id}
title={title}
isTab={true}
isEphemeralTab={isEphemeral && !isPinned}
isPinned={isPinned}
Expand Down
6 changes: 5 additions & 1 deletion src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,14 @@ declare module "obsidian" {
section?: string;
}

interface Webview extends ItemView {
interface WebviewView extends ItemView {
toggleReaderMode: () => void;
saveAsMarkdown: () => Promise<TFile | null>;
zoomIn: () => void;
zoomOut: () => void;
zoomReset: () => void;
webview: {
addEventListener: (type: string, listener: unknown) => void;
};
}
}

0 comments on commit d608d38

Please sign in to comment.