parts
1 month ago
ActionBar.js
2 months ago
ActionBar.scss
2 months ago
Behavior.js
2 years ago
CTA.js
2 months ago
CTA.scss
2 months ago
Controls.js
5 years ago
Edit.js
5 days ago
Edit.scss
2 months ago
Email.js
2 months ago
Email.scss
2 months ago
Preset.js
4 years ago
Search.js
3 years ago
Style.js
2 months ago
Style.scss
2 months ago
Watermark.js
4 years ago
index.js
2 months ago
index.scss
2 months ago
Edit.js
408 lines
| 1 | /** |
| 2 | * WordPress dependencies |
| 3 | */ |
| 4 | const { __ } = wp.i18n; |
| 5 | const { |
| 6 | TextControl, |
| 7 | BaseControl, |
| 8 | Icon, |
| 9 | Notice, |
| 10 | Button, |
| 11 | Flex, |
| 12 | FlexItem, |
| 13 | SelectControl, |
| 14 | FlexBlock, |
| 15 | Modal, |
| 16 | } = wp.components; |
| 17 | const { useState, useEffect } = wp.element; |
| 18 | const { useSelect, dispatch } = wp.data; |
| 19 | |
| 20 | // eslint-disable-next-line import/no-unresolved |
| 21 | import { snackbarNotice } from '@/admin/blocks/util'; |
| 22 | // eslint-disable-next-line import/no-unresolved |
| 23 | import Menu from '@/admin/ui/Menu'; |
| 24 | import './Edit.scss'; |
| 25 | import Preview from '../Preview'; |
| 26 | import ActionBar from './ActionBar'; |
| 27 | import Behavior from './Behavior'; |
| 28 | import Controls from './Controls'; |
| 29 | import Watermark from './Watermark'; |
| 30 | import Search from './Search'; |
| 31 | import CTA from './CTA'; |
| 32 | import Email from './Email'; |
| 33 | import Style from './Style'; |
| 34 | |
| 35 | function EditPlayerPreset( { |
| 36 | type = 'new', |
| 37 | closeModal, |
| 38 | addPreset, |
| 39 | onSave, |
| 40 | updatePreset, |
| 41 | name = '', |
| 42 | preset = { |
| 43 | 'play-large': true, |
| 44 | rewind: true, |
| 45 | play: true, |
| 46 | 'fast-forward': true, |
| 47 | progress: true, |
| 48 | 'current-time': true, |
| 49 | mute: true, |
| 50 | volume: true, |
| 51 | speed: false, |
| 52 | pip: false, |
| 53 | fullscreen: true, |
| 54 | captions: false, |
| 55 | // behavior |
| 56 | save_player_position: false, |
| 57 | reset_on_end: false, |
| 58 | auto_hide: true, |
| 59 | show_time_elapsed: false, |
| 60 | sticky_scroll: false, |
| 61 | // style |
| 62 | hide_logo: false, |
| 63 | border_radius: 0, |
| 64 | skin: 'modern', |
| 65 | caption_style: 'default', |
| 66 | caption_background: '#000000', |
| 67 | // youtube |
| 68 | hide_youtube: false, |
| 69 | lazy_load_youtube: false, |
| 70 | |
| 71 | // features |
| 72 | cta: {}, |
| 73 | email_collection: {}, |
| 74 | action_bar: {}, |
| 75 | |
| 76 | // search |
| 77 | enabled: false, |
| 78 | minMatchCharLength: 1, |
| 79 | threshold: 0.3, |
| 80 | placeholder: 'search', |
| 81 | }, |
| 82 | } ) { |
| 83 | const [ loading, setLoading ] = useState( false ); |
| 84 | const [ error, setError ] = useState( '' ); |
| 85 | const [ menu, setMenu ] = useState( '' ); |
| 86 | const [ thisName, setThisName ] = useState( name ); |
| 87 | const [ state, setState ] = useState( preset ); |
| 88 | const branding = useSelect( ( select ) => { |
| 89 | return select( 'presto-player/player' ).branding(); |
| 90 | } ); |
| 91 | |
| 92 | const [ value, setValue ] = useState( '' ); |
| 93 | |
| 94 | const genericError = { |
| 95 | message: __( |
| 96 | 'The preset could not be saved. Please reload the page and try again.', |
| 97 | 'presto-player' |
| 98 | ), |
| 99 | }; |
| 100 | |
| 101 | //you tube feature |
| 102 | const youtube = useSelect( ( select ) => { |
| 103 | return select( 'presto-player/player' ).youtube(); |
| 104 | } ); |
| 105 | |
| 106 | useEffect( () => { |
| 107 | setValue( youtube.channel_id ); |
| 108 | }, [ youtube?.channel_id ] ); |
| 109 | |
| 110 | // update state |
| 111 | const updateState = ( updated = {} ) => { |
| 112 | setState( { ...state, ...updated } ); |
| 113 | }; |
| 114 | |
| 115 | const putPreset = async () => { |
| 116 | setLoading( true ); |
| 117 | try { |
| 118 | const data = { |
| 119 | ...state, |
| 120 | ...{ name: thisName }, |
| 121 | }; |
| 122 | const saved = await wp.apiFetch( { |
| 123 | method: 'POST', |
| 124 | url: wp.url.addQueryArgs( |
| 125 | `${ window.prestoPlayer.root }${ window.prestoPlayer.prestoVersionString }preset/${ preset.id }`, |
| 126 | { _method: 'PUT' } |
| 127 | ), |
| 128 | data, |
| 129 | } ); |
| 130 | |
| 131 | if ( ! saved ) { |
| 132 | throw genericError; |
| 133 | } |
| 134 | |
| 135 | // update or create here |
| 136 | updatePreset( saved ); |
| 137 | closeModal(); |
| 138 | if ( onSave ) { |
| 139 | onSave( saved ); |
| 140 | } |
| 141 | snackbarNotice( { message: __( 'Preset updated!', 'presto-player' ) } ); |
| 142 | } catch ( e ) { |
| 143 | setError( e?.message ? e.message : genericError ); |
| 144 | } finally { |
| 145 | setLoading( false ); |
| 146 | } |
| 147 | |
| 148 | // youtube id save |
| 149 | dispatch( 'presto-player/player' ).updateYoutube( { channel_id: value } ); |
| 150 | |
| 151 | const data = { |
| 152 | ...youtube, |
| 153 | ...{ channel_id: value }, |
| 154 | }; |
| 155 | |
| 156 | try { |
| 157 | const response = await wp.apiFetch( { |
| 158 | path: 'wp/v2/settings', |
| 159 | method: 'POST', |
| 160 | data: { |
| 161 | presto_player_youtube: data, |
| 162 | }, |
| 163 | } ); |
| 164 | if ( response?.presto_player_youtube ) { |
| 165 | dispatch( 'presto-player/player' ).setYoutube( |
| 166 | response?.presto_player_youtube |
| 167 | ); |
| 168 | } |
| 169 | } catch ( e ) { |
| 170 | // eslint-disable-next-line no-console |
| 171 | console.log( e ); |
| 172 | } |
| 173 | }; |
| 174 | |
| 175 | const createPreset = async () => { |
| 176 | setLoading( true ); |
| 177 | try { |
| 178 | const saved = await wp.apiFetch( { |
| 179 | method: 'POST', |
| 180 | url: |
| 181 | window.prestoPlayer.root + |
| 182 | window.prestoPlayer.prestoVersionString + |
| 183 | 'preset', |
| 184 | data: { |
| 185 | ...{ name: thisName }, |
| 186 | ...state, |
| 187 | }, |
| 188 | } ); |
| 189 | if ( ! saved ) { |
| 190 | throw genericError; |
| 191 | } |
| 192 | |
| 193 | // update or create here |
| 194 | addPreset( saved ); |
| 195 | closeModal(); |
| 196 | if ( onSave ) { |
| 197 | onSave( saved ); |
| 198 | } |
| 199 | snackbarNotice( { message: __( 'Preset created!', 'presto-player' ) } ); |
| 200 | } catch ( e ) { |
| 201 | setError( e?.message ? e.message : genericError ); |
| 202 | } finally { |
| 203 | setLoading( false ); |
| 204 | } |
| 205 | }; |
| 206 | |
| 207 | // validate and save |
| 208 | const save = () => { |
| 209 | if ( ! thisName ) { |
| 210 | setError( |
| 211 | __( 'You must enter a name for the preset.', 'presto-player' ) |
| 212 | ); |
| 213 | return; |
| 214 | } |
| 215 | return type === 'edit' ? putPreset() : createPreset(); |
| 216 | }; |
| 217 | |
| 218 | const tabs = [ |
| 219 | { |
| 220 | name: 'controls', |
| 221 | title: __( 'Controls', 'presto-player' ), |
| 222 | icon: <Icon icon="admin-settings" />, |
| 223 | component: <Controls updateState={ updateState } state={ state } />, |
| 224 | }, |
| 225 | { |
| 226 | name: 'behavior', |
| 227 | title: __( 'Behavior', 'presto-player' ), |
| 228 | icon: <Icon icon="admin-generic" />, |
| 229 | component: <Behavior updateState={ updateState } state={ state } />, |
| 230 | }, |
| 231 | { |
| 232 | name: 'style', |
| 233 | title: __( 'Style', 'presto-player' ), |
| 234 | icon: <Icon icon="admin-customizer" />, |
| 235 | component: <Style updateState={ updateState } state={ state } />, |
| 236 | }, |
| 237 | { |
| 238 | name: 'email', |
| 239 | title: __( 'Email Capture', 'presto-player' ), |
| 240 | icon: <Icon icon="email" />, |
| 241 | component: <Email updateState={ updateState } state={ state } />, |
| 242 | }, |
| 243 | { |
| 244 | name: 'cta', |
| 245 | title: __( 'Call To Action', 'presto-player' ), |
| 246 | icon: <Icon icon="megaphone" />, |
| 247 | component: <CTA updateState={ updateState } state={ state } />, |
| 248 | }, |
| 249 | { |
| 250 | name: 'action_bar', |
| 251 | title: __( 'Action Bar', 'presto-player' ), |
| 252 | icon: <Icon icon="cover-image" />, |
| 253 | component: ( |
| 254 | <ActionBar |
| 255 | updateState={ updateState } |
| 256 | state={ state } |
| 257 | value={ value } |
| 258 | setValue={ setValue } |
| 259 | /> |
| 260 | ), |
| 261 | }, |
| 262 | { |
| 263 | name: 'watermark', |
| 264 | title: __( 'Dynamic Watermark', 'presto-player' ), |
| 265 | icon: <Icon icon="lock" />, |
| 266 | component: <Watermark updateState={ updateState } state={ state } />, |
| 267 | }, |
| 268 | { |
| 269 | name: 'search', |
| 270 | title: __( 'Searchable Captions', 'presto-player' ), |
| 271 | icon: <Icon icon="search" />, |
| 272 | component: <Search updateState={ updateState } state={ state } />, |
| 273 | }, |
| 274 | // { |
| 275 | // name: "cta", |
| 276 | // title: __("Call to Action", "presto-player"), |
| 277 | // icon: <Icon icon="megaphone" />, |
| 278 | // }, |
| 279 | ]; |
| 280 | |
| 281 | return ( |
| 282 | <Modal |
| 283 | title={ |
| 284 | type === 'edit' |
| 285 | ? __( 'Edit A Video Preset', 'presto-player' ) |
| 286 | : __( 'Create A New Video Preset', 'presto-player' ) |
| 287 | } |
| 288 | onRequestClose={ closeModal } |
| 289 | className="presto-player__modal-presets" |
| 290 | overlayClassName="presto-player__modal-presets-overlay" |
| 291 | > |
| 292 | <div className="presto-player__preset-options" data-cy="preset-modal"> |
| 293 | <TextControl |
| 294 | value={ thisName } |
| 295 | hideLabelFromVision={ true } |
| 296 | label={ __( 'Preset Name', 'presto-player' ) } |
| 297 | onChange={ ( newName ) => setThisName( newName ) } |
| 298 | /* eslint-disable @wordpress/i18n-ellipsis */ |
| 299 | placeholder={ __( 'Enter a preset name...', 'presto-player' ) } |
| 300 | /* eslint-enable @wordpress/i18n-ellipsis */ |
| 301 | className="presto-player__modal--style-name" |
| 302 | /* eslint-disable jsx-a11y/no-autofocus */ |
| 303 | autoFocus |
| 304 | /* eslint-enable jsx-a11y/no-autofocus */ |
| 305 | /> |
| 306 | |
| 307 | <Flex align="stretch" className="presto-player__style-preview-area"> |
| 308 | <FlexItem className="presto-player__style-sidebar"> |
| 309 | <div> |
| 310 | <Menu |
| 311 | items={ tabs } |
| 312 | title={ __( 'Customize', 'presto-player' ) } |
| 313 | onSelect={ setMenu } |
| 314 | > |
| 315 | { ( item ) => item.component } |
| 316 | </Menu> |
| 317 | </div> |
| 318 | </FlexItem> |
| 319 | <FlexBlock className="presto-player__style-preview-panel"> |
| 320 | <div |
| 321 | style={ { |
| 322 | position: 'absolute', |
| 323 | top: 0, |
| 324 | left: 0, |
| 325 | padding: '20px', |
| 326 | } } |
| 327 | > |
| 328 | <SelectControl |
| 329 | label={ __( 'Skin', 'presto-player' ) } |
| 330 | labelPosition="side" |
| 331 | value={ state?.skin } |
| 332 | options={ [ |
| 333 | { label: __( 'Modern', 'presto-player' ), value: 'modern' }, |
| 334 | { |
| 335 | label: __( 'Business', 'presto-player' ), |
| 336 | value: 'business', |
| 337 | }, |
| 338 | { label: __( 'Stacked', 'presto-player' ), value: 'stacked' }, |
| 339 | { label: __( 'Basic', 'presto-player' ), value: 'default' }, |
| 340 | { |
| 341 | label: __( 'Floating Pill', 'presto-player' ), |
| 342 | value: 'floating-pill', |
| 343 | }, |
| 344 | ] } |
| 345 | onChange={ ( skin ) => { |
| 346 | updateState( { skin } ); |
| 347 | } } |
| 348 | /> |
| 349 | </div> |
| 350 | { /* |
| 351 | Disable the video tag so the user clicking on it won't play the |
| 352 | video when the controls are enabled. |
| 353 | */ } |
| 354 | |
| 355 | <Preview |
| 356 | poster="https://source.unsplash.com/daily" |
| 357 | state={ state } |
| 358 | branding={ branding } |
| 359 | menu={ menu } |
| 360 | /> |
| 361 | </FlexBlock> |
| 362 | </Flex> |
| 363 | |
| 364 | <br /> |
| 365 | |
| 366 | { error && ( |
| 367 | <BaseControl> |
| 368 | <Notice |
| 369 | className="presto-player__modal--error-notice" |
| 370 | status="error" |
| 371 | isDismissible={ false } |
| 372 | style={ { margin: 0 } } |
| 373 | > |
| 374 | { error.replace( /(<([^>]+)>)/gi, '' ) } |
| 375 | </Notice> |
| 376 | </BaseControl> |
| 377 | ) } |
| 378 | <div className="presto-presets-edit__footer"> |
| 379 | <div className="presto-presets-edit__meta"> |
| 380 | Preset ID: { preset.id } |
| 381 | </div> |
| 382 | <div> |
| 383 | <Button |
| 384 | isTertiary |
| 385 | onClick={ closeModal } |
| 386 | style={ { margin: '0 6px' } } |
| 387 | > |
| 388 | { __( 'Cancel', 'presto-player' ) } |
| 389 | </Button> |
| 390 | <Button |
| 391 | isPrimary |
| 392 | isBusy={ loading } |
| 393 | disabled={ loading } |
| 394 | onClick={ save } |
| 395 | data-cy="submit-preset" |
| 396 | > |
| 397 | { type === 'edit' |
| 398 | ? __( 'Update Preset', 'presto-player' ) |
| 399 | : __( 'Create Preset', 'presto-player' ) } |
| 400 | </Button> |
| 401 | </div> |
| 402 | </div> |
| 403 | </div> |
| 404 | </Modal> |
| 405 | ); |
| 406 | } |
| 407 | export default EditPlayerPreset; |
| 408 |