| 1 |
import { isObject } from '@shared/lib/utils'; |
| 2 |
|
| 3 |
// Patterns are authored against natural-1 class names, so each payload node is |
| 4 |
// re-keyed from the vibe's own slug to its natural-1 name. |
| 5 |
export const rekeyVibeBlocks = (vibeBlocks, selectedVibe) => |
| 6 |
Object.fromEntries( |
| 7 |
Object.entries(vibeBlocks ?? {}) |
| 8 |
.filter(([, variations]) => isObject(variations)) |
| 9 |
.map(([blockName, variations]) => [ |
| 10 |
blockName, |
| 11 |
{ |
| 12 |
variations: Object.fromEntries( |
| 13 |
Object.entries(variations).map(([styleName, styleProperties]) => [ |
| 14 |
styleName.replace(`--${selectedVibe}--`, '--natural-1--'), |
| 15 |
{ ...styleProperties }, |
| 16 |
]), |
| 17 |
), |
| 18 |
}, |
| 19 |
]), |
| 20 |
); |
| 21 |
|
| 22 |
// ext-preset-- variations belong to the outgoing vibe; user variations stay. |
| 23 |
// natural-1 is written too: a theme with no presets has nothing to fall back on. |
| 24 |
export const replaceVibeBlocks = ({ |
| 25 |
currentBlocks = {}, |
| 26 |
vibeBlocks = {}, |
| 27 |
selectedVibe, |
| 28 |
}) => { |
| 29 |
const updatedBlocks = Object.fromEntries( |
| 30 |
Object.entries(currentBlocks).map(([blockName, blockObj]) => { |
| 31 |
const { variations = {}, ...rest } = blockObj; |
| 32 |
const userVariations = Object.fromEntries( |
| 33 |
Object.entries(variations).filter( |
| 34 |
([styleName]) => !styleName.startsWith('ext-preset--'), |
| 35 |
), |
| 36 |
); |
| 37 |
|
| 38 |
return [ |
| 39 |
blockName, |
| 40 |
Object.keys(userVariations).length > 0 |
| 41 |
? { ...rest, variations: userVariations } |
| 42 |
: rest, |
| 43 |
]; |
| 44 |
}), |
| 45 |
); |
| 46 |
|
| 47 |
for (const [blockName, { variations }] of Object.entries( |
| 48 |
rekeyVibeBlocks(vibeBlocks, selectedVibe), |
| 49 |
)) { |
| 50 |
const currentBlock = updatedBlocks[blockName] || {}; |
| 51 |
updatedBlocks[blockName] = { |
| 52 |
...currentBlock, |
| 53 |
variations: { ...currentBlock.variations, ...variations }, |
| 54 |
}; |
| 55 |
} |
| 56 |
|
| 57 |
return updatedBlocks; |
| 58 |
}; |
| 59 |
|