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