fix: draft folder support (#840)

This commit is contained in:
Daniel Lautzenheiser 2023-06-13 15:45:26 -04:00 committed by GitHub
parent 0c592f6943
commit ad0545dd7a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 73 additions and 35 deletions

View File

@ -4,6 +4,7 @@ import flatten from 'lodash/flatten';
import get from 'lodash/get'; import get from 'lodash/get';
import isError from 'lodash/isError'; import isError from 'lodash/isError';
import uniq from 'lodash/uniq'; import uniq from 'lodash/uniq';
import { dirname } from 'path';
import { resolveFormat } from './formats/formats'; import { resolveFormat } from './formats/formats';
import { commitMessageFormatter, slugFormatter } from './lib/formatters'; import { commitMessageFormatter, slugFormatter } from './lib/formatters';
@ -40,12 +41,9 @@ import {
selectMediaFolders, selectMediaFolders,
} from './lib/util/collection.util'; } from './lib/util/collection.util';
import filterEntries from './lib/util/filter.util'; import filterEntries from './lib/util/filter.util';
import { import { DRAFT_MEDIA_FILES, selectMediaFilePublicPath } from './lib/util/media.util';
DRAFT_MEDIA_FILES,
selectMediaFilePath,
selectMediaFilePublicPath,
} from './lib/util/media.util';
import { selectCustomPath, slugFromCustomPath } from './lib/util/nested.util'; import { selectCustomPath, slugFromCustomPath } from './lib/util/nested.util';
import { isNullish } from './lib/util/null.util';
import { set } from './lib/util/object.util'; import { set } from './lib/util/object.util';
import { dateParsers, expandPath, extractTemplateVars } from './lib/widgets/stringTemplate'; import { dateParsers, expandPath, extractTemplateVars } from './lib/widgets/stringTemplate';
import createEntry from './valueObjects/createEntry'; import createEntry from './valueObjects/createEntry';
@ -63,46 +61,70 @@ import type {
DisplayURL, DisplayURL,
Entry, Entry,
EntryData, EntryData,
EntryDraft,
EventData, EventData,
FilterRule, FilterRule,
ImplementationEntry, ImplementationEntry,
MediaField, MediaField,
ObjectValue,
PersistArgs, PersistArgs,
SearchQueryResponse, SearchQueryResponse,
SearchResponse, SearchResponse,
UnknownField, UnknownField,
User, User,
ValueOrNestedValue,
} from './interface'; } from './interface';
import type { AsyncLock } from './lib/util'; import type { AsyncLock } from './lib/util';
import type { RootState } from './store'; import type { RootState } from './store';
import type AssetProxy from './valueObjects/AssetProxy'; import type AssetProxy from './valueObjects/AssetProxy';
function updateAssetProxies( function updatePath(entryPath: string, assetPath: string): string | null {
assetProxies: AssetProxy[], const pathDir = dirname(entryPath);
config: Config,
collection: Collection,
entryDraft: EntryDraft,
path: string,
) {
assetProxies.map(asset => {
// update media files path based on entry path
const oldPath = asset.path;
entryDraft.entry.path = path;
const folderPath = joinUrlPath( const pathParts = assetPath.split(DRAFT_MEDIA_FILES);
collection && 'folder' in collection ? collection.folder : '', const restOfPath = pathParts.length > 1 ? pathParts[1] : null;
DRAFT_MEDIA_FILES, if (restOfPath === null) {
); return null;
}
const newPath = selectMediaFilePath( return joinUrlPath(pathDir, restOfPath).replace(/\/\//g, '');
config, }
collection,
entryDraft.entry, function updateAssetFields(data: ValueOrNestedValue, path: string): ValueOrNestedValue {
oldPath.replace(folderPath, ''), if (
asset.field, isNullish(data) ||
); typeof data === 'number' ||
typeof data === 'boolean' ||
data instanceof Date
) {
return data;
}
if (Array.isArray(data)) {
return data.map(child => updateAssetFields(child, path));
}
if (typeof data === 'object') {
return Object.keys(data).reduce((acc, key) => {
acc[key] = updateAssetFields(data[key], path);
return acc;
}, {} as ObjectValue);
}
const newPath = updatePath(path, data);
if (!newPath) {
return data;
}
return newPath;
}
function updateAssetProxies(assetProxies: AssetProxy[], path: string) {
assetProxies.forEach(asset => {
const newPath = updatePath(path, asset.path);
if (newPath) {
asset.path = newPath; asset.path = newPath;
}
}); });
} }
@ -865,13 +887,16 @@ export class Backend<EF extends BaseField = UnknownField, BC extends BackendClas
customPath, customPath,
); );
const path = customPath || (selectEntryPath(collection, slug) ?? ''); const path = customPath || (selectEntryPath(collection, slug) ?? '');
entryDraft.entry.path = path;
entryDraft.entry.data = updateAssetFields(entryDraft.entry.data, path) as ObjectValue;
updateAssetProxies(assetProxies, path);
dataFile = { dataFile = {
path, path,
slug, slug,
raw: this.entryToRaw(collection, entryDraft.entry), raw: this.entryToRaw(collection, entryDraft.entry),
}; };
updateAssetProxies(assetProxies, config, collection, entryDraft, path);
} else { } else {
const slug = entryDraft.entry.slug; const slug = entryDraft.entry.slug;
dataFile = { dataFile = {

View File

@ -320,7 +320,6 @@ export function selectMediaFilePath(
} }
let mediaFolder = selectMediaFolder(config, collection, entryMap, field, currentFolder); let mediaFolder = selectMediaFolder(config, collection, entryMap, field, currentFolder);
if (!currentFolder) { if (!currentFolder) {
let publicFolder = trim(config['public_folder'] ?? mediaFolder, '/'); let publicFolder = trim(config['public_folder'] ?? mediaFolder, '/');
let mediaPathDir = trim(dirname(mediaPath), '/'); let mediaPathDir = trim(dirname(mediaPath), '/');
@ -341,10 +340,14 @@ export function selectMediaFilePath(
collection, collection,
entryMap, entryMap,
field, field,
mediaPathDir.replace(publicFolder, mediaFolder), publicFolder === '' && mediaPathDir.startsWith(mediaFolder)
? mediaPathDir
: mediaPathDir.replace(publicFolder, mediaFolder),
); );
} }
} }
return joinUrlPath(mediaFolder, basename(mediaPath)); return mediaPath.startsWith(mediaFolder)
? mediaPath
: joinUrlPath(mediaFolder, basename(mediaPath));
} }

View File

@ -109,8 +109,17 @@ function mediaLibrary(
return { return {
...state, ...state,
isVisible: false, isVisible: false,
insertOptions: undefined, forImage: false,
forFolder: false,
controlID: undefined,
config: undefined,
collection: undefined,
collectionFile: undefined,
field: undefined,
value: undefined,
alt: undefined, alt: undefined,
replaceIndex: undefined,
insertOptions: undefined,
}; };
case MEDIA_INSERT: { case MEDIA_INSERT: {

View File

@ -280,6 +280,7 @@ const PlateEditor: FC<PlateEditorProps> = ({
...editableProps, ...editableProps,
onFocus, onFocus,
onBlur, onBlur,
className: 'outline-none',
}} }}
> >
<div key="editor-inner-wrapper" ref={innerEditorContainerRef}> <div key="editor-inner-wrapper" ref={innerEditorContainerRef}>