From 48e39a0cbcbc23232e2bbc7be98adf495a3ff816 Mon Sep 17 00:00:00 2001 From: Yuri Karamian Date: Wed, 14 Oct 2026 00:08:24 +0200 Subject: [PATCH] feat: add colors to notebook icons and 16 more built-in icons --- src/lib/components/NotebookGlyph.svelte | 44 +++++++++ src/lib/components/Sidebar.svelte | 118 ++++++++++++++++++++++-- src/lib/utils/notebook-icons.test.mjs | 30 ++++++ src/lib/utils/notebook-icons.ts | 56 ++++++++++- 4 files changed, 235 insertions(+), 13 deletions(-) create mode 100644 src/lib/utils/notebook-icons.test.mjs diff --git a/src/lib/components/NotebookGlyph.svelte b/src/lib/components/NotebookGlyph.svelte index d0d24fb..b67a0ee 100644 --- a/src/lib/components/NotebookGlyph.svelte +++ b/src/lib/components/NotebookGlyph.svelte @@ -54,6 +54,50 @@ {:else if icon === 'plane'} + {:else if icon === 'music'} + + + + {:else if icon === 'camera'} + + + {:else if icon === 'film'} + + + {:else if icon === 'gamepad'} + + + {:else if icon === 'cart'} + + + + {:else if icon === 'money'} + + {:else if icon === 'health'} + + {:else if icon === 'food'} + + {:else if icon === 'coffee'} + + {:else if icon === 'leaf'} + + + {:else if icon === 'users'} + + + + {:else if icon === 'chat'} + + {:else if icon === 'mail'} + + + {:else if icon === 'lock'} + + + {:else if icon === 'wrench'} + + {:else if icon === 'pen'} + {:else} diff --git a/src/lib/components/Sidebar.svelte b/src/lib/components/Sidebar.svelte index 82c6158..e6625f9 100644 --- a/src/lib/components/Sidebar.svelte +++ b/src/lib/components/Sidebar.svelte @@ -38,11 +38,12 @@ import TagStylePicker from './TagStylePicker.svelte'; import { NOTEBOOK_ICON_OPTIONS, - decodeBuiltinNotebookIcon, + decodeNotebookIcon, encodeBuiltinNotebookIcon, normalizeNotebookIconKey, type NotebookIconId } from '$lib/utils/notebook-icons'; + import { TAG_COLOR_PRESETS } from '$lib/utils/tag-styles'; let { onViewChanged = () => {}, onBeforeNoteChange = async () => true, onBeforePathMutation = async () => null, onPathsMoved = async (_old: string, _next: string) => {} }: { onViewChanged?: () => void; @@ -134,6 +135,7 @@ let contextMenu = $state<{ x: number; y: number; notebook: NotebookEntry } | null>(null); let iconPickerNotebook = $state(null); let iconPickerElement = $state(null); + let iconPickerColor = $state(null); let trashContextMenu = $state<{ x: number; y: number } | null>(null); let tagsCollapsed = $state(true); let tagContextMenu = $state<{ x: number; y: number; tag: string } | null>(null); @@ -633,6 +635,7 @@ async function openIconPicker(nb: NotebookEntry) { contextMenu = null; + iconPickerColor = decodeNotebookIcon(get(notebookIcons)[normalizeNotebookIconKey(nb.relative_path)])?.color ?? null; iconPickerNotebook = nb; await tick(); if (!ownerIsCurrent()) return; @@ -647,22 +650,29 @@ notebookIcons.set(icons); } - async function handleBuiltinIcon(nb: NotebookEntry, icon: NotebookIconId) { + async function handleBuiltinIcon(nb: NotebookEntry, icon: NotebookIconId, close = true) { if (!ownerVault || !ownerIsCurrent()) return; const key = normalizeNotebookIconKey(nb.relative_path); const latest = notebookAppearance.choose(ownerVault, key); - const value = encodeBuiltinNotebookIcon(icon); + const value = encodeBuiltinNotebookIcon(icon, iconPickerColor); iconError = ''; try { await notebookAppearance.run(ownerVault, key, latest, () => setNotebookIcon(ownerVault, key, value), () => publishNotebookIcon(key, value)); - if (ownerIsCurrent() && latest()) iconPickerNotebook = null; + if (close && ownerIsCurrent() && latest()) iconPickerNotebook = null; } catch (e) { if (ownerIsCurrent() && latest()) iconError = String(e); } } + // The color is kept for the next built-in icon when none is set yet. + function handleIconColor(nb: NotebookEntry, color: string | null) { + iconPickerColor = color; + const current = decodeNotebookIcon(get(notebookIcons)[normalizeNotebookIconKey(nb.relative_path)]); + if (current) void handleBuiltinIcon(nb, current.icon, false); + } + async function handleCustomIcon(nb: NotebookEntry) { if (!ownerVault || !ownerIsCurrent()) return; const key = normalizeNotebookIconKey(nb.relative_path); @@ -1069,7 +1079,8 @@ {#each NOTEBOOK_ICON_OPTIONS as option} {/each} +
+ Color +
+ + {#each TAG_COLOR_PRESETS as preset (preset)} + + {/each} + +
+
{/if} -

Recommended size: 1:1 aspect ratio

+

Colors apply to the built-in icons. Recommended image size: 1:1 aspect ratio

@@ -1172,7 +1213,7 @@ {@const hasChildren = nb.children.length > 0} {@const isCollapsed = $collapsedNotebooks.includes(nb.path)} {@const iconSrc = getNotebookIconSrc(nb)} - {@const builtinIcon = decodeBuiltinNotebookIcon($notebookIcons[normalizeNotebookIconKey(nb.relative_path)])} + {@const builtinIcon = decodeNotebookIcon($notebookIcons[normalizeNotebookIconKey(nb.relative_path)])} {#if editingNotebook === nb.path}
{/if} - + {#if builtinIcon} - + {:else if iconSrc} {:else if isCollapsed || nb.children.length === 0} @@ -1928,6 +1969,65 @@ color: var(--accent); } + .icon-color-section { + display: flex; + flex-direction: column; + gap: 8px; + padding: 0 14px 12px; + } + + .icon-color-label { + color: var(--text-secondary); + font-size: 12px; + font-weight: 600; + } + + .icon-swatches { + display: flex; + flex-wrap: wrap; + gap: 7px; + align-items: center; + } + + .icon-swatch { + width: 22px; + height: 22px; + padding: 0; + border: 2px solid transparent; + border-radius: 50%; + background: var(--bg-tertiary); + cursor: pointer; + } + + .icon-swatch:hover, + .icon-swatch.active { + border-color: var(--text-primary); + } + + .icon-swatch.none { + background: + linear-gradient(to bottom right, transparent calc(50% - 1px), var(--text-tertiary) calc(50% - 1px), var(--text-tertiary) calc(50% + 1px), transparent calc(50% + 1px)), + var(--bg-secondary); + border-color: var(--border-color); + } + + .icon-swatch.custom { + display: grid; + place-items: center; + overflow: hidden; + background: conic-gradient(from 90deg, #e11d48, #eab308, #22c55e, #3b82f6, #8b5cf6, #e11d48); + } + + .icon-swatch.custom input { + width: 28px; + height: 28px; + padding: 0; + border: none; + background: none; + cursor: pointer; + transform: scale(1.4); + } + .icon-picker-actions { display: flex; flex-wrap: wrap; diff --git a/src/lib/utils/notebook-icons.test.mjs b/src/lib/utils/notebook-icons.test.mjs new file mode 100644 index 0000000..1201a49 --- /dev/null +++ b/src/lib/utils/notebook-icons.test.mjs @@ -0,0 +1,30 @@ +import assert from 'node:assert/strict'; +import test from 'node:test'; + +const { decodeBuiltinNotebookIcon, decodeNotebookIcon, encodeBuiltinNotebookIcon } = await import( + new URL('./notebook-icons.ts', import.meta.url) +); + +test('encodes a builtin notebook icon with an optional color', () => { + assert.equal(encodeBuiltinNotebookIcon('star'), 'builtin:star'); + assert.equal(encodeBuiltinNotebookIcon('star', null), 'builtin:star'); + assert.equal(encodeBuiltinNotebookIcon('star', '#E11D48'), 'builtin:star#e11d48'); + assert.equal(encodeBuiltinNotebookIcon('star', 'red'), 'builtin:star'); +}); + +test('decodes notebook icons with and without a color', () => { + assert.deepEqual(decodeNotebookIcon('builtin:star'), { icon: 'star', color: null }); + assert.deepEqual(decodeNotebookIcon('builtin:star#e11d48'), { icon: 'star', color: '#e11d48' }); + assert.deepEqual(decodeNotebookIcon('builtin:star#nope'), { icon: 'star', color: null }); +}); + +test('ignores custom images and unknown builtin icons', () => { + assert.equal(decodeNotebookIcon(null), null); + assert.equal(decodeNotebookIcon('.helixnotes/attachments/icon#1.png'), null); + assert.equal(decodeNotebookIcon('builtin:unknown#e11d48'), null); +}); + +test('tag icons stay uncolored builtin ids', () => { + assert.equal(decodeBuiltinNotebookIcon('builtin:star'), 'star'); + assert.equal(decodeBuiltinNotebookIcon('builtin:star#e11d48'), null); +}); diff --git a/src/lib/utils/notebook-icons.ts b/src/lib/utils/notebook-icons.ts index e934136..d30960e 100644 --- a/src/lib/utils/notebook-icons.ts +++ b/src/lib/utils/notebook-icons.ts @@ -1,3 +1,5 @@ +import { isValidTagColor } from './tag-styles.ts'; + export const NOTEBOOK_ICON_OPTIONS = [ { id: 'book', label: 'Book' }, { id: 'folder', label: 'Folder' }, @@ -14,7 +16,23 @@ export const NOTEBOOK_ICON_OPTIONS = [ { id: 'archive', label: 'Archive' }, { id: 'globe', label: 'Globe' }, { id: 'plane', label: 'Travel' }, - { id: 'palette', label: 'Creative' } + { id: 'palette', label: 'Creative' }, + { id: 'music', label: 'Music' }, + { id: 'camera', label: 'Photos' }, + { id: 'film', label: 'Film' }, + { id: 'gamepad', label: 'Games' }, + { id: 'cart', label: 'Shopping' }, + { id: 'money', label: 'Finance' }, + { id: 'health', label: 'Health' }, + { id: 'food', label: 'Food' }, + { id: 'coffee', label: 'Coffee' }, + { id: 'leaf', label: 'Nature' }, + { id: 'users', label: 'People' }, + { id: 'chat', label: 'Chat' }, + { id: 'mail', label: 'Mail' }, + { id: 'lock', label: 'Private' }, + { id: 'wrench', label: 'Tools' }, + { id: 'pen', label: 'Writing' } ] as const; export type NotebookIconId = (typeof NOTEBOOK_ICON_OPTIONS)[number]['id']; @@ -36,15 +54,31 @@ const BUILTIN_IDS: Record = { archive: true, globe: true, plane: true, - palette: true + palette: true, + music: true, + camera: true, + film: true, + gamepad: true, + cart: true, + money: true, + health: true, + food: true, + coffee: true, + leaf: true, + users: true, + chat: true, + mail: true, + lock: true, + wrench: true, + pen: true }; export function normalizeNotebookIconKey(path: string): string { return path.replace(/\\/g, '/'); } -export function encodeBuiltinNotebookIcon(icon: NotebookIconId): string { - return `${BUILTIN_PREFIX}${icon}`; +export function encodeBuiltinNotebookIcon(icon: NotebookIconId, color?: string | null): string { + return `${BUILTIN_PREFIX}${icon}${isValidTagColor(color) ? color.toLowerCase() : ''}`; } export function decodeBuiltinNotebookIcon(value: string | null | undefined): NotebookIconId | null { @@ -52,3 +86,17 @@ export function decodeBuiltinNotebookIcon(value: string | null | undefined): Not const icon = value.slice(BUILTIN_PREFIX.length); return Object.hasOwn(BUILTIN_IDS, icon) ? icon as NotebookIconId : null; } + +/** Notebook icons keep their color in the stored value: `builtin:star#e11d48`. */ +export function decodeNotebookIcon( + value: string | null | undefined +): { icon: NotebookIconId; color: string | null } | null { + const hash = value?.indexOf('#') ?? -1; + if (hash < 0) { + const icon = decodeBuiltinNotebookIcon(value); + return icon ? { icon, color: null } : null; + } + const icon = decodeBuiltinNotebookIcon(value!.slice(0, hash)); + const color = value!.slice(hash); + return icon ? { icon, color: isValidTagColor(color) ? color : null } : null; +}