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.

shapes-functions.tsx 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. import * as React from 'react'
  2. import { IconButton, ButtonsRow, breakpoints } from '../shared'
  3. import { Trash } from '../icons'
  4. import { Tooltip } from '../tooltip'
  5. import {
  6. ArrowDownIcon,
  7. ArrowUpIcon,
  8. AspectRatioIcon,
  9. CopyIcon,
  10. GroupIcon,
  11. LockClosedIcon,
  12. LockOpen1Icon,
  13. PinBottomIcon,
  14. PinTopIcon,
  15. RotateCounterClockwiseIcon,
  16. } from '@radix-ui/react-icons'
  17. import { useTLDrawContext } from '~hooks'
  18. import type { Data } from '~types'
  19. const isAllLockedSelector = (s: Data) => {
  20. const { selectedIds } = s.pageState
  21. return selectedIds.every((id) => s.page.shapes[id].isLocked)
  22. }
  23. const isAllAspectLockedSelector = (s: Data) => {
  24. const { selectedIds } = s.pageState
  25. return selectedIds.every((id) => s.page.shapes[id].isAspectRatioLocked)
  26. }
  27. const isAllGroupedSelector = (s: Data) => {
  28. const selectedShapes = s.pageState.selectedIds.map((id) => s.page.shapes[id])
  29. return selectedShapes.every(
  30. (shape) =>
  31. shape.children !== undefined ||
  32. (shape.parentId === selectedShapes[0].parentId &&
  33. selectedShapes[0].parentId !== s.appState.currentPageId)
  34. )
  35. }
  36. const hasSelectionSelector = (s: Data) => s.pageState.selectedIds.length > 0
  37. const hasMultipleSelectionSelector = (s: Data) => s.pageState.selectedIds.length > 1
  38. export const ShapesFunctions = React.memo(() => {
  39. const { tlstate, useSelector } = useTLDrawContext()
  40. const isAllLocked = useSelector(isAllLockedSelector)
  41. const isAllAspectLocked = useSelector(isAllAspectLockedSelector)
  42. const isAllGrouped = useSelector(isAllGroupedSelector)
  43. const hasSelection = useSelector(hasSelectionSelector)
  44. const hasMultipleSelection = useSelector(hasMultipleSelectionSelector)
  45. const handleRotate = React.useCallback(() => {
  46. tlstate.rotate()
  47. }, [tlstate])
  48. const handleDuplicate = React.useCallback(() => {
  49. tlstate.duplicate()
  50. }, [tlstate])
  51. const handleToggleLocked = React.useCallback(() => {
  52. tlstate.toggleLocked()
  53. }, [tlstate])
  54. const handleToggleAspectRatio = React.useCallback(() => {
  55. tlstate.toggleAspectRatioLocked()
  56. }, [tlstate])
  57. const handleGroup = React.useCallback(() => {
  58. tlstate.group()
  59. }, [tlstate])
  60. const handleMoveToBack = React.useCallback(() => {
  61. tlstate.moveToBack()
  62. }, [tlstate])
  63. const handleMoveBackward = React.useCallback(() => {
  64. tlstate.moveBackward()
  65. }, [tlstate])
  66. const handleMoveForward = React.useCallback(() => {
  67. tlstate.moveForward()
  68. }, [tlstate])
  69. const handleMoveToFront = React.useCallback(() => {
  70. tlstate.moveToFront()
  71. }, [tlstate])
  72. const handleDelete = React.useCallback(() => {
  73. tlstate.delete()
  74. }, [tlstate])
  75. return (
  76. <>
  77. <ButtonsRow>
  78. <IconButton
  79. bp={breakpoints}
  80. disabled={!hasSelection}
  81. size="small"
  82. onClick={handleDuplicate}
  83. >
  84. <Tooltip label="Duplicate" kbd={`#D`}>
  85. <CopyIcon />
  86. </Tooltip>
  87. </IconButton>
  88. <IconButton disabled={!hasSelection} size="small" onClick={handleRotate}>
  89. <Tooltip label="Rotate">
  90. <RotateCounterClockwiseIcon />
  91. </Tooltip>
  92. </IconButton>
  93. <IconButton
  94. bp={breakpoints}
  95. disabled={!hasSelection}
  96. size="small"
  97. onClick={handleToggleLocked}
  98. >
  99. <Tooltip label="Toogle Locked" kbd={`#L`}>
  100. {isAllLocked ? <LockClosedIcon /> : <LockOpen1Icon opacity={0.4} />}
  101. </Tooltip>
  102. </IconButton>
  103. <IconButton
  104. bp={breakpoints}
  105. disabled={!hasSelection}
  106. size="small"
  107. onClick={handleToggleAspectRatio}
  108. >
  109. <Tooltip label="Toogle Aspect Ratio Lock">
  110. <AspectRatioIcon opacity={isAllAspectLocked ? 1 : 0.4} />
  111. </Tooltip>
  112. </IconButton>
  113. <IconButton
  114. bp={breakpoints}
  115. disabled={!isAllGrouped && !hasMultipleSelection}
  116. size="small"
  117. onClick={handleGroup}
  118. >
  119. <Tooltip label="Group" kbd={`#G`}>
  120. <GroupIcon opacity={isAllGrouped ? 1 : 0.4} />
  121. </Tooltip>
  122. </IconButton>
  123. </ButtonsRow>
  124. <ButtonsRow>
  125. <IconButton
  126. bp={breakpoints}
  127. disabled={!hasSelection}
  128. size="small"
  129. onClick={handleMoveToBack}
  130. >
  131. <Tooltip label="Move to Back" kbd={`#⇧[`}>
  132. <PinBottomIcon />
  133. </Tooltip>
  134. </IconButton>
  135. <IconButton
  136. bp={breakpoints}
  137. disabled={!hasSelection}
  138. size="small"
  139. onClick={handleMoveBackward}
  140. >
  141. <Tooltip label="Move Backward" kbd={`#[`}>
  142. <ArrowDownIcon />
  143. </Tooltip>
  144. </IconButton>
  145. <IconButton
  146. bp={breakpoints}
  147. disabled={!hasSelection}
  148. size="small"
  149. onClick={handleMoveForward}
  150. >
  151. <Tooltip label="Move Forward" kbd={`#]`}>
  152. <ArrowUpIcon />
  153. </Tooltip>
  154. </IconButton>
  155. <IconButton
  156. bp={breakpoints}
  157. disabled={!hasSelection}
  158. size="small"
  159. onClick={handleMoveToFront}
  160. >
  161. <Tooltip label="More to Front" kbd={`#⇧]`}>
  162. <PinTopIcon />
  163. </Tooltip>
  164. </IconButton>
  165. <IconButton bp={breakpoints} disabled={!hasSelection} size="small" onClick={handleDelete}>
  166. <Tooltip label="Delete" kbd="⌫">
  167. <Trash />
  168. </Tooltip>
  169. </IconButton>
  170. </ButtonsRow>
  171. </>
  172. )
  173. })