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