index.js
278 lines
| 1 | /** |
| 2 | * WordPress dependencies |
| 3 | */ |
| 4 | const { __ } = wp.i18n; |
| 5 | const { |
| 6 | ToggleControl, |
| 7 | SelectControl, |
| 8 | BaseControl, |
| 9 | Button, |
| 10 | PanelRow, |
| 11 | Icon, |
| 12 | Flex, |
| 13 | } = wp.components; |
| 14 | const { dispatch } = wp.data; |
| 15 | import { isHLS } from "@/shared/util"; |
| 16 | |
| 17 | const { MediaUpload, MediaUploadCheck } = wp.blockEditor; |
| 18 | import ProBadge from "@/admin/blocks/shared/components/ProBadge"; |
| 19 | import MutedPreviewOptions from "./MutedPreviewOptions"; |
| 20 | |
| 21 | const VIDEO_POSTER_ALLOWED_MEDIA_TYPES = ["image"]; |
| 22 | |
| 23 | const { useInstanceId } = wp.compose; |
| 24 | |
| 25 | const VideoSettings = ({ setAttributes, attributes }) => { |
| 26 | const { |
| 27 | mutedPreview, |
| 28 | autoplay, |
| 29 | playsInline, |
| 30 | preload, |
| 31 | poster, |
| 32 | mutedOverlay, |
| 33 | } = attributes; |
| 34 | |
| 35 | const instanceId = useInstanceId(VideoSettings); |
| 36 | |
| 37 | const videoPosterDescription = `video-block__poster-image-description-${instanceId}`; |
| 38 | |
| 39 | const getAutoplayHelp = (checked) => { |
| 40 | return checked |
| 41 | ? __( |
| 42 | "Note: Autoplaying videos may cause usability issues for some visitors.", |
| 43 | "presto-player" |
| 44 | ) |
| 45 | : null; |
| 46 | }; |
| 47 | |
| 48 | const posterRecommended = () => { |
| 49 | // if is hls and we've selected metadata or none, recommend a poster |
| 50 | if ( |
| 51 | attributes?.src && |
| 52 | isHLS(attributes?.src) && |
| 53 | ["metadata", "none"].includes(preload) |
| 54 | ) { |
| 55 | return true; |
| 56 | } |
| 57 | |
| 58 | return preload === "none" && !poster; |
| 59 | }; |
| 60 | |
| 61 | const toggleAttribute = (attribute) => { |
| 62 | return (newValue) => { |
| 63 | setAttributes({ [attribute]: newValue }); |
| 64 | }; |
| 65 | }; |
| 66 | |
| 67 | // handle poster select |
| 68 | function onSelectPoster(image) { |
| 69 | setAttributes({ poster: image.url }); |
| 70 | } |
| 71 | |
| 72 | function onRemovePoster() { |
| 73 | setAttributes({ poster: "" }); |
| 74 | } |
| 75 | |
| 76 | const mutedPreviewControls = () => { |
| 77 | return ( |
| 78 | <> |
| 79 | <ToggleControl |
| 80 | label={ |
| 81 | <> |
| 82 | {__("Muted Autoplay Preview", "presto-player")}{" "} |
| 83 | {!prestoPlayer?.isPremium && <ProBadge />} |
| 84 | </> |
| 85 | } |
| 86 | onChange={(value) => { |
| 87 | if (!prestoPlayer?.isPremium) { |
| 88 | dispatch("presto-player/player").setProModal(true); |
| 89 | return; |
| 90 | } |
| 91 | setAttributes({ |
| 92 | mutedPreview: { |
| 93 | ...mutedPreview, |
| 94 | ...{ enabled: value }, |
| 95 | }, |
| 96 | }); |
| 97 | }} |
| 98 | checked={mutedPreview?.enabled} |
| 99 | className="presto-setting__mutedPreview" |
| 100 | help={__("Shows a muted preview of the video.", "presto-player")} |
| 101 | /> |
| 102 | {!!mutedPreview?.enabled && !attributes?.video_id && ( |
| 103 | <PanelRow> |
| 104 | <ToggleControl |
| 105 | label={__("Muted Preview Captions", "presto-player")} |
| 106 | onChange={(value) => { |
| 107 | setAttributes({ |
| 108 | mutedPreview: { |
| 109 | ...mutedPreview, |
| 110 | ...{ captions: value }, |
| 111 | }, |
| 112 | }); |
| 113 | }} |
| 114 | checked={mutedPreview?.captions} |
| 115 | className="presto-setting__mutedPreviewCaptions" |
| 116 | help={__("Play captions during muted autoplay", "presto-player")} |
| 117 | /> |
| 118 | </PanelRow> |
| 119 | )} |
| 120 | |
| 121 | {!!mutedPreview.enabled && ( |
| 122 | <PanelRow> |
| 123 | <ToggleControl |
| 124 | label={ |
| 125 | <> |
| 126 | {__("Muted Preview Overlay", "presto-player")}{" "} |
| 127 | {!prestoPlayer?.isPremium && <ProBadge />} |
| 128 | </> |
| 129 | } |
| 130 | onChange={(value) => { |
| 131 | if (!prestoPlayer?.isPremium) { |
| 132 | dispatch("presto-player/player").setProModal(true); |
| 133 | return; |
| 134 | } |
| 135 | setAttributes({ |
| 136 | mutedOverlay: { |
| 137 | ...mutedOverlay, |
| 138 | ...{ enabled: value }, |
| 139 | }, |
| 140 | }); |
| 141 | }} |
| 142 | checked={mutedOverlay?.enabled} |
| 143 | className="presto-setting__mutedOverlay" |
| 144 | help={__( |
| 145 | "Show an image over the top of the video either before or after the video.", |
| 146 | "presto-player" |
| 147 | )} |
| 148 | /> |
| 149 | </PanelRow> |
| 150 | )} |
| 151 | {mutedOverlay?.enabled && mutedPreview?.enabled && ( |
| 152 | <MutedPreviewOptions |
| 153 | attributes={attributes} |
| 154 | setAttributes={setAttributes} |
| 155 | /> |
| 156 | )} |
| 157 | </> |
| 158 | ); |
| 159 | }; |
| 160 | |
| 161 | return ( |
| 162 | <> |
| 163 | {!autoplay && mutedPreviewControls()} |
| 164 | |
| 165 | {!mutedPreview?.enabled && ( |
| 166 | <ToggleControl |
| 167 | label={__("Autoplay", "presto-player")} |
| 168 | className="presto-setting__autoplay" |
| 169 | onChange={toggleAttribute("autoplay")} |
| 170 | checked={autoplay} |
| 171 | help={getAutoplayHelp} |
| 172 | /> |
| 173 | )} |
| 174 | <PanelRow> |
| 175 | <ToggleControl |
| 176 | label={__("Play inline", "presto-player")} |
| 177 | className="presto-setting__playsInline" |
| 178 | data-cy={"playsInline"} |
| 179 | onChange={toggleAttribute("playsInline")} |
| 180 | checked={playsInline} |
| 181 | help={__( |
| 182 | "On mobile browsers, play the video on the page instead of opening it up fullscreen.", |
| 183 | "presto-player" |
| 184 | )} |
| 185 | /> |
| 186 | </PanelRow> |
| 187 | {!attributes?.video_id && ( |
| 188 | <PanelRow> |
| 189 | <SelectControl |
| 190 | label={ |
| 191 | <Flex> |
| 192 | <div>{__("Performance Preference", "presto-player")}</div> |
| 193 | <a |
| 194 | href="https://prestoplayer.com/docs/performance-preferences-explained" |
| 195 | target="_blank" |
| 196 | style={{ textDecoration: "none" }} |
| 197 | > |
| 198 | <Icon icon="editor-help" /> |
| 199 | </a> |
| 200 | </Flex> |
| 201 | } |
| 202 | className="presto-setting__preload" |
| 203 | value={preload} |
| 204 | onChange={(value) => setAttributes({ preload: value })} |
| 205 | help={ |
| 206 | posterRecommended() && |
| 207 | __( |
| 208 | "A poster image is recommended for this setting.", |
| 209 | "presto-player" |
| 210 | ) |
| 211 | } |
| 212 | options={[ |
| 213 | { |
| 214 | value: "auto", |
| 215 | label: __("Video Playback Speed", "presto-player"), |
| 216 | }, |
| 217 | { |
| 218 | value: "metadata", |
| 219 | label: __("Page Load Speed", "presto-player"), |
| 220 | }, |
| 221 | { |
| 222 | value: "none", |
| 223 | label: __("Page Load Speed (Extreme)", "presto-player"), |
| 224 | }, |
| 225 | ]} |
| 226 | /> |
| 227 | </PanelRow> |
| 228 | )} |
| 229 | <MediaUploadCheck> |
| 230 | <BaseControl className="editor-video-poster-control"> |
| 231 | <BaseControl.VisualLabel> |
| 232 | <p>{__("Poster image", "presto-player")}</p> |
| 233 | </BaseControl.VisualLabel> |
| 234 | <MediaUpload |
| 235 | title={__("Select poster image", "presto-player")} |
| 236 | onSelect={onSelectPoster} |
| 237 | allowedTypes={VIDEO_POSTER_ALLOWED_MEDIA_TYPES} |
| 238 | render={({ open }) => ( |
| 239 | <Button |
| 240 | className="presto-setting__poster" |
| 241 | isPrimary |
| 242 | onClick={open} |
| 243 | aria-describedby={videoPosterDescription} |
| 244 | > |
| 245 | {!poster |
| 246 | ? __("Select", "presto-player") |
| 247 | : __("Replace", "presto-player")} |
| 248 | </Button> |
| 249 | )} |
| 250 | /> |
| 251 | <p id={videoPosterDescription} hidden> |
| 252 | {poster |
| 253 | ? sprintf( |
| 254 | __("The current poster image url is %s", "presto-player"), |
| 255 | poster |
| 256 | ) |
| 257 | : __( |
| 258 | "There is no poster image currently selected", |
| 259 | "presto-player" |
| 260 | )} |
| 261 | </p> |
| 262 | {!!poster && ( |
| 263 | <Button |
| 264 | onClick={onRemovePoster} |
| 265 | className="presto-setting__remove-poster" |
| 266 | isTertiary |
| 267 | > |
| 268 | {__("Remove", "presto-player")} |
| 269 | </Button> |
| 270 | )} |
| 271 | </BaseControl> |
| 272 | </MediaUploadCheck> |
| 273 | </> |
| 274 | ); |
| 275 | }; |
| 276 | |
| 277 | export default VideoSettings; |
| 278 |