@onesy/ui-react 1.0.130 → 1.0.131

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (444) hide show
  1. package/Accordion/Accordion.js +342 -242
  2. package/Accordion/index.js +12 -7
  3. package/AdvancedTextField/AdvancedTextField.js +165 -157
  4. package/AdvancedTextField/index.js +12 -7
  5. package/Append/Append.js +502 -519
  6. package/Append/index.js +12 -7
  7. package/AreaChart/AreaChart.js +53 -40
  8. package/AreaChart/index.js +12 -7
  9. package/AreaChartItem/AreaChartItem.js +325 -237
  10. package/AreaChartItem/index.js +12 -7
  11. package/AudioPlayer/AudioPlayer.js +683 -466
  12. package/AudioPlayer/index.js +12 -7
  13. package/AudioRecorder/AudioRecorder.js +359 -259
  14. package/AudioRecorder/index.js +12 -7
  15. package/AutoComplete/AutoComplete.js +693 -568
  16. package/AutoComplete/index.js +12 -7
  17. package/AutoCompleteCountry/AutoCompleteCountry.js +99 -62
  18. package/AutoCompleteCountry/index.js +12 -7
  19. package/AutoCompleteCurrency/AutoCompleteCurrency.js +73 -61
  20. package/AutoCompleteCurrency/index.js +12 -7
  21. package/Avatar/Avatar.js +113 -86
  22. package/Avatar/index.js +12 -7
  23. package/AvatarGroup/AvatarGroup.js +104 -87
  24. package/AvatarGroup/index.js +12 -7
  25. package/Backdrop/Backdrop.js +160 -139
  26. package/Backdrop/index.js +12 -7
  27. package/Badge/Badge.js +116 -96
  28. package/Badge/index.js +12 -7
  29. package/Banner/Banner.js +126 -85
  30. package/Banner/index.js +12 -7
  31. package/BottomAppBar/BottomAppBar.js +89 -60
  32. package/BottomAppBar/index.js +12 -7
  33. package/BottomSheet/BottomSheet.js +66 -59
  34. package/BottomSheet/index.js +12 -7
  35. package/Box/Box.js +43 -35
  36. package/Box/index.js +12 -7
  37. package/Breadcrumbs/Breadcrumbs.js +168 -144
  38. package/Breadcrumbs/index.js +12 -7
  39. package/BubbleChart/BubbleChart.js +261 -132
  40. package/BubbleChart/index.js +12 -7
  41. package/BubbleChartItem/BubbleChartItem.js +228 -161
  42. package/BubbleChartItem/index.js +12 -7
  43. package/Button/Button.js +488 -467
  44. package/Button/index.js +12 -7
  45. package/Buttons/Buttons.js +366 -293
  46. package/Buttons/index.js +12 -7
  47. package/Calendar/Calendar.js +715 -452
  48. package/Calendar/index.js +12 -7
  49. package/CalendarAvailability/CalendarAvailability.js +709 -388
  50. package/CalendarAvailability/index.js +12 -7
  51. package/CalendarMenu/CalendarMenu.js +430 -303
  52. package/CalendarMenu/index.js +12 -7
  53. package/CalendarMonth/CalendarMonth.js +537 -498
  54. package/CalendarMonth/index.js +12 -7
  55. package/CalendarViews/CalendarViews.js +403 -254
  56. package/CalendarViews/index.js +12 -7
  57. package/CalendarWeek/CalendarWeek.js +536 -395
  58. package/CalendarWeek/index.js +12 -7
  59. package/Card/Card.js +133 -101
  60. package/Card/index.js +12 -7
  61. package/CardButton/CardButton.js +78 -58
  62. package/CardButton/index.js +12 -7
  63. package/CardFooter/CardFooter.js +58 -39
  64. package/CardFooter/index.js +12 -7
  65. package/CardHeader/CardHeader.js +61 -44
  66. package/CardHeader/index.js +12 -7
  67. package/CardImage/CardImage.js +80 -67
  68. package/CardImage/index.js +12 -7
  69. package/CardMain/CardMain.js +53 -39
  70. package/CardMain/index.js +12 -7
  71. package/Carousel/Carousel.js +1025 -913
  72. package/Carousel/index.js +12 -7
  73. package/Chart/Chart.js +1325 -1174
  74. package/Chart/index.js +12 -7
  75. package/Checkbox/Checkbox.js +339 -325
  76. package/Checkbox/index.js +12 -7
  77. package/Chip/Chip.js +131 -102
  78. package/Chip/index.js +12 -7
  79. package/Chips/Chips.js +121 -94
  80. package/Chips/index.js +12 -7
  81. package/ClickListener/ClickListener.js +86 -89
  82. package/ClickListener/index.js +12 -7
  83. package/Clock/Clock.js +470 -417
  84. package/Clock/index.js +12 -7
  85. package/ColorTextField/ColorTextField.js +234 -171
  86. package/ColorTextField/index.js +12 -7
  87. package/ColumnChart/ColumnChart.js +63 -46
  88. package/ColumnChart/index.js +12 -7
  89. package/ColumnChartItem/ColumnChartItem.js +208 -149
  90. package/ColumnChartItem/index.js +12 -7
  91. package/Confirm/Confirm.js +184 -129
  92. package/Confirm/Context.js +9 -7
  93. package/Confirm/index.js +32 -13
  94. package/Confirm/useConfirm.js +11 -10
  95. package/Container/Container.js +154 -133
  96. package/Container/index.js +12 -7
  97. package/CookieBanner/CookieBanner.js +142 -76
  98. package/CookieBanner/index.js +12 -7
  99. package/Countdown/Countdown.js +403 -255
  100. package/Countdown/index.js +12 -7
  101. package/DatePicker/DatePicker.js +802 -547
  102. package/DatePicker/index.js +12 -7
  103. package/DateRangePicker/DateRangePicker.js +44 -37
  104. package/DateRangePicker/index.js +12 -7
  105. package/DateTimePicker/DateTimePicker.js +672 -459
  106. package/DateTimePicker/index.js +12 -7
  107. package/DateTimeRangePicker/DateTimeRangePicker.js +44 -37
  108. package/DateTimeRangePicker/index.js +12 -7
  109. package/Divider/Divider.js +198 -185
  110. package/Divider/index.js +12 -7
  111. package/DonutChart/DonutChart.js +49 -42
  112. package/DonutChart/index.js +12 -7
  113. package/DragAndDropList/DragAndDropList.js +160 -156
  114. package/DragAndDropList/index.js +12 -7
  115. package/Drawing/Drawing.js +850 -523
  116. package/Drawing/index.js +12 -7
  117. package/DropZone/DropZone.js +239 -201
  118. package/DropZone/index.js +12 -7
  119. package/Emojis/Emojis.js +409 -317
  120. package/Emojis/emojis_list.js +7279 -9250
  121. package/Emojis/index.js +19 -9
  122. package/Expand/Expand.js +201 -149
  123. package/Expand/index.js +12 -7
  124. package/Fab/Fab.js +46 -26
  125. package/Fab/index.js +12 -7
  126. package/Fade/Fade.js +131 -101
  127. package/Fade/index.js +12 -7
  128. package/FileChoose/FileChoose.js +218 -150
  129. package/FileChoose/index.js +12 -7
  130. package/Focus/Focus.js +87 -88
  131. package/Focus/index.js +12 -7
  132. package/Form/Form.js +194 -120
  133. package/Form/index.js +12 -7
  134. package/FormRow/FormRow.js +123 -78
  135. package/FormRow/index.js +12 -7
  136. package/Forms/Forms.js +55 -41
  137. package/Forms/index.js +12 -7
  138. package/Frame/Frame.js +212 -150
  139. package/Frame/index.js +12 -7
  140. package/Grid/Grid.js +231 -166
  141. package/Grid/index.js +12 -7
  142. package/Grow/Grow.js +137 -109
  143. package/Grow/index.js +12 -7
  144. package/HTMLCanvas/HTMLCanvas.js +752 -542
  145. package/HTMLCanvas/index.js +24 -21
  146. package/IFrame/IFrame.js +122 -104
  147. package/IFrame/index.js +12 -7
  148. package/Icon/Icon.js +112 -95
  149. package/Icon/index.js +24 -21
  150. package/IconButton/IconButton.js +49 -37
  151. package/IconButton/index.js +12 -7
  152. package/Image/Image.js +285 -252
  153. package/Image/index.js +12 -7
  154. package/ImageCrop/ImageCrop.js +1073 -1048
  155. package/ImageCrop/index.js +12 -7
  156. package/ImageEdit/ImageEdit.js +1088 -783
  157. package/ImageEdit/index.js +12 -7
  158. package/ImageGallery/ImageGallery.js +518 -473
  159. package/ImageGallery/index.js +12 -7
  160. package/ImageList/ImageList.js +136 -129
  161. package/ImageList/index.js +12 -7
  162. package/ImageListItem/ImageListItem.js +101 -87
  163. package/ImageListItem/index.js +12 -7
  164. package/ImageListItemBox/ImageListItemBox.js +61 -50
  165. package/ImageListItemBox/index.js +12 -7
  166. package/Info/Info.js +99 -63
  167. package/Info/index.js +12 -7
  168. package/Interaction/Interaction.js +405 -345
  169. package/Interaction/index.js +12 -7
  170. package/Keyframes/Context.js +9 -7
  171. package/Keyframes/Keyframes.js +189 -174
  172. package/Keyframes/index.js +42 -25
  173. package/Keyframes/useKeyframes.js +11 -10
  174. package/Label/Label.js +177 -151
  175. package/Label/index.js +12 -7
  176. package/Labels/Labels.js +126 -98
  177. package/Labels/index.js +12 -7
  178. package/Line/Line.js +296 -174
  179. package/Line/index.js +12 -7
  180. package/LineChart/LineChart.js +57 -40
  181. package/LineChart/index.js +12 -7
  182. package/LineChartItem/LineChartItem.js +239 -177
  183. package/LineChartItem/index.js +12 -7
  184. package/LinearMeter/LinearMeter.js +453 -377
  185. package/LinearMeter/index.js +12 -7
  186. package/LinearProgress/LinearProgress.js +324 -255
  187. package/LinearProgress/index.js +12 -7
  188. package/Link/Link.js +182 -127
  189. package/Link/index.js +12 -7
  190. package/Links/Links.js +277 -156
  191. package/Links/index.js +12 -7
  192. package/List/List.js +148 -113
  193. package/List/index.js +12 -7
  194. package/ListItem/ListItem.js +388 -362
  195. package/ListItem/index.js +12 -7
  196. package/ListSubheader/ListSubheader.js +52 -44
  197. package/ListSubheader/index.js +12 -7
  198. package/MainProgress/Context.js +9 -7
  199. package/MainProgress/MainProgress.js +193 -161
  200. package/MainProgress/index.js +32 -13
  201. package/MainProgress/useMainProgress.js +11 -10
  202. package/Markdown/Markdown.js +676 -704
  203. package/Markdown/index.js +12 -7
  204. package/Masonry/Masonry.js +207 -181
  205. package/Masonry/index.js +12 -7
  206. package/Medias/Medias.js +436 -398
  207. package/Medias/index.js +12 -7
  208. package/Menu/Menu.js +278 -243
  209. package/Menu/index.js +12 -7
  210. package/MenuDesktop/MenuDesktop.js +496 -419
  211. package/MenuDesktop/index.js +12 -7
  212. package/MenuItem/MenuItem.js +286 -231
  213. package/MenuItem/index.js +12 -7
  214. package/Meta/Meta.js +78 -54
  215. package/Meta/index.js +12 -7
  216. package/Modal/Modal.js +333 -272
  217. package/Modal/index.js +12 -7
  218. package/ModalFooter/ModalFooter.js +50 -39
  219. package/ModalFooter/index.js +12 -7
  220. package/ModalHeader/ModalHeader.js +51 -39
  221. package/ModalHeader/index.js +12 -7
  222. package/ModalIcon/ModalIcon.js +29 -23
  223. package/ModalIcon/index.js +12 -7
  224. package/ModalMain/ModalMain.js +50 -41
  225. package/ModalMain/index.js +12 -7
  226. package/ModalText/ModalText.js +47 -37
  227. package/ModalText/index.js +12 -7
  228. package/ModalTitle/ModalTitle.js +47 -37
  229. package/ModalTitle/index.js +12 -7
  230. package/MoreOptions/MoreOptions.js +72 -52
  231. package/MoreOptions/index.js +12 -7
  232. package/Move/Move.js +166 -141
  233. package/Move/index.js +12 -7
  234. package/NavigationBar/NavigationBar.js +133 -109
  235. package/NavigationBar/index.js +12 -7
  236. package/NavigationDrawer/NavigationDrawer.js +241 -211
  237. package/NavigationDrawer/index.js +12 -7
  238. package/NavigationItem/NavigationItem.js +293 -262
  239. package/NavigationItem/index.js +12 -7
  240. package/NavigationRail/NavigationRail.js +187 -150
  241. package/NavigationRail/index.js +12 -7
  242. package/NotFound/NotFound.js +89 -66
  243. package/NotFound/index.js +12 -7
  244. package/NumericTextField/NumericTextField.js +217 -212
  245. package/NumericTextField/index.js +12 -7
  246. package/Page/Page.js +131 -67
  247. package/Page/index.js +12 -7
  248. package/PageTransition/PageTransition.js +112 -102
  249. package/PageTransition/index.js +12 -7
  250. package/Pagination/Pagination.js +153 -98
  251. package/Pagination/index.js +12 -7
  252. package/PaginationItem/PaginationItem.js +85 -65
  253. package/PaginationItem/index.js +12 -7
  254. package/Parallax/Parallax.js +138 -145
  255. package/Parallax/index.js +12 -7
  256. package/Path/Path.js +40 -35
  257. package/Path/index.js +12 -7
  258. package/PieChart/PieChart.js +382 -267
  259. package/PieChart/index.js +12 -7
  260. package/Placeholder/Placeholder.js +125 -117
  261. package/Placeholder/index.js +12 -7
  262. package/Portal/Portal.js +37 -38
  263. package/Portal/index.js +12 -7
  264. package/Properties/Properties.js +85 -64
  265. package/Properties/index.js +12 -7
  266. package/Property/Property.js +120 -108
  267. package/Property/index.js +12 -7
  268. package/Radio/Radio.js +211 -181
  269. package/Radio/index.js +12 -7
  270. package/Radios/Radios.js +124 -92
  271. package/Radios/index.js +12 -7
  272. package/Rating/Rating.js +353 -360
  273. package/Rating/index.js +12 -7
  274. package/Reset/Reset.js +19 -24
  275. package/Reset/index.js +12 -7
  276. package/Reveal/Reveal.js +98 -89
  277. package/Reveal/index.js +12 -7
  278. package/RichTextEditor/RichTextEditor.js +1764 -972
  279. package/RichTextEditor/index.js +12 -7
  280. package/RoundMeter/RoundMeter.js +629 -537
  281. package/RoundMeter/index.js +12 -7
  282. package/RoundProgress/RoundProgress.js +233 -174
  283. package/RoundProgress/index.js +12 -7
  284. package/ScatterChart/ScatterChart.js +53 -40
  285. package/ScatterChart/index.js +12 -7
  286. package/ScatterChartItem/ScatterChartItem.js +214 -153
  287. package/ScatterChartItem/index.js +12 -7
  288. package/ScreenCapture/ScreenCapture.js +427 -335
  289. package/ScreenCapture/index.js +12 -7
  290. package/Section/Section.js +380 -319
  291. package/Section/index.js +12 -7
  292. package/SectionAction/SectionAction.js +72 -61
  293. package/SectionAction/index.js +12 -7
  294. package/SectionBoxes/SectionBoxes.js +240 -208
  295. package/SectionBoxes/index.js +12 -7
  296. package/SectionCards/SectionCards.js +256 -217
  297. package/SectionCards/index.js +12 -7
  298. package/SectionCarousel/SectionCarousel.js +284 -242
  299. package/SectionCarousel/index.js +12 -7
  300. package/SectionContact/SectionContact.js +174 -116
  301. package/SectionContact/index.js +12 -7
  302. package/SectionImageGallery/SectionImageGallery.js +64 -51
  303. package/SectionImageGallery/index.js +12 -7
  304. package/SectionLogos/SectionLogos.js +123 -109
  305. package/SectionLogos/index.js +12 -7
  306. package/SectionMedia/SectionMedia.js +170 -152
  307. package/SectionMedia/index.js +12 -7
  308. package/SectionReviews/SectionReviews.js +170 -129
  309. package/SectionReviews/index.js +12 -7
  310. package/SectionTextMedia/SectionTextMedia.js +187 -142
  311. package/SectionTextMedia/index.js +12 -7
  312. package/SectionTimeline/SectionTimeline.js +126 -95
  313. package/SectionTimeline/index.js +12 -7
  314. package/SectionWatch/SectionWatch.js +65 -53
  315. package/SectionWatch/index.js +12 -7
  316. package/Select/Select.js +517 -406
  317. package/Select/index.js +12 -7
  318. package/Share/Share.js +458 -319
  319. package/Share/index.js +12 -7
  320. package/Slide/Slide.js +184 -140
  321. package/Slide/index.js +12 -7
  322. package/Slider/Slider.js +927 -837
  323. package/Slider/index.js +12 -7
  324. package/SmartTextField/SmartTextField.js +1406 -1014
  325. package/SmartTextField/index.js +12 -7
  326. package/Snackbar/Snackbar.js +227 -185
  327. package/Snackbar/index.js +12 -7
  328. package/Snackbars/Context.js +9 -7
  329. package/Snackbars/Snackbars.js +234 -210
  330. package/Snackbars/index.js +32 -13
  331. package/Snackbars/useSnackbars.js +11 -10
  332. package/Space/Space.js +1683 -968
  333. package/Space/index.js +24 -21
  334. package/SpeechToText/SpeechToText.js +207 -172
  335. package/SpeechToText/index.js +12 -7
  336. package/SpeedDial/SpeedDial.js +434 -398
  337. package/SpeedDial/index.js +12 -7
  338. package/SpeedDialItem/SpeedDialItem.js +89 -63
  339. package/SpeedDialItem/index.js +12 -7
  340. package/SpyScroll/SpyScroll.js +154 -142
  341. package/SpyScroll/index.js +12 -7
  342. package/Step/Step.js +181 -127
  343. package/Step/index.js +12 -7
  344. package/Stepper/Stepper.js +159 -139
  345. package/Stepper/index.js +12 -7
  346. package/Surface/Surface.js +760 -798
  347. package/Surface/index.js +12 -7
  348. package/Switch/Switch.js +688 -532
  349. package/Switch/index.js +12 -7
  350. package/Tab/Tab.js +185 -150
  351. package/Tab/index.js +12 -7
  352. package/Table/Table.js +100 -77
  353. package/Table/index.js +12 -7
  354. package/TableBody/TableBody.js +76 -63
  355. package/TableBody/index.js +12 -7
  356. package/TableCell/TableCell.js +258 -220
  357. package/TableCell/index.js +12 -7
  358. package/TableFooter/TableFooter.js +60 -48
  359. package/TableFooter/index.js +12 -7
  360. package/TableHead/TableHead.js +139 -120
  361. package/TableHead/index.js +12 -7
  362. package/TableHeader/TableHeader.js +65 -50
  363. package/TableHeader/index.js +12 -7
  364. package/TablePagination/TablePagination.js +171 -98
  365. package/TablePagination/index.js +12 -7
  366. package/TableRow/TableRow.js +77 -64
  367. package/TableRow/index.js +12 -7
  368. package/Tabs/Tabs.js +425 -377
  369. package/Tabs/index.js +12 -7
  370. package/Text/Text.js +131 -113
  371. package/Text/index.js +12 -7
  372. package/TextField/TextField.js +940 -879
  373. package/TextField/index.js +12 -7
  374. package/TextToSpeech/TextToSpeech.js +176 -153
  375. package/TextToSpeech/index.js +12 -7
  376. package/TimePicker/TimePicker.js +985 -718
  377. package/TimePicker/index.js +12 -7
  378. package/TimeRangePicker/TimeRangePicker.js +44 -37
  379. package/TimeRangePicker/index.js +12 -7
  380. package/Timeline/Timeline.js +54 -42
  381. package/Timeline/index.js +12 -7
  382. package/TimelineItem/TimelineItem.js +144 -147
  383. package/TimelineItem/index.js +12 -7
  384. package/Timer/Timer.js +321 -204
  385. package/Timer/index.js +12 -7
  386. package/ToggleButton/ToggleButton.js +67 -58
  387. package/ToggleButton/index.js +12 -7
  388. package/ToggleButtons/ToggleButtons.js +46 -37
  389. package/ToggleButtons/index.js +12 -7
  390. package/Tooltip/Tooltip.js +489 -454
  391. package/Tooltip/index.js +12 -7
  392. package/TopAppBar/TopAppBar.js +237 -197
  393. package/TopAppBar/index.js +12 -7
  394. package/Transition/Context.js +9 -7
  395. package/Transition/Transition.js +321 -339
  396. package/Transition/index.js +42 -25
  397. package/Transition/useTransition.js +11 -10
  398. package/Transitions/Transitions.js +215 -179
  399. package/Transitions/index.js +12 -7
  400. package/Tree/Tree.js +375 -312
  401. package/Tree/index.js +12 -7
  402. package/Type/Type.js +251 -152
  403. package/Type/index.js +12 -7
  404. package/VideoPlayer/VideoPlayer.js +866 -656
  405. package/VideoPlayer/index.js +12 -7
  406. package/ViewSplit/ViewSplit.js +413 -387
  407. package/ViewSplit/index.js +12 -7
  408. package/Watch/Watch.js +436 -290
  409. package/Watch/index.js +12 -7
  410. package/Weather/Weather.js +294 -247
  411. package/Weather/index.js +12 -7
  412. package/Whiteboard/Whiteboard.js +1392 -1292
  413. package/Whiteboard/index.js +12 -7
  414. package/Widgets/Context.js +9 -7
  415. package/Widgets/Widgets.js +220 -168
  416. package/Widgets/index.js +32 -13
  417. package/Widgets/useWidgets.js +11 -10
  418. package/WindowSplit/WindowSplit.js +381 -336
  419. package/WindowSplit/index.js +12 -7
  420. package/Zoom/Zoom.js +126 -99
  421. package/Zoom/index.js +12 -7
  422. package/esm/Buttons/Buttons.js +1 -1
  423. package/esm/index.js +1 -1
  424. package/index.js +4053 -660
  425. package/package.json +1 -1
  426. package/types.js +4 -1
  427. package/useForm/index.js +25 -11
  428. package/useForm/useForm.js +203 -174
  429. package/useForm/validate.js +215 -203
  430. package/useLocation/index.js +12 -7
  431. package/useLocation/useLocation.js +54 -49
  432. package/useMediaQuery/index.js +12 -7
  433. package/useMediaQuery/useMediaQuery.js +44 -43
  434. package/useQuery/index.js +12 -7
  435. package/useQuery/useQuery.js +14 -10
  436. package/useScroll/index.js +12 -7
  437. package/useScroll/useScroll.js +61 -56
  438. package/useSubscription/index.js +12 -7
  439. package/useSubscription/useSubscription.js +35 -35
  440. package/useSwipe/index.js +12 -7
  441. package/useSwipe/useSwipe.js +157 -131
  442. package/useVisible/index.js +12 -7
  443. package/useVisible/useVisible.js +86 -76
  444. package/utils.js +1224 -1362
@@ -1,1076 +1,1101 @@
1
1
  "use strict";
2
- var __rest = (this && this.__rest) || function (s, e) {
3
- var t = {};
4
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
5
- t[p] = s[p];
6
- if (s != null && typeof Object.getOwnPropertySymbols === "function")
7
- for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
8
- if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
9
- t[p[i]] = s[p[i]];
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.default = void 0;
8
+ var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
9
+ var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
10
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
11
+ var _react = _interopRequireDefault(require("react"));
12
+ var _utils = require("@onesy/utils");
13
+ var _styleReact = require("@onesy/style-react");
14
+ var _Tooltip = _interopRequireDefault(require("../Tooltip"));
15
+ var _utils2 = require("../utils");
16
+ const _excluded = ["image", "minWidth", "minHeight", "maxWidth", "maxHeight", "selectorDefault", "selector", "onSelectorChange", "type", "quality", "aspectRatio", "gridLines", "dynamicParent", "onFocus", "onBlur", "TooltipProps", "Component", "className"];
17
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
18
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2.default)(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
19
+ const useStyle = (0, _styleReact.style)(theme => {
20
+ const dot = {
21
+ display: 'inline-block',
22
+ position: 'absolute',
23
+ width: '6px',
24
+ height: '6px',
25
+ borderRadius: '50%',
26
+ background: '#a4a4a4',
27
+ outline: '1px solid white'
28
+ };
29
+ return {
30
+ root: {
31
+ width: '100%',
32
+ minHeight: '1px',
33
+ lineHeight: '0',
34
+ userSelect: 'none',
35
+ overflow: 'hidden'
36
+ },
37
+ mouseDown_move: {
38
+ '& *': {
39
+ cursor: 'grab !important'
40
+ }
41
+ },
42
+ mouseDown_top: {
43
+ '& *': {
44
+ cursor: 'ns-resize !important'
45
+ }
46
+ },
47
+ mouseDown_left: {
48
+ '& *': {
49
+ cursor: 'ew-resize !important'
50
+ }
51
+ },
52
+ mouseDown_right: {
53
+ '& *': {
54
+ cursor: 'ew-resize !important'
55
+ }
56
+ },
57
+ mouseDown_bottom: {
58
+ '& *': {
59
+ cursor: 'ns-resize !important'
60
+ }
61
+ },
62
+ mouseDown_top_left: {
63
+ '& *': {
64
+ cursor: 'nwse-resize !important'
65
+ }
66
+ },
67
+ mouseDown_top_right: {
68
+ '& *': {
69
+ cursor: 'nesw-resize !important'
70
+ }
71
+ },
72
+ mouseDown_bottom_left: {
73
+ '& *': {
74
+ cursor: 'nesw-resize !important'
75
+ }
76
+ },
77
+ mouseDown_bottom_right: {
78
+ '& *': {
79
+ cursor: 'nwse-resize !important'
80
+ }
81
+ },
82
+ canvas: {
83
+ position: 'absolute',
84
+ zIndex: '0'
85
+ },
86
+ canvas_main: {
87
+ position: 'relative',
88
+ width: '100%',
89
+ height: 'auto'
90
+ },
91
+ canvas_imageSelector: {
92
+ zIndex: -1
93
+ },
94
+ background: {
95
+ position: 'absolute',
96
+ inset: '0',
97
+ width: '100%',
98
+ height: '100%',
99
+ opacity: '0',
100
+ background: theme.methods.palette.color.colorToRgb(theme.palette.text.default.primary, 44),
101
+ transition: theme.methods.transitions.make('opacity', {
102
+ duration: 'xxs'
103
+ }),
104
+ zIndex: '1'
105
+ },
106
+ background_in: {
107
+ opacity: '1'
108
+ },
109
+ imageSelector_main: {
110
+ position: 'absolute',
111
+ top: '0',
112
+ left: '0',
113
+ width: '0',
114
+ height: '0',
115
+ outline: '2px dashed white',
116
+ background: 'transparent',
117
+ touchAction: 'none',
118
+ opacity: '0',
119
+ zIndex: '14',
120
+ userSelect: 'none',
121
+ transition: theme.methods.transitions.make('opacity')
122
+ },
123
+ imageSelector_main_in: {
124
+ opacity: '1'
125
+ },
126
+ imageSelector: {
127
+ position: 'absolute',
128
+ top: '0',
129
+ left: '0',
130
+ width: '0',
131
+ height: '0',
132
+ touchAction: 'none',
133
+ overflow: 'hidden',
134
+ userSelect: 'none',
135
+ zIndex: '11'
136
+ },
137
+ move: {
138
+ position: 'absolute',
139
+ inset: '0',
140
+ width: '100%',
141
+ height: '100%',
142
+ cursor: 'grab',
143
+ '&:active': {
144
+ cursor: 'grabbing !important'
145
+ }
146
+ },
147
+ grid_line: {
148
+ position: 'absolute',
149
+ background: 'white',
150
+ mixBlendMode: 'difference'
151
+ },
152
+ grid_line_top_start: {
153
+ top: '33.3333%',
154
+ insetInline: '0',
155
+ width: '100%',
156
+ height: '1px'
157
+ },
158
+ grid_line_top_end: {
159
+ top: '66.6666%',
160
+ insetInline: '0',
161
+ width: '100%',
162
+ height: '1px'
163
+ },
164
+ grid_line_left_start: {
165
+ left: '33.3333%',
166
+ insetBlock: '0',
167
+ width: '1px',
168
+ height: '100%'
169
+ },
170
+ grid_line_left_end: {
171
+ left: '66.6666%',
172
+ insetBlock: '0',
173
+ width: '1px',
174
+ height: '100%'
175
+ },
176
+ dot: _objectSpread({}, dot),
177
+ dot_top_left: {
178
+ top: '-1px',
179
+ left: '-1px',
180
+ transform: 'translate(-50%, -50%)',
181
+ cursor: 'nwse-resize'
182
+ },
183
+ dot_top_right: {
184
+ top: '-1px',
185
+ right: '-1px',
186
+ transform: 'translate(50%, -50%)',
187
+ cursor: 'nesw-resize'
188
+ },
189
+ dot_bottom_left: {
190
+ bottom: '-1px',
191
+ left: '-1px',
192
+ transform: 'translate(-50%, 50%)',
193
+ cursor: 'nesw-resize'
194
+ },
195
+ dot_bottom_right: {
196
+ bottom: '-1px',
197
+ right: '-1px',
198
+ transform: 'translate(50%, 50%)',
199
+ cursor: 'nwse-resize'
200
+ },
201
+ border: {
202
+ position: 'absolute'
203
+ },
204
+ border_top: {
205
+ top: '-2px',
206
+ height: '2px',
207
+ width: '100%',
208
+ cursor: 'ns-resize',
209
+ '&::before': _objectSpread(_objectSpread({}, dot), {}, {
210
+ content: '""',
211
+ top: '50%',
212
+ left: '50%',
213
+ transform: 'translate(-50%, -50%)'
214
+ })
215
+ },
216
+ border_left: {
217
+ left: '-2px',
218
+ height: '100%',
219
+ width: '2px',
220
+ cursor: 'ew-resize',
221
+ '&::before': _objectSpread(_objectSpread({}, dot), {}, {
222
+ content: '""',
223
+ top: '50%',
224
+ left: '50%',
225
+ transform: 'translate(-50%, -50%)'
226
+ })
227
+ },
228
+ border_right: {
229
+ right: '-2px',
230
+ height: '100%',
231
+ width: '2px',
232
+ cursor: 'ew-resize',
233
+ '&::before': _objectSpread(_objectSpread({}, dot), {}, {
234
+ content: '""',
235
+ top: '50%',
236
+ right: '50%',
237
+ transform: 'translate(50%, -50%)'
238
+ })
239
+ },
240
+ border_bottom: {
241
+ bottom: '-2px',
242
+ height: '2px',
243
+ width: '100%',
244
+ cursor: 'ns-resize',
245
+ '&::before': _objectSpread(_objectSpread({}, dot), {}, {
246
+ content: '""',
247
+ bottom: '50%',
248
+ left: '50%',
249
+ transform: 'translate(-50%, 50%)'
250
+ })
251
+ }
252
+ };
253
+ }, {
254
+ name: 'onesy-ImageCrop'
255
+ });
256
+ const ImageCrop = /*#__PURE__*/_react.default.forwardRef((props_, ref) => {
257
+ var _refs$root$current4;
258
+ const theme = (0, _styleReact.useOnesyTheme)();
259
+ const props = _react.default.useMemo(() => {
260
+ var _theme$ui, _theme$ui2;
261
+ return _objectSpread(_objectSpread(_objectSpread({}, theme === null || theme === void 0 || (_theme$ui = theme.ui) === null || _theme$ui === void 0 || (_theme$ui = _theme$ui.elements) === null || _theme$ui === void 0 || (_theme$ui = _theme$ui.all) === null || _theme$ui === void 0 || (_theme$ui = _theme$ui.props) === null || _theme$ui === void 0 ? void 0 : _theme$ui.default), theme === null || theme === void 0 || (_theme$ui2 = theme.ui) === null || _theme$ui2 === void 0 || (_theme$ui2 = _theme$ui2.elements) === null || _theme$ui2 === void 0 || (_theme$ui2 = _theme$ui2.onesyImageCrop) === null || _theme$ui2 === void 0 || (_theme$ui2 = _theme$ui2.props) === null || _theme$ui2 === void 0 ? void 0 : _theme$ui2.default), props_);
262
+ }, [props_]);
263
+ const Tooltip = _react.default.useMemo(() => {
264
+ var _theme$elements;
265
+ return (theme === null || theme === void 0 || (_theme$elements = theme.elements) === null || _theme$elements === void 0 ? void 0 : _theme$elements.Tooltip) || _Tooltip.default;
266
+ }, [theme]);
267
+ const {
268
+ // url, datauri or canvas
269
+ image: image_,
270
+ minWidth,
271
+ minHeight,
272
+ maxWidth,
273
+ maxHeight,
274
+ selectorDefault,
275
+ selector: selector_,
276
+ onSelectorChange: onSelectorChange_,
277
+ type = `image/jpeg`,
278
+ quality = 1,
279
+ aspectRatio,
280
+ gridLines,
281
+ dynamicParent,
282
+ onFocus: onFocus_,
283
+ onBlur: onBlur_,
284
+ TooltipProps,
285
+ Component = 'div',
286
+ className
287
+ } = props,
288
+ other = (0, _objectWithoutProperties2.default)(props, _excluded);
289
+ const {
290
+ classes
291
+ } = useStyle();
292
+ const [image, setImage] = _react.default.useState();
293
+ const [focus, setFocus] = _react.default.useState();
294
+ const [mouseDown, setMouseDown] = _react.default.useState();
295
+ const [selector, setSelector] = _react.default.useState(selectorDefault !== undefined ? selectorDefault : selector_);
296
+ const [selectorRelative, setSelectorRelative] = _react.default.useState();
297
+ const refs = {
298
+ root: _react.default.useRef(null),
299
+ image: _react.default.useRef(null),
300
+ mouseDown: _react.default.useRef(null),
301
+ selector: _react.default.useRef(null),
302
+ imageSelectorMain: _react.default.useRef(null),
303
+ imageSelector: _react.default.useRef(null),
304
+ previousMouseEvent: _react.default.useRef(null),
305
+ canvasMain: _react.default.useRef(null),
306
+ canvasImageSelector: _react.default.useRef(null),
307
+ move: _react.default.useRef(null),
308
+ borderTop: _react.default.useRef(null),
309
+ borderLeft: _react.default.useRef(null),
310
+ borderRight: _react.default.useRef(null),
311
+ borderBottom: _react.default.useRef(null),
312
+ dotTopLeft: _react.default.useRef(null),
313
+ dotTopRight: _react.default.useRef(null),
314
+ dotBottomLeft: _react.default.useRef(null),
315
+ dotBottomRight: _react.default.useRef(null),
316
+ props: _react.default.useRef(null),
317
+ dynamicParent: _react.default.useRef(null),
318
+ focus: _react.default.useRef(null),
319
+ aspectRatio: _react.default.useRef(null)
320
+ };
321
+ refs.image.current = image;
322
+ refs.mouseDown.current = mouseDown;
323
+ refs.selector.current = selector;
324
+ refs.props.current = props;
325
+ refs.focus.current = focus;
326
+ refs.dynamicParent.current = dynamicParent;
327
+ refs.aspectRatio.current = aspectRatio;
328
+ const onSelectorChange = valueNew => {
329
+ var _refs$canvasMain$curr, _refs$canvasMain$curr2, _refs$canvasMain$curr3, _refs$canvasMain$curr4;
330
+ // min, max for width, height
331
+ // + resolve it to max width and height for aspect ratio
332
+ if ((0, _utils.is)('object', valueNew) && !!Object.keys(valueNew).length && refs.root.current) {
333
+ var _refs$root$current, _refs$mouseDown$curre;
334
+ const rootRect = (_refs$root$current = refs.root.current) === null || _refs$root$current === void 0 ? void 0 : _refs$root$current.getBoundingClientRect();
335
+ const minWidth_ = refs.props.current.minWidth !== undefined ? refs.props.current.minWidth : Number.MIN_SAFE_INTEGER;
336
+ const minHeight_ = refs.props.current.minHeight !== undefined ? refs.props.current.minHeight : Number.MIN_SAFE_INTEGER;
337
+ const maxWidth_ = refs.props.current.maxWidth !== undefined ? refs.props.current.maxWidth : Number.MAX_SAFE_INTEGER;
338
+ const maxHeight_ = refs.props.current.maxHeight !== undefined ? refs.props.current.maxHeight : Number.MAX_SAFE_INTEGER;
339
+ valueNew.width = (0, _utils.clamp)(valueNew.width, minWidth_, maxWidth_);
340
+ valueNew.height = (0, _utils.clamp)(valueNew.height, minHeight_, maxHeight_);
341
+ if (refs.aspectRatio.current !== undefined) {
342
+ if (valueNew.width + valueNew.width / refs.aspectRatio.current >= valueNew.height + valueNew.height * refs.aspectRatio.current) {
343
+ valueNew.height = valueNew.width / refs.aspectRatio.current;
344
+ } else {
345
+ valueNew.width = valueNew.height * refs.aspectRatio.current;
346
+ }
347
+
348
+ // Max width
349
+ if (valueNew.left + valueNew.width > rootRect.width) {
350
+ valueNew.width = rootRect.width - valueNew.left;
351
+ valueNew.height = valueNew.width / refs.aspectRatio.current;
352
+ }
353
+
354
+ // Max height
355
+ if (valueNew.top + valueNew.height > rootRect.height) {
356
+ valueNew.height = rootRect.height - valueNew.top;
357
+ valueNew.width = valueNew.height * refs.aspectRatio.current;
358
+ }
359
+
360
+ // Max width, height per maxWidth and maxHeight
361
+ if (valueNew.width < minWidth_ || valueNew.width > maxWidth_) {
362
+ valueNew.width = (0, _utils.clamp)(valueNew.width, minWidth_, maxWidth_);
363
+ valueNew.height = valueNew.width / refs.aspectRatio.current;
10
364
  }
11
- return t;
12
- };
13
- var __importDefault = (this && this.__importDefault) || function (mod) {
14
- return (mod && mod.__esModule) ? mod : { "default": mod };
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- const jsx_runtime_1 = require("react/jsx-runtime");
18
- const react_1 = __importDefault(require("react"));
19
- const utils_1 = require("@onesy/utils");
20
- const style_react_1 = require("@onesy/style-react");
21
- const Tooltip_1 = __importDefault(require("../Tooltip"));
22
- const utils_2 = require("../utils");
23
- const useStyle = (0, style_react_1.style)(theme => {
24
- const dot = {
25
- display: 'inline-block',
26
- position: 'absolute',
27
- width: '6px',
28
- height: '6px',
29
- borderRadius: '50%',
30
- background: '#a4a4a4',
31
- outline: '1px solid white'
365
+ if (valueNew.height < minHeight_ || valueNew.height > maxHeight_) {
366
+ valueNew.height = (0, _utils.clamp)(valueNew.height, minHeight_, maxHeight_);
367
+ valueNew.width = valueNew.height / refs.aspectRatio.current;
368
+ }
369
+ }
370
+
371
+ // If previous values are within maxWidth and/or maxHeight
372
+ // and left or top are not same as before
373
+ // return
374
+ if (!['move'].includes((_refs$mouseDown$curre = refs.mouseDown.current) === null || _refs$mouseDown$curre === void 0 ? void 0 : _refs$mouseDown$curre.version)) {
375
+ if (maxWidth_ !== undefined || maxHeight_ !== undefined) {
376
+ var _refs$selector$curren, _refs$selector$curren2, _refs$selector$curren3, _refs$selector$curren4;
377
+ if ((valueNew.top < ((_refs$selector$curren = refs.selector.current) === null || _refs$selector$curren === void 0 ? void 0 : _refs$selector$curren.top) || valueNew.left < ((_refs$selector$curren2 = refs.selector.current) === null || _refs$selector$curren2 === void 0 ? void 0 : _refs$selector$curren2.left)) && (((_refs$selector$curren3 = refs.selector.current) === null || _refs$selector$curren3 === void 0 ? void 0 : _refs$selector$curren3.width) === maxWidth_ || ((_refs$selector$curren4 = refs.selector.current) === null || _refs$selector$curren4 === void 0 ? void 0 : _refs$selector$curren4.height) === maxHeight_)) return;
378
+ }
379
+ }
380
+ }
381
+ const rootRect_ = refs.root.current.getBoundingClientRect();
382
+ const selectorRelative_ = {
383
+ top: valueNew.top / (rootRect_ === null || rootRect_ === void 0 ? void 0 : rootRect_.height) * ((_refs$canvasMain$curr = refs.canvasMain.current) === null || _refs$canvasMain$curr === void 0 ? void 0 : _refs$canvasMain$curr.height),
384
+ left: valueNew.left / (rootRect_ === null || rootRect_ === void 0 ? void 0 : rootRect_.width) * ((_refs$canvasMain$curr2 = refs.canvasMain.current) === null || _refs$canvasMain$curr2 === void 0 ? void 0 : _refs$canvasMain$curr2.width),
385
+ width: valueNew.width / (rootRect_ === null || rootRect_ === void 0 ? void 0 : rootRect_.width) * ((_refs$canvasMain$curr3 = refs.canvasMain.current) === null || _refs$canvasMain$curr3 === void 0 ? void 0 : _refs$canvasMain$curr3.width),
386
+ height: valueNew.height / (rootRect_ === null || rootRect_ === void 0 ? void 0 : rootRect_.height) * ((_refs$canvasMain$curr4 = refs.canvasMain.current) === null || _refs$canvasMain$curr4 === void 0 ? void 0 : _refs$canvasMain$curr4.height)
387
+ };
388
+
389
+ // Update inner or controlled
390
+ if (!props.hasOwnProperty('selector')) {
391
+ setSelector(valueNew);
392
+ setSelectorRelative(selectorRelative_);
393
+ }
394
+ if ((0, _utils.is)('function', onSelectorChange_)) onSelectorChange_(selectorRelative_);
395
+ };
396
+ _react.default.useEffect(() => {
397
+ var _refs$root$current2;
398
+ const method = event => {
399
+ switch (event.key) {
400
+ case 'Escape':
401
+ if (refs.focus.current) setSelector({});
402
+ break;
403
+ default:
404
+ break;
405
+ }
32
406
  };
33
- return {
34
- root: {
35
- width: '100%',
36
- minHeight: '1px',
37
- lineHeight: '0',
38
- userSelect: 'none',
39
- overflow: 'hidden'
40
- },
41
- mouseDown_move: {
42
- '& *': {
43
- cursor: 'grab !important'
407
+ const onMouseUp = event => {
408
+ setMouseDown(false);
409
+ refs.previousMouseEvent.current = undefined;
410
+ };
411
+
412
+ // Move
413
+ const onMove = (x, y) => {
414
+ if (refs.mouseDown.current && refs.previousMouseEvent.current) {
415
+ var _refs$mouseDown$curre2, _refs$mouseDown$curre3, _refs$mouseDown$curre4, _refs$mouseDown$curre5, _refs$mouseDown$curre6, _refs$mouseDown$curre7, _refs$mouseDown$curre8, _refs$mouseDown$curre9, _refs$mouseDown$curre10, _refs$mouseDown$curre11;
416
+ const {
417
+ top: previousTop,
418
+ left: previousLeft
419
+ } = refs.mouseDown.current;
420
+ const rootRect = refs.root.current.getBoundingClientRect();
421
+ const selectorRect_ = refs.imageSelectorMain.current.getBoundingClientRect();
422
+ const selectorRect = {};
423
+
424
+ // Normalize relative to root
425
+ selectorRect.width = selectorRect_.width;
426
+ selectorRect.height = selectorRect_.height;
427
+ selectorRect.top = selectorRect_.top - rootRect.top;
428
+ selectorRect.left = selectorRect_.left - rootRect.left;
429
+ selectorRect.right = selectorRect.left + selectorRect_.width;
430
+ selectorRect.bottom = selectorRect.top + selectorRect_.height;
431
+ if (((_refs$mouseDown$curre2 = refs.mouseDown.current) === null || _refs$mouseDown$curre2 === void 0 ? void 0 : _refs$mouseDown$curre2.version) === 'make') {
432
+ const top_ = (0, _utils.clamp)(y - rootRect.top, 0, rootRect.height);
433
+ const left_ = (0, _utils.clamp)(x - rootRect.left, 0, rootRect.width);
434
+ let top = (0, _utils.clamp)(top_, 0, previousTop);
435
+ let left = (0, _utils.clamp)(left_, 0, previousLeft);
436
+ let width = Math.abs(left_ - previousLeft);
437
+ let height = Math.abs(top_ - previousTop);
438
+ if (refs.aspectRatio.current !== undefined) {
439
+ if (left < previousLeft) {
440
+ var _refs$selector$curren5, _refs$selector$curren6, _refs$selector$curren7, _refs$selector$curren8;
441
+ if (((_refs$selector$curren5 = refs.selector.current) === null || _refs$selector$curren5 === void 0 ? void 0 : _refs$selector$curren5.left) < previousLeft && left < ((_refs$selector$curren6 = refs.selector.current) === null || _refs$selector$curren6 === void 0 ? void 0 : _refs$selector$curren6.left) && (top <= 0 || ((_refs$selector$curren7 = refs.selector.current) === null || _refs$selector$curren7 === void 0 ? void 0 : _refs$selector$curren7.top) + ((_refs$selector$curren8 = refs.selector.current) === null || _refs$selector$curren8 === void 0 ? void 0 : _refs$selector$curren8.height) >= rootRect.height)) return;
44
442
  }
45
- },
46
- mouseDown_top: {
47
- '& *': {
48
- cursor: 'ns-resize !important'
443
+ if (top < previousTop) {
444
+ var _refs$selector$curren9, _refs$selector$curren10, _refs$selector$curren11, _refs$selector$curren12;
445
+ if (((_refs$selector$curren9 = refs.selector.current) === null || _refs$selector$curren9 === void 0 ? void 0 : _refs$selector$curren9.top) < previousTop && top < ((_refs$selector$curren10 = refs.selector.current) === null || _refs$selector$curren10 === void 0 ? void 0 : _refs$selector$curren10.top) && (left <= 0 || ((_refs$selector$curren11 = refs.selector.current) === null || _refs$selector$curren11 === void 0 ? void 0 : _refs$selector$curren11.left) + ((_refs$selector$curren12 = refs.selector.current) === null || _refs$selector$curren12 === void 0 ? void 0 : _refs$selector$curren12.width) >= rootRect.width)) return;
49
446
  }
50
- },
51
- mouseDown_left: {
52
- '& *': {
53
- cursor: 'ew-resize !important'
447
+
448
+ // Max surface
449
+ if (width + width / refs.aspectRatio.current >= height + height * refs.aspectRatio.current) {
450
+ height = width / refs.aspectRatio.current;
451
+ } else {
452
+ width = height * refs.aspectRatio.current;
54
453
  }
55
- },
56
- mouseDown_right: {
57
- '& *': {
58
- cursor: 'ew-resize !important'
454
+
455
+ // Moved left
456
+ if (left < previousLeft) {
457
+ left = (0, _utils.clamp)(previousLeft - width, 0, previousLeft);
458
+
459
+ // Update width, height upto the previousLeft
460
+ width = (0, _utils.clamp)(width, 0, previousLeft);
461
+ height = width / refs.aspectRatio.current;
59
462
  }
60
- },
61
- mouseDown_bottom: {
62
- '& *': {
63
- cursor: 'ns-resize !important'
463
+
464
+ // Moved top
465
+ if (top < previousTop) {
466
+ top = (0, _utils.clamp)(previousTop - height, 0, previousTop);
467
+
468
+ // Update width, height upto the previousTop
469
+ height = (0, _utils.clamp)(height, 0, previousTop);
470
+ width = height * refs.aspectRatio.current;
64
471
  }
65
- },
66
- mouseDown_top_left: {
67
- '& *': {
68
- cursor: 'nwse-resize !important'
472
+
473
+ // Max width
474
+ if (left + width > rootRect.width) {
475
+ width = rootRect.width - left;
476
+ height = width / refs.aspectRatio.current;
69
477
  }
70
- },
71
- mouseDown_top_right: {
72
- '& *': {
73
- cursor: 'nesw-resize !important'
478
+
479
+ // Max height
480
+ if (top + height > rootRect.height) {
481
+ height = rootRect.height - top;
482
+ width = height * refs.aspectRatio.current;
74
483
  }
75
- },
76
- mouseDown_bottom_left: {
77
- '& *': {
78
- cursor: 'nesw-resize !important'
484
+
485
+ // Min left
486
+ if (left < previousLeft) left = previousLeft - width;
487
+
488
+ // Min top
489
+ if (top < previousTop) top = previousTop - height;
490
+ }
491
+ onSelectorChange(_objectSpread(_objectSpread({}, refs.selector.current), {}, {
492
+ top,
493
+ left,
494
+ width,
495
+ height
496
+ }));
497
+ } else if (((_refs$mouseDown$curre3 = refs.mouseDown.current) === null || _refs$mouseDown$curre3 === void 0 ? void 0 : _refs$mouseDown$curre3.version) === 'move') {
498
+ const top = y - refs.previousMouseEvent.current.clientY;
499
+ const left = x - refs.previousMouseEvent.current.clientX;
500
+ onSelectorChange(_objectSpread(_objectSpread({}, refs.selector.current), {}, {
501
+ top: (0, _utils.clamp)(refs.selector.current.top + top, 0, rootRect.height - selectorRect.height),
502
+ left: (0, _utils.clamp)(refs.selector.current.left + left, 0, rootRect.width - selectorRect.width)
503
+ }));
504
+ } else if (((_refs$mouseDown$curre4 = refs.mouseDown.current) === null || _refs$mouseDown$curre4 === void 0 ? void 0 : _refs$mouseDown$curre4.version) === 'top_left') {
505
+ const incY = y - refs.previousMouseEvent.current.clientY;
506
+ const incX = x - refs.previousMouseEvent.current.clientX;
507
+ let top = (0, _utils.clamp)(selectorRect.top + incY, 0);
508
+ let left = (0, _utils.clamp)(selectorRect.left + incX, 0);
509
+ if (selectorRect.bottom - top < 0 && selectorRect.right - left < 0) refs.mouseDown.current.version = 'bottom_right';else if (selectorRect.bottom - top < 0) refs.mouseDown.current.version = 'bottom_left';else if (selectorRect.right - left < 0) refs.mouseDown.current.version = 'top_right';
510
+ let width = (0, _utils.clamp)(selectorRect.right - left, 0);
511
+ let height = (0, _utils.clamp)(selectorRect.bottom - top, 0);
512
+ if (refs.aspectRatio.current !== undefined) {
513
+ var _refs$selector$curren13;
514
+ // Left
515
+ if (left < refs.selector.current.left && refs.selector.current.left < previousLeft && ((_refs$selector$curren13 = refs.selector.current) === null || _refs$selector$curren13 === void 0 ? void 0 : _refs$selector$curren13.top) === 0) return;
516
+
517
+ // Max surface
518
+ if (width + width / refs.aspectRatio.current >= height + height * refs.aspectRatio.current) {
519
+ height = width / refs.aspectRatio.current;
520
+ } else {
521
+ width = height * refs.aspectRatio.current;
79
522
  }
80
- },
81
- mouseDown_bottom_right: {
82
- '& *': {
83
- cursor: 'nwse-resize !important'
523
+
524
+ // Moved left
525
+ if (left < previousLeft) {
526
+ left = (0, _utils.clamp)(previousLeft - width, 0, previousLeft);
527
+
528
+ // Update width, height upto the previousLeft
529
+ width = (0, _utils.clamp)(width, 0, previousLeft);
530
+ height = width / refs.aspectRatio.current;
84
531
  }
85
- },
86
- canvas: {
87
- position: 'absolute',
88
- zIndex: '0'
89
- },
90
- canvas_main: {
91
- position: 'relative',
92
- width: '100%',
93
- height: 'auto'
94
- },
95
- canvas_imageSelector: {
96
- zIndex: -1
97
- },
98
- background: {
99
- position: 'absolute',
100
- inset: '0',
101
- width: '100%',
102
- height: '100%',
103
- opacity: '0',
104
- background: theme.methods.palette.color.colorToRgb(theme.palette.text.default.primary, 44),
105
- transition: theme.methods.transitions.make('opacity', { duration: 'xxs' }),
106
- zIndex: '1'
107
- },
108
- background_in: {
109
- opacity: '1'
110
- },
111
- imageSelector_main: {
112
- position: 'absolute',
113
- top: '0',
114
- left: '0',
115
- width: '0',
116
- height: '0',
117
- outline: '2px dashed white',
118
- background: 'transparent',
119
- touchAction: 'none',
120
- opacity: '0',
121
- zIndex: '14',
122
- userSelect: 'none',
123
- transition: theme.methods.transitions.make('opacity'),
124
- },
125
- imageSelector_main_in: {
126
- opacity: '1'
127
- },
128
- imageSelector: {
129
- position: 'absolute',
130
- top: '0',
131
- left: '0',
132
- width: '0',
133
- height: '0',
134
- touchAction: 'none',
135
- overflow: 'hidden',
136
- userSelect: 'none',
137
- zIndex: '11'
138
- },
139
- move: {
140
- position: 'absolute',
141
- inset: '0',
142
- width: '100%',
143
- height: '100%',
144
- cursor: 'grab',
145
- '&:active': {
146
- cursor: 'grabbing !important'
532
+
533
+ // Moved top
534
+ if (top < previousTop) {
535
+ top = (0, _utils.clamp)(previousTop - height, 0, previousTop);
536
+
537
+ // Update width, height upto the previousTop
538
+ height = (0, _utils.clamp)(height, 0, previousTop);
539
+ width = height * refs.aspectRatio.current;
147
540
  }
148
- },
149
- grid_line: {
150
- position: 'absolute',
151
- background: 'white',
152
- mixBlendMode: 'difference'
153
- },
154
- grid_line_top_start: {
155
- top: '33.3333%',
156
- insetInline: '0',
157
- width: '100%',
158
- height: '1px'
159
- },
160
- grid_line_top_end: {
161
- top: '66.6666%',
162
- insetInline: '0',
163
- width: '100%',
164
- height: '1px'
165
- },
166
- grid_line_left_start: {
167
- left: '33.3333%',
168
- insetBlock: '0',
169
- width: '1px',
170
- height: '100%'
171
- },
172
- grid_line_left_end: {
173
- left: '66.6666%',
174
- insetBlock: '0',
175
- width: '1px',
176
- height: '100%'
177
- },
178
- dot: Object.assign({}, dot),
179
- dot_top_left: {
180
- top: '-1px',
181
- left: '-1px',
182
- transform: 'translate(-50%, -50%)',
183
- cursor: 'nwse-resize'
184
- },
185
- dot_top_right: {
186
- top: '-1px',
187
- right: '-1px',
188
- transform: 'translate(50%, -50%)',
189
- cursor: 'nesw-resize'
190
- },
191
- dot_bottom_left: {
192
- bottom: '-1px',
193
- left: '-1px',
194
- transform: 'translate(-50%, 50%)',
195
- cursor: 'nesw-resize'
196
- },
197
- dot_bottom_right: {
198
- bottom: '-1px',
199
- right: '-1px',
200
- transform: 'translate(50%, 50%)',
201
- cursor: 'nwse-resize'
202
- },
203
- border: {
204
- position: 'absolute'
205
- },
206
- border_top: {
207
- top: '-2px',
208
- height: '2px',
209
- width: '100%',
210
- cursor: 'ns-resize',
211
- '&::before': Object.assign(Object.assign({}, dot), { content: '""', top: '50%', left: '50%', transform: 'translate(-50%, -50%)' })
212
- },
213
- border_left: {
214
- left: '-2px',
215
- height: '100%',
216
- width: '2px',
217
- cursor: 'ew-resize',
218
- '&::before': Object.assign(Object.assign({}, dot), { content: '""', top: '50%', left: '50%', transform: 'translate(-50%, -50%)' })
219
- },
220
- border_right: {
221
- right: '-2px',
222
- height: '100%',
223
- width: '2px',
224
- cursor: 'ew-resize',
225
- '&::before': Object.assign(Object.assign({}, dot), { content: '""', top: '50%', right: '50%', transform: 'translate(50%, -50%)' })
226
- },
227
- border_bottom: {
228
- bottom: '-2px',
229
- height: '2px',
230
- width: '100%',
231
- cursor: 'ns-resize',
232
- '&::before': Object.assign(Object.assign({}, dot), { content: '""', bottom: '50%', left: '50%', transform: 'translate(-50%, 50%)' })
233
- }
234
- };
235
- }, { name: 'onesy-ImageCrop' });
236
- const ImageCrop = react_1.default.forwardRef((props_, ref) => {
237
- var _a;
238
- const theme = (0, style_react_1.useOnesyTheme)();
239
- const props = react_1.default.useMemo(() => { var _a, _b, _c, _d, _e, _f, _g, _h; return (Object.assign(Object.assign(Object.assign({}, (_d = (_c = (_b = (_a = theme === null || theme === void 0 ? void 0 : theme.ui) === null || _a === void 0 ? void 0 : _a.elements) === null || _b === void 0 ? void 0 : _b.all) === null || _c === void 0 ? void 0 : _c.props) === null || _d === void 0 ? void 0 : _d.default), (_h = (_g = (_f = (_e = theme === null || theme === void 0 ? void 0 : theme.ui) === null || _e === void 0 ? void 0 : _e.elements) === null || _f === void 0 ? void 0 : _f.onesyImageCrop) === null || _g === void 0 ? void 0 : _g.props) === null || _h === void 0 ? void 0 : _h.default), props_)); }, [props_]);
240
- const Tooltip = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.Tooltip) || Tooltip_1.default; }, [theme]);
241
- const {
242
- // url, datauri or canvas
243
- image: image_, minWidth, minHeight, maxWidth, maxHeight, selectorDefault, selector: selector_, onSelectorChange: onSelectorChange_, type = `image/jpeg`, quality = 1, aspectRatio, gridLines, dynamicParent, onFocus: onFocus_, onBlur: onBlur_, TooltipProps, Component = 'div', className } = props, other = __rest(props, ["image", "minWidth", "minHeight", "maxWidth", "maxHeight", "selectorDefault", "selector", "onSelectorChange", "type", "quality", "aspectRatio", "gridLines", "dynamicParent", "onFocus", "onBlur", "TooltipProps", "Component", "className"]);
244
- const { classes } = useStyle();
245
- const [image, setImage] = react_1.default.useState();
246
- const [focus, setFocus] = react_1.default.useState();
247
- const [mouseDown, setMouseDown] = react_1.default.useState();
248
- const [selector, setSelector] = react_1.default.useState(selectorDefault !== undefined ? selectorDefault : selector_);
249
- const [selectorRelative, setSelectorRelative] = react_1.default.useState();
250
- const refs = {
251
- root: react_1.default.useRef(null),
252
- image: react_1.default.useRef(null),
253
- mouseDown: react_1.default.useRef(null),
254
- selector: react_1.default.useRef(null),
255
- imageSelectorMain: react_1.default.useRef(null),
256
- imageSelector: react_1.default.useRef(null),
257
- previousMouseEvent: react_1.default.useRef(null),
258
- canvasMain: react_1.default.useRef(null),
259
- canvasImageSelector: react_1.default.useRef(null),
260
- move: react_1.default.useRef(null),
261
- borderTop: react_1.default.useRef(null),
262
- borderLeft: react_1.default.useRef(null),
263
- borderRight: react_1.default.useRef(null),
264
- borderBottom: react_1.default.useRef(null),
265
- dotTopLeft: react_1.default.useRef(null),
266
- dotTopRight: react_1.default.useRef(null),
267
- dotBottomLeft: react_1.default.useRef(null),
268
- dotBottomRight: react_1.default.useRef(null),
269
- props: react_1.default.useRef(null),
270
- dynamicParent: react_1.default.useRef(null),
271
- focus: react_1.default.useRef(null),
272
- aspectRatio: react_1.default.useRef(null)
273
- };
274
- refs.image.current = image;
275
- refs.mouseDown.current = mouseDown;
276
- refs.selector.current = selector;
277
- refs.props.current = props;
278
- refs.focus.current = focus;
279
- refs.dynamicParent.current = dynamicParent;
280
- refs.aspectRatio.current = aspectRatio;
281
- const onSelectorChange = (valueNew) => {
282
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
283
- // min, max for width, height
284
- // + resolve it to max width and height for aspect ratio
285
- if ((0, utils_1.is)('object', valueNew) && !!Object.keys(valueNew).length && refs.root.current) {
286
- const rootRect = (_a = refs.root.current) === null || _a === void 0 ? void 0 : _a.getBoundingClientRect();
287
- const minWidth_ = refs.props.current.minWidth !== undefined ? refs.props.current.minWidth : Number.MIN_SAFE_INTEGER;
288
- const minHeight_ = refs.props.current.minHeight !== undefined ? refs.props.current.minHeight : Number.MIN_SAFE_INTEGER;
289
- const maxWidth_ = refs.props.current.maxWidth !== undefined ? refs.props.current.maxWidth : Number.MAX_SAFE_INTEGER;
290
- const maxHeight_ = refs.props.current.maxHeight !== undefined ? refs.props.current.maxHeight : Number.MAX_SAFE_INTEGER;
291
- valueNew.width = (0, utils_1.clamp)(valueNew.width, minWidth_, maxWidth_);
292
- valueNew.height = (0, utils_1.clamp)(valueNew.height, minHeight_, maxHeight_);
293
- if (refs.aspectRatio.current !== undefined) {
294
- if (valueNew.width + (valueNew.width / refs.aspectRatio.current) >= valueNew.height + (valueNew.height * refs.aspectRatio.current)) {
295
- valueNew.height = valueNew.width / refs.aspectRatio.current;
296
- }
297
- else {
298
- valueNew.width = valueNew.height * refs.aspectRatio.current;
299
- }
300
- // Max width
301
- if (valueNew.left + valueNew.width > rootRect.width) {
302
- valueNew.width = rootRect.width - valueNew.left;
303
- valueNew.height = valueNew.width / refs.aspectRatio.current;
304
- }
305
- // Max height
306
- if (valueNew.top + valueNew.height > rootRect.height) {
307
- valueNew.height = rootRect.height - valueNew.top;
308
- valueNew.width = valueNew.height * refs.aspectRatio.current;
309
- }
310
- // Max width, height per maxWidth and maxHeight
311
- if (valueNew.width < minWidth_ || valueNew.width > maxWidth_) {
312
- valueNew.width = (0, utils_1.clamp)(valueNew.width, minWidth_, maxWidth_);
313
- valueNew.height = valueNew.width / refs.aspectRatio.current;
314
- }
315
- if (valueNew.height < minHeight_ || valueNew.height > maxHeight_) {
316
- valueNew.height = (0, utils_1.clamp)(valueNew.height, minHeight_, maxHeight_);
317
- valueNew.width = valueNew.height / refs.aspectRatio.current;
318
- }
541
+
542
+ // Max width
543
+ if (left + width > rootRect.width) {
544
+ width = rootRect.width - left;
545
+ height = width / refs.aspectRatio.current;
319
546
  }
320
- // If previous values are within maxWidth and/or maxHeight
321
- // and left or top are not same as before
322
- // return
323
- if (!['move'].includes((_b = refs.mouseDown.current) === null || _b === void 0 ? void 0 : _b.version)) {
324
- if (maxWidth_ !== undefined || maxHeight_ !== undefined) {
325
- if ((valueNew.top < ((_c = refs.selector.current) === null || _c === void 0 ? void 0 : _c.top) || valueNew.left < ((_d = refs.selector.current) === null || _d === void 0 ? void 0 : _d.left)) && (((_e = refs.selector.current) === null || _e === void 0 ? void 0 : _e.width) === maxWidth_ || ((_f = refs.selector.current) === null || _f === void 0 ? void 0 : _f.height) === maxHeight_))
326
- return;
327
- }
547
+
548
+ // Max height
549
+ if (top + height > rootRect.height) {
550
+ height = rootRect.height - top;
551
+ width = height * refs.aspectRatio.current;
328
552
  }
329
- }
330
- const rootRect_ = refs.root.current.getBoundingClientRect();
331
- const selectorRelative_ = {
332
- top: (valueNew.top / (rootRect_ === null || rootRect_ === void 0 ? void 0 : rootRect_.height)) * ((_g = refs.canvasMain.current) === null || _g === void 0 ? void 0 : _g.height),
333
- left: (valueNew.left / (rootRect_ === null || rootRect_ === void 0 ? void 0 : rootRect_.width)) * ((_h = refs.canvasMain.current) === null || _h === void 0 ? void 0 : _h.width),
334
- width: (valueNew.width / (rootRect_ === null || rootRect_ === void 0 ? void 0 : rootRect_.width)) * ((_j = refs.canvasMain.current) === null || _j === void 0 ? void 0 : _j.width),
335
- height: (valueNew.height / (rootRect_ === null || rootRect_ === void 0 ? void 0 : rootRect_.height)) * ((_k = refs.canvasMain.current) === null || _k === void 0 ? void 0 : _k.height)
336
- };
337
- // Update inner or controlled
338
- if (!props.hasOwnProperty('selector')) {
339
- setSelector(valueNew);
340
- setSelectorRelative(selectorRelative_);
341
- }
342
- if ((0, utils_1.is)('function', onSelectorChange_))
343
- onSelectorChange_(selectorRelative_);
344
- };
345
- react_1.default.useEffect(() => {
346
- var _a;
347
- const method = (event) => {
348
- switch (event.key) {
349
- case 'Escape':
350
- if (refs.focus.current)
351
- setSelector({});
352
- break;
353
- default:
354
- break;
553
+
554
+ // Min left
555
+ if (left < previousLeft) left = previousLeft - width;
556
+
557
+ // Min top
558
+ if (top < previousTop) top = previousTop - height;
559
+ }
560
+ onSelectorChange(_objectSpread(_objectSpread({}, refs.selector.current), {}, {
561
+ top,
562
+ left,
563
+ width,
564
+ height
565
+ }));
566
+ } else if (((_refs$mouseDown$curre5 = refs.mouseDown.current) === null || _refs$mouseDown$curre5 === void 0 ? void 0 : _refs$mouseDown$curre5.version) === 'top_right') {
567
+ const incY = y - refs.previousMouseEvent.current.clientY;
568
+ const incX = x - refs.previousMouseEvent.current.clientX;
569
+ const top = (0, _utils.clamp)(selectorRect.top + incY, 0);
570
+ if (selectorRect.bottom - top < 0 && selectorRect.width + incX < 0) refs.mouseDown.current.version = 'bottom_left';else if (selectorRect.bottom - top < 0) refs.mouseDown.current.version = 'bottom_right';else if (selectorRect.width + incX < 0) refs.mouseDown.current.version = 'top_left';
571
+ let width = (0, _utils.clamp)(Math.abs(selectorRect.width + incX), 0, rootRect.width - selectorRect.left);
572
+ let height = (0, _utils.clamp)(selectorRect.bottom - top, 0);
573
+
574
+ // Top
575
+ if (refs.aspectRatio.current !== undefined) {
576
+ // Top
577
+ if (top < refs.selector.current.top && refs.selector.current.top < previousTop && refs.selector.current.left + refs.selector.current.width >= rootRect.width) return;
578
+ width = height * refs.aspectRatio.current;
579
+ height = width / refs.aspectRatio.current;
580
+
581
+ // Max width
582
+ if (refs.selector.current.left + width > rootRect.width) {
583
+ width = rootRect.width - refs.selector.current.left;
584
+ height = width / refs.aspectRatio.current;
355
585
  }
356
- };
357
- const onMouseUp = (event) => {
358
- setMouseDown(false);
359
- refs.previousMouseEvent.current = undefined;
360
- };
361
- // Move
362
- const onMove = (x, y) => {
363
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y;
364
- if (refs.mouseDown.current && refs.previousMouseEvent.current) {
365
- const { top: previousTop, left: previousLeft } = refs.mouseDown.current;
366
- const rootRect = refs.root.current.getBoundingClientRect();
367
- const selectorRect_ = refs.imageSelectorMain.current.getBoundingClientRect();
368
- const selectorRect = {};
369
- // Normalize relative to root
370
- selectorRect.width = selectorRect_.width;
371
- selectorRect.height = selectorRect_.height;
372
- selectorRect.top = selectorRect_.top - rootRect.top;
373
- selectorRect.left = selectorRect_.left - rootRect.left;
374
- selectorRect.right = selectorRect.left + selectorRect_.width;
375
- selectorRect.bottom = selectorRect.top + selectorRect_.height;
376
- if (((_a = refs.mouseDown.current) === null || _a === void 0 ? void 0 : _a.version) === 'make') {
377
- const top_ = (0, utils_1.clamp)(y - rootRect.top, 0, rootRect.height);
378
- const left_ = (0, utils_1.clamp)(x - rootRect.left, 0, rootRect.width);
379
- let top = (0, utils_1.clamp)(top_, 0, previousTop);
380
- let left = (0, utils_1.clamp)(left_, 0, previousLeft);
381
- let width = Math.abs(left_ - previousLeft);
382
- let height = Math.abs(top_ - previousTop);
383
- if (refs.aspectRatio.current !== undefined) {
384
- if (left < previousLeft) {
385
- if (((_b = refs.selector.current) === null || _b === void 0 ? void 0 : _b.left) < previousLeft && left < ((_c = refs.selector.current) === null || _c === void 0 ? void 0 : _c.left) && (top <= 0 || ((_d = refs.selector.current) === null || _d === void 0 ? void 0 : _d.top) + ((_e = refs.selector.current) === null || _e === void 0 ? void 0 : _e.height) >= rootRect.height))
386
- return;
387
- }
388
- if (top < previousTop) {
389
- if (((_f = refs.selector.current) === null || _f === void 0 ? void 0 : _f.top) < previousTop && top < ((_g = refs.selector.current) === null || _g === void 0 ? void 0 : _g.top) && (left <= 0 || ((_h = refs.selector.current) === null || _h === void 0 ? void 0 : _h.left) + ((_j = refs.selector.current) === null || _j === void 0 ? void 0 : _j.width) >= rootRect.width))
390
- return;
391
- }
392
- // Max surface
393
- if (width + (width / refs.aspectRatio.current) >= height + (height * refs.aspectRatio.current)) {
394
- height = width / refs.aspectRatio.current;
395
- }
396
- else {
397
- width = height * refs.aspectRatio.current;
398
- }
399
- // Moved left
400
- if (left < previousLeft) {
401
- left = (0, utils_1.clamp)(previousLeft - width, 0, previousLeft);
402
- // Update width, height upto the previousLeft
403
- width = (0, utils_1.clamp)(width, 0, previousLeft);
404
- height = width / refs.aspectRatio.current;
405
- }
406
- // Moved top
407
- if (top < previousTop) {
408
- top = (0, utils_1.clamp)(previousTop - height, 0, previousTop);
409
- // Update width, height upto the previousTop
410
- height = (0, utils_1.clamp)(height, 0, previousTop);
411
- width = height * refs.aspectRatio.current;
412
- }
413
- // Max width
414
- if (left + width > rootRect.width) {
415
- width = rootRect.width - left;
416
- height = width / refs.aspectRatio.current;
417
- }
418
- // Max height
419
- if (top + height > rootRect.height) {
420
- height = rootRect.height - top;
421
- width = height * refs.aspectRatio.current;
422
- }
423
- // Min left
424
- if (left < previousLeft)
425
- left = previousLeft - width;
426
- // Min top
427
- if (top < previousTop)
428
- top = previousTop - height;
429
- }
430
- onSelectorChange(Object.assign(Object.assign({}, refs.selector.current), { top,
431
- left,
432
- width,
433
- height }));
434
- }
435
- else if (((_k = refs.mouseDown.current) === null || _k === void 0 ? void 0 : _k.version) === 'move') {
436
- const top = y - refs.previousMouseEvent.current.clientY;
437
- const left = x - refs.previousMouseEvent.current.clientX;
438
- onSelectorChange(Object.assign(Object.assign({}, refs.selector.current), { top: (0, utils_1.clamp)(refs.selector.current.top + top, 0, rootRect.height - selectorRect.height), left: (0, utils_1.clamp)(refs.selector.current.left + left, 0, rootRect.width - selectorRect.width) }));
439
- }
440
- else if (((_l = refs.mouseDown.current) === null || _l === void 0 ? void 0 : _l.version) === 'top_left') {
441
- const incY = y - refs.previousMouseEvent.current.clientY;
442
- const incX = x - refs.previousMouseEvent.current.clientX;
443
- let top = (0, utils_1.clamp)(selectorRect.top + incY, 0);
444
- let left = (0, utils_1.clamp)(selectorRect.left + incX, 0);
445
- if ((selectorRect.bottom - top < 0) &&
446
- (selectorRect.right - left < 0))
447
- refs.mouseDown.current.version = 'bottom_right';
448
- else if (selectorRect.bottom - top < 0)
449
- refs.mouseDown.current.version = 'bottom_left';
450
- else if (selectorRect.right - left < 0)
451
- refs.mouseDown.current.version = 'top_right';
452
- let width = (0, utils_1.clamp)(selectorRect.right - left, 0);
453
- let height = (0, utils_1.clamp)(selectorRect.bottom - top, 0);
454
- if (refs.aspectRatio.current !== undefined) {
455
- // Left
456
- if (left < refs.selector.current.left && refs.selector.current.left < previousLeft && ((_m = refs.selector.current) === null || _m === void 0 ? void 0 : _m.top) === 0)
457
- return;
458
- // Max surface
459
- if (width + (width / refs.aspectRatio.current) >= height + (height * refs.aspectRatio.current)) {
460
- height = width / refs.aspectRatio.current;
461
- }
462
- else {
463
- width = height * refs.aspectRatio.current;
464
- }
465
- // Moved left
466
- if (left < previousLeft) {
467
- left = (0, utils_1.clamp)(previousLeft - width, 0, previousLeft);
468
- // Update width, height upto the previousLeft
469
- width = (0, utils_1.clamp)(width, 0, previousLeft);
470
- height = width / refs.aspectRatio.current;
471
- }
472
- // Moved top
473
- if (top < previousTop) {
474
- top = (0, utils_1.clamp)(previousTop - height, 0, previousTop);
475
- // Update width, height upto the previousTop
476
- height = (0, utils_1.clamp)(height, 0, previousTop);
477
- width = height * refs.aspectRatio.current;
478
- }
479
- // Max width
480
- if (left + width > rootRect.width) {
481
- width = rootRect.width - left;
482
- height = width / refs.aspectRatio.current;
483
- }
484
- // Max height
485
- if (top + height > rootRect.height) {
486
- height = rootRect.height - top;
487
- width = height * refs.aspectRatio.current;
488
- }
489
- // Min left
490
- if (left < previousLeft)
491
- left = previousLeft - width;
492
- // Min top
493
- if (top < previousTop)
494
- top = previousTop - height;
495
- }
496
- onSelectorChange(Object.assign(Object.assign({}, refs.selector.current), { top,
497
- left,
498
- width,
499
- height }));
500
- }
501
- else if (((_o = refs.mouseDown.current) === null || _o === void 0 ? void 0 : _o.version) === 'top_right') {
502
- const incY = y - refs.previousMouseEvent.current.clientY;
503
- const incX = x - refs.previousMouseEvent.current.clientX;
504
- const top = (0, utils_1.clamp)(selectorRect.top + incY, 0);
505
- if ((selectorRect.bottom - top < 0) &&
506
- (selectorRect.width + incX < 0))
507
- refs.mouseDown.current.version = 'bottom_left';
508
- else if (selectorRect.bottom - top < 0)
509
- refs.mouseDown.current.version = 'bottom_right';
510
- else if (selectorRect.width + incX < 0)
511
- refs.mouseDown.current.version = 'top_left';
512
- let width = (0, utils_1.clamp)(Math.abs(selectorRect.width + incX), 0, rootRect.width - selectorRect.left);
513
- let height = (0, utils_1.clamp)(selectorRect.bottom - top, 0);
514
- // Top
515
- if (refs.aspectRatio.current !== undefined) {
516
- // Top
517
- if (top < refs.selector.current.top && refs.selector.current.top < previousTop && refs.selector.current.left + refs.selector.current.width >= rootRect.width)
518
- return;
519
- width = height * refs.aspectRatio.current;
520
- height = width / refs.aspectRatio.current;
521
- // Max width
522
- if (refs.selector.current.left + width > rootRect.width) {
523
- width = rootRect.width - refs.selector.current.left;
524
- height = width / refs.aspectRatio.current;
525
- }
526
- // Max height
527
- if (refs.selector.current.top + height > rootRect.height) {
528
- height = rootRect.height - refs.selector.current.top;
529
- width = height * refs.aspectRatio.current;
530
- }
531
- }
532
- onSelectorChange(Object.assign(Object.assign({}, refs.selector.current), { top,
533
- width,
534
- height }));
535
- }
536
- else if (((_p = refs.mouseDown.current) === null || _p === void 0 ? void 0 : _p.version) === 'bottom_right') {
537
- const incY = y - refs.previousMouseEvent.current.clientY;
538
- const incX = x - refs.previousMouseEvent.current.clientX;
539
- if ((selectorRect.height + incY < 0) && (selectorRect.width + incX < 0))
540
- refs.mouseDown.current.version = 'top_left';
541
- else if (selectorRect.height + incY < 0)
542
- refs.mouseDown.current.version = 'top_right';
543
- else if (selectorRect.width + incX < 0)
544
- refs.mouseDown.current.version = 'bottom_left';
545
- let width = (0, utils_1.clamp)(Math.abs(selectorRect.width + incX), 0, rootRect.width - selectorRect.left);
546
- let height = (0, utils_1.clamp)(Math.abs(selectorRect.height + incY), 0, rootRect.height - selectorRect.top);
547
- // Right
548
- if (refs.aspectRatio.current !== undefined) {
549
- height = width / refs.aspectRatio.current;
550
- // Max width
551
- if (refs.selector.current.left + width > rootRect.width) {
552
- width = rootRect.width - refs.selector.current.left;
553
- height = width / refs.aspectRatio.current;
554
- }
555
- // Max height
556
- if (refs.selector.current.top + height > rootRect.height) {
557
- height = rootRect.height - refs.selector.current.top;
558
- width = height * refs.aspectRatio.current;
559
- }
560
- width = height * refs.aspectRatio.current;
561
- // Max width
562
- if (refs.selector.current.left + width > rootRect.width) {
563
- width = rootRect.width - refs.selector.current.left;
564
- height = width / refs.aspectRatio.current;
565
- }
566
- // Max height
567
- if (refs.selector.current.top + height > rootRect.height) {
568
- height = rootRect.height - refs.selector.current.top;
569
- width = height * refs.aspectRatio.current;
570
- }
571
- }
572
- onSelectorChange(Object.assign(Object.assign({}, refs.selector.current), { width,
573
- height }));
574
- }
575
- else if (((_q = refs.mouseDown.current) === null || _q === void 0 ? void 0 : _q.version) === 'bottom_left') {
576
- const incY = y - refs.previousMouseEvent.current.clientY;
577
- const incX = x - refs.previousMouseEvent.current.clientX;
578
- const left = (0, utils_1.clamp)(selectorRect.left + incX, 0);
579
- if ((selectorRect.height + incY < 0) && (selectorRect.right - left < 0))
580
- refs.mouseDown.current.version = 'top_right';
581
- else if (selectorRect.height + incY < 0)
582
- refs.mouseDown.current.version = 'top_left';
583
- else if (selectorRect.right - left < 0)
584
- refs.mouseDown.current.version = 'bottom_right';
585
- let width = (0, utils_1.clamp)(selectorRect.right - left, 0);
586
- let height = (0, utils_1.clamp)(Math.abs(selectorRect.height + incY), 0, rootRect.height - selectorRect.top);
587
- // Left
588
- if (refs.aspectRatio.current !== undefined) {
589
- // Left
590
- if (left < refs.selector.current.left && ((_r = refs.selector.current) === null || _r === void 0 ? void 0 : _r.top) + ((_s = refs.selector.current) === null || _s === void 0 ? void 0 : _s.height) >= rootRect.height)
591
- return;
592
- height = width / refs.aspectRatio.current;
593
- // Max width
594
- if (refs.selector.current.left + width > rootRect.width) {
595
- width = rootRect.width - refs.selector.current.left;
596
- height = width / refs.aspectRatio.current;
597
- }
598
- // Max height
599
- if (refs.selector.current.top + height > rootRect.height) {
600
- height = rootRect.height - refs.selector.current.top;
601
- width = height * refs.aspectRatio.current;
602
- }
603
- width = height * refs.aspectRatio.current;
604
- // Max width
605
- if (refs.selector.current.left + width > rootRect.width) {
606
- width = rootRect.width - refs.selector.current.left;
607
- height = width / refs.aspectRatio.current;
608
- }
609
- // Max height
610
- if (refs.selector.current.top + height > rootRect.height) {
611
- height = rootRect.height - refs.selector.current.top;
612
- width = height * refs.aspectRatio.current;
613
- }
614
- }
615
- onSelectorChange(Object.assign(Object.assign({}, refs.selector.current), { left,
616
- width,
617
- height }));
618
- }
619
- else if (((_t = refs.mouseDown.current) === null || _t === void 0 ? void 0 : _t.version) === 'top') {
620
- const inc = y - refs.previousMouseEvent.current.clientY;
621
- const top = (0, utils_1.clamp)(refs.selector.current.top + inc, 0);
622
- if (refs.selector.current.top <= 0 && top <= 0)
623
- return;
624
- if (selectorRect.bottom - top < 0)
625
- refs.mouseDown.current.version = 'bottom';
626
- let width = refs.selector.current.width;
627
- let height = (0, utils_1.clamp)(Math.abs(refs.selector.current.height - inc), 0, previousTop - top);
628
- if (refs.aspectRatio.current !== undefined) {
629
- // Top
630
- if (top < refs.selector.current.top && refs.selector.current.top < previousTop && refs.selector.current.left + refs.selector.current.width >= rootRect.width)
631
- return;
632
- width = height * refs.aspectRatio.current;
633
- // Max width
634
- if (refs.selector.current.left + width > rootRect.width) {
635
- width = rootRect.width - refs.selector.current.left;
636
- height = width / refs.aspectRatio.current;
637
- }
638
- // Max height
639
- if (top + height > rootRect.height) {
640
- height = rootRect.height - top;
641
- width = height * refs.aspectRatio.current;
642
- }
643
- }
644
- onSelectorChange(Object.assign(Object.assign({}, refs.selector.current), { top,
645
- width,
646
- height }));
647
- }
648
- else if (((_u = refs.mouseDown.current) === null || _u === void 0 ? void 0 : _u.version) === 'bottom') {
649
- const inc = y - refs.previousMouseEvent.current.clientY;
650
- if (selectorRect.height + inc < 0)
651
- refs.mouseDown.current.version = 'top';
652
- let width = refs.selector.current.width;
653
- let height = (0, utils_1.clamp)(Math.abs(selectorRect.height + inc), 0, rootRect.height - refs.selector.current.top);
654
- if (refs.aspectRatio.current !== undefined) {
655
- width = height * refs.aspectRatio.current;
656
- // Max height
657
- if (refs.selector.current.top + height > rootRect.height) {
658
- height = rootRect.height - refs.selector.current.top;
659
- width = height * refs.aspectRatio.current;
660
- }
661
- // Max width
662
- if (refs.selector.current.left + width > rootRect.width) {
663
- width = rootRect.width - refs.selector.current.left;
664
- height = width / refs.aspectRatio.current;
665
- }
666
- }
667
- onSelectorChange(Object.assign(Object.assign({}, refs.selector.current), { width,
668
- height }));
669
- }
670
- else if (((_v = refs.mouseDown.current) === null || _v === void 0 ? void 0 : _v.version) === 'left') {
671
- const inc = x - refs.previousMouseEvent.current.clientX;
672
- const left = (0, utils_1.clamp)(selectorRect.left + inc, 0);
673
- if (selectorRect.right - left < 0)
674
- refs.mouseDown.current.version = 'right';
675
- let width = (0, utils_1.clamp)(selectorRect.right - left, 0);
676
- let height = refs.selector.current.height;
677
- if (refs.aspectRatio.current !== undefined) {
678
- if (left < refs.selector.current.left && ((_w = refs.selector.current) === null || _w === void 0 ? void 0 : _w.top) + ((_x = refs.selector.current) === null || _x === void 0 ? void 0 : _x.height) >= rootRect.height)
679
- return;
680
- height = width / refs.aspectRatio.current;
681
- // Max width
682
- if (refs.selector.current.left + width > rootRect.width) {
683
- width = rootRect.width - refs.selector.current.left;
684
- height = width / refs.aspectRatio.current;
685
- }
686
- // Max height
687
- if (refs.selector.current.top + height > rootRect.height) {
688
- height = rootRect.height - refs.selector.current.top;
689
- width = height * refs.aspectRatio.current;
690
- }
691
- }
692
- onSelectorChange(Object.assign(Object.assign({}, refs.selector.current), { left,
693
- width,
694
- height }));
695
- }
696
- else if (((_y = refs.mouseDown.current) === null || _y === void 0 ? void 0 : _y.version) === 'right') {
697
- const inc = x - refs.previousMouseEvent.current.clientX;
698
- if (selectorRect.width + inc < 0)
699
- refs.mouseDown.current.version = 'left';
700
- let width = (0, utils_1.clamp)(Math.abs(selectorRect.width + inc), 0, rootRect.width - selectorRect.left);
701
- let height = refs.selector.current.height;
702
- if (refs.aspectRatio.current !== undefined) {
703
- height = width / refs.aspectRatio.current;
704
- // Max width
705
- if (refs.selector.current.left + width > rootRect.width) {
706
- width = rootRect.width - refs.selector.current.left;
707
- height = width / refs.aspectRatio.current;
708
- }
709
- // Max height
710
- if (refs.selector.current.top + height > rootRect.height) {
711
- height = rootRect.height - refs.selector.current.top;
712
- width = height * refs.aspectRatio.current;
713
- }
714
- }
715
- onSelectorChange(Object.assign(Object.assign({}, refs.selector.current), { width,
716
- height }));
717
- }
586
+
587
+ // Max height
588
+ if (refs.selector.current.top + height > rootRect.height) {
589
+ height = rootRect.height - refs.selector.current.top;
590
+ width = height * refs.aspectRatio.current;
718
591
  }
719
- };
720
- // Mouse move
721
- const onMouseMove = (event) => {
722
- if (refs.mouseDown.current) {
723
- const { clientY, clientX } = event;
724
- onMove(clientX, clientY);
725
- refs.previousMouseEvent.current = event;
592
+ }
593
+ onSelectorChange(_objectSpread(_objectSpread({}, refs.selector.current), {}, {
594
+ top,
595
+ width,
596
+ height
597
+ }));
598
+ } else if (((_refs$mouseDown$curre6 = refs.mouseDown.current) === null || _refs$mouseDown$curre6 === void 0 ? void 0 : _refs$mouseDown$curre6.version) === 'bottom_right') {
599
+ const incY = y - refs.previousMouseEvent.current.clientY;
600
+ const incX = x - refs.previousMouseEvent.current.clientX;
601
+ if (selectorRect.height + incY < 0 && selectorRect.width + incX < 0) refs.mouseDown.current.version = 'top_left';else if (selectorRect.height + incY < 0) refs.mouseDown.current.version = 'top_right';else if (selectorRect.width + incX < 0) refs.mouseDown.current.version = 'bottom_left';
602
+ let width = (0, _utils.clamp)(Math.abs(selectorRect.width + incX), 0, rootRect.width - selectorRect.left);
603
+ let height = (0, _utils.clamp)(Math.abs(selectorRect.height + incY), 0, rootRect.height - selectorRect.top);
604
+
605
+ // Right
606
+ if (refs.aspectRatio.current !== undefined) {
607
+ height = width / refs.aspectRatio.current;
608
+
609
+ // Max width
610
+ if (refs.selector.current.left + width > rootRect.width) {
611
+ width = rootRect.width - refs.selector.current.left;
612
+ height = width / refs.aspectRatio.current;
726
613
  }
727
- };
728
- // Touch move
729
- const onTouchMove = (event) => {
730
- if (refs.mouseDown.current) {
731
- const { clientY, clientX } = event.touches[0];
732
- onMove(clientX, clientY);
733
- refs.previousMouseEvent.current = event;
734
- // Normalize for use as a mouseDown value
735
- refs.previousMouseEvent.current.clientY = clientY;
736
- refs.previousMouseEvent.current.clientX = clientX;
614
+
615
+ // Max height
616
+ if (refs.selector.current.top + height > rootRect.height) {
617
+ height = rootRect.height - refs.selector.current.top;
618
+ width = height * refs.aspectRatio.current;
737
619
  }
738
- };
739
- const rootDocument = (0, utils_1.isEnvironment)('browser') ? (((_a = refs.root.current) === null || _a === void 0 ? void 0 : _a.ownerDocument) || window.document) : undefined;
740
- rootDocument.addEventListener('keydown', method);
741
- rootDocument.addEventListener('mousemove', onMouseMove);
742
- rootDocument.addEventListener('mouseup', onMouseUp);
743
- rootDocument.addEventListener('touchmove', onTouchMove, { passive: true });
744
- rootDocument.addEventListener('touchend', onMouseUp);
745
- return () => {
746
- // Clean up
747
- rootDocument.removeEventListener('keydown', method);
748
- rootDocument.removeEventListener('mousemove', onMouseMove);
749
- rootDocument.removeEventListener('mouseup', onMouseUp);
750
- rootDocument.removeEventListener('touchmove', onTouchMove);
751
- rootDocument.removeEventListener('touchend', onMouseUp);
752
- if (refs.image.current) {
753
- setImage('');
754
- if (rootDocument.body.style.overflow === 'hidden')
755
- rootDocument.body.style.removeProperty('overflow');
620
+ width = height * refs.aspectRatio.current;
621
+
622
+ // Max width
623
+ if (refs.selector.current.left + width > rootRect.width) {
624
+ width = rootRect.width - refs.selector.current.left;
625
+ height = width / refs.aspectRatio.current;
756
626
  }
757
- };
758
- }, []);
759
- react_1.default.useEffect(() => {
760
- if (image_ !== image) {
761
- if (image_ instanceof HTMLCanvasElement)
762
- setImage(image_);
763
- else if ((0, utils_1.is)('string', image_))
764
- !refs.dynamicParent.current ? makeImage(image_) : setTimeout(() => makeImage(image_), 140);
765
- }
766
- }, [image_]);
767
- const updateSelector = (selector__ = refs.selector.current, image__ = refs.image.current) => {
768
- var _a, _b, _c, _d;
769
- if (selector__ !== undefined && image__ !== undefined) {
770
- const rootRect = refs.root.current.getBoundingClientRect();
771
- const selectorValue = {
772
- top: (0, utils_1.clamp)(selector__.top, 0, image__.height),
773
- left: (0, utils_1.clamp)(selector__.left, 0, image__.height)
774
- };
775
- selectorValue.width = (0, utils_1.clamp)((selector__.width || 0), 0, rootRect.width - selectorValue.left);
776
- selectorValue.height = (0, utils_1.clamp)((selector__.height || 0), 0, rootRect.height - selectorValue.top);
777
- // Aspect ratio
778
- if (refs.aspectRatio.current !== undefined) {
779
- // Max surface
780
- if (selectorValue.width + (selectorValue.width / refs.aspectRatio.current) >= selectorValue.height + (selectorValue.height * refs.aspectRatio.current)) {
781
- selectorValue.height = selectorValue.width / refs.aspectRatio.current;
782
- }
783
- else {
784
- selectorValue.width = selectorValue.height * refs.aspectRatio.current;
785
- }
786
- // Max width
787
- if (selectorValue.left + selectorValue.width > rootRect.width) {
788
- selectorValue.width = rootRect.width - selectorValue.left;
789
- selectorValue.height = selectorValue.width / refs.aspectRatio.current;
790
- }
791
- // Max height
792
- if (selectorValue.top + selectorValue.height > rootRect.height) {
793
- selectorValue.height = rootRect.height - selectorValue.top;
794
- selectorValue.width = selectorValue.height * refs.aspectRatio.current;
795
- }
627
+
628
+ // Max height
629
+ if (refs.selector.current.top + height > rootRect.height) {
630
+ height = rootRect.height - refs.selector.current.top;
631
+ width = height * refs.aspectRatio.current;
632
+ }
633
+ }
634
+ onSelectorChange(_objectSpread(_objectSpread({}, refs.selector.current), {}, {
635
+ width,
636
+ height
637
+ }));
638
+ } else if (((_refs$mouseDown$curre7 = refs.mouseDown.current) === null || _refs$mouseDown$curre7 === void 0 ? void 0 : _refs$mouseDown$curre7.version) === 'bottom_left') {
639
+ const incY = y - refs.previousMouseEvent.current.clientY;
640
+ const incX = x - refs.previousMouseEvent.current.clientX;
641
+ const left = (0, _utils.clamp)(selectorRect.left + incX, 0);
642
+ if (selectorRect.height + incY < 0 && selectorRect.right - left < 0) refs.mouseDown.current.version = 'top_right';else if (selectorRect.height + incY < 0) refs.mouseDown.current.version = 'top_left';else if (selectorRect.right - left < 0) refs.mouseDown.current.version = 'bottom_right';
643
+ let width = (0, _utils.clamp)(selectorRect.right - left, 0);
644
+ let height = (0, _utils.clamp)(Math.abs(selectorRect.height + incY), 0, rootRect.height - selectorRect.top);
645
+
646
+ // Left
647
+ if (refs.aspectRatio.current !== undefined) {
648
+ var _refs$selector$curren14, _refs$selector$curren15;
649
+ // Left
650
+ if (left < refs.selector.current.left && ((_refs$selector$curren14 = refs.selector.current) === null || _refs$selector$curren14 === void 0 ? void 0 : _refs$selector$curren14.top) + ((_refs$selector$curren15 = refs.selector.current) === null || _refs$selector$curren15 === void 0 ? void 0 : _refs$selector$curren15.height) >= rootRect.height) return;
651
+ height = width / refs.aspectRatio.current;
652
+
653
+ // Max width
654
+ if (refs.selector.current.left + width > rootRect.width) {
655
+ width = rootRect.width - refs.selector.current.left;
656
+ height = width / refs.aspectRatio.current;
657
+ }
658
+
659
+ // Max height
660
+ if (refs.selector.current.top + height > rootRect.height) {
661
+ height = rootRect.height - refs.selector.current.top;
662
+ width = height * refs.aspectRatio.current;
663
+ }
664
+ width = height * refs.aspectRatio.current;
665
+
666
+ // Max width
667
+ if (refs.selector.current.left + width > rootRect.width) {
668
+ width = rootRect.width - refs.selector.current.left;
669
+ height = width / refs.aspectRatio.current;
796
670
  }
797
- // Update selector
798
- setSelector(selectorValue);
799
- // Update selector relative
800
- setSelectorRelative({
801
- top: (selectorValue.top / (rootRect === null || rootRect === void 0 ? void 0 : rootRect.height)) * ((_a = refs.canvasMain.current) === null || _a === void 0 ? void 0 : _a.height),
802
- left: (selectorValue.left / (rootRect === null || rootRect === void 0 ? void 0 : rootRect.width)) * ((_b = refs.canvasMain.current) === null || _b === void 0 ? void 0 : _b.width),
803
- width: (selectorValue.width / (rootRect === null || rootRect === void 0 ? void 0 : rootRect.width)) * ((_c = refs.canvasMain.current) === null || _c === void 0 ? void 0 : _c.width),
804
- height: (selectorValue.height / (rootRect === null || rootRect === void 0 ? void 0 : rootRect.height)) * ((_d = refs.canvasMain.current) === null || _d === void 0 ? void 0 : _d.height)
805
- });
671
+
672
+ // Max height
673
+ if (refs.selector.current.top + height > rootRect.height) {
674
+ height = rootRect.height - refs.selector.current.top;
675
+ width = height * refs.aspectRatio.current;
676
+ }
677
+ }
678
+ onSelectorChange(_objectSpread(_objectSpread({}, refs.selector.current), {}, {
679
+ left,
680
+ width,
681
+ height
682
+ }));
683
+ } else if (((_refs$mouseDown$curre8 = refs.mouseDown.current) === null || _refs$mouseDown$curre8 === void 0 ? void 0 : _refs$mouseDown$curre8.version) === 'top') {
684
+ const inc = y - refs.previousMouseEvent.current.clientY;
685
+ const top = (0, _utils.clamp)(refs.selector.current.top + inc, 0);
686
+ if (refs.selector.current.top <= 0 && top <= 0) return;
687
+ if (selectorRect.bottom - top < 0) refs.mouseDown.current.version = 'bottom';
688
+ let width = refs.selector.current.width;
689
+ let height = (0, _utils.clamp)(Math.abs(refs.selector.current.height - inc), 0, previousTop - top);
690
+ if (refs.aspectRatio.current !== undefined) {
691
+ // Top
692
+ if (top < refs.selector.current.top && refs.selector.current.top < previousTop && refs.selector.current.left + refs.selector.current.width >= rootRect.width) return;
693
+ width = height * refs.aspectRatio.current;
694
+
695
+ // Max width
696
+ if (refs.selector.current.left + width > rootRect.width) {
697
+ width = rootRect.width - refs.selector.current.left;
698
+ height = width / refs.aspectRatio.current;
699
+ }
700
+
701
+ // Max height
702
+ if (top + height > rootRect.height) {
703
+ height = rootRect.height - top;
704
+ width = height * refs.aspectRatio.current;
705
+ }
706
+ }
707
+ onSelectorChange(_objectSpread(_objectSpread({}, refs.selector.current), {}, {
708
+ top,
709
+ width,
710
+ height
711
+ }));
712
+ } else if (((_refs$mouseDown$curre9 = refs.mouseDown.current) === null || _refs$mouseDown$curre9 === void 0 ? void 0 : _refs$mouseDown$curre9.version) === 'bottom') {
713
+ const inc = y - refs.previousMouseEvent.current.clientY;
714
+ if (selectorRect.height + inc < 0) refs.mouseDown.current.version = 'top';
715
+ let width = refs.selector.current.width;
716
+ let height = (0, _utils.clamp)(Math.abs(selectorRect.height + inc), 0, rootRect.height - refs.selector.current.top);
717
+ if (refs.aspectRatio.current !== undefined) {
718
+ width = height * refs.aspectRatio.current;
719
+
720
+ // Max height
721
+ if (refs.selector.current.top + height > rootRect.height) {
722
+ height = rootRect.height - refs.selector.current.top;
723
+ width = height * refs.aspectRatio.current;
724
+ }
725
+
726
+ // Max width
727
+ if (refs.selector.current.left + width > rootRect.width) {
728
+ width = rootRect.width - refs.selector.current.left;
729
+ height = width / refs.aspectRatio.current;
730
+ }
731
+ }
732
+ onSelectorChange(_objectSpread(_objectSpread({}, refs.selector.current), {}, {
733
+ width,
734
+ height
735
+ }));
736
+ } else if (((_refs$mouseDown$curre10 = refs.mouseDown.current) === null || _refs$mouseDown$curre10 === void 0 ? void 0 : _refs$mouseDown$curre10.version) === 'left') {
737
+ const inc = x - refs.previousMouseEvent.current.clientX;
738
+ const left = (0, _utils.clamp)(selectorRect.left + inc, 0);
739
+ if (selectorRect.right - left < 0) refs.mouseDown.current.version = 'right';
740
+ let width = (0, _utils.clamp)(selectorRect.right - left, 0);
741
+ let height = refs.selector.current.height;
742
+ if (refs.aspectRatio.current !== undefined) {
743
+ var _refs$selector$curren16, _refs$selector$curren17;
744
+ if (left < refs.selector.current.left && ((_refs$selector$curren16 = refs.selector.current) === null || _refs$selector$curren16 === void 0 ? void 0 : _refs$selector$curren16.top) + ((_refs$selector$curren17 = refs.selector.current) === null || _refs$selector$curren17 === void 0 ? void 0 : _refs$selector$curren17.height) >= rootRect.height) return;
745
+ height = width / refs.aspectRatio.current;
746
+
747
+ // Max width
748
+ if (refs.selector.current.left + width > rootRect.width) {
749
+ width = rootRect.width - refs.selector.current.left;
750
+ height = width / refs.aspectRatio.current;
751
+ }
752
+
753
+ // Max height
754
+ if (refs.selector.current.top + height > rootRect.height) {
755
+ height = rootRect.height - refs.selector.current.top;
756
+ width = height * refs.aspectRatio.current;
757
+ }
758
+ }
759
+ onSelectorChange(_objectSpread(_objectSpread({}, refs.selector.current), {}, {
760
+ left,
761
+ width,
762
+ height
763
+ }));
764
+ } else if (((_refs$mouseDown$curre11 = refs.mouseDown.current) === null || _refs$mouseDown$curre11 === void 0 ? void 0 : _refs$mouseDown$curre11.version) === 'right') {
765
+ const inc = x - refs.previousMouseEvent.current.clientX;
766
+ if (selectorRect.width + inc < 0) refs.mouseDown.current.version = 'left';
767
+ let width = (0, _utils.clamp)(Math.abs(selectorRect.width + inc), 0, rootRect.width - selectorRect.left);
768
+ let height = refs.selector.current.height;
769
+ if (refs.aspectRatio.current !== undefined) {
770
+ height = width / refs.aspectRatio.current;
771
+
772
+ // Max width
773
+ if (refs.selector.current.left + width > rootRect.width) {
774
+ width = rootRect.width - refs.selector.current.left;
775
+ height = width / refs.aspectRatio.current;
776
+ }
777
+
778
+ // Max height
779
+ if (refs.selector.current.top + height > rootRect.height) {
780
+ height = rootRect.height - refs.selector.current.top;
781
+ width = height * refs.aspectRatio.current;
782
+ }
783
+ }
784
+ onSelectorChange(_objectSpread(_objectSpread({}, refs.selector.current), {}, {
785
+ width,
786
+ height
787
+ }));
806
788
  }
789
+ }
807
790
  };
808
- react_1.default.useEffect(() => {
809
- updateSelector();
810
- }, [aspectRatio]);
811
- react_1.default.useEffect(() => {
812
- if (selector_ !== selector)
813
- updateSelector(selector_);
814
- }, [selector_]);
815
- react_1.default.useEffect(() => {
816
- if (image) {
817
- refs.canvasMain.current.width = image.width;
818
- refs.canvasMain.current.height = image.height;
819
- refs.canvasMain.current.getContext('2d').drawImage(image, 0, 0), image.width, image.height;
820
- refs.canvasImageSelector.current.width = image.width;
821
- refs.canvasImageSelector.current.height = image.height;
822
- refs.canvasImageSelector.current.getContext('2d').drawImage(image, 0, 0), image.width, image.height;
823
- // Update selector
824
- updateSelector();
825
- }
826
- }, [image]);
827
- const makeImage = async (value = image) => {
828
- var _a;
829
- const img = await (0, utils_2.image)(value);
830
- const rootDocument = (0, utils_1.isEnvironment)('browser') ? (((_a = refs.root.current) === null || _a === void 0 ? void 0 : _a.ownerDocument) || window.document) : undefined;
831
- const canvas = rootDocument.createElement('canvas');
832
- const rootRect = refs.root.current.getBoundingClientRect();
833
- const aspectRatioImg = img.width / img.height;
834
- // width being parent width
835
- img.width = rootRect.width;
836
- // height keep aspect ratio of the img for the height
837
- img.height = img.width / aspectRatioImg;
838
- canvas.width = img.width;
839
- canvas.height = img.height;
840
- canvas.getContext('2d').drawImage(img, 0, 0, img.width, img.height);
841
- setImage(canvas);
791
+
792
+ // Mouse move
793
+ const onMouseMove = event => {
794
+ if (refs.mouseDown.current) {
795
+ const {
796
+ clientY,
797
+ clientX
798
+ } = event;
799
+ onMove(clientX, clientY);
800
+ refs.previousMouseEvent.current = event;
801
+ }
842
802
  };
843
- const onFocus = react_1.default.useCallback((event) => {
844
- setFocus(true);
845
- if ((0, utils_1.is)('function', onFocus_))
846
- onFocus_(event);
847
- }, []);
848
- const onBlur = react_1.default.useCallback((event) => {
849
- setFocus(false);
850
- if ((0, utils_1.is)('function', onBlur_))
851
- onBlur_(event);
852
- }, []);
853
- const onTouchStart = react_1.default.useCallback((event) => {
854
- if (![
855
- refs.imageSelectorMain.current,
856
- refs.imageSelector.current,
857
- refs.move.current,
858
- refs.borderTop.current,
859
- refs.borderLeft.current,
860
- refs.borderRight.current,
861
- refs.borderBottom.current,
862
- refs.dotTopLeft.current,
863
- refs.dotTopRight.current,
864
- refs.dotBottomLeft.current,
865
- refs.dotBottomRight.current
866
- ].includes(event.target)) {
867
- const { clientY, clientX } = event.touches[0];
868
- const rootRect = refs.root.current.getBoundingClientRect();
869
- setMouseDown({
870
- version: 'make',
871
- top: clientY - rootRect.top,
872
- left: clientX - rootRect.left
873
- });
874
- }
875
- }, []);
876
- const onMouseDown = react_1.default.useCallback((event) => {
877
- if (![
878
- refs.imageSelectorMain.current,
879
- refs.imageSelector.current,
880
- refs.move.current,
881
- refs.borderTop.current,
882
- refs.borderLeft.current,
883
- refs.borderRight.current,
884
- refs.borderBottom.current,
885
- refs.dotTopLeft.current,
886
- refs.dotTopRight.current,
887
- refs.dotBottomLeft.current,
888
- refs.dotBottomRight.current
889
- ].includes(event.target)) {
890
- const { clientY, clientX } = event;
891
- const rootRect = refs.root.current.getBoundingClientRect();
892
- setMouseDown({
893
- version: 'make',
894
- top: clientY - rootRect.top,
895
- left: clientX - rootRect.left
896
- });
897
- }
898
- }, []);
899
- const onTouchStartSelector = react_1.default.useCallback((event) => {
900
- const { clientY, clientX } = event.touches[0];
901
- const rootRect = refs.root.current.getBoundingClientRect();
902
- setMouseDown({
903
- version: 'move',
904
- top: clientY - rootRect.top,
905
- left: clientX - rootRect.left
906
- });
907
- }, []);
908
- const onMouseDownSelector = react_1.default.useCallback((event) => {
909
- const { clientY, clientX } = event;
910
- const rootRect = refs.root.current.getBoundingClientRect();
911
- setMouseDown({
912
- version: 'move',
913
- top: clientY - rootRect.top,
914
- left: clientX - rootRect.left
915
- });
916
- }, []);
917
- const onTouchStartBorder = (version) => (event) => {
918
- var _a, _b, _c, _d;
919
- setMouseDown({
920
- version,
921
- top: ((_a = refs.selector.current) === null || _a === void 0 ? void 0 : _a.top) + ((_b = refs.selector.current) === null || _b === void 0 ? void 0 : _b.height),
922
- left: ((_c = refs.selector.current) === null || _c === void 0 ? void 0 : _c.left) + ((_d = refs.selector.current) === null || _d === void 0 ? void 0 : _d.width)
923
- });
803
+
804
+ // Touch move
805
+ const onTouchMove = event => {
806
+ if (refs.mouseDown.current) {
807
+ const {
808
+ clientY,
809
+ clientX
810
+ } = event.touches[0];
811
+ onMove(clientX, clientY);
812
+ refs.previousMouseEvent.current = event;
813
+
814
+ // Normalize for use as a mouseDown value
815
+ refs.previousMouseEvent.current.clientY = clientY;
816
+ refs.previousMouseEvent.current.clientX = clientX;
817
+ }
924
818
  };
925
- const onMouseDownBorder = (version) => (event) => {
926
- var _a, _b, _c, _d;
927
- setMouseDown({
928
- version,
929
- top: ((_a = refs.selector.current) === null || _a === void 0 ? void 0 : _a.top) + ((_b = refs.selector.current) === null || _b === void 0 ? void 0 : _b.height),
930
- left: ((_c = refs.selector.current) === null || _c === void 0 ? void 0 : _c.left) + ((_d = refs.selector.current) === null || _d === void 0 ? void 0 : _d.width)
931
- });
819
+ const rootDocument = (0, _utils.isEnvironment)('browser') ? ((_refs$root$current2 = refs.root.current) === null || _refs$root$current2 === void 0 ? void 0 : _refs$root$current2.ownerDocument) || window.document : undefined;
820
+ rootDocument.addEventListener('keydown', method);
821
+ rootDocument.addEventListener('mousemove', onMouseMove);
822
+ rootDocument.addEventListener('mouseup', onMouseUp);
823
+ rootDocument.addEventListener('touchmove', onTouchMove, {
824
+ passive: true
825
+ });
826
+ rootDocument.addEventListener('touchend', onMouseUp);
827
+ return () => {
828
+ // Clean up
829
+ rootDocument.removeEventListener('keydown', method);
830
+ rootDocument.removeEventListener('mousemove', onMouseMove);
831
+ rootDocument.removeEventListener('mouseup', onMouseUp);
832
+ rootDocument.removeEventListener('touchmove', onTouchMove);
833
+ rootDocument.removeEventListener('touchend', onMouseUp);
834
+ if (refs.image.current) {
835
+ setImage('');
836
+ if (rootDocument.body.style.overflow === 'hidden') rootDocument.body.style.removeProperty('overflow');
837
+ }
932
838
  };
933
- const rect = (_a = refs.root.current) === null || _a === void 0 ? void 0 : _a.getBoundingClientRect();
934
- return ((0, jsx_runtime_1.jsxs)(Component, Object.assign({ ref: item => {
935
- if (ref) {
936
- if ((0, utils_1.is)('function', ref))
937
- ref(item);
938
- else
939
- ref.current = item;
940
- }
941
- refs.root.current = item;
942
- }, tabIndex: 0, onFocus: onFocus, onBlur: onBlur, onTouchStart: onTouchStart, onMouseDown: onMouseDown, className: (0, style_react_1.classNames)([
943
- (0, utils_2.staticClassName)('ImageCrop', theme) && [
944
- 'onesy-ImageCrop-root'
945
- ],
946
- className,
947
- classes.root
948
- ]) }, other, { children: [(0, jsx_runtime_1.jsx)("canvas", { ref: refs.canvasMain, className: (0, style_react_1.classNames)([
949
- (0, utils_2.staticClassName)('ImageCrop', theme) && [
950
- 'onesy-ImageCrop-canvas',
951
- 'onesy-ImageCrop-canvas-main'
952
- ],
953
- classes.canvas,
954
- classes.canvas_main
955
- ]) }), (0, jsx_runtime_1.jsx)("div", { className: (0, style_react_1.classNames)([
956
- (0, utils_2.staticClassName)('ImageCrop', theme) && [
957
- 'onesy-ImageCrop-background'
958
- ],
959
- classes.background,
960
- image && classes.background_in
961
- ]) }), (0, jsx_runtime_1.jsx)(Tooltip, Object.assign({ open: mouseDown && ((selector === null || selector === void 0 ? void 0 : selector.width) + (selector === null || selector === void 0 ? void 0 : selector.height) > 0), label: `${Math.round((selectorRelative === null || selectorRelative === void 0 ? void 0 : selectorRelative.width) || 0)} x ${Math.round((selectorRelative === null || selectorRelative === void 0 ? void 0 : selectorRelative.height) || 0)}`, position: 'bottom' }, TooltipProps, { children: (0, jsx_runtime_1.jsxs)("div", Object.assign({ ref: refs.imageSelectorMain, className: (0, style_react_1.classNames)([
962
- (0, utils_2.staticClassName)('ImageCrop', theme) && [
963
- 'onesy-ImageCrop-image-selector-main'
964
- ],
965
- classes.imageSelector_main,
966
- selector && classes.imageSelector_main_in
967
- ]), style: Object.assign({}, selector) }, { children: [(0, jsx_runtime_1.jsx)("div", { ref: refs.move, onTouchStart: onTouchStartSelector, onMouseDown: onMouseDownSelector, className: (0, style_react_1.classNames)([
968
- (0, utils_2.staticClassName)('ImageCrop', theme) && [
969
- 'onesy-ImageCrop-move'
970
- ],
971
- classes.move
972
- ]) }), gridLines && (0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)("div", { className: (0, style_react_1.classNames)([
973
- (0, utils_2.staticClassName)('ImageCrop', theme) && [
974
- 'onesy-ImageCrop-grid-line',
975
- 'onesy-ImageCrop-grid-line-top-start'
976
- ],
977
- classes.grid_line,
978
- classes.grid_line_top_start
979
- ]) }), (0, jsx_runtime_1.jsx)("div", { className: (0, style_react_1.classNames)([
980
- (0, utils_2.staticClassName)('ImageCrop', theme) && [
981
- 'onesy-ImageCrop-grid-line',
982
- 'onesy-ImageCrop-grid-line-top-end'
983
- ],
984
- classes.grid_line,
985
- classes.grid_line_top_end
986
- ]) }), (0, jsx_runtime_1.jsx)("div", { className: (0, style_react_1.classNames)([
987
- (0, utils_2.staticClassName)('ImageCrop', theme) && [
988
- 'onesy-ImageCrop-grid-line',
989
- 'onesy-ImageCrop-grid-line-left-start'
990
- ],
991
- classes.grid_line,
992
- classes.grid_line_left_start
993
- ]) }), (0, jsx_runtime_1.jsx)("div", { className: (0, style_react_1.classNames)([
994
- (0, utils_2.staticClassName)('ImageCrop', theme) && [
995
- 'onesy-ImageCrop-grid-line',
996
- 'onesy-ImageCrop-grid-line-left-end'
997
- ],
998
- classes.grid_line,
999
- classes.grid_line_left_end
1000
- ]) })] }), (0, jsx_runtime_1.jsx)("div", { ref: refs.dotTopLeft, onTouchStart: onTouchStartBorder('top_left'), onMouseDown: onMouseDownBorder('top_left'), className: (0, style_react_1.classNames)([
1001
- (0, utils_2.staticClassName)('ImageCrop', theme) && [
1002
- 'onesy-ImageCrop-dot',
1003
- 'onesy-ImageCrop-dot-top-left'
1004
- ],
1005
- classes.dot,
1006
- classes.dot_top_left
1007
- ]) }), (0, jsx_runtime_1.jsx)("div", { ref: refs.dotTopRight, onTouchStart: onTouchStartBorder('top_right'), onMouseDown: onMouseDownBorder('top_right'), className: (0, style_react_1.classNames)([
1008
- (0, utils_2.staticClassName)('ImageCrop', theme) && [
1009
- 'onesy-ImageCrop-dot',
1010
- 'onesy-ImageCrop-dot-top-right'
1011
- ],
1012
- classes.dot,
1013
- classes.dot_top_right
1014
- ]) }), (0, jsx_runtime_1.jsx)("div", { ref: refs.dotBottomLeft, onTouchStart: onTouchStartBorder('bottom_left'), onMouseDown: onMouseDownBorder('bottom_left'), className: (0, style_react_1.classNames)([
1015
- (0, utils_2.staticClassName)('ImageCrop', theme) && [
1016
- 'onesy-ImageCrop-dot',
1017
- 'onesy-ImageCrop-dot-bottom-left'
1018
- ],
1019
- classes.dot,
1020
- classes.dot_bottom_left
1021
- ]) }), (0, jsx_runtime_1.jsx)("div", { ref: refs.dotBottomRight, onTouchStart: onTouchStartBorder('bottom_right'), onMouseDown: onMouseDownBorder('bottom_right'), className: (0, style_react_1.classNames)([
1022
- (0, utils_2.staticClassName)('ImageCrop', theme) && [
1023
- 'onesy-ImageCrop-dot',
1024
- 'onesy-ImageCrop-dot-bottom-right'
1025
- ],
1026
- classes.dot,
1027
- classes.dot_bottom_right
1028
- ]) }), (0, jsx_runtime_1.jsx)("div", { ref: refs.borderTop, onTouchStart: onTouchStartBorder('top'), onMouseDown: onMouseDownBorder('top'), className: (0, style_react_1.classNames)([
1029
- (0, utils_2.staticClassName)('ImageCrop', theme) && [
1030
- 'onesy-ImageCrop-border',
1031
- 'onesy-ImageCrop-border-top'
1032
- ],
1033
- classes.border,
1034
- classes.border_top
1035
- ]) }), (0, jsx_runtime_1.jsx)("div", { ref: refs.borderLeft, onTouchStart: onTouchStartBorder('left'), onMouseDown: onMouseDownBorder('left'), className: (0, style_react_1.classNames)([
1036
- (0, utils_2.staticClassName)('ImageCrop', theme) && [
1037
- 'onesy-ImageCrop-border',
1038
- 'onesy-ImageCrop-border-left'
1039
- ],
1040
- classes.border,
1041
- classes.border_left
1042
- ]) }), (0, jsx_runtime_1.jsx)("div", { ref: refs.borderRight, onTouchStart: onTouchStartBorder('right'), onMouseDown: onMouseDownBorder('right'), className: (0, style_react_1.classNames)([
1043
- (0, utils_2.staticClassName)('ImageCrop', theme) && [
1044
- 'onesy-ImageCrop-border',
1045
- 'onesy-ImageCrop-border-right'
1046
- ],
1047
- classes.border,
1048
- classes.border_right
1049
- ]) }), (0, jsx_runtime_1.jsx)("div", { ref: refs.borderBottom, onTouchStart: onTouchStartBorder('bottom'), onMouseDown: onMouseDownBorder('bottom'), className: (0, style_react_1.classNames)([
1050
- (0, utils_2.staticClassName)('ImageCrop', theme) && [
1051
- 'onesy-ImageCrop-border',
1052
- 'onesy-ImageCrop-border-bottom'
1053
- ],
1054
- classes.border,
1055
- classes.border_bottom
1056
- ]) })] })) })), (0, jsx_runtime_1.jsx)("div", Object.assign({ ref: refs.imageSelector, className: (0, style_react_1.classNames)([
1057
- (0, utils_2.staticClassName)('ImageCrop', theme) && [
1058
- 'onesy-ImageCrop-image-selector'
1059
- ],
1060
- classes.imageSelector
1061
- ]), style: Object.assign({}, selector) }, { children: (0, jsx_runtime_1.jsx)("canvas", { ref: refs.canvasImageSelector, className: (0, style_react_1.classNames)([
1062
- (0, utils_2.staticClassName)('ImageCrop', theme) && [
1063
- 'onesy-ImageCrop-canvas',
1064
- 'onesy-ImageCrop-canvas-image-selector'
1065
- ],
1066
- classes.canvas,
1067
- classes.canvas_imageSelector
1068
- ]), width: (image === null || image === void 0 ? void 0 : image.width) || 0, height: (image === null || image === void 0 ? void 0 : image.height) || 0, style: {
1069
- top: `${((selector === null || selector === void 0 ? void 0 : selector.top) || 0) * -1}px`,
1070
- left: `${((selector === null || selector === void 0 ? void 0 : selector.left) || 0) * -1}px`,
1071
- width: rect === null || rect === void 0 ? void 0 : rect.width,
1072
- height: rect === null || rect === void 0 ? void 0 : rect.height
1073
- } }) }))] })));
839
+ }, []);
840
+ _react.default.useEffect(() => {
841
+ if (image_ !== image) {
842
+ if (image_ instanceof HTMLCanvasElement) setImage(image_);else if ((0, _utils.is)('string', image_)) !refs.dynamicParent.current ? makeImage(image_) : setTimeout(() => makeImage(image_), 140);
843
+ }
844
+ }, [image_]);
845
+ const updateSelector = (selector__ = refs.selector.current, image__ = refs.image.current) => {
846
+ if (selector__ !== undefined && image__ !== undefined) {
847
+ var _refs$canvasMain$curr5, _refs$canvasMain$curr6, _refs$canvasMain$curr7, _refs$canvasMain$curr8;
848
+ const rootRect = refs.root.current.getBoundingClientRect();
849
+ const selectorValue = {
850
+ top: (0, _utils.clamp)(selector__.top, 0, image__.height),
851
+ left: (0, _utils.clamp)(selector__.left, 0, image__.height)
852
+ };
853
+ selectorValue.width = (0, _utils.clamp)(selector__.width || 0, 0, rootRect.width - selectorValue.left);
854
+ selectorValue.height = (0, _utils.clamp)(selector__.height || 0, 0, rootRect.height - selectorValue.top);
855
+
856
+ // Aspect ratio
857
+ if (refs.aspectRatio.current !== undefined) {
858
+ // Max surface
859
+ if (selectorValue.width + selectorValue.width / refs.aspectRatio.current >= selectorValue.height + selectorValue.height * refs.aspectRatio.current) {
860
+ selectorValue.height = selectorValue.width / refs.aspectRatio.current;
861
+ } else {
862
+ selectorValue.width = selectorValue.height * refs.aspectRatio.current;
863
+ }
864
+
865
+ // Max width
866
+ if (selectorValue.left + selectorValue.width > rootRect.width) {
867
+ selectorValue.width = rootRect.width - selectorValue.left;
868
+ selectorValue.height = selectorValue.width / refs.aspectRatio.current;
869
+ }
870
+
871
+ // Max height
872
+ if (selectorValue.top + selectorValue.height > rootRect.height) {
873
+ selectorValue.height = rootRect.height - selectorValue.top;
874
+ selectorValue.width = selectorValue.height * refs.aspectRatio.current;
875
+ }
876
+ }
877
+
878
+ // Update selector
879
+ setSelector(selectorValue);
880
+
881
+ // Update selector relative
882
+ setSelectorRelative({
883
+ top: selectorValue.top / (rootRect === null || rootRect === void 0 ? void 0 : rootRect.height) * ((_refs$canvasMain$curr5 = refs.canvasMain.current) === null || _refs$canvasMain$curr5 === void 0 ? void 0 : _refs$canvasMain$curr5.height),
884
+ left: selectorValue.left / (rootRect === null || rootRect === void 0 ? void 0 : rootRect.width) * ((_refs$canvasMain$curr6 = refs.canvasMain.current) === null || _refs$canvasMain$curr6 === void 0 ? void 0 : _refs$canvasMain$curr6.width),
885
+ width: selectorValue.width / (rootRect === null || rootRect === void 0 ? void 0 : rootRect.width) * ((_refs$canvasMain$curr7 = refs.canvasMain.current) === null || _refs$canvasMain$curr7 === void 0 ? void 0 : _refs$canvasMain$curr7.width),
886
+ height: selectorValue.height / (rootRect === null || rootRect === void 0 ? void 0 : rootRect.height) * ((_refs$canvasMain$curr8 = refs.canvasMain.current) === null || _refs$canvasMain$curr8 === void 0 ? void 0 : _refs$canvasMain$curr8.height)
887
+ });
888
+ }
889
+ };
890
+ _react.default.useEffect(() => {
891
+ updateSelector();
892
+ }, [aspectRatio]);
893
+ _react.default.useEffect(() => {
894
+ if (selector_ !== selector) updateSelector(selector_);
895
+ }, [selector_]);
896
+ _react.default.useEffect(() => {
897
+ if (image) {
898
+ refs.canvasMain.current.width = image.width;
899
+ refs.canvasMain.current.height = image.height;
900
+ refs.canvasMain.current.getContext('2d').drawImage(image, 0, 0), image.width, image.height;
901
+ refs.canvasImageSelector.current.width = image.width;
902
+ refs.canvasImageSelector.current.height = image.height;
903
+ refs.canvasImageSelector.current.getContext('2d').drawImage(image, 0, 0), image.width, image.height;
904
+
905
+ // Update selector
906
+ updateSelector();
907
+ }
908
+ }, [image]);
909
+ const makeImage = async (value = image) => {
910
+ var _refs$root$current3;
911
+ const img = await (0, _utils2.image)(value);
912
+ const rootDocument = (0, _utils.isEnvironment)('browser') ? ((_refs$root$current3 = refs.root.current) === null || _refs$root$current3 === void 0 ? void 0 : _refs$root$current3.ownerDocument) || window.document : undefined;
913
+ const canvas = rootDocument.createElement('canvas');
914
+ const rootRect = refs.root.current.getBoundingClientRect();
915
+ const aspectRatioImg = img.width / img.height;
916
+
917
+ // width being parent width
918
+ img.width = rootRect.width;
919
+
920
+ // height keep aspect ratio of the img for the height
921
+ img.height = img.width / aspectRatioImg;
922
+ canvas.width = img.width;
923
+ canvas.height = img.height;
924
+ canvas.getContext('2d').drawImage(img, 0, 0, img.width, img.height);
925
+ setImage(canvas);
926
+ };
927
+ const onFocus = _react.default.useCallback(event => {
928
+ setFocus(true);
929
+ if ((0, _utils.is)('function', onFocus_)) onFocus_(event);
930
+ }, []);
931
+ const onBlur = _react.default.useCallback(event => {
932
+ setFocus(false);
933
+ if ((0, _utils.is)('function', onBlur_)) onBlur_(event);
934
+ }, []);
935
+ const onTouchStart = _react.default.useCallback(event => {
936
+ if (![refs.imageSelectorMain.current, refs.imageSelector.current, refs.move.current, refs.borderTop.current, refs.borderLeft.current, refs.borderRight.current, refs.borderBottom.current, refs.dotTopLeft.current, refs.dotTopRight.current, refs.dotBottomLeft.current, refs.dotBottomRight.current].includes(event.target)) {
937
+ const {
938
+ clientY,
939
+ clientX
940
+ } = event.touches[0];
941
+ const rootRect = refs.root.current.getBoundingClientRect();
942
+ setMouseDown({
943
+ version: 'make',
944
+ top: clientY - rootRect.top,
945
+ left: clientX - rootRect.left
946
+ });
947
+ }
948
+ }, []);
949
+ const onMouseDown = _react.default.useCallback(event => {
950
+ if (![refs.imageSelectorMain.current, refs.imageSelector.current, refs.move.current, refs.borderTop.current, refs.borderLeft.current, refs.borderRight.current, refs.borderBottom.current, refs.dotTopLeft.current, refs.dotTopRight.current, refs.dotBottomLeft.current, refs.dotBottomRight.current].includes(event.target)) {
951
+ const {
952
+ clientY,
953
+ clientX
954
+ } = event;
955
+ const rootRect = refs.root.current.getBoundingClientRect();
956
+ setMouseDown({
957
+ version: 'make',
958
+ top: clientY - rootRect.top,
959
+ left: clientX - rootRect.left
960
+ });
961
+ }
962
+ }, []);
963
+ const onTouchStartSelector = _react.default.useCallback(event => {
964
+ const {
965
+ clientY,
966
+ clientX
967
+ } = event.touches[0];
968
+ const rootRect = refs.root.current.getBoundingClientRect();
969
+ setMouseDown({
970
+ version: 'move',
971
+ top: clientY - rootRect.top,
972
+ left: clientX - rootRect.left
973
+ });
974
+ }, []);
975
+ const onMouseDownSelector = _react.default.useCallback(event => {
976
+ const {
977
+ clientY,
978
+ clientX
979
+ } = event;
980
+ const rootRect = refs.root.current.getBoundingClientRect();
981
+ setMouseDown({
982
+ version: 'move',
983
+ top: clientY - rootRect.top,
984
+ left: clientX - rootRect.left
985
+ });
986
+ }, []);
987
+ const onTouchStartBorder = version => event => {
988
+ var _refs$selector$curren18, _refs$selector$curren19, _refs$selector$curren20, _refs$selector$curren21;
989
+ setMouseDown({
990
+ version,
991
+ top: ((_refs$selector$curren18 = refs.selector.current) === null || _refs$selector$curren18 === void 0 ? void 0 : _refs$selector$curren18.top) + ((_refs$selector$curren19 = refs.selector.current) === null || _refs$selector$curren19 === void 0 ? void 0 : _refs$selector$curren19.height),
992
+ left: ((_refs$selector$curren20 = refs.selector.current) === null || _refs$selector$curren20 === void 0 ? void 0 : _refs$selector$curren20.left) + ((_refs$selector$curren21 = refs.selector.current) === null || _refs$selector$curren21 === void 0 ? void 0 : _refs$selector$curren21.width)
993
+ });
994
+ };
995
+ const onMouseDownBorder = version => event => {
996
+ var _refs$selector$curren22, _refs$selector$curren23, _refs$selector$curren24, _refs$selector$curren25;
997
+ setMouseDown({
998
+ version,
999
+ top: ((_refs$selector$curren22 = refs.selector.current) === null || _refs$selector$curren22 === void 0 ? void 0 : _refs$selector$curren22.top) + ((_refs$selector$curren23 = refs.selector.current) === null || _refs$selector$curren23 === void 0 ? void 0 : _refs$selector$curren23.height),
1000
+ left: ((_refs$selector$curren24 = refs.selector.current) === null || _refs$selector$curren24 === void 0 ? void 0 : _refs$selector$curren24.left) + ((_refs$selector$curren25 = refs.selector.current) === null || _refs$selector$curren25 === void 0 ? void 0 : _refs$selector$curren25.width)
1001
+ });
1002
+ };
1003
+ const rect = (_refs$root$current4 = refs.root.current) === null || _refs$root$current4 === void 0 ? void 0 : _refs$root$current4.getBoundingClientRect();
1004
+ return /*#__PURE__*/_react.default.createElement(Component, (0, _extends2.default)({
1005
+ ref: item => {
1006
+ if (ref) {
1007
+ if ((0, _utils.is)('function', ref)) ref(item);else ref.current = item;
1008
+ }
1009
+ refs.root.current = item;
1010
+ },
1011
+ tabIndex: 0,
1012
+ onFocus: onFocus,
1013
+ onBlur: onBlur,
1014
+ onTouchStart: onTouchStart,
1015
+ onMouseDown: onMouseDown,
1016
+ className: (0, _styleReact.classNames)([(0, _utils2.staticClassName)('ImageCrop', theme) && ['onesy-ImageCrop-root'], className, classes.root])
1017
+ }, other), /*#__PURE__*/_react.default.createElement("canvas", {
1018
+ ref: refs.canvasMain,
1019
+ className: (0, _styleReact.classNames)([(0, _utils2.staticClassName)('ImageCrop', theme) && ['onesy-ImageCrop-canvas', 'onesy-ImageCrop-canvas-main'], classes.canvas, classes.canvas_main])
1020
+ }), /*#__PURE__*/_react.default.createElement("div", {
1021
+ className: (0, _styleReact.classNames)([(0, _utils2.staticClassName)('ImageCrop', theme) && ['onesy-ImageCrop-background'], classes.background, image && classes.background_in])
1022
+ }), /*#__PURE__*/_react.default.createElement(Tooltip, (0, _extends2.default)({
1023
+ open: mouseDown && (selector === null || selector === void 0 ? void 0 : selector.width) + (selector === null || selector === void 0 ? void 0 : selector.height) > 0,
1024
+ label: `${Math.round((selectorRelative === null || selectorRelative === void 0 ? void 0 : selectorRelative.width) || 0)} x ${Math.round((selectorRelative === null || selectorRelative === void 0 ? void 0 : selectorRelative.height) || 0)}`,
1025
+ position: "bottom"
1026
+ }, TooltipProps), /*#__PURE__*/_react.default.createElement("div", {
1027
+ ref: refs.imageSelectorMain,
1028
+ className: (0, _styleReact.classNames)([(0, _utils2.staticClassName)('ImageCrop', theme) && ['onesy-ImageCrop-image-selector-main'], classes.imageSelector_main, selector && classes.imageSelector_main_in]),
1029
+ style: _objectSpread({}, selector)
1030
+ }, /*#__PURE__*/_react.default.createElement("div", {
1031
+ ref: refs.move,
1032
+ onTouchStart: onTouchStartSelector,
1033
+ onMouseDown: onMouseDownSelector,
1034
+ className: (0, _styleReact.classNames)([(0, _utils2.staticClassName)('ImageCrop', theme) && ['onesy-ImageCrop-move'], classes.move])
1035
+ }), gridLines && /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement("div", {
1036
+ className: (0, _styleReact.classNames)([(0, _utils2.staticClassName)('ImageCrop', theme) && ['onesy-ImageCrop-grid-line', 'onesy-ImageCrop-grid-line-top-start'], classes.grid_line, classes.grid_line_top_start])
1037
+ }), /*#__PURE__*/_react.default.createElement("div", {
1038
+ className: (0, _styleReact.classNames)([(0, _utils2.staticClassName)('ImageCrop', theme) && ['onesy-ImageCrop-grid-line', 'onesy-ImageCrop-grid-line-top-end'], classes.grid_line, classes.grid_line_top_end])
1039
+ }), /*#__PURE__*/_react.default.createElement("div", {
1040
+ className: (0, _styleReact.classNames)([(0, _utils2.staticClassName)('ImageCrop', theme) && ['onesy-ImageCrop-grid-line', 'onesy-ImageCrop-grid-line-left-start'], classes.grid_line, classes.grid_line_left_start])
1041
+ }), /*#__PURE__*/_react.default.createElement("div", {
1042
+ className: (0, _styleReact.classNames)([(0, _utils2.staticClassName)('ImageCrop', theme) && ['onesy-ImageCrop-grid-line', 'onesy-ImageCrop-grid-line-left-end'], classes.grid_line, classes.grid_line_left_end])
1043
+ })), /*#__PURE__*/_react.default.createElement("div", {
1044
+ ref: refs.dotTopLeft,
1045
+ onTouchStart: onTouchStartBorder('top_left'),
1046
+ onMouseDown: onMouseDownBorder('top_left'),
1047
+ className: (0, _styleReact.classNames)([(0, _utils2.staticClassName)('ImageCrop', theme) && ['onesy-ImageCrop-dot', 'onesy-ImageCrop-dot-top-left'], classes.dot, classes.dot_top_left])
1048
+ }), /*#__PURE__*/_react.default.createElement("div", {
1049
+ ref: refs.dotTopRight,
1050
+ onTouchStart: onTouchStartBorder('top_right'),
1051
+ onMouseDown: onMouseDownBorder('top_right'),
1052
+ className: (0, _styleReact.classNames)([(0, _utils2.staticClassName)('ImageCrop', theme) && ['onesy-ImageCrop-dot', 'onesy-ImageCrop-dot-top-right'], classes.dot, classes.dot_top_right])
1053
+ }), /*#__PURE__*/_react.default.createElement("div", {
1054
+ ref: refs.dotBottomLeft,
1055
+ onTouchStart: onTouchStartBorder('bottom_left'),
1056
+ onMouseDown: onMouseDownBorder('bottom_left'),
1057
+ className: (0, _styleReact.classNames)([(0, _utils2.staticClassName)('ImageCrop', theme) && ['onesy-ImageCrop-dot', 'onesy-ImageCrop-dot-bottom-left'], classes.dot, classes.dot_bottom_left])
1058
+ }), /*#__PURE__*/_react.default.createElement("div", {
1059
+ ref: refs.dotBottomRight,
1060
+ onTouchStart: onTouchStartBorder('bottom_right'),
1061
+ onMouseDown: onMouseDownBorder('bottom_right'),
1062
+ className: (0, _styleReact.classNames)([(0, _utils2.staticClassName)('ImageCrop', theme) && ['onesy-ImageCrop-dot', 'onesy-ImageCrop-dot-bottom-right'], classes.dot, classes.dot_bottom_right])
1063
+ }), /*#__PURE__*/_react.default.createElement("div", {
1064
+ ref: refs.borderTop,
1065
+ onTouchStart: onTouchStartBorder('top'),
1066
+ onMouseDown: onMouseDownBorder('top'),
1067
+ className: (0, _styleReact.classNames)([(0, _utils2.staticClassName)('ImageCrop', theme) && ['onesy-ImageCrop-border', 'onesy-ImageCrop-border-top'], classes.border, classes.border_top])
1068
+ }), /*#__PURE__*/_react.default.createElement("div", {
1069
+ ref: refs.borderLeft,
1070
+ onTouchStart: onTouchStartBorder('left'),
1071
+ onMouseDown: onMouseDownBorder('left'),
1072
+ className: (0, _styleReact.classNames)([(0, _utils2.staticClassName)('ImageCrop', theme) && ['onesy-ImageCrop-border', 'onesy-ImageCrop-border-left'], classes.border, classes.border_left])
1073
+ }), /*#__PURE__*/_react.default.createElement("div", {
1074
+ ref: refs.borderRight,
1075
+ onTouchStart: onTouchStartBorder('right'),
1076
+ onMouseDown: onMouseDownBorder('right'),
1077
+ className: (0, _styleReact.classNames)([(0, _utils2.staticClassName)('ImageCrop', theme) && ['onesy-ImageCrop-border', 'onesy-ImageCrop-border-right'], classes.border, classes.border_right])
1078
+ }), /*#__PURE__*/_react.default.createElement("div", {
1079
+ ref: refs.borderBottom,
1080
+ onTouchStart: onTouchStartBorder('bottom'),
1081
+ onMouseDown: onMouseDownBorder('bottom'),
1082
+ className: (0, _styleReact.classNames)([(0, _utils2.staticClassName)('ImageCrop', theme) && ['onesy-ImageCrop-border', 'onesy-ImageCrop-border-bottom'], classes.border, classes.border_bottom])
1083
+ }))), /*#__PURE__*/_react.default.createElement("div", {
1084
+ ref: refs.imageSelector,
1085
+ className: (0, _styleReact.classNames)([(0, _utils2.staticClassName)('ImageCrop', theme) && ['onesy-ImageCrop-image-selector'], classes.imageSelector]),
1086
+ style: _objectSpread({}, selector)
1087
+ }, /*#__PURE__*/_react.default.createElement("canvas", {
1088
+ ref: refs.canvasImageSelector,
1089
+ className: (0, _styleReact.classNames)([(0, _utils2.staticClassName)('ImageCrop', theme) && ['onesy-ImageCrop-canvas', 'onesy-ImageCrop-canvas-image-selector'], classes.canvas, classes.canvas_imageSelector]),
1090
+ width: (image === null || image === void 0 ? void 0 : image.width) || 0,
1091
+ height: (image === null || image === void 0 ? void 0 : image.height) || 0,
1092
+ style: {
1093
+ top: `${((selector === null || selector === void 0 ? void 0 : selector.top) || 0) * -1}px`,
1094
+ left: `${((selector === null || selector === void 0 ? void 0 : selector.left) || 0) * -1}px`,
1095
+ width: rect === null || rect === void 0 ? void 0 : rect.width,
1096
+ height: rect === null || rect === void 0 ? void 0 : rect.height
1097
+ }
1098
+ })));
1074
1099
  });
1075
1100
  ImageCrop.displayName = 'onesy-ImageCrop';
1076
- exports.default = ImageCrop;
1101
+ var _default = exports.default = ImageCrop;