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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. import * as _ContextMenu from '@radix-ui/react-context-menu'
  2. import * as _Dropdown from '@radix-ui/react-dropdown-menu'
  3. import styled from 'styles'
  4. import {
  5. IconWrapper,
  6. IconButton as _IconButton,
  7. RowButton,
  8. } from 'components/shared'
  9. import {
  10. commandKey,
  11. deepCompareArrays,
  12. getSelectedShapes,
  13. isMobile,
  14. } from 'utils/utils'
  15. import state, { useSelector } from 'state'
  16. import {
  17. AlignType,
  18. DistributeType,
  19. MoveType,
  20. ShapeType,
  21. StretchType,
  22. } from 'types'
  23. import React, { useRef } from 'react'
  24. import {
  25. ChevronRightIcon,
  26. AlignBottomIcon,
  27. AlignCenterHorizontallyIcon,
  28. AlignCenterVerticallyIcon,
  29. AlignLeftIcon,
  30. AlignRightIcon,
  31. AlignTopIcon,
  32. SpaceEvenlyHorizontallyIcon,
  33. SpaceEvenlyVerticallyIcon,
  34. StretchHorizontallyIcon,
  35. StretchVerticallyIcon,
  36. } from '@radix-ui/react-icons'
  37. function alignTop() {
  38. state.send('ALIGNED', { type: AlignType.Top })
  39. }
  40. function alignCenterVertical() {
  41. state.send('ALIGNED', { type: AlignType.CenterVertical })
  42. }
  43. function alignBottom() {
  44. state.send('ALIGNED', { type: AlignType.Bottom })
  45. }
  46. function stretchVertically() {
  47. state.send('STRETCHED', { type: StretchType.Vertical })
  48. }
  49. function distributeVertically() {
  50. state.send('DISTRIBUTED', { type: DistributeType.Vertical })
  51. }
  52. function alignLeft() {
  53. state.send('ALIGNED', { type: AlignType.Left })
  54. }
  55. function alignCenterHorizontal() {
  56. state.send('ALIGNED', { type: AlignType.CenterHorizontal })
  57. }
  58. function alignRight() {
  59. state.send('ALIGNED', { type: AlignType.Right })
  60. }
  61. function stretchHorizontally() {
  62. state.send('STRETCHED', { type: StretchType.Horizontal })
  63. }
  64. function distributeHorizontally() {
  65. state.send('DISTRIBUTED', { type: DistributeType.Horizontal })
  66. }
  67. export default function ContextMenu({
  68. children,
  69. }: {
  70. children: React.ReactNode
  71. }) {
  72. const selectedShapes = useSelector(
  73. (s) => getSelectedShapes(s.data),
  74. deepCompareArrays
  75. )
  76. const rContent = useRef<HTMLDivElement>(null)
  77. const hasGroupSelectd = selectedShapes.some((s) => s.type === ShapeType.Group)
  78. const hasTwoOrMore = selectedShapes.length > 1
  79. const hasThreeOrMore = selectedShapes.length > 2
  80. return (
  81. <_ContextMenu.Root>
  82. <_ContextMenu.Trigger>{children}</_ContextMenu.Trigger>
  83. <StyledContent ref={rContent} isMobile={isMobile()}>
  84. {selectedShapes.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. {hasGroupSelectd ||
  110. (hasTwoOrMore && (
  111. <>
  112. {hasGroupSelectd && (
  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. <StyledDivider />
  197. <Button onSelect={() => state.send('DELETED')}>
  198. <span>Delete</span>
  199. <kbd>
  200. <span>⌫</span>
  201. </kbd>
  202. </Button>
  203. </>
  204. ) : (
  205. <>
  206. <Button onSelect={() => state.send('UNDO')}>
  207. <span>Undo</span>
  208. <kbd>
  209. <span>{commandKey()}</span>
  210. <span>Z</span>
  211. </kbd>
  212. </Button>
  213. <Button onSelect={() => state.send('REDO')}>
  214. <span>Redo</span>
  215. <kbd>
  216. <span>{commandKey()}</span>
  217. <span>⇧</span>
  218. <span>Z</span>
  219. </kbd>
  220. </Button>
  221. </>
  222. )}
  223. </StyledContent>
  224. </_ContextMenu.Root>
  225. )
  226. }
  227. const StyledContent = styled(_ContextMenu.Content, {
  228. position: 'relative',
  229. backgroundColor: '$panel',
  230. borderRadius: '4px',
  231. overflow: 'hidden',
  232. pointerEvents: 'all',
  233. userSelect: 'none',
  234. zIndex: 200,
  235. padding: 3,
  236. boxShadow: '0px 2px 4px rgba(0,0,0,.2)',
  237. minWidth: 128,
  238. '& kbd': {
  239. marginLeft: '32px',
  240. fontSize: '$1',
  241. fontFamily: '$ui',
  242. },
  243. '& kbd > span': {
  244. display: 'inline-block',
  245. width: '12px',
  246. },
  247. variants: {
  248. isMobile: {
  249. true: {
  250. '& kbd': {
  251. display: 'none',
  252. },
  253. },
  254. },
  255. },
  256. })
  257. const StyledDivider = styled(_ContextMenu.Separator, {
  258. backgroundColor: '$hover',
  259. height: 1,
  260. margin: '3px -3px',
  261. })
  262. function Button({
  263. onSelect,
  264. children,
  265. disabled = false,
  266. }: {
  267. onSelect: () => void
  268. disabled?: boolean
  269. children: React.ReactNode
  270. }) {
  271. return (
  272. <_ContextMenu.Item
  273. as={RowButton}
  274. disabled={disabled}
  275. bp={{ '@initial': 'mobile', '@sm': 'small' }}
  276. onSelect={onSelect}
  277. >
  278. {children}
  279. </_ContextMenu.Item>
  280. )
  281. }
  282. function IconButton({
  283. onSelect,
  284. children,
  285. disabled = false,
  286. }: {
  287. onSelect: () => void
  288. disabled?: boolean
  289. children: React.ReactNode
  290. }) {
  291. return (
  292. <_ContextMenu.Item
  293. as={_IconButton}
  294. bp={{ '@initial': 'mobile', '@sm': 'small' }}
  295. disabled={disabled}
  296. onSelect={onSelect}
  297. >
  298. {children}
  299. </_ContextMenu.Item>
  300. )
  301. }
  302. function SubMenu({
  303. children,
  304. label,
  305. }: {
  306. label: string
  307. children: React.ReactNode
  308. }) {
  309. return (
  310. <_ContextMenu.Root>
  311. <_ContextMenu.TriggerItem
  312. as={RowButton}
  313. bp={{ '@initial': 'mobile', '@sm': 'small' }}
  314. >
  315. <span>{label}</span>
  316. <IconWrapper size="small">
  317. <ChevronRightIcon />
  318. </IconWrapper>
  319. </_ContextMenu.TriggerItem>
  320. <StyledContent sideOffset={2} alignOffset={-2} isMobile={isMobile()}>
  321. {children}
  322. <StyledArrow offset={13} />
  323. </StyledContent>
  324. </_ContextMenu.Root>
  325. )
  326. }
  327. function AlignDistributeSubMenu({
  328. hasTwoOrMore,
  329. hasThreeOrMore,
  330. }: {
  331. hasTwoOrMore: boolean
  332. hasThreeOrMore: boolean
  333. }) {
  334. return (
  335. <_ContextMenu.Root>
  336. <_ContextMenu.TriggerItem
  337. as={RowButton}
  338. bp={{ '@initial': 'mobile', '@sm': 'small' }}
  339. >
  340. <span>Align / Distribute</span>
  341. <IconWrapper size="small">
  342. <ChevronRightIcon />
  343. </IconWrapper>
  344. </_ContextMenu.TriggerItem>
  345. <StyledGrid
  346. sideOffset={2}
  347. alignOffset={-2}
  348. isMobile={isMobile()}
  349. selectedStyle={hasThreeOrMore ? 'threeOrMore' : 'twoOrMore'}
  350. >
  351. <IconButton onSelect={alignLeft}>
  352. <AlignLeftIcon />
  353. </IconButton>
  354. <IconButton onSelect={alignCenterHorizontal}>
  355. <AlignCenterHorizontallyIcon />
  356. </IconButton>
  357. <IconButton onSelect={alignRight}>
  358. <AlignRightIcon />
  359. </IconButton>
  360. <IconButton onSelect={stretchHorizontally}>
  361. <StretchHorizontallyIcon />
  362. </IconButton>
  363. {hasThreeOrMore && (
  364. <IconButton onSelect={distributeHorizontally}>
  365. <SpaceEvenlyHorizontallyIcon />
  366. </IconButton>
  367. )}
  368. <IconButton onSelect={alignTop}>
  369. <AlignTopIcon />
  370. </IconButton>
  371. <IconButton onSelect={alignCenterVertical}>
  372. <AlignCenterVerticallyIcon />
  373. </IconButton>
  374. <IconButton onSelect={alignBottom}>
  375. <AlignBottomIcon />
  376. </IconButton>
  377. <IconButton onSelect={stretchVertically}>
  378. <StretchVerticallyIcon />
  379. </IconButton>
  380. {hasThreeOrMore && (
  381. <IconButton onSelect={distributeVertically}>
  382. <SpaceEvenlyVerticallyIcon />
  383. </IconButton>
  384. )}
  385. <StyledArrow offset={13} />
  386. </StyledGrid>
  387. </_ContextMenu.Root>
  388. )
  389. }
  390. const StyledGrid = styled(StyledContent, {
  391. display: 'grid',
  392. variants: {
  393. selectedStyle: {
  394. threeOrMore: {
  395. gridTemplateColumns: 'repeat(5, auto)',
  396. },
  397. twoOrMore: {
  398. gridTemplateColumns: 'repeat(4, auto)',
  399. },
  400. },
  401. },
  402. })
  403. function MoveToPageMenu() {
  404. const documentPages = useSelector((s) => s.data.document.pages)
  405. const currentPageId = useSelector((s) => s.data.currentPageId)
  406. if (!documentPages[currentPageId]) return null
  407. const sorted = Object.values(documentPages)
  408. .sort((a, b) => a.childIndex - b.childIndex)
  409. .filter((a) => a.id !== currentPageId)
  410. if (sorted.length === 0) return null
  411. return (
  412. <_ContextMenu.Root>
  413. <_ContextMenu.TriggerItem
  414. as={RowButton}
  415. bp={{ '@initial': 'mobile', '@sm': 'small' }}
  416. >
  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 StyledDialogContent = styled(_Dropdown.Content, {
  438. // position: 'fixed',
  439. // top: '50%',
  440. // left: '50%',
  441. // transform: 'translate(-50%, -50%)',
  442. // minWidth: 200,
  443. // maxWidth: 'fit-content',
  444. // maxHeight: '85vh',
  445. // marginTop: '-5vh',
  446. minWidth: 128,
  447. backgroundColor: '$panel',
  448. borderRadius: '4px',
  449. overflow: 'hidden',
  450. pointerEvents: 'all',
  451. userSelect: 'none',
  452. zIndex: 200,
  453. padding: 2,
  454. border: '1px solid $panel',
  455. boxShadow: '0px 2px 4px rgba(0,0,0,.2)',
  456. '&:focus': {
  457. outline: 'none',
  458. },
  459. })
  460. const StyledArrow = styled(_ContextMenu.Arrow, {
  461. fill: 'white',
  462. })