| 1 |
import classnames from 'classnames/dedupe'; |
| 2 |
|
| 3 |
import { |
| 4 |
__experimentalUnitControl as ExperimentalUnitControl, |
| 5 |
Button, |
| 6 |
PanelBody, |
| 7 |
SelectControl, |
| 8 |
TextControl, |
| 9 |
ToggleControl, |
| 10 |
UnitControl as StableUnitControl, |
| 11 |
} from '@wordpress/components'; |
| 12 |
import { useState } from '@wordpress/element'; |
| 13 |
import { applyFilters } from '@wordpress/hooks'; |
| 14 |
import { __ } from '@wordpress/i18n'; |
| 15 |
|
| 16 |
import RangeControl from '../../components/range-control'; |
| 17 |
import PreviewLottie from './preview-lottie'; |
| 18 |
|
| 19 |
const UnitControl = StableUnitControl || ExperimentalUnitControl; |
| 20 |
|
| 21 |
import { |
| 22 |
InspectorControls, |
| 23 |
MediaPlaceholder, |
| 24 |
useBlockProps, |
| 25 |
} from '@wordpress/block-editor'; |
| 26 |
|
| 27 |
/** |
| 28 |
* Block Edit Class. |
| 29 |
* |
| 30 |
* @param {Object} props - component props. |
| 31 |
* |
| 32 |
* @return {JSX} component. |
| 33 |
*/ |
| 34 |
export default function BlockEdit(props) { |
| 35 |
const { attributes, setAttributes, isSelected } = props; |
| 36 |
|
| 37 |
let { className = '' } = props; |
| 38 |
|
| 39 |
const { |
| 40 |
fileUrl, |
| 41 |
fileWidth, |
| 42 |
fileHeight, |
| 43 |
trigger, |
| 44 |
loop, |
| 45 |
direction, |
| 46 |
speed, |
| 47 |
width, |
| 48 |
} = attributes; |
| 49 |
|
| 50 |
const [isWidthPercent, setIsWidthPercent] = useState(width.endsWith('%')); |
| 51 |
|
| 52 |
className = classnames('ghostkit-lottie', className); |
| 53 |
|
| 54 |
className = applyFilters('ghostkit.editor.className', className, props); |
| 55 |
|
| 56 |
const blockProps = useBlockProps({ |
| 57 |
className, |
| 58 |
'data-trigger': trigger, |
| 59 |
style: |
| 60 |
fileWidth && fileHeight |
| 61 |
? { |
| 62 |
'--gkt-lottie__ar': `${fileWidth} / ${fileHeight}`, |
| 63 |
'--gkt-lottie__width': width, |
| 64 |
} |
| 65 |
: {}, |
| 66 |
}); |
| 67 |
|
| 68 |
return ( |
| 69 |
<> |
| 70 |
{fileUrl ? ( |
| 71 |
<InspectorControls> |
| 72 |
<PanelBody> |
| 73 |
<SelectControl |
| 74 |
label={__('Play Animation On', 'ghostkit')} |
| 75 |
value={trigger} |
| 76 |
options={[ |
| 77 |
{ |
| 78 |
label: __('Page Load', 'ghostkit'), |
| 79 |
value: '', |
| 80 |
}, |
| 81 |
{ |
| 82 |
label: __('Viewport', 'ghostkit'), |
| 83 |
value: 'viewport', |
| 84 |
}, |
| 85 |
{ |
| 86 |
label: __('Hover', 'ghostkit'), |
| 87 |
value: 'hover', |
| 88 |
}, |
| 89 |
{ |
| 90 |
label: __('Click', 'ghostkit'), |
| 91 |
value: 'click', |
| 92 |
}, |
| 93 |
{ |
| 94 |
label: __('Scroll', 'ghostkit'), |
| 95 |
value: 'scroll', |
| 96 |
}, |
| 97 |
]} |
| 98 |
onChange={(value) => |
| 99 |
setAttributes({ trigger: value }) |
| 100 |
} |
| 101 |
/> |
| 102 |
{trigger !== 'scroll' ? ( |
| 103 |
<> |
| 104 |
<RangeControl |
| 105 |
label={__('Speed', 'otter-blocks')} |
| 106 |
value={speed} |
| 107 |
onChange={(val) => |
| 108 |
setAttributes({ speed: val }) |
| 109 |
} |
| 110 |
step={0.1} |
| 111 |
min={0} |
| 112 |
max={10} |
| 113 |
/> |
| 114 |
<ToggleControl |
| 115 |
label={__('Loop', 'ghostkit')} |
| 116 |
checked={!!loop} |
| 117 |
onChange={() => |
| 118 |
setAttributes({ loop: !loop }) |
| 119 |
} |
| 120 |
/> |
| 121 |
</> |
| 122 |
) : null} |
| 123 |
<ToggleControl |
| 124 |
label={__('Reverse', 'ghostkit')} |
| 125 |
checked={direction === -1} |
| 126 |
onChange={() => { |
| 127 |
setAttributes({ |
| 128 |
direction: direction === 1 ? -1 : 1, |
| 129 |
}); |
| 130 |
}} |
| 131 |
/> |
| 132 |
<UnitControl |
| 133 |
label={__('Width', 'ghostkit')} |
| 134 |
value={width} |
| 135 |
onChange={(val) => setAttributes({ width: val })} |
| 136 |
onUnitChange={(unit) => |
| 137 |
setIsWidthPercent(unit === '%') |
| 138 |
} |
| 139 |
labelPosition="edge" |
| 140 |
__unstableInputWidth="70px" |
| 141 |
units={[ |
| 142 |
{ value: 'px', label: 'px' }, |
| 143 |
{ value: '%', label: '%' }, |
| 144 |
]} |
| 145 |
min={0} |
| 146 |
max={isWidthPercent ? 100 : Infinity} |
| 147 |
/> |
| 148 |
</PanelBody> |
| 149 |
<PanelBody> |
| 150 |
<TextControl |
| 151 |
label={__('Lottie File', 'ghostkit')} |
| 152 |
value={fileUrl} |
| 153 |
disabled |
| 154 |
/> |
| 155 |
<Button |
| 156 |
isSecondary |
| 157 |
onClick={() => { |
| 158 |
setAttributes({ |
| 159 |
fileId: undefined, |
| 160 |
fileUrl: undefined, |
| 161 |
fileWidth: undefined, |
| 162 |
fileHeight: undefined, |
| 163 |
}); |
| 164 |
}} |
| 165 |
> |
| 166 |
{__('Clear', 'ghostkit')} |
| 167 |
</Button> |
| 168 |
</PanelBody> |
| 169 |
</InspectorControls> |
| 170 |
) : null} |
| 171 |
<div {...blockProps}> |
| 172 |
{fileUrl ? ( |
| 173 |
<PreviewLottie |
| 174 |
url={fileUrl} |
| 175 |
trigger={trigger} |
| 176 |
speed={speed} |
| 177 |
loop={loop} |
| 178 |
direction={direction} |
| 179 |
isSelected={isSelected} |
| 180 |
onLoad={(e) => { |
| 181 |
const newWidth = |
| 182 |
e?.target?._lottie?.animationData?.w; |
| 183 |
const newHeight = |
| 184 |
e?.target?._lottie?.animationData?.h; |
| 185 |
|
| 186 |
if ( |
| 187 |
newWidth && |
| 188 |
newHeight && |
| 189 |
(newWidth !== fileWidth || |
| 190 |
newHeight !== fileHeight) |
| 191 |
) { |
| 192 |
setAttributes({ |
| 193 |
fileWidth: newWidth, |
| 194 |
fileHeight: newHeight, |
| 195 |
}); |
| 196 |
} |
| 197 |
}} |
| 198 |
/> |
| 199 |
) : ( |
| 200 |
<MediaPlaceholder |
| 201 |
icon="format-image" |
| 202 |
labels={{ |
| 203 |
title: __('Lottie JSON', 'ghostkit'), |
| 204 |
name: __('lottie', 'ghostkit'), |
| 205 |
instructions: __( |
| 206 |
'Upload a JSON file or pick one from your media library.' |
| 207 |
), |
| 208 |
}} |
| 209 |
onSelect={(file) => { |
| 210 |
setAttributes({ |
| 211 |
fileId: file.id, |
| 212 |
fileUrl: file.url, |
| 213 |
}); |
| 214 |
}} |
| 215 |
onSelectURL={(url) => { |
| 216 |
setAttributes({ fileId: undefined, fileUrl: url }); |
| 217 |
}} |
| 218 |
accept={['application/json']} |
| 219 |
allowedTypes={['application/json']} |
| 220 |
disableMaxUploadErrorMessages |
| 221 |
onError={() => { |
| 222 |
setAttributes({ |
| 223 |
fileId: undefined, |
| 224 |
fileUrl: undefined, |
| 225 |
}); |
| 226 |
}} |
| 227 |
/> |
| 228 |
)} |
| 229 |
</div> |
| 230 |
</> |
| 231 |
); |
| 232 |
} |
| 233 |
|