| 1 |
import apiFetch from '@wordpress/api-fetch'; |
| 2 |
|
| 3 |
const id = window.extSharedData.globalStylesPostID; |
| 4 |
|
| 5 |
export default async ({ palette, duotone }) => { |
| 6 |
const currentStyles = await apiFetch({ |
| 7 |
path: `/wp/v2/global-styles/${id}`, |
| 8 |
}); |
| 9 |
|
| 10 |
const currentVibes = await apiFetch({ |
| 11 |
path: '/extendify/v1/agent/block-style-variations', |
| 12 |
}); |
| 13 |
|
| 14 |
const preservedBlocks = Object.keys(currentVibes).reduce( |
| 15 |
(blocks, blockName) => { |
| 16 |
blocks[blockName] = { |
| 17 |
...currentStyles.styles?.blocks?.[blockName], |
| 18 |
variations: currentVibes[blockName], |
| 19 |
}; |
| 20 |
return blocks; |
| 21 |
}, |
| 22 |
{}, |
| 23 |
); |
| 24 |
|
| 25 |
const newPalette = palette.colors.map(({ slug, color, name }) => ({ |
| 26 |
slug, |
| 27 |
color, |
| 28 |
name, |
| 29 |
})); |
| 30 |
|
| 31 |
const colorSettings = { |
| 32 |
...currentStyles.settings?.color, |
| 33 |
palette: { |
| 34 |
...currentStyles.settings?.color?.palette, |
| 35 |
theme: newPalette, |
| 36 |
}, |
| 37 |
}; |
| 38 |
|
| 39 |
if (duotone) { |
| 40 |
colorSettings.duotone = { |
| 41 |
...currentStyles.settings?.color?.duotone, |
| 42 |
theme: duotone, |
| 43 |
}; |
| 44 |
} |
| 45 |
|
| 46 |
return apiFetch({ |
| 47 |
method: 'POST', |
| 48 |
path: `/wp/v2/global-styles/${id}`, |
| 49 |
data: { |
| 50 |
id, |
| 51 |
settings: { |
| 52 |
...currentStyles.settings, |
| 53 |
color: colorSettings, |
| 54 |
}, |
| 55 |
styles: { |
| 56 |
...currentStyles.styles, |
| 57 |
blocks: { |
| 58 |
...currentStyles.styles?.blocks, |
| 59 |
...preservedBlocks, |
| 60 |
}, |
| 61 |
}, |
| 62 |
}, |
| 63 |
}); |
| 64 |
}; |
| 65 |
|