Skip to content

Commit 0be3834

Browse files
committed
refactor: use toggle for adding and removing lucide icon pack (#527)
1 parent a71926c commit 0be3834

File tree

6 files changed

+74
-42
lines changed

6 files changed

+74
-42
lines changed

src/icon-pack-manager.ts

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -61,26 +61,28 @@ export const setIconPacks = (newIconPacks: IconPack[]): void => {
6161
iconPacks = newIconPacks;
6262
};
6363

64-
export const addLucideIconsPack = (): void => {
64+
export const addLucideIconsPack = (plugin: IconizePlugin): void => {
6565
iconPacks.push({
6666
name: LUCIDE_ICON_PACK_NAME,
6767
prefix: 'Li',
6868
custom: false,
69-
icons: getIconIds()
70-
.map((iconId) => iconId.replace(/^lucide-/, ''))
71-
.map((iconId) => {
72-
const iconEl = getIcon(iconId);
73-
iconEl.removeClass('svg-icon'); // Removes native `svg-icon` class.
74-
return {
75-
name: getNormalizedName(iconId),
76-
filename: iconId,
77-
prefix: 'Li',
78-
svgElement: iconEl?.outerHTML,
79-
svgContent: iconEl?.innerHTML,
80-
svgViewbox: '',
81-
iconPackName: LUCIDE_ICON_PACK_NAME,
82-
};
83-
}),
69+
icons: plugin.doesUseNativeLucideIconPack()
70+
? getIconIds()
71+
.map((iconId) => iconId.replace(/^lucide-/, ''))
72+
.map((iconId) => {
73+
const iconEl = getIcon(iconId);
74+
iconEl.removeClass('svg-icon'); // Removes native `svg-icon` class.
75+
return {
76+
name: getNormalizedName(iconId),
77+
filename: iconId,
78+
prefix: 'Li',
79+
svgElement: iconEl?.outerHTML,
80+
svgContent: iconEl?.innerHTML,
81+
svgViewbox: '',
82+
iconPackName: LUCIDE_ICON_PACK_NAME,
83+
};
84+
})
85+
: [],
8486
});
8587
};
8688

@@ -374,12 +376,12 @@ export const createIconPackPrefix = (iconPackName: string): string => {
374376
};
375377

376378
export const loadUsedIcons = async (plugin: IconizePlugin, icons: string[]) => {
377-
const iconPacks = [
378-
...(await listPath(plugin)).folders.map((iconPack) =>
379-
iconPack.split('/').pop(),
380-
),
381-
LUCIDE_ICON_PACK_NAME,
382-
];
379+
const iconPacks = (await listPath(plugin)).folders.map((iconPack) =>
380+
iconPack.split('/').pop(),
381+
);
382+
if (plugin.doesUseNativeLucideIconPack()) {
383+
iconPacks.push(LUCIDE_ICON_PACK_NAME);
384+
}
383385

384386
for (let i = 0; i < icons.length; i++) {
385387
const entry = icons[i];
@@ -431,7 +433,7 @@ export const loadIcon = async (
431433

432434
if (
433435
iconPack === LUCIDE_ICON_PACK_NAME &&
434-
!plugin.getSettings().useCustomLucideIconPack
436+
plugin.doesUseNativeLucideIconPack()
435437
) {
436438
// Native lucide icons already exist for Obsidian.
437439
const lucideIcons = iconPacks.find(
@@ -524,7 +526,7 @@ export const initIconPacks = async (plugin: IconizePlugin): Promise<void> => {
524526
const prefix = createIconPackPrefix(zipFile);
525527
if (
526528
zipFile === LUCIDE_ICON_PACK_NAME &&
527-
!plugin.getSettings().useCustomLucideIconPack
529+
!plugin.doesUseCustomLucideIconPack()
528530
) {
529531
continue;
530532
}

src/lib/icon.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import iconTabs from './icon-tabs';
77
import { getFileItemInnerTitleEl, getFileItemTitleEl } from '@app/util';
88
import {
99
Icon,
10+
LUCIDE_ICON_PACK_NAME,
1011
extractIconToIconPack,
1112
getAllIconPacks,
1213
getIconFromIconPack,
@@ -36,7 +37,10 @@ const checkMissingIcons = async (
3637
const iconPrefix = iconNameWithPrefix.substring(0, iconNextIdentifier);
3738
const iconPackName = getIconPackNameByPrefix(iconPrefix);
3839

39-
if (!plugin.getSettings().useCustomLucideIconPack) {
40+
if (
41+
iconPackName === LUCIDE_ICON_PACK_NAME &&
42+
!plugin.doesUseCustomLucideIconPack()
43+
) {
4044
return;
4145
}
4246

src/main.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,9 @@ export default class IconizePlugin extends Plugin {
108108
await migrate(this);
109109

110110
const usedIconNames = icon.getAllWithPath(this).map((value) => value.icon);
111-
addLucideIconsPack();
111+
if (!this.doesUseCustomLucideIconPack()) {
112+
addLucideIconsPack(this);
113+
}
112114
await loadUsedIcons(this, usedIconNames);
113115

114116
this.app.workspace.onLayoutReady(() => this.handleChangeLayout());
@@ -914,6 +916,14 @@ export default class IconizePlugin extends Plugin {
914916
return this.registeredFileExplorers;
915917
}
916918

919+
doesUseCustomLucideIconPack(): boolean {
920+
return this.getSettings().lucideIconPackType === 'custom';
921+
}
922+
923+
doesUseNativeLucideIconPack(): boolean {
924+
return this.getSettings().lucideIconPackType === 'native';
925+
}
926+
917927
/**
918928
* Returns a possible data path by the given value. This function checks for
919929
* direct icon and custom rules.

src/settings/data.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ export enum IconInTitlePosition {
55
Inline = 'inline',
66
}
77

8+
export type LucideIconPackType = 'none' | 'custom' | 'native';
9+
810
export interface ExtraMarginSettings {
911
/**
1012
* Controls the extra margin on the top of the icon.
@@ -166,11 +168,13 @@ export interface IconFolderSettings {
166168
*/
167169
iconIdentifier: string;
168170
/**
169-
* Whether to use the custom or native lucide icon pack or not.
170-
* `false` refers to using the native lucide icon pack.
171-
* @default false
171+
* Specifies which Lucide icon pack to use.
172+
* - 'none': Don't use any Lucide icon pack
173+
* - 'native': Use the native Lucide icon pack
174+
* - 'custom': Use a custom Lucide icon pack
175+
* @default 'native'
172176
*/
173-
useCustomLucideIconPack: boolean;
177+
lucideIconPackType: LucideIconPackType;
174178
/**
175179
* Sets whether the plugin should be in debug mode. This will enable more logging
176180
* in the console.
@@ -203,6 +207,6 @@ export const DEFAULT_SETTINGS: IconFolderSettings = {
203207
iconsInNotesEnabled: true,
204208
iconsInLinksEnabled: true,
205209
iconIdentifier: ':',
206-
useCustomLucideIconPack: false,
210+
lucideIconPackType: 'native',
207211
debugMode: false,
208212
};

src/settings/ui/customIconPack.ts

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
import IconizePlugin from '@app/main';
1616
import { readFileSync } from '@app/util';
1717
import icon from '@app/lib/icon';
18+
import { LucideIconPackType } from '../data';
1819

1920
export default class CustomIconPackSetting extends IconFolderSetting {
2021
private textComponent: TextComponent;
@@ -104,7 +105,14 @@ export default class CustomIconPackSetting extends IconFolderSetting {
104105
});
105106
});
106107

107-
getAllIconPacks().forEach((iconPack) => {
108+
// Sorts lucide icon pack always to the top.
109+
const iconPacks = [...getAllIconPacks()].sort((a, b) => {
110+
if (a.name === LUCIDE_ICON_PACK_NAME) return -1;
111+
if (b.name === LUCIDE_ICON_PACK_NAME) return 1;
112+
return a.name.localeCompare(b.name);
113+
});
114+
115+
iconPacks.forEach((iconPack) => {
108116
const isLucideIconPack = iconPack.name === LUCIDE_ICON_PACK_NAME;
109117
const additionalLucideDescription =
110118
'(Native Pack has fewer icons but 100% Obsidian Sync support)';
@@ -147,17 +155,21 @@ export default class CustomIconPackSetting extends IconFolderSetting {
147155
// });
148156

149157
if (isLucideIconPack) {
150-
iconPackSetting.addToggle((toggle) => {
151-
toggle.setTooltip('Use custom Lucide Icons Pack');
152-
toggle.setValue(this.plugin.getSettings().useCustomLucideIconPack);
153-
toggle.onChange(async (value) => {
154-
toggle.setDisabled(true);
158+
iconPackSetting.addDropdown((dropdown) => {
159+
dropdown.addOptions({
160+
native: 'Native',
161+
custom: 'Custom',
162+
none: 'None',
163+
} satisfies Record<LucideIconPackType, string>);
164+
dropdown.setValue(this.plugin.getSettings().lucideIconPackType);
165+
dropdown.onChange(async (value: LucideIconPackType) => {
166+
dropdown.setDisabled(true);
155167
new Notice('Changing icon packs...');
156-
this.plugin.getSettings().useCustomLucideIconPack = value;
168+
this.plugin.getSettings().lucideIconPackType = value;
157169
await this.plugin.saveIconFolderData();
158-
if (!value) {
170+
if (value === 'native' || value === 'none') {
159171
await removeCustomLucideIconPack(this.plugin);
160-
addLucideIconsPack();
172+
addLucideIconsPack(this.plugin);
161173
} else {
162174
await addCustomLucideIconPack(this.plugin);
163175
await icon.checkMissingIcons(
@@ -166,7 +178,7 @@ export default class CustomIconPackSetting extends IconFolderSetting {
166178
);
167179
}
168180

169-
toggle.setDisabled(false);
181+
dropdown.setDisabled(false);
170182
new Notice('Done. This change requires a restart of Obsidian');
171183
});
172184
});

src/util.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export const saveIconToIconPack = (
9191
const iconPackName = getIconPackNameByPrefix(iconPrefix);
9292
if (
9393
iconPackName === LUCIDE_ICON_PACK_NAME &&
94-
!plugin.getSettings().useCustomLucideIconPack
94+
!plugin.doesUseCustomLucideIconPack()
9595
) {
9696
return;
9797
}

0 commit comments

Comments
 (0)