Skip to content

Commit 573204a

Browse files
committed
feat(index-files): fix editorial workflow with the new nested collection options
1 parent 3b497a5 commit 573204a

File tree

5 files changed

+28
-7
lines changed

5 files changed

+28
-7
lines changed

packages/decap-cms-core/src/actions/config.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,15 +208,18 @@ export function normalizeConfig(config: CmsConfig) {
208208
}
209209

210210
function applyMetaFieldsToCollection(collection: CmsCollection, meta: CmsCollectionMeta) {
211-
const metaFields = [
211+
const metaFields: CmsFieldBase[] = [
212212
{
213213
name: 'path',
214214
meta: true,
215215
required: true,
216216
i18n: 'duplicate',
217217
...meta!.path,
218218
} as CmsFieldMeta,
219-
{
219+
];
220+
221+
if (collection.index_file) {
222+
metaFields.push({
220223
name: 'path_type',
221224
meta: true,
222225
required: true,
@@ -226,8 +229,8 @@ function applyMetaFieldsToCollection(collection: CmsCollection, meta: CmsCollect
226229
label: 'Path type',
227230
options: ['index', 'slug'],
228231
default: 'slug',
229-
} as CmsFieldBase & CmsFieldSelect,
230-
];
232+
} as CmsFieldBase & CmsFieldSelect)
233+
}
231234

232235
collection.fields = [...metaFields, ...(collection.fields || [])];
233236

packages/decap-cms-core/src/backend.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -942,6 +942,7 @@ export class Backend {
942942
);
943943

944944
const formatData = (data: string, path: string, newFile: boolean) => {
945+
const path_type = prepareMetaPathType(slug, collection);
945946
const entry = createEntry(collection.get('name'), slug, path, {
946947
raw: data,
947948
isModification: !newFile,
@@ -950,7 +951,7 @@ export class Backend {
950951
updatedOn: entryData.updatedAt,
951952
author: entryData.pullRequestAuthor,
952953
status: entryData.status,
953-
meta: { path: prepareMetaPath(path, collection) },
954+
meta: { path: prepareMetaPath(path, collection), path_type },
954955
});
955956

956957
const entryWithFormat = this.entryWithFormat(collection)(entry);
@@ -983,6 +984,9 @@ export class Backend {
983984
);
984985
entries = entries.filter(Boolean);
985986
const grouped = await groupEntries(collection, extension, entries as EntryValue[]);
987+
if (grouped[0]?.srcSlug) {
988+
grouped[0].slug = grouped[0].srcSlug;
989+
}
986990
return grouped[0];
987991
} else {
988992
const entryWithFormat = await readAndFormatDataFile(dataFiles[0]);

packages/decap-cms-core/src/lib/i18n.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,26 +324,35 @@ export function groupEntries(collection: Collection, extension: string, entries:
324324

325325
const groupedEntries = Object.values(grouped).reduce((acc, values) => {
326326
const entryValue = mergeValues(collection, structure, defaultLocale, values);
327+
if (values[0]?.value?.slug !== entryValue.slug) {
328+
entryValue.srcSlug = values[0]?.value?.slug;
329+
}
327330
return [...acc, entryValue];
328331
}, [] as EntryValue[]);
329332

330333
return groupedEntries;
331334
}
332335

336+
function compareFilePathEndings(path1: string, path2: string, subfolders = false) {
337+
const [p1, p2] = [path1, path2].map(p => p.split('/'));
338+
return subfolders ? p1.slice(-2).join('/') === p2.slice(-2).join('/') : p1.at(-1) === p2.at(-1);
339+
}
340+
333341
export function getI18nDataFiles(
334342
collection: Collection,
335343
extension: string,
336344
path: string,
337345
slug: string,
338-
diffFiles: { path: string; id: string; newFile: boolean }[],
346+
diffFiles: { path: string; id: string; newFile: boolean; prevPath?: string }[],
339347
) {
340348
const { structure } = getI18nInfo(collection) as I18nInfo;
341349
if (structure === I18N_STRUCTURE.SINGLE_FILE) {
342350
return diffFiles;
343351
}
344352
const paths = getFilePaths(collection, extension, path, slug);
353+
const subfolders = collection.get('nested')?.get('subfolders') !== false;
345354
const dataFiles = paths.reduce((acc, path) => {
346-
const dataFile = diffFiles.find(file => file.path === path);
355+
const dataFile = diffFiles.find(file => compareFilePathEndings(file.path, path, subfolders));
347356
if (dataFile) {
348357
return [...acc, dataFile];
349358
} else {

packages/decap-cms-core/src/reducers/entryDraft.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,10 @@ export function selectCustomPath(collection, entryDraft, config) {
218218
const fileSlug = entryDraft.getIn(['entry', 'slug'])
219219
? entryDraft.getIn(['entry', 'slug']).split('/').pop()
220220
: fileBaseName;
221+
const subfolders = collection.get('nested')?.get('subfolders') !== false;
222+
if (subfolders) {
223+
return path && join(collection.get('folder'), path, `${indexFile}.${extension}`);
224+
}
221225
const customPath =
222226
path &&
223227
join(

packages/decap-cms-core/src/valueObjects/Entry.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ interface Options {
2323
export interface EntryValue {
2424
collection: string;
2525
slug: string;
26+
srcSlug?: string;
2627
path: string;
2728
partial: boolean;
2829
raw: string;

0 commit comments

Comments
 (0)