PluginProbe
Code Block Pro – Beautiful Syntax Highlighting / 1.18.0
Code Block Pro – Beautiful Syntax Highlighting v1.18.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 / hooks / useDefaults.ts

useDefaults.ts in Code Block Pro – Beautiful Syntax Highlighting 1.18.0, at src/hooks/useDefaults.ts

131 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { useEffect, useRef } from '@wordpress/element';
2 import { Theme } from 'shiki';
3 import { useThemeStore, useThemeStoreReady } from '../state/theme';
4 import { AttributesPropsAndSetter } from '../types';
5
6 export const useDefaults = ({
7 attributes,
8 setAttributes,
9 }: AttributesPropsAndSetter) => {
10 const {
11 theme,
12 fontSize,
13 fontFamily,
14 lineHeight,
15 copyButton,
16 buttonTheme,
17 headerType,
18 footerType,
19 clampFonts,
20 disablePadding,
21 lineNumbers,
22 highlightingHover,
23 } = attributes;
24 const {
25 previousTheme,
26 previousFontSize,
27 previousFontFamily,
28 previousLineHeight,
29 previousHeaderType,
30 previousFooterType,
31 previousClampFonts,
32 previousDisablePadding,
33 previousLineNumbers,
34 previousHighlightingHover,
35 previousCopyButton,
36 previousButtonTheme,
37 } = useThemeStore();
38 const ready = useThemeStoreReady();
39 const once = useRef(false);
40
41 useEffect(() => {
42 if (once.current) return;
43 if (copyButton !== undefined || !previousCopyButton) return;
44 setAttributes({ copyButton: previousCopyButton });
45 }, [previousCopyButton, copyButton, setAttributes]);
46
47 useEffect(() => {
48 if (once.current) return;
49 if (buttonTheme || !previousButtonTheme) return;
50 setAttributes({ buttonTheme: previousButtonTheme });
51 }, [previousButtonTheme, buttonTheme, setAttributes]);
52
53 useEffect(() => {
54 if (once.current) return;
55 if (theme || !previousTheme) return;
56 setAttributes({ theme: previousTheme as Theme });
57 }, [previousTheme, theme, setAttributes]);
58
59 useEffect(() => {
60 if (once.current) return;
61 if (fontSize || !previousFontSize) return;
62 setAttributes({ fontSize: previousFontSize });
63 }, [previousFontSize, fontSize, setAttributes]);
64
65 useEffect(() => {
66 if (once.current) return;
67 if (fontFamily || fontFamily === '' || previousFontFamily === undefined)
68 return;
69 setAttributes({ fontFamily: previousFontFamily });
70 }, [previousFontFamily, fontFamily, setAttributes]);
71
72 useEffect(() => {
73 if (once.current) return;
74 if (lineHeight || !previousLineHeight) return;
75 setAttributes({ lineHeight: previousLineHeight });
76 }, [previousLineHeight, lineHeight, setAttributes]);
77
78 useEffect(() => {
79 if (once.current) return;
80 if (headerType || !previousHeaderType) return;
81 setAttributes({ headerType: previousHeaderType });
82 }, [previousHeaderType, headerType, setAttributes]);
83
84 useEffect(() => {
85 if (once.current) return;
86 if (footerType !== undefined || previousFooterType === undefined)
87 return;
88 setAttributes({ footerType: previousFooterType });
89 }, [previousFooterType, footerType, setAttributes]);
90
91 useEffect(() => {
92 if (once.current) return;
93 if (clampFonts !== undefined || previousClampFonts === undefined)
94 return;
95 setAttributes({ clampFonts: previousClampFonts });
96 }, [previousClampFonts, clampFonts, setAttributes]);
97
98 useEffect(() => {
99 if (once.current) return;
100 if (
101 disablePadding !== undefined ||
102 previousDisablePadding === undefined
103 )
104 return;
105 setAttributes({ disablePadding: previousDisablePadding });
106 }, [previousDisablePadding, disablePadding, setAttributes]);
107
108 useEffect(() => {
109 if (once.current) return;
110 if (lineNumbers !== undefined || previousLineNumbers === undefined)
111 return;
112 setAttributes({ lineNumbers: previousLineNumbers });
113 }, [previousLineNumbers, lineNumbers, setAttributes]);
114
115 useEffect(() => {
116 if (once.current) return;
117 if (
118 highlightingHover !== undefined ||
119 previousHighlightingHover === undefined
120 )
121 return;
122 setAttributes({ highlightingHover: previousHighlightingHover });
123 }, [previousHighlightingHover, highlightingHover, setAttributes]);
124
125 useEffect(() => {
126 requestAnimationFrame(() => {
127 once.current = true;
128 });
129 }, [ready]);
130 };
131