2021-06-10 09:49:16 +00:00
|
|
|
import * as _ContextMenu from '@radix-ui/react-context-menu'
|
|
|
|
import styled from 'styles'
|
2021-06-17 12:03:08 +00:00
|
|
|
import {
|
|
|
|
IconWrapper,
|
|
|
|
IconButton as _IconButton,
|
|
|
|
RowButton,
|
|
|
|
} from 'components/shared'
|
2021-06-10 09:49:16 +00:00
|
|
|
import {
|
|
|
|
commandKey,
|
|
|
|
deepCompareArrays,
|
2021-06-27 09:07:20 +00:00
|
|
|
getSelectedIds,
|
|
|
|
getShape,
|
2021-06-10 09:49:16 +00:00
|
|
|
isMobile,
|
2021-06-27 09:07:20 +00:00
|
|
|
setToArray,
|
2021-06-24 08:18:14 +00:00
|
|
|
} from 'utils'
|
2021-06-10 09:49:16 +00:00
|
|
|
import state, { useSelector } from 'state'
|
2021-06-17 12:03:08 +00:00
|
|
|
import {
|
|
|
|
AlignType,
|
|
|
|
DistributeType,
|
|
|
|
MoveType,
|
|
|
|
ShapeType,
|
|
|
|
StretchType,
|
|
|
|
} from 'types'
|
2021-06-10 09:49:16 +00:00
|
|
|
import React, { useRef } from 'react'
|
2021-06-17 12:03:08 +00:00
|
|
|
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 })
|
|
|
|
}
|
2021-06-10 09:49:16 +00:00
|
|
|
|
|
|
|
export default function ContextMenu({
|
|
|
|
children,
|
|
|
|
}: {
|
|
|
|
children: React.ReactNode
|
2021-06-21 21:35:28 +00:00
|
|
|
}): JSX.Element {
|
2021-06-27 09:07:20 +00:00
|
|
|
const selectedShapeIds = useSelector(
|
|
|
|
(s) => setToArray(getSelectedIds(s.data)),
|
2021-06-10 09:49:16 +00:00
|
|
|
deepCompareArrays
|
|
|
|
)
|
|
|
|
|
|
|
|
const rContent = useRef<HTMLDivElement>(null)
|
|
|
|
|
2021-06-27 09:07:20 +00:00
|
|
|
const hasGroupSelected = useSelector((s) =>
|
|
|
|
selectedShapeIds.some((id) => getShape(s.data, id).type === ShapeType.Group)
|
|
|
|
)
|
|
|
|
|
|
|
|
const hasTwoOrMore = selectedShapeIds.length > 1
|
|
|
|
const hasThreeOrMore = selectedShapeIds.length > 2
|
2021-06-10 09:49:16 +00:00
|
|
|
|
|
|
|
return (
|
2021-06-27 19:19:57 +00:00
|
|
|
<_ContextMenu.Root dir="ltr">
|
2021-06-10 09:49:16 +00:00
|
|
|
<_ContextMenu.Trigger>{children}</_ContextMenu.Trigger>
|
|
|
|
<StyledContent ref={rContent} isMobile={isMobile()}>
|
2021-06-27 09:07:20 +00:00
|
|
|
{selectedShapeIds.length ? (
|
2021-06-10 09:49:16 +00:00
|
|
|
<>
|
|
|
|
{/* <Button onSelect={() => state.send('COPIED')}>
|
|
|
|
<span>Copy</span>
|
|
|
|
<kbd>
|
|
|
|
<span>{commandKey()}</span>
|
|
|
|
<span>C</span>
|
|
|
|
</kbd>
|
|
|
|
</Button>
|
|
|
|
<Button onSelect={() => state.send('CUT')}>
|
|
|
|
<span>Cut</span>
|
|
|
|
<kbd>
|
|
|
|
<span>{commandKey()}</span>
|
|
|
|
<span>X</span>
|
|
|
|
</kbd>
|
|
|
|
</Button>
|
|
|
|
*/}
|
|
|
|
<Button onSelect={() => state.send('DUPLICATED')}>
|
|
|
|
<span>Duplicate</span>
|
|
|
|
<kbd>
|
|
|
|
<span>{commandKey()}</span>
|
|
|
|
<span>D</span>
|
|
|
|
</kbd>
|
|
|
|
</Button>
|
|
|
|
<StyledDivider />
|
2021-06-27 09:07:20 +00:00
|
|
|
{hasGroupSelected ||
|
2021-06-17 12:03:08 +00:00
|
|
|
(hasTwoOrMore && (
|
2021-06-10 09:49:16 +00:00
|
|
|
<>
|
2021-06-27 09:07:20 +00:00
|
|
|
{hasGroupSelected && (
|
2021-06-10 09:49:16 +00:00
|
|
|
<Button onSelect={() => state.send('UNGROUPED')}>
|
|
|
|
<span>Ungroup</span>
|
|
|
|
<kbd>
|
|
|
|
<span>{commandKey()}</span>
|
|
|
|
<span>⇧</span>
|
|
|
|
<span>G</span>
|
|
|
|
</kbd>
|
|
|
|
</Button>
|
|
|
|
)}
|
2021-06-17 12:03:08 +00:00
|
|
|
{hasTwoOrMore && (
|
2021-06-10 09:49:16 +00:00
|
|
|
<Button onSelect={() => state.send('GROUPED')}>
|
|
|
|
<span>Group</span>
|
|
|
|
<kbd>
|
|
|
|
<span>{commandKey()}</span>
|
|
|
|
<span>G</span>
|
|
|
|
</kbd>
|
|
|
|
</Button>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
))}
|
2021-06-16 12:09:45 +00:00
|
|
|
<SubMenu label="Move">
|
|
|
|
<Button
|
|
|
|
onSelect={() =>
|
|
|
|
state.send('MOVED', {
|
|
|
|
type: MoveType.ToFront,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<span>To Front</span>
|
|
|
|
<kbd>
|
|
|
|
<span>{commandKey()}</span>
|
|
|
|
<span>⇧</span>
|
|
|
|
<span>]</span>
|
|
|
|
</kbd>
|
|
|
|
</Button>
|
2021-06-10 09:49:16 +00:00
|
|
|
|
2021-06-16 12:09:45 +00:00
|
|
|
<Button
|
|
|
|
onSelect={() =>
|
|
|
|
state.send('MOVED', {
|
|
|
|
type: MoveType.Forward,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<span>Forward</span>
|
|
|
|
<kbd>
|
|
|
|
<span>{commandKey()}</span>
|
|
|
|
<span>]</span>
|
|
|
|
</kbd>
|
|
|
|
</Button>
|
|
|
|
<Button
|
|
|
|
onSelect={() =>
|
|
|
|
state.send('MOVED', {
|
|
|
|
type: MoveType.Backward,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<span>Backward</span>
|
|
|
|
<kbd>
|
|
|
|
<span>{commandKey()}</span>
|
|
|
|
<span>[</span>
|
|
|
|
</kbd>
|
|
|
|
</Button>
|
|
|
|
<Button
|
|
|
|
onSelect={() =>
|
|
|
|
state.send('MOVED', {
|
|
|
|
type: MoveType.ToBack,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<span>To Back</span>
|
|
|
|
<kbd>
|
|
|
|
<span>{commandKey()}</span>
|
|
|
|
<span>⇧</span>
|
|
|
|
<span>[</span>
|
|
|
|
</kbd>
|
|
|
|
</Button>
|
|
|
|
</SubMenu>
|
2021-06-17 12:03:08 +00:00
|
|
|
{hasTwoOrMore && (
|
|
|
|
<AlignDistributeSubMenu
|
|
|
|
hasTwoOrMore={hasTwoOrMore}
|
|
|
|
hasThreeOrMore={hasThreeOrMore}
|
|
|
|
/>
|
|
|
|
)}
|
2021-06-16 12:09:45 +00:00
|
|
|
<MoveToPageMenu />
|
2021-06-20 22:01:40 +00:00
|
|
|
<Button onSelect={() => state.send('COPIED_TO_SVG')}>
|
|
|
|
<span>Copy to SVG</span>
|
|
|
|
<kbd>
|
|
|
|
<span>{commandKey()}</span>
|
|
|
|
<span>⇧</span>
|
|
|
|
<span>C</span>
|
|
|
|
</kbd>
|
|
|
|
</Button>
|
2021-06-16 12:09:45 +00:00
|
|
|
<StyledDivider />
|
2021-06-10 09:49:16 +00:00
|
|
|
<Button onSelect={() => state.send('DELETED')}>
|
|
|
|
<span>Delete</span>
|
|
|
|
<kbd>
|
|
|
|
<span>⌫</span>
|
|
|
|
</kbd>
|
|
|
|
</Button>
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
<>
|
|
|
|
<Button onSelect={() => state.send('UNDO')}>
|
|
|
|
<span>Undo</span>
|
|
|
|
<kbd>
|
|
|
|
<span>{commandKey()}</span>
|
|
|
|
<span>Z</span>
|
|
|
|
</kbd>
|
|
|
|
</Button>
|
|
|
|
<Button onSelect={() => state.send('REDO')}>
|
|
|
|
<span>Redo</span>
|
|
|
|
<kbd>
|
|
|
|
<span>{commandKey()}</span>
|
|
|
|
<span>⇧</span>
|
|
|
|
<span>Z</span>
|
|
|
|
</kbd>
|
|
|
|
</Button>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</StyledContent>
|
|
|
|
</_ContextMenu.Root>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const StyledContent = styled(_ContextMenu.Content, {
|
|
|
|
position: 'relative',
|
|
|
|
backgroundColor: '$panel',
|
|
|
|
borderRadius: '4px',
|
|
|
|
overflow: 'hidden',
|
|
|
|
pointerEvents: 'all',
|
|
|
|
userSelect: 'none',
|
|
|
|
zIndex: 200,
|
2021-06-16 12:09:45 +00:00
|
|
|
padding: 3,
|
2021-06-10 09:49:16 +00:00
|
|
|
boxShadow: '0px 2px 4px rgba(0,0,0,.2)',
|
|
|
|
minWidth: 128,
|
2021-06-27 21:30:37 +00:00
|
|
|
font: '$ui',
|
2021-06-10 09:49:16 +00:00
|
|
|
|
|
|
|
'& kbd': {
|
|
|
|
marginLeft: '32px',
|
|
|
|
fontSize: '$1',
|
|
|
|
fontFamily: '$ui',
|
2021-06-27 21:30:37 +00:00
|
|
|
fontWeight: 400,
|
2021-06-10 09:49:16 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
'& kbd > span': {
|
|
|
|
display: 'inline-block',
|
|
|
|
width: '12px',
|
|
|
|
},
|
|
|
|
|
|
|
|
variants: {
|
|
|
|
isMobile: {
|
|
|
|
true: {
|
|
|
|
'& kbd': {
|
|
|
|
display: 'none',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
const StyledDivider = styled(_ContextMenu.Separator, {
|
|
|
|
backgroundColor: '$hover',
|
|
|
|
height: 1,
|
2021-06-16 12:09:45 +00:00
|
|
|
margin: '3px -3px',
|
2021-06-10 09:49:16 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
function Button({
|
|
|
|
onSelect,
|
|
|
|
children,
|
|
|
|
disabled = false,
|
|
|
|
}: {
|
|
|
|
onSelect: () => void
|
|
|
|
disabled?: boolean
|
|
|
|
children: React.ReactNode
|
|
|
|
}) {
|
|
|
|
return (
|
|
|
|
<_ContextMenu.Item
|
|
|
|
as={RowButton}
|
|
|
|
disabled={disabled}
|
|
|
|
bp={{ '@initial': 'mobile', '@sm': 'small' }}
|
|
|
|
onSelect={onSelect}
|
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</_ContextMenu.Item>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-06-17 12:03:08 +00:00
|
|
|
function IconButton({
|
|
|
|
onSelect,
|
|
|
|
children,
|
|
|
|
disabled = false,
|
|
|
|
}: {
|
|
|
|
onSelect: () => void
|
|
|
|
disabled?: boolean
|
|
|
|
children: React.ReactNode
|
|
|
|
}) {
|
|
|
|
return (
|
|
|
|
<_ContextMenu.Item
|
|
|
|
as={_IconButton}
|
|
|
|
bp={{ '@initial': 'mobile', '@sm': 'small' }}
|
|
|
|
disabled={disabled}
|
|
|
|
onSelect={onSelect}
|
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</_ContextMenu.Item>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-06-16 12:09:45 +00:00
|
|
|
function SubMenu({
|
|
|
|
children,
|
|
|
|
label,
|
|
|
|
}: {
|
|
|
|
label: string
|
|
|
|
children: React.ReactNode
|
|
|
|
}) {
|
|
|
|
return (
|
2021-06-27 19:19:57 +00:00
|
|
|
<_ContextMenu.Root dir="ltr">
|
2021-06-16 12:09:45 +00:00
|
|
|
<_ContextMenu.TriggerItem
|
|
|
|
as={RowButton}
|
|
|
|
bp={{ '@initial': 'mobile', '@sm': 'small' }}
|
|
|
|
>
|
|
|
|
<span>{label}</span>
|
|
|
|
<IconWrapper size="small">
|
|
|
|
<ChevronRightIcon />
|
|
|
|
</IconWrapper>
|
|
|
|
</_ContextMenu.TriggerItem>
|
|
|
|
<StyledContent sideOffset={2} alignOffset={-2} isMobile={isMobile()}>
|
|
|
|
{children}
|
|
|
|
<StyledArrow offset={13} />
|
|
|
|
</StyledContent>
|
|
|
|
</_ContextMenu.Root>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-06-17 12:03:08 +00:00
|
|
|
function AlignDistributeSubMenu({
|
|
|
|
hasThreeOrMore,
|
|
|
|
}: {
|
|
|
|
hasTwoOrMore: boolean
|
|
|
|
hasThreeOrMore: boolean
|
|
|
|
}) {
|
|
|
|
return (
|
2021-06-27 19:19:57 +00:00
|
|
|
<_ContextMenu.Root dir="ltr">
|
2021-06-17 12:03:08 +00:00
|
|
|
<_ContextMenu.TriggerItem
|
|
|
|
as={RowButton}
|
|
|
|
bp={{ '@initial': 'mobile', '@sm': 'small' }}
|
|
|
|
>
|
|
|
|
<span>Align / Distribute</span>
|
|
|
|
<IconWrapper size="small">
|
|
|
|
<ChevronRightIcon />
|
|
|
|
</IconWrapper>
|
|
|
|
</_ContextMenu.TriggerItem>
|
|
|
|
<StyledGrid
|
|
|
|
sideOffset={2}
|
|
|
|
alignOffset={-2}
|
|
|
|
isMobile={isMobile()}
|
|
|
|
selectedStyle={hasThreeOrMore ? 'threeOrMore' : 'twoOrMore'}
|
|
|
|
>
|
|
|
|
<IconButton onSelect={alignLeft}>
|
|
|
|
<AlignLeftIcon />
|
|
|
|
</IconButton>
|
|
|
|
<IconButton onSelect={alignCenterHorizontal}>
|
|
|
|
<AlignCenterHorizontallyIcon />
|
|
|
|
</IconButton>
|
|
|
|
<IconButton onSelect={alignRight}>
|
|
|
|
<AlignRightIcon />
|
|
|
|
</IconButton>
|
|
|
|
<IconButton onSelect={stretchHorizontally}>
|
|
|
|
<StretchHorizontallyIcon />
|
|
|
|
</IconButton>
|
|
|
|
{hasThreeOrMore && (
|
|
|
|
<IconButton onSelect={distributeHorizontally}>
|
|
|
|
<SpaceEvenlyHorizontallyIcon />
|
|
|
|
</IconButton>
|
|
|
|
)}
|
|
|
|
|
|
|
|
<IconButton onSelect={alignTop}>
|
|
|
|
<AlignTopIcon />
|
|
|
|
</IconButton>
|
|
|
|
<IconButton onSelect={alignCenterVertical}>
|
|
|
|
<AlignCenterVerticallyIcon />
|
|
|
|
</IconButton>
|
|
|
|
<IconButton onSelect={alignBottom}>
|
|
|
|
<AlignBottomIcon />
|
|
|
|
</IconButton>
|
|
|
|
<IconButton onSelect={stretchVertically}>
|
|
|
|
<StretchVerticallyIcon />
|
|
|
|
</IconButton>
|
|
|
|
{hasThreeOrMore && (
|
|
|
|
<IconButton onSelect={distributeVertically}>
|
|
|
|
<SpaceEvenlyVerticallyIcon />
|
|
|
|
</IconButton>
|
|
|
|
)}
|
|
|
|
<StyledArrow offset={13} />
|
|
|
|
</StyledGrid>
|
|
|
|
</_ContextMenu.Root>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const StyledGrid = styled(StyledContent, {
|
|
|
|
display: 'grid',
|
|
|
|
variants: {
|
|
|
|
selectedStyle: {
|
|
|
|
threeOrMore: {
|
|
|
|
gridTemplateColumns: 'repeat(5, auto)',
|
|
|
|
},
|
|
|
|
twoOrMore: {
|
|
|
|
gridTemplateColumns: 'repeat(4, auto)',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2021-06-16 12:09:45 +00:00
|
|
|
function MoveToPageMenu() {
|
2021-06-10 09:49:16 +00:00
|
|
|
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)
|
|
|
|
|
2021-06-17 12:03:08 +00:00
|
|
|
if (sorted.length === 0) return null
|
|
|
|
|
2021-06-10 09:49:16 +00:00
|
|
|
return (
|
2021-06-27 19:19:57 +00:00
|
|
|
<_ContextMenu.Root dir="ltr">
|
2021-06-16 12:09:45 +00:00
|
|
|
<_ContextMenu.TriggerItem
|
2021-06-10 09:49:16 +00:00
|
|
|
as={RowButton}
|
|
|
|
bp={{ '@initial': 'mobile', '@sm': 'small' }}
|
|
|
|
>
|
2021-06-16 12:09:45 +00:00
|
|
|
<span>Move To Page</span>
|
|
|
|
<IconWrapper size="small">
|
|
|
|
<ChevronRightIcon />
|
|
|
|
</IconWrapper>
|
|
|
|
</_ContextMenu.TriggerItem>
|
|
|
|
<StyledContent sideOffset={2} alignOffset={-2} isMobile={isMobile()}>
|
2021-06-10 09:49:16 +00:00
|
|
|
{sorted.map(({ id, name }) => (
|
2021-06-16 12:09:45 +00:00
|
|
|
<Button
|
2021-06-10 09:49:16 +00:00
|
|
|
key={id}
|
|
|
|
disabled={id === currentPageId}
|
|
|
|
onSelect={() => state.send('MOVED_TO_PAGE', { id })}
|
|
|
|
>
|
|
|
|
<span>{name}</span>
|
2021-06-16 12:09:45 +00:00
|
|
|
</Button>
|
2021-06-10 09:49:16 +00:00
|
|
|
))}
|
2021-06-16 12:09:45 +00:00
|
|
|
<StyledArrow offset={13} />
|
|
|
|
</StyledContent>
|
|
|
|
</_ContextMenu.Root>
|
2021-06-10 09:49:16 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-06-16 12:09:45 +00:00
|
|
|
const StyledArrow = styled(_ContextMenu.Arrow, {
|
|
|
|
fill: 'white',
|
|
|
|
})
|