Skip to content

Commit

Permalink
refactor: minor icon dom improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianWoelki committed Sep 17, 2023
1 parent 4824e30 commit d650068
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 19 deletions.
1 change: 0 additions & 1 deletion src/MetaData.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

export default class MetaData {
public static pluginName: string;
}
28 changes: 11 additions & 17 deletions src/lib/util/dom.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { FileItem } from '@app/@types/obsidian';
import twemoji from 'twemoji';
import { getSvgFromLoadedIcon, nextIdentifier } from '../../iconPackManager';
import IconFolderPlugin from '../../main';
Expand Down Expand Up @@ -110,11 +109,6 @@ interface CreateOptions {
* @param color Optional color of the icon to add.
*/
const createIconNode = (plugin: IconFolderPlugin, path: string, iconName: string, options?: CreateOptions): void => {
// TODO: Refactor to more efficient solution.
if (plugin.getData()[path]) {
removeIconInPath(path, { container: options?.container });
}

// Get the container from the provided options or try to find the node that has the
// path from the document itself.
const node = options?.container ?? document.querySelector(`[data-path="${path}"]`);
Expand All @@ -134,19 +128,19 @@ const createIconNode = (plugin: IconFolderPlugin, path: string, iconName: string
}
}

// Check for possible inheritance and remove the inherited icon node.
const possibleInheritanceIcon = node.querySelector('.obsidian-icon-folder-icon');
if (possibleInheritanceIcon) {
possibleInheritanceIcon.remove();
}

// Creates a new icon node and inserts it to the DOM.
const iconNode = document.createElement('div');
iconNode.classList.add('obsidian-icon-folder-icon');
let iconNode: HTMLDivElement = node.querySelector('.obsidian-icon-folder-icon');
// If the icon is already set in the path, we do not need to create a new div element.
if (iconNode) {
setIconForNode(plugin, iconName, iconNode, options?.color);
} else {
// Creates a new icon node and inserts it to the DOM.
iconNode = document.createElement('div');
iconNode.classList.add('obsidian-icon-folder-icon');

setIconForNode(plugin, iconName, iconNode, options?.color);
setIconForNode(plugin, iconName, iconNode, options?.color);

node.insertBefore(iconNode, titleNode);
node.insertBefore(iconNode, titleNode);
}
};

/**
Expand Down
3 changes: 2 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ export default class IconFolderPlugin extends Plugin {
console.log('...icons migrated');
}

// @ts-ignore
// @ts-ignore - Required because an older version of the plugin saved the `migrated`
// property as a boolean instead of a number.
if (this.getSettings().migrated === true) {
this.getSettings().migrated = 1;
}
Expand Down

0 comments on commit d650068

Please sign in to comment.