index.js
160 lines
| 1 | /** |
| 2 | * WordPress dependencies |
| 3 | */ |
| 4 | import { __, sprintf } from "@wordpress/i18n"; |
| 5 | const { ColorPicker, Button, withNotices, BaseControl, RangeControl } = |
| 6 | wp.components; |
| 7 | const { MediaUpload, MediaUploadCheck } = wp.blockEditor; |
| 8 | const { useInstanceId } = wp.compose; |
| 9 | const { useState } = wp.element; |
| 10 | const { dispatch, useSelect } = wp.data; |
| 11 | import ProBadge from "@/admin/blocks/shared/components/ProBadge"; |
| 12 | |
| 13 | const VIDEO_LOGO_ALLOWED_MEDIA_TYPES = ["image"]; |
| 14 | |
| 15 | function PlayerSettings({ setAttributes, attributes, type }) { |
| 16 | const instanceId = useInstanceId(PlayerSettings); |
| 17 | const [pickerRender, setPickerRender] = useState(1); |
| 18 | |
| 19 | // get branding options |
| 20 | const branding = useSelect((select) => { |
| 21 | return select("presto-player/player").branding(); |
| 22 | }); |
| 23 | // save options |
| 24 | const saveOptions = async () => { |
| 25 | await dispatch("presto-player/player").saveOptions({ |
| 26 | branding, |
| 27 | }); |
| 28 | wp.data.dispatch("core/notices").createNotice( |
| 29 | "success", // Can be one of: success, info, warning, error. |
| 30 | "Player branding saved.", // Text string to display. |
| 31 | { |
| 32 | type: "snackbar", |
| 33 | isDismissible: true, // Whether the user can dismiss the notice. |
| 34 | // Any actions the user can perform. |
| 35 | } |
| 36 | ); |
| 37 | }; |
| 38 | |
| 39 | function onSelectLogo(image) { |
| 40 | dispatch("presto-player/player").updateBranding("logo", image.url); |
| 41 | } |
| 42 | |
| 43 | function onRemoveLogo() { |
| 44 | dispatch("presto-player/player").updateBranding("logo", ""); |
| 45 | } |
| 46 | |
| 47 | return ( |
| 48 | <div className="presto-player__panel--branding"> |
| 49 | {type !== "audio" && ( |
| 50 | <p> |
| 51 | {__( |
| 52 | "Here you can select the global player branding This will apply to all players on the site.", |
| 53 | "presto-player" |
| 54 | )} |
| 55 | </p> |
| 56 | )} |
| 57 | |
| 58 | <MediaUploadCheck> |
| 59 | <BaseControl className="editor-video-poster-control"> |
| 60 | {type !== "audio" && ( |
| 61 | <> |
| 62 | <BaseControl.VisualLabel> |
| 63 | <p> |
| 64 | {__("Logo Overlay", "presto-player")}{" "} |
| 65 | {!prestoPlayer?.isPremium && <ProBadge />} |
| 66 | </p> |
| 67 | </BaseControl.VisualLabel> |
| 68 | <MediaUpload |
| 69 | title={__("Select logo overlay image", "presto-player")} |
| 70 | onSelect={onSelectLogo} |
| 71 | allowedTypes={VIDEO_LOGO_ALLOWED_MEDIA_TYPES} |
| 72 | render={({ open }) => ( |
| 73 | <Button |
| 74 | className="presto-setting__logo" |
| 75 | isSecondary |
| 76 | onClick={() => { |
| 77 | if (!prestoPlayer?.isPremium) { |
| 78 | dispatch("presto-player/player").setProModal(true); |
| 79 | return; |
| 80 | } |
| 81 | open(); |
| 82 | }} |
| 83 | aria-describedby={`video-block__logo-image-description-${instanceId}`} |
| 84 | > |
| 85 | {!branding?.logo |
| 86 | ? __("Select", "presto-player") |
| 87 | : __("Replace", "presto-player")} |
| 88 | </Button> |
| 89 | )} |
| 90 | /> |
| 91 | </> |
| 92 | )} |
| 93 | |
| 94 | <p id={`video-block__logo-image-description-${instanceId}`} hidden> |
| 95 | {branding?.logo |
| 96 | ? sprintf( |
| 97 | /* translators: %s: poster image URL. */ |
| 98 | __("The current logo image url is %s", "presto-player"), |
| 99 | branding?.logo |
| 100 | ) |
| 101 | : __( |
| 102 | "There is no logo image currently selected", |
| 103 | "presto-player" |
| 104 | )} |
| 105 | </p> |
| 106 | {!!branding?.logo && ( |
| 107 | <Button onClick={onRemoveLogo} isTertiary> |
| 108 | {__("Remove", "presto-player")} |
| 109 | </Button> |
| 110 | )} |
| 111 | </BaseControl> |
| 112 | </MediaUploadCheck> |
| 113 | |
| 114 | {!!branding?.logo && ( |
| 115 | <RangeControl |
| 116 | label={__("Logo Max Width", "presto-player")} |
| 117 | value={branding?.logo_width || 150} |
| 118 | onChange={(width) => |
| 119 | dispatch("presto-player/player").updateBranding("logo_width", width) |
| 120 | } |
| 121 | min={1} |
| 122 | max={400} |
| 123 | /> |
| 124 | )} |
| 125 | |
| 126 | <ColorPicker |
| 127 | color={branding?.color} |
| 128 | onChangeComplete={(value) => { |
| 129 | dispatch("presto-player/player").updateBranding("color", value.hex); |
| 130 | }} |
| 131 | key={pickerRender} |
| 132 | disableAlpha |
| 133 | /> |
| 134 | |
| 135 | {branding?.color && ( |
| 136 | <BaseControl> |
| 137 | <Button |
| 138 | isSecondary |
| 139 | onClick={() => { |
| 140 | dispatch("presto-player/player").updateBranding( |
| 141 | "color", |
| 142 | prestoPlayer?.defaults?.color || "#00b3ff" |
| 143 | ); |
| 144 | setPickerRender(pickerRender + 1); |
| 145 | }} |
| 146 | > |
| 147 | {__("Reset Color", "presto-player")} |
| 148 | </Button> |
| 149 | </BaseControl> |
| 150 | )} |
| 151 | |
| 152 | <Button isPrimary onClick={saveOptions}> |
| 153 | {__("Save Branding", "presto-player")} |
| 154 | </Button> |
| 155 | </div> |
| 156 | ); |
| 157 | } |
| 158 | |
| 159 | export default withNotices(PlayerSettings); |
| 160 |