Fix unwanted auto create of folder for unsupported file types

This commit is contained in:
Paul 2026-06-01 17:36:48 +02:00
parent 681320e2f2
commit ec73ad869f
3 changed files with 27 additions and 11 deletions

View file

@ -128,7 +128,7 @@ export class Commands {
id: `create-${type}-folder-note-for-active-file-explorer-folder`,
name: `Create ${type} folder note for current active folder in file explorer`,
checkCallback: (checking: boolean) => {
const folder = getFileExplorerActiveFolder();
const folder = getFileExplorerActiveFolder(this.plugin);
if (!folder) return false;
// Is there already a folder note for the active folder?
const folderNote = getFolderNote(this.plugin, folder.path);
@ -162,7 +162,7 @@ export class Commands {
id: 'delete-folder-note-of-active-file-explorer-folder',
name: 'Delete folder note of current active folder in file explorer',
checkCallback: (checking: boolean) => {
const folder = getFileExplorerActiveFolder();
const folder = getFileExplorerActiveFolder(this.plugin);
if (!folder) return false;
// Is there any folder note for the active folder?
const folderNote = getFolderNote(this.plugin, folder.path);
@ -191,7 +191,7 @@ export class Commands {
id: 'open-folder-note-of-active-file-explorer-folder',
name: 'Open folder note of current active folder in file explorer',
checkCallback: (checking: boolean) => {
const folder = getFileExplorerActiveFolder();
const folder = getFileExplorerActiveFolder(this.plugin);
if (!folder) return false;
// Is there any folder note for the active folder?
const folderNote = getFolderNote(this.plugin, folder.path);

View file

@ -11,6 +11,7 @@ import {
removeCSSClassFromFileExplorerEL,
addCSSClassToFileExplorerEl,
} from 'src/functions/styleFunctions';
import { isFileInAttachmentFolder } from '@app/functions/utils';
export async function handleCreate(file: TAbstractFile, plugin: FolderNotesPlugin): Promise<void> {
if (!plugin.app.workspace.layoutReady) return;
@ -50,7 +51,8 @@ async function handleFileCreation(file: TFile, plugin: FolderNotesPlugin): Promi
if (folderNote && folderNote.path === file.path) {
addCSSClassToFileExplorerEl(folder.path, 'has-folder-note', false, plugin);
addCSSClassToFileExplorerEl(file.path, 'is-folder-note', false, plugin);
} else if (plugin.settings.autoCreateForFiles) {
} else if (plugin.settings.autoCreateForFiles && !isFileInAttachmentFolder(plugin, file)) {
if (!plugin.settings.supportedFileTypes.includes(file.extension)) { return; }
if (!file.parent) { return; }
const newFolder = await plugin.app.fileManager.createNewFolder(file.parent);
turnIntoFolderNote(plugin, file, newFolder);

View file

@ -1,4 +1,4 @@
import { TFolder, TFile, View } from 'obsidian';
import { TFolder, TFile, View, TAbstractFile } from 'obsidian';
import type { FileExplorerWorkspaceLeaf, FileExplorerView } from 'src/globals';
import { getFolderNote } from './folderNoteFunctions';
import type FolderNotesPlugin from 'src/main';
@ -37,7 +37,7 @@ export function getFolderPathFromString(path: string): string {
}
export function getParentFolderPath(path: string): string {
return this.getFolderPathFromString(this.getFolderPathFromString(path));
return getFolderPathFromString(getFolderPathFromString(path));
}
export function getFileExplorer(
@ -71,9 +71,9 @@ export function getFocusedItem(plugin: FolderNotesPlugin): TreeNode<FileTreeItem
return focusedItem;
}
export function getFileExplorerActiveFolder(): TFolder | null {
export function getFileExplorerActiveFolder(plugin: FolderNotesPlugin): TFolder | null {
// Check if the active view is a file explorer.
const view = this.app.workspace.getActiveViewOfType(View);
const view = plugin.app.workspace.getActiveViewOfType(View);
if (view?.getViewType() !== 'file-explorer') return null;
// Check if there is a focused or active item in the file explorer.
const fe = view as FileExplorerView;
@ -83,11 +83,25 @@ export function getFileExplorerActiveFolder(): TFolder | null {
return activeFileOrFolder;
}
export function getFileExplorerActiveFolderNote(): TFile | null {
const folder = getFileExplorerActiveFolder();
export function getFileExplorerActiveFolderNote(plugin: FolderNotesPlugin): TFile | null {
const folder = getFileExplorerActiveFolder(plugin);
if (!folder) return null;
// Is there any folder note for the active folder?
const folderNote = getFolderNote(this.plugin, folder.path);
const folderNote = getFolderNote(plugin, folder.path);
if (!(folderNote instanceof TFile)) return null;
return folderNote;
}
export function getAttachmentFolderPath(plugin: FolderNotesPlugin): string {
const attachmentFolderPath = plugin.app.vault.getConfig('attachmentFolderPath') as string;
const cleanAttachmentFolderPath = attachmentFolderPath?.replace('./', '') || '';
return cleanAttachmentFolderPath;
}
export function isFileInAttachmentFolder(plugin: FolderNotesPlugin, file: TAbstractFile): boolean {
const attachmentFolderPath = getAttachmentFolderPath(plugin);
if (!attachmentFolderPath) return false;
return file.path.startsWith(attachmentFolderPath + '/')
|| file.path.includes('/' + attachmentFolderPath + '/');
}