Skip to content

Commit 12ed129

Browse files
committed
feat(index-file): add index file config options
1 parent 35a3be2 commit 12ed129

File tree

5 files changed

+52
-11
lines changed

5 files changed

+52
-11
lines changed

packages/decap-cms-core/index.d.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,6 @@ declare module 'decap-cms-core' {
323323

324324
frontmatter_delimiter?: string[] | string;
325325
fields?: CmsField[];
326-
index_fields?: CmsField[];
327326
filter?: { field: string; value: any };
328327
path?: string;
329328
media_folder?: string;
@@ -332,6 +331,11 @@ declare module 'decap-cms-core' {
332331
view_filters?: ViewFilter[];
333332
view_groups?: ViewGroup[];
334333
i18n?: boolean | CmsI18nConfig;
334+
index_file?: {
335+
pattern: string;
336+
fields?: CmsField[];
337+
label?: string;
338+
};
335339

336340
/**
337341
* @deprecated Use sortable_fields instead

packages/decap-cms-core/src/components/Collection/Entries/EntryCard.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ const CollectionLabel = styled.h2`
5454
text-transform: uppercase;
5555
`;
5656

57+
const IndexFileLabel = styled.span`
58+
font-size: 12px;
59+
font-weight: 600;
60+
color: ${colors.textLead};
61+
text-transform: uppercase;
62+
margin-right: 6px;
63+
`;
64+
5765
const ListCardTitle = styled.h2`
5866
margin-bottom: 0;
5967
`;
@@ -95,6 +103,7 @@ function EntryCard({
95103
image,
96104
imageField,
97105
collectionLabel,
106+
indexFileLabel,
98107
viewStyle = VIEW_STYLE_LIST,
99108
getAsset,
100109
}) {
@@ -103,7 +112,10 @@ function EntryCard({
103112
<ListCard>
104113
<ListCardLink to={path}>
105114
{collectionLabel ? <CollectionLabel>{collectionLabel}</CollectionLabel> : null}
106-
<ListCardTitle>{summary}</ListCardTitle>
115+
<ListCardTitle>
116+
{indexFileLabel && <IndexFileLabel>{indexFileLabel}</IndexFileLabel>}
117+
{summary}
118+
</ListCardTitle>
107119
</ListCardLink>
108120
</ListCard>
109121
);
@@ -115,7 +127,10 @@ function EntryCard({
115127
<GridCardLink to={path}>
116128
<CardBody hasImage={image}>
117129
{collectionLabel ? <CollectionLabel>{collectionLabel}</CollectionLabel> : null}
118-
<CardHeading>{summary}</CardHeading>
130+
<CardHeading>
131+
{indexFileLabel && <IndexFileLabel>{indexFileLabel}</IndexFileLabel>}
132+
{summary}
133+
</CardHeading>
119134
</CardBody>
120135
{image ? <CardImage src={getAsset(image, imageField).toString()} /> : null}
121136
</GridCardLink>
@@ -136,6 +151,9 @@ function mapStateToProps(state, ownProps) {
136151

137152
const isLoadingAsset = selectIsLoadingAsset(state.medias);
138153

154+
const indexFileConfig = collection.get('index_file');
155+
const fileSlug = entry.get('slug')?.split('/').pop();
156+
139157
return {
140158
summary,
141159
path: `/collections/${collection.get('name')}/entries/${entry.get('slug')}`,
@@ -144,6 +162,8 @@ function mapStateToProps(state, ownProps) {
144162
.get('fields')
145163
?.find(f => f.get('name') === inferredFields.imageField && f.get('widget') === 'image'),
146164
isLoadingAsset,
165+
indexFileLabel:
166+
new RegExp(indexFileConfig.get('pattern')).test(fileSlug) && indexFileConfig.get('label'),
147167
};
148168
}
149169

packages/decap-cms-core/src/constants/configSchema.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,6 @@ function getConfigSchema() {
242242
},
243243
},
244244
fields: fieldsConfig(),
245-
index_fields: fieldsConfig(),
246245
sortable_fields: {
247246
type: 'array',
248247
items: {
@@ -282,6 +281,14 @@ function getConfigSchema() {
282281
minProperties: 1,
283282
},
284283
i18n: i18nCollection,
284+
index_file: {
285+
type: 'object',
286+
properties: {
287+
pattern: { type: 'string' },
288+
fields: fieldsConfig(),
289+
label: { type: 'string' },
290+
},
291+
},
285292
},
286293
required: ['name', 'label'],
287294
oneOf: [{ required: ['files'] }, { required: ['folder', 'fields'] }],

packages/decap-cms-core/src/reducers/collections.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,9 @@ function collections(state = defaultState, action: ConfigAction) {
4343
}
4444
}
4545

46-
function isIndexFile(filePath: string) {
46+
function isIndexFile(filePath: string, pattern: string) {
4747
const fileSlug = filePath?.split('/').pop();
48-
const indexFiles = Set(['_index', 'index']);
49-
50-
return fileSlug && indexFiles.has(fileSlug);
48+
return fileSlug && new RegExp(pattern).test(fileSlug);
5149
}
5250

5351
const selectors = {
@@ -63,9 +61,15 @@ const selectors = {
6361
return ext.replace(/^\./, '');
6462
},
6563
fields(collection: Collection, slug: string) {
66-
if (collection.get('index_fields') && isIndexFile(slug)) {
67-
return collection.get('index_fields');
64+
const indexFileConfig = collection.get('index_file');
65+
if (
66+
indexFileConfig &&
67+
isIndexFile(slug, indexFileConfig.get('pattern')) &&
68+
indexFileConfig.has('fields')
69+
) {
70+
return indexFileConfig.get('fields');
6871
}
72+
6973
return collection.get('fields');
7074
},
7175
entryPath(collection: Collection, slug: string) {

packages/decap-cms-core/src/types/redux.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,14 +600,19 @@ type i18n = StaticallyTypedRecord<{
600600
default_locale: string;
601601
}>;
602602

603+
type IndexFile = StaticallyTypedRecord<{
604+
pattern: string;
605+
fields?: EntryFields;
606+
label?: string;
607+
}>;
608+
603609
export type Format = keyof typeof formatExtensions | string;
604610

605611
type CollectionObject = {
606612
name: string;
607613
folder?: string;
608614
files?: CollectionFiles;
609615
fields: EntryFields;
610-
index_fields?: EntryFields;
611616
isFetching: boolean;
612617
media_folder?: string;
613618
public_folder?: string;
@@ -632,6 +637,7 @@ type CollectionObject = {
632637
nested?: Nested;
633638
meta?: Meta;
634639
i18n: i18n;
640+
index_file?: IndexFile;
635641
};
636642

637643
export type Collection = StaticallyTypedRecord<CollectionObject>;

0 commit comments

Comments
 (0)