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