← All changes
|
src/Agent/workflows/theme/tools/update-site-vibes.js
+39
-45
3.1.5
→
trunk
View file →
| @@ -1,8 +1,14 @@ | ||
| 1 | +import { replaceVibeBlocks } from '@shared/lib/vibe-blocks'; | |
| 2 | +import { | |
| 3 | + applyVibeGlobals, | |
| 4 | + applyVibeStyles, | |
| 5 | + vibeGlobalsEntry, | |
| 6 | +} from '@shared/lib/vibe-globals'; | |
| 7 | +import { getVibes, vibesBySlug } from '@shared/lib/vibes'; | |
| 1 | 8 | import apiFetch from '@wordpress/api-fetch'; |
| 2 | 9 | |
| 3 | 10 | const globalStylesPostID = window.extSharedData?.globalStylesPostID; |
| 4 | -const themeSlug = window.extAgentData?.context?.themeSlug; | |
| 5 | 11 | |
| 6 | 12 | export default async ({ selectedVibe }) => { |
| 7 | 13 | if ( |
| 8 | 14 | !selectedVibe || |
| @@ -12,38 +18,47 @@ | ||
| 12 | 18 | return; |
| 13 | 19 | } |
| 14 | 20 | |
| 15 | 21 | try { |
| 16 | - // Fetch variation | |
| 17 | - const { styles: variationStyles } = await apiFetch({ | |
| 18 | - path: `/wp/v2/global-styles/${globalStylesPostID}?context=edit`, | |
| 19 | - }); | |
| 20 | - // Fetch theme styles | |
| 21 | - const { styles: themeStyles } = await apiFetch({ | |
| 22 | - path: `/wp/v2/global-styles/themes/${themeSlug}?context=edit`, | |
| 23 | - }); | |
| 22 | + const [currentGlobalStyles, payloads] = await Promise.all([ | |
| 23 | + apiFetch({ | |
| 24 | + path: `/wp/v2/global-styles/${globalStylesPostID}?context=edit`, | |
| 25 | + }), | |
| 26 | + // Same query the picker asked for, so this reads the cached response. | |
| 27 | + getVibes(`agent,${selectedVibe}`), | |
| 28 | + ]); | |
| 24 | 29 | |
| 25 | - const updatedBlocks = {}; | |
| 26 | - for (const [blockName, blockObj] of Object.entries(themeStyles.blocks)) { | |
| 27 | - if (!blockObj?.variations) { | |
| 28 | - updatedBlocks[blockName] = blockObj; | |
| 29 | - continue; | |
| 30 | - } | |
| 30 | + const vibes = vibesBySlug(payloads); | |
| 31 | + const entry = vibeGlobalsEntry(vibes, selectedVibe); | |
| 32 | + const currentStyles = currentGlobalStyles.styles; | |
| 31 | 33 | |
| 32 | - const { variations, ...rest } = blockObj; | |
| 33 | - updatedBlocks[blockName] = { | |
| 34 | - ...rest, | |
| 35 | - variations: processBlockVariations(variations, selectedVibe), | |
| 36 | - }; | |
| 37 | - } | |
| 34 | + const styles = applyVibeStyles({ | |
| 35 | + currentStyles, | |
| 36 | + vibeStyles: entry?.styles, | |
| 37 | + vibes, | |
| 38 | + }); | |
| 38 | 39 | |
| 39 | - // Apply the update | |
| 40 | + // One post holds all three, so a second POST would drop the first. | |
| 40 | 41 | await Promise.all([ |
| 41 | 42 | apiFetch({ |
| 42 | - path: `wp/v2/global-styles/${globalStylesPostID}`, | |
| 43 | 43 | method: 'POST', |
| 44 | + path: `/wp/v2/global-styles/${globalStylesPostID}`, | |
| 44 | 45 | data: { |
| 45 | - styles: { ...variationStyles, blocks: updatedBlocks }, | |
| 46 | + id: globalStylesPostID, | |
| 47 | + settings: applyVibeGlobals({ | |
| 48 | + currentSettings: currentGlobalStyles.settings, | |
| 49 | + vibeSettings: entry?.settings, | |
| 50 | + vibes, | |
| 51 | + }), | |
| 52 | + styles: { | |
| 53 | + ...styles, | |
| 54 | + // Pre-apply blocks here would drop the css applyVibeStyles wrote. | |
| 55 | + blocks: replaceVibeBlocks({ | |
| 56 | + currentBlocks: styles?.blocks, | |
| 57 | + vibeBlocks: vibes[selectedVibe]?.blocks, | |
| 58 | + selectedVibe, | |
| 59 | + }), | |
| 60 | + }, | |
| 46 | 61 | }, |
| 47 | 62 | }), |
| 48 | 63 | updateSiteStyleOption(selectedVibe), |
| 49 | 64 | ]); |
| @@ -72,26 +87,5 @@ | ||
| 72 | 87 | path: '/extendify/v1/launch/options', |
| 73 | 88 | method: 'POST', |
| 74 | 89 | data: { option: 'extendify_siteStyle', value: updatedSiteStyle }, |
| 75 | 90 | }); |
| 76 | -}; | |
| 77 | - | |
| 78 | -const processBlockVariations = (variations, targetVibe) => { | |
| 79 | - const updatedVariations = {}; | |
| 80 | - | |
| 81 | - for (const [styleName, styleProperties] of Object.entries(variations)) { | |
| 82 | - if (!styleName.includes('--natural-1--')) { | |
| 83 | - updatedVariations[styleName] = styleProperties; | |
| 84 | - continue; | |
| 85 | - } | |
| 86 | - | |
| 87 | - const sourceStyleName = styleName.replace( | |
| 88 | - '--natural-1--', | |
| 89 | - `--${targetVibe}--`, | |
| 90 | - ); | |
| 91 | - const sourceStyle = variations[sourceStyleName]; | |
| 92 | - | |
| 93 | - updatedVariations[styleName] = sourceStyle || styleProperties; | |
| 94 | - } | |
| 95 | - | |
| 96 | - return updatedVariations; | |
| 97 | 91 | }; |