mirror of
https://github.com/lostpaul/obsidian-folder-notes.git
synced 2026-07-22 07:40:24 +00:00
Fixed renaming & added sync move setting
This commit is contained in:
parent
61db5be4b1
commit
b3d8f6e01e
4 changed files with 109 additions and 21 deletions
|
|
@ -1,7 +1,7 @@
|
|||
import { TFile, TFolder, TAbstractFile, Notice } from 'obsidian';
|
||||
import FolderNotesPlugin from 'src/main';
|
||||
import { extractFolderName, getFolderNote } from '../functions/folderNoteFunctions';
|
||||
import { getExcludedFolder } from '../excludedFolder';
|
||||
import { extractFolderName, getFolderNote, getFolderNoteFolder } from '../functions/folderNoteFunctions';
|
||||
import { getExcludedFolder, ExcludedFolder, addExcludedFolder, updateExcludedFolder, deleteExcludedFolder } from '../excludedFolder';
|
||||
export function handleFolderRename(file: TFolder, oldPath: string, plugin: FolderNotesPlugin) {
|
||||
const fileName = plugin.settings.folderNoteName.replace('{{folder_name}}', file.name);
|
||||
const folder = plugin.app.vault.getAbstractFileByPath(file.path);
|
||||
|
|
@ -36,7 +36,11 @@ export function handleFolderRename(file: TFolder, oldPath: string, plugin: Folde
|
|||
let newPath = '';
|
||||
if (plugin.settings.storageLocation === 'parentFolder') {
|
||||
const parentFolderPath = plugin.getFolderPathFromString(file.path);
|
||||
if (parentFolderPath.trim() === '') {
|
||||
const oldParentFolderPath = plugin.getFolderPathFromString(oldPath);
|
||||
if (parentFolderPath !== oldParentFolderPath) {
|
||||
if (!plugin.settings.syncMove) { return; }
|
||||
newPath = `${parentFolderPath}/${fileName}.${folderNote.extension}`;
|
||||
} else if (parentFolderPath.trim() === '') {
|
||||
folderNote.path = `${folderNote.name}`;
|
||||
newPath = `${fileName}.${folderNote.extension}`;
|
||||
} else {
|
||||
|
|
@ -51,15 +55,15 @@ export function handleFolderRename(file: TFolder, oldPath: string, plugin: Folde
|
|||
}
|
||||
|
||||
export function handleFileRename(file: TFile, oldPath: string, plugin: FolderNotesPlugin) {
|
||||
const oldFileName = plugin.getFileNameFromPathString(oldPath);
|
||||
const oldFilePath = plugin.getFolderPathFromString(oldPath);
|
||||
const fileExtension = plugin.getExtensionFromPathString(oldPath);
|
||||
const oldFolder = plugin.app.vault.getAbstractFileByPath(oldFilePath);
|
||||
const newFilePath = plugin.getFolderPathFromString(file.path);
|
||||
const newFolder = plugin.app.vault.getAbstractFileByPath(newFilePath);
|
||||
const excludedFolder = getExcludedFolder(plugin, newFolder?.path || '');
|
||||
const oldFileName = plugin.removeExtension(plugin.getFileNameFromPathString(oldPath));
|
||||
const oldFolder = getFolderNoteFolder(plugin, oldPath, oldFileName);
|
||||
const folderName = extractFolderName(plugin.settings.folderNoteName, file.basename) || file.basename;
|
||||
const oldFolderName = extractFolderName(plugin.settings.folderNoteName, oldFileName) || oldFileName;
|
||||
const newFolder = getFolderNoteFolder(plugin, file, file.basename);
|
||||
let excludedFolder = getExcludedFolder(plugin, newFolder?.path || '');
|
||||
const folderNote = getFolderNote(plugin, oldPath, plugin.settings.storageLocation, file);
|
||||
|
||||
if (excludedFolder?.disableSync && extractFolderName(plugin.settings.folderNoteName, file.name.slice(0, file.name.lastIndexOf('.'))) === newFolder?.name) {
|
||||
if (excludedFolder?.disableSync && folderName === newFolder?.name) {
|
||||
plugin.addCSSClassToTitleEL(file.path, 'is-folder-note');
|
||||
plugin.addCSSClassToTitleEL(newFolder.path, 'has-folder-note');
|
||||
return;
|
||||
|
|
@ -70,9 +74,34 @@ export function handleFileRename(file: TFile, oldPath: string, plugin: FolderNot
|
|||
}
|
||||
|
||||
// file has been moved into position where it can be a folder note!
|
||||
if (extractFolderName(plugin.settings.folderNoteName, file.name.slice(0, file.name.lastIndexOf('.'))) === newFolder?.name) {
|
||||
if (folderName === newFolder?.name && folderNote) {
|
||||
new Notice('Folder with same name already exists!');
|
||||
let excludedFolderExisted = true;
|
||||
let disabledSync = false;
|
||||
|
||||
if (!excludedFolder) {
|
||||
excludedFolderExisted = false;
|
||||
excludedFolder = new ExcludedFolder(oldFolder?.path || '', plugin.settings.excludeFolders.length);
|
||||
addExcludedFolder(plugin, excludedFolder);
|
||||
} else if (!excludedFolder.disableSync) {
|
||||
disabledSync = false;
|
||||
excludedFolder.disableSync = true;
|
||||
updateExcludedFolder(plugin, excludedFolder, excludedFolder);
|
||||
}
|
||||
return plugin.app.vault.rename(file, oldPath).then(() => {
|
||||
if (!excludedFolder) { return; }
|
||||
if (!excludedFolderExisted) {
|
||||
deleteExcludedFolder(plugin, excludedFolder);
|
||||
} else if (!disabledSync) {
|
||||
excludedFolder.disableSync = false;
|
||||
updateExcludedFolder(plugin, excludedFolder, excludedFolder);
|
||||
}
|
||||
});
|
||||
}
|
||||
if (folderName === newFolder?.name) {
|
||||
plugin.addCSSClassToTitleEL(file.path, 'is-folder-note');
|
||||
plugin.addCSSClassToTitleEL(newFolder.path, 'has-folder-note');
|
||||
plugin.removeCSSClassFromEL(oldFolder?.path, 'has-folder-note');
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -80,15 +109,15 @@ export function handleFileRename(file: TFile, oldPath: string, plugin: FolderNot
|
|||
// file hasnt moved just renamed
|
||||
// Need to rename the folder
|
||||
if (!oldFolder) return;
|
||||
if (plugin.settings.folderNoteName.replace('{{folder_name}}', oldFolder.name) + fileExtension === oldFileName && newFilePath === oldFilePath) {
|
||||
if (oldFolderName === oldFolder.name && newFolder?.path === oldFolder.path) {
|
||||
return renameFolderOnFileRename(file, oldPath, oldFolder, plugin);
|
||||
} else if (plugin.settings.folderNoteName.replace('{{folder_name}}', oldFolder.name) + fileExtension === file.name && newFilePath === oldFilePath) {
|
||||
} else if (folderNote && oldFolderName === oldFolder.name) {
|
||||
return renameFolderOnFileRename(file, oldPath, oldFolder, plugin);
|
||||
}
|
||||
|
||||
// the note has been moved somewhere and is no longer a folder note
|
||||
// cleanup css on the folder and note
|
||||
if (oldFolder.name + plugin.getExtensionFromPathString(oldFilePath) === oldFileName && newFilePath !== oldFilePath) {
|
||||
if (oldFolder.name === oldFileName && newFolder?.path !== oldFolder.path) {
|
||||
plugin.removeCSSClassFromEL(oldFolder.path, 'has-folder-note');
|
||||
plugin.removeCSSClassFromEL(file.path, 'is-folder-note');
|
||||
plugin.removeCSSClassFromEL(oldPath, 'is-folder-note');
|
||||
|
|
@ -106,7 +135,17 @@ async function renameFolderOnFileRename(file: TFile, oldPath: string, oldFolder:
|
|||
plugin.addCSSClassToTitleEL(file.path, 'is-folder-note');
|
||||
return;
|
||||
}
|
||||
const newFolderPath = oldFolder.parent.path + '/' + extractFolderName(plugin.settings.folderNoteName, file.basename);
|
||||
let newFolderPath = '';
|
||||
if (plugin.settings.storageLocation === 'insideFolder') {
|
||||
newFolderPath = oldFolder.parent?.path + '/' + newFolderName;
|
||||
} else {
|
||||
const parentFolderPath = plugin.getFolderPathFromString(file.path);
|
||||
if (parentFolderPath.trim() === '') {
|
||||
newFolderPath = `${newFolderName}`;
|
||||
} else {
|
||||
newFolderPath = `${parentFolderPath}/${newFolderName}`;
|
||||
}
|
||||
}
|
||||
if (plugin.app.vault.getAbstractFileByPath(newFolderPath) || plugin.app.vault.getAbstractFileByPath(newFolderName || '')) {
|
||||
await plugin.app.vault.rename(file, oldPath);
|
||||
return new Notice('A folder with the same name already exists');
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ 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);
|
||||
let path = `${folderPath}/${fileName}${plugin.settings.folderNoteType}`;
|
||||
let path = '';
|
||||
if (plugin.settings.storageLocation === 'parentFolder') {
|
||||
const parentFolderPath = plugin.getFolderPathFromString(folderPath);
|
||||
if (parentFolderPath.trim() === '') {
|
||||
|
|
@ -20,6 +20,8 @@ export async function createFolderNote(plugin: FolderNotesPlugin, folderPath: st
|
|||
}
|
||||
} else if (plugin.settings.storageLocation === 'vaultFolder') {
|
||||
path = `${fileName}${plugin.settings.folderNoteType}`;
|
||||
} else {
|
||||
path = `${folderPath}/${fileName}${plugin.settings.folderNoteType}`;
|
||||
}
|
||||
let file: TFile;
|
||||
if (!existingNote) {
|
||||
|
|
@ -128,13 +130,16 @@ export function extractFolderName(template: string, changedFileName: string) {
|
|||
return null;
|
||||
}
|
||||
|
||||
export function getFolderNote(plugin: FolderNotesPlugin, folderPath: string, storageLocation?: string) {
|
||||
export function getFolderNote(plugin: FolderNotesPlugin, folderPath: string, storageLocation?: string, file?: TFile) {
|
||||
if (!folderPath) return null;
|
||||
const folder = {
|
||||
path: folderPath,
|
||||
name: plugin.getFolderNameFromPathString(folderPath),
|
||||
};
|
||||
const fileName = plugin.settings.folderNoteName.replace('{{folder_name}}', folder.name);
|
||||
let fileName = plugin.settings.folderNoteName.replace('{{folder_name}}', folder.name);
|
||||
if (file) {
|
||||
fileName = plugin.settings.folderNoteName.replace('{{folder_name}}', file.basename);
|
||||
}
|
||||
|
||||
if ((plugin.settings.storageLocation === 'parentFolder' || storageLocation === 'parentFolder') && storageLocation !== 'insideFolder') {
|
||||
folder.path = plugin.getFolderPathFromString(folderPath);
|
||||
|
|
@ -144,7 +149,6 @@ export function getFolderNote(plugin: FolderNotesPlugin, folderPath: string, sto
|
|||
folder.path = fileName;
|
||||
path = `${fileName}`;
|
||||
}
|
||||
|
||||
let folderNote = plugin.app.vault.getAbstractFileByPath(path + plugin.settings.folderNoteType);
|
||||
if (folderNote instanceof TFile) {
|
||||
return folderNote;
|
||||
|
|
@ -177,3 +181,29 @@ export function getFolder(plugin: FolderNotesPlugin, file: TFile, storageLocatio
|
|||
if (!folder) { return null; }
|
||||
return folder;
|
||||
}
|
||||
|
||||
export function getFolderNoteFolder(plugin: FolderNotesPlugin, folderNote: TFile | string, fileName: string) {
|
||||
if (!folderNote) return null;
|
||||
let filePath = '';
|
||||
if (typeof folderNote === 'string') {
|
||||
filePath = folderNote;
|
||||
} else {
|
||||
fileName = folderNote.basename;
|
||||
filePath = folderNote.path;
|
||||
}
|
||||
const folderName = extractFolderName(plugin.settings.folderNoteName, fileName);
|
||||
if (!folderName) return null;
|
||||
let folderPath = plugin.getFolderPathFromString(filePath);
|
||||
if (plugin.settings.storageLocation === 'parentFolder') {
|
||||
if (folderPath.trim() === '') {
|
||||
folderPath = folderName;
|
||||
} else {
|
||||
folderPath = `${folderPath}/${folderName}`;
|
||||
}
|
||||
} else {
|
||||
folderPath = plugin.getFolderPathFromString(filePath);
|
||||
}
|
||||
const folder = plugin.app.vault.getAbstractFileByPath(folderPath);
|
||||
if (!folder) { return null; }
|
||||
return folder;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -182,6 +182,10 @@ export default class FolderNotesPlugin extends Plugin {
|
|||
}
|
||||
}
|
||||
|
||||
removeExtension(name: string): string {
|
||||
return name.replace(/\.[^/.]+$/, '');
|
||||
}
|
||||
|
||||
getExtensionFromPathString(path: string): string {
|
||||
return path.slice(path.lastIndexOf('.') >= 0 ? path.lastIndexOf('.') : 0);
|
||||
}
|
||||
|
|
@ -224,7 +228,8 @@ export default class FolderNotesPlugin extends Plugin {
|
|||
});
|
||||
}
|
||||
|
||||
removeCSSClassFromEL(path: string, cssClass: string) {
|
||||
removeCSSClassFromEL(path: string | undefined, cssClass: string) {
|
||||
if (!path) return;
|
||||
const fileExplorerItem = this.getEL(path);
|
||||
const viewHeaderItems = document.querySelectorAll(`[data-path="${path}"]`);
|
||||
viewHeaderItems.forEach((item) => {
|
||||
|
|
|
|||
|
|
@ -29,6 +29,7 @@ export interface FolderNotesSettings {
|
|||
syncDelete: boolean;
|
||||
defaultOverview: yamlSettings;
|
||||
useSubmenus: boolean;
|
||||
syncMove: boolean;
|
||||
}
|
||||
|
||||
export const DEFAULT_SETTINGS: FolderNotesSettings = {
|
||||
|
|
@ -62,6 +63,7 @@ export const DEFAULT_SETTINGS: FolderNotesSettings = {
|
|||
disableCanvasTag: false,
|
||||
},
|
||||
useSubmenus: true,
|
||||
syncMove: true,
|
||||
};
|
||||
export class SettingsTab extends PluginSettingTab {
|
||||
plugin: FolderNotesPlugin;
|
||||
|
|
@ -165,6 +167,18 @@ export class SettingsTab extends PluginSettingTab {
|
|||
}
|
||||
)
|
||||
);
|
||||
new Setting(containerEl)
|
||||
.setName('Move folder notes when moving the folder')
|
||||
.setDesc('Move the folder note when moving the folder')
|
||||
.addToggle((toggle) =>
|
||||
toggle
|
||||
.setValue(this.plugin.settings.syncMove)
|
||||
.onChange(async (value) => {
|
||||
this.plugin.settings.syncMove = value;
|
||||
await this.plugin.saveSettings();
|
||||
}
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
const disableSetting = new Setting(containerEl);
|
||||
|
|
|
|||
Loading…
Reference in a new issue