Fix rename handler & active folder highlight

This commit is contained in:
Lost Paul 2025-06-18 12:30:25 +02:00
parent 17251a4826
commit d1e2aa1c64
6 changed files with 109 additions and 54 deletions

View file

@ -9,7 +9,7 @@ export async function handleCreate(file: TAbstractFile, plugin: FolderNotesPlugi
const folder = file.parent;
if (folder instanceof TFolder) {
if (plugin.isEmptyFolderNoteFolder(folder)) {
if (plugin.isEmptyFolderNoteFolder(folder) && getFolderNote(plugin, folder.path)) {
addCSSClassToFileExplorerEl(folder.path, 'only-has-folder-note', true, plugin);
} else {
removeCSSClassFromFileExplorerEL(folder.path, 'only-has-folder-note', true, plugin);

View file

@ -1,13 +1,13 @@
import { TAbstractFile, TFolder, TFile } from 'obsidian';
import FolderNotesPlugin from 'src/main';
import { getFolderNote, getFolder, deleteFolderNote } from 'src/functions/folderNoteFunctions';
import { removeCSSClassFromFileExplorerEL, addCSSClassToFileExplorerEl } from 'src/functions/styleFunctions';
import { removeCSSClassFromFileExplorerEL, addCSSClassToFileExplorerEl, hideFolderNoteInFileExplorer } from 'src/functions/styleFunctions';
import { getFolderPathFromString } from 'src/functions/utils';
export function handleDelete(file: TAbstractFile, plugin: FolderNotesPlugin) {
const folder = plugin.app.vault.getAbstractFileByPath(getFolderPathFromString(file.path));
if (folder instanceof TFolder) {
if (plugin.isEmptyFolderNoteFolder(folder)) {
if (plugin.isEmptyFolderNoteFolder(folder) && getFolderNote(plugin, folder.path)) {
addCSSClassToFileExplorerEl(folder.path, 'only-has-folder-note', true, plugin);
} else {
removeCSSClassFromFileExplorerEL(folder.path, 'only-has-folder-note', true, plugin);
@ -21,6 +21,7 @@ export function handleDelete(file: TAbstractFile, plugin: FolderNotesPlugin) {
if (folderNote) { return; }
removeCSSClassFromFileExplorerEL(folder.path, 'has-folder-note', false, plugin);
removeCSSClassFromFileExplorerEL(folder.path, 'only-has-folder-note', true, plugin);
hideFolderNoteInFileExplorer(folder.path, plugin);
}
if (!(file instanceof TFolder)) { return; }

View file

@ -3,15 +3,15 @@ import FolderNotesPlugin from 'src/main';
import { extractFolderName, getFolderNote, getFolderNoteFolder } from '../functions/folderNoteFunctions';
import { getExcludedFolder, addExcludedFolder, updateExcludedFolder, deleteExcludedFolder, getDetachedFolder } from '../ExcludeFolders/functions/folderFunctions';
import { ExcludedFolder } from 'src/ExcludeFolders/ExcludeFolder';
import { removeCSSClassFromFileExplorerEL, addCSSClassToFileExplorerEl } from 'src/functions/styleFunctions';
import { removeCSSClassFromFileExplorerEL, addCSSClassToFileExplorerEl, markFileAsFolderNote, unmarkFileAsFolderNote, unmarkFolderAsFolderNote, markFolderWithFolderNoteClasses, hideFolderNoteInFileExplorer, removeActiveFolder, setActiveFolder } from 'src/functions/styleFunctions';
import { getFolderPathFromString, removeExtension, getFileNameFromPathString } from 'src/functions/utils';
export function handleRename(file: TAbstractFile, oldPath: string, plugin: FolderNotesPlugin) {
const folder = file.parent;
const oldFolder = plugin.app.vault.getAbstractFileByPath(getFolderPathFromString(oldPath));
const isRename = (file.parent?.path === getFolderPathFromString(oldPath));
if (folder instanceof TFolder) {
if (plugin.isEmptyFolderNoteFolder(folder)) {
if (plugin.isEmptyFolderNoteFolder(folder) && getFolderNote(plugin, folder.path)) {
addCSSClassToFileExplorerEl(folder.path, 'only-has-folder-note', true, plugin);
} else {
removeCSSClassFromFileExplorerEL(folder.path, 'only-has-folder-note', true, plugin);
@ -19,7 +19,7 @@ export function handleRename(file: TAbstractFile, oldPath: string, plugin: Folde
}
if (oldFolder instanceof TFolder) {
if (plugin.isEmptyFolderNoteFolder(oldFolder)) {
if (plugin.isEmptyFolderNoteFolder(oldFolder) && getFolderNote(plugin, oldFolder.path)) {
addCSSClassToFileExplorerEl(oldFolder.path, 'only-has-folder-note', true, plugin);
} else {
removeCSSClassFromFileExplorerEL(oldFolder.path, 'only-has-folder-note', true, plugin);
@ -27,22 +27,45 @@ export function handleRename(file: TAbstractFile, oldPath: string, plugin: Folde
}
if (file instanceof TFolder) {
plugin.tabManager.updateTab(file.path);
updateExcludedFolderPath(file, oldPath, plugin);
if (isRename) {
return handleFolderRename(file, oldPath, plugin);
const folder = file;
plugin.tabManager.updateTab(folder.path);
updateExcludedFolderPath(folder, oldPath, plugin);
if (isFolderRename(folder, oldPath)) {
return handleFolderRename(folder, oldPath, plugin);
} else {
return handleFolderMove(file, oldPath, plugin);
return handleFolderMove(folder, oldPath, plugin);
}
} else if (file instanceof TFile) {
if (isRename) {
if (isFileRename(file, oldPath)) {
console.log('File rename detected');
return fmptUpdateFileName(file, oldPath, plugin);
} else {
console.log('File move detected');
return handleFileMove(file, oldPath, plugin);
}
}
}
function isFileRename(file: TFile, oldPath: string): boolean {
const oldFolderPath = getFolderPathFromString(oldPath);
const newFolderPath = file.parent?.path;
const oldName = getFileNameFromPathString(oldPath);
const newName = file.name;
return oldFolderPath === newFolderPath && oldName !== newName;
}
function isFolderRename(folder: TFolder, oldPath: string): boolean {
const oldName = getFileNameFromPathString(oldPath);
const newName = folder.name;
const oldParent = getFolderPathFromString(oldPath);
const newParent = folder.parent?.path;
return oldParent === newParent && oldName !== newName;
}
export function handleFolderMove(file: TFolder, oldPath: string, plugin: FolderNotesPlugin) {
// Soon
@ -54,12 +77,19 @@ export async function handleFileMove(file: TFile, oldPath: string, plugin: Folde
const folderName = extractFolderName(plugin.settings.folderNoteName, file.basename) || file.basename;
const oldFileName = removeExtension(getFileNameFromPathString(oldPath));
const newFolder = getFolderNoteFolder(plugin, file, file.basename);
const folderNote = getFolderNote(plugin, oldPath, plugin.settings.storageLocation, file);
let excludedFolder = getExcludedFolder(plugin, newFolder?.path || '', true);
const oldFolder = getFolderNoteFolder(plugin, oldPath, oldFileName);
const folderNote = getFolderNote(plugin, oldPath, plugin.settings.storageLocation, file);
// file has been moved into position where it can be a folder note!
if (folderName === newFolder?.name && folderNote) {
const isFileNowFolderNoteInNewFolder = folderName === newFolder?.name;
const isFileMovedFromOldFolderNote = oldFolder && oldFolder.name === oldFileName && newFolder?.path !== oldFolder.path;
// this is for turning files into folder notes for folders that already have a folder note
// e.g. Turn into folder note for "Folder name"
const isFileNowFolderNoteWithExistingNote = folderName === newFolder?.name && folderNote;
if (isFileNowFolderNoteWithExistingNote) {
let excludedFolderExisted = true;
let disabledSync = false;
@ -81,12 +111,28 @@ export async function handleFileMove(file: TFile, oldPath: string, plugin: Folde
updateExcludedFolder(plugin, excludedFolder, excludedFolder);
}
});
} else if (oldFolder && oldFolder.name === oldFileName && newFolder?.path !== oldFolder.path) {
// the note has been moved somewhere and is no longer a folder note
// cleanup css on the folder and note
removeCSSClassFromFileExplorerEL(oldFolder.path, 'has-folder-note', false, plugin);
removeCSSClassFromFileExplorerEL(file.path, 'is-folder-note', false, plugin);
removeCSSClassFromFileExplorerEL(oldPath, 'is-folder-note', false, plugin);
} else if (isFileNowFolderNoteInNewFolder) {
if (!excludedFolder?.disableFolderNote) {
markFileAsFolderNote(file, plugin);
if (newFolder instanceof TFolder) {
markFolderWithFolderNoteClasses(newFolder, plugin);
if (plugin.app.workspace.getActiveFile()?.path === file.path) {
removeActiveFolder(plugin);
setActiveFolder(newFolder.path, plugin);
}
}
if (oldFolder instanceof TFolder) {
hideFolderNoteInFileExplorer(oldFolder.path, plugin);
unmarkFolderAsFolderNote(oldFolder, plugin);
}
}
} else if (isFileMovedFromOldFolderNote) {
unmarkFileAsFolderNote(file, plugin);
if (oldFolder instanceof TFolder) {
removeActiveFolder(plugin);
hideFolderNoteInFileExplorer(oldFolder.path, plugin);
unmarkFolderAsFolderNote(oldFolder, plugin);
}
}
}
@ -96,7 +142,6 @@ export async function handleFolderRename(file: TFolder, oldPath: string, plugin:
if (fileName === oldFileName) { return; }
const folderNote = getFolderNote(plugin, oldPath);
if (!(folderNote instanceof TFile)) return;

View file

@ -7,7 +7,7 @@ import { addExcludedFolder, deleteExcludedFolder, getDetachedFolder, getExcluded
import { ExcludedFolder } from '../ExcludeFolders/ExcludeFolder';
import { openExcalidrawView } from './excalidraw';
import { AskForExtensionModal } from 'src/modals/AskForExtension';
import { getFileExplorerElement, addCSSClassToFileExplorerEl, removeCSSClassFromFileExplorerEL } from 'src/functions/styleFunctions';
import { addCSSClassToFileExplorerEl, removeCSSClassFromFileExplorerEL, removeActiveFolder, setActiveFolder } from 'src/functions/styleFunctions';
import { getFolderNameFromPathString, getFolderPathFromString, removeExtension } from 'src/functions/utils';
const defaultExcalidrawTemplate = `---
@ -120,16 +120,12 @@ export async function createFolderNote(plugin: FolderNotesPlugin, folderPath: st
if (openFile) {
if (plugin.app.workspace.getActiveFile()?.path === path) {
if (plugin.activeFolderDom) {
plugin.activeFolderDom.removeClass('fn-is-active');
plugin.activeFolderDom = null;
}
removeActiveFolder(plugin);
const folder = getFolder(plugin, folderNote);
if (!folder) { return; }
plugin.activeFolderDom = getFileExplorerElement(folder.path, plugin);
if (plugin.activeFolderDom) plugin.activeFolderDom.addClass('fn-is-active');
setActiveFolder(folder.path, plugin);
}
await leaf.openFile(folderNote);
if (plugin.settings.folderNoteType === '.excalidraw' || extension === '.excalidraw') {
@ -193,13 +189,8 @@ export async function turnIntoFolderNote(plugin: FolderNotesPlugin, file: TFile,
addCSSClassToFileExplorerEl(path, 'is-folder-note', false, plugin, true);
addCSSClassToFileExplorerEl(folder.path, 'has-folder-note', false, plugin);
if (plugin.activeFolderDom) {
plugin.activeFolderDom.removeClass('fn-is-active');
plugin.activeFolderDom = null;
}
plugin.activeFolderDom = getFileExplorerElement(folder.path, plugin);
if (plugin.activeFolderDom) plugin.activeFolderDom.addClass('fn-is-active');
removeActiveFolder(plugin);
setActiveFolder(folder.path, plugin);
}
export async function tempDisableSync(plugin: FolderNotesPlugin, folder: TFolder): Promise<[excludedFolder: ExcludedFolder | undefined, excludedFolderExisted: boolean, disabledSync: boolean]> {

View file

@ -44,6 +44,7 @@ export async function updateCSSClassesForFolder(folderPath: string, plugin: Fold
if (!folder || !(folder instanceof TFolder)) { return; }
const folderNote = getFolderNote(plugin, folder.path);
// console.log('Updating CSS classes for folder:', folder.path, 'Folder Note:', folderNote);
const detachedFolderNote = getDetachedFolder(plugin, folder.path);
if (folder.children.length === 0) {
@ -68,7 +69,7 @@ export async function updateCSSClassesForFolder(folderPath: string, plugin: Fold
unmarkFileAsFolderNote(folderNote, plugin);
return;
}
if (plugin.isEmptyFolderNoteFolder(folder)) {
if (plugin.isEmptyFolderNoteFolder(folder) && getFolderNote(plugin, folder.path)) {
addCSSClassToFileExplorerEl(folder.path, 'only-has-folder-note', true, plugin);
} else {
removeCSSClassFromFileExplorerEL(folder.path, 'only-has-folder-note', true, plugin);
@ -103,7 +104,7 @@ export function clearFolderAndNoteClasses(folder: TFolder, file: TFile, plugin:
export function markFolderWithFolderNoteClasses(folder: TFolder, plugin: FolderNotesPlugin) {
addCSSClassToFileExplorerEl(folder.path, 'has-folder-note', false, plugin);
if (plugin.isEmptyFolderNoteFolder(folder)) {
if (plugin.isEmptyFolderNoteFolder(folder) && getFolderNote(plugin, folder.path)) {
addCSSClassToFileExplorerEl(folder.path, 'only-has-folder-note', true, plugin);
} else {
removeCSSClassFromFileExplorerEL(folder.path, 'only-has-folder-note', true, plugin);
@ -118,6 +119,11 @@ export function unmarkFileAsFolderNote(file: TFile, plugin: FolderNotesPlugin) {
removeCSSClassFromFileExplorerEL(file.path, 'is-folder-note', false, plugin);
}
export function unmarkFolderAsFolderNote(folder: TFolder, plugin: FolderNotesPlugin) {
removeCSSClassFromFileExplorerEL(folder.path, 'has-folder-note', false, plugin);
removeCSSClassFromFileExplorerEL(folder.path, 'only-has-folder-note', true, plugin);
}
export function clearFolderNoteClassesFromFolder(folder: TFolder, plugin: FolderNotesPlugin) {
removeCSSClassFromFileExplorerEL(folder.path, 'has-folder-note', false, plugin);
removeCSSClassFromFileExplorerEL(folder.path, 'only-has-folder-note', true, plugin);
@ -144,10 +150,10 @@ export async function addCSSClassToFileExplorerEl(path: string, cssClass: string
}
} else {
fileExplorerItem.addClass(cssClass);
document.querySelectorAll(`[data-path='${CSS.escape(path)}']`).forEach((item) => {
item.addClass(cssClass);
});
}
document.querySelectorAll(`[data-path='${CSS.escape(path)}']`).forEach((item) => {
item.addClass(cssClass);
});
}
/**
@ -195,10 +201,26 @@ export function showFolderNoteInFileExplorer(path: string, plugin: FolderNotesPl
updateCSSClassesForFolder(path, plugin);
}
export function hideFolderNoteInFileExplorer(path: string, plugin: FolderNotesPlugin) {
export function hideFolderNoteInFileExplorer(folderPath: string, plugin: FolderNotesPlugin) {
plugin.settings.excludeFolders = plugin.settings.excludeFolders.filter(
(folder) => (folder.path !== path) && folder.showFolderNote);
(folder) => (folder.path !== folderPath) && folder.showFolderNote);
plugin.saveSettings(false);
removeCSSClassFromFileExplorerEL(path, 'show-folder-note-in-explorer', true, plugin);
updateCSSClassesForFolder(path, plugin);
removeCSSClassFromFileExplorerEL(folderPath, 'show-folder-note-in-explorer', true, plugin);
updateCSSClassesForFolder(folderPath, plugin);
}
export function setActiveFolder(folderPath: string, plugin: FolderNotesPlugin) {
const fileExplorerItem = getFileExplorerElement(folderPath, plugin);
if (fileExplorerItem) {
fileExplorerItem.addClass('fn-is-active');
plugin.activeFolderDom = fileExplorerItem;
}
}
export function removeActiveFolder(plugin: FolderNotesPlugin) {
if (plugin.activeFolderDom) {
plugin.activeFolderDom.removeClass('fn-is-active');
plugin.activeFolderDom?.removeClass('has-focus');
plugin.activeFolderDom = null;
}
}

View file

@ -13,7 +13,7 @@ import { FolderOverview } from './obsidian-folder-overview/src/FolderOverview';
import { TabManager } from './events/TabManager';
import './functions/ListComponent';
import { handleDelete } from './events/handleDelete';
import { addCSSClassToFileExplorerEl, getFileExplorerElement, removeCSSClassFromFileExplorerEL, refreshAllFolderStyles } from './functions/styleFunctions';
import { addCSSClassToFileExplorerEl, getFileExplorerElement, removeCSSClassFromFileExplorerEL, refreshAllFolderStyles, setActiveFolder, removeActiveFolder } from './functions/styleFunctions';
import { getExcludedFolder } from './ExcludeFolders/functions/folderFunctions';
import { FileExplorerView, InternalPlugin } from 'obsidian-typings';
import { getFocusedItem } from './functions/utils';
@ -109,10 +109,7 @@ export default class FolderNotesPlugin extends Plugin {
}));
this.registerEvent(this.app.workspace.on('file-open', async (openFile: TFile | null) => {
if (this.activeFolderDom) {
this.activeFolderDom.removeClass('fn-is-active');
this.activeFolderDom = null;
}
removeActiveFolder(this);
if (!openFile || !openFile.basename) { return; }
@ -123,8 +120,7 @@ export default class FolderNotesPlugin extends Plugin {
const folderNote = getFolderNote(this, folder.path);
if (!folderNote) { return; }
if (folderNote.path !== openFile.path) { return; }
this.activeFolderDom = getFileExplorerElement(folder.path, this);
if (this.activeFolderDom) this.activeFolderDom.addClass('fn-is-active');
setActiveFolder(folder.path, this);
}));
this.registerEvent(this.app.vault.on('rename', (file: TAbstractFile, oldPath: string) => {
@ -429,7 +425,7 @@ export default class FolderNotesPlugin extends Plugin {
document.body.classList.remove('folder-note-underline');
document.body.classList.remove('hide-folder-note');
document.body.classList.remove('fn-whitespace-stop-collapsing');
if (this.activeFolderDom) { this.activeFolderDom.removeClass('is-active'); }
removeActiveFolder(this);
if (this.fmtpHandler) {
this.fmtpHandler.deleteEvent();
}