Multiple excluded folders/patterns instead only one

Previously the plugin checked if a folder matches a folder path or pattern and stopped searching for other patterns/excluded folder after its first find.
This meant you couldn't "override" an already excluded folder in the settings. So if you had a regex that matches every folder it would just use the settings you set for the regex and ignore everything else that would also match the folder.
This commit is contained in:
Lost Paul 2024-05-21 17:30:03 +02:00
parent cd38702394
commit f48598d55f
3 changed files with 196 additions and 93 deletions

View file

@ -2,29 +2,99 @@ import FolderNotesPlugin from '../../main';
import { getFolderNameFromPathString, getFolderPathFromString } from '../../functions/utils';
import { ExcludedFolder } from '../ExcludeFolder';
import { ExcludePattern } from '../ExcludePattern';
import { Setting } from 'obsidian';
import { Platform, Setting } from 'obsidian';
import { FolderSuggest } from '../../suggesters/FolderSuggester';
import { SettingsTab } from '../../settings/SettingsTab';
import ExcludedFolderSettings from '../modals/ExcludeFolderSettings';
import { updatePattern, getExcludedFolderByPattern, addExcludePatternListItem } from './patternFunctions';
import { updatePattern, getExcludedFoldersByPattern, addExcludePatternListItem } from './patternFunctions';
import { getWhitelistedFolder } from './whitelistFolderFunctions';
import { WhitelistedFolder } from '../WhitelistFolder';
import { WhitelistedPattern } from '../WhitelistPattern';
export function getExcludedFolder(plugin: FolderNotesPlugin, path: string) {
let excludedFolder = {} as ExcludedFolder | ExcludePattern | undefined;
const whitelistedFolder = getWhitelistedFolder(plugin, path) as WhitelistedFolder | WhitelistedPattern | undefined;
const folderName = getFolderNameFromPathString(path);
const matchedPattern = getExcludedFolderByPattern(plugin, folderName);
if (matchedPattern) { return matchedPattern; }
const excludedFolder = getExcludedFolderByPath(plugin, path);
if (excludedFolder?.path === '') { return; }
const matchedPatterns = getExcludedFoldersByPattern(plugin, folderName);
const excludedFolders = getExcludedFoldersByPath(plugin, path);
const combinedExcludedFolders = [...matchedPatterns, ...excludedFolders];
console.log('excludedFolders', excludedFolders)
console.log('combinedExcludedFolders', combinedExcludedFolders)
const propertiesToCopy: (keyof ExcludedFolder)[] = [
'disableAutoCreate',
'disableFolderNote',
'disableSync',
'enableCollapsing',
'excludeFromFolderOverview'
];
if (combinedExcludedFolders.length > 0) {
for (const matchedFolder of combinedExcludedFolders) {
propertiesToCopy.forEach(property => {
if (matchedFolder[property] === true) {
(excludedFolder as any)[property] = true;
} else if (!matchedFolder[property]) {
(excludedFolder as any)[property] = false;
}
});
}
}
console.log('excludedFolder', excludedFolder)
if (whitelistedFolder && excludedFolder) {
excludedFolder.disableAutoCreate ? excludedFolder.disableAutoCreate = !whitelistedFolder.enableAutoCreate : '';
excludedFolder.disableFolderNote ? excludedFolder.disableFolderNote = !whitelistedFolder.enableFolderNote : '';
excludedFolder.disableSync ? excludedFolder.disableSync = !whitelistedFolder.enableSync : '';
excludedFolder.enableCollapsing = whitelistedFolder.enableCollapsing;
excludedFolder.excludeFromFolderOverview ? excludedFolder.excludeFromFolderOverview = !whitelistedFolder.showInFolderOverview : '';
} else if (excludedFolder && Object.keys(excludedFolder).length === 0) {
excludedFolder = {
type: 'folder',
id: '',
path: '',
string: '',
subFolders: false,
disableSync: false,
disableAutoCreate: false,
disableFolderNote: false,
enableCollapsing: false,
position: 0,
excludeFromFolderOverview: false,
hideInSettings: false
}
}
return excludedFolder;
}
export function getExcludedFolderByPath(plugin: FolderNotesPlugin, path: string) {
return plugin.settings.excludeFolders.find((excludedFolder) => {
if (path.trim() === '' || !excludedFolder.path) { return false; }
if (excludedFolder.path === path) { return true; }
if (!excludedFolder.subFolders) { return false; }
const excludedFolderPath = excludedFolder.path.includes('/') ? excludedFolder.path : excludedFolder.path + '/';
let folderPath = getFolderPathFromString(path);
folderPath = folderPath.includes('/') ? folderPath : folderPath + '/';
if (folderPath.includes('/') || folderPath.includes('\\')) {
return folderPath.startsWith(excludedFolderPath) || folderPath === excludedFolderPath;
} else {
return folderPath === excludedFolderPath;
}
});
}
export function getExcludedFoldersByPath(plugin: FolderNotesPlugin, path: string) {
return plugin.settings.excludeFolders.filter((excludedFolder) => {
console.log('excludedFolder path', excludedFolder.path)
if (path.trim() === '' || !excludedFolder.path) { return false; }
if (excludedFolder.path === path) { return true; }
if (!excludedFolder.subFolders) { return false; }
const excludedFolderPath = excludedFolder.path.includes('/') ? excludedFolder.path : excludedFolder.path + '/';
let folderPath = getFolderPathFromString(path);
folderPath = folderPath.includes('/') ? folderPath : folderPath + '/';
if (folderPath.includes('/') || folderPath.includes('\\')) {
return folderPath.startsWith(excludedFolderPath) || folderPath === excludedFolderPath;
} else {
@ -39,13 +109,13 @@ export function addExcludedFolder(plugin: FolderNotesPlugin, excludedFolder: Exc
}
export function deleteExcludedFolder(plugin: FolderNotesPlugin, excludedFolder: ExcludedFolder) {
plugin.settings.excludeFolders = plugin.settings.excludeFolders.filter((folder) => folder.path !== excludedFolder.path || folder.type === 'pattern');
plugin.settings.excludeFolders = plugin.settings.excludeFolders.filter((folder) => folder.id !== excludedFolder.id || folder.type === 'pattern');
plugin.saveSettings(true);
resyncArray(plugin);
}
export function updateExcludedFolder(plugin: FolderNotesPlugin, excludedFolder: ExcludePattern, newExcludeFolder: ExcludePattern) {
plugin.settings.excludeFolders = plugin.settings.excludeFolders.filter((folder) => folder.path !== excludedFolder.path);
plugin.settings.excludeFolders = plugin.settings.excludeFolders.filter((folder) => folder.id !== excludedFolder.id);
addExcludedFolder(plugin, newExcludeFolder);
}
@ -65,16 +135,17 @@ export function addExcludeFolderListItem(settings: SettingsTab, containerEl: HTM
setting.addSearch((cb) => {
new FolderSuggest(
cb.inputEl,
plugin
plugin,
false
);
// @ts-ignore
cb.containerEl.addClass('fn-exclude-folder-path');
cb.setPlaceholder('Folder path');
cb.setValue(excludedFolder.path);
cb.setValue(excludedFolder.path || '');
cb.onChange((value) => {
if (value.startsWith('{regex}') || value.includes('*')) {
deleteExcludedFolder(plugin, excludedFolder);
const pattern = new ExcludePattern(value, plugin.settings.excludeFolders.length, plugin);
const pattern = new ExcludePattern(value, plugin.settings.excludeFolders.length, undefined, plugin);
addExcludedFolder(plugin, pattern);
addExcludePatternListItem(settings, containerEl, pattern);
setting.clear();
@ -94,49 +165,51 @@ export function addExcludeFolderListItem(settings: SettingsTab, containerEl: HTM
});
});
setting.addButton((cb) => {
cb.setIcon('up-chevron-glyph');
cb.setTooltip('Move up');
cb.onClick(() => {
if (excludedFolder.position === 0) { return; }
excludedFolder.position -= 1;
updateExcludedFolder(plugin, excludedFolder, excludedFolder);
const oldExcludedFolder = plugin.settings.excludeFolders.find((folder) => folder.position === excludedFolder.position);
if (oldExcludedFolder) {
oldExcludedFolder.position += 1;
if (oldExcludedFolder.type === 'pattern') {
updatePattern(plugin, oldExcludedFolder, oldExcludedFolder);
} else {
updateExcludedFolder(plugin, oldExcludedFolder, oldExcludedFolder);
if (Platform.isDesktop || Platform.isTablet) {
setting.addButton((cb) => {
cb.setIcon('up-chevron-glyph');
cb.setTooltip('Move up');
cb.onClick(() => {
if (excludedFolder.position === 0) { return; }
excludedFolder.position -= 1;
updateExcludedFolder(plugin, excludedFolder, excludedFolder);
const oldExcludedFolder = plugin.settings.excludeFolders.find((folder) => folder.position === excludedFolder.position);
if (oldExcludedFolder) {
oldExcludedFolder.position += 1;
if (oldExcludedFolder.type === 'pattern') {
updatePattern(plugin, oldExcludedFolder, oldExcludedFolder);
} else {
updateExcludedFolder(plugin, oldExcludedFolder, oldExcludedFolder);
}
}
}
settings.display();
settings.display();
});
});
});
setting.addButton((cb) => {
cb.setIcon('down-chevron-glyph');
cb.setTooltip('Move down');
cb.onClick(() => {
if (excludedFolder.position === plugin.settings.excludeFolders.length - 1) {
return;
}
excludedFolder.position += 1;
updateExcludedFolder(plugin, excludedFolder, excludedFolder);
const oldExcludedFolder = plugin.settings.excludeFolders.find((folder) => folder.position === excludedFolder.position);
if (oldExcludedFolder) {
oldExcludedFolder.position -= 1;
if (oldExcludedFolder.type === 'pattern') {
updatePattern(plugin, oldExcludedFolder, oldExcludedFolder);
} else {
updateExcludedFolder(plugin, oldExcludedFolder, oldExcludedFolder);
setting.addButton((cb) => {
cb.setIcon('down-chevron-glyph');
cb.setTooltip('Move down');
cb.onClick(() => {
if (excludedFolder.position === plugin.settings.excludeFolders.length - 1) {
return;
}
}
excludedFolder.position += 1;
settings.display();
updateExcludedFolder(plugin, excludedFolder, excludedFolder);
const oldExcludedFolder = plugin.settings.excludeFolders.find((folder) => folder.position === excludedFolder.position);
if (oldExcludedFolder) {
oldExcludedFolder.position -= 1;
if (oldExcludedFolder.type === 'pattern') {
updatePattern(plugin, oldExcludedFolder, oldExcludedFolder);
} else {
updateExcludedFolder(plugin, oldExcludedFolder, oldExcludedFolder);
}
}
settings.display();
});
});
});
}
setting.addButton((cb) => {
cb.setIcon('trash-2');

View file

@ -1,22 +1,50 @@
import FolderNotesPlugin from '../../main';
import { ExcludePattern } from '../ExcludePattern';
import { Setting } from 'obsidian';
import { Setting, Platform } from 'obsidian';
import { SettingsTab } from '../../settings/SettingsTab';
import { addExcludedFolder, resyncArray, updateExcludedFolder } from './folderFunctions';
import PatternSettings from '../modals/PatternSettings';
export function updatePattern(plugin: FolderNotesPlugin, pattern: ExcludePattern, newPattern: ExcludePattern) {
plugin.settings.excludeFolders = plugin.settings.excludeFolders.filter((folder) => folder.string !== pattern.string);
plugin.settings.excludeFolders = plugin.settings.excludeFolders.filter((folder) => folder.id !== pattern.id);
addExcludedFolder(plugin, newPattern);
}
export function deletePattern(plugin: FolderNotesPlugin, pattern: ExcludePattern) {
plugin.settings.excludeFolders = plugin.settings.excludeFolders.filter((folder) => folder.string !== pattern.string || folder.type === 'folder');
plugin.settings.excludeFolders = plugin.settings.excludeFolders.filter((folder) => folder.id !== pattern.id || folder.type === 'folder');
plugin.saveSettings();
resyncArray(plugin);
}
export function getExcludedFolderByPattern(plugin: FolderNotesPlugin, folderName: string) {
export function getExcludedFoldersByPattern(plugin: FolderNotesPlugin, folderName: string): ExcludePattern[] {
return plugin.settings.excludeFolders.filter((s) => s.type == 'pattern').filter((pattern) => {
if (!pattern.string) { return false; }
const string = pattern.string.trim();
if (!string.startsWith('{regex}') && !(string.startsWith('*') || string.endsWith('*'))) { return false; }
const regex = string.replace('{regex}', '').trim();
if (string.startsWith('{regex}') && regex === '') { return false; }
if (regex !== undefined && string.startsWith('{regex}')) {
const match = new RegExp(regex).exec(folderName);
if (match) {
return true;
}
} else if (string.startsWith('*') && string.endsWith('*')) {
if (folderName.includes(string.slice(1, -1))) {
return true;
}
} else if (string.startsWith('*')) {
if (folderName.endsWith(string.slice(1))) {
return true;
}
} else if (string.endsWith('*')) {
if (folderName.startsWith(string.slice(0, -1))) {
return true;
}
}
});
}
export function getExcludedFolderByPattern(plugin: FolderNotesPlugin, folderName: string): ExcludePattern | undefined{
return plugin.settings.excludeFolders.filter((s) => s.type == 'pattern').find((pattern) => {
if (!pattern.string) { return false; }
const string = pattern.string.trim();
@ -54,7 +82,6 @@ export function addExcludePatternListItem(settings: SettingsTab, containerEl: HT
cb.setPlaceholder('Pattern');
cb.setValue(pattern.string);
cb.onChange((value) => {
if (plugin.settings.excludeFolders.find((folder) => folder.string === value)) { return; }
pattern.string = value;
updatePattern(plugin, pattern, pattern);
});
@ -67,48 +94,50 @@ export function addExcludePatternListItem(settings: SettingsTab, containerEl: HT
});
});
setting.addButton((cb) => {
cb.setIcon('up-chevron-glyph');
cb.setTooltip('Move up');
cb.onClick(() => {
if (pattern.position === 0) { return; }
pattern.position -= 1;
updatePattern(plugin, pattern, pattern);
const oldPattern = plugin.settings.excludeFolders.find((folder) => folder.position === pattern.position);
if (oldPattern) {
oldPattern.position += 1;
if (oldPattern.type === 'pattern') {
updatePattern(plugin, oldPattern, oldPattern);
} else {
updateExcludedFolder(plugin, oldPattern, oldPattern);
if (Platform.isDesktop || Platform.isTablet) {
setting.addButton((cb) => {
cb.setIcon('up-chevron-glyph');
cb.setTooltip('Move up');
cb.onClick(() => {
if (pattern.position === 0) { return; }
pattern.position -= 1;
updatePattern(plugin, pattern, pattern);
const oldPattern = plugin.settings.excludeFolders.find((folder) => folder.position === pattern.position);
if (oldPattern) {
oldPattern.position += 1;
if (oldPattern.type === 'pattern') {
updatePattern(plugin, oldPattern, oldPattern);
} else {
updateExcludedFolder(plugin, oldPattern, oldPattern);
}
}
}
settings.display();
settings.display();
});
});
});
setting.addButton((cb) => {
cb.setIcon('down-chevron-glyph');
cb.setTooltip('Move down');
cb.onClick(() => {
if (pattern.position === plugin.settings.excludeFolders.length - 1) {
return;
}
pattern.position += 1;
updatePattern(plugin, pattern, pattern);
const oldPattern = plugin.settings.excludeFolders.find((folder) => folder.position === pattern.position);
if (oldPattern) {
oldPattern.position -= 1;
if (oldPattern.type === 'pattern') {
updatePattern(plugin, oldPattern, oldPattern);
} else {
updateExcludedFolder(plugin, oldPattern, oldPattern);
setting.addButton((cb) => {
cb.setIcon('down-chevron-glyph');
cb.setTooltip('Move down');
cb.onClick(() => {
if (pattern.position === plugin.settings.excludeFolders.length - 1) {
return;
}
}
settings.display();
pattern.position += 1;
updatePattern(plugin, pattern, pattern);
const oldPattern = plugin.settings.excludeFolders.find((folder) => folder.position === pattern.position);
if (oldPattern) {
oldPattern.position -= 1;
if (oldPattern.type === 'pattern') {
updatePattern(plugin, oldPattern, oldPattern);
} else {
updateExcludedFolder(plugin, oldPattern, oldPattern);
}
}
settings.display();
});
});
});
}
setting.addButton((cb) => {
cb.setIcon('trash-2');

View file

@ -48,7 +48,7 @@ export function handleFolderRename(file: TFolder, oldPath: string, plugin: Folde
if (!(folder instanceof TFolder)) return;
const excludedFolders = plugin.settings.excludeFolders.filter(
(excludedFolder) => excludedFolder.path.includes(oldPath)
(excludedFolder) => excludedFolder.path?.includes(oldPath)
);
excludedFolders.forEach((excludedFolder) => {
@ -56,6 +56,7 @@ export function handleFolderRename(file: TFolder, oldPath: string, plugin: Folde
excludedFolder.path = folder.path;
return;
}
if (!excludedFolder.path) return;
const folders = excludedFolder.path.split('/');
if (folders.length < 1) {
folders.push(excludedFolder.path);
@ -119,7 +120,7 @@ export function handleFileRename(file: TFile, oldPath: string, plugin: FolderNot
if (!excludedFolder) {
excludedFolderExisted = false;
excludedFolder = new ExcludedFolder(oldFolder?.path || '', plugin.settings.excludeFolders.length, plugin);
excludedFolder = new ExcludedFolder(oldFolder?.path || '', plugin.settings.excludeFolders.length, undefined, plugin);
addExcludedFolder(plugin, excludedFolder);
} else if (!excludedFolder.disableSync) {
disabledSync = false;