feat: add auto reveal option

This commit is contained in:
d7sd6u 2025-05-29 01:49:52 +04:00
parent 8ffb970a54
commit 30dfc0acaf
8 changed files with 87 additions and 17 deletions

3
.gitmodules vendored
View file

@ -1,3 +1,6 @@
[submodule "obsidian-typings"] [submodule "obsidian-typings"]
path = obsidian-typings path = obsidian-typings
url = git@github.com:d7sd6u/obsidian-typings.git url = git@github.com:d7sd6u/obsidian-typings.git
[submodule "obsidian-reusables"]
path = obsidian-reusables
url = ssh://git@github.com/d7sd6u/obsidian-reusables.git

View file

@ -1,6 +1,6 @@
# Reveal Folded # Reveal Folded
Reveal the current file in the file explorer while collapsing all other tree items. Reveal the current file in the file explorer while collapsing all other tree items. Has an auto-reveal option for the desktop Obsidian - refocuses active file whenever you move around your vault.
https://github.com/user-attachments/assets/7169035f-361e-4e6f-91cf-99dbd210a1e9 https://github.com/user-attachments/assets/7169035f-361e-4e6f-91cf-99dbd210a1e9

1
obsidian-reusables Submodule

@ -0,0 +1 @@
Subproject commit ead18b5b7d58d16c8083f213915da93ba8ef2873

View file

@ -28,5 +28,7 @@
"typescript": "^5.7.3", "typescript": "^5.7.3",
"typescript-eslint": "^8.24.1" "typescript-eslint": "^8.24.1"
}, },
"dependencies": {} "dependencies": {
"monkey-around": "^3.0.0"
}
} }

View file

@ -4,6 +4,11 @@ settings:
autoInstallPeers: true autoInstallPeers: true
excludeLinksFromLockfile: false excludeLinksFromLockfile: false
dependencies:
monkey-around:
specifier: ^3.0.0
version: 3.0.0
devDependencies: devDependencies:
'@eslint/js': '@eslint/js':
specifier: ^9.20.0 specifier: ^9.20.0
@ -976,6 +981,10 @@ packages:
resolution: {integrity: sha512-5LC9SOxjSc2HF6vO2CyuTDNivEdoz2IvyJJGj6X8DJ0eFyfszE0QiEd+iXmBvUP3WHxSjFH/vIsA0EN00cgr8w==} resolution: {integrity: sha512-5LC9SOxjSc2HF6vO2CyuTDNivEdoz2IvyJJGj6X8DJ0eFyfszE0QiEd+iXmBvUP3WHxSjFH/vIsA0EN00cgr8w==}
dev: true dev: true
/monkey-around@3.0.0:
resolution: {integrity: sha512-jL6uY2lEAoaHxZep1cNRkCZjoIWY4g5VYCjriEWmcyHU7w8NU1+JH57xE251UVTohK0lCxMjv0ZV4ByDLIXEpw==}
dev: false
/ms@2.1.3: /ms@2.1.3:
resolution: {integrity: sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==} resolution: {integrity: sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==}
dev: true dev: true

View file

@ -1,22 +1,33 @@
import { Plugin } from "obsidian"; import { Platform, TFile } from "obsidian";
import PluginWithSettings from "obsidian-reusables/src/PluginWithSettings";
import { DEFAULT_SETTINGS, MainPluginSettingsTab } from "./settings";
export default class RevelFolded extends Plugin { export default class RevelFolded extends PluginWithSettings(DEFAULT_SETTINGS) {
override onload() { override async onload() {
await this.initSettings(MainPluginSettingsTab);
this.addCommand({ this.addCommand({
id: "reveal-active-file-folded", id: "reveal-active-file-folded",
name: "Reveal active file in folded file explorer", name: "Reveal active file in folded file explorer",
icon: "folder-search", icon: "folder-search",
checkCallback: (checking) => { checkCallback: (checking) => {
const hasActiveFile = !!this.app.workspace.getActiveFile(); const activeFile = this.app.workspace.getActiveFile();
if (!checking && hasActiveFile) this.doCommand(); if (!checking && activeFile) this.doCommand(activeFile);
return hasActiveFile; return !!activeFile;
}, },
}); });
if (Platform.isDesktop)
this.app.workspace.on("file-open", (file) => {
const hasActiveFile = this.app.workspace.getActiveFile();
if (hasActiveFile && file && this.settings.autoReveal)
this.doCommand(file);
});
} }
private doCommand() { private doCommand(file: TFile) {
for (const leave of this.app.workspace.getLeavesOfType( for (const leave of this.app.workspace.getLeavesOfType(
"file-explorer", "file-explorer",
)) { )) {
@ -31,12 +42,13 @@ export default class RevelFolded extends Plugin {
leave.view.tree.isAllCollapsed = false; leave.view.tree.isAllCollapsed = false;
} }
}, 0); }, 0);
setTimeout( setTimeout(() => {
() => this.app.commands.executeCommandById(
this.app.commands.executeCommandById( "file-explorer:reveal-active-file",
"file-explorer:reveal-active-file", );
), setTimeout(() => {
100, void this.app.workspace.getLeaf().openFile(file);
); }, 50);
}, 100);
} }
} }

43
src/settings.ts Normal file
View file

@ -0,0 +1,43 @@
import { App, PluginSettingTab, Setting } from "obsidian";
import Main from "./main";
import { FolderSuggest } from "../obsidian-reusables/src/FolderSuggest";
export const DEFAULT_SETTINGS = {
autoReveal: false,
};
export class MainPluginSettingsTab extends PluginSettingTab {
constructor(
app: App,
override plugin: Main,
) {
super(app, plugin);
this.plugin = plugin;
}
suggest?: FolderSuggest;
display() {
const { containerEl } = this;
containerEl.empty();
const options = Object.fromEntries(
this.app.vault.getAllFolders().map((v) => [v.path, v.path]),
);
options["/"] = "/";
const setAutoReveal = async (v: boolean) => {
this.plugin.settings.autoReveal = v;
await this.plugin.saveSettings();
};
new Setting(containerEl)
.setName("Auto reveal")
.setDesc(
"Automatically reveal your current file (is disabled on mobile)",
)
.addToggle((toggle) => {
toggle
.setValue(this.plugin.settings.autoReveal)
.onChange(setAutoReveal);
});
}
}

View file

@ -8,7 +8,7 @@
"target": "ES6", "target": "ES6",
"allowJs": true, "allowJs": true,
"moduleResolution": "node", "moduleResolution": "node",
"importHelpers": true, "importHelpers": false,
"isolatedModules": true, "isolatedModules": true,
"noEmit": true, "noEmit": true,
"allowImportingTsExtensions": true, "allowImportingTsExtensions": true,