|
| 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; |
| 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 fill="#f03" |
| 27 | + d="M66.52 7.74c-.78-2.93-2.49-5.41-5.42-6.19C55.79.13 34 0 34 0S12.21.13 6.9 1.55c-2.93.78-4.63 3.26-5.42 6.19C.06 13.05 0 24 0 24s.06 10.95 1.48 16.26c.78 2.93 2.49 5.41 5.42 6.19C12.21 47.87 34 48 34 48s21.79-.13 27.1-1.55c2.93-.78 4.64-3.26 5.42-6.19C67.94 34.95 68 24 68 24s-.06-10.95-1.48-16.26"/> |
| 28 | + <path fill="#fff" d="M45 24 27 14v20"/> |
| 29 | + </svg> |
| 30 | +); |
| 31 | + |
| 32 | +registerBlockType( |
| 33 | + metadata, |
| 34 | + { |
| 35 | + edit: ({attributes, setAttributes}) => { |
| 36 | + const {url, videoId, title, poster, params, aspectRatio} = attributes; |
| 37 | + const effectiveId = videoId || extractId(url); |
| 38 | + const thumbnail = poster || (effectiveId ? `https://i.ytimg.com/vi/${effectiveId}/hqdefault.jpg` : ''); |
| 39 | + |
| 40 | + const blockProps = useBlockProps({ |
| 41 | + className: 'yt-lite', |
| 42 | + }); |
| 43 | + |
| 44 | + return ( |
| 45 | + <> |
| 46 | + <InspectorControls> |
| 47 | + <PanelBody title={__('Settings', 'starter-kit')}> |
| 48 | + <TextControl |
| 49 | + label={__('YouTube URL or ID', 'starter-kit')} |
| 50 | + value={url} |
| 51 | + onChange={(v) => { |
| 52 | + const id = extractId(v); |
| 53 | + setAttributes({ |
| 54 | + url: v, |
| 55 | + videoId: id, |
| 56 | + poster: id ? `https://i.ytimg.com/vi/${id}/hqdefault.jpg` : poster, |
| 57 | + }); |
| 58 | + }} |
| 59 | + help={__('Paste a YouTube URL or video ID', 'starter-kit')} |
| 60 | + /> |
| 61 | + <TextControl |
| 62 | + label={__('Title', 'starter-kit')} |
| 63 | + value={title} |
| 64 | + onChange={(v) => setAttributes({title: v})} |
| 65 | + /> |
| 66 | + <TextControl |
| 67 | + label={__('Poster (optional)', 'starter-kit')} |
| 68 | + value={poster} |
| 69 | + onChange={(v) => setAttributes({poster: v})} |
| 70 | + help={__('Leave empty to use the default YouTube thumbnail', 'starter-kit')} |
| 71 | + /> |
| 72 | + <TextControl |
| 73 | + label={__('Iframe params', 'starter-kit')} |
| 74 | + value={params} |
| 75 | + onChange={(v) => setAttributes({params: v})} |
| 76 | + help={__('Example: rel=0&start=10', 'starter-kit')} |
| 77 | + /> |
| 78 | + <TextControl |
| 79 | + label={__('Aspect ratio', 'starter-kit')} |
| 80 | + value={aspectRatio} |
| 81 | + onChange={(v) => setAttributes({aspectRatio: v})} |
| 82 | + help={__('CSS aspect-ratio value, e.g. 16/9 or 4/3', 'starter-kit')} |
| 83 | + /> |
| 84 | + </PanelBody> |
| 85 | + </InspectorControls> |
| 86 | + <div |
| 87 | + {...blockProps} |
| 88 | + role="button" |
| 89 | + aria-label={title} |
| 90 | + > |
| 91 | + {thumbnail ? ( |
| 92 | + <img |
| 93 | + src={thumbnail} |
| 94 | + alt={title} |
| 95 | + /> |
| 96 | + ) : ( |
| 97 | + <div className="yt-lite__placeholder"> |
| 98 | + {__('Paste a YouTube URL', 'starter-kit')} |
| 99 | + </div> |
| 100 | + )} |
| 101 | + <div |
| 102 | + className="yt-lite__play" |
| 103 | + aria-hidden="true" |
| 104 | + > |
| 105 | + <PlaySvg /> |
| 106 | + </div> |
| 107 | + </div> |
| 108 | + </> |
| 109 | + ); |
| 110 | + }, |
| 111 | + save: (props) => { |
| 112 | + const {attributes} = props; |
| 113 | + const {videoId, title, poster, params} = attributes; |
| 114 | + const thumbnail = poster || (videoId ? `https://i.ytimg.com/vi/${videoId}/hqdefault.jpg` : ''); |
| 115 | + |
| 116 | + const {className} = useBlockProps.save(); |
| 117 | + const blockClass = ['yt-lite', className].filter(Boolean).join(' ').trim(); |
| 118 | + |
| 119 | + const blockProps = { |
| 120 | + className: blockClass, |
| 121 | + 'data-video-id': videoId || '', |
| 122 | + 'data-params': params || 'rel=0', |
| 123 | + role: 'button', |
| 124 | + 'aria-label': title || 'YouTube video', |
| 125 | + }; |
| 126 | + |
| 127 | + return ( |
| 128 | + <div {...blockProps}> |
| 129 | + {thumbnail ? ( |
| 130 | + <img |
| 131 | + src={thumbnail} |
| 132 | + alt={title || 'YouTube video'} |
| 133 | + loading="lazy" |
| 134 | + decoding="async" |
| 135 | + /> |
| 136 | + ) : null} |
| 137 | + <div |
| 138 | + className="yt-lite__play" |
| 139 | + aria-hidden="true" |
| 140 | + > |
| 141 | + <PlaySvg /> |
| 142 | + </div> |
| 143 | + </div> |
| 144 | + ); |
| 145 | + }, |
| 146 | + }); |
| 147 | + |
0 commit comments