-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #103 from heikkivihersalo/feature/no-ref/image-gal…
…lery-block-to-typescript Image gallery to typescript
- Loading branch information
Showing
17 changed files
with
2,346 additions
and
2,451 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
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
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
30 changes: 30 additions & 0 deletions
30
src/block-library/custom/image-gallery/components/Image.tsx
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import type { BlockJSON_ImageAttributes } from '@components/media'; | ||
|
||
type Props = { | ||
image: BlockJSON_ImageAttributes; | ||
}; | ||
|
||
/** | ||
* Individual Image Component | ||
* @param {Object} props - Image props | ||
* @param {Object} props.image - Image object | ||
* @return {JSX.Element} Image component | ||
*/ | ||
function Image({ image }: Props): JSX.Element { | ||
return ( | ||
<img | ||
data-id={image.mediaId} | ||
loading={`${image.lazyLoad ? 'lazy' : 'eager'}`} | ||
className="image-gallery__image" | ||
src={image.mediaUrl} | ||
alt={image.mediaAlt} | ||
width={image.mediaWidth} | ||
height={image.mediaHeight} | ||
/> | ||
); | ||
} | ||
|
||
export default Image; |
This file was deleted.
Oops, something went wrong.
58 changes: 0 additions & 58 deletions
58
src/block-library/custom/image-gallery/components/MediaUploader.jsx
This file was deleted.
Oops, something went wrong.
102 changes: 102 additions & 0 deletions
102
src/block-library/custom/image-gallery/components/MediaUploader.tsx
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 |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
import { MediaUpload, MediaUploadCheck } from '@wordpress/block-editor'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { Button } from '@wordpress/components'; | ||
import type { MediaUploader_Image } from '@components/media'; | ||
import type { BlockSetAttributes } from '../types'; | ||
|
||
type Props = { | ||
mediaIds: number[]; | ||
setAttributes: BlockSetAttributes; | ||
}; | ||
|
||
/** | ||
* Get the largest image size slug available | ||
* @param {Object[]} image Image sizes | ||
* @return {string} Image size slug key | ||
*/ | ||
const getImageWidthAndHeight = ( | ||
image: MediaUploader_Image | ||
): { width: number | undefined; height: number | undefined } => { | ||
/** | ||
* There is a possibility that the image does not have any sizes available | ||
* In that case, return empty strings for width and height | ||
*/ | ||
if (!image.sizes) { | ||
return { | ||
width: undefined, | ||
height: undefined, | ||
}; | ||
} | ||
|
||
/** | ||
* For normal cases where the image has sizes available we will get the largest possible size | ||
*/ | ||
const sizeSlugs = Object.keys(image.sizes); | ||
|
||
const largestSize = sizeSlugs.reduce((prev, current) => { | ||
return image.sizes[prev].width > image.sizes[current].width | ||
? prev | ||
: current; | ||
}); | ||
|
||
return { | ||
width: image.sizes[largestSize].width, | ||
height: image.sizes[largestSize].height, | ||
}; | ||
}; | ||
|
||
/** | ||
* Block edit function | ||
* @param {Object} props Properties | ||
* @param {number[]} props.mediaIds Media IDs | ||
* @param {Function} props.setAttributes Set attributes function | ||
* @return {JSX.Element} React component | ||
*/ | ||
const MediaUploader = ({ mediaIds, setAttributes }: Props): JSX.Element => { | ||
const onSelect = (images: MediaUploader_Image[]) => { | ||
setAttributes({ | ||
images: images.map((image) => { | ||
const { id, url, alt, mime } = image; | ||
const { width, height } = getImageWidthAndHeight(image); | ||
|
||
return { | ||
mediaId: id, | ||
mediaUrl: url, | ||
mediaAlt: alt, | ||
mediaMime: mime, | ||
mediaWidth: width, | ||
mediaHeight: height, | ||
lazyLoad: true, | ||
}; | ||
}), | ||
}); | ||
}; | ||
|
||
return ( | ||
<MediaUploadCheck> | ||
<MediaUpload | ||
onSelect={onSelect} | ||
allowedTypes={['image']} | ||
value={mediaIds} | ||
render={({ open }) => ( | ||
<Button onClick={open} className="button button-large"> | ||
{mediaIds.length < 1 | ||
? __('Upload/Select Images', 'kotisivu-block-theme') | ||
: __('Edit', 'kotisivu-block-theme')} | ||
</Button> | ||
)} | ||
gallery | ||
multiple | ||
/> | ||
</MediaUploadCheck> | ||
); | ||
}; | ||
|
||
export default MediaUploader; |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
import { useBlockProps } from '@wordpress/block-editor'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import MediaUploader from './components/MediaUploader'; | ||
import Image from './components/Image'; | ||
import type { BlockAttributes, BlockSetAttributes } from './types'; | ||
|
||
import './editor.css'; | ||
|
||
type Props = { | ||
attributes: BlockAttributes; | ||
setAttributes: BlockSetAttributes; | ||
}; | ||
|
||
/** | ||
* Block edit function | ||
* @param {Props} props Properties | ||
* @param {BlockAttributes} props.attributes Block attributes | ||
* @param {BlockSetAttributes} props.setAttributes Set attributes | ||
* @return {JSX.Element} React component | ||
*/ | ||
export default function Edit({ | ||
attributes, | ||
setAttributes, | ||
}: Props): JSX.Element { | ||
const { images, blockClass } = attributes; | ||
|
||
/** | ||
* Set block props | ||
*/ | ||
const blockProps = useBlockProps({ | ||
className: `image-gallery ${blockClass}`, | ||
}); | ||
|
||
return ( | ||
<div className="editor-image-gallery"> | ||
{images.length >= 1 ? ( | ||
<div {...blockProps}> | ||
{images.map((image) => ( | ||
<Image key={image.mediaId} image={image} /> | ||
))} | ||
</div> | ||
) : ( | ||
<div className="editor-image-gallery__help"> | ||
{__( | ||
'Selected images will be shown here', | ||
'kotisivu-block-theme' | ||
)} | ||
</div> | ||
)} | ||
<MediaUploader | ||
mediaIds={images.map((image) => image.mediaId)} | ||
setAttributes={setAttributes} | ||
/> | ||
</div> | ||
); | ||
} |
Oops, something went wrong.