MediaProviders
2 weeks ago
ProvidersPlaceholder
2 weeks ago
audioPresets
2 weeks ago
branding
7 months ago
chapters
2 years ago
components
2 weeks ago
media
2 weeks ago
overlays
2 weeks ago
presets
2 weeks ago
services
4 years ago
settings
1 month ago
styles
2 years ago
tracks
2 weeks ago
BlockInspectorControls.js
2 weeks ago
BlockInspectorControls.scss
2 weeks ago
Editing.js
2 years ago
LinkPlaceholder.js
1 month ago
Player.js
1 month ago
Preview.js
2 years ago
ProUpgradeModal.js
1 month ago
VisibilityEditor.js
7 months ago
audio-placeholder.js
2 weeks ago
helpers.js
5 years ago
options.js
2 weeks ago
placeholder.js
2 weeks ago
audio-placeholder.js
247 lines
| 1 | /** |
| 2 | * External dependencies |
| 3 | */ |
| 4 | |
| 5 | import classnames from "classnames"; |
| 6 | import helpers from "./helpers"; |
| 7 | |
| 8 | /** |
| 9 | * WordPress dependencies |
| 10 | */ |
| 11 | import { Button, Placeholder, withFilters } from "@wordpress/components"; |
| 12 | import { __ } from "@wordpress/i18n"; |
| 13 | import { useState, useEffect } from "@wordpress/element"; |
| 14 | import { useSelect } from "@wordpress/data"; |
| 15 | import { |
| 16 | MediaUpload, |
| 17 | MediaUploadCheck, |
| 18 | URLPopover, |
| 19 | } from "@wordpress/block-editor"; |
| 20 | |
| 21 | const InsertFromURLPopover = ({ src, onChange, onSubmit, onClose }) => ( |
| 22 | <URLPopover onClose={onClose}> |
| 23 | <form |
| 24 | className="block-editor-media-placeholder__url-input-form" |
| 25 | onSubmit={onSubmit} |
| 26 | > |
| 27 | <input |
| 28 | data-cy="url-input" |
| 29 | className="block-editor-media-placeholder__url-input-field" |
| 30 | type="url" |
| 31 | aria-label={__("URL", "presto-player")} |
| 32 | placeholder={__("Paste an mp3 URL", "presto-player")} |
| 33 | onChange={onChange} |
| 34 | value={src} |
| 35 | /> |
| 36 | <Button |
| 37 | data-cy="url-submit" |
| 38 | className="block-editor-media-placeholder__url-input-submit-button" |
| 39 | icon={"editor-break"} |
| 40 | label={__("Apply", "presto-player")} |
| 41 | type="submit" |
| 42 | /> |
| 43 | </form> |
| 44 | </URLPopover> |
| 45 | ); |
| 46 | |
| 47 | export function MediaPlaceholder({ |
| 48 | value = {}, |
| 49 | allowedTypes = [], |
| 50 | className, |
| 51 | icon, |
| 52 | url = true, |
| 53 | labels = {}, |
| 54 | mediaPreview, |
| 55 | notices, |
| 56 | isAppender, |
| 57 | isPrivate, |
| 58 | addToGallery, |
| 59 | onSelect, |
| 60 | onCancel, |
| 61 | onSelectURL, |
| 62 | onDoubleClick, |
| 63 | children, |
| 64 | allowURLs, |
| 65 | }) { |
| 66 | const mediaUpload = useSelect((select) => { |
| 67 | const { getSettings } = select("core/block-editor"); |
| 68 | return getSettings().mediaUpload; |
| 69 | }, []); |
| 70 | |
| 71 | const [src, setSrc] = useState(""); |
| 72 | const [isURLInputVisible, setIsURLInputVisible] = useState(false); |
| 73 | |
| 74 | useEffect(() => { |
| 75 | setSrc(value?.src ?? ""); |
| 76 | }, [value]); |
| 77 | |
| 78 | const onChangeSrc = (event) => { |
| 79 | setSrc(event.target.value); |
| 80 | }; |
| 81 | |
| 82 | const openURLInput = () => { |
| 83 | setIsURLInputVisible(true); |
| 84 | }; |
| 85 | |
| 86 | const closeURLInput = () => { |
| 87 | setIsURLInputVisible(false); |
| 88 | }; |
| 89 | |
| 90 | const onSubmitSrc = (event) => { |
| 91 | event.preventDefault(); |
| 92 | if (src && onSelectURL) { |
| 93 | onSelectURL(src); |
| 94 | closeURLInput(); |
| 95 | } |
| 96 | }; |
| 97 | |
| 98 | const renderPlaceholder = (content, onClick) => { |
| 99 | let { instructions, title } = labels; |
| 100 | |
| 101 | if (!mediaUpload && !onSelectURL) { |
| 102 | instructions = __( |
| 103 | "To edit this block, you need permission to upload media.", |
| 104 | "presto-player" |
| 105 | ); |
| 106 | } |
| 107 | |
| 108 | // set class names |
| 109 | const placeholderClassName = classnames( |
| 110 | "block-editor-media-placeholder", |
| 111 | className, |
| 112 | { |
| 113 | "is-appender": isAppender, |
| 114 | } |
| 115 | ); |
| 116 | |
| 117 | return ( |
| 118 | <Placeholder |
| 119 | icon={icon} |
| 120 | label={title} |
| 121 | instructions={instructions} |
| 122 | className={placeholderClassName} |
| 123 | notices={notices} |
| 124 | onClick={onClick} |
| 125 | onDoubleClick={onDoubleClick} |
| 126 | preview={mediaPreview} |
| 127 | > |
| 128 | {children} |
| 129 | {content} |
| 130 | </Placeholder> |
| 131 | ); |
| 132 | }; |
| 133 | |
| 134 | const renderCancelLink = () => { |
| 135 | return ( |
| 136 | onCancel && ( |
| 137 | <Button |
| 138 | className="block-editor-media-placeholder__cancel-button" |
| 139 | title={__("Cancel", "presto-player")} |
| 140 | isLink |
| 141 | onClick={onCancel} |
| 142 | > |
| 143 | {__("Cancel", "presto-player")} |
| 144 | </Button> |
| 145 | ) |
| 146 | ); |
| 147 | }; |
| 148 | |
| 149 | const renderUrlSelectionUI = () => { |
| 150 | return ( |
| 151 | onSelectURL && ( |
| 152 | <div className="block-editor-media-placeholder__url-input-container"> |
| 153 | {url && isPrivate === false && ( |
| 154 | <Button |
| 155 | className="block-editor-media-placeholder__button" |
| 156 | onClick={openURLInput} |
| 157 | isPressed={isURLInputVisible} |
| 158 | isTertiary |
| 159 | > |
| 160 | {__("Audio URL", "presto-player")} |
| 161 | </Button> |
| 162 | )} |
| 163 | {isURLInputVisible && ( |
| 164 | <InsertFromURLPopover |
| 165 | src={src} |
| 166 | onChange={onChangeSrc} |
| 167 | onSubmit={onSubmitSrc} |
| 168 | onClose={closeURLInput} |
| 169 | /> |
| 170 | )} |
| 171 | </div> |
| 172 | ) |
| 173 | ); |
| 174 | }; |
| 175 | |
| 176 | const renderMediaUploadChecked = () => { |
| 177 | const mediaLibraryButton = ( |
| 178 | <MediaUpload |
| 179 | title={ |
| 180 | isPrivate |
| 181 | ? __("Select or Upload Private Audio", "presto-player") |
| 182 | : __("Select or Upload Audio", "presto-player") |
| 183 | } |
| 184 | addToGallery={addToGallery} |
| 185 | gallery={false} |
| 186 | multiple={false} |
| 187 | onSelect={(event) => { |
| 188 | // set private/public url params |
| 189 | helpers.unsetUrlParams(); |
| 190 | onSelect(event); |
| 191 | }} |
| 192 | onClose={() => { |
| 193 | // unset private/public url params |
| 194 | helpers.unsetUrlParams(); |
| 195 | }} |
| 196 | allowedTypes={allowedTypes} |
| 197 | value={Array.isArray(value) ? value.map(({ id }) => id) : value.id} |
| 198 | render={({ open }) => { |
| 199 | return ( |
| 200 | <Button |
| 201 | isPrimary |
| 202 | onClick={(event) => { |
| 203 | event.stopPropagation(); |
| 204 | helpers.unsetUrlParams(); |
| 205 | if (isPrivate) { |
| 206 | helpers.setUrlPrivate(); |
| 207 | } else { |
| 208 | helpers.setUrlPublic(); |
| 209 | } |
| 210 | open(); |
| 211 | }} |
| 212 | > |
| 213 | {isPrivate |
| 214 | ? __("Add/Select Private Audio", "presto-player") |
| 215 | : __("Add/Select Audio", "presto-player")} |
| 216 | </Button> |
| 217 | ); |
| 218 | }} |
| 219 | /> |
| 220 | ); |
| 221 | |
| 222 | if (mediaUpload) { |
| 223 | const content = ( |
| 224 | <> |
| 225 | {mediaLibraryButton} |
| 226 | {!!allowURLs && renderUrlSelectionUI()} |
| 227 | {renderCancelLink()} |
| 228 | </> |
| 229 | ); |
| 230 | return renderPlaceholder(content); |
| 231 | } |
| 232 | |
| 233 | return renderPlaceholder(mediaLibraryButton); |
| 234 | }; |
| 235 | |
| 236 | return ( |
| 237 | <MediaUploadCheck fallback={renderPlaceholder(renderUrlSelectionUI())}> |
| 238 | {renderMediaUploadChecked()} |
| 239 | </MediaUploadCheck> |
| 240 | ); |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * @see https://github.com/WordPress/gutenberg/blob/master/packages/block-editor/src/components/media-placeholder/README.md |
| 245 | */ |
| 246 | export default withFilters("editor.MediaPlaceholder")(MediaPlaceholder); |
| 247 |