PluginProbe
Code Block Pro – Beautiful Syntax Highlighting / 1.26.0
Code Block Pro – Beautiful Syntax Highlighting v1.26.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.26.0, at src/state/theme.ts

138 lines 4.8 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 { useEffect, useState } from '@wordpress/element';
3 import { __ } from '@wordpress/i18n';
4 import { create } from 'zustand';
5 import { createJSONStorage, devtools, persist } from 'zustand/middleware';
6 import { Attributes } from '../types';
7
8 type ThemeType = {
9 previousTheme: string;
10 previousLineHeight: string;
11 previousFontFamily?: string;
12 previousFontSize: string;
13 previousHeaderType: string;
14 previousFooterType?: string;
15 previousClampFonts?: boolean;
16 previousDisablePadding?: boolean;
17 previousLineNumbers?: boolean;
18 previousHighlightingHover?: boolean;
19 previousCopyButton: boolean;
20 previousCopyButtonType: string;
21 previousCopyButtonString?: string;
22 previousCopyButtonStringCopied?: string;
23 previousTabSize: number;
24 previousUseTabs?: boolean;
25 // previousEnableMaxHeight?: boolean;
26 // previousSeeMoreAfterLine?: string;
27 previousSeeMoreType?: string;
28 previousSeeMoreString?: string;
29 previousSeeMoreTransition?: boolean;
30 updateThemeHistory: (settings: Partial<Attributes>) => void;
31 };
32 const path = '/code-block-pro/v1/settings';
33 const getSettings = async (name: string) => {
34 const allSettings = await apiFetch({ path });
35 // eslint-disable-next-line @typescript-eslint/ban-ts-comment
36 // @ts-ignore-next-line
37 return allSettings?.[name];
38 };
39 const defaultSettings = {
40 previousTheme: 'nord',
41 previousLineHeight: '1.25rem',
42 previousFontFamily: 'Code-Pro-JetBrains-Mono',
43 previousFontSize: '.875rem',
44 previousHeaderType: 'headlights',
45 previousFooterType: 'none',
46 previousClampFonts: false,
47 previousDisablePadding: false,
48 previousLineNumbers: false,
49 previousHighlightingHover: false,
50 previousCopyButton: true,
51 previousCopyButtonType: 'heroicons',
52 previousCopyButtonString: __('Copy', 'code-block-pro'),
53 previusCopyButtonStringCopied: __('Copied', 'code-block-pro'),
54 previousTabSize: 2,
55 previousUseTabs: false,
56 // TODO: maybe impliment these with an extra UI to make them optional
57 // previousEnableMaxHeight: undefined,
58 // previousSeeMoreAfterLine: undefined,
59 previousSeeMoreType: '',
60 previousSeeMoreString: '',
61 previousSeeMoreTransition: false,
62 };
63 const storage = {
64 getItem: async (name: string) => {
65 const settings = await getSettings(name);
66 return JSON.stringify({
67 version: settings?.version ?? 0,
68 state: settings || defaultSettings,
69 });
70 },
71 setItem: async (name: string, value: string) => {
72 const { state, version } = JSON.parse(value);
73 const data = {
74 [name]: Object.assign(
75 (await getSettings(name)) || defaultSettings,
76 state,
77 version,
78 ),
79 };
80 await apiFetch({ path, method: 'POST', data });
81 },
82 removeItem: async (name: string) => {
83 const data = { [name]: null };
84 await apiFetch({ path, method: 'POST', data });
85 return undefined;
86 },
87 };
88
89 export const useThemeStore = create<ThemeType>()(
90 persist(
91 devtools(
92 (set) => ({
93 ...defaultSettings,
94 updateThemeHistory(attributes: Partial<Attributes>) {
95 set((state) => {
96 return Object.keys(attributes).reduce<
97 Partial<ThemeType>
98 >(
99 (acc, attr) => {
100 const att = attr as keyof Attributes;
101 const fmted = `previous${
102 attr.charAt(0).toUpperCase() + attr.slice(1)
103 }` as keyof ThemeType;
104 const keyExists =
105 Object.keys(state).includes(fmted);
106 return keyExists
107 ? { ...acc, [fmted]: attributes[att] }
108 : acc;
109 },
110 { ...state },
111 );
112 });
113 },
114 }),
115 { name: 'Code Block Pro Theme Settings' },
116 ),
117 {
118 name: 'code_block_pro_settings',
119 storage: createJSONStorage(() => storage),
120 },
121 ),
122 );
123
124 // const useThemeStoreReady =
125 /* Hook useful for when you need to wait on the async state to hydrate */
126 export const useThemeStoreReady = () => {
127 const [hydrated, setHydrated] = useState(useThemeStore.persist.hasHydrated);
128 useEffect(() => {
129 const unsubFinishHydration = useThemeStore.persist.onFinishHydration(
130 () => setHydrated(true),
131 );
132 return () => {
133 unsubFinishHydration();
134 };
135 }, []);
136 return hydrated;
137 };
138