Skip to content

Commit

Permalink
fix: issues with icons and custom rule icons
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianWoelki committed Jul 5, 2022
1 parent ef0bdca commit 3fe0ee2
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export interface FolderIconObject {

export default class IconFolderPlugin extends Plugin {
private data: Record<string, boolean | string | IconFolderSettings | FolderIconObject>;
private registeredFileExplorers = new WeakSet<ExplorerView>();
private registeredFileExplorers = new Set<ExplorerView>();

async onload() {
MetaData.pluginName = this.manifest.id;
Expand Down Expand Up @@ -319,7 +319,7 @@ export default class IconFolderPlugin extends Plugin {
return this.data;
}

getRegisteredFileExplorers(): WeakSet<ExplorerView> {
getRegisteredFileExplorers(): Set<ExplorerView> {
return this.registeredFileExplorers;
}

Expand Down
53 changes: 41 additions & 12 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,8 @@ export const refreshIconStyle = (plugin: IconFolderPlugin): void => {
* @public
* @param {string} path - The path toe the to be removed DOM element.
*/
export const removeFromDOM = (path: string): void => {
const node = document.querySelector(`[data-path="${path}"]`);
export const removeFromDOM = (path: string, el?: HTMLElement): void => {
const node = el ?? document.querySelector(`[data-path="${path}"]`);
if (!node) {
console.error('element with data path not found', path);
return;
Expand Down Expand Up @@ -325,11 +325,25 @@ export const doesCustomRuleIconExists = (rule: CustomRule, path: string): boolea
* @param {CustomRule} rule - Specific rule that will match all loaded files.
*/
export const removeCustomRuleIconsFromDOM = (plugin: IconFolderPlugin, rule: CustomRule): void => {
plugin.app.vault.getAllLoadedFiles().forEach(async (file) => {
const fileType = (await plugin.app.vault.adapter.stat(file.path)).type;
if (doesCustomRuleIconExists(rule, file.path) && isToRuleApplicable(rule, fileType)) {
removeFromDOM(file.path);
}
const inheritanceFolders = Object.entries(plugin.getData()).filter(
([k, v]) => k !== 'settings' && typeof v === 'object',
);

plugin.getRegisteredFileExplorers().forEach(async (explorerView) => {
const files = Object.entries(explorerView.fileItems);
files.forEach(async ([path, fileItem]) => {
const fileType = (await plugin.app.vault.adapter.stat(path)).type;
const dataFile =
typeof plugin.getData()[path] === 'object'
? (plugin.getData()[path] as FolderIconObject).iconName
: plugin.getData()[path];
const isInfluencedByInheritance = inheritanceFolders.find(([key]) => path.includes(key) && fileType === 'file');

const existingIcon = dataFile || isInfluencedByInheritance;
if (!existingIcon && doesCustomRuleIconExists(rule, path) && isToRuleApplicable(rule, fileType)) {
removeFromDOM(path, fileItem.titleEl);
}
});
});
};

Expand Down Expand Up @@ -381,11 +395,26 @@ export const addCustomRuleIconsToDOM = async (
addToDOM(plugin, file.path, rule.icon, rule.color);
}
} else {
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)) {
addToDOM(plugin, file.path, rule.icon, rule.color);
}
plugin.getRegisteredFileExplorers().forEach(async (explorerView) => {
const files = Object.entries(explorerView.fileItems);
files.forEach(async ([path, fileItem]) => {
const fileType = (await plugin.app.vault.adapter.stat(path)).type;
if (fileItem) {
if (path.match(regex) && isToRuleApplicable(rule, fileType)) {
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');

insertIconToNode(plugin, rule.icon, iconNode);

titleEl.insertBefore(iconNode, titleInnerEl);
}
}
}
});
});
}
} catch {
Expand Down

0 comments on commit 3fe0ee2

Please sign in to comment.