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