Skip to content

Commit

Permalink
Support root asset folder
Browse files Browse the repository at this point in the history
Part of #230
  • Loading branch information
kyoshino committed Nov 22, 2024
1 parent 69604cc commit 03431d3
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/lib/components/assets/list/primary-sidebar.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<!-- Can’t upload assets if collection assets are saved at entry-relative paths -->
{@const uploadDisabled = entryRelative}
{@const selected =
(!internalPath && !$selectedAssetFolder) ||
(internalPath === undefined && !$selectedAssetFolder) ||
internalPath === $selectedAssetFolder?.internalPath}
<Option
{selected}
Expand All @@ -45,7 +45,7 @@
return;
}

if (!internalPath || selected) {
if (internalPath === undefined || selected) {
/** @type {DataTransfer} */ (event.dataTransfer).dropEffect = 'none';
} else {
/** @type {DataTransfer} */ (event.dataTransfer).dropEffect = 'move';
Expand Down
14 changes: 10 additions & 4 deletions src/lib/services/assets/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,11 @@ export const getAssetFoldersByPath = (path, { matchSubFolders = false } = {}) =>
// Compare that the enclosing directory is exactly the same as the internal path, and ignore
// any subdirectories, unless the `matchSubFolders` option is specified. The internal path can
// contain template tags like `{{slug}}` so that we have to take it into account.
return !!getPathInfo(path).dirname?.match(
`^${internalPath.replace(/{{.+?}}/g, '.+?')}${matchSubFolders ? '\\b' : '$'}`,
const regex = new RegExp(
`^${internalPath.replace(/{{.+?}}/g, '.+?')}${internalPath && matchSubFolders ? '\\b' : '$'}`,
);

return !!(getPathInfo(path).dirname ?? '').match(regex);
})
.sort((a, b) => b.internalPath?.localeCompare(a.internalPath ?? '') ?? 0);
};
Expand Down Expand Up @@ -329,7 +331,11 @@ export const getAssetByPath = (savedPath, { entry, collection } = {}) => {
return getAsset(_collection);
});

return assets.flat(1).filter(Boolean)[0] ?? undefined;
return (
assets.flat(1).filter(Boolean)[0] ??
// Fall back to exact match at the root folder
get(allAssets).find((asset) => asset.path === savedPath)
);
}

const exactMatch = get(allAssets).find((asset) => asset.path === stripSlashes(savedPath));
Expand All @@ -350,7 +356,7 @@ export const getAssetByPath = (savedPath, { entry, collection } = {}) => {
publicPath.match(`^${folder.publicPath.replace(/{{.+?}}/g, '.+?')}\\b`),
) ?? {};

if (!internalPath) {
if (internalPath === undefined) {
return undefined;
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib/services/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ const validate = (config) => {
throw new Error(get(_)('config.error.oauth_no_app_id'));
}

if (!config.media_folder) {
if (typeof config.media_folder !== 'string') {
throw new Error(get(_)('config.error.no_media_folder'));
}
};
Expand Down
10 changes: 7 additions & 3 deletions src/lib/services/contents/draft/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -603,18 +603,22 @@ export const saveEntry = async ({ skipCI = undefined } = {}) => {
const file = files[locale][keyPath];
const sha = await getHash(file);
const dupFile = savingAssets.find((f) => f.sha === sha);
const useSubFolder = !!publicAssetFolder && publicAssetFolder !== '/';

// Check if the file has already been added for other field or locale
if (dupFile) {
content[keyPath] = publicAssetFolder
content[keyPath] = useSubFolder
? `${publicAssetFolder}/${dupFile.name}`
: dupFile.name;

continue;
}

const assetName = renameIfNeeded(file.name.normalize(), assetNamesInSameFolder);
const assetPath = `${internalAssetFolder}/${assetName}`;

const assetPath = internalAssetFolder
? `${internalAssetFolder}/${assetName}`
: assetName;

assetNamesInSameFolder.push(assetName);
changes.push({ action: 'create', path: assetPath, data: file });
Expand All @@ -629,7 +633,7 @@ export const saveEntry = async ({ skipCI = undefined } = {}) => {
kind: getAssetKind(assetName),
});

content[keyPath] = publicAssetFolder ? `${publicAssetFolder}/${assetName}` : assetName;
content[keyPath] = useSubFolder ? `${publicAssetFolder}/${assetName}` : assetName;
}
}

Expand Down

0 comments on commit 03431d3

Please sign in to comment.