| 1 |
import apiFetch from '@wordpress/api-fetch'; |
| 2 |
import create from 'zustand'; |
| 3 |
import { devtools, persist } from 'zustand/middleware'; |
| 4 |
import { Attributes } from '../types'; |
| 5 |
|
| 6 |
type ThemeType = { |
| 7 |
previousTheme: string; |
| 8 |
previousLineHeight: string; |
| 9 |
previousFontFamily: string; |
| 10 |
previousFontSize: string; |
| 11 |
previousHeaderType: string; |
| 12 |
previousClampFonts: boolean; |
| 13 |
updateThemeHistory: (settings: Partial<Attributes>) => void; |
| 14 |
}; |
| 15 |
const path = '/wp/v2/settings'; |
| 16 |
const getSettings = async (name: string) => { |
| 17 |
const allSettings = await apiFetch({ path }); |
| 18 |
// eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 19 |
// @ts-ignore-next-line |
| 20 |
return allSettings?.[name]; |
| 21 |
}; |
| 22 |
export const useThemeStore = create<ThemeType>()( |
| 23 |
persist( |
| 24 |
devtools( |
| 25 |
(set) => ({ |
| 26 |
previousTheme: 'nord', |
| 27 |
previousLineHeight: '1.25rem', |
| 28 |
previousFontFamily: '', |
| 29 |
previousFontSize: '.875rem', |
| 30 |
previousHeaderType: 'headlights', |
| 31 |
previousClampFonts: false, |
| 32 |
updateThemeHistory(attributes) { |
| 33 |
set((state) => ({ |
| 34 |
...state, |
| 35 |
previousTheme: attributes.theme, |
| 36 |
previousLineHeight: attributes.lineHeight, |
| 37 |
previousFontFamily: attributes.fontFamily, |
| 38 |
previousFontSize: attributes.fontSize, |
| 39 |
previousHeaderType: attributes.headerType, |
| 40 |
previousClampFonts: attributes.clampFonts, |
| 41 |
})); |
| 42 |
}, |
| 43 |
}), |
| 44 |
{ name: 'Code Block Pro Theme Settings' }, |
| 45 |
), |
| 46 |
{ |
| 47 |
name: 'code_block_pro_settings', |
| 48 |
getStorage: () => ({ |
| 49 |
getItem: async (name: string) => { |
| 50 |
const settings = await getSettings(name); |
| 51 |
return JSON.stringify({ |
| 52 |
version: settings.version, |
| 53 |
state: settings, |
| 54 |
}); |
| 55 |
}, |
| 56 |
setItem: async (name: string, value: string) => { |
| 57 |
const { state, version } = JSON.parse(value); |
| 58 |
const data = { |
| 59 |
[name]: Object.assign( |
| 60 |
(await getSettings(name)) ?? {}, |
| 61 |
state, |
| 62 |
version, |
| 63 |
), |
| 64 |
}; |
| 65 |
await apiFetch({ path, method: 'POST', data }); |
| 66 |
}, |
| 67 |
removeItem: async (name: string) => { |
| 68 |
const data = { [name]: null }; |
| 69 |
return await apiFetch({ path, method: 'POST', data }); |
| 70 |
}, |
| 71 |
}), |
| 72 |
}, |
| 73 |
), |
| 74 |
); |
| 75 |
|