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

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