| 1 |
import { useEffect } 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 |
clampFonts, |
| 19 |
disablePadding, |
| 20 |
lineNumbers, |
| 21 |
} = attributes; |
| 22 |
const { previousSettings } = useGlobalStore(); |
| 23 |
const { |
| 24 |
previousTheme, |
| 25 |
previousFontSize, |
| 26 |
previousFontFamily, |
| 27 |
previousLineHeight, |
| 28 |
previousHeaderType, |
| 29 |
previousClampFonts, |
| 30 |
previousDisablePadding, |
| 31 |
previousLineNumbers, |
| 32 |
} = useThemeStore(); |
| 33 |
|
| 34 |
useEffect(() => { |
| 35 |
if (copyButton || !previousSettings.copyButton) return; |
| 36 |
setAttributes({ copyButton: previousSettings.copyButton }); |
| 37 |
}, [previousSettings, copyButton, setAttributes]); |
| 38 |
|
| 39 |
useEffect(() => { |
| 40 |
if (theme || !previousTheme) return; |
| 41 |
setAttributes({ theme: previousTheme as Theme }); |
| 42 |
}, [previousTheme, theme, setAttributes]); |
| 43 |
|
| 44 |
useEffect(() => { |
| 45 |
if (fontSize || !previousFontSize) return; |
| 46 |
setAttributes({ fontSize: previousFontSize }); |
| 47 |
}, [previousFontSize, fontSize, setAttributes]); |
| 48 |
|
| 49 |
useEffect(() => { |
| 50 |
if (fontFamily || !previousFontFamily) return; |
| 51 |
setAttributes({ fontFamily: previousFontFamily }); |
| 52 |
}, [previousFontFamily, fontFamily, setAttributes]); |
| 53 |
|
| 54 |
useEffect(() => { |
| 55 |
if (lineHeight || !previousLineHeight) return; |
| 56 |
setAttributes({ lineHeight: previousLineHeight }); |
| 57 |
}, [previousLineHeight, lineHeight, setAttributes]); |
| 58 |
|
| 59 |
useEffect(() => { |
| 60 |
if (headerType || !previousHeaderType) return; |
| 61 |
setAttributes({ headerType: previousHeaderType }); |
| 62 |
}, [previousHeaderType, headerType, setAttributes]); |
| 63 |
|
| 64 |
useEffect(() => { |
| 65 |
if (clampFonts || !previousClampFonts) return; |
| 66 |
setAttributes({ clampFonts: previousClampFonts }); |
| 67 |
}, [previousClampFonts, clampFonts, setAttributes]); |
| 68 |
|
| 69 |
useEffect(() => { |
| 70 |
if (disablePadding || !previousDisablePadding) return; |
| 71 |
setAttributes({ disablePadding: previousDisablePadding }); |
| 72 |
}, [previousDisablePadding, disablePadding, setAttributes]); |
| 73 |
|
| 74 |
useEffect(() => { |
| 75 |
if (lineNumbers || !previousLineNumbers) return; |
| 76 |
setAttributes({ lineNumbers: previousLineNumbers }); |
| 77 |
}, [previousLineNumbers, lineNumbers, setAttributes]); |
| 78 |
}; |
| 79 |
|