edit.js
211 lines
| 1 | import { __ } from '@wordpress/i18n'; |
| 2 | import { |
| 3 | useBlockProps, |
| 4 | InspectorControls, |
| 5 | MediaUpload, |
| 6 | MediaUploadCheck, |
| 7 | } from '@wordpress/block-editor'; |
| 8 | import { |
| 9 | PanelBody, |
| 10 | ToggleControl, |
| 11 | SelectControl, |
| 12 | Button, |
| 13 | __experimentalHeading as Heading, |
| 14 | } from '@wordpress/components'; |
| 15 | import ServerSideRender from '@wordpress/server-side-render'; |
| 16 | import Styler from '../../styler'; |
| 17 | import Wrapper from '../../wrapper'; |
| 18 | |
| 19 | const Inspector = ( { attributes, setAttributes, clientId } ) => { |
| 20 | const { show_label, overlay, icon, icon_pos } = attributes; |
| 21 | |
| 22 | const onMediaSelect = ( media ) => { |
| 23 | const { id, url, type } = media; |
| 24 | |
| 25 | const icon = { value: id, url, type }; |
| 26 | |
| 27 | setAttributes( { icon: icon } ); |
| 28 | }; |
| 29 | |
| 30 | const onRemoveImage = () => { |
| 31 | setAttributes( { icon: { value: '', url: '', type: '' } } ); |
| 32 | }; |
| 33 | |
| 34 | return ( |
| 35 | <InspectorControls> |
| 36 | <PanelBody |
| 37 | title={ __( 'General', 'shop-press' ) } |
| 38 | initialOpen={ true } |
| 39 | > |
| 40 | <ToggleControl |
| 41 | label={ __( 'Show Label', 'shop-press' ) } |
| 42 | checked={ show_label } |
| 43 | onChange={ () => |
| 44 | setAttributes( { |
| 45 | show_label: ! show_label, |
| 46 | } ) |
| 47 | } |
| 48 | /> |
| 49 | |
| 50 | <ToggleControl |
| 51 | label={ __( 'Floating Position', 'shop-press' ) } |
| 52 | checked={ overlay } |
| 53 | onChange={ () => |
| 54 | setAttributes( { |
| 55 | overlay: ! overlay, |
| 56 | } ) |
| 57 | } |
| 58 | /> |
| 59 | |
| 60 | <SelectControl |
| 61 | label={ __( 'Icon Position', 'shop-press' ) } |
| 62 | value={ icon_pos } |
| 63 | options={ [ |
| 64 | { label: 'Left', value: 'left' }, |
| 65 | { label: 'Right', value: 'right' }, |
| 66 | ] } |
| 67 | onChange={ ( value ) => |
| 68 | setAttributes( { icon_pos: value } ) |
| 69 | } |
| 70 | __nextHasNoMarginBottom |
| 71 | /> |
| 72 | |
| 73 | <MediaUploadCheck> |
| 74 | <MediaUpload |
| 75 | onSelect={ ( media ) => onMediaSelect( media ) } |
| 76 | allowedTypes={ [ 'image' ] } |
| 77 | value={ icon?.value } |
| 78 | render={ ( { open } ) => ( |
| 79 | <> |
| 80 | <Heading> |
| 81 | { __( 'Icon', 'shop-press' ) } |
| 82 | </Heading> |
| 83 | <div style={ { display: 'flex', gap: '10px' } }> |
| 84 | <Button variant="primary" onClick={ open }> |
| 85 | { __( 'Upload', 'shop-press' ) } |
| 86 | </Button> |
| 87 | |
| 88 | { icon?.value && ( |
| 89 | <Button |
| 90 | variant="secondary" |
| 91 | onClick={ onRemoveImage } |
| 92 | > |
| 93 | { __( 'Remove', 'shop-press' ) } |
| 94 | </Button> |
| 95 | ) } |
| 96 | </div> |
| 97 | </> |
| 98 | ) } |
| 99 | /> |
| 100 | |
| 101 | { icon && ( |
| 102 | <div style={ { marginTop: '15px' } }> |
| 103 | <img width={ 70 } src={ icon?.url } /> |
| 104 | </div> |
| 105 | ) } |
| 106 | </MediaUploadCheck> |
| 107 | </PanelBody> |
| 108 | <PanelBody |
| 109 | title={ __( 'Styles', 'shop-press' ) } |
| 110 | initialOpen={ false } |
| 111 | > |
| 112 | <Styler |
| 113 | clientId={ clientId } |
| 114 | label={ __( 'Wrapper', 'shop-press' ) } |
| 115 | selector=".sp-product-compare" |
| 116 | wrapper={ `.${ attributes[ 'wrapperID' ] }` } |
| 117 | attributes={ attributes } |
| 118 | setAttributes={ setAttributes } |
| 119 | /> |
| 120 | |
| 121 | <Styler |
| 122 | clientId={ clientId } |
| 123 | label={ __( 'Button', 'shop-press' ) } |
| 124 | selector=".sp-product-compare .sp-compare-button-loop" |
| 125 | wrapper={ `.${ attributes[ 'wrapperID' ] }` } |
| 126 | attributes={ attributes } |
| 127 | setAttributes={ setAttributes } |
| 128 | /> |
| 129 | |
| 130 | <Styler |
| 131 | clientId={ clientId } |
| 132 | label={ __( 'Icon Wrapper', 'shop-press' ) } |
| 133 | selector=".sp-product-compare .sp-compare-icon" |
| 134 | wrapper={ `.${ attributes[ 'wrapperID' ] }` } |
| 135 | attributes={ attributes } |
| 136 | setAttributes={ setAttributes } |
| 137 | /> |
| 138 | |
| 139 | <Styler |
| 140 | clientId={ clientId } |
| 141 | label={ __( 'Icon', 'shop-press' ) } |
| 142 | selector=".sp-product-compare .sp-compare-icon i.sp-icon" |
| 143 | wrapper={ `.${ attributes[ 'wrapperID' ] }` } |
| 144 | attributes={ attributes } |
| 145 | setAttributes={ setAttributes } |
| 146 | /> |
| 147 | |
| 148 | <Styler |
| 149 | clientId={ clientId } |
| 150 | label={ __( 'Active Icon', 'shop-press' ) } |
| 151 | selector=".sp-product-compare[data-status='yes'] .sp-compare-icon i.sp-icon" |
| 152 | wrapper={ `.${ attributes[ 'wrapperID' ] }` } |
| 153 | attributes={ attributes } |
| 154 | setAttributes={ setAttributes } |
| 155 | /> |
| 156 | |
| 157 | <Styler |
| 158 | clientId={ clientId } |
| 159 | label={ __( 'Label', 'shop-press' ) } |
| 160 | selector=".sp-product-compare .sp-compare-label" |
| 161 | wrapper={ `.${ attributes[ 'wrapperID' ] }` } |
| 162 | attributes={ attributes } |
| 163 | setAttributes={ setAttributes } |
| 164 | /> |
| 165 | |
| 166 | <Styler |
| 167 | clientId={ clientId } |
| 168 | label={ __( 'Tooltip', 'shop-press' ) } |
| 169 | selector=".sp-product-compare.overlay .sp-compare-label" |
| 170 | wrapper={ `.${ attributes[ 'wrapperID' ] }` } |
| 171 | attributes={ attributes } |
| 172 | setAttributes={ setAttributes } |
| 173 | /> |
| 174 | |
| 175 | <Styler |
| 176 | clientId={ clientId } |
| 177 | label={ __( 'Tooltip Arrow', 'shop-press' ) } |
| 178 | selector=".sp-product-compare.overlay span:before" |
| 179 | wrapper={ `.${ attributes[ 'wrapperID' ] }` } |
| 180 | attributes={ attributes } |
| 181 | setAttributes={ setAttributes } |
| 182 | /> |
| 183 | </PanelBody> |
| 184 | </InspectorControls> |
| 185 | ); |
| 186 | }; |
| 187 | |
| 188 | const Edit = ( props ) => { |
| 189 | const { attributes, setAttributes, clientId } = props; |
| 190 | const blockProps = useBlockProps(); |
| 191 | |
| 192 | return ( |
| 193 | <div { ...blockProps }> |
| 194 | <Inspector |
| 195 | attributes={ attributes } |
| 196 | setAttributes={ setAttributes } |
| 197 | clientId={ clientId } |
| 198 | /> |
| 199 | |
| 200 | <Wrapper { ...props }> |
| 201 | <ServerSideRender |
| 202 | block="shop-press/loop-compare" |
| 203 | attributes={ attributes } |
| 204 | /> |
| 205 | </Wrapper> |
| 206 | </div> |
| 207 | ); |
| 208 | }; |
| 209 | |
| 210 | export default Edit; |
| 211 |