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 / hooks / useDefaults.ts

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

111 lines 3.5 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 { useGlobalStore } from '../state/global';
4 import { useThemeStore } from '../state/theme';
5 import { AttributesPropsAndSetter } from '../types';
6
7 export const useDefaults = ({
8 attributes,
9 setAttributes,
10 }: AttributesPropsAndSetter) => {
11 const {
12 theme,
13 fontSize,
14 fontFamily,
15 lineHeight,
16 copyButton,
17 headerType,
18 footerType,
19 clampFonts,
20 disablePadding,
21 lineNumbers,
22 } = attributes;
23 const { previousSettings } = useGlobalStore();
24 const {
25 previousTheme,
26 previousFontSize,
27 previousFontFamily,
28 previousLineHeight,
29 previousHeaderType,
30 previousFooterType,
31 previousClampFonts,
32 previousDisablePadding,
33 previousLineNumbers,
34 } = useThemeStore();
35 const once = useRef(false);
36
37 useEffect(() => {
38 if (once.current) return;
39 if (copyButton !== undefined || !previousSettings.copyButton) return;
40 setAttributes({ copyButton: previousSettings.copyButton });
41 }, [previousSettings, copyButton, setAttributes]);
42
43 useEffect(() => {
44 if (once.current) return;
45 if (theme || !previousTheme) return;
46 setAttributes({ theme: previousTheme as Theme });
47 }, [previousTheme, theme, setAttributes]);
48
49 useEffect(() => {
50 if (once.current) return;
51 if (fontSize || !previousFontSize) return;
52 setAttributes({ fontSize: previousFontSize });
53 }, [previousFontSize, fontSize, setAttributes]);
54
55 useEffect(() => {
56 if (once.current) return;
57 if (fontFamily || fontFamily === '' || previousFontFamily === undefined)
58 return;
59 setAttributes({ fontFamily: previousFontFamily });
60 }, [previousFontFamily, fontFamily, setAttributes]);
61
62 useEffect(() => {
63 if (once.current) return;
64 if (lineHeight || !previousLineHeight) return;
65 setAttributes({ lineHeight: previousLineHeight });
66 }, [previousLineHeight, lineHeight, setAttributes]);
67
68 useEffect(() => {
69 if (once.current) return;
70 if (headerType || !previousHeaderType) return;
71 setAttributes({ headerType: previousHeaderType });
72 }, [previousHeaderType, headerType, setAttributes]);
73
74 useEffect(() => {
75 if (once.current) return;
76 if (footerType !== undefined || previousFooterType === undefined)
77 return;
78 setAttributes({ footerType: previousFooterType });
79 }, [previousFooterType, footerType, setAttributes]);
80
81 useEffect(() => {
82 if (once.current) return;
83 if (clampFonts !== undefined || previousClampFonts === undefined)
84 return;
85 setAttributes({ clampFonts: previousClampFonts });
86 }, [previousClampFonts, clampFonts, setAttributes]);
87
88 useEffect(() => {
89 if (once.current) return;
90 if (
91 disablePadding !== undefined ||
92 previousDisablePadding === undefined
93 )
94 return;
95 setAttributes({ disablePadding: previousDisablePadding });
96 }, [previousDisablePadding, disablePadding, setAttributes]);
97
98 useEffect(() => {
99 if (once.current) return;
100 if (lineNumbers !== undefined || previousLineNumbers === undefined)
101 return;
102 setAttributes({ lineNumbers: previousLineNumbers });
103 }, [previousLineNumbers, lineNumbers, setAttributes]);
104
105 useEffect(() => {
106 requestAnimationFrame(() => {
107 once.current = true;
108 });
109 }, []);
110 };
111