Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

context-menu.tsx 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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. <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. '& kbd': {
  247. marginLeft: '32px',
  248. fontSize: '$1',
  249. fontFamily: '$ui',
  250. },
  251. '& kbd > span': {
  252. display: 'inline-block',
  253. width: '12px',
  254. },
  255. variants: {
  256. isMobile: {
  257. true: {
  258. '& kbd': {
  259. display: 'none',
  260. },
  261. },
  262. },
  263. },
  264. })
  265. const StyledDivider = styled(_ContextMenu.Separator, {
  266. backgroundColor: '$hover',
  267. height: 1,
  268. margin: '3px -3px',
  269. })
  270. function Button({
  271. onSelect,
  272. children,
  273. disabled = false,
  274. }: {
  275. onSelect: () => void
  276. disabled?: boolean
  277. children: React.ReactNode
  278. }) {
  279. return (
  280. <_ContextMenu.Item
  281. as={RowButton}
  282. disabled={disabled}
  283. bp={{ '@initial': 'mobile', '@sm': 'small' }}
  284. onSelect={onSelect}
  285. >
  286. {children}
  287. </_ContextMenu.Item>
  288. )
  289. }
  290. function IconButton({
  291. onSelect,
  292. children,
  293. disabled = false,
  294. }: {
  295. onSelect: () => void
  296. disabled?: boolean
  297. children: React.ReactNode
  298. }) {
  299. return (
  300. <_ContextMenu.Item
  301. as={_IconButton}
  302. bp={{ '@initial': 'mobile', '@sm': 'small' }}
  303. disabled={disabled}
  304. onSelect={onSelect}
  305. >
  306. {children}
  307. </_ContextMenu.Item>
  308. )
  309. }
  310. function SubMenu({
  311. children,
  312. label,
  313. }: {
  314. label: string
  315. children: React.ReactNode
  316. }) {
  317. return (
  318. <_ContextMenu.Root>
  319. <_ContextMenu.TriggerItem
  320. as={RowButton}
  321. bp={{ '@initial': 'mobile', '@sm': 'small' }}
  322. >
  323. <span>{label}</span>
  324. <IconWrapper size="small">
  325. <ChevronRightIcon />
  326. </IconWrapper>
  327. </_ContextMenu.TriggerItem>
  328. <StyledContent sideOffset={2} alignOffset={-2} isMobile={isMobile()}>
  329. {children}
  330. <StyledArrow offset={13} />
  331. </StyledContent>
  332. </_ContextMenu.Root>
  333. )
  334. }
  335. function AlignDistributeSubMenu({
  336. hasTwoOrMore,
  337. hasThreeOrMore,
  338. }: {
  339. hasTwoOrMore: boolean
  340. hasThreeOrMore: boolean
  341. }) {
  342. return (
  343. <_ContextMenu.Root>
  344. <_ContextMenu.TriggerItem
  345. as={RowButton}
  346. bp={{ '@initial': 'mobile', '@sm': 'small' }}
  347. >
  348. <span>Align / Distribute</span>
  349. <IconWrapper size="small">
  350. <ChevronRightIcon />
  351. </IconWrapper>
  352. </_ContextMenu.TriggerItem>
  353. <StyledGrid
  354. sideOffset={2}
  355. alignOffset={-2}
  356. isMobile={isMobile()}
  357. selectedStyle={hasThreeOrMore ? 'threeOrMore' : 'twoOrMore'}
  358. >
  359. <IconButton onSelect={alignLeft}>
  360. <AlignLeftIcon />
  361. </IconButton>
  362. <IconButton onSelect={alignCenterHorizontal}>
  363. <AlignCenterHorizontallyIcon />
  364. </IconButton>
  365. <IconButton onSelect={alignRight}>
  366. <AlignRightIcon />
  367. </IconButton>
  368. <IconButton onSelect={stretchHorizontally}>
  369. <StretchHorizontallyIcon />
  370. </IconButton>
  371. {hasThreeOrMore && (
  372. <IconButton onSelect={distributeHorizontally}>
  373. <SpaceEvenlyHorizontallyIcon />
  374. </IconButton>
  375. )}
  376. <IconButton onSelect={alignTop}>
  377. <AlignTopIcon />
  378. </IconButton>
  379. <IconButton onSelect={alignCenterVertical}>
  380. <AlignCenterVerticallyIcon />
  381. </IconButton>
  382. <IconButton onSelect={alignBottom}>
  383. <AlignBottomIcon />
  384. </IconButton>
  385. <IconButton onSelect={stretchVertically}>
  386. <StretchVerticallyIcon />
  387. </IconButton>
  388. {hasThreeOrMore && (
  389. <IconButton onSelect={distributeVertically}>
  390. <SpaceEvenlyVerticallyIcon />
  391. </IconButton>
  392. )}
  393. <StyledArrow offset={13} />
  394. </StyledGrid>
  395. </_ContextMenu.Root>
  396. )
  397. }
  398. const StyledGrid = styled(StyledContent, {
  399. display: 'grid',
  400. variants: {
  401. selectedStyle: {
  402. threeOrMore: {
  403. gridTemplateColumns: 'repeat(5, auto)',
  404. },
  405. twoOrMore: {
  406. gridTemplateColumns: 'repeat(4, auto)',
  407. },
  408. },
  409. },
  410. })
  411. function MoveToPageMenu() {
  412. const documentPages = useSelector((s) => s.data.document.pages)
  413. const currentPageId = useSelector((s) => s.data.currentPageId)
  414. if (!documentPages[currentPageId]) return null
  415. const sorted = Object.values(documentPages)
  416. .sort((a, b) => a.childIndex - b.childIndex)
  417. .filter((a) => a.id !== currentPageId)
  418. if (sorted.length === 0) return null
  419. return (
  420. <_ContextMenu.Root>
  421. <_ContextMenu.TriggerItem
  422. as={RowButton}
  423. bp={{ '@initial': 'mobile', '@sm': 'small' }}
  424. >
  425. <span>Move To Page</span>
  426. <IconWrapper size="small">
  427. <ChevronRightIcon />
  428. </IconWrapper>
  429. </_ContextMenu.TriggerItem>
  430. <StyledContent sideOffset={2} alignOffset={-2} isMobile={isMobile()}>
  431. {sorted.map(({ id, name }) => (
  432. <Button
  433. key={id}
  434. disabled={id === currentPageId}
  435. onSelect={() => state.send('MOVED_TO_PAGE', { id })}
  436. >
  437. <span>{name}</span>
  438. </Button>
  439. ))}
  440. <StyledArrow offset={13} />
  441. </StyledContent>
  442. </_ContextMenu.Root>
  443. )
  444. }
  445. const StyledDialogContent = styled(_Dropdown.Content, {
  446. // position: 'fixed',
  447. // top: '50%',
  448. // left: '50%',
  449. // transform: 'translate(-50%, -50%)',
  450. // minWidth: 200,
  451. // maxWidth: 'fit-content',
  452. // maxHeight: '85vh',
  453. // marginTop: '-5vh',
  454. minWidth: 128,
  455. backgroundColor: '$panel',
  456. borderRadius: '4px',
  457. overflow: 'hidden',
  458. pointerEvents: 'all',
  459. userSelect: 'none',
  460. zIndex: 200,
  461. padding: 2,
  462. border: '1px solid $panel',
  463. boxShadow: '0px 2px 4px rgba(0,0,0,.2)',
  464. '&:focus': {
  465. outline: 'none',
  466. },
  467. })
  468. const StyledArrow = styled(_ContextMenu.Arrow, {
  469. fill: 'white',
  470. })