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