From 6e7dd60a94a6359779b975f4d21641a216f1fe95 Mon Sep 17 00:00:00 2001 From: XuQuan-nikkkki Date: Sun, 7 Dec 2025 16:07:00 +0800 Subject: [PATCH] feat: add open folder in file manager option in context menu --- src/components/File/Content.tsx | 8 +++++--- src/components/Folder/Content.tsx | 30 +++++++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/src/components/File/Content.tsx b/src/components/File/Content.tsx index ff43bef..ca13dc9 100644 --- a/src/components/File/Content.tsx +++ b/src/components/File/Content.tsx @@ -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); }, diff --git a/src/components/Folder/Content.tsx b/src/components/Folder/Content.tsx index c22ab88..6361956 100644 --- a/src/components/Folder/Content.tsx +++ b/src/components/Folder/Content.tsx @@ -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) => { 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,