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

This commit is contained in:
XuQuan-nikkkki 2025-12-07 16:02:13 +08:00
parent 27eafca800
commit 764348c093
3 changed files with 39 additions and 1 deletions

11
package-lock.json generated
View file

@ -20,6 +20,7 @@
"zustand": "^5.0.3"
},
"devDependencies": {
"@types/electron": "^1.4.38",
"@types/node": "^16.11.6",
"@types/react": "^19.0.4",
"@types/react-dom": "^19.0.2",
@ -671,6 +672,16 @@
"@types/tern": "*"
}
},
"node_modules/@types/electron": {
"version": "1.4.38",
"resolved": "https://registry.npmjs.org/@types/electron/-/electron-1.4.38.tgz",
"integrity": "sha512-Cu6laqBamT6VSPi0LLlF9vE9Os8EbTaI/5eJSsd7CPoLUG3Znjh04u9TxMhQYPF1wGFM14Z8TFQ2914JZ+rGLg==",
"dev": true,
"license": "MIT",
"dependencies": {
"@types/node": "*"
}
},
"node_modules/@types/estree": {
"version": "1.0.6",
"resolved": "https://registry.npmmirror.com/@types/estree/-/estree-1.0.6.tgz",

View file

@ -16,6 +16,7 @@
"author": "",
"license": "MIT",
"devDependencies": {
"@types/electron": "^1.4.38",
"@types/node": "^16.11.6",
"@types/react": "^19.0.4",
"@types/react-dom": "^19.0.2",

View file

@ -1,5 +1,6 @@
import classNames from "classnames";
import { Menu, TFile } from "obsidian";
import { shell } from "electron";
import { Menu, normalizePath, TFile } from "obsidian";
import { useRef } from "react";
import { useShallow } from "zustand/react/shallow";
@ -120,6 +121,28 @@ const FileContent = ({ file }: FileProps) => {
await createFileWithDefaultName(folder);
};
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 openInFileManager = (menu: Menu) => {
addMenuItem(menu, {
title: _getOpenInFileManagerTitle(),
icon: "move-up-right",
action: () => {
const vaultPath = plugin.app.vault.adapter.getBasePath();
const abs = normalizePath(`${vaultPath}/${file.path}`);
shell.showItemInFolder(abs);
},
});
};
const onShowContextMenu = (e: React.MouseEvent<HTMLDivElement>) => {
e.preventDefault();
e.stopPropagation();
@ -136,6 +159,9 @@ const FileContent = ({ file }: FileProps) => {
addMoveMenuItem(menu, onShowTargetFoldersList);
menu.addSeparator();
openInFileManager(menu);
menu.addSeparator();
addRenameMenuItem(menu, nameRef.current?.onStartEditingName ?? noop);
addDeleteMenuItem(menu, () => trashFile(file));