| 1 |
import { getGlobalStyles } from '@auto-launch/functions/theme'; |
| 2 |
|
| 3 |
const isValidVibe = (selectedVibe) => |
| 4 |
!!selectedVibe && |
| 5 |
typeof selectedVibe === 'string' && |
| 6 |
selectedVibe.trim() !== '' && |
| 7 |
selectedVibe !== 'natural-1'; |
| 8 |
|
| 9 |
const generateSourceStyleName = (naturalStyleName, targetVibe) => |
| 10 |
naturalStyleName.replace('--natural-1--', `--${targetVibe}--`); |
| 11 |
|
| 12 |
const processBlockVariations = (variations, targetVibe) => |
| 13 |
Object.fromEntries( |
| 14 |
Object.entries(variations).map(([styleName, styleProperties]) => { |
| 15 |
if (!styleName.includes('--natural-1--')) { |
| 16 |
return [styleName, { ...styleProperties }]; |
| 17 |
} |
| 18 |
|
| 19 |
const sourceStyleName = generateSourceStyleName(styleName, targetVibe); |
| 20 |
const sourceStyle = variations[sourceStyleName]; |
| 21 |
|
| 22 |
return [ |
| 23 |
styleName, |
| 24 |
sourceStyle ? { ...sourceStyle } : { ...styleProperties }, |
| 25 |
]; |
| 26 |
}), |
| 27 |
); |
| 28 |
|
| 29 |
// Compute the vibe-adjusted blocks from the theme's global styles. |
| 30 |
// Returns the blocks object (ready to merge into a variation) without |
| 31 |
// POSTing anything. Callers should merge the result into the variation's |
| 32 |
// styles before calling updateVariation — doing it this way avoids a |
| 33 |
// separate POST that would overwrite the fonts, colors and other style |
| 34 |
// overrides already set in the variation. |
| 35 |
export const computeVibeAdjustedBlocks = async (selectedVibe) => { |
| 36 |
if (!isValidVibe(selectedVibe)) return null; |
| 37 |
|
| 38 |
const { styles: themeStyles } = await getGlobalStyles(); |
| 39 |
if (!themeStyles?.blocks) return null; |
| 40 |
|
| 41 |
return Object.fromEntries( |
| 42 |
Object.entries(themeStyles.blocks).map(([blockName, blockObj]) => { |
| 43 |
if (!blockObj?.variations) { |
| 44 |
return [blockName, blockObj]; |
| 45 |
} |
| 46 |
|
| 47 |
const { variations, ...rest } = blockObj; |
| 48 |
const hasNaturalVariations = Object.keys(variations).some((styleName) => |
| 49 |
styleName.includes('--natural-1--'), |
| 50 |
); |
| 51 |
|
| 52 |
if (!hasNaturalVariations) { |
| 53 |
return [blockName, blockObj]; |
| 54 |
} |
| 55 |
|
| 56 |
return [ |
| 57 |
blockName, |
| 58 |
{ |
| 59 |
...rest, |
| 60 |
variations: processBlockVariations(variations, selectedVibe), |
| 61 |
}, |
| 62 |
]; |
| 63 |
}), |
| 64 |
); |
| 65 |
}; |
| 66 |
|