mirror of
https://github.com/lostpaul/obsidian-folder-notes.git
synced 2026-07-22 07:40:24 +00:00
Store folder note in parent folder option
This commit is contained in:
parent
a05e7b03ae
commit
39a9f6dffe
4 changed files with 128 additions and 41 deletions
|
|
@ -1,6 +1,6 @@
|
|||
import { TFile, TFolder, TAbstractFile, Notice } from 'obsidian';
|
||||
import FolderNotesPlugin from 'src/main';
|
||||
import { extractFolderName } from '../folderNoteFunctions';
|
||||
import { extractFolderName, getFolderNote } from '../folderNoteFunctions';
|
||||
export function handleFolderRename(file: TFolder, oldPath: string, plugin: FolderNotesPlugin) {
|
||||
const oldFileName = plugin.getFileNameFromPathString(oldPath);
|
||||
const folder = plugin.app.vault.getAbstractFileByPath(file.path);
|
||||
|
|
@ -24,23 +24,14 @@ export function handleFolderRename(file: TFolder, oldPath: string, plugin: Folde
|
|||
});
|
||||
plugin.saveSettings();
|
||||
const excludedFolder = plugin.getExcludedFolderByPath(file.path);
|
||||
|
||||
const folderNotePath = oldPath + '/' + plugin.settings.folderNoteName.replace('{{folder_name}}', oldFileName) + '.md';
|
||||
let note = plugin.app.vault.getAbstractFileByPath(folderNotePath) || plugin.app.vault.getAbstractFileByPath(folderNotePath.slice(0, -3) + '.canvas');
|
||||
if (!(note instanceof TFile)) {
|
||||
note = plugin.app.vault.getAbstractFileByPath(`${oldPath}/${oldFileName}.md`) || plugin.app.vault.getAbstractFileByPath(`${oldPath}/${oldFileName}.canvas`);
|
||||
if (!(note instanceof TFile)) { return; }
|
||||
note.path = folder.path + '/' + oldFileName + plugin.getExtensionFromPathString(note.path);
|
||||
} else {
|
||||
note.path = folder.path + '/' + plugin.settings.folderNoteName.replace('{{folder_name}}', oldFileName) + plugin.getExtensionFromPathString(note.path);
|
||||
}
|
||||
|
||||
const newPath = folder.path + '/' + plugin.settings.folderNoteName.replace('{{folder_name}}', folder.name) + plugin.getExtensionFromPathString(note.path);
|
||||
if (excludedFolder?.disableSync && !plugin.app.vault.getAbstractFileByPath(newPath)) {
|
||||
const folderNote = getFolderNote(plugin, oldPath);
|
||||
if (excludedFolder?.disableSync && !folderNote) {
|
||||
return plugin.removeCSSClassFromEL(file.path, 'has-folder-note');
|
||||
}
|
||||
|
||||
plugin.app.vault.rename(note, newPath);
|
||||
if (!(folderNote instanceof TFile)) return;
|
||||
const newPath = folder.path + '/' + plugin.settings.folderNoteName.replace('{{folder_name}}', folder.name) + '.' + folderNote.extension;
|
||||
plugin.app.vault.rename(folderNote, newPath);
|
||||
}
|
||||
|
||||
export function handleFileRename(file: TFile, oldPath: string, plugin: FolderNotesPlugin) {
|
||||
|
|
|
|||
|
|
@ -8,7 +8,17 @@ export async function createFolderNote(plugin: FolderNotesPlugin, folderPath: st
|
|||
const leaf = plugin.app.workspace.getLeaf(false);
|
||||
const folderName = plugin.getFolderNameFromPathString(folderPath);
|
||||
const fileName = plugin.settings.folderNoteName.replace('{{folder_name}}', folderName);
|
||||
const path = `${folderPath}/${fileName}${plugin.settings.folderNoteType}`;
|
||||
let path = `${folderPath}/${fileName}${plugin.settings.folderNoteType}`;
|
||||
if (plugin.settings.storageLocation === 'parentFolder') {
|
||||
const parentFolderPath = plugin.getParentFolderPathFromPathString(folderPath);
|
||||
if (parentFolderPath.trim() === '') {
|
||||
path = `${fileName}${plugin.settings.folderNoteType}`;
|
||||
} else {
|
||||
path = `${parentFolderPath}/${fileName}${plugin.settings.folderNoteType}`;
|
||||
}
|
||||
} else if (plugin.settings.storageLocation === 'vaultFolder') {
|
||||
path = `${fileName}${plugin.settings.folderNoteType}`;
|
||||
}
|
||||
const file = await plugin.app.vault.create(path, '');
|
||||
if (openFile) {
|
||||
await leaf.openFile(file);
|
||||
|
|
@ -16,12 +26,14 @@ export async function createFolderNote(plugin: FolderNotesPlugin, folderPath: st
|
|||
if (file) {
|
||||
applyTemplate(this, file, plugin.settings.templatePath);
|
||||
}
|
||||
plugin.addCSSClassToTitleEL(path, 'is-folder-note', true);
|
||||
plugin.addCSSClassToTitleEL(file.parent.path, 'has-folder-note');
|
||||
if (!plugin.settings.autoCreate) return;
|
||||
if (!useModal) return;
|
||||
|
||||
const folder = plugin.app.vault.getAbstractFileByPath(folderPath);
|
||||
if (!(folder instanceof TFolder)) return;
|
||||
plugin.addCSSClassToTitleEL(path, 'is-folder-note', true);
|
||||
plugin.addCSSClassToTitleEL(folder.path, 'has-folder-note');
|
||||
|
||||
if (!plugin.settings.autoCreate) return;
|
||||
if (!useModal) return;
|
||||
const modal = new FolderNameModal(plugin.app, plugin, folder);
|
||||
modal.open();
|
||||
}
|
||||
|
|
@ -66,30 +78,41 @@ export function getFolderNote(plugin: FolderNotesPlugin, folderPath: string) {
|
|||
name: plugin.getFolderNameFromPathString(folderPath),
|
||||
};
|
||||
const fileName = plugin.settings.folderNoteName.replace('{{folder_name}}', folder.name);
|
||||
let path = `${folder.path}/${fileName}${plugin.settings.folderNoteType}`;
|
||||
let folderNote = plugin.app.vault.getAbstractFileByPath(path);
|
||||
|
||||
if (plugin.settings.storageLocation === 'parentFolder') {
|
||||
folder.path = plugin.getParentFolderPathFromPathString(folderPath);
|
||||
}
|
||||
let path = `${folder.path}/${fileName}`;
|
||||
if (folder.path.trim() === '') {
|
||||
path = fileName;
|
||||
}
|
||||
let folderNote = plugin.app.vault.getAbstractFileByPath(path + plugin.settings.folderNoteType);
|
||||
if (folderNote instanceof TFile) {
|
||||
return folderNote;
|
||||
} else {
|
||||
if (plugin.settings.folderNoteType === '.canvas') {
|
||||
folderNote = plugin.app.vault.getAbstractFileByPath(`${folder.path}/${fileName}.md`);
|
||||
folderNote = plugin.app.vault.getAbstractFileByPath(path + '.md');
|
||||
} else {
|
||||
folderNote = plugin.app.vault.getAbstractFileByPath(`${folder.path}/${fileName}.canvas`);
|
||||
folderNote = plugin.app.vault.getAbstractFileByPath(path + '.canvas');
|
||||
}
|
||||
if (!(folderNote instanceof TFile)) {
|
||||
path = `${folder.path}/${folder.name}${plugin.settings.folderNoteType}`;
|
||||
folderNote = plugin.app.vault.getAbstractFileByPath(path);
|
||||
if (!(folderNote instanceof TFile)) {
|
||||
if (plugin.settings.folderNoteType === '.canvas') {
|
||||
folderNote = plugin.app.vault.getAbstractFileByPath(`${folder.path}/${folder.name}.md`);
|
||||
} else {
|
||||
folderNote = plugin.app.vault.getAbstractFileByPath(`${folder.path}/${folder.name}.canvas`);
|
||||
}
|
||||
return folderNote;
|
||||
if (!folderNote && plugin.settings.storageLocation === 'parentFolder') {
|
||||
folderNote = plugin.app.vault.getAbstractFileByPath(`${folderPath}/${fileName}.md`);
|
||||
if (!folderNote && plugin.settings.folderNoteType === '.canvas') {
|
||||
folderNote = plugin.app.vault.getAbstractFileByPath(`${folderPath}/${fileName}.canvas`);
|
||||
}
|
||||
} else if (!folderNote) {
|
||||
folder.path = plugin.getParentFolderPathFromPathString(folderPath);
|
||||
path = `${folder.path}/${fileName}`;
|
||||
if (folder.path.trim() === '') {
|
||||
path = fileName;
|
||||
}
|
||||
folderNote = plugin.app.vault.getAbstractFileByPath(path + plugin.settings.folderNoteType);
|
||||
if (!folderNote && plugin.settings.folderNoteType === '.canvas') {
|
||||
folderNote = plugin.app.vault.getAbstractFileByPath(path + '.md');
|
||||
} else if (!folderNote) {
|
||||
folderNote = plugin.app.vault.getAbstractFileByPath(path + '.canvas');
|
||||
}
|
||||
} else {
|
||||
return folderNote;
|
||||
}
|
||||
|
||||
return folderNote;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
48
src/main.ts
48
src/main.ts
|
|
@ -115,6 +115,15 @@ export default class FolderNotesPlugin extends Plugin {
|
|||
}
|
||||
}));
|
||||
|
||||
this.registerEvent(this.app.vault.on('delete', (file: TAbstractFile) => {
|
||||
if (!(file instanceof TFolder)) { return; }
|
||||
const folderNote = getFolderNote(this, file.path);
|
||||
if (!folderNote) { return; }
|
||||
this.removeCSSClassFromEL(folderNote.path, 'is-folder-note');
|
||||
if (!this.settings.syncDelete) { return; }
|
||||
this.app.vault.delete(folderNote);
|
||||
}));
|
||||
|
||||
if (this.app.workspace.layoutReady) {
|
||||
this.loadFileClasses();
|
||||
} else {
|
||||
|
|
@ -140,12 +149,16 @@ export default class FolderNotesPlugin extends Plugin {
|
|||
}
|
||||
}
|
||||
|
||||
getParentFolderPathFromPathString(path: string): string {
|
||||
return path.substring(0, path.lastIndexOf('/' || '\\') >= 0 ? path.lastIndexOf('/' || '\\') : 0);
|
||||
}
|
||||
|
||||
getExtensionFromPathString(path: string): string {
|
||||
return path.slice(path.lastIndexOf('.') >= 0 ? path.lastIndexOf('.') : 0);
|
||||
}
|
||||
|
||||
getFolderPathFromString(path: string): string {
|
||||
const subString = path.lastIndexOf('/' || '\\') >= 0 ? path.lastIndexOf('/') : path.length;
|
||||
const subString = path.lastIndexOf('/' || '\\') >= 0 ? path.lastIndexOf('/') : 0;
|
||||
return path.substring(0, subString);
|
||||
}
|
||||
|
||||
|
|
@ -209,16 +222,43 @@ export default class FolderNotesPlugin extends Plugin {
|
|||
if (this.activeFileExplorer === this.getFileExplorer() && !forceReload) { return; }
|
||||
this.activeFileExplorer = this.getFileExplorer();
|
||||
this.app.vault.getFiles().forEach((file) => {
|
||||
if (extractFolderName(this.settings.folderNoteName, file.basename) !== file.parent.name) { return; }
|
||||
let folder: TFolder | TAbstractFile | null;
|
||||
const folderName = extractFolderName(this.settings.folderNoteName, file.basename);
|
||||
if (!folderName) { return; }
|
||||
if (this.settings.storageLocation === 'parentFolder') {
|
||||
let folderPath = this.getFolderPathFromString(file.path);
|
||||
if (folderPath.trim() === '') {
|
||||
folderPath = folderName;
|
||||
} else {
|
||||
folderPath = `${folderPath}/${folderName}`;
|
||||
}
|
||||
folder = this.app.vault.getAbstractFileByPath(folderPath);
|
||||
if (!folder) {
|
||||
folder = this.app.vault.getAbstractFileByPath(file.parent.path);
|
||||
}
|
||||
} else {
|
||||
folder = this.app.vault.getAbstractFileByPath(file.parent.path);
|
||||
if (!(folder instanceof TFolder) || extractFolderName(this.settings.folderNoteName, file.basename) !== folder.name) {
|
||||
let folderPath = this.getFolderPathFromString(file.path);
|
||||
if (folderPath.trim() === '') {
|
||||
folderPath = folderName;
|
||||
} else {
|
||||
folderPath = `${folderPath}/${folderName}`;
|
||||
}
|
||||
folder = this.app.vault.getAbstractFileByPath(folderPath);
|
||||
}
|
||||
}
|
||||
if (!(folder instanceof TFolder)) { return; }
|
||||
|
||||
const excludedFolder = this.getExcludedFolderByPath(file.parent.path);
|
||||
// cleanup after ourselves
|
||||
// Incase settings have changed
|
||||
if (excludedFolder?.disableFolderNote) {
|
||||
this.removeCSSClassFromEL(file.path, 'is-folder-note');
|
||||
this.removeCSSClassFromEL(file.parent.path, 'has-folder-note');
|
||||
this.removeCSSClassFromEL(folder.path, 'has-folder-note');
|
||||
return;
|
||||
}
|
||||
this.addCSSClassToTitleEL(file.parent.path, 'has-folder-note');
|
||||
this.addCSSClassToTitleEL(folder.path, 'has-folder-note');
|
||||
this.addCSSClassToTitleEL(file.path, 'is-folder-note');
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,6 +24,8 @@ export interface FolderNotesSettings {
|
|||
newFolderNoteName: string;
|
||||
folderNoteType: '.md' | '.canvas';
|
||||
disableFolderHighlighting: boolean;
|
||||
storageLocation: 'insideFolder' | 'parentFolder' | 'vaultFolder';
|
||||
syncDelete: boolean;
|
||||
}
|
||||
|
||||
export const DEFAULT_SETTINGS: FolderNotesSettings = {
|
||||
|
|
@ -45,6 +47,8 @@ export const DEFAULT_SETTINGS: FolderNotesSettings = {
|
|||
folderNoteType: '.md',
|
||||
disableFolderHighlighting: false,
|
||||
newFolderNoteName: '{{folder_name}}',
|
||||
storageLocation: 'insideFolder',
|
||||
syncDelete: false,
|
||||
};
|
||||
export class SettingsTab extends PluginSettingTab {
|
||||
plugin: FolderNotesPlugin;
|
||||
|
|
@ -95,6 +99,35 @@ export class SettingsTab extends PluginSettingTab {
|
|||
})
|
||||
);
|
||||
|
||||
new Setting(containerEl)
|
||||
.setName('Storage location')
|
||||
.setDesc('Choose where to store the folder notes')
|
||||
.addDropdown((dropdown) =>
|
||||
dropdown
|
||||
.addOption('insideFolder', 'Inside the folder')
|
||||
.addOption('parentFolder', 'In the parent folder')
|
||||
.setValue(this.plugin.settings.storageLocation)
|
||||
.onChange(async (value: 'insideFolder' | 'parentFolder' | 'vaultFolder') => {
|
||||
this.plugin.settings.storageLocation = value;
|
||||
await this.plugin.saveSettings();
|
||||
this.display();
|
||||
})
|
||||
);
|
||||
|
||||
if (this.plugin.settings.storageLocation === 'parentFolder') {
|
||||
new Setting(containerEl)
|
||||
.setName('Delete folder notes when deleting the folder')
|
||||
.setDesc('Delete the folder note when deleting the folder')
|
||||
.addToggle((toggle) =>
|
||||
toggle
|
||||
.setValue(this.plugin.settings.syncDelete)
|
||||
.onChange(async (value) => {
|
||||
this.plugin.settings.syncDelete = value;
|
||||
await this.plugin.saveSettings();
|
||||
}
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
const disableSetting = new Setting(containerEl);
|
||||
disableSetting.setName('Disable folder collapsing');
|
||||
|
|
|
|||
Loading…
Reference in a new issue