import React, { useCallback, useEffect, useRef, useState } from 'react'; import { useAppDispatch } from '$app/stores/store'; import { useSubscribeDocument } from '$app/components/document/_shared/SubscribeDoc.hooks'; import { Align } from '$app/interfaces/document'; import { FormatAlignCenter, FormatAlignLeft, FormatAlignRight } from '@mui/icons-material'; import { updateNodeDataThunk } from '$app_reducers/document/async-actions'; import MenuTooltip from '$app/components/document/TextActionMenu/menu/MenuTooltip'; import Popover from '@mui/material/Popover'; function ImageAlign({ id, align, onOpen, onClose, }: { id: string; align: Align; onOpen: () => void; onClose: () => void; }) { const ref = useRef(null); const [anchorEl, setAnchorEl] = useState(); const popoverOpen = Boolean(anchorEl); useEffect(() => { if (popoverOpen) { onOpen(); } else { onClose(); } }, [onClose, onOpen, popoverOpen]); const dispatch = useAppDispatch(); const { controller } = useSubscribeDocument(); const renderAlign = (align: Align) => { switch (align) { case Align.Left: return ; case Align.Center: return ; default: return ; } }; const updateAlign = useCallback( (align: Align) => { dispatch( updateNodeDataThunk({ id, data: { align, }, controller, }) ); setAnchorEl(undefined); }, [controller, dispatch, id] ); return ( <>
{ ref.current && setAnchorEl(ref.current); }} > {renderAlign(align)}
e.stopPropagation()} anchorEl={anchorEl} onClose={() => setAnchorEl(undefined)} PaperProps={{ style: { backgroundColor: '#1E1E1E', opacity: 0.8, }, }} >
{[Align.Left, Align.Center, Align.Right].map((item: Align) => { return (
{ updateAlign(item); }} > {renderAlign(item)}
); })}
); } export default ImageAlign;