CustomizeControls
2 months ago
Steps
2 weeks ago
App.js
2 years ago
CategoryButtons.js
2 weeks ago
CustomTooltip.js
2 years ago
EditorSelector.js
2 years ago
FeaturesList.js
1 year ago
Filters.js
2 weeks ago
Header.js
2 weeks ago
ImportError.js
2 years ago
ImportForm.js
1 year ago
ImportMock.js
2 years ago
ImportProgress.js
1 year ago
Onboarding.js
2 weeks ago
ProgressBar.js
2 years ago
Search.js
2 weeks ago
SitePreview.js
2 years ago
SiteSettings.js
2 months ago
Sites.js
2 weeks ago
StarterSiteCard.js
2 weeks ago
Toast.js
2 weeks ago
WelcomeMock.js
2 years ago
EditorSelector.js
119 lines
| 1 | /* global tiobDash */ |
| 2 | import { __ } from '@wordpress/i18n'; |
| 3 | import { useState, useEffect, useRef } from '@wordpress/element'; |
| 4 | import { Button, Dashicon } from '@wordpress/components'; |
| 5 | import { withDispatch, withSelect } from '@wordpress/data'; |
| 6 | import { compose } from '@wordpress/compose'; |
| 7 | import classnames from 'classnames'; |
| 8 | import { EDITOR_MAP } from '../utils/common'; |
| 9 | |
| 10 | const EditorSelector = ( { editor, setCurrentEditor, sites } ) => { |
| 11 | const [ open, setOpen ] = useState( false ); |
| 12 | const editorsOrderedFromAPI = Object.keys( sites ); |
| 13 | const dropdownRef = useRef(); |
| 14 | |
| 15 | const toggleDropdown = () => setOpen( ! open ); |
| 16 | |
| 17 | const handleOutsideClick = ( e ) => { |
| 18 | if ( |
| 19 | dropdownRef.current && |
| 20 | ! dropdownRef.current.contains( e.target ) |
| 21 | ) { |
| 22 | setOpen( false ); |
| 23 | } |
| 24 | }; |
| 25 | |
| 26 | useEffect( () => { |
| 27 | const win = document.defaultView; |
| 28 | win.addEventListener( 'click', handleOutsideClick ); |
| 29 | |
| 30 | return () => { |
| 31 | win.removeEventListener( 'click', handleOutsideClick ); |
| 32 | }; |
| 33 | }, [] ); |
| 34 | |
| 35 | const toggleButtonClasses = classnames( |
| 36 | 'select', |
| 37 | 'ob-dropdown', |
| 38 | open ? 'active' : '' |
| 39 | ); |
| 40 | |
| 41 | return ( |
| 42 | <div className="ob-editor-selector" ref={ dropdownRef }> |
| 43 | <Button |
| 44 | onClick={ toggleDropdown } |
| 45 | className={ toggleButtonClasses } |
| 46 | > |
| 47 | <span className="ob-current-editor"> |
| 48 | <img |
| 49 | className="editor-icon" |
| 50 | src={ |
| 51 | tiobDash.assets + 'img/' + EDITOR_MAP[ editor ].icon |
| 52 | } |
| 53 | alt={ __( |
| 54 | 'Builder Logo', |
| 55 | 'templates-patterns-collection' |
| 56 | ) } |
| 57 | /> |
| 58 | <Dashicon |
| 59 | size={ 14 } |
| 60 | icon={ open ? 'arrow-up-alt2' : 'arrow-down-alt2' } |
| 61 | /> |
| 62 | </span> |
| 63 | { open && ( |
| 64 | <span className="ob-editor-options"> |
| 65 | <ul className="options"> |
| 66 | { editorsOrderedFromAPI.map( ( key, index ) => { |
| 67 | if ( key === editor ) { |
| 68 | return null; |
| 69 | } |
| 70 | return ( |
| 71 | <li key={ index }> |
| 72 | <button |
| 73 | className="ob-editor-option" |
| 74 | onClick={ ( e ) => { |
| 75 | e.preventDefault(); |
| 76 | setCurrentEditor( key ); |
| 77 | setOpen( false ); |
| 78 | } } |
| 79 | > |
| 80 | <img |
| 81 | className="editor-icon" |
| 82 | src={ |
| 83 | tiobDash.assets + |
| 84 | 'img/' + |
| 85 | EDITOR_MAP[ key ].icon |
| 86 | } |
| 87 | alt={ __( |
| 88 | 'Builder Logo', |
| 89 | 'templates-patterns-collection' |
| 90 | ) } |
| 91 | /> |
| 92 | </button> |
| 93 | </li> |
| 94 | ); |
| 95 | } ) } |
| 96 | </ul> |
| 97 | </span> |
| 98 | ) } |
| 99 | </Button> |
| 100 | </div> |
| 101 | ); |
| 102 | }; |
| 103 | |
| 104 | export default compose( |
| 105 | withSelect( ( select ) => { |
| 106 | const { getCurrentEditor, getSites } = select( 'ti-onboarding' ); |
| 107 | return { |
| 108 | editor: getCurrentEditor(), |
| 109 | sites: getSites().sites, |
| 110 | }; |
| 111 | } ), |
| 112 | withDispatch( ( dispatch ) => { |
| 113 | const { setCurrentEditor } = dispatch( 'ti-onboarding' ); |
| 114 | return { |
| 115 | setCurrentEditor: ( editor ) => setCurrentEditor( editor ), |
| 116 | }; |
| 117 | } ) |
| 118 | )( EditorSelector ); |
| 119 |