Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

context-menu.tsx 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. import * as _ContextMenu from '@radix-ui/react-context-menu'
  2. import styled from 'styles'
  3. import {
  4. IconWrapper,
  5. breakpoints,
  6. RowButton,
  7. ContextMenuArrow,
  8. ContextMenuDivider,
  9. ContextMenuButton,
  10. ContextMenuSubMenu,
  11. ContextMenuIconButton,
  12. ContextMenuRoot,
  13. MenuContent,
  14. } from 'components/shared'
  15. import { commandKey, deepCompareArrays, isMobile } from 'utils'
  16. import state, { useSelector } from 'state'
  17. import {
  18. AlignType,
  19. DistributeType,
  20. MoveType,
  21. ShapeType,
  22. StretchType,
  23. } from 'types'
  24. import tld from 'utils/tld'
  25. import React, { useRef } from 'react'
  26. import {
  27. ChevronRightIcon,
  28. AlignBottomIcon,
  29. AlignCenterHorizontallyIcon,
  30. AlignCenterVerticallyIcon,
  31. AlignLeftIcon,
  32. AlignRightIcon,
  33. AlignTopIcon,
  34. SpaceEvenlyHorizontallyIcon,
  35. SpaceEvenlyVerticallyIcon,
  36. StretchHorizontallyIcon,
  37. StretchVerticallyIcon,
  38. } from '@radix-ui/react-icons'
  39. function alignTop() {
  40. state.send('ALIGNED', { type: AlignType.Top })
  41. }
  42. function alignCenterVertical() {
  43. state.send('ALIGNED', { type: AlignType.CenterVertical })
  44. }
  45. function alignBottom() {
  46. state.send('ALIGNED', { type: AlignType.Bottom })
  47. }
  48. function stretchVertically() {
  49. state.send('STRETCHED', { type: StretchType.Vertical })
  50. }
  51. function distributeVertically() {
  52. state.send('DISTRIBUTED', { type: DistributeType.Vertical })
  53. }
  54. function alignLeft() {
  55. state.send('ALIGNED', { type: AlignType.Left })
  56. }
  57. function alignCenterHorizontal() {
  58. state.send('ALIGNED', { type: AlignType.CenterHorizontal })
  59. }
  60. function alignRight() {
  61. state.send('ALIGNED', { type: AlignType.Right })
  62. }
  63. function stretchHorizontally() {
  64. state.send('STRETCHED', { type: StretchType.Horizontal })
  65. }
  66. function distributeHorizontally() {
  67. state.send('DISTRIBUTED', { type: DistributeType.Horizontal })
  68. }
  69. export default function ContextMenu({
  70. children,
  71. }: {
  72. children: React.ReactNode
  73. }): JSX.Element {
  74. const selectedShapeIds = useSelector(
  75. (s) => s.values.selectedIds,
  76. deepCompareArrays
  77. )
  78. const rContent = useRef<HTMLDivElement>(null)
  79. const hasGroupSelected = useSelector((s) =>
  80. selectedShapeIds.some(
  81. (id) => tld.getShape(s.data, id)?.type === ShapeType.Group
  82. )
  83. )
  84. const hasTwoOrMore = selectedShapeIds.length > 1
  85. const hasThreeOrMore = selectedShapeIds.length > 2
  86. return (
  87. <ContextMenuRoot>
  88. <_ContextMenu.Trigger>{children}</_ContextMenu.Trigger>
  89. <MenuContent
  90. as={_ContextMenu.Content}
  91. ref={rContent}
  92. isMobile={isMobile()}
  93. >
  94. {selectedShapeIds.length ? (
  95. <>
  96. {/* <ContextMenuButton onSelect={() => state.send('COPIED')}>
  97. <span>Copy</span>
  98. <kbd>
  99. <span>{commandKey()}</span>
  100. <span>C</span>
  101. </kbd>
  102. </ContextMenuButton>
  103. <ContextMenuButton onSelect={() => state.send('CUT')}>
  104. <span>Cut</span>
  105. <kbd>
  106. <span>{commandKey()}</span>
  107. <span>X</span>
  108. </kbd>
  109. </ContextMenuButton>
  110. */}
  111. <ContextMenuButton onSelect={() => state.send('DUPLICATED')}>
  112. <span>Duplicate</span>
  113. <kbd>
  114. <span>{commandKey()}</span>
  115. <span>D</span>
  116. </kbd>
  117. </ContextMenuButton>
  118. <ContextMenuDivider />
  119. {hasGroupSelected ||
  120. (hasTwoOrMore && (
  121. <>
  122. {hasGroupSelected && (
  123. <ContextMenuButton onSelect={() => state.send('UNGROUPED')}>
  124. <span>Ungroup</span>
  125. <kbd>
  126. <span>{commandKey()}</span>
  127. <span>⇧</span>
  128. <span>G</span>
  129. </kbd>
  130. </ContextMenuButton>
  131. )}
  132. {hasTwoOrMore && (
  133. <ContextMenuButton onSelect={() => state.send('GROUPED')}>
  134. <span>Group</span>
  135. <kbd>
  136. <span>{commandKey()}</span>
  137. <span>G</span>
  138. </kbd>
  139. </ContextMenuButton>
  140. )}
  141. </>
  142. ))}
  143. <ContextMenuSubMenu label="Move">
  144. <ContextMenuButton
  145. onSelect={() =>
  146. state.send('MOVED', {
  147. type: MoveType.ToFront,
  148. })
  149. }
  150. >
  151. <span>To Front</span>
  152. <kbd>
  153. <span>{commandKey()}</span>
  154. <span>⇧</span>
  155. <span>]</span>
  156. </kbd>
  157. </ContextMenuButton>
  158. <ContextMenuButton
  159. onSelect={() =>
  160. state.send('MOVED', {
  161. type: MoveType.Forward,
  162. })
  163. }
  164. >
  165. <span>Forward</span>
  166. <kbd>
  167. <span>{commandKey()}</span>
  168. <span>]</span>
  169. </kbd>
  170. </ContextMenuButton>
  171. <ContextMenuButton
  172. onSelect={() =>
  173. state.send('MOVED', {
  174. type: MoveType.Backward,
  175. })
  176. }
  177. >
  178. <span>Backward</span>
  179. <kbd>
  180. <span>{commandKey()}</span>
  181. <span>[</span>
  182. </kbd>
  183. </ContextMenuButton>
  184. <ContextMenuButton
  185. onSelect={() =>
  186. state.send('MOVED', {
  187. type: MoveType.ToBack,
  188. })
  189. }
  190. >
  191. <span>To Back</span>
  192. <kbd>
  193. <span>{commandKey()}</span>
  194. <span>⇧</span>
  195. <span>[</span>
  196. </kbd>
  197. </ContextMenuButton>
  198. </ContextMenuSubMenu>
  199. {hasTwoOrMore && (
  200. <AlignDistributeSubMenu
  201. hasTwoOrMore={hasTwoOrMore}
  202. hasThreeOrMore={hasThreeOrMore}
  203. />
  204. )}
  205. <MoveToPageMenu />
  206. <ContextMenuButton onSelect={() => state.send('COPIED_TO_SVG')}>
  207. <span>Copy to SVG</span>
  208. <kbd>
  209. <span>{commandKey()}</span>
  210. <span>⇧</span>
  211. <span>C</span>
  212. </kbd>
  213. </ContextMenuButton>
  214. <ContextMenuDivider />
  215. <ContextMenuButton onSelect={() => state.send('DELETED')}>
  216. <span>Delete</span>
  217. <kbd>
  218. <span>⌫</span>
  219. </kbd>
  220. </ContextMenuButton>
  221. </>
  222. ) : (
  223. <>
  224. <ContextMenuButton onSelect={() => state.send('UNDO')}>
  225. <span>Undo</span>
  226. <kbd>
  227. <span>{commandKey()}</span>
  228. <span>Z</span>
  229. </kbd>
  230. </ContextMenuButton>
  231. <ContextMenuButton onSelect={() => state.send('REDO')}>
  232. <span>Redo</span>
  233. <kbd>
  234. <span>{commandKey()}</span>
  235. <span>⇧</span>
  236. <span>Z</span>
  237. </kbd>
  238. </ContextMenuButton>
  239. </>
  240. )}
  241. </MenuContent>
  242. </ContextMenuRoot>
  243. )
  244. }
  245. function AlignDistributeSubMenu({
  246. hasThreeOrMore,
  247. }: {
  248. hasTwoOrMore: boolean
  249. hasThreeOrMore: boolean
  250. }) {
  251. return (
  252. <ContextMenuRoot>
  253. <_ContextMenu.TriggerItem as={RowButton} bp={breakpoints}>
  254. <span>Align / Distribute</span>
  255. <IconWrapper size="small">
  256. <ChevronRightIcon />
  257. </IconWrapper>
  258. </_ContextMenu.TriggerItem>
  259. <StyledGrid
  260. as={_ContextMenu.Content}
  261. sideOffset={2}
  262. alignOffset={-2}
  263. isMobile={isMobile()}
  264. selectedStyle={hasThreeOrMore ? 'threeOrMore' : 'twoOrMore'}
  265. >
  266. <ContextMenuIconButton onSelect={alignLeft}>
  267. <AlignLeftIcon />
  268. </ContextMenuIconButton>
  269. <ContextMenuIconButton onSelect={alignCenterHorizontal}>
  270. <AlignCenterHorizontallyIcon />
  271. </ContextMenuIconButton>
  272. <ContextMenuIconButton onSelect={alignRight}>
  273. <AlignRightIcon />
  274. </ContextMenuIconButton>
  275. <ContextMenuIconButton onSelect={stretchHorizontally}>
  276. <StretchHorizontallyIcon />
  277. </ContextMenuIconButton>
  278. {hasThreeOrMore && (
  279. <ContextMenuIconButton onSelect={distributeHorizontally}>
  280. <SpaceEvenlyHorizontallyIcon />
  281. </ContextMenuIconButton>
  282. )}
  283. <ContextMenuIconButton onSelect={alignTop}>
  284. <AlignTopIcon />
  285. </ContextMenuIconButton>
  286. <ContextMenuIconButton onSelect={alignCenterVertical}>
  287. <AlignCenterVerticallyIcon />
  288. </ContextMenuIconButton>
  289. <ContextMenuIconButton onSelect={alignBottom}>
  290. <AlignBottomIcon />
  291. </ContextMenuIconButton>
  292. <ContextMenuIconButton onSelect={stretchVertically}>
  293. <StretchVerticallyIcon />
  294. </ContextMenuIconButton>
  295. {hasThreeOrMore && (
  296. <ContextMenuIconButton onSelect={distributeVertically}>
  297. <SpaceEvenlyVerticallyIcon />
  298. </ContextMenuIconButton>
  299. )}
  300. <ContextMenuArrow offset={13} />
  301. </StyledGrid>
  302. </ContextMenuRoot>
  303. )
  304. }
  305. const StyledGrid = styled(MenuContent, {
  306. display: 'grid',
  307. variants: {
  308. selectedStyle: {
  309. threeOrMore: {
  310. gridTemplateColumns: 'repeat(5, auto)',
  311. },
  312. twoOrMore: {
  313. gridTemplateColumns: 'repeat(4, auto)',
  314. },
  315. },
  316. },
  317. })
  318. function MoveToPageMenu() {
  319. const documentPages = useSelector((s) => s.data.document.pages)
  320. const currentPageId = useSelector((s) => s.data.currentPageId)
  321. if (!documentPages[currentPageId]) return null
  322. const sorted = Object.values(documentPages)
  323. .sort((a, b) => a.childIndex - b.childIndex)
  324. .filter((a) => a.id !== currentPageId)
  325. if (sorted.length === 0) return null
  326. return (
  327. <ContextMenuRoot>
  328. <ContextMenuButton>
  329. <span>Move To Page</span>
  330. <IconWrapper size="small">
  331. <ChevronRightIcon />
  332. </IconWrapper>
  333. </ContextMenuButton>
  334. <MenuContent
  335. as={_ContextMenu.Content}
  336. sideOffset={2}
  337. alignOffset={-2}
  338. isMobile={isMobile()}
  339. >
  340. {sorted.map(({ id, name }) => (
  341. <ContextMenuButton
  342. key={id}
  343. disabled={id === currentPageId}
  344. onSelect={() => state.send('MOVED_TO_PAGE', { id })}
  345. >
  346. <span>{name}</span>
  347. </ContextMenuButton>
  348. ))}
  349. <ContextMenuArrow offset={13} />
  350. </MenuContent>
  351. </ContextMenuRoot>
  352. )
  353. }