|
1 | 1 | /** |
2 | | - * Breakpoint utility functions for responsive video resolution selection |
3 | | - * Based on container width and device pixel ratio (DPR) |
| 2 | + * Breakpoint utility for responsive video resolution |
| 3 | + * Passes container width and DPR to Cloudinary for optimal video delivery |
4 | 4 | */ |
5 | 5 |
|
| 6 | +const DEFAULT_DPR = 2.0; |
| 7 | + |
6 | 8 | /** |
7 | | - * Normalize DPR to allowed values (1.0, 1.5, or 2.0) |
8 | | - * Rounds to nearest allowed value and caps at 2.0 |
9 | | - * @param {number} dpr - User-specified DPR |
| 9 | + * Validate and normalize DPR to 1.0, 1.5, or 2.0 |
| 10 | + * @param {number} dpr - User-specified DPR value |
10 | 11 | * @returns {number} Normalized DPR (1.0, 1.5, or 2.0) |
11 | 12 | */ |
12 | | -export const normalizeDpr = (dpr) => { |
13 | | - // Cap at 2.0 first |
14 | | - const capped = Math.min(dpr, 2.0); |
15 | | - |
16 | | - // Round to nearest allowed value (1.0, 1.5, or 2.0) |
17 | | - if (capped < 1.25) return 1.0; |
18 | | - if (capped < 1.75) return 1.5; |
19 | | - return 2.0; |
| 13 | +export const validateDpr = (dpr) => { |
| 14 | + const capped = (typeof dpr === 'number' && !isNaN(dpr) && dpr >= 1.0) ? Math.min(dpr, 2.0) : DEFAULT_DPR; |
| 15 | + return capped < 1.25 ? 1.0 : capped < 1.75 ? 1.5 : 2.0; |
20 | 16 | }; |
21 | 17 |
|
22 | 18 | /** |
23 | | - * Calculate optimal video width based on container and DPR |
| 19 | + * Get breakpoint transformation for responsive video |
24 | 20 | * @param {Object} params |
25 | | - * @param {number} params.containerWidth - Container element width in pixels |
26 | | - * @param {number} params.dpr - Normalized DPR (1.0, 1.5, or 2.0) |
27 | | - * @param {number[]} params.renditions - Available rendition widths (sorted ascending) |
28 | | - * @returns {Object} { width, height } |
| 21 | + * @param {boolean} params.breakpointsEnabled - Whether breakpoints are enabled |
| 22 | + * @param {number} params.dpr - DPR value (already validated) |
| 23 | + * @returns {Object|null} Transformation with width, dpr, and crop |
29 | 24 | */ |
30 | | -export const calculateBreakpoint = ({ |
31 | | - containerWidth, |
32 | | - dpr, |
33 | | - renditions |
34 | | -}) => { |
35 | | - // Calculate required width (DPR already normalized) |
36 | | - const requiredWidth = Math.round(containerWidth * dpr); |
| 25 | +export const getBreakpointTransformation = ({ breakpointsEnabled, dpr }) => { |
| 26 | + if (!breakpointsEnabled) return null; |
37 | 27 |
|
38 | | - // Select smallest rendition >= requiredWidth, or largest if none found |
39 | | - const selectedWidth = renditions.find(w => w >= requiredWidth) || renditions[renditions.length - 1]; |
| 28 | + // Get player element from DOM |
| 29 | + const playerElement = document.querySelector('.cld-video-player, video.vjs-tech'); |
| 30 | + const width = playerElement?.clientWidth; |
| 31 | + |
| 32 | + if (!width) return null; |
40 | 33 |
|
41 | 34 | return { |
42 | | - width: selectedWidth, |
43 | | - height: null // Let Cloudinary auto-calculate |
| 35 | + width, |
| 36 | + dpr, |
| 37 | + crop: 'limit' |
44 | 38 | }; |
45 | 39 | }; |
46 | | - |
47 | | -/** |
48 | | - * Validate breakpoint configuration |
49 | | - * @param {Object} config - Breakpoint config object |
50 | | - * @returns {Object} { valid: boolean, error: string|null } |
51 | | - */ |
52 | | -export const validateBreakpointConfig = (config) => { |
53 | | - if (!config || typeof config !== 'object') { |
54 | | - return { valid: false, error: 'Breakpoint config must be an object' }; |
55 | | - } |
56 | | - |
57 | | - if (config.dpr !== undefined) { |
58 | | - if (typeof config.dpr !== 'number' || isNaN(config.dpr)) { |
59 | | - return { valid: false, error: 'dpr must be a valid number' }; |
60 | | - } |
61 | | - if (config.dpr < 1.0) { |
62 | | - return { valid: false, error: 'dpr must be at least 1.0' }; |
63 | | - } |
64 | | - } |
65 | | - |
66 | | - return { valid: true, error: null }; |
67 | | -}; |
68 | | - |
69 | | -/** |
70 | | - * Get container element for width measurement |
71 | | - * Always uses parent element, with fallback to player element itself |
72 | | - * @param {HTMLElement} playerElement - Video player element |
73 | | - * @returns {HTMLElement|null} Container element or null if not found |
74 | | - */ |
75 | | -export const getContainerElement = (playerElement) => { |
76 | | - if (!playerElement) { |
77 | | - return null; |
78 | | - } |
79 | | - |
80 | | - // Use parent element, fallback to player element itself |
81 | | - return playerElement.parentElement || playerElement; |
82 | | -}; |
0 commit comments