import * as _ContextMenu from '@radix-ui/react-context-menu' import styled from 'styles' import { IconWrapper, breakpoints, RowButton, ContextMenuArrow, ContextMenuDivider, ContextMenuButton, ContextMenuSubMenu, ContextMenuIconButton, ContextMenuRoot, MenuContent, } from 'components/shared' import { commandKey, deepCompareArrays, isMobile } from 'utils' import state, { useSelector } from 'state' import { AlignType, DistributeType, MoveType, ShapeType, StretchType, } from 'types' import tld from 'utils/tld' import React, { useRef } from 'react' import { ChevronRightIcon, AlignBottomIcon, AlignCenterHorizontallyIcon, AlignCenterVerticallyIcon, AlignLeftIcon, AlignRightIcon, AlignTopIcon, SpaceEvenlyHorizontallyIcon, SpaceEvenlyVerticallyIcon, StretchHorizontallyIcon, StretchVerticallyIcon, } from '@radix-ui/react-icons' function alignTop() { state.send('ALIGNED', { type: AlignType.Top }) } function alignCenterVertical() { state.send('ALIGNED', { type: AlignType.CenterVertical }) } function alignBottom() { state.send('ALIGNED', { type: AlignType.Bottom }) } function stretchVertically() { state.send('STRETCHED', { type: StretchType.Vertical }) } function distributeVertically() { state.send('DISTRIBUTED', { type: DistributeType.Vertical }) } function alignLeft() { state.send('ALIGNED', { type: AlignType.Left }) } function alignCenterHorizontal() { state.send('ALIGNED', { type: AlignType.CenterHorizontal }) } function alignRight() { state.send('ALIGNED', { type: AlignType.Right }) } function stretchHorizontally() { state.send('STRETCHED', { type: StretchType.Horizontal }) } function distributeHorizontally() { state.send('DISTRIBUTED', { type: DistributeType.Horizontal }) } export default function ContextMenu({ children, }: { children: React.ReactNode }): JSX.Element { const selectedShapeIds = useSelector( (s) => s.values.selectedIds, deepCompareArrays ) const rContent = useRef(null) const hasGroupSelected = useSelector((s) => selectedShapeIds.some( (id) => tld.getShape(s.data, id)?.type === ShapeType.Group ) ) const hasTwoOrMore = selectedShapeIds.length > 1 const hasThreeOrMore = selectedShapeIds.length > 2 return ( <_ContextMenu.Trigger>{children} {selectedShapeIds.length ? ( <> {/* state.send('COPIED')}> Copy {commandKey()} C state.send('CUT')}> Cut {commandKey()} X */} state.send('DUPLICATED')}> Duplicate {commandKey()} D {hasGroupSelected || (hasTwoOrMore && ( <> {hasGroupSelected && ( state.send('UNGROUPED')}> Ungroup {commandKey()} G )} {hasTwoOrMore && ( state.send('GROUPED')}> Group {commandKey()} G )} ))} state.send('MOVED', { type: MoveType.ToFront, }) } > To Front {commandKey()} ] state.send('MOVED', { type: MoveType.Forward, }) } > Forward {commandKey()} ] state.send('MOVED', { type: MoveType.Backward, }) } > Backward {commandKey()} [ state.send('MOVED', { type: MoveType.ToBack, }) } > To Back {commandKey()} [ {hasTwoOrMore && ( )} state.send('COPIED_TO_SVG')}> Copy to SVG {commandKey()} C state.send('DELETED')}> Delete ) : ( <> state.send('UNDO')}> Undo {commandKey()} Z state.send('REDO')}> Redo {commandKey()} Z )} ) } function AlignDistributeSubMenu({ hasThreeOrMore, }: { hasTwoOrMore: boolean hasThreeOrMore: boolean }) { return ( <_ContextMenu.TriggerItem as={RowButton} bp={breakpoints}> Align / Distribute {hasThreeOrMore && ( )} {hasThreeOrMore && ( )} ) } const StyledGrid = styled(MenuContent, { display: 'grid', variants: { selectedStyle: { threeOrMore: { gridTemplateColumns: 'repeat(5, auto)', }, twoOrMore: { gridTemplateColumns: 'repeat(4, auto)', }, }, }, }) function MoveToPageMenu() { const documentPages = useSelector((s) => s.data.document.pages) const currentPageId = useSelector((s) => s.data.currentPageId) if (!documentPages[currentPageId]) return null const sorted = Object.values(documentPages) .sort((a, b) => a.childIndex - b.childIndex) .filter((a) => a.id !== currentPageId) if (sorted.length === 0) return null return ( Move To Page {sorted.map(({ id, name }) => ( state.send('MOVED_TO_PAGE', { id })} > {name} ))} ) }