2b41d8a838
* fix(media_folder_relative): use collection name in unpublished entry * refactor: pass arguments as object to AssetProxy ctor * feat: support media folders per collection * feat: resolve media files path based on entry path * fix: asset public path resolving * refactor: introduce typescript for AssetProxy * refactor: code cleanup * refactor(asset-proxy): add tests,switch to typescript,extract arguments * refactor: typescript for editorialWorkflow * refactor: add typescript for media library actions * refactor: fix type error on map set * refactor: move locale selector into reducer * refactor: add typescript for entries actions * refactor: remove duplication between asset store and media lib * feat: load assets from backend using API * refactor(github): add typescript, cache media files * fix: don't load media URL if already loaded * feat: add media folder config to collection * fix: load assets from API when not in UI state * feat: load entry media files when opening media library * fix: editorial workflow draft media files bug fixes * test(unit): fix unit tests * fix: editor control losing focus * style: add eslint object-shorthand rule * test(cypress): re-record mock data * fix: fix non github backends, large media * test: uncomment only in tests * fix(backend-test): add missing displayURL property * test(e2e): add media library tests * test(e2e): enable visual testing * test(e2e): add github backend media library tests * test(e2e): add git-gateway large media tests * chore: post rebase fixes * test: fix tests * test: fix tests * test(cypress): fix tests * docs: add media_folder docs * test(e2e): add media library delete test * test(e2e): try and fix image comparison on CI * ci: reduce test machines from 9 to 8 * test: add reducers and selectors unit tests * test(e2e): disable visual regression testing for now * test: add getAsset unit tests * refactor: use Asset class component instead of hooks * build: don't inline source maps * test: add more media path tests
196 lines
7.3 KiB
TypeScript
196 lines
7.3 KiB
TypeScript
import { List } from 'immutable';
|
|
import { get, escapeRegExp } from 'lodash';
|
|
import consoleError from '../lib/consoleError';
|
|
import { CONFIG_SUCCESS } from '../actions/config';
|
|
import { FILES, FOLDER } from '../constants/collectionTypes';
|
|
import { INFERABLE_FIELDS, IDENTIFIER_FIELDS } from '../constants/fieldInference';
|
|
import { formatExtensions } from '../formats/formats';
|
|
import { CollectionsAction, Collection, CollectionFiles, EntryField } from '../types/redux';
|
|
|
|
const collections = (state = null, action: CollectionsAction) => {
|
|
switch (action.type) {
|
|
case CONFIG_SUCCESS: {
|
|
const configCollections = action.payload
|
|
? action.payload.get('collections')
|
|
: List<Collection>();
|
|
|
|
return (
|
|
configCollections
|
|
.toOrderedMap()
|
|
.map(item => {
|
|
const collection = item as Collection;
|
|
if (collection.has('folder')) {
|
|
return collection.set('type', FOLDER);
|
|
}
|
|
if (collection.has('files')) {
|
|
return collection.set('type', FILES);
|
|
}
|
|
})
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
|
|
// @ts-ignore
|
|
.mapKeys((key: string, collection: Collection) => collection.get('name'))
|
|
);
|
|
}
|
|
default:
|
|
return state;
|
|
}
|
|
};
|
|
|
|
enum ListMethod {
|
|
ENTRIES_BY_FOLDER = 'entriesByFolder',
|
|
ENTRIES_BY_FILES = 'entriesByFiles',
|
|
}
|
|
|
|
const selectors = {
|
|
[FOLDER]: {
|
|
entryExtension(collection: Collection) {
|
|
return (
|
|
collection.get('extension') ||
|
|
get(formatExtensions, collection.get('format') || 'frontmatter')
|
|
).replace(/^\./, '');
|
|
},
|
|
fields(collection: Collection) {
|
|
return collection.get('fields');
|
|
},
|
|
entryPath(collection: Collection, slug: string) {
|
|
const folder = (collection.get('folder') as string).replace(/\/$/, '');
|
|
return `${folder}/${slug}.${this.entryExtension(collection)}`;
|
|
},
|
|
entrySlug(collection: Collection, path: string) {
|
|
const folder = (collection.get('folder') as string).replace(/\/$/, '');
|
|
const slug = path
|
|
.split(folder + '/')
|
|
.pop()
|
|
?.replace(new RegExp(`\\.${escapeRegExp(this.entryExtension(collection))}$`), '');
|
|
|
|
return slug;
|
|
},
|
|
listMethod() {
|
|
return ListMethod.ENTRIES_BY_FOLDER;
|
|
},
|
|
allowNewEntries(collection: Collection) {
|
|
return collection.get('create');
|
|
},
|
|
allowDeletion(collection: Collection) {
|
|
return collection.get('delete', true);
|
|
},
|
|
templateName(collection: Collection) {
|
|
return collection.get('name');
|
|
},
|
|
},
|
|
[FILES]: {
|
|
fileForEntry(collection: Collection, slug: string) {
|
|
const files = collection.get('files');
|
|
return files && files.filter(f => f?.get('name') === slug).get(0);
|
|
},
|
|
fields(collection: Collection, slug: string) {
|
|
const file = this.fileForEntry(collection, slug);
|
|
return file && file.get('fields');
|
|
},
|
|
entryPath(collection: Collection, slug: string) {
|
|
const file = this.fileForEntry(collection, slug);
|
|
return file && file.get('file');
|
|
},
|
|
entrySlug(collection: Collection, path: string) {
|
|
const file = (collection.get('files') as CollectionFiles)
|
|
.filter(f => f?.get('file') === path)
|
|
.get(0);
|
|
return file && file.get('name');
|
|
},
|
|
entryLabel(collection: Collection, slug: string) {
|
|
const path = this.entryPath(collection, slug);
|
|
const files = collection.get('files');
|
|
return files && files.find(f => f?.get('file') === path).get('label');
|
|
},
|
|
listMethod() {
|
|
return ListMethod.ENTRIES_BY_FILES;
|
|
},
|
|
allowNewEntries() {
|
|
return false;
|
|
},
|
|
allowDeletion(collection: Collection) {
|
|
return collection.get('delete', false);
|
|
},
|
|
templateName(collection: Collection, slug: string) {
|
|
return slug;
|
|
},
|
|
},
|
|
};
|
|
|
|
export const selectFields = (collection: Collection, slug: string) =>
|
|
selectors[collection.get('type')].fields(collection, slug);
|
|
export const selectFolderEntryExtension = (collection: Collection) =>
|
|
selectors[FOLDER].entryExtension(collection);
|
|
export const selectFileEntryLabel = (collection: Collection, slug: string) =>
|
|
selectors[FILES].entryLabel(collection, slug);
|
|
export const selectEntryPath = (collection: Collection, slug: string) =>
|
|
selectors[collection.get('type')].entryPath(collection, slug);
|
|
export const selectEntrySlug = (collection: Collection, path: string) =>
|
|
selectors[collection.get('type')].entrySlug(collection, path);
|
|
export const selectListMethod = (collection: Collection) =>
|
|
selectors[collection.get('type')].listMethod();
|
|
export const selectAllowNewEntries = (collection: Collection) =>
|
|
selectors[collection.get('type')].allowNewEntries(collection);
|
|
export const selectAllowDeletion = (collection: Collection) =>
|
|
selectors[collection.get('type')].allowDeletion(collection);
|
|
export const selectTemplateName = (collection: Collection, slug: string) =>
|
|
selectors[collection.get('type')].templateName(collection, slug);
|
|
export const selectIdentifier = (collection: Collection) => {
|
|
const identifier = collection.get('identifier_field');
|
|
const identifierFields = identifier ? [identifier, ...IDENTIFIER_FIELDS] : IDENTIFIER_FIELDS;
|
|
const fieldNames = collection.get('fields', List<EntryField>()).map(field => field?.get('name'));
|
|
return identifierFields.find(id =>
|
|
fieldNames.find(name => name?.toLowerCase().trim() === id.toLowerCase().trim()),
|
|
);
|
|
};
|
|
export const selectInferedField = (collection: Collection, fieldName: string) => {
|
|
if (fieldName === 'title' && collection.get('identifier_field')) {
|
|
return selectIdentifier(collection);
|
|
}
|
|
const inferableField = (INFERABLE_FIELDS as Record<
|
|
string,
|
|
{
|
|
type: string;
|
|
synonyms: string[];
|
|
secondaryTypes: string[];
|
|
fallbackToFirstField: boolean;
|
|
showError: boolean;
|
|
}
|
|
>)[fieldName];
|
|
const fields = collection.get('fields');
|
|
let field;
|
|
|
|
// If collection has no fields or fieldName is not defined within inferables list, return null
|
|
if (!fields || !inferableField) return null;
|
|
// Try to return a field of the specified type with one of the synonyms
|
|
const mainTypeFields = fields
|
|
.filter(f => f?.get('widget', 'string') === inferableField.type)
|
|
.map(f => f?.get('name'));
|
|
field = mainTypeFields.filter(f => inferableField.synonyms.indexOf(f as string) !== -1);
|
|
if (field && field.size > 0) return field.first();
|
|
|
|
// Try to return a field for each of the specified secondary types
|
|
const secondaryTypeFields = fields
|
|
.filter(f => inferableField.secondaryTypes.indexOf(f?.get('widget', 'string') as string) !== -1)
|
|
.map(f => f?.get('name'));
|
|
field = secondaryTypeFields.filter(f => inferableField.synonyms.indexOf(f as string) !== -1);
|
|
if (field && field.size > 0) return field.first();
|
|
|
|
// Try to return the first field of the specified type
|
|
if (inferableField.fallbackToFirstField && mainTypeFields.size > 0) return mainTypeFields.first();
|
|
|
|
// Coundn't infer the field. Show error and return null.
|
|
if (inferableField.showError) {
|
|
consoleError(
|
|
`The Field ${fieldName} is missing for the collection “${collection.get('name')}”`,
|
|
`Netlify CMS tries to infer the entry ${fieldName} automatically, but one couldn't be found for entries of the collection “${collection.get(
|
|
'name',
|
|
)}”. Please check your site configuration.`,
|
|
);
|
|
}
|
|
|
|
return null;
|
|
};
|
|
|
|
export default collections;
|