PluginProbe
Code Block Pro – Beautiful Syntax Highlighting / 1.13.0
Code Block Pro – Beautiful Syntax Highlighting v1.13.0
1.27.1 1.27.2 1.27.3 1.27.4 1.27.5 1.27.6 1.27.7 1.28.0 1.3.0 1.4.0 1.5.0 1.5.1 1.5.2 1.6.0 1.7.0 1.8.0 1.9.0 1.9.1 1.9.2 1.9.3 trunk 1.1.0 1.10.0 1.11.0 1.11.1 All 63 releases
code-block-pro / src / state / theme.ts

theme.ts in Code Block Pro – Beautiful Syntax Highlighting 1.13.0, at src/state/theme.ts

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