mirror of
https://github.com/aleksey-hoffman/sigma-file-manager.git
synced 2026-10-19 01:31:05 -05:00
Refine: extensions API
This commit is contained in:
1 parent
9ec716863a
commit
6a143e37ad
4 files changed
+51
-1
No files matched your search
@@ -91,4 +91,13 @@ describe('createViewAPI', () => {
|
||||
expect(userSettingsStoreMock.set).toHaveBeenCalledWith('navigator.gridSortColumn', 'modified');
|
||||
expect(userSettingsStoreMock.set).toHaveBeenCalledWith('navigator.gridSortDirection', 'desc');
|
||||
});
|
||||
|
||||
it('rejects invalid sort order values', async () => {
|
||||
const viewApi = createViewAPI(createContext(['view']));
|
||||
|
||||
await expect(viewApi.setSorting({
|
||||
by: 'name',
|
||||
order: 'ascending' as never,
|
||||
})).rejects.toThrow('Invalid sort order: ascending');
|
||||
});
|
||||
});
|
||||
@@ -929,6 +929,12 @@ function createBridge() {
|
||||
http: {
|
||||
request: makeCall('http.request'),
|
||||
},
|
||||
view: {
|
||||
getLayout: makeCall('view.getLayout'),
|
||||
setLayout: makeCall('view.setLayout'),
|
||||
getSorting: makeCall('view.getSorting'),
|
||||
setSorting: makeCall('view.setSorting'),
|
||||
},
|
||||
settings: {
|
||||
get: makeCall('settings.get'),
|
||||
set: makeCall('settings.set'),
|
||||
|
||||
@@ -7,7 +7,9 @@ import type { UserSettingsNavigator } from '@/types/user-settings';
|
||||
import {
|
||||
buildExtensionViewSortingUpdates,
|
||||
isExtensionViewLayout,
|
||||
isExtensionViewSortDirection,
|
||||
normalizeExtensionViewSortColumn,
|
||||
normalizeExtensionViewSortDirection,
|
||||
readExtensionViewLayout,
|
||||
readExtensionViewSorting,
|
||||
toNavigatorLayoutType,
|
||||
@@ -52,6 +54,19 @@ describe('extension-view-settings', () => {
|
||||
expect(normalizeExtensionViewSortColumn('type')).toBe('kind');
|
||||
});
|
||||
|
||||
it('validates supported sort directions', () => {
|
||||
expect(isExtensionViewSortDirection('asc')).toBe(true);
|
||||
expect(isExtensionViewSortDirection('desc')).toBe(true);
|
||||
expect(isExtensionViewSortDirection('ascending')).toBe(false);
|
||||
});
|
||||
|
||||
it('rejects invalid sort order values', () => {
|
||||
expect(() => buildExtensionViewSortingUpdates(createNavigator(), 'list', {
|
||||
by: 'name',
|
||||
order: 'ascending' as never,
|
||||
})).toThrow('Invalid sort order: ascending');
|
||||
});
|
||||
|
||||
it('reads layout and sorting for the active view', () => {
|
||||
const navigator = createNavigator({
|
||||
layout: {
|
||||
|
||||
@@ -6,6 +6,7 @@ import type {
|
||||
ExtensionViewLayout,
|
||||
ExtensionViewSetSortingOptions,
|
||||
ExtensionViewSortColumn,
|
||||
ExtensionViewSortDirection,
|
||||
ExtensionViewSorting,
|
||||
} from '@sigma-file-manager/api';
|
||||
import type {
|
||||
@@ -29,6 +30,11 @@ const EXTENSION_VIEW_LAYOUTS: readonly ExtensionViewLayout[] = [
|
||||
'grid',
|
||||
];
|
||||
|
||||
const EXTENSION_VIEW_SORT_DIRECTIONS: readonly ExtensionViewSortDirection[] = [
|
||||
'asc',
|
||||
'desc',
|
||||
];
|
||||
|
||||
const EXTENSION_VIEW_SORT_COLUMN_ALIASES: Record<string, ExtensionViewSortColumn> = {
|
||||
dateModified: 'modified',
|
||||
dateCreated: 'created',
|
||||
@@ -39,6 +45,10 @@ export function isExtensionViewLayout(value: string): value is ExtensionViewLayo
|
||||
return EXTENSION_VIEW_LAYOUTS.includes(value as ExtensionViewLayout);
|
||||
}
|
||||
|
||||
export function isExtensionViewSortDirection(value: string): value is ExtensionViewSortDirection {
|
||||
return EXTENSION_VIEW_SORT_DIRECTIONS.includes(value as ExtensionViewSortDirection);
|
||||
}
|
||||
|
||||
export function normalizeExtensionViewSortColumn(value: string): ExtensionViewSortColumn {
|
||||
const normalizedValue = EXTENSION_VIEW_SORT_COLUMN_ALIASES[value] ?? value;
|
||||
|
||||
@@ -49,6 +59,14 @@ export function normalizeExtensionViewSortColumn(value: string): ExtensionViewSo
|
||||
return normalizedValue;
|
||||
}
|
||||
|
||||
export function normalizeExtensionViewSortDirection(value: string): ExtensionViewSortDirection {
|
||||
if (!isExtensionViewSortDirection(value)) {
|
||||
throw new Error(`Invalid sort order: ${value}`);
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
export function toNavigatorLayoutType(mode: ExtensionViewLayout): NavigatorLayout['type'] {
|
||||
const layoutTitleByName = {
|
||||
'compact-list': 'compactListLayout',
|
||||
@@ -98,6 +116,8 @@ export function buildExtensionViewSortingUpdates(
|
||||
const settingKeys = getNavigatorSortSettingKeys(sortLayout);
|
||||
|
||||
if (options.order !== undefined) {
|
||||
const order = normalizeExtensionViewSortDirection(options.order);
|
||||
|
||||
return [
|
||||
{
|
||||
key: settingKeys.column,
|
||||
@@ -105,7 +125,7 @@ export function buildExtensionViewSortingUpdates(
|
||||
},
|
||||
{
|
||||
key: settingKeys.direction,
|
||||
value: options.order,
|
||||
value: order,
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user