feat: folder selection support in useMediaAsset and file widget (#706)

This commit is contained in:
2023-04-19 06:42:18 +02:00
committed by GitHub
parent 0fae2ce73d
commit 23df691a0a
12 changed files with 98 additions and 25 deletions

View File

@ -14,6 +14,7 @@ export default {
},
media_folder: { type: 'string' },
public_folder: { type: 'string' },
select_folder: { type: 'boolean' },
choose_url: { type: 'boolean' },
multiple: { type: 'boolean' },
media_library: {

View File

@ -62,6 +62,8 @@ const withFileControl = ({ forImage = false }: WithFileControlProps = {}) => {
const uploadButtonRef = useRef<HTMLButtonElement | null>(null);
const forFolder = useMemo(() => field.select_folder ?? false, [field.select_folder]);
const handleOnChange = useCallback(
({ path: newValue }: MediaPath) => {
if (newValue !== internalValue) {
@ -76,7 +78,7 @@ const withFileControl = ({ forImage = false }: WithFileControlProps = {}) => {
const handleOpenMediaLibrary = useMediaInsert(
{ path: internalValue },
{ collection, field, controlID, forImage },
{ collection, field, controlID, forImage, forFolder },
handleOnChange,
);
@ -87,7 +89,7 @@ const withFileControl = ({ forImage = false }: WithFileControlProps = {}) => {
const chooseUrl = useMemo(() => field.choose_url ?? false, [field.choose_url]);
const handleUrl = useCallback(
(subject: 'image' | 'file') => (e: MouseEvent) => {
(subject: 'image' | 'folder' | 'file') => (e: MouseEvent) => {
e.preventDefault();
const url = window.prompt(t(`editor.editorWidgets.${subject}.promptUrl`));
@ -122,6 +124,7 @@ const withFileControl = ({ forImage = false }: WithFileControlProps = {}) => {
return openMediaLibrary({
controlID,
forImage,
forFolder,
value: internalValue,
replaceIndex: index,
allowMultiple: false,
@ -130,7 +133,7 @@ const withFileControl = ({ forImage = false }: WithFileControlProps = {}) => {
field,
});
},
[openMediaLibrary, controlID, internalValue, collection, field],
[openMediaLibrary, controlID, internalValue, collection, field, forFolder],
);
// TODO Readd when multiple uploads is supported
@ -212,7 +215,7 @@ const withFileControl = ({ forImage = false }: WithFileControlProps = {}) => {
}, [collection, field, internalValue, onRemoveOne, onReplaceOne, renderFileLink]);
const content: JSX.Element = useMemo(() => {
const subject = forImage ? 'image' : 'file';
const subject = forImage ? 'image' : forFolder ? 'folder' : 'file';
if (Array.isArray(internalValue) ? internalValue.length === 0 : isEmpty(internalValue)) {
return (