| 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 |
|