-
Notifications
You must be signed in to change notification settings - Fork 44
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 #269 from 10up/feature/include-figure-in-image-com…
…ponent-inline-controls Add inline media controls for the Image component
- Loading branch information
Showing
11 changed files
with
395 additions
and
85 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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import PropTypes from 'prop-types'; | ||
import { StyledComponentContext } from '../../styled-components-context'; | ||
import { InlineControlsStyleWrapper } from '../styles'; | ||
|
||
export const Figure = (props) => { | ||
const { style, children, ...rest } = props; | ||
|
||
return ( | ||
<StyledComponentContext cacheKey="tenup-component-image"> | ||
<InlineControlsStyleWrapper style={{ ...style }} {...rest}> | ||
{children} | ||
</InlineControlsStyleWrapper> | ||
</StyledComponentContext> | ||
); | ||
}; | ||
|
||
Figure.defaultProps = { | ||
style: {}, | ||
children: undefined, | ||
}; | ||
|
||
Figure.propTypes = { | ||
style: PropTypes.object, | ||
children: PropTypes.node, | ||
}; |
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,5 @@ | ||
import { Media, ImageContext } from './media'; | ||
import { Figure } from './figure'; | ||
import { InlineControls } from './inline-controls'; | ||
|
||
export { Media, ImageContext, Figure, InlineControls }; |
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,39 @@ | ||
import { __ } from '@wordpress/i18n'; | ||
import { MediaReplaceFlow } from '@wordpress/block-editor'; | ||
import { ToolbarButton } from '@wordpress/components'; | ||
import PropTypes from 'prop-types'; | ||
|
||
/** | ||
* Internal Dependencies | ||
*/ | ||
|
||
export const InlineControls = (props) => { | ||
const { imageUrl, onSelect, isOptional, onRemove } = props; | ||
|
||
return ( | ||
<div className="inline-controls-sticky-wrapper"> | ||
<div className="inline-controls"> | ||
<MediaReplaceFlow mediaUrl={imageUrl} onSelect={onSelect} name={__('Replace')} /> | ||
{!!isOptional && ( | ||
<ToolbarButton onClick={onRemove} className="remove-button"> | ||
{__('Remove')} | ||
</ToolbarButton> | ||
)} | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
InlineControls.defaultProps = { | ||
imageUrl: '', | ||
onSelect: undefined, | ||
isOptional: false, | ||
onRemove: undefined, | ||
}; | ||
|
||
InlineControls.propTypes = { | ||
imageUrl: PropTypes.string, | ||
onSelect: PropTypes.func, | ||
isOptional: PropTypes.bool, | ||
onRemove: PropTypes.func, | ||
}; |
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,82 @@ | ||
import PropTypes from 'prop-types'; | ||
import { useContext, createContext } from '@wordpress/element'; | ||
import { MediaPlaceholder, InspectorControls } from '@wordpress/block-editor'; | ||
import { Spinner, FocalPointPicker, PanelBody, Placeholder } from '@wordpress/components'; | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
export const ImageContext = createContext(); | ||
|
||
export const Media = (props) => { | ||
const { style, ...rest } = props; | ||
const { | ||
imageUrl, | ||
altText, | ||
labels, | ||
onSelect, | ||
isResolvingMedia, | ||
shouldDisplayFocalPointPicker, | ||
focalPoint, | ||
onChangeFocalPoint, | ||
canEditImage, | ||
hasImage, | ||
} = useContext(ImageContext); | ||
|
||
let focalPointStyle = {}; | ||
|
||
if (shouldDisplayFocalPointPicker) { | ||
focalPointStyle = { | ||
objectFit: 'cover', | ||
objectPosition: `${focalPoint.x * 100}% ${focalPoint.y * 100}%`, | ||
}; | ||
} | ||
|
||
if (isResolvingMedia) { | ||
return <Spinner />; | ||
} | ||
|
||
if (!hasImage && !canEditImage) { | ||
return <Placeholder className="block-editor-media-placeholder" withIllustration />; | ||
} | ||
|
||
return ( | ||
<> | ||
{shouldDisplayFocalPointPicker && ( | ||
<InspectorControls> | ||
<PanelBody title={__('Image Settings')}> | ||
<FocalPointPicker | ||
label={__('Focal Point Picker')} | ||
url={imageUrl} | ||
value={focalPoint} | ||
onChange={onChangeFocalPoint} | ||
/> | ||
</PanelBody> | ||
</InspectorControls> | ||
)} | ||
{hasImage && ( | ||
<img | ||
src={imageUrl} | ||
alt={altText} | ||
style={{ ...style, ...focalPointStyle }} | ||
{...rest} | ||
/> | ||
)} | ||
{canEditImage && ( | ||
<MediaPlaceholder | ||
labels={labels} | ||
onSelect={onSelect} | ||
accept="image" | ||
multiple={false} | ||
disableMediaButtons={imageUrl} | ||
/> | ||
)} | ||
</> | ||
); | ||
}; | ||
|
||
Media.defaultProps = { | ||
style: {}, | ||
}; | ||
|
||
Media.propTypes = { | ||
style: PropTypes.object, | ||
}; |
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
Oops, something went wrong.