| @@ -1,8 +1,7 @@ | ||
| 1 | 1 | import { useEffect, useRef } from '@wordpress/element'; |
| 2 | 2 | import { Theme } from 'shiki'; |
| 3 | -import { useGlobalStore } from '../state/global'; | |
| 4 | -import { useThemeStore } from '../state/theme'; | |
| 3 | +import { useThemeStore, useThemeStoreReady } from '../state/theme'; | |
| 5 | 4 | import { AttributesPropsAndSetter } from '../types'; |
| 6 | 5 | |
| 7 | 6 | export const useDefaults = ({ |
| 8 | 7 | attributes, |
| @@ -13,15 +12,23 @@ | ||
| 13 | 12 | fontSize, |
| 14 | 13 | fontFamily, |
| 15 | 14 | lineHeight, |
| 16 | 15 | copyButton, |
| 16 | + copyButtonType, | |
| 17 | + copyButtonString, | |
| 18 | + copyButtonStringCopied, | |
| 17 | 19 | headerType, |
| 18 | 20 | footerType, |
| 19 | 21 | clampFonts, |
| 20 | 22 | disablePadding, |
| 21 | 23 | lineNumbers, |
| 24 | + highlightingHover, | |
| 25 | + tabSize, | |
| 26 | + useTabs, | |
| 27 | + seeMoreType, | |
| 28 | + seeMoreString, | |
| 29 | + seeMoreTransition, | |
| 22 | 30 | } = attributes; |
| 23 | - const { previousSettings } = useGlobalStore(); | |
| 24 | 31 | const { |
| 25 | 32 | previousTheme, |
| 26 | 33 | previousFontSize, |
| 27 | 34 | previousFontFamily, |
| @@ -30,81 +37,143 @@ | ||
| 30 | 37 | previousFooterType, |
| 31 | 38 | previousClampFonts, |
| 32 | 39 | previousDisablePadding, |
| 33 | 40 | previousLineNumbers, |
| 41 | + previousHighlightingHover, | |
| 42 | + previousCopyButton, | |
| 43 | + previousCopyButtonType, | |
| 44 | + previousCopyButtonString, | |
| 45 | + previousCopyButtonStringCopied, | |
| 46 | + previousTabSize, | |
| 47 | + previousUseTabs, | |
| 48 | + previousSeeMoreType, | |
| 49 | + previousSeeMoreString, | |
| 50 | + previousSeeMoreTransition, | |
| 34 | 51 | } = useThemeStore(); |
| 52 | + const ready = useThemeStoreReady(); | |
| 35 | 53 | const once = useRef(false); |
| 36 | 54 | |
| 37 | 55 | useEffect(() => { |
| 38 | 56 | if (once.current) return; |
| 39 | - if (copyButton !== undefined || !previousSettings.copyButton) return; | |
| 40 | - setAttributes({ copyButton: previousSettings.copyButton }); | |
| 41 | - }, [previousSettings, copyButton, setAttributes]); | |
| 57 | + if (copyButton !== undefined || !previousCopyButton) return; | |
| 58 | + setAttributes({ copyButton: previousCopyButton }); | |
| 59 | + }, [previousCopyButton, copyButton, setAttributes]); | |
| 42 | 60 | |
| 43 | 61 | useEffect(() => { |
| 44 | 62 | if (once.current) return; |
| 45 | - if (theme || !previousTheme) return; | |
| 63 | + if (copyButtonType) return; | |
| 64 | + setAttributes({ copyButtonType: previousCopyButtonType }); | |
| 65 | + }, [previousCopyButtonType, copyButtonType, setAttributes]); | |
| 66 | + | |
| 67 | + useEffect(() => { | |
| 68 | + if (once.current) return; | |
| 69 | + // Checks undefined to avoid invalidating old blocks | |
| 70 | + if (copyButtonString || copyButtonString === undefined) return; | |
| 71 | + setAttributes({ copyButtonString: previousCopyButtonString }); | |
| 72 | + }, [previousCopyButtonString, copyButtonString, setAttributes]); | |
| 73 | + | |
| 74 | + useEffect(() => { | |
| 75 | + if (once.current) return; | |
| 76 | + // Checks undefined to avoid invalidating old blocks | |
| 77 | + if (copyButtonStringCopied || copyButtonStringCopied === undefined) { | |
| 78 | + return; | |
| 79 | + } | |
| 80 | + const cpyP = previousCopyButtonStringCopied; | |
| 81 | + setAttributes({ copyButtonStringCopied: cpyP }); | |
| 82 | + }, [previousCopyButtonStringCopied, copyButtonStringCopied, setAttributes]); | |
| 83 | + | |
| 84 | + useEffect(() => { | |
| 85 | + if (once.current) return; | |
| 86 | + if (theme) return; | |
| 46 | 87 | setAttributes({ theme: previousTheme as Theme }); |
| 47 | 88 | }, [previousTheme, theme, setAttributes]); |
| 48 | 89 | |
| 49 | 90 | useEffect(() => { |
| 50 | 91 | if (once.current) return; |
| 51 | - if (fontSize || !previousFontSize) return; | |
| 92 | + if (fontSize) return; | |
| 52 | 93 | setAttributes({ fontSize: previousFontSize }); |
| 53 | 94 | }, [previousFontSize, fontSize, setAttributes]); |
| 54 | 95 | |
| 55 | 96 | useEffect(() => { |
| 56 | 97 | if (once.current) return; |
| 57 | - if (fontFamily || fontFamily === '' || previousFontFamily === undefined) | |
| 58 | - return; | |
| 98 | + if (fontFamily || fontFamily === '') return; | |
| 59 | 99 | setAttributes({ fontFamily: previousFontFamily }); |
| 60 | 100 | }, [previousFontFamily, fontFamily, setAttributes]); |
| 61 | 101 | |
| 62 | 102 | useEffect(() => { |
| 63 | 103 | if (once.current) return; |
| 64 | - if (lineHeight || !previousLineHeight) return; | |
| 104 | + if (lineHeight) return; | |
| 65 | 105 | setAttributes({ lineHeight: previousLineHeight }); |
| 66 | 106 | }, [previousLineHeight, lineHeight, setAttributes]); |
| 67 | 107 | |
| 68 | 108 | useEffect(() => { |
| 69 | 109 | if (once.current) return; |
| 70 | - if (headerType || !previousHeaderType) return; | |
| 110 | + if (headerType) return; | |
| 71 | 111 | setAttributes({ headerType: previousHeaderType }); |
| 72 | 112 | }, [previousHeaderType, headerType, setAttributes]); |
| 73 | 113 | |
| 74 | 114 | useEffect(() => { |
| 75 | 115 | if (once.current) return; |
| 76 | - if (footerType !== undefined || previousFooterType === undefined) | |
| 77 | - return; | |
| 116 | + if (footerType !== undefined) return; | |
| 78 | 117 | setAttributes({ footerType: previousFooterType }); |
| 79 | 118 | }, [previousFooterType, footerType, setAttributes]); |
| 80 | 119 | |
| 81 | 120 | useEffect(() => { |
| 82 | 121 | if (once.current) return; |
| 83 | - if (clampFonts !== undefined || previousClampFonts === undefined) | |
| 84 | - return; | |
| 122 | + if (clampFonts !== undefined) return; | |
| 85 | 123 | setAttributes({ clampFonts: previousClampFonts }); |
| 86 | 124 | }, [previousClampFonts, clampFonts, setAttributes]); |
| 87 | 125 | |
| 88 | 126 | useEffect(() => { |
| 89 | 127 | if (once.current) return; |
| 90 | - if ( | |
| 91 | - disablePadding !== undefined || | |
| 92 | - previousDisablePadding === undefined | |
| 93 | - ) | |
| 94 | - return; | |
| 128 | + if (disablePadding !== undefined) return; | |
| 95 | 129 | setAttributes({ disablePadding: previousDisablePadding }); |
| 96 | 130 | }, [previousDisablePadding, disablePadding, setAttributes]); |
| 97 | 131 | |
| 98 | 132 | useEffect(() => { |
| 99 | 133 | if (once.current) return; |
| 100 | - if (lineNumbers !== undefined || previousLineNumbers === undefined) | |
| 101 | - return; | |
| 134 | + if (lineNumbers !== undefined) return; | |
| 102 | 135 | setAttributes({ lineNumbers: previousLineNumbers }); |
| 103 | 136 | }, [previousLineNumbers, lineNumbers, setAttributes]); |
| 104 | 137 | |
| 105 | 138 | useEffect(() => { |
| 139 | + if (once.current) return; | |
| 140 | + if (tabSize !== undefined) return; | |
| 141 | + setAttributes({ tabSize: previousTabSize }); | |
| 142 | + }, [previousTabSize, tabSize, setAttributes]); | |
| 143 | + | |
| 144 | + useEffect(() => { | |
| 145 | + if (once.current) return; | |
| 146 | + if (useTabs !== undefined) return; | |
| 147 | + setAttributes({ useTabs: previousUseTabs }); | |
| 148 | + }, [previousUseTabs, useTabs, setAttributes]); | |
| 149 | + | |
| 150 | + useEffect(() => { | |
| 151 | + if (once.current) return; | |
| 152 | + if (seeMoreType !== undefined) return; | |
| 153 | + setAttributes({ seeMoreType: previousSeeMoreType }); | |
| 154 | + }, [previousSeeMoreType, seeMoreType, setAttributes]); | |
| 155 | + | |
| 156 | + useEffect(() => { | |
| 157 | + if (once.current) return; | |
| 158 | + if (seeMoreString !== undefined) return; | |
| 159 | + setAttributes({ seeMoreString: previousSeeMoreString }); | |
| 160 | + }, [previousSeeMoreString, seeMoreString, setAttributes]); | |
| 161 | + | |
| 162 | + useEffect(() => { | |
| 163 | + if (once.current) return; | |
| 164 | + if (seeMoreTransition !== undefined) return; | |
| 165 | + setAttributes({ seeMoreTransition: previousSeeMoreTransition }); | |
| 166 | + }, [previousSeeMoreTransition, seeMoreTransition, setAttributes]); | |
| 167 | + | |
| 168 | + useEffect(() => { | |
| 169 | + if (once.current) return; | |
| 170 | + if (highlightingHover !== undefined) return; | |
| 171 | + setAttributes({ highlightingHover: previousHighlightingHover }); | |
| 172 | + }, [previousHighlightingHover, highlightingHover, setAttributes]); | |
| 173 | + | |
| 174 | + useEffect(() => { | |
| 106 | 175 | requestAnimationFrame(() => { |
| 107 | 176 | once.current = true; |
| 108 | 177 | }); |
| 109 | - }, []); | |
| 178 | + }, [ready]); | |
| 110 | 179 | }; |