Skip to content

Commit

Permalink
fix: icon in title when changing an icon
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianWoelki committed Oct 14, 2023
1 parent a0dac40 commit 9b41bca
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 12 deletions.
30 changes: 24 additions & 6 deletions src/lib/icon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,12 +335,14 @@ const getAllWithPath = (plugin: IconFolderPlugin): IconWithPath[] => {
return result;
};

const getIconByPath = (plugin: IconFolderPlugin, path: string): Icon | null => {
const iconNameWithPrefix = getByPath(plugin, path);
if (!iconNameWithPrefix) {
return null;
}

/**
* Returns the {@link Icon} for the given icon name. It is important, that the icon name
* contains the icon pack prefix.
* @param iconNameWithPrefix String that contains the icon pack prefix combined with the
* icon name.
* @returns Icon if it exists, `null` otherwise.
*/
const getIconByName = (iconNameWithPrefix: string): Icon | null => {
const iconNextIdentifier = nextIdentifier(iconNameWithPrefix);
const iconName = iconNameWithPrefix.substring(iconNextIdentifier);
const iconPrefix = iconNameWithPrefix.substring(0, iconNextIdentifier);
Expand All @@ -353,10 +355,26 @@ const getIconByPath = (plugin: IconFolderPlugin, path: string): Icon | null => {
return icon;
};

/**
* Returns the {@link Icon} for the given path.
* @param plugin IconFolderPlugin instance.
* @param path String which is the path to get the icon of.
* @returns Icon if it exists, `null` otherwise.
*/
const getIconByPath = (plugin: IconFolderPlugin, path: string): Icon | null => {
const iconNameWithPrefix = getByPath(plugin, path);
if (!iconNameWithPrefix) {
return null;
}

return getIconByName(iconNameWithPrefix);
};

export default {
addAll,
getByPath,
getAllWithPath,
getIconByPath,
getIconByName,
checkMissingIcons,
};
37 changes: 31 additions & 6 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,18 +144,23 @@ export default class IconFolderPlugin extends Plugin {
const modal = new IconsPickerModal(this.app, this, file.path);
modal.open();

// Update icon in tab when setting is enabled.
if (this.getSettings().iconInTabsEnabled) {
modal.onSelect = (iconName: string): void => {
modal.onSelect = (iconName: string): void => {
// Update icon in tab when setting is enabled.
if (this.getSettings().iconInTabsEnabled) {
const tabLeaves = iconTabs.getTabLeavesOfFilePath(
this,
file.path,
);
for (const tabLeaf of tabLeaves) {
iconTabs.update(this, iconName, tabLeaf.tabHeaderInnerIconEl);
}
};
}
}

// Update icon in title when setting is enabled.
if (this.getSettings().iconInTitleEnabled) {
this.addIconInTitle(iconName);
}
};
});
};

Expand All @@ -178,6 +183,7 @@ export default class IconFolderPlugin extends Plugin {
inheritance.add(this, folderPath, iconName, {
file,
onAdd: (file) => {
// Update icon in tab when setting is enabled.
if (this.getSettings().iconInTabsEnabled) {
const tabLeaves = iconTabs.getTabLeavesOfFilePath(
this,
Expand All @@ -194,6 +200,11 @@ export default class IconFolderPlugin extends Plugin {
);
}
}

// Update icon in title when setting is enabled.
if (this.getSettings().iconInTitleEnabled) {
this.addIconInTitle(iconName);
}
},
});
}
Expand Down Expand Up @@ -351,7 +362,6 @@ export default class IconFolderPlugin extends Plugin {

// Adds the title icon to the active leaf view.
if (this.getSettings().iconInTitleEnabled) {
// const usedIconsWithPaths = icon.getAllWithPath(this);
for (const openedFile of getAllOpenedFiles(this)) {
const iconName = icon.getByPath(this, openedFile.path);
const activeView = openedFile.leaf.view;
Expand Down Expand Up @@ -570,6 +580,21 @@ export default class IconFolderPlugin extends Plugin {
return fontSize * inlineTitleSize;
}

addIconInTitle(iconName: string): void {
for (const openedFile of getAllOpenedFiles(this)) {
const activeView = openedFile.leaf.view;
if (activeView instanceof MarkdownView) {
const possibleIcon = icon.getIconByName(iconName);

if (possibleIcon) {
titleIcon.add(activeView.contentEl, possibleIcon.svgElement, {
fontSize: this.calculateIconInTitleSize(),
});
}
}
}
}

private saveInheritanceData(
folderPath: string,
icon: Icon | string | null,
Expand Down

0 comments on commit 9b41bca

Please sign in to comment.