fix: folder collection path (#549)
This commit is contained in:
committed by
GitHub
parent
93915dac35
commit
8f7237ab7c
@ -21,7 +21,7 @@ import {
|
||||
} from '../constants';
|
||||
|
||||
import type { MediaLibraryAction } from '../actions/mediaLibrary';
|
||||
import type { Field, MediaFile, MediaLibraryInstance } from '../interface';
|
||||
import type { Collection, Field, MediaFile, MediaLibraryInstance } from '../interface';
|
||||
|
||||
export interface MediaLibraryDisplayURL {
|
||||
url?: string;
|
||||
@ -39,6 +39,7 @@ export type MediaLibraryState = {
|
||||
page?: number;
|
||||
files?: MediaFile[];
|
||||
config: Record<string, unknown>;
|
||||
collection?: Collection;
|
||||
field?: Field;
|
||||
value?: string | string[];
|
||||
replaceIndex?: number;
|
||||
@ -75,7 +76,8 @@ function mediaLibrary(
|
||||
};
|
||||
|
||||
case MEDIA_LIBRARY_OPEN: {
|
||||
const { controlID, forImage, config, field, value, replaceIndex } = action.payload;
|
||||
const { controlID, forImage, config, collection, field, value, replaceIndex } =
|
||||
action.payload;
|
||||
const libConfig = config || {};
|
||||
|
||||
return {
|
||||
@ -85,6 +87,7 @@ function mediaLibrary(
|
||||
controlID,
|
||||
canInsert: !!controlID,
|
||||
config: libConfig,
|
||||
collection,
|
||||
field,
|
||||
value,
|
||||
replaceIndex,
|
||||
|
@ -1,20 +1,9 @@
|
||||
import get from 'lodash/get';
|
||||
import groupBy from 'lodash/groupBy';
|
||||
import orderBy from 'lodash/orderBy';
|
||||
|
||||
import { SORT_DIRECTION_ASCENDING, SORT_DIRECTION_NONE } from '@staticcms/core/constants';
|
||||
import { selectSortDataPath } from '@staticcms/core/lib/util/sort.util';
|
||||
import { SORT_DIRECTION_NONE } from '@staticcms/core/constants';
|
||||
|
||||
import type { CollectionViewStyle } from '@staticcms/core/constants/collectionViews';
|
||||
import type {
|
||||
Collection,
|
||||
Entry,
|
||||
Filter,
|
||||
Group,
|
||||
GroupMap,
|
||||
GroupOfEntries,
|
||||
Sort,
|
||||
} from '@staticcms/core/interface';
|
||||
import type { Entry, Group, GroupMap, Sort } from '@staticcms/core/interface';
|
||||
import type { RootState } from '@staticcms/core/store';
|
||||
|
||||
export function selectEntriesSort(entries: RootState, collection: string) {
|
||||
@ -22,84 +11,49 @@ export function selectEntriesSort(entries: RootState, collection: string) {
|
||||
return sort?.[collection];
|
||||
}
|
||||
|
||||
export function selectEntriesFilter(entries: RootState, collection: string) {
|
||||
const filter = entries.entries.filter as Filter | undefined;
|
||||
return filter?.[collection] || {};
|
||||
}
|
||||
export const selectEntriesFilter = (collectionName: string) => (entries: RootState) => {
|
||||
return entries.entries.filter?.[collectionName];
|
||||
};
|
||||
|
||||
export function selectEntriesGroup(entries: RootState, collection: string) {
|
||||
const group = entries.entries.group as Group | undefined;
|
||||
return group?.[collection] || {};
|
||||
}
|
||||
|
||||
export function selectEntriesGroupField(entries: RootState, collection: string) {
|
||||
export const selectEntriesGroupField = (collection: string) => (entries: RootState) => {
|
||||
const groups = selectEntriesGroup(entries, collection);
|
||||
const value = Object.values(groups ?? {}).find(v => v?.active === true);
|
||||
return value;
|
||||
}
|
||||
return Object.values(groups ?? {}).find(v => v?.active === true);
|
||||
};
|
||||
|
||||
export function selectEntriesSortFields(entries: RootState, collection: string) {
|
||||
const sort = selectEntriesSort(entries, collection);
|
||||
const values = Object.values(sort ?? {}).filter(v => v?.direction !== SORT_DIRECTION_NONE) || [];
|
||||
|
||||
return values;
|
||||
}
|
||||
|
||||
export function selectEntriesFilterFields(entries: RootState, collection: string) {
|
||||
const filter = selectEntriesFilter(entries, collection);
|
||||
const values = Object.values(filter ?? {}).filter(v => v?.active === true) || [];
|
||||
return values;
|
||||
}
|
||||
export const selectEntriesSortField = (collectionName: string) => (entries: RootState) => {
|
||||
const sort = selectEntriesSort(entries, collectionName);
|
||||
return Object.values(sort ?? {}).find(v => v?.direction !== SORT_DIRECTION_NONE);
|
||||
};
|
||||
|
||||
export function selectViewStyle(entries: RootState): CollectionViewStyle {
|
||||
return entries.entries.viewStyle;
|
||||
}
|
||||
|
||||
export function selectEntriesBySlugs(state: RootState) {
|
||||
return state.entries.entities;
|
||||
}
|
||||
|
||||
export function selectEntry(state: RootState, collection: string, slug: string) {
|
||||
return state.entries.entities[`${collection}.${slug}`];
|
||||
}
|
||||
|
||||
export function selectPublishedSlugs(state: RootState, collection: string) {
|
||||
export const selectPublishedSlugs = (collection: string) => (state: RootState) => {
|
||||
return state.entries.pages[collection]?.ids ?? [];
|
||||
}
|
||||
};
|
||||
|
||||
function getPublishedEntries(state: RootState, collectionName: string) {
|
||||
const slugs = selectPublishedSlugs(state, collectionName);
|
||||
const entries =
|
||||
slugs && (slugs.map(slug => selectEntry(state, collectionName, slug as string)) as Entry[]);
|
||||
return entries;
|
||||
}
|
||||
export const selectPublishedEntries = (collectionName: string) => (state: RootState) => {
|
||||
const slugs = selectPublishedSlugs(collectionName)(state);
|
||||
return (
|
||||
slugs && (slugs.map(slug => selectEntry(state, collectionName, slug as string)) as Entry[])
|
||||
);
|
||||
};
|
||||
|
||||
export function selectEntries(state: RootState, collection: Collection) {
|
||||
const collectionName = collection.name;
|
||||
let entries = getPublishedEntries(state, collectionName);
|
||||
|
||||
const sortFields = selectEntriesSortFields(state, collectionName);
|
||||
if (sortFields && sortFields.length > 0) {
|
||||
const keys = sortFields.map(v => selectSortDataPath(collection, v.key));
|
||||
const orders = sortFields.map(v => (v.direction === SORT_DIRECTION_ASCENDING ? 'asc' : 'desc'));
|
||||
entries = orderBy(entries, keys, orders);
|
||||
}
|
||||
|
||||
const filters = selectEntriesFilterFields(state, collectionName);
|
||||
if (filters && filters.length > 0) {
|
||||
entries = entries.filter(e => {
|
||||
const allMatched = filters.every(f => {
|
||||
const pattern = f.pattern;
|
||||
const field = f.field;
|
||||
const data = e!.data || {};
|
||||
const toMatch = get(data, field);
|
||||
const matched = toMatch !== undefined && new RegExp(String(pattern)).test(String(toMatch));
|
||||
return matched;
|
||||
});
|
||||
return allMatched;
|
||||
});
|
||||
}
|
||||
|
||||
return entries;
|
||||
}
|
||||
|
||||
function getGroup(entry: Entry, selectedGroup: GroupMap) {
|
||||
export function getGroup(entry: Entry, selectedGroup: GroupMap) {
|
||||
const label = selectedGroup.label;
|
||||
const field = selectedGroup.field;
|
||||
|
||||
@ -139,35 +93,8 @@ function getGroup(entry: Entry, selectedGroup: GroupMap) {
|
||||
};
|
||||
}
|
||||
|
||||
export function selectGroups(state: RootState, collection: Collection) {
|
||||
const collectionName = collection.name;
|
||||
const entries = getPublishedEntries(state, collectionName);
|
||||
|
||||
const selectedGroup = selectEntriesGroupField(state, collectionName);
|
||||
if (selectedGroup === undefined) {
|
||||
return [];
|
||||
}
|
||||
|
||||
let groups: Record<string, { id: string; label: string; value: string | boolean | undefined }> =
|
||||
{};
|
||||
const groupedEntries = groupBy(entries, entry => {
|
||||
const group = getGroup(entry, selectedGroup);
|
||||
groups = { ...groups, [group.id]: group };
|
||||
return group.id;
|
||||
});
|
||||
|
||||
const groupsArray: GroupOfEntries[] = Object.entries(groupedEntries).map(([id, entries]) => {
|
||||
return {
|
||||
...groups[id],
|
||||
paths: new Set(entries.map(entry => entry.path)),
|
||||
};
|
||||
});
|
||||
|
||||
return groupsArray;
|
||||
}
|
||||
|
||||
export function selectEntryByPath(state: RootState, collection: string, path: string) {
|
||||
const slugs = selectPublishedSlugs(state, collection);
|
||||
const slugs = selectPublishedSlugs(collection)(state);
|
||||
const entries =
|
||||
slugs && (slugs.map(slug => selectEntry(state, collection, slug as string)) as Entry[]);
|
||||
|
||||
|
Reference in New Issue
Block a user