Skip to content

Commit

Permalink
perf(util): loading optimization for custom rule icons
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianWoelki committed Jul 5, 2022
1 parent a7b79b2 commit ef0bdca
Showing 1 changed file with 38 additions and 43 deletions.
81 changes: 38 additions & 43 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,52 +176,47 @@ export const addIconsToDOM = (
}
});

addCustomRuleIcons(plugin);
const addCustomIconRule = (rule: CustomRule, file: TAbstractFile) => {
const fileItem = fileExplorer.view.fileItems[file.path];
if (fileItem) {
const titleEl = fileItem.titleEl;
const titleInnerEl = fileItem.titleInnerEl;
const existingIcon = titleEl.querySelector('.obsidian-icon-folder-icon');
if (!existingIcon) {
const iconNode = titleEl.createDiv();
iconNode.classList.add('obsidian-icon-folder-icon');

if (callback) {
callback();
}
});
};
insertIconToNode(plugin, rule.icon, iconNode);

const addCustomRuleIcons = (plugin: IconFolderPlugin) => {
plugin.getSettings().rules.forEach((rule) => {
const inheritanceFolders = Object.entries(plugin.getData()).filter(
([k, v]) => k !== 'settings' && typeof v === 'object',
);
try {
// Rule is in some sort of regex.
const regex = new RegExp(rule.rule);
plugin.app.vault.getAllLoadedFiles().forEach(async (file) => {
const fileType = (await plugin.app.vault.adapter.stat(file.path)).type;
const isInfluencedByInheritance = inheritanceFolders.find(
([key]) => file.path.includes(key) && fileType === 'file',
);
if (
!plugin.getData()[file.path] &&
file.name.match(regex) &&
isToRuleApplicable(rule, fileType) &&
!isInfluencedByInheritance
) {
addCustomRuleIconsToDOM(plugin, rule, file);
titleEl.insertBefore(iconNode, titleInnerEl);
}
});
} catch {
// Rule is not applicable to a regex format.
plugin.app.vault.getAllLoadedFiles().forEach(async (file) => {
const fileType = (await plugin.app.vault.adapter.stat(file.path)).type;
const isInfluencedByInheritance = inheritanceFolders.find(
([key]) => file.path.includes(key) && fileType === 'file',
);
if (
!plugin.getData()[file.path] &&
file.name.includes(rule.rule) &&
isToRuleApplicable(rule, fileType) &&
!isInfluencedByInheritance
) {
addCustomRuleIconsToDOM(plugin, rule, file);
}
});
}
};

// Add custom rule icons.
plugin.getSettings().rules.forEach((rule) => {
try {
// Rule is in some sort of regex.
const regex = new RegExp(rule.rule);
plugin.app.vault.getAllLoadedFiles().forEach(async (file) => {
const fileType = (await plugin.app.vault.adapter.stat(file.path)).type;
if (file.name.match(regex) && isToRuleApplicable(rule, fileType)) {
addCustomIconRule(rule, file);
}
});
} catch {
// Rule is not applicable to a regex format.
plugin.app.vault.getAllLoadedFiles().forEach(async (file) => {
const fileType = (await plugin.app.vault.adapter.stat(file.path)).type;
if (file.name.includes(rule.rule) && isToRuleApplicable(rule, fileType)) {
addCustomIconRule(rule, file);
}
});
}
});

if (callback) {
callback();
}
});
};
Expand Down

0 comments on commit ef0bdca

Please sign in to comment.