feat: add open folder in file manager option in context menu

This commit is contained in:
XuQuan-nikkkki 2025-12-07 16:07:00 +08:00
parent 764348c093
commit 6e7dd60a94
2 changed files with 34 additions and 4 deletions

View file

@ -1,6 +1,6 @@
import classNames from "classnames";
import { shell } from "electron";
import { Menu, normalizePath, TFile } from "obsidian";
import { FileSystemAdapter, Menu, normalizePath, TFile } from "obsidian";
import { useRef } from "react";
import { useShallow } from "zustand/react/shallow";
@ -129,14 +129,16 @@ const FileContent = ({ file }: FileProps) => {
} else {
return isWin ? "在资源管理器中显示" : "在访达中显示";
}
}
};
const openInFileManager = (menu: Menu) => {
addMenuItem(menu, {
title: _getOpenInFileManagerTitle(),
icon: "move-up-right",
action: () => {
const vaultPath = plugin.app.vault.adapter.getBasePath();
const vaultPath = (
plugin.app.vault.adapter as FileSystemAdapter
).getBasePath();
const abs = normalizePath(`${vaultPath}/${file.path}`);
shell.showItemInFolder(abs);
},

View file

@ -1,4 +1,5 @@
import { Menu, TFolder } from "obsidian";
import { shell } from "electron";
import { FileSystemAdapter, Menu, normalizePath, TFolder } from "obsidian";
import { useRef } from "react";
import { useShallow } from "zustand/react/shallow";
@ -85,6 +86,30 @@ const FolderContent = ({ folder }: Props) => {
});
};
const _getOpenInFileManagerTitle = () => {
const isEn = language == "en";
const isWin = process.platform === "win32";
if (isEn) {
return isWin ? "Show in Explorer" : "Reveal in Finder";
} else {
return isWin ? "在资源管理器中显示" : "在访达中显示";
}
};
const openFolderInFileManager = (menu: Menu) => {
addMenuItem(menu, {
title: _getOpenInFileManagerTitle(),
icon: "move-up-right",
action: () => {
const vaultPath = (
plugin.app.vault.adapter as FileSystemAdapter
).getBasePath();
const abs = normalizePath(`${vaultPath}/${folder.path}`);
shell.showItemInFolder(abs);
},
});
};
const onShowContextMenu = (e: React.MouseEvent<HTMLDivElement>) => {
e.preventDefault();
e.stopPropagation();
@ -105,6 +130,9 @@ const FolderContent = ({ folder }: Props) => {
addMoveMenuItem(menu, onShowTargetFoldersList, isRootFolder);
menu.addSeparator();
openFolderInFileManager(menu);
menu.addSeparator();
addRenameMenuItem(
menu,
nameRef?.current?.onStartEditingName ?? noop,