Skip to content

Commit

Permalink
Merge pull request #64 from KevinBatdorf/fix-global-setting-bug
Browse files Browse the repository at this point in the history
Run defaults check only once
  • Loading branch information
KevinBatdorf committed Nov 17, 2022
2 parents d6a6564 + 0b529d0 commit cbb86e2
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/hooks/useDefaults.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect } from '@wordpress/element';
import { useEffect, useRef } from '@wordpress/element';
import { Theme } from 'shiki';
import { useGlobalStore } from '../state/global';
import { useThemeStore } from '../state/theme';
Expand Down Expand Up @@ -32,51 +32,61 @@ export const useDefaults = ({
previousDisablePadding,
previousLineNumbers,
} = useThemeStore();
const once = useRef(false);

useEffect(() => {
if (once.current) return;
if (copyButton !== undefined || !previousSettings.copyButton) return;
setAttributes({ copyButton: previousSettings.copyButton });
}, [previousSettings, copyButton, setAttributes]);

useEffect(() => {
if (once.current) return;
if (theme || !previousTheme) return;
setAttributes({ theme: previousTheme as Theme });
}, [previousTheme, theme, setAttributes]);

useEffect(() => {
if (once.current) return;
if (fontSize || !previousFontSize) return;
setAttributes({ fontSize: previousFontSize });
}, [previousFontSize, fontSize, setAttributes]);

useEffect(() => {
if (once.current) return;
if (fontFamily || fontFamily === '' || previousFontFamily === undefined)
return;
setAttributes({ fontFamily: previousFontFamily });
}, [previousFontFamily, fontFamily, setAttributes]);

useEffect(() => {
if (once.current) return;
if (lineHeight || !previousLineHeight) return;
setAttributes({ lineHeight: previousLineHeight });
}, [previousLineHeight, lineHeight, setAttributes]);

useEffect(() => {
if (once.current) return;
if (headerType || !previousHeaderType) return;
setAttributes({ headerType: previousHeaderType });
}, [previousHeaderType, headerType, setAttributes]);

useEffect(() => {
if (once.current) return;
if (footerType !== undefined || previousFooterType === undefined)
return;
setAttributes({ footerType: previousFooterType });
}, [previousFooterType, footerType, setAttributes]);

useEffect(() => {
if (once.current) return;
if (clampFonts !== undefined || previousClampFonts === undefined)
return;
setAttributes({ clampFonts: previousClampFonts });
}, [previousClampFonts, clampFonts, setAttributes]);

useEffect(() => {
if (once.current) return;
if (
disablePadding !== undefined ||
previousDisablePadding === undefined
Expand All @@ -86,8 +96,15 @@ export const useDefaults = ({
}, [previousDisablePadding, disablePadding, setAttributes]);

useEffect(() => {
if (once.current) return;
if (lineNumbers !== undefined || previousLineNumbers === undefined)
return;
setAttributes({ lineNumbers: previousLineNumbers });
}, [previousLineNumbers, lineNumbers, setAttributes]);

useEffect(() => {
requestAnimationFrame(() => {
once.current = true;
});
}, []);
};

0 comments on commit cbb86e2

Please sign in to comment.