| 1 |
import { deepMerge } from '@shared/lib/utils'; |
| 2 |
import { |
| 3 |
preserveVibeSettings, |
| 4 |
preserveVibeStyles, |
| 5 |
vibeGlobalsEntry, |
| 6 |
} from '@shared/lib/vibe-globals'; |
| 7 |
import { getVibes, vibesBySlug } from '@shared/lib/vibes'; |
| 8 |
import apiFetch from '@wordpress/api-fetch'; |
| 9 |
|
| 10 |
const { globalStylesPostID } = window.extSharedData; |
| 11 |
|
| 12 |
// The vibe layer keeps its owned leaves even when the variation declares them. |
| 13 |
export default async ({ variation }) => { |
| 14 |
const [current, vibes] = await Promise.all([ |
| 15 |
apiFetch({ |
| 16 |
path: `/wp/v2/global-styles/${globalStylesPostID}?context=edit`, |
| 17 |
}), |
| 18 |
getCurrentVibes(), |
| 19 |
]); |
| 20 |
|
| 21 |
const merged = deepMerge( |
| 22 |
{ settings: current.settings, styles: current.styles }, |
| 23 |
variation, |
| 24 |
); |
| 25 |
|
| 26 |
return apiFetch({ |
| 27 |
method: 'POST', |
| 28 |
path: `/wp/v2/global-styles/${globalStylesPostID}`, |
| 29 |
data: { |
| 30 |
id: globalStylesPostID, |
| 31 |
settings: preserveVibeSettings({ |
| 32 |
mergedSettings: merged.settings, |
| 33 |
currentSettings: current.settings, |
| 34 |
vibes, |
| 35 |
}), |
| 36 |
styles: preserveVibeStyles({ |
| 37 |
mergedStyles: merged.styles, |
| 38 |
currentStyles: current.styles, |
| 39 |
vibes, |
| 40 |
}), |
| 41 |
}, |
| 42 |
}); |
| 43 |
}; |
| 44 |
|
| 45 |
const getCurrentVibes = async () => { |
| 46 |
try { |
| 47 |
const { data } = await apiFetch({ |
| 48 |
path: '/extendify/v1/launch/options?option=extendify_siteStyle', |
| 49 |
}); |
| 50 |
const selectedVibe = data?.vibe || 'natural-1'; |
| 51 |
const payloads = await getVibes(`agent,${selectedVibe}`); |
| 52 |
// Widening to the served set purges leaves this vibe never declared. |
| 53 |
const entry = vibeGlobalsEntry(vibesBySlug(payloads), selectedVibe); |
| 54 |
return entry ? { [selectedVibe]: entry } : {}; |
| 55 |
} catch { |
| 56 |
// No payloads means no ownership map; the merged document ships as-is. |
| 57 |
return {}; |
| 58 |
} |
| 59 |
}; |
| 60 |
|