|
| 1 | +/** |
| 2 | + * WordPress dependencies |
| 3 | + */ |
| 4 | +import { useState, createRef } from '@wordpress/element'; |
| 5 | +import { __ } from '@wordpress/i18n'; |
| 6 | +import { speak } from '@wordpress/a11y'; |
| 7 | +import { |
| 8 | + FormFileUpload, |
| 9 | + NavigableMenu, |
| 10 | + MenuItem, |
| 11 | + Toolbar, |
| 12 | + Button, |
| 13 | + Popover, |
| 14 | + withNotices, |
| 15 | +} from '@wordpress/components'; |
| 16 | +import { |
| 17 | + LEFT, |
| 18 | + RIGHT, |
| 19 | + UP, |
| 20 | + DOWN, |
| 21 | + BACKSPACE, |
| 22 | + ENTER, |
| 23 | +} from '@wordpress/keycodes'; |
| 24 | +import { useSelect } from '@wordpress/data'; |
| 25 | +import { compose } from '@wordpress/compose'; |
| 26 | + |
| 27 | +/** |
| 28 | + * Internal dependencies |
| 29 | + */ |
| 30 | +import MediaUpload from '../media-upload'; |
| 31 | +import MediaUploadCheck from '../media-upload/check'; |
| 32 | +import LinkEditor from '../url-popover/link-editor'; |
| 33 | +import LinkViewer from '../url-popover/link-viewer'; |
| 34 | + |
| 35 | +const MediaReplaceFlow = ( |
| 36 | + { |
| 37 | + mediaURL, |
| 38 | + allowedTypes, |
| 39 | + accept, |
| 40 | + onSelect, |
| 41 | + onSelectURL, |
| 42 | + onError, |
| 43 | + name = __( 'Replace' ), |
| 44 | + } |
| 45 | +) => { |
| 46 | + const [ showURLInput, setShowURLInput ] = useState( false ); |
| 47 | + const [ showEditURLInput, setShowEditURLInput ] = useState( false ); |
| 48 | + const [ mediaURLValue, setMediaURLValue ] = useState( mediaURL ); |
| 49 | + const [ showMediaReplaceOptions, setShowMediaReplaceOptions ] = useState( false ); |
| 50 | + const mediaUpload = useSelect( ( select ) => { |
| 51 | + return select( 'core/block-editor' ).getSettings().mediaUpload; |
| 52 | + } ); |
| 53 | + const editMediaButtonRef = createRef(); |
| 54 | + |
| 55 | + const stopPropagation = ( event ) => { |
| 56 | + event.stopPropagation(); |
| 57 | + }; |
| 58 | + |
| 59 | + const stopPropagationRelevantKeys = ( event ) => { |
| 60 | + if ( [ LEFT, DOWN, RIGHT, UP, BACKSPACE, ENTER ].indexOf( event.keyCode ) > -1 ) { |
| 61 | + // Stop the key event from propagating up to ObserveTyping.startTypingInTextField. |
| 62 | + event.stopPropagation(); |
| 63 | + } |
| 64 | + }; |
| 65 | + |
| 66 | + const selectMedia = ( media ) => { |
| 67 | + onSelect( media ); |
| 68 | + setMediaURLValue( media.url ); |
| 69 | + speak( __( 'The media file has been replaced' ) ); |
| 70 | + }; |
| 71 | + |
| 72 | + const selectURL = ( newURL ) => { |
| 73 | + onSelectURL( newURL ); |
| 74 | + setShowEditURLInput( false ); |
| 75 | + }; |
| 76 | + |
| 77 | + const uploadFiles = ( event ) => { |
| 78 | + const files = event.target.files; |
| 79 | + const setMedia = ( [ media ] ) => { |
| 80 | + setShowMediaReplaceOptions( false ); |
| 81 | + selectMedia( media ); |
| 82 | + }; |
| 83 | + mediaUpload( { |
| 84 | + allowedTypes, |
| 85 | + filesList: files, |
| 86 | + onFileChange: setMedia, |
| 87 | + onError, |
| 88 | + } ); |
| 89 | + }; |
| 90 | + |
| 91 | + const onClose = () => { |
| 92 | + editMediaButtonRef.current.focus(); |
| 93 | + }; |
| 94 | + |
| 95 | + const onClickOutside = () => ( setShowMediaReplaceOptions( false ) ); |
| 96 | + |
| 97 | + const openOnArrowDown = ( event ) => { |
| 98 | + if ( event.keyCode === DOWN ) { |
| 99 | + event.preventDefault(); |
| 100 | + event.stopPropagation(); |
| 101 | + event.target.click(); |
| 102 | + } |
| 103 | + }; |
| 104 | + |
| 105 | + let urlInputUIContent; |
| 106 | + if ( showEditURLInput ) { |
| 107 | + urlInputUIContent = ( |
| 108 | + <LinkEditor |
| 109 | + onKeyDown={ stopPropagationRelevantKeys } |
| 110 | + onKeyPress={ stopPropagation } |
| 111 | + value={ mediaURLValue } |
| 112 | + isFullWidthInput={ true } |
| 113 | + hasInputBorder={ true } |
| 114 | + onChangeInputValue={ ( url ) => ( setMediaURLValue( url ) ) } |
| 115 | + onSubmit={ ( event ) => { |
| 116 | + event.preventDefault(); |
| 117 | + selectURL( mediaURLValue ); |
| 118 | + editMediaButtonRef.current.focus(); |
| 119 | + } } |
| 120 | + /> |
| 121 | + ); |
| 122 | + } else { |
| 123 | + urlInputUIContent = ( |
| 124 | + <LinkViewer |
| 125 | + isFullWidth={ true } |
| 126 | + className="editor-format-toolbar__link-container-content block-editor-format-toolbar__link-container-content" |
| 127 | + url={ mediaURLValue } |
| 128 | + onEditLinkClick={ () => ( setShowEditURLInput( ! showEditURLInput ) ) } |
| 129 | + /> |
| 130 | + ); |
| 131 | + } |
| 132 | + |
| 133 | + return ( |
| 134 | + <MediaUpload |
| 135 | + onSelect={ ( media ) => selectMedia( media ) } |
| 136 | + onClose={ () => setShowMediaReplaceOptions( true ) } |
| 137 | + allowedTypes={ allowedTypes } |
| 138 | + render={ ( { open } ) => ( |
| 139 | + <Toolbar className={ 'media-replace-flow components-dropdown-menu' }> |
| 140 | + <Button |
| 141 | + ref={ editMediaButtonRef } |
| 142 | + className={ 'components-icon-button components-dropdown-menu__toggle' } |
| 143 | + onClick={ () => { |
| 144 | + setShowMediaReplaceOptions( ! showMediaReplaceOptions ); |
| 145 | + } } |
| 146 | + onKeyDown={ openOnArrowDown } |
| 147 | + > |
| 148 | + <span className="components-dropdown-menu__label" > { name } </span> |
| 149 | + <span className="components-dropdown-menu__indicator" /> |
| 150 | + </Button> |
| 151 | + { showMediaReplaceOptions && |
| 152 | + <Popover |
| 153 | + onClickOutside={ onClickOutside } |
| 154 | + onClose={ onClose } |
| 155 | + className={ 'media-replace-flow__options' } |
| 156 | + > |
| 157 | + <NavigableMenu> |
| 158 | + <MenuItem |
| 159 | + icon="admin-media" |
| 160 | + onClick={ open } |
| 161 | + > |
| 162 | + { __( 'Open Media Library' ) } |
| 163 | + </MenuItem> |
| 164 | + <MediaUploadCheck> |
| 165 | + <FormFileUpload |
| 166 | + onChange={ uploadFiles } |
| 167 | + accept={ accept } |
| 168 | + render={ ( { openFileDialog } ) => { |
| 169 | + return ( |
| 170 | + <MenuItem |
| 171 | + icon="upload" |
| 172 | + onClick={ () => { |
| 173 | + openFileDialog(); |
| 174 | + } } |
| 175 | + > |
| 176 | + { __( 'Upload' ) } |
| 177 | + </MenuItem> |
| 178 | + ); |
| 179 | + } } |
| 180 | + /> |
| 181 | + </MediaUploadCheck> |
| 182 | + <MenuItem |
| 183 | + icon="admin-links" |
| 184 | + onClick={ () => ( setShowURLInput( ! showURLInput ) ) } |
| 185 | + aria-expanded={ showURLInput } |
| 186 | + > |
| 187 | + <div> { __( 'Insert from URL' ) } </div> |
| 188 | + </MenuItem> |
| 189 | + </NavigableMenu> |
| 190 | + { showURLInput && <div className="block-editor-media-flow__url-input"> |
| 191 | + { urlInputUIContent } |
| 192 | + </div> } |
| 193 | + </Popover> |
| 194 | + } |
| 195 | + </Toolbar> |
| 196 | + ) } |
| 197 | + /> |
| 198 | + ); |
| 199 | +}; |
| 200 | + |
| 201 | +export default compose( |
| 202 | + withNotices, |
| 203 | +)( MediaReplaceFlow ); |
0 commit comments