Overlay.js
201 lines
| 1 | const { __ } = wp.i18n; |
| 2 | const { |
| 3 | Flex, |
| 4 | FlexItem, |
| 5 | TextControl, |
| 6 | Button, |
| 7 | BaseControl, |
| 8 | RadioControl, |
| 9 | RangeControl, |
| 10 | withFocusReturn, |
| 11 | } = wp.components; |
| 12 | const { useRef, useEffect } = wp.element; |
| 13 | import "./Overlay.scss"; |
| 14 | |
| 15 | import { sanitizeTime, timeToSeconds, secondsToTime } from "../../../util"; |
| 16 | import UrlSelect from "../../components/UrlSelect"; |
| 17 | import DynamicText from "./DynamicText"; |
| 18 | import ColorPopup from "../../components/ColorPopup"; |
| 19 | |
| 20 | const { useState } = wp.element; |
| 21 | |
| 22 | const Overlay = ({ |
| 23 | overlayIndex, |
| 24 | update, |
| 25 | remove, |
| 26 | className, |
| 27 | startTime, |
| 28 | endTime, |
| 29 | text, |
| 30 | link, |
| 31 | position, |
| 32 | color, |
| 33 | backgroundColor, |
| 34 | opacity, |
| 35 | updateCurrentTime, |
| 36 | }) => { |
| 37 | const [draftStartTime, setDraftStartTime] = useState(startTime); |
| 38 | const [draftEndTime, setDraftEndTime] = useState(endTime); |
| 39 | const [draftPosition, setDraftPosition] = useState(position); |
| 40 | const startControl = useRef(); |
| 41 | |
| 42 | useEffect(() => { |
| 43 | if (timeToSeconds(startTime) >= timeToSeconds(endTime)) { |
| 44 | let endTime = sanitizeTime(startTime); |
| 45 | let seconds = timeToSeconds(endTime) + 1; |
| 46 | endTime = sanitizeTime(secondsToTime(seconds)); |
| 47 | update({ endTime }); |
| 48 | setDraftEndTime(endTime); |
| 49 | } |
| 50 | }, [startTime, endTime]); |
| 51 | |
| 52 | const updateStartTime = () => { |
| 53 | const startTime = sanitizeTime(draftStartTime); |
| 54 | update({ startTime }); |
| 55 | setDraftStartTime(startTime); |
| 56 | updateCurrentTime(startTime); |
| 57 | }; |
| 58 | |
| 59 | const updateEndTime = () => { |
| 60 | const endTime = sanitizeTime(draftEndTime); |
| 61 | update({ endTime }); |
| 62 | setDraftEndTime(endTime); |
| 63 | }; |
| 64 | |
| 65 | return ( |
| 66 | <div className="presto-overlay"> |
| 67 | <Flex align="center" className={className}> |
| 68 | <FlexItem> |
| 69 | <TextControl |
| 70 | ref={startControl} |
| 71 | id={`start-time-${overlayIndex}`} |
| 72 | label={__("Start Time", "presto-player")} |
| 73 | className="presto-player__overlay--start-time" |
| 74 | value={draftStartTime} |
| 75 | onChange={(startTime) => setDraftStartTime(startTime)} |
| 76 | onBlur={updateStartTime} |
| 77 | onFocus={updateStartTime} |
| 78 | autoComplete="off" |
| 79 | placeholder="0:00" |
| 80 | /> |
| 81 | </FlexItem> |
| 82 | |
| 83 | <FlexItem> |
| 84 | <TextControl |
| 85 | label={__("End Time", "presto-player")} |
| 86 | className="presto-player__overlay--end-time" |
| 87 | value={draftEndTime} |
| 88 | onChange={setDraftEndTime} |
| 89 | onBlur={updateEndTime} |
| 90 | autoComplete="off" |
| 91 | placeholder="0:00" |
| 92 | /> |
| 93 | </FlexItem> |
| 94 | </Flex> |
| 95 | |
| 96 | <DynamicText |
| 97 | text={text} |
| 98 | update={update} |
| 99 | onFocus={() => { |
| 100 | updateCurrentTime(sanitizeTime(draftStartTime)); |
| 101 | }} |
| 102 | /> |
| 103 | |
| 104 | <BaseControl style={{ width: "100%" }}> |
| 105 | <BaseControl.VisualLabel> |
| 106 | <p> {__("Link", "presto-player")}</p> |
| 107 | </BaseControl.VisualLabel> |
| 108 | <UrlSelect |
| 109 | onFocus={() => { |
| 110 | updateCurrentTime(sanitizeTime(draftStartTime)); |
| 111 | }} |
| 112 | setSettings={(link) => update({ link })} |
| 113 | settings={link || {}} |
| 114 | /> |
| 115 | </BaseControl> |
| 116 | |
| 117 | <BaseControl className={className}> |
| 118 | <RadioControl |
| 119 | label={__("Position", "presto-player")} |
| 120 | options={[ |
| 121 | { label: __("Top Right", "presto-player"), value: "top-right" }, |
| 122 | { label: __("Top Left", "presto-player"), value: "top-left" }, |
| 123 | ]} |
| 124 | selected={draftPosition || "right"} |
| 125 | onFocus={() => { |
| 126 | updateCurrentTime(sanitizeTime(draftStartTime)); |
| 127 | }} |
| 128 | onChange={(position) => { |
| 129 | update({ position }); |
| 130 | setDraftPosition(position); |
| 131 | updateCurrentTime(sanitizeTime(draftStartTime)); |
| 132 | }} |
| 133 | /> |
| 134 | </BaseControl> |
| 135 | |
| 136 | <BaseControl className="presto-player__control--overlay-text-color"> |
| 137 | <Flex> |
| 138 | <BaseControl.VisualLabel> |
| 139 | {__("Text Color", "presto-player")} |
| 140 | </BaseControl.VisualLabel> |
| 141 | <ColorPopup |
| 142 | onFocus={() => { |
| 143 | updateCurrentTime(sanitizeTime(draftStartTime)); |
| 144 | }} |
| 145 | color={color} |
| 146 | setColor={(value) => { |
| 147 | update({ |
| 148 | color: value && value.hex, |
| 149 | }); |
| 150 | }} |
| 151 | /> |
| 152 | </Flex> |
| 153 | </BaseControl> |
| 154 | |
| 155 | <BaseControl className="presto-player__control--overlay-background-color"> |
| 156 | <Flex> |
| 157 | <BaseControl.VisualLabel> |
| 158 | {__("Background Color", "presto-player")} |
| 159 | </BaseControl.VisualLabel> |
| 160 | <ColorPopup |
| 161 | onFocus={() => { |
| 162 | updateCurrentTime(sanitizeTime(draftStartTime)); |
| 163 | }} |
| 164 | color={backgroundColor} |
| 165 | setColor={(value) => { |
| 166 | update({ |
| 167 | backgroundColor: value && value.hex, |
| 168 | }); |
| 169 | }} |
| 170 | /> |
| 171 | </Flex> |
| 172 | </BaseControl> |
| 173 | |
| 174 | <BaseControl> |
| 175 | <RangeControl |
| 176 | label={__("Opacity", "presto-player")} |
| 177 | help={__("Opacity percentage of the overlay.", "presto-player")} |
| 178 | value={opacity} |
| 179 | onChange={(opacity) => update({ opacity })} |
| 180 | min={0} |
| 181 | max={100} |
| 182 | /> |
| 183 | </BaseControl> |
| 184 | |
| 185 | {remove && ( |
| 186 | <BaseControl className={className}> |
| 187 | <Flex justify="flex-end"> |
| 188 | <Button isDestructive isSmall onClick={remove}> |
| 189 | {__("Remove Overlay", "presto-player")} |
| 190 | </Button> |
| 191 | </Flex> |
| 192 | </BaseControl> |
| 193 | )} |
| 194 | |
| 195 | <hr /> |
| 196 | </div> |
| 197 | ); |
| 198 | }; |
| 199 | |
| 200 | export default Overlay; |
| 201 |