Skip to content

Commit

Permalink
Merge pull request #32 from knapior/main
Browse files Browse the repository at this point in the history
  • Loading branch information
chroxify authored Jan 4, 2025
2 parents 4f3d741 + 002a6b9 commit fee3f24
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
4 changes: 2 additions & 2 deletions apps/desktop/src/lib/api/collection.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
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';
Expand All @@ -19,7 +19,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
Expand Down
14 changes: 14 additions & 0 deletions apps/desktop/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' });
};
4 changes: 2 additions & 2 deletions apps/web/src/lib/api/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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');
}
Expand Down
16 changes: 16 additions & 0 deletions apps/web/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' });
};

0 comments on commit fee3f24

Please sign in to comment.