Hi, thanks for your great plugin, but it doesn't work for me (Windows 11 Pro, Obsidian 1.10.3). I don't come for a bug report but for a solution. ;-)
I can well imagine that you are interested in the solution. You can use it or do anything you want with the code. It works.
const { App, Modal, Plugin, Notice, TFolder } = require("obsidian");
// Schritt 1: Wir definieren eine eigene Klasse für unser Umbenennungs-Fenster.
class RenameModal extends Modal {
constructor(app, file, onSave) {
super(app);
this.file = file;
this.onSave = onSave;
// Analysiert den Dateinamen, um ihn ohne Erweiterung anzuzeigen
const { path } = file;
const isFolder = file instanceof TFolder;
const lastSlash = path.lastIndexOf("/");
const fullName = path.slice(lastSlash + 1);
let displayName = fullName, extension = "";
if (!isFolder) {
const lastDot = fullName.lastIndexOf(".");
if (lastDot > 0 && lastDot < fullName.length - 1) {
displayName = fullName.slice(0, lastDot);
extension = fullName.slice(lastDot);
}
}
this.displayName = displayName;
this.extension = extension;
this.parentPath = lastSlash === -1 ? "" : path.slice(0, lastSlash);
}
// Wird aufgerufen, wenn das Modal geöffnet wird. Hier bauen wir die UI.
onOpen() {
const { contentEl } = this;
contentEl.empty();
contentEl.createEl("h2", { text: `"${this.displayName}" umbenennen` });
// Das Texteingabefeld
const input = contentEl.createEl("input", { type: "text", value: this.displayName });
input.style.width = "100%";
input.focus();
input.select();
// Der "Umbenennen"-Button
const saveButton = contentEl.createEl("button", { text: "Umbenennen" });
saveButton.style.marginTop = "1rem";
// Die Logik, die beim Klick auf den Button ausgeführt wird.
const rename = async () => {
const newDisplayName = input.value.trim().replace(/[\\/]/g, "");
if (newDisplayName === "" || newDisplayName === this.displayName) {
this.close();
return;
}
const newFullName = newDisplayName + this.extension;
const newPath = this.parentPath ? `${this.parentPath}/${newFullName}` : newFullName;
const existingFile = this.app.vault.getAbstractFileByPath(newPath);
if (existingFile && existingFile.path !== this.file.path) {
new Notice(`Fehler: "${newFullName}" existiert bereits.`);
return;
}
try {
await this.app.fileManager.renameFile(this.file, newPath);
this.close();
this.onSave();
} catch (error) {
new Notice(`Umbenennen fehlgeschlagen: ${error.message}`);
}
};
saveButton.addEventListener("click", rename);
// Erlaubt das Speichern mit der Enter-Taste
input.addEventListener("keydown", (e) => {
if (e.key === "Enter") {
e.preventDefault();
rename();
} else if (e.key === "Escape") {
this.close();
}
});
}
// Wird aufgerufen, wenn das Modal geschlossen wird.
onClose() {
const { contentEl } = this;
contentEl.empty();
}
}
// Schritt 2: Die Haupt-Plugin-Klasse wird extrem einfach.
module.exports = class DoubleClickRename extends Plugin {
onload() {
this._dblHandler = (evt) => {
const target = evt.target.closest('.tree-item-self');
if (!target || evt.target.closest('.collapse-icon')) {
return;
}
evt.preventDefault();
evt.stopImmediatePropagation();
const dataPath = target.dataset.path;
if (!dataPath) return;
const file = this.app.vault.getAbstractFileByPath(dataPath);
if (!file) return;
// Die einzige Aufgabe: Das Modal öffnen und ihm die Datei übergeben.
new RenameModal(this.app, file, () => {
// Diese Funktion könnte nach erfolgreicher Umbenennung ausgeführt werden,
// z.B. um den Explorer zu aktualisieren, falls nötig.
}).open();
};
this.app.workspace.onLayoutReady(() => {
const fileExplorer = this.app.workspace.getLeavesOfType("file-explorer")[0];
if (fileExplorer) {
this.registerDomEvent(fileExplorer.view.containerEl, "dblclick", this._dblHandler, true);
}
});
}
};
Hi, thanks for your great plugin, but it doesn't work for me (Windows 11 Pro, Obsidian 1.10.3). I don't come for a bug report but for a solution. ;-)
I can well imagine that you are interested in the solution. You can use it or do anything you want with the code. It works.