← All changes
|
src/Agent/workflows/theme/tools/update-variation.js
+40
-19
3.1.2
→
3.2.1
View file →
| @@ -1,32 +1,53 @@ | ||
| 1 | +import { appliedVibeOwnership } from '@shared/lib/applied-vibes'; | |
| 1 | 2 | import { deepMerge } from '@shared/lib/utils'; |
| 3 | +import { | |
| 4 | + preserveVibeSettings, | |
| 5 | + preserveVibeStyles, | |
| 6 | +} from '@shared/lib/vibe-globals'; | |
| 2 | 7 | import apiFetch from '@wordpress/api-fetch'; |
| 3 | 8 | |
| 4 | 9 | const { globalStylesPostID } = window.extSharedData; |
| 5 | 10 | |
| 6 | -// Will merge into the current variation and preserve block style variations (vibes) | |
| 7 | -export default async ({ variation }) => { | |
| 8 | - const currentTheme = await apiFetch({ | |
| 9 | - path: `/wp/v2/global-styles/${globalStylesPostID}?context=edit`, | |
| 10 | - }); | |
| 11 | - const currentVariations = await apiFetch({ | |
| 12 | - path: '/extendify/v1/agent/block-style-variations', | |
| 13 | - }); | |
| 11 | +// The vibe layer keeps its owned leaves even when the variation declares them. | |
| 12 | +export default async ({ variation, purge = (document) => document }) => { | |
| 13 | + const [current, vibes] = await Promise.all([ | |
| 14 | + apiFetch({ | |
| 15 | + path: `/wp/v2/global-styles/${globalStylesPostID}?context=edit`, | |
| 16 | + }), | |
| 17 | + getCurrentVibes(), | |
| 18 | + ]); | |
| 14 | 19 | |
| 15 | - const preservedBlockVibes = Object.keys(currentVariations).reduce( | |
| 16 | - (blocks, blockName) => { | |
| 17 | - blocks[blockName] = { variations: currentVariations[blockName] }; | |
| 18 | - return blocks; | |
| 19 | - }, | |
| 20 | - {}, | |
| 20 | + const merged = deepMerge( | |
| 21 | + purge({ settings: current.settings, styles: current.styles }), | |
| 22 | + variation, | |
| 21 | 23 | ); |
| 22 | 24 | |
| 23 | - const final = deepMerge(currentTheme, variation, { | |
| 24 | - styles: { blocks: preservedBlockVibes }, | |
| 25 | - }); | |
| 26 | - | |
| 27 | 25 | return apiFetch({ |
| 28 | 26 | method: 'POST', |
| 29 | 27 | path: `/wp/v2/global-styles/${globalStylesPostID}`, |
| 30 | - data: { id: globalStylesPostID, ...final }, | |
| 28 | + data: { | |
| 29 | + id: globalStylesPostID, | |
| 30 | + settings: preserveVibeSettings({ | |
| 31 | + mergedSettings: merged.settings, | |
| 32 | + currentSettings: current.settings, | |
| 33 | + vibes, | |
| 34 | + }), | |
| 35 | + styles: preserveVibeStyles({ | |
| 36 | + mergedStyles: merged.styles, | |
| 37 | + currentStyles: current.styles, | |
| 38 | + vibes, | |
| 39 | + }), | |
| 40 | + }, | |
| 31 | 41 | }); |
| 42 | +}; | |
| 43 | + | |
| 44 | +const getCurrentVibes = async () => { | |
| 45 | + try { | |
| 46 | + const { data } = await apiFetch({ | |
| 47 | + path: '/extendify/v1/launch/options?option=extendify_siteStyle', | |
| 48 | + }); | |
| 49 | + return await appliedVibeOwnership(data?.vibe); | |
| 50 | + } catch { | |
| 51 | + return {}; | |
| 52 | + } | |
| 32 | 53 | }; |