PluginProbe
Extendify / trunk
Extendify vtrunk
3.2.1 3.2.0 3.1.6 3.1.5 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.6 3.0.5 3.0.4 trunk 0.1.0 0.10.0 0.10.1 0.10.2 0.11.0 0.11.1 0.2.0 0.3.0 0.3.1 0.4.0 0.5.0 0.6.0 All 127 releases
extendify / src / Agent / workflows / theme / tools / select-generated-palette.js

select-generated-palette.js in Extendify trunk, at src/Agent/workflows/theme/tools/select-generated-palette.js

79 lines 1.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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