From d0f75047db25b18a75c86ca87560dbd0efacc248 Mon Sep 17 00:00:00 2001 From: knapior Date: Tue, 31 Dec 2024 18:08:39 +0100 Subject: [PATCH 1/3] Change sort to move directory at the top of file list --- apps/desktop/src/lib/api/collection.ts | 3 ++- .../components/shared/command-menu/helpers.ts | 16 ++++++++++++++++ apps/desktop/src/routes/notes/entries.svelte | 8 +++++++- 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/apps/desktop/src/lib/api/collection.ts b/apps/desktop/src/lib/api/collection.ts index cec8e1e..86ed8f4 100644 --- a/apps/desktop/src/lib/api/collection.ts +++ b/apps/desktop/src/lib/api/collection.ts @@ -5,6 +5,7 @@ import { get } from 'svelte/store'; import { open } from '@tauri-apps/api/dialog'; import { writeTextFile, readTextFile, BaseDirectory } from '@tauri-apps/api/fs'; import type { CollectionParams } from '@/types'; +import { sortFileEntry } from '@/components/shared/command-menu/helpers'; // Fetch the collection entries export const fetchCollectionEntries = async ( @@ -19,7 +20,7 @@ export const fetchCollectionEntries = async ( let files = await readDir(dirPath, { recursive: true }); if (sort === 'name') { - files.sort((a, b) => a.name!.localeCompare(b.name!)); + files.sort((a, b) => sortFileEntry(a, b)); } // Hide dotfiles diff --git a/apps/desktop/src/lib/components/shared/command-menu/helpers.ts b/apps/desktop/src/lib/components/shared/command-menu/helpers.ts index 03806cc..e65201c 100644 --- a/apps/desktop/src/lib/components/shared/command-menu/helpers.ts +++ b/apps/desktop/src/lib/components/shared/command-menu/helpers.ts @@ -36,3 +36,19 @@ export const getAllItems = async ( return items; }; + +export const sortFileEntry = (a: FileEntry, b: FileEntry): number => { + const isDirectory = (file: FileEntry) => file.children != null; + + if (isDirectory(a) && isDirectory(b)) { + return a.name!.localeCompare(b.name!); + } + if (isDirectory(a)) { + return -1; + } + if (isDirectory(b)) { + return 1; + } + + return a.name!.localeCompare(b.name!); +}; diff --git a/apps/desktop/src/routes/notes/entries.svelte b/apps/desktop/src/routes/notes/entries.svelte index 443882f..a9d4c22 100644 --- a/apps/desktop/src/routes/notes/entries.svelte +++ b/apps/desktop/src/routes/notes/entries.svelte @@ -12,6 +12,7 @@ import { cn } from '@haptic/ui/lib/utils'; import type { FileEntry } from '@tauri-apps/api/fs'; import { get } from 'svelte/store'; + import { sortFileEntry } from '@/components/shared/command-menu/helpers'; export let entries: FileEntry[]; export let toggleState: 'collapse' | 'expand'; @@ -274,6 +275,11 @@ // Remove dragover event listener from document document.removeEventListener('dragover', handleDragOver); } + + function sort(entry: FileEntry) { + entry.children!.sort((a, b) => sortFileEntry(a, b)); + return entry.children; + } {#each entries as entry, i} @@ -463,7 +469,7 @@ - + {:else} From 9ca98bc0468049d6b2f79128e435a188b32a5fa7 Mon Sep 17 00:00:00 2001 From: Christo Todorov Date: Sat, 4 Jan 2025 18:34:03 +0100 Subject: [PATCH 2/3] refactor: small adjustments --- .../components/shared/command-menu/helpers.ts | 16 ++++++++-------- apps/desktop/src/routes/notes/entries.svelte | 7 +------ 2 files changed, 9 insertions(+), 14 deletions(-) diff --git a/apps/desktop/src/lib/components/shared/command-menu/helpers.ts b/apps/desktop/src/lib/components/shared/command-menu/helpers.ts index e65201c..f55faea 100644 --- a/apps/desktop/src/lib/components/shared/command-menu/helpers.ts +++ b/apps/desktop/src/lib/components/shared/command-menu/helpers.ts @@ -41,14 +41,14 @@ export const sortFileEntry = (a: FileEntry, b: FileEntry): number => { const isDirectory = (file: FileEntry) => file.children != null; if (isDirectory(a) && isDirectory(b)) { - return a.name!.localeCompare(b.name!); - } - if (isDirectory(a)) { - return -1; - } - if (isDirectory(b)) { - return 1; + return naturalSort(a.name!, b.name!); } + if (isDirectory(a)) return -1; + if (isDirectory(b)) return 1; + + return naturalSort(a.name!, b.name!); +}; - return a.name!.localeCompare(b.name!); +const naturalSort = (a: string, b: string): number => { + return a.localeCompare(b, undefined, { numeric: true, sensitivity: 'base' }); }; diff --git a/apps/desktop/src/routes/notes/entries.svelte b/apps/desktop/src/routes/notes/entries.svelte index a9d4c22..290d492 100644 --- a/apps/desktop/src/routes/notes/entries.svelte +++ b/apps/desktop/src/routes/notes/entries.svelte @@ -275,11 +275,6 @@ // Remove dragover event listener from document document.removeEventListener('dragover', handleDragOver); } - - function sort(entry: FileEntry) { - entry.children!.sort((a, b) => sortFileEntry(a, b)); - return entry.children; - } {#each entries as entry, i} @@ -469,7 +464,7 @@ - + sortFileEntry(a, b))} /> {:else} From 002a6b935d91c070a46403da33a2669ec107eb57 Mon Sep 17 00:00:00 2001 From: Christo Todorov Date: Sat, 4 Jan 2025 18:40:37 +0100 Subject: [PATCH 3/3] fix: add same sorting to web and small polishing --- apps/desktop/src/lib/api/collection.ts | 3 +-- .../components/shared/command-menu/helpers.ts | 16 ---------------- apps/desktop/src/lib/utils.ts | 14 ++++++++++++++ apps/desktop/src/routes/notes/entries.svelte | 3 +-- apps/web/src/lib/api/collection.ts | 4 ++-- apps/web/src/lib/utils.ts | 16 ++++++++++++++++ 6 files changed, 34 insertions(+), 22 deletions(-) diff --git a/apps/desktop/src/lib/api/collection.ts b/apps/desktop/src/lib/api/collection.ts index 86ed8f4..d282e5e 100644 --- a/apps/desktop/src/lib/api/collection.ts +++ b/apps/desktop/src/lib/api/collection.ts @@ -1,11 +1,10 @@ import { activeFile, collection, noteHistory } from '@/store'; -import { hideDotFiles, validateHapticFolder } from '@/utils'; +import { hideDotFiles, validateHapticFolder, sortFileEntry } from '@/utils'; import { readDir } from '@tauri-apps/api/fs'; import { get } from 'svelte/store'; import { open } from '@tauri-apps/api/dialog'; import { writeTextFile, readTextFile, BaseDirectory } from '@tauri-apps/api/fs'; import type { CollectionParams } from '@/types'; -import { sortFileEntry } from '@/components/shared/command-menu/helpers'; // Fetch the collection entries export const fetchCollectionEntries = async ( diff --git a/apps/desktop/src/lib/components/shared/command-menu/helpers.ts b/apps/desktop/src/lib/components/shared/command-menu/helpers.ts index f55faea..03806cc 100644 --- a/apps/desktop/src/lib/components/shared/command-menu/helpers.ts +++ b/apps/desktop/src/lib/components/shared/command-menu/helpers.ts @@ -36,19 +36,3 @@ export const getAllItems = async ( return items; }; - -export const sortFileEntry = (a: FileEntry, b: FileEntry): number => { - const isDirectory = (file: FileEntry) => file.children != null; - - if (isDirectory(a) && isDirectory(b)) { - return naturalSort(a.name!, b.name!); - } - if (isDirectory(a)) return -1; - if (isDirectory(b)) return 1; - - return naturalSort(a.name!, b.name!); -}; - -const naturalSort = (a: string, b: string): number => { - return a.localeCompare(b, undefined, { numeric: true, sensitivity: 'base' }); -}; diff --git a/apps/desktop/src/lib/utils.ts b/apps/desktop/src/lib/utils.ts index 547108d..1c11527 100644 --- a/apps/desktop/src/lib/utils.ts +++ b/apps/desktop/src/lib/utils.ts @@ -303,3 +303,17 @@ export const getNextUntitledName = (files: FileEntry[], prefix: string, extensio // This should never happen, but just in case return `${prefix} ${maxNumber + 1}${extension}`; }; + +export const sortFileEntry = (a: FileEntry, b: FileEntry): number => { + const isDirectory = (file: FileEntry) => file.children != null; + if (isDirectory(a) && isDirectory(b)) { + return naturalSort(a.name!, b.name!); + } + if (isDirectory(a)) return -1; + if (isDirectory(b)) return 1; + return naturalSort(a.name!, b.name!); +}; + +const naturalSort = (a: string, b: string): number => { + return a.localeCompare(b, undefined, { numeric: true, sensitivity: 'base' }); +}; diff --git a/apps/desktop/src/routes/notes/entries.svelte b/apps/desktop/src/routes/notes/entries.svelte index 290d492..443882f 100644 --- a/apps/desktop/src/routes/notes/entries.svelte +++ b/apps/desktop/src/routes/notes/entries.svelte @@ -12,7 +12,6 @@ import { cn } from '@haptic/ui/lib/utils'; import type { FileEntry } from '@tauri-apps/api/fs'; import { get } from 'svelte/store'; - import { sortFileEntry } from '@/components/shared/command-menu/helpers'; export let entries: FileEntry[]; export let toggleState: 'collapse' | 'expand'; @@ -464,7 +463,7 @@ - sortFileEntry(a, b))} /> + {:else} diff --git a/apps/web/src/lib/api/collection.ts b/apps/web/src/lib/api/collection.ts index bd51ba8..0de4d30 100644 --- a/apps/web/src/lib/api/collection.ts +++ b/apps/web/src/lib/api/collection.ts @@ -2,7 +2,7 @@ import { db } from '@/database/client'; import { collection as collectionTable, entry as entryTable } from '@/database/schema'; import { activeFile, collection, collectionEntries, noteHistory } from '@/store'; import type { FileEntry } from '@/types'; -import { buildFileTree } from '@/utils'; +import { buildFileTree, sortFileEntry } from '@/utils'; import { and, eq } from 'drizzle-orm'; import { get } from 'svelte/store'; @@ -41,7 +41,7 @@ export const fetchCollectionEntries = async ( const sortEntries = (entries: FileEntry[]) => { entries.sort((a, b) => { if (sort === 'name' && a.name && b.name) { - return a.name.localeCompare(b.name); + return sortFileEntry(a, b); } else if (sort === 'date') { console.warn('Sorting by date is not implemented yet'); } diff --git a/apps/web/src/lib/utils.ts b/apps/web/src/lib/utils.ts index de0120f..9cae40d 100644 --- a/apps/web/src/lib/utils.ts +++ b/apps/web/src/lib/utils.ts @@ -417,3 +417,19 @@ export function createDeviceDetector() { } ); } + +export const sortFileEntry = (a: FileEntry, b: FileEntry): number => { + const isDirectory = (file: FileEntry) => file.children != null; + + if (isDirectory(a) && isDirectory(b)) { + return naturalSort(a.name!, b.name!); + } + if (isDirectory(a)) return -1; + if (isDirectory(b)) return 1; + + return naturalSort(a.name!, b.name!); +}; + +const naturalSort = (a: string, b: string): number => { + return a.localeCompare(b, undefined, { numeric: true, sensitivity: 'base' }); +};