mirror of
https://github.com/lostpaul/obsidian-folder-notes.git
synced 2026-07-22 07:40:24 +00:00
Fix fmtp changing names in the explorer
This commit is contained in:
parent
84d8c608fd
commit
095e275ef8
3 changed files with 74 additions and 45 deletions
|
|
@ -41,9 +41,9 @@ export class FrontMatterTitlePluginHandler {
|
|||
if (ref) {
|
||||
this.eventRef = ref;
|
||||
}
|
||||
this.plugin.app.vault.getFiles().forEach((file) => {
|
||||
this.handleRename({ id: '', result: false, path: file.path }, false);
|
||||
});
|
||||
// this.plugin.app.vault.getFiles().forEach((file) => {
|
||||
// this.handleRename({ id: '', result: false, path: file.path }, false);
|
||||
// });
|
||||
this.plugin.updateBreadcrumbs();
|
||||
})();
|
||||
}
|
||||
|
|
@ -84,4 +84,28 @@ export class FrontMatterTitlePluginHandler {
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
async handleRenameFolder(data: {
|
||||
id: string;
|
||||
result: boolean;
|
||||
path: string;
|
||||
}, isEvent: boolean) {
|
||||
if ((data as any).data) data = (data as any).data;
|
||||
const folder = this.app.vault.getAbstractFileByPath(data.path);
|
||||
if (!(folder instanceof TFolder)) { return; }
|
||||
const folderNote = getFolderNote(this.plugin, folder.path);
|
||||
if (!folderNote) { return; }
|
||||
|
||||
const resolver = this.api?.getResolverFactory()?.createResolver('#feature-id#');
|
||||
const newName = resolver?.resolve(folderNote?.path ?? '');
|
||||
if (!newName) return;
|
||||
|
||||
if (isEvent) {
|
||||
this.plugin.changeName(folder, newName, true);
|
||||
} else {
|
||||
this.plugin.changeName(folder, newName, false);
|
||||
}
|
||||
folder.newName = newName;
|
||||
this.modifiedFolders.set(folder.path, folder);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
import { TFile, Keymap, Platform } from 'obsidian';
|
||||
import { Keymap, Platform } from 'obsidian';
|
||||
import FolderNotesPlugin from 'src/main';
|
||||
import { getFolderNote } from 'src/functions/folderNoteFunctions';
|
||||
import { handleFolderClick, handleViewHeaderClick } from './handleClick';
|
||||
|
|
@ -11,21 +11,21 @@ export function registerFileExplorerObserver(plugin: FolderNotesPlugin) {
|
|||
// Run once on initial layout
|
||||
plugin.app.workspace.onLayoutReady(() => {
|
||||
initializeFolderNoteFeatures(plugin);
|
||||
initializeBreadcrumbs(plugin);
|
||||
});
|
||||
|
||||
// Re-run when layout changes (e.g. File Explorer is reopened)
|
||||
plugin.registerEvent(
|
||||
plugin.app.workspace.on('layout-change', () => {
|
||||
initializeFolderNoteFeatures(plugin);
|
||||
})
|
||||
);
|
||||
|
||||
// Also listen for file-open events (once)
|
||||
plugin.registerEvent(
|
||||
plugin.app.workspace.on('file-open', (file) => {
|
||||
if (file instanceof TFile) {
|
||||
scheduleIdle(() => updateBreadcrumbs(plugin), { timeout: 1000 });
|
||||
}
|
||||
const activeLeaf = plugin.app.workspace.getActiveFileView()?.containerEl;
|
||||
if (!activeLeaf) return;
|
||||
|
||||
const titleContainer = activeLeaf.querySelector('.view-header-title-container');
|
||||
if (!(titleContainer instanceof HTMLElement)) return;
|
||||
|
||||
updateBreadcrumbs(plugin, titleContainer);
|
||||
})
|
||||
);
|
||||
}
|
||||
|
|
@ -43,8 +43,15 @@ function initializeFolderNoteFeatures(plugin: FolderNotesPlugin) {
|
|||
|
||||
initializeAllFolderTitles(explorer, plugin);
|
||||
observeFolderTitleMutations(explorer, plugin);
|
||||
}
|
||||
|
||||
explorer.addEventListener('click', (e) => handleBreadcrumbClick((e as MouseEvent), plugin), true);
|
||||
function initializeBreadcrumbs(plugin: FolderNotesPlugin) {
|
||||
const titleContainers = document.querySelectorAll('.view-header-title-container');
|
||||
if (!titleContainers.length) return;
|
||||
titleContainers.forEach((container) => {
|
||||
if (!(container instanceof HTMLElement)) return;
|
||||
scheduleIdle(() => updateBreadcrumbs(plugin, container), { timeout: 1000 });
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -75,7 +82,9 @@ function initializeAllFolderTitles(container: Element, plugin: FolderNotesPlugin
|
|||
if (!folderEl) continue;
|
||||
|
||||
const folderPath = folderEl.getAttribute('data-path') || '';
|
||||
setupFolderTitle(folderTitle, plugin, folderPath);
|
||||
setTimeout(() => {
|
||||
setupFolderTitle(folderTitle, plugin, folderPath);
|
||||
}, 1000);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -113,6 +122,10 @@ async function setupFolderTitle(folderTitle: HTMLElement, plugin: FolderNotesPlu
|
|||
folderTitle.dataset.initialized = 'true';
|
||||
await applyCSSClassesToFolder(folderPath, plugin);
|
||||
|
||||
if (plugin.settings.frontMatterTitle.enabled) {
|
||||
plugin.fmtpHandler?.handleRenameFolder({ id: '', result: false, path: folderPath }, false);
|
||||
}
|
||||
|
||||
folderTitle.addEventListener('auxclick', (event: MouseEvent) => {
|
||||
if (event.button === 1) handleFolderClick(event, plugin);
|
||||
}, { capture: true });
|
||||
|
|
@ -147,42 +160,32 @@ async function setupFolderTitle(folderTitle: HTMLElement, plugin: FolderNotesPlu
|
|||
});
|
||||
}
|
||||
|
||||
async function updateBreadcrumbs(plugin: FolderNotesPlugin) {
|
||||
const headers = document.querySelectorAll('span.view-header-breadcrumb');
|
||||
async function updateBreadcrumbs(plugin: FolderNotesPlugin, titleContainer: HTMLElement) {
|
||||
const headers = titleContainer.querySelectorAll('span.view-header-breadcrumb');
|
||||
let path = '';
|
||||
headers.forEach(async (breadcrumb: HTMLElement) => {
|
||||
let path = '';
|
||||
const allCrumbs = breadcrumb.parentElement?.querySelectorAll('span.view-header-breadcrumb');
|
||||
if (!allCrumbs) return;
|
||||
|
||||
for (const crumb of Array.from(allCrumbs)) {
|
||||
path += crumb.getAttribute('old-name') ?? (crumb as HTMLElement).innerText.trim();
|
||||
path += '/';
|
||||
const folderPath = path.slice(0, -1);
|
||||
const folder = plugin.fmtpHandler?.modifiedFolders.get(folderPath);
|
||||
if (folder && plugin.settings.frontMatterTitle.path && plugin.settings.frontMatterTitle.enabled) {
|
||||
crumb.setAttribute('old-name', folder.name || '');
|
||||
(crumb as HTMLElement).innerText = folder.newName || '';
|
||||
}
|
||||
const excludedFolder = getExcludedFolder(plugin, folderPath, true);
|
||||
if (excludedFolder?.disableFolderNote) return;
|
||||
const folderNote = getFolderNote(plugin, folderPath);
|
||||
if (folderNote) crumb.classList.add('has-folder-note');
|
||||
path += breadcrumb.getAttribute('old-name') ?? (breadcrumb as HTMLElement).innerText.trim();
|
||||
path += '/';
|
||||
const folderPath = path.slice(0, -1);
|
||||
if (plugin.settings.frontMatterTitle.enabled) {
|
||||
plugin.fmtpHandler?.handleRenameFolder({ id: '', result: false, path: folderPath }, false);
|
||||
}
|
||||
|
||||
breadcrumb.parentElement?.setAttribute('data-path', path.slice(0, -1));
|
||||
const excludedFolder = getExcludedFolder(plugin, folderPath, true);
|
||||
if (excludedFolder?.disableFolderNote) return;
|
||||
const folderNote = getFolderNote(plugin, folderPath);
|
||||
if (folderNote) breadcrumb.classList.add('has-folder-note');
|
||||
|
||||
|
||||
breadcrumb?.setAttribute('data-path', path.slice(0, -1));
|
||||
if (!breadcrumb.onclick) {
|
||||
breadcrumb.addEventListener('click', (e) => {
|
||||
handleViewHeaderClick(e as MouseEvent, plugin);
|
||||
}, { capture: true });
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function handleBreadcrumbClick(event: MouseEvent, plugin: FolderNotesPlugin) {
|
||||
const target = event.target as HTMLElement;
|
||||
const breadcrumb = target.closest('span.view-header-breadcrumb') as HTMLElement;
|
||||
if (!breadcrumb || breadcrumb.onclick) return;
|
||||
|
||||
breadcrumb.addEventListener('click', (e) => {
|
||||
handleViewHeaderClick(e, plugin);
|
||||
}, { capture: true });
|
||||
}
|
||||
|
||||
// Schedules a callback to run when the browser is idle, or after a timeout as a fallback.
|
||||
// - callback: The function to execute when idle or after the timeout.
|
||||
// - options: Optional object with a 'timeout' property (in milliseconds).
|
||||
|
|
|
|||
|
|
@ -429,7 +429,7 @@ export async function renderGeneral(settingsTab: SettingsTab) {
|
|||
' with folder notes. It allows you to set the folder name to some name you set in the front matter.',
|
||||
);
|
||||
|
||||
new Setting(containerEl)
|
||||
const fmtpSetting = new Setting(containerEl)
|
||||
.setName('Enable front matter title plugin integration')
|
||||
.setDesc(desc1)
|
||||
.addToggle((toggle) =>
|
||||
|
|
@ -453,6 +453,8 @@ export async function renderGeneral(settingsTab: SettingsTab) {
|
|||
settingsTab.display();
|
||||
})
|
||||
);
|
||||
fmtpSetting.infoEl.appendText('Requires a restart to take effect');
|
||||
fmtpSetting.infoEl.style.color = settingsTab.app.vault.getConfig('accentColor') as string || '#7d5bed';
|
||||
|
||||
settingsTab.settingsPage.createEl('h3', { text: 'Session & Persistence' });
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue