-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0a5478e
commit 287b869
Showing
2 changed files
with
93 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,62 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { useStrapiApp } from '@strapi/strapi/admin'; | ||
import prefixFileUrlWithBackendUrl from '../utils/prefixFileUrlWithBackendUrl'; | ||
import { useStrapiApp } from '@strapi/strapi/admin'; | ||
|
||
|
||
const MediaLib = ( { isOpen = false, onChange = () => {}, onToggle = () => {} } ) => { | ||
const { components } = useStrapiApp( 'library', app => app ); | ||
const MediaLibraryDialog = components[ 'media-library' ]; | ||
|
||
const handleSelectAssets = files => { | ||
const formattedFiles = files.map(f => ( { | ||
interface IMediaLibComponent { | ||
isOpen: boolean; | ||
onChange: (files: FormattedMediaFile[]) => void; | ||
onToggle: (idx: any) => void; | ||
allowedTypes: string[]; | ||
} | ||
|
||
interface FormattedMediaFile { | ||
alternativeText?: string; | ||
name?: string; | ||
alt: string; | ||
url: string; | ||
width: number | undefined; | ||
height: number | undefined; | ||
size: number | undefined; | ||
mime: string | undefined; | ||
formats: Record<string, any> | undefined; | ||
} | ||
|
||
|
||
const MediaLibComponent: React.FC<IMediaLibComponent> = ({ | ||
isOpen = false, | ||
onChange, | ||
onToggle, | ||
allowedTypes | ||
}) => { | ||
|
||
const { components } = useStrapiApp('library', (app) => app); | ||
const MediaLibraryDialog = components['media-library'] as React.ComponentType<any>; | ||
|
||
|
||
const handleSelectAssets = (files: FormattedMediaFile[]) => { | ||
const formattedFiles: any = files.map((f) => ({ | ||
alt: f.alternativeText || f.name, | ||
url: prefixFileUrlWithBackendUrl( f.url ), | ||
url: prefixFileUrlWithBackendUrl(f.url), | ||
width: f.width, | ||
height: f.height, | ||
size: f.size, | ||
mime: f.mime, | ||
} ) ); | ||
|
||
onChange( formattedFiles ); | ||
formats: f.formats, | ||
})); | ||
onChange(formattedFiles); | ||
}; | ||
|
||
if ( !isOpen ) { | ||
return null | ||
}; | ||
if (!isOpen) { | ||
return null; | ||
} | ||
|
||
return( | ||
<MediaLibraryDialog onClose={ onToggle } onSelectAssets={ handleSelectAssets } /> | ||
return ( | ||
<MediaLibraryDialog | ||
allowedTypes={allowedTypes} | ||
onClose={onToggle} | ||
onSelectAssets={handleSelectAssets} | ||
/> | ||
); | ||
}; | ||
|
||
MediaLib.propTypes = { | ||
isOpen: PropTypes.bool, | ||
onChange: PropTypes.func, | ||
onToggle: PropTypes.func, | ||
}; | ||
|
||
export default MediaLib; | ||
export default MediaLibComponent; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters