Jake Vanderwerf
7 days ago 46d681c6b825d21b3f698d793c4e630c687d90ad
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
//edit.js
import { __ } from '@wordpress/i18n';
import {
    useBlockProps,
    InspectorControls,
    MediaUpload,
    MediaUploadCheck,
    InnerBlocks,
    useInnerBlocksProps
} from '@wordpress/block-editor';
import {
    PanelBody,
    Button,
    ToggleControl,
    BaseControl,
    RangeControl,
    SelectControl
} from '@wordpress/components';
import './editor.scss';
 
const ALLOWED_VIDEO_TYPES = ['video'];
 
const INNER_BLOCKS_TEMPLATE = [
    ['core/heading', {
        level: 1,
        placeholder: 'Add heading...',
        textAlign: 'center'
    }],
    ['core/paragraph', {
        placeholder: 'Add description...',
        align: 'center'
    }],
    ['core/buttons', {
        layout: { type: 'flex', justifyContent: 'center' }
    }]
];
 
 
 
export default function Edit({ attributes, setAttributes }) {
    const {
        posterId,
        posterUrl,
        videoSources,
        fadeEffect,
        overlayOpacity,
        contentAlignment,
        minHeight
    } = attributes;
 
    const blockProps = useBlockProps({
        className: 'video-cover-editor',
        style: {
            minHeight: minHeight ? `${minHeight}px` : undefined
        }
    });
 
    const innerBlocksProps = useInnerBlocksProps(
        { className: 'video-cover-content' },
        {
            template: INNER_BLOCKS_TEMPLATE,
            templateLock: false
        }
    );
 
    const onSelectPoster = (media) => {
        setAttributes({
            posterId: media.id,
            posterUrl: media.url
        });
    };
 
    const onSelectVideos = (mediaItems) => {
        // multiple=true returns an array
        const items = Array.isArray(mediaItems) ? mediaItems : [mediaItems];
        const newSources = items
            .filter(media => !videoSources.some(s => s.id === media.id))
            .map(media => ({
                id: media.id,
                url: media.url,
                mime: media.mime
            }));
 
        if (newSources.length) {
            setAttributes({
                videoSources: [...videoSources, ...newSources]
            });
        }
    };
 
    const removeVideoSource = (index) => {
        const updated = [...videoSources];
        updated.splice(index, 1);
        setAttributes({ videoSources: updated });
    };
 
    const renderVideoSourceList = (sources, isMobile = false) => {
        if (sources.length === 0) return null;
 
        return (
            <ul className="video-source-list">
                {sources.map((source, index) => (
                    <li key={index} className="video-source-item">
                        <span className="video-source-mime">{source.mime}</span>
                        <Button
                            isDestructive
                            isSmall
                            onClick={() => removeVideoSource(index, isMobile)}
                        >
                            {__('Remove', 'jvb')}
                        </Button>
                    </li>
                ))}
            </ul>
        );
    };
 
    return (
        <>
            <InspectorControls>
                <PanelBody title={__('Video Settings', 'jvb')} initialOpen={true}>
                    <BaseControl
                        label={__('Poster Image', 'jvb')}
                        help={__('Image shown while video loads', 'jvb')}
                    >
                        <MediaUploadCheck>
                            <MediaUpload
                                onSelect={onSelectPoster}
                                allowedTypes={['image']}
                                value={posterId}
                                render={({ open }) => (
                                    <>
                                        {posterUrl && (
                                            <img
                                                src={posterUrl}
                                                alt={__('Poster preview', 'jvb')}
                                                style={{ maxWidth: '100%', marginBottom: '10px' }}
                                            />
                                        )}
                                        <Button
                                            onClick={open}
                                            variant={posterUrl ? 'secondary' : 'primary'}
                                        >
                                            {posterUrl
                                                ? __('Change Poster', 'jvb')
                                                : __('Select Poster', 'jvb')}
                                        </Button>
                                        {posterUrl && (
                                            <Button
                                                isDestructive
                                                onClick={() => setAttributes({ posterId: 0, posterUrl: '' })}
                                                style={{ marginLeft: '10px' }}
                                            >
                                                {__('Remove', 'jvb')}
                                            </Button>
                                        )}
                                    </>
                                )}
                            />
                        </MediaUploadCheck>
                    </BaseControl>
 
                    <BaseControl
                        label={__('Video Sources', 'jvb')}
                        help={__('Add multiple formats for better browser support (mp4, webm, etc.)', 'jvb')}
                    >
                        {videoSources.length > 0 && (
                            <ul className="video-source-list">
                                {videoSources.map((source, index) => (
                                    <li key={index} className="video-source-item">
                                        <span className="video-source-mime">{source.mime}</span>
                                        <Button
                                            isDestructive
                                            isSmall
                                            onClick={() => removeVideoSource(index)}
                                        >
                                            {__('Remove', 'jvb')}
                                        </Button>
                                    </li>
                                ))}
                            </ul>
                        )}
                        <MediaUploadCheck>
                            <MediaUpload
                                multiple={true}
                                onSelect={onSelectVideos}
                                allowedTypes={ALLOWED_VIDEO_TYPES}
                                render={({ open }) => (
                                    <Button onClick={open} variant="secondary">
                                        {__('Add Video', 'jvb')}
                                    </Button>
                                )}
                            />
                        </MediaUploadCheck>
                    </BaseControl>
 
                    <ToggleControl
                        label={__('Fade Effect', 'jvb')}
                        help={__('Add fade class to video element', 'jvb')}
                        checked={fadeEffect}
                        onChange={(value) => setAttributes({ fadeEffect: value })}
                    />
                </PanelBody>
 
                <PanelBody title={__('Overlay Settings', 'jvb')} initialOpen={true}>
                    <RangeControl
                        label={__('Overlay Opacity', 'jvb')}
                        help={__('Darken video for better text readability', 'jvb')}
                        value={overlayOpacity}
                        onChange={(value) => setAttributes({ overlayOpacity: value })}
                        min={0}
                        max={100}
                        step={5}
                    />
 
                    <SelectControl
                        label={__('Content Alignment', 'jvb')}
                        value={contentAlignment}
                        options={[
                            { label: __('Top Left', 'jvb'), value: 'top-left' },
                            { label: __('Top Center', 'jvb'), value: 'top-center' },
                            { label: __('Top Right', 'jvb'), value: 'top-right' },
                            { label: __('Center Left', 'jvb'), value: 'center-left' },
                            { label: __('Center', 'jvb'), value: 'center' },
                            { label: __('Center Right', 'jvb'), value: 'center-right' },
                            { label: __('Bottom Left', 'jvb'), value: 'bottom-left' },
                            { label: __('Bottom Center', 'jvb'), value: 'bottom-center' },
                            { label: __('Bottom Right', 'jvb'), value: 'bottom-right' }
                        ]}
                        onChange={(value) => setAttributes({ contentAlignment: value })}
                    />
 
                    <RangeControl
                        label={__('Minimum Height', 'jvb')}
                        help={__('Minimum height in pixels (leave 0 for auto)', 'jvb')}
                        value={minHeight}
                        onChange={(value) => setAttributes({ minHeight: value })}
                        min={0}
                        max={1000}
                        step={50}
                    />
                </PanelBody>
            </InspectorControls>
 
            <div {...blockProps}>
                {posterUrl || videoSources.length > 0 ? (
                    <div className="video-cover-preview">
                        {posterUrl && (
                            <>
                                <img src={posterUrl} alt={__('Video poster', 'jvb')} />
                                {overlayOpacity > 0 && (
                                    <div
                                        className="video-overlay-preview"
                                        style={{ opacity: overlayOpacity / 100 }}
                                    />
                                )}
                            </>
                        )}
                        <div className={`video-cover-content-preview align-${contentAlignment}`}>
                            <div {...innerBlocksProps} />
                        </div>
                        <div className="video-info">
                            <p>
                                {videoSources.length} {__('desktop source(s)', 'jvb')}
                            </p>
                        </div>
                    </div>
                ) : (
                    <div className="video-cover-placeholder">
                        <p>{__('Configure video sources in the sidebar →', 'jvb')}</p>
                    </div>
                )}
            </div>
        </>
    );
}