| 1 |
import { |
| 2 |
applyPaletteGlobals, |
| 3 |
applyPaletteStyles, |
| 4 |
} from '@shared/lib/palette-globals'; |
| 5 |
import { getServedPalettes, palettesBySlug } from '@shared/lib/palettes'; |
| 6 |
import { deepMerge } from '@shared/lib/utils'; |
| 7 |
|
| 8 |
const empty = { settings: {}, styles: {} }; |
| 9 |
|
| 10 |
// The families ship with the theme; only a hosted font needs installing. |
| 11 |
const fontLeaves = (fonts) => { |
| 12 |
const { heading, body } = fonts ?? {}; |
| 13 |
if (!heading?.slug && !body?.slug) return empty; |
| 14 |
|
| 15 |
return { |
| 16 |
settings: { |
| 17 |
typography: { |
| 18 |
fontFamilies: { |
| 19 |
custom: [heading, body].filter((font) => !!font?.host), |
| 20 |
}, |
| 21 |
}, |
| 22 |
}, |
| 23 |
styles: { |
| 24 |
...(body?.slug && { |
| 25 |
typography: { |
| 26 |
fontFamily: `var(--wp--preset--font-family--${body.slug})`, |
| 27 |
}, |
| 28 |
}), |
| 29 |
...(heading?.slug && { |
| 30 |
elements: { |
| 31 |
heading: { |
| 32 |
typography: { |
| 33 |
fontFamily: `var(--wp--preset--font-family--${heading.slug})`, |
| 34 |
}, |
| 35 |
}, |
| 36 |
}, |
| 37 |
}), |
| 38 |
}, |
| 39 |
}; |
| 40 |
}; |
| 41 |
|
| 42 |
const paletteLeaves = async (colorPalette) => { |
| 43 |
if (!colorPalette) return empty; |
| 44 |
|
| 45 |
const palettes = palettesBySlug( |
| 46 |
await getServedPalettes('launch', colorPalette), |
| 47 |
); |
| 48 |
const palette = palettes[colorPalette]; |
| 49 |
// An unresolved slug leaves the theme's own colourway standing. |
| 50 |
if (!palette) return empty; |
| 51 |
|
| 52 |
return { |
| 53 |
settings: applyPaletteGlobals({ |
| 54 |
currentSettings: {}, |
| 55 |
paletteSettings: palette.settings, |
| 56 |
palettes, |
| 57 |
}), |
| 58 |
styles: applyPaletteStyles({ |
| 59 |
currentStyles: {}, |
| 60 |
paletteStyles: palette.styles, |
| 61 |
palettes, |
| 62 |
}), |
| 63 |
}; |
| 64 |
}; |
| 65 |
|
| 66 |
export const getStyleDocument = async ({ colorPalette, fonts }) => |
| 67 |
deepMerge(await paletteLeaves(colorPalette), fontLeaves(fonts)); |
| 68 |
|