| 1 |
import { useBlockProps } from '@wordpress/block-editor'; |
| 2 |
import { useEffect } from '@wordpress/element'; |
| 3 |
import { applyFilters } from '@wordpress/hooks'; |
| 4 |
|
| 5 |
import IconPicker from '../../components/icon-picker'; |
| 6 |
import { hasClass, removeClass } from '../../utils/classes-replacer'; |
| 7 |
import BackgroundColor from './edit/background-color'; |
| 8 |
import BlockInspectorControls from './edit/inspector-controls'; |
| 9 |
|
| 10 |
/** |
| 11 |
* Load YouTube / Vimeo poster image |
| 12 |
*/ |
| 13 |
const videoPosterCache = {}; |
| 14 |
let videoPosterTimeout; |
| 15 |
function getVideoPoster(url, cb) { |
| 16 |
if (videoPosterCache[url]) { |
| 17 |
cb(videoPosterCache[url]); |
| 18 |
return; |
| 19 |
} |
| 20 |
|
| 21 |
if (typeof window.VideoWorker === 'undefined') { |
| 22 |
cb(''); |
| 23 |
return; |
| 24 |
} |
| 25 |
|
| 26 |
clearTimeout(videoPosterTimeout); |
| 27 |
videoPosterTimeout = setTimeout(() => { |
| 28 |
const videoObject = new window.VideoWorker(url); |
| 29 |
|
| 30 |
if (videoObject.isValid()) { |
| 31 |
videoObject.getImageURL((videoPosterUrl) => { |
| 32 |
videoPosterCache[url] = videoPosterUrl; |
| 33 |
cb(videoPosterUrl); |
| 34 |
}); |
| 35 |
} else { |
| 36 |
cb(''); |
| 37 |
} |
| 38 |
}, 500); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Block Edit Class. |
| 43 |
* |
| 44 |
* @param props |
| 45 |
*/ |
| 46 |
export default function BlockEdit(props) { |
| 47 |
const { attributes, setAttributes, isSelected, clientId } = props; |
| 48 |
|
| 49 |
const { |
| 50 |
type, |
| 51 |
video, |
| 52 |
videoPosterPreview, |
| 53 |
videoAspectRatio, |
| 54 |
|
| 55 |
iconPlay, |
| 56 |
|
| 57 |
posterId, |
| 58 |
posterUrl, |
| 59 |
posterAlt, |
| 60 |
posterWidth, |
| 61 |
posterHeight, |
| 62 |
|
| 63 |
clickAction, |
| 64 |
|
| 65 |
className, |
| 66 |
} = attributes; |
| 67 |
|
| 68 |
// Mount and update. |
| 69 |
useEffect(() => { |
| 70 |
// load YouTube / Vimeo poster |
| 71 |
if (!posterId && type === 'yt_vm_video' && video) { |
| 72 |
getVideoPoster(video, (url) => { |
| 73 |
if (url !== videoPosterPreview) { |
| 74 |
setAttributes({ videoPosterPreview: url }); |
| 75 |
} |
| 76 |
}); |
| 77 |
} |
| 78 |
}); |
| 79 |
|
| 80 |
useEffect(() => { |
| 81 |
// Change click action to Fullscreen when used Icon Only style. |
| 82 |
if ( |
| 83 |
clickAction === 'plain' && |
| 84 |
className && |
| 85 |
hasClass(className, 'is-style-icon-only') |
| 86 |
) { |
| 87 |
setAttributes({ clickAction: 'fullscreen' }); |
| 88 |
} |
| 89 |
// eslint-disable-next-line react-hooks/exhaustive-deps |
| 90 |
}, [clickAction, className]); |
| 91 |
|
| 92 |
useEffect(() => { |
| 93 |
// Remove unused classes. |
| 94 |
if (className && !hasClass(className, 'is-style-icon-only')) { |
| 95 |
let newClassName = className; |
| 96 |
|
| 97 |
newClassName = removeClass( |
| 98 |
newClassName, |
| 99 |
'ghostkit-video-style-icon-only-align-right' |
| 100 |
); |
| 101 |
newClassName = removeClass( |
| 102 |
newClassName, |
| 103 |
'ghostkit-video-style-icon-only-align-left' |
| 104 |
); |
| 105 |
|
| 106 |
if (className !== newClassName) { |
| 107 |
setAttributes({ className: newClassName }); |
| 108 |
} |
| 109 |
} |
| 110 |
// eslint-disable-next-line react-hooks/exhaustive-deps |
| 111 |
}, [className]); |
| 112 |
|
| 113 |
const blockProps = useBlockProps({ |
| 114 |
className: applyFilters( |
| 115 |
'ghostkit.editor.className', |
| 116 |
'ghostkit-video', |
| 117 |
props |
| 118 |
), |
| 119 |
'data-video-aspect-ratio': videoAspectRatio, |
| 120 |
}); |
| 121 |
|
| 122 |
return ( |
| 123 |
<> |
| 124 |
<BackgroundColor |
| 125 |
attributes={attributes} |
| 126 |
setAttributes={setAttributes} |
| 127 |
clientId={clientId} |
| 128 |
/> |
| 129 |
<BlockInspectorControls |
| 130 |
attributes={attributes} |
| 131 |
setAttributes={setAttributes} |
| 132 |
isSelected={isSelected} |
| 133 |
/> |
| 134 |
|
| 135 |
<div {...blockProps}> |
| 136 |
{posterUrl && !hasClass(className, 'is-style-icon-only') ? ( |
| 137 |
<div className="ghostkit-video-poster"> |
| 138 |
<img |
| 139 |
src={posterUrl} |
| 140 |
alt={posterAlt} |
| 141 |
width={posterWidth} |
| 142 |
height={posterHeight} |
| 143 |
/> |
| 144 |
</div> |
| 145 |
) : null} |
| 146 |
{!posterUrl && |
| 147 |
type === 'yt_vm_video' && |
| 148 |
videoPosterPreview && |
| 149 |
!hasClass(className, 'is-style-icon-only') ? ( |
| 150 |
<div className="ghostkit-video-poster"> |
| 151 |
<img src={videoPosterPreview} alt="" /> |
| 152 |
</div> |
| 153 |
) : null} |
| 154 |
{iconPlay ? ( |
| 155 |
<div className="ghostkit-video-play-icon"> |
| 156 |
<IconPicker.Dropdown |
| 157 |
onChange={(value) => |
| 158 |
setAttributes({ iconPlay: value }) |
| 159 |
} |
| 160 |
value={iconPlay} |
| 161 |
renderToggle={({ onToggle }) => ( |
| 162 |
<IconPicker.Preview |
| 163 |
onClick={onToggle} |
| 164 |
name={iconPlay} |
| 165 |
/> |
| 166 |
)} |
| 167 |
/> |
| 168 |
</div> |
| 169 |
) : null} |
| 170 |
</div> |
| 171 |
</> |
| 172 |
); |
| 173 |
} |
| 174 |
|