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