| 1 |
import { |
| 2 |
applyPaletteGlobals, |
| 3 |
applyPaletteStyles, |
| 4 |
} from '@shared/lib/palette-globals'; |
| 5 |
import { getServedPalettes, palettesBySlug } from '@shared/lib/palettes'; |
| 6 |
import apiFetch from '@wordpress/api-fetch'; |
| 7 |
|
| 8 |
const id = window.extSharedData.globalStylesPostID; |
| 9 |
|
| 10 |
export default async ({ palette, duotone }) => { |
| 11 |
const [currentStyles, currentVibes, palettes] = await Promise.all([ |
| 12 |
apiFetch({ path: `/wp/v2/global-styles/${id}` }), |
| 13 |
apiFetch({ path: '/extendify/v1/agent/block-style-variations' }), |
| 14 |
appliedPalettes(), |
| 15 |
]); |
| 16 |
|
| 17 |
const preservedBlocks = Object.keys(currentVibes).reduce( |
| 18 |
(blocks, blockName) => { |
| 19 |
blocks[blockName] = { |
| 20 |
...currentStyles.styles?.blocks?.[blockName], |
| 21 |
variations: currentVibes[blockName], |
| 22 |
}; |
| 23 |
return blocks; |
| 24 |
}, |
| 25 |
{}, |
| 26 |
); |
| 27 |
|
| 28 |
// A generated palette declares presets only; the rest would stand on the old one. |
| 29 |
const settings = applyPaletteGlobals({ |
| 30 |
currentSettings: currentStyles.settings, |
| 31 |
palettes, |
| 32 |
}); |
| 33 |
|
| 34 |
const styles = applyPaletteStyles({ |
| 35 |
currentStyles: { |
| 36 |
...currentStyles.styles, |
| 37 |
blocks: { |
| 38 |
...currentStyles.styles?.blocks, |
| 39 |
...preservedBlocks, |
| 40 |
}, |
| 41 |
}, |
| 42 |
palettes, |
| 43 |
}); |
| 44 |
|
| 45 |
const colorSettings = { |
| 46 |
...settings?.color, |
| 47 |
palette: { |
| 48 |
...settings?.color?.palette, |
| 49 |
theme: palette.colors.map(({ slug, color, name }) => ({ |
| 50 |
slug, |
| 51 |
color, |
| 52 |
name, |
| 53 |
})), |
| 54 |
}, |
| 55 |
}; |
| 56 |
|
| 57 |
if (duotone) { |
| 58 |
colorSettings.duotone = { ...settings?.color?.duotone, theme: duotone }; |
| 59 |
} |
| 60 |
|
| 61 |
return apiFetch({ |
| 62 |
method: 'POST', |
| 63 |
path: `/wp/v2/global-styles/${id}`, |
| 64 |
data: { id, settings: { ...settings, color: colorSettings }, styles }, |
| 65 |
}); |
| 66 |
}; |
| 67 |
|
| 68 |
const appliedPalettes = async () => { |
| 69 |
try { |
| 70 |
const { data } = await apiFetch({ |
| 71 |
path: '/extendify/v1/launch/options?option=extendify_siteStyle', |
| 72 |
}); |
| 73 |
|
| 74 |
return palettesBySlug(await getServedPalettes('agent', data?.colorPalette)); |
| 75 |
} catch { |
| 76 |
return {}; |
| 77 |
} |
| 78 |
}; |
| 79 |
|