|
| 1 | +import metadata from '../block.json'; |
| 2 | + |
| 3 | +const {registerBlockType} = wp.blocks; |
| 4 | +const {__} = wp.i18n; |
| 5 | +const {TextControl, PanelBody} = wp.components; |
| 6 | +const {InspectorControls, useBlockProps} = wp.blockEditor || wp.editor; |
| 7 | + |
| 8 | +function extractId(input) { |
| 9 | + if (!input) return ''; |
| 10 | + // youtu.be/<id> |
| 11 | + let m = String(input).match(/youtu\.be\/([a-zA-Z0-9_-]{6,})/i); |
| 12 | + if (m && m[1]) return m[1]; |
| 13 | + // youtube.com/watch?v=<id> |
| 14 | + m = String(input).match(/[?&]v=([a-zA-Z0-9_-]{6,})/i); |
| 15 | + if (m && m[1]) return m[1]; |
| 16 | + // youtube.com/embed/<id> |
| 17 | + m = String(input).match(/youtube\.com\/embed\/([a-zA-Z0-9_-]{6,})/i); |
| 18 | + if (m && m[1]) return m[1]; |
| 19 | + // Assume raw ID |
| 20 | + if (/^[a-zA-Z0-9_-]{6,}$/.test(input)) return input; |
| 21 | + return ''; |
| 22 | +} |
| 23 | + |
| 24 | +const PlaySvg = () => ( |
| 25 | + <svg xmlns="http://www.w3.org/2000/svg" width="68" height="48" viewBox="0 0 68 48"> |
| 26 | + <path d="M66.52 7.74a8 8 0 0 0-5.63-5.66C56.7.67 34 .67 34 .67s-22.7 0-26.89 1.41A8 8 0 0 0 1.48 7.74 84.3 84.3 0 0 0 .07 24a84.3 84.3 0 0 0 1.41 16.26 8 8 0 0 0 5.63 5.66C11.3 47.33 34 47.33 34 47.33s22.7 0 26.89-1.41a8 8 0 0 0 5.63-5.66A84.3 84.3 0 0 0 67.93 24a84.3 84.3 0 0 0-1.41-16.26z" fill="#212121" fillOpacity=".8" /> |
| 27 | + <path d="M45 24 27 14v20" fill="#fff" /> |
| 28 | + </svg> |
| 29 | +); |
| 30 | + |
| 31 | +registerBlockType( |
| 32 | + metadata, |
| 33 | + { |
| 34 | + edit: ({attributes, setAttributes}) => { |
| 35 | + const {url, videoId, title, poster, params, aspectRatio} = attributes; |
| 36 | + const effectiveId = videoId || extractId(url); |
| 37 | + const thumbnail = poster || (effectiveId ? `https://i.ytimg.com/vi/${effectiveId}/hqdefault.jpg` : ''); |
| 38 | + |
| 39 | + const blockProps = useBlockProps({ |
| 40 | + className: 'yt-lite', |
| 41 | + }); |
| 42 | + |
| 43 | + return ( |
| 44 | + <> |
| 45 | + <InspectorControls> |
| 46 | + <PanelBody title={__('Settings', 'starter-kit')}> |
| 47 | + <TextControl |
| 48 | + label={__('YouTube URL or ID', 'starter-kit')} |
| 49 | + value={url} |
| 50 | + onChange={(v) => { |
| 51 | + const id = extractId(v); |
| 52 | + setAttributes({ |
| 53 | + url: v, |
| 54 | + videoId: id, |
| 55 | + poster: id ? `https://i.ytimg.com/vi/${id}/hqdefault.jpg` : poster, |
| 56 | + }); |
| 57 | + }} |
| 58 | + help={__('Paste a YouTube URL or video ID', 'starter-kit')} |
| 59 | + /> |
| 60 | + <TextControl |
| 61 | + label={__('Title', 'starter-kit')} |
| 62 | + value={title} |
| 63 | + onChange={(v) => setAttributes({title: v})} |
| 64 | + /> |
| 65 | + <TextControl |
| 66 | + label={__('Poster (optional)', 'starter-kit')} |
| 67 | + value={poster} |
| 68 | + onChange={(v) => setAttributes({poster: v})} |
| 69 | + help={__('Leave empty to use the default YouTube thumbnail', 'starter-kit')} |
| 70 | + /> |
| 71 | + <TextControl |
| 72 | + label={__('Iframe params', 'starter-kit')} |
| 73 | + value={params} |
| 74 | + onChange={(v) => setAttributes({params: v})} |
| 75 | + help={__('Example: rel=0&start=10', 'starter-kit')} |
| 76 | + /> |
| 77 | + <TextControl |
| 78 | + label={__('Aspect ratio', 'starter-kit')} |
| 79 | + value={aspectRatio} |
| 80 | + onChange={(v) => setAttributes({aspectRatio: v})} |
| 81 | + help={__('CSS aspect-ratio value, e.g. 16/9 or 4/3', 'starter-kit')} |
| 82 | + /> |
| 83 | + </PanelBody> |
| 84 | + </InspectorControls> |
| 85 | + <div |
| 86 | + {...blockProps} |
| 87 | + role="button" |
| 88 | + aria-label={title} |
| 89 | + style={{ |
| 90 | + position: 'relative', |
| 91 | + display: 'block', |
| 92 | + width: '100%', |
| 93 | + backgroundColor: '#000', |
| 94 | + overflow: 'hidden', |
| 95 | + aspectRatio: aspectRatio || '16/9', |
| 96 | + cursor: 'pointer', |
| 97 | + }} |
| 98 | + > |
| 99 | + {thumbnail ? ( |
| 100 | + <img |
| 101 | + src={thumbnail} |
| 102 | + alt={title} |
| 103 | + style={{width: '100%', height: '100%', objectFit: 'cover', filter: 'brightness(0.8)'}} |
| 104 | + /> |
| 105 | + ) : ( |
| 106 | + <div style={{color: '#fff', padding: '1rem'}}>{__('Paste a YouTube URL', 'starter-kit')}</div> |
| 107 | + )} |
| 108 | + <div |
| 109 | + className="yt-lite__play" |
| 110 | + aria-hidden="true" |
| 111 | + style={{ |
| 112 | + position: 'absolute', |
| 113 | + top: '50%', |
| 114 | + left: '50%', |
| 115 | + transform: 'translate(-50%,-50%)', |
| 116 | + display: 'flex', |
| 117 | + alignItems: 'center', |
| 118 | + justifyContent: 'center', |
| 119 | + width: '68px', |
| 120 | + height: '48px', |
| 121 | + borderRadius: '10px', |
| 122 | + background: 'rgba(0,0,0,.6)', |
| 123 | + }} |
| 124 | + > |
| 125 | + <PlaySvg /> |
| 126 | + </div> |
| 127 | + </div> |
| 128 | + </> |
| 129 | + ); |
| 130 | + }, |
| 131 | + save: ({attributes}) => { |
| 132 | + const {videoId, title, poster, params, aspectRatio} = attributes; |
| 133 | + const thumbnail = poster || (videoId ? `https://i.ytimg.com/vi/${videoId}/hqdefault.jpg` : ''); |
| 134 | + return ( |
| 135 | + <div |
| 136 | + className="yt-lite" |
| 137 | + data-video-id={videoId || ''} |
| 138 | + data-params={params || 'rel=0'} |
| 139 | + role="button" |
| 140 | + aria-label={title || 'YouTube video'} |
| 141 | + style={{position: 'relative', display: 'block', width: '100%', backgroundColor: '#000', overflow: 'hidden', aspectRatio: aspectRatio || '16/9', cursor: 'pointer'}} |
| 142 | + > |
| 143 | + {thumbnail ? ( |
| 144 | + <img src={thumbnail} alt={title || 'YouTube video'} loading="lazy" decoding="async" style={{width: '100%', height: '100%', objectFit: 'cover', filter: 'brightness(0.8)'}} /> |
| 145 | + ) : null} |
| 146 | + <div className="yt-lite__play" aria-hidden="true" style={{position: 'absolute', top: '50%', left: '50%', transform: 'translate(-50%,-50%)', display: 'flex', alignItems: 'center', justifyContent: 'center', width: '68px', height: '48px', borderRadius: '10px', background: 'rgba(0,0,0,.6)'}}> |
| 147 | + <PlaySvg /> |
| 148 | + </div> |
| 149 | + </div> |
| 150 | + ); |
| 151 | + }, |
| 152 | + }); |
| 153 | + |
0 commit comments