import type { CSSProperties, ContextType, Dispatch, FC, SetStateAction } from 'react'; import { createContext, useCallback, useContext, useEffect, useMemo, useRef, useState } from 'react'; import { ColorPicker } from '../ColorPicker'; import { usePageStoreContext } from '../PageStore'; const StoreContext = createContext( {} as { styles: CSSProperties; dispatchStyles: (input: CSSProperties) => void; boxBackground: string; dispatchBoxBackground: Dispatch>; }, ); type StoreType = ContextType; const useStoreContext = () => { return useContext(StoreContext); }; const SvgRender: FC = () => { const { svgComponent, svgProps, dispatchSvgElement } = usePageStoreContext(); const { styles } = useStoreContext(); const ref = useRef(null); useEffect(() => { const dom = ref.current?.firstElementChild as HTMLElement; if (dom && svgComponent?.name) { dispatchSvgElement(dom); } }, [dispatchSvgElement, svgComponent?.name]); const SvgComponent = svgComponent?.component; if (!SvgComponent) { return null; } return (
); }; const FontSize: FC = () => { const { styles, dispatchStyles } = useStoreContext(); const size = useMemo(() => { return (styles.fontSize as string).replace('px', ''); }, [styles.fontSize]); return (
size:  { dispatchStyles({ fontSize: `${e.target.value}px`, }); }} />
); }; const Background: FC = () => { const { boxBackground, dispatchBoxBackground } = useStoreContext(); return (
background: 
); }; export const Preview: FC = () => { const [styles, setStyles] = useState({ fontSize: '200px', }); const dispatchStyles: StoreType['dispatchStyles'] = useCallback((input) => { setStyles((prev) => { return { ...prev, ...input, }; }); }, []); const [boxBackground, dispatchBoxBackground] = useState('#fff'); return (
); };