You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

context-menu.tsx 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. import * as _ContextMenu from '@radix-ui/react-context-menu'
  2. import styled from 'styles'
  3. import {
  4. IconWrapper,
  5. IconButton as _IconButton,
  6. RowButton,
  7. } from 'components/shared'
  8. import {
  9. commandKey,
  10. deepCompareArrays,
  11. getSelectedShapes,
  12. isMobile,
  13. } from 'utils/utils'
  14. import state, { useSelector } from 'state'
  15. import {
  16. AlignType,
  17. DistributeType,
  18. MoveType,
  19. ShapeType,
  20. StretchType,
  21. } from 'types'
  22. import React, { useRef } from 'react'
  23. import {
  24. ChevronRightIcon,
  25. AlignBottomIcon,
  26. AlignCenterHorizontallyIcon,
  27. AlignCenterVerticallyIcon,
  28. AlignLeftIcon,
  29. AlignRightIcon,
  30. AlignTopIcon,
  31. SpaceEvenlyHorizontallyIcon,
  32. SpaceEvenlyVerticallyIcon,
  33. StretchHorizontallyIcon,
  34. StretchVerticallyIcon,
  35. } from '@radix-ui/react-icons'
  36. function alignTop() {
  37. state.send('ALIGNED', { type: AlignType.Top })
  38. }
  39. function alignCenterVertical() {
  40. state.send('ALIGNED', { type: AlignType.CenterVertical })
  41. }
  42. function alignBottom() {
  43. state.send('ALIGNED', { type: AlignType.Bottom })
  44. }
  45. function stretchVertically() {
  46. state.send('STRETCHED', { type: StretchType.Vertical })
  47. }
  48. function distributeVertically() {
  49. state.send('DISTRIBUTED', { type: DistributeType.Vertical })
  50. }
  51. function alignLeft() {
  52. state.send('ALIGNED', { type: AlignType.Left })
  53. }
  54. function alignCenterHorizontal() {
  55. state.send('ALIGNED', { type: AlignType.CenterHorizontal })
  56. }
  57. function alignRight() {
  58. state.send('ALIGNED', { type: AlignType.Right })
  59. }
  60. function stretchHorizontally() {
  61. state.send('STRETCHED', { type: StretchType.Horizontal })
  62. }
  63. function distributeHorizontally() {
  64. state.send('DISTRIBUTED', { type: DistributeType.Horizontal })
  65. }
  66. export default function ContextMenu({
  67. children,
  68. }: {
  69. children: React.ReactNode
  70. }): JSX.Element {
  71. const selectedShapes = useSelector(
  72. (s) => getSelectedShapes(s.data),
  73. deepCompareArrays
  74. )
  75. const rContent = useRef<HTMLDivElement>(null)
  76. const hasGroupSelectd = selectedShapes.some((s) => s.type === ShapeType.Group)
  77. const hasTwoOrMore = selectedShapes.length > 1
  78. const hasThreeOrMore = selectedShapes.length > 2
  79. return (
  80. <_ContextMenu.Root>
  81. <_ContextMenu.Trigger>{children}</_ContextMenu.Trigger>
  82. <StyledContent ref={rContent} isMobile={isMobile()}>
  83. {selectedShapes.length ? (
  84. <>
  85. {/* <Button onSelect={() => state.send('COPIED')}>
  86. <span>Copy</span>
  87. <kbd>
  88. <span>{commandKey()}</span>
  89. <span>C</span>
  90. </kbd>
  91. </Button>
  92. <Button onSelect={() => state.send('CUT')}>
  93. <span>Cut</span>
  94. <kbd>
  95. <span>{commandKey()}</span>
  96. <span>X</span>
  97. </kbd>
  98. </Button>
  99. */}
  100. <Button onSelect={() => state.send('DUPLICATED')}>
  101. <span>Duplicate</span>
  102. <kbd>
  103. <span>{commandKey()}</span>
  104. <span>D</span>
  105. </kbd>
  106. </Button>
  107. <StyledDivider />
  108. {hasGroupSelectd ||
  109. (hasTwoOrMore && (
  110. <>
  111. {hasGroupSelectd && (
  112. <Button onSelect={() => state.send('UNGROUPED')}>
  113. <span>Ungroup</span>
  114. <kbd>
  115. <span>{commandKey()}</span>
  116. <span>⇧</span>
  117. <span>G</span>
  118. </kbd>
  119. </Button>
  120. )}
  121. {hasTwoOrMore && (
  122. <Button onSelect={() => state.send('GROUPED')}>
  123. <span>Group</span>
  124. <kbd>
  125. <span>{commandKey()}</span>
  126. <span>G</span>
  127. </kbd>
  128. </Button>
  129. )}
  130. </>
  131. ))}
  132. <SubMenu label="Move">
  133. <Button
  134. onSelect={() =>
  135. state.send('MOVED', {
  136. type: MoveType.ToFront,
  137. })
  138. }
  139. >
  140. <span>To Front</span>
  141. <kbd>
  142. <span>{commandKey()}</span>
  143. <span>⇧</span>
  144. <span>]</span>
  145. </kbd>
  146. </Button>
  147. <Button
  148. onSelect={() =>
  149. state.send('MOVED', {
  150. type: MoveType.Forward,
  151. })
  152. }
  153. >
  154. <span>Forward</span>
  155. <kbd>
  156. <span>{commandKey()}</span>
  157. <span>]</span>
  158. </kbd>
  159. </Button>
  160. <Button
  161. onSelect={() =>
  162. state.send('MOVED', {
  163. type: MoveType.Backward,
  164. })
  165. }
  166. >
  167. <span>Backward</span>
  168. <kbd>
  169. <span>{commandKey()}</span>
  170. <span>[</span>
  171. </kbd>
  172. </Button>
  173. <Button
  174. onSelect={() =>
  175. state.send('MOVED', {
  176. type: MoveType.ToBack,
  177. })
  178. }
  179. >
  180. <span>To Back</span>
  181. <kbd>
  182. <span>{commandKey()}</span>
  183. <span>⇧</span>
  184. <span>[</span>
  185. </kbd>
  186. </Button>
  187. </SubMenu>
  188. {hasTwoOrMore && (
  189. <AlignDistributeSubMenu
  190. hasTwoOrMore={hasTwoOrMore}
  191. hasThreeOrMore={hasThreeOrMore}
  192. />
  193. )}
  194. <MoveToPageMenu />
  195. <Button onSelect={() => state.send('COPIED_TO_SVG')}>
  196. <span>Copy to SVG</span>
  197. <kbd>
  198. <span>{commandKey()}</span>
  199. <span>⇧</span>
  200. <span>C</span>
  201. </kbd>
  202. </Button>
  203. <StyledDivider />
  204. <Button onSelect={() => state.send('DELETED')}>
  205. <span>Delete</span>
  206. <kbd>
  207. <span>⌫</span>
  208. </kbd>
  209. </Button>
  210. </>
  211. ) : (
  212. <>
  213. <Button onSelect={() => state.send('UNDO')}>
  214. <span>Undo</span>
  215. <kbd>
  216. <span>{commandKey()}</span>
  217. <span>Z</span>
  218. </kbd>
  219. </Button>
  220. <Button onSelect={() => state.send('REDO')}>
  221. <span>Redo</span>
  222. <kbd>
  223. <span>{commandKey()}</span>
  224. <span>⇧</span>
  225. <span>Z</span>
  226. </kbd>
  227. </Button>
  228. </>
  229. )}
  230. </StyledContent>
  231. </_ContextMenu.Root>
  232. )
  233. }
  234. const StyledContent = styled(_ContextMenu.Content, {
  235. position: 'relative',
  236. backgroundColor: '$panel',
  237. borderRadius: '4px',
  238. overflow: 'hidden',
  239. pointerEvents: 'all',
  240. userSelect: 'none',
  241. zIndex: 200,
  242. padding: 3,
  243. boxShadow: '0px 2px 4px rgba(0,0,0,.2)',
  244. minWidth: 128,
  245. '& kbd': {
  246. marginLeft: '32px',
  247. fontSize: '$1',
  248. fontFamily: '$ui',
  249. },
  250. '& kbd > span': {
  251. display: 'inline-block',
  252. width: '12px',
  253. },
  254. variants: {
  255. isMobile: {
  256. true: {
  257. '& kbd': {
  258. display: 'none',
  259. },
  260. },
  261. },
  262. },
  263. })
  264. const StyledDivider = styled(_ContextMenu.Separator, {
  265. backgroundColor: '$hover',
  266. height: 1,
  267. margin: '3px -3px',
  268. })
  269. function Button({
  270. onSelect,
  271. children,
  272. disabled = false,
  273. }: {
  274. onSelect: () => void
  275. disabled?: boolean
  276. children: React.ReactNode
  277. }) {
  278. return (
  279. <_ContextMenu.Item
  280. as={RowButton}
  281. disabled={disabled}
  282. bp={{ '@initial': 'mobile', '@sm': 'small' }}
  283. onSelect={onSelect}
  284. >
  285. {children}
  286. </_ContextMenu.Item>
  287. )
  288. }
  289. function IconButton({
  290. onSelect,
  291. children,
  292. disabled = false,
  293. }: {
  294. onSelect: () => void
  295. disabled?: boolean
  296. children: React.ReactNode
  297. }) {
  298. return (
  299. <_ContextMenu.Item
  300. as={_IconButton}
  301. bp={{ '@initial': 'mobile', '@sm': 'small' }}
  302. disabled={disabled}
  303. onSelect={onSelect}
  304. >
  305. {children}
  306. </_ContextMenu.Item>
  307. )
  308. }
  309. function SubMenu({
  310. children,
  311. label,
  312. }: {
  313. label: string
  314. children: React.ReactNode
  315. }) {
  316. return (
  317. <_ContextMenu.Root>
  318. <_ContextMenu.TriggerItem
  319. as={RowButton}
  320. bp={{ '@initial': 'mobile', '@sm': 'small' }}
  321. >
  322. <span>{label}</span>
  323. <IconWrapper size="small">
  324. <ChevronRightIcon />
  325. </IconWrapper>
  326. </_ContextMenu.TriggerItem>
  327. <StyledContent sideOffset={2} alignOffset={-2} isMobile={isMobile()}>
  328. {children}
  329. <StyledArrow offset={13} />
  330. </StyledContent>
  331. </_ContextMenu.Root>
  332. )
  333. }
  334. function AlignDistributeSubMenu({
  335. hasThreeOrMore,
  336. }: {
  337. hasTwoOrMore: boolean
  338. hasThreeOrMore: boolean
  339. }) {
  340. return (
  341. <_ContextMenu.Root>
  342. <_ContextMenu.TriggerItem
  343. as={RowButton}
  344. bp={{ '@initial': 'mobile', '@sm': 'small' }}
  345. >
  346. <span>Align / Distribute</span>
  347. <IconWrapper size="small">
  348. <ChevronRightIcon />
  349. </IconWrapper>
  350. </_ContextMenu.TriggerItem>
  351. <StyledGrid
  352. sideOffset={2}
  353. alignOffset={-2}
  354. isMobile={isMobile()}
  355. selectedStyle={hasThreeOrMore ? 'threeOrMore' : 'twoOrMore'}
  356. >
  357. <IconButton onSelect={alignLeft}>
  358. <AlignLeftIcon />
  359. </IconButton>
  360. <IconButton onSelect={alignCenterHorizontal}>
  361. <AlignCenterHorizontallyIcon />
  362. </IconButton>
  363. <IconButton onSelect={alignRight}>
  364. <AlignRightIcon />
  365. </IconButton>
  366. <IconButton onSelect={stretchHorizontally}>
  367. <StretchHorizontallyIcon />
  368. </IconButton>
  369. {hasThreeOrMore && (
  370. <IconButton onSelect={distributeHorizontally}>
  371. <SpaceEvenlyHorizontallyIcon />
  372. </IconButton>
  373. )}
  374. <IconButton onSelect={alignTop}>
  375. <AlignTopIcon />
  376. </IconButton>
  377. <IconButton onSelect={alignCenterVertical}>
  378. <AlignCenterVerticallyIcon />
  379. </IconButton>
  380. <IconButton onSelect={alignBottom}>
  381. <AlignBottomIcon />
  382. </IconButton>
  383. <IconButton onSelect={stretchVertically}>
  384. <StretchVerticallyIcon />
  385. </IconButton>
  386. {hasThreeOrMore && (
  387. <IconButton onSelect={distributeVertically}>
  388. <SpaceEvenlyVerticallyIcon />
  389. </IconButton>
  390. )}
  391. <StyledArrow offset={13} />
  392. </StyledGrid>
  393. </_ContextMenu.Root>
  394. )
  395. }
  396. const StyledGrid = styled(StyledContent, {
  397. display: 'grid',
  398. variants: {
  399. selectedStyle: {
  400. threeOrMore: {
  401. gridTemplateColumns: 'repeat(5, auto)',
  402. },
  403. twoOrMore: {
  404. gridTemplateColumns: 'repeat(4, auto)',
  405. },
  406. },
  407. },
  408. })
  409. function MoveToPageMenu() {
  410. const documentPages = useSelector((s) => s.data.document.pages)
  411. const currentPageId = useSelector((s) => s.data.currentPageId)
  412. if (!documentPages[currentPageId]) return null
  413. const sorted = Object.values(documentPages)
  414. .sort((a, b) => a.childIndex - b.childIndex)
  415. .filter((a) => a.id !== currentPageId)
  416. if (sorted.length === 0) return null
  417. return (
  418. <_ContextMenu.Root>
  419. <_ContextMenu.TriggerItem
  420. as={RowButton}
  421. bp={{ '@initial': 'mobile', '@sm': 'small' }}
  422. >
  423. <span>Move To Page</span>
  424. <IconWrapper size="small">
  425. <ChevronRightIcon />
  426. </IconWrapper>
  427. </_ContextMenu.TriggerItem>
  428. <StyledContent sideOffset={2} alignOffset={-2} isMobile={isMobile()}>
  429. {sorted.map(({ id, name }) => (
  430. <Button
  431. key={id}
  432. disabled={id === currentPageId}
  433. onSelect={() => state.send('MOVED_TO_PAGE', { id })}
  434. >
  435. <span>{name}</span>
  436. </Button>
  437. ))}
  438. <StyledArrow offset={13} />
  439. </StyledContent>
  440. </_ContextMenu.Root>
  441. )
  442. }
  443. const StyledArrow = styled(_ContextMenu.Arrow, {
  444. fill: 'white',
  445. })