| 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 |
previousFooterType?: string; |
| 13 |
previousClampFonts?: boolean; |
| 14 |
previousDisablePadding?: boolean; |
| 15 |
previousLineNumbers?: boolean; |
| 16 |
updateThemeHistory: (settings: Partial<Attributes>) => void; |
| 17 |
}; |
| 18 |
const path = '/wp/v2/settings'; |
| 19 |
const getSettings = async (name: string) => { |
| 20 |
const allSettings = await apiFetch({ path }); |
| 21 |
// eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 22 |
// @ts-ignore-next-line |
| 23 |
return allSettings?.[name]; |
| 24 |
}; |
| 25 |
export const useThemeStore = create<ThemeType>()( |
| 26 |
persist( |
| 27 |
devtools( |
| 28 |
(set) => ({ |
| 29 |
previousTheme: 'nord', |
| 30 |
previousLineHeight: '1.25rem', |
| 31 |
previousFontFamily: undefined, |
| 32 |
previousFontSize: '.875rem', |
| 33 |
previousHeaderType: 'headlights', |
| 34 |
previousFooterType: undefined, |
| 35 |
previousClampFonts: undefined, |
| 36 |
previousDisablePadding: undefined, |
| 37 |
previousLineNumbers: undefined, |
| 38 |
updateThemeHistory(attributes) { |
| 39 |
set((state) => ({ |
| 40 |
...state, |
| 41 |
previousTheme: attributes.theme, |
| 42 |
previousLineHeight: attributes.lineHeight, |
| 43 |
previousFontFamily: attributes.fontFamily, |
| 44 |
previousFontSize: attributes.fontSize, |
| 45 |
previousHeaderType: attributes.headerType, |
| 46 |
previousFooterType: attributes.footerType, |
| 47 |
previousClampFonts: attributes.clampFonts, |
| 48 |
previousDisablePadding: attributes.disablePadding, |
| 49 |
previousLineNumbers: attributes.lineNumbers, |
| 50 |
})); |
| 51 |
}, |
| 52 |
}), |
| 53 |
{ name: 'Code Block Pro Theme Settings' }, |
| 54 |
), |
| 55 |
{ |
| 56 |
name: 'code_block_pro_settings', |
| 57 |
getStorage: () => ({ |
| 58 |
getItem: async (name: string) => { |
| 59 |
const settings = await getSettings(name); |
| 60 |
return JSON.stringify({ |
| 61 |
version: settings?.version ?? 0, |
| 62 |
state: settings, |
| 63 |
}); |
| 64 |
}, |
| 65 |
setItem: async (name: string, value: string) => { |
| 66 |
const { state, version } = JSON.parse(value); |
| 67 |
const data = { |
| 68 |
[name]: Object.assign( |
| 69 |
(await getSettings(name)) ?? {}, |
| 70 |
state, |
| 71 |
version, |
| 72 |
), |
| 73 |
}; |
| 74 |
await apiFetch({ path, method: 'POST', data }); |
| 75 |
}, |
| 76 |
removeItem: async (name: string) => { |
| 77 |
const data = { [name]: null }; |
| 78 |
return await apiFetch({ path, method: 'POST', data }); |
| 79 |
}, |
| 80 |
}), |
| 81 |
}, |
| 82 |
), |
| 83 |
); |
| 84 |
|