-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adds a media replace flow component to the image block
- Loading branch information
1 parent
9b9250a
commit 758a22b
Showing
6 changed files
with
320 additions
and
75 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
199 changes: 199 additions & 0 deletions
199
packages/block-editor/src/components/media-replace-flow/index.js
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,199 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useState, createRef } from '@wordpress/element'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { speak } from '@wordpress/a11y'; | ||
import { | ||
FormFileUpload, | ||
NavigableMenu, | ||
MenuItem, | ||
Toolbar, | ||
Button, | ||
Popover, | ||
withNotices, | ||
} from '@wordpress/components'; | ||
import { | ||
LEFT, | ||
RIGHT, | ||
UP, | ||
DOWN, | ||
BACKSPACE, | ||
ENTER, | ||
} from '@wordpress/keycodes'; | ||
import { useSelect } from '@wordpress/data'; | ||
import { compose } from '@wordpress/compose'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import MediaUpload from '../media-upload'; | ||
import MediaUploadCheck from '../media-upload/check'; | ||
import LinkEditor from '../url-popover/link-editor'; | ||
import LinkViewer from '../url-popover/link-viewer'; | ||
|
||
const MediaReplaceFlow = ( | ||
{ | ||
mediaURL, | ||
allowedTypes, | ||
accept, | ||
onSelect, | ||
onSelectURL, | ||
onError, | ||
name = __( 'Replace' ), | ||
} | ||
) => { | ||
const [ showURLInput, setShowURLInput ] = useState( false ); | ||
const [ showEditURLInput, setShowEditURLInput ] = useState( false ); | ||
const [ mediaURLValue, setMediaURLValue ] = useState( mediaURL ); | ||
const [ showMediaReplaceOptions, setShowMediaReplaceOptions ] = useState( false ); | ||
const mediaUpload = useSelect( ( select ) => { | ||
return select( 'core/block-editor' ).getSettings().mediaUpload; | ||
} ); | ||
const editMediaButtonRef = createRef(); | ||
|
||
const stopPropagation = ( event ) => { | ||
event.stopPropagation(); | ||
}; | ||
|
||
const stopPropagationRelevantKeys = ( event ) => { | ||
if ( [ LEFT, DOWN, RIGHT, UP, BACKSPACE, ENTER ].indexOf( event.keyCode ) > -1 ) { | ||
// Stop the key event from propagating up to ObserveTyping.startTypingInTextField. | ||
event.stopPropagation(); | ||
} | ||
}; | ||
|
||
const selectMedia = ( media ) => { | ||
onSelect( media ); | ||
setMediaURLValue( media.url ); | ||
speak( __( 'The media file has been replaced' ) ); | ||
}; | ||
|
||
const uploadFiles = ( event ) => { | ||
const files = event.target.files; | ||
const setMedia = ( [ media ] ) => { | ||
setShowMediaReplaceOptions( false ); | ||
selectMedia( media ); | ||
}; | ||
mediaUpload( { | ||
allowedTypes, | ||
filesList: files, | ||
onFileChange: setMedia, | ||
onError, | ||
} ); | ||
}; | ||
|
||
const onClose = () => { | ||
editMediaButtonRef.current.focus(); | ||
}; | ||
|
||
const onClickOutside = () => ( setShowMediaReplaceOptions( false ) ); | ||
|
||
const openOnArrowDown = ( event ) => { | ||
if ( event.keyCode === DOWN ) { | ||
event.preventDefault(); | ||
event.stopPropagation(); | ||
event.target.click(); | ||
} | ||
}; | ||
|
||
let urlInputUIContent; | ||
if ( showEditURLInput ) { | ||
urlInputUIContent = ( | ||
<LinkEditor | ||
onKeyDown={ stopPropagationRelevantKeys } | ||
onKeyPress={ stopPropagation } | ||
value={ mediaURLValue } | ||
isFullWidthInput={ true } | ||
hasInputBorder={ true } | ||
onChangeInputValue={ ( url ) => ( setMediaURLValue( url ) ) } | ||
onSubmit={ ( event ) => { | ||
event.preventDefault(); | ||
onSelectURL( mediaURLValue ); | ||
setShowEditURLInput( ! showEditURLInput ); | ||
editMediaButtonRef.current.focus(); | ||
} } | ||
/> | ||
); | ||
} else { | ||
urlInputUIContent = ( | ||
<LinkViewer | ||
isFullWidth={ true } | ||
className="editor-format-toolbar__link-container-content block-editor-format-toolbar__link-container-content" | ||
url={ mediaURLValue } | ||
onEditLinkClick={ () => ( setShowEditURLInput( ! showEditURLInput ) ) } | ||
/> | ||
); | ||
} | ||
|
||
return ( | ||
<MediaUpload | ||
onSelect={ ( media ) => selectMedia( media ) } | ||
onClose={ () => setShowMediaReplaceOptions( true ) } | ||
allowedTypes={ allowedTypes } | ||
render={ ( { open } ) => ( | ||
<Toolbar className={ 'media-replace-flow components-dropdown-menu' }> | ||
<Button | ||
ref={ editMediaButtonRef } | ||
className={ 'components-icon-button components-dropdown-menu__toggle' } | ||
onClick={ () => { | ||
setShowMediaReplaceOptions( ! showMediaReplaceOptions ); | ||
} } | ||
onKeyDown={ openOnArrowDown } | ||
> | ||
<span className="components-dropdown-menu__label" > { name } </span> | ||
<span className="components-dropdown-menu__indicator" /> | ||
</Button> | ||
{ showMediaReplaceOptions && | ||
<Popover | ||
onClickOutside={ onClickOutside } | ||
onClose={ onClose } | ||
className={ 'media-replace-flow__options' } | ||
> | ||
<NavigableMenu> | ||
<MenuItem | ||
icon="admin-media" | ||
onClick={ open } | ||
> | ||
{ __( 'Open Media Library' ) } | ||
</MenuItem> | ||
<MediaUploadCheck> | ||
<FormFileUpload | ||
onChange={ uploadFiles } | ||
accept={ accept } | ||
render={ ( { openFileDialog } ) => { | ||
return ( | ||
<MenuItem | ||
icon="upload" | ||
onClick={ () => { | ||
openFileDialog(); | ||
} } | ||
> | ||
{ __( 'Upload' ) } | ||
</MenuItem> | ||
); | ||
} } | ||
/> | ||
</MediaUploadCheck> | ||
<MenuItem | ||
icon="admin-links" | ||
onClick={ () => ( setShowURLInput( ! showURLInput ) ) } | ||
aria-expanded={ showURLInput } | ||
> | ||
<div> { __( 'Insert from URL' ) } </div> | ||
</MenuItem> | ||
</NavigableMenu> | ||
{ showURLInput && <div className="block-editor-media-flow__url-input"> | ||
{ urlInputUIContent } | ||
</div> } | ||
</Popover> | ||
} | ||
</Toolbar> | ||
) } | ||
/> | ||
); | ||
}; | ||
|
||
export default compose( | ||
withNotices, | ||
)( MediaReplaceFlow ); |
72 changes: 72 additions & 0 deletions
72
packages/block-editor/src/components/media-replace-flow/style.scss
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,72 @@ | ||
.media-replace-flow .components-dropdown-menu__indicator { | ||
margin-left: 4px; | ||
|
||
.components-dropdown-menu.media-flow_toolbar { | ||
.components-dropdown-menu__label { | ||
margin-right: 6px; | ||
margin-left: 2px; | ||
} | ||
} | ||
} | ||
|
||
.media-replace-flow__options.components-popover:not(.is-mobile) { | ||
|
||
.components-popover__content { | ||
// this is a safari problem that shows scrollbars | ||
// when display:flex and max-width are used together | ||
overflow-x: hidden; | ||
} | ||
|
||
.block-editor-media-flow__url-input { | ||
|
||
padding: 0 15px 10px 25px; | ||
|
||
.components-external-link__icon { | ||
position: absolute; | ||
right: 50px; | ||
bottom: 22px; | ||
} | ||
|
||
input { | ||
max-width: 169px; | ||
border: 1px solid $dark-gray-500; | ||
border-radius: 4px; | ||
} | ||
|
||
.block-editor-url-popover__link-viewer-url { | ||
padding-right: 10px; | ||
padding-top: 3px; | ||
max-width: 169px; | ||
} | ||
|
||
// Mimic toolbar component styles for the icons in this popover. | ||
.components-icon-button { | ||
padding: 5px; | ||
width: 40px; | ||
height: 40px; | ||
|
||
> svg { | ||
padding: 5px; | ||
border-radius: $radius-round-rectangle; | ||
height: 30px; | ||
width: 30px; | ||
} | ||
|
||
&:not(:disabled):not([aria-disabled="true"]):not(.is-default):hover { | ||
box-shadow: none; | ||
|
||
> svg { | ||
@include formatting-button-style__hover; | ||
} | ||
} | ||
|
||
&:not(:disabled):focus { | ||
box-shadow: none; | ||
|
||
> svg { | ||
@include formatting-button-style__focus; | ||
} | ||
} | ||
} | ||
} | ||
} |
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.