@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.
- package/Accordion/Accordion.js +342 -242
- package/Accordion/index.js +12 -7
- package/AdvancedTextField/AdvancedTextField.js +165 -157
- package/AdvancedTextField/index.js +12 -7
- package/Append/Append.js +502 -519
- package/Append/index.js +12 -7
- package/AreaChart/AreaChart.js +53 -40
- package/AreaChart/index.js +12 -7
- package/AreaChartItem/AreaChartItem.js +325 -237
- package/AreaChartItem/index.js +12 -7
- package/AudioPlayer/AudioPlayer.js +683 -466
- package/AudioPlayer/index.js +12 -7
- package/AudioRecorder/AudioRecorder.js +359 -259
- package/AudioRecorder/index.js +12 -7
- package/AutoComplete/AutoComplete.js +693 -568
- package/AutoComplete/index.js +12 -7
- package/AutoCompleteCountry/AutoCompleteCountry.js +99 -62
- package/AutoCompleteCountry/index.js +12 -7
- package/AutoCompleteCurrency/AutoCompleteCurrency.js +73 -61
- package/AutoCompleteCurrency/index.js +12 -7
- package/Avatar/Avatar.js +113 -86
- package/Avatar/index.js +12 -7
- package/AvatarGroup/AvatarGroup.js +104 -87
- package/AvatarGroup/index.js +12 -7
- package/Backdrop/Backdrop.js +160 -139
- package/Backdrop/index.js +12 -7
- package/Badge/Badge.js +116 -96
- package/Badge/index.js +12 -7
- package/Banner/Banner.js +126 -85
- package/Banner/index.js +12 -7
- package/BottomAppBar/BottomAppBar.js +89 -60
- package/BottomAppBar/index.js +12 -7
- package/BottomSheet/BottomSheet.js +66 -59
- package/BottomSheet/index.js +12 -7
- package/Box/Box.js +43 -35
- package/Box/index.js +12 -7
- package/Breadcrumbs/Breadcrumbs.js +168 -144
- package/Breadcrumbs/index.js +12 -7
- package/BubbleChart/BubbleChart.js +261 -132
- package/BubbleChart/index.js +12 -7
- package/BubbleChartItem/BubbleChartItem.js +228 -161
- package/BubbleChartItem/index.js +12 -7
- package/Button/Button.js +488 -467
- package/Button/index.js +12 -7
- package/Buttons/Buttons.js +366 -293
- package/Buttons/index.js +12 -7
- package/Calendar/Calendar.js +715 -452
- package/Calendar/index.js +12 -7
- package/CalendarAvailability/CalendarAvailability.js +709 -388
- package/CalendarAvailability/index.js +12 -7
- package/CalendarMenu/CalendarMenu.js +430 -303
- package/CalendarMenu/index.js +12 -7
- package/CalendarMonth/CalendarMonth.js +537 -498
- package/CalendarMonth/index.js +12 -7
- package/CalendarViews/CalendarViews.js +403 -254
- package/CalendarViews/index.js +12 -7
- package/CalendarWeek/CalendarWeek.js +536 -395
- package/CalendarWeek/index.js +12 -7
- package/Card/Card.js +133 -101
- package/Card/index.js +12 -7
- package/CardButton/CardButton.js +78 -58
- package/CardButton/index.js +12 -7
- package/CardFooter/CardFooter.js +58 -39
- package/CardFooter/index.js +12 -7
- package/CardHeader/CardHeader.js +61 -44
- package/CardHeader/index.js +12 -7
- package/CardImage/CardImage.js +80 -67
- package/CardImage/index.js +12 -7
- package/CardMain/CardMain.js +53 -39
- package/CardMain/index.js +12 -7
- package/Carousel/Carousel.js +1025 -913
- package/Carousel/index.js +12 -7
- package/Chart/Chart.js +1325 -1174
- package/Chart/index.js +12 -7
- package/Checkbox/Checkbox.js +339 -325
- package/Checkbox/index.js +12 -7
- package/Chip/Chip.js +131 -102
- package/Chip/index.js +12 -7
- package/Chips/Chips.js +121 -94
- package/Chips/index.js +12 -7
- package/ClickListener/ClickListener.js +86 -89
- package/ClickListener/index.js +12 -7
- package/Clock/Clock.js +470 -417
- package/Clock/index.js +12 -7
- package/ColorTextField/ColorTextField.js +234 -171
- package/ColorTextField/index.js +12 -7
- package/ColumnChart/ColumnChart.js +63 -46
- package/ColumnChart/index.js +12 -7
- package/ColumnChartItem/ColumnChartItem.js +208 -149
- package/ColumnChartItem/index.js +12 -7
- package/Confirm/Confirm.js +184 -129
- package/Confirm/Context.js +9 -7
- package/Confirm/index.js +32 -13
- package/Confirm/useConfirm.js +11 -10
- package/Container/Container.js +154 -133
- package/Container/index.js +12 -7
- package/CookieBanner/CookieBanner.js +142 -76
- package/CookieBanner/index.js +12 -7
- package/Countdown/Countdown.js +403 -255
- package/Countdown/index.js +12 -7
- package/DatePicker/DatePicker.js +802 -547
- package/DatePicker/index.js +12 -7
- package/DateRangePicker/DateRangePicker.js +44 -37
- package/DateRangePicker/index.js +12 -7
- package/DateTimePicker/DateTimePicker.js +672 -459
- package/DateTimePicker/index.js +12 -7
- package/DateTimeRangePicker/DateTimeRangePicker.js +44 -37
- package/DateTimeRangePicker/index.js +12 -7
- package/Divider/Divider.js +198 -185
- package/Divider/index.js +12 -7
- package/DonutChart/DonutChart.js +49 -42
- package/DonutChart/index.js +12 -7
- package/DragAndDropList/DragAndDropList.js +160 -156
- package/DragAndDropList/index.js +12 -7
- package/Drawing/Drawing.js +850 -523
- package/Drawing/index.js +12 -7
- package/DropZone/DropZone.js +239 -201
- package/DropZone/index.js +12 -7
- package/Emojis/Emojis.js +409 -317
- package/Emojis/emojis_list.js +7279 -9250
- package/Emojis/index.js +19 -9
- package/Expand/Expand.js +201 -149
- package/Expand/index.js +12 -7
- package/Fab/Fab.js +46 -26
- package/Fab/index.js +12 -7
- package/Fade/Fade.js +131 -101
- package/Fade/index.js +12 -7
- package/FileChoose/FileChoose.js +218 -150
- package/FileChoose/index.js +12 -7
- package/Focus/Focus.js +87 -88
- package/Focus/index.js +12 -7
- package/Form/Form.js +194 -120
- package/Form/index.js +12 -7
- package/FormRow/FormRow.js +123 -78
- package/FormRow/index.js +12 -7
- package/Forms/Forms.js +55 -41
- package/Forms/index.js +12 -7
- package/Frame/Frame.js +212 -150
- package/Frame/index.js +12 -7
- package/Grid/Grid.js +231 -166
- package/Grid/index.js +12 -7
- package/Grow/Grow.js +137 -109
- package/Grow/index.js +12 -7
- package/HTMLCanvas/HTMLCanvas.js +752 -542
- package/HTMLCanvas/index.js +24 -21
- package/IFrame/IFrame.js +122 -104
- package/IFrame/index.js +12 -7
- package/Icon/Icon.js +112 -95
- package/Icon/index.js +24 -21
- package/IconButton/IconButton.js +49 -37
- package/IconButton/index.js +12 -7
- package/Image/Image.js +285 -252
- package/Image/index.js +12 -7
- package/ImageCrop/ImageCrop.js +1073 -1048
- package/ImageCrop/index.js +12 -7
- package/ImageEdit/ImageEdit.js +1088 -783
- package/ImageEdit/index.js +12 -7
- package/ImageGallery/ImageGallery.js +518 -473
- package/ImageGallery/index.js +12 -7
- package/ImageList/ImageList.js +136 -129
- package/ImageList/index.js +12 -7
- package/ImageListItem/ImageListItem.js +101 -87
- package/ImageListItem/index.js +12 -7
- package/ImageListItemBox/ImageListItemBox.js +61 -50
- package/ImageListItemBox/index.js +12 -7
- package/Info/Info.js +99 -63
- package/Info/index.js +12 -7
- package/Interaction/Interaction.js +405 -345
- package/Interaction/index.js +12 -7
- package/Keyframes/Context.js +9 -7
- package/Keyframes/Keyframes.js +189 -174
- package/Keyframes/index.js +42 -25
- package/Keyframes/useKeyframes.js +11 -10
- package/Label/Label.js +177 -151
- package/Label/index.js +12 -7
- package/Labels/Labels.js +126 -98
- package/Labels/index.js +12 -7
- package/Line/Line.js +296 -174
- package/Line/index.js +12 -7
- package/LineChart/LineChart.js +57 -40
- package/LineChart/index.js +12 -7
- package/LineChartItem/LineChartItem.js +239 -177
- package/LineChartItem/index.js +12 -7
- package/LinearMeter/LinearMeter.js +453 -377
- package/LinearMeter/index.js +12 -7
- package/LinearProgress/LinearProgress.js +324 -255
- package/LinearProgress/index.js +12 -7
- package/Link/Link.js +182 -127
- package/Link/index.js +12 -7
- package/Links/Links.js +277 -156
- package/Links/index.js +12 -7
- package/List/List.js +148 -113
- package/List/index.js +12 -7
- package/ListItem/ListItem.js +388 -362
- package/ListItem/index.js +12 -7
- package/ListSubheader/ListSubheader.js +52 -44
- package/ListSubheader/index.js +12 -7
- package/MainProgress/Context.js +9 -7
- package/MainProgress/MainProgress.js +193 -161
- package/MainProgress/index.js +32 -13
- package/MainProgress/useMainProgress.js +11 -10
- package/Markdown/Markdown.js +676 -704
- package/Markdown/index.js +12 -7
- package/Masonry/Masonry.js +207 -181
- package/Masonry/index.js +12 -7
- package/Medias/Medias.js +436 -398
- package/Medias/index.js +12 -7
- package/Menu/Menu.js +278 -243
- package/Menu/index.js +12 -7
- package/MenuDesktop/MenuDesktop.js +496 -419
- package/MenuDesktop/index.js +12 -7
- package/MenuItem/MenuItem.js +286 -231
- package/MenuItem/index.js +12 -7
- package/Meta/Meta.js +78 -54
- package/Meta/index.js +12 -7
- package/Modal/Modal.js +333 -272
- package/Modal/index.js +12 -7
- package/ModalFooter/ModalFooter.js +50 -39
- package/ModalFooter/index.js +12 -7
- package/ModalHeader/ModalHeader.js +51 -39
- package/ModalHeader/index.js +12 -7
- package/ModalIcon/ModalIcon.js +29 -23
- package/ModalIcon/index.js +12 -7
- package/ModalMain/ModalMain.js +50 -41
- package/ModalMain/index.js +12 -7
- package/ModalText/ModalText.js +47 -37
- package/ModalText/index.js +12 -7
- package/ModalTitle/ModalTitle.js +47 -37
- package/ModalTitle/index.js +12 -7
- package/MoreOptions/MoreOptions.js +72 -52
- package/MoreOptions/index.js +12 -7
- package/Move/Move.js +166 -141
- package/Move/index.js +12 -7
- package/NavigationBar/NavigationBar.js +133 -109
- package/NavigationBar/index.js +12 -7
- package/NavigationDrawer/NavigationDrawer.js +241 -211
- package/NavigationDrawer/index.js +12 -7
- package/NavigationItem/NavigationItem.js +293 -262
- package/NavigationItem/index.js +12 -7
- package/NavigationRail/NavigationRail.js +187 -150
- package/NavigationRail/index.js +12 -7
- package/NotFound/NotFound.js +89 -66
- package/NotFound/index.js +12 -7
- package/NumericTextField/NumericTextField.js +217 -212
- package/NumericTextField/index.js +12 -7
- package/Page/Page.js +131 -67
- package/Page/index.js +12 -7
- package/PageTransition/PageTransition.js +112 -102
- package/PageTransition/index.js +12 -7
- package/Pagination/Pagination.js +153 -98
- package/Pagination/index.js +12 -7
- package/PaginationItem/PaginationItem.js +85 -65
- package/PaginationItem/index.js +12 -7
- package/Parallax/Parallax.js +138 -145
- package/Parallax/index.js +12 -7
- package/Path/Path.js +40 -35
- package/Path/index.js +12 -7
- package/PieChart/PieChart.js +382 -267
- package/PieChart/index.js +12 -7
- package/Placeholder/Placeholder.js +125 -117
- package/Placeholder/index.js +12 -7
- package/Portal/Portal.js +37 -38
- package/Portal/index.js +12 -7
- package/Properties/Properties.js +85 -64
- package/Properties/index.js +12 -7
- package/Property/Property.js +120 -108
- package/Property/index.js +12 -7
- package/Radio/Radio.js +211 -181
- package/Radio/index.js +12 -7
- package/Radios/Radios.js +124 -92
- package/Radios/index.js +12 -7
- package/Rating/Rating.js +353 -360
- package/Rating/index.js +12 -7
- package/Reset/Reset.js +19 -24
- package/Reset/index.js +12 -7
- package/Reveal/Reveal.js +98 -89
- package/Reveal/index.js +12 -7
- package/RichTextEditor/RichTextEditor.js +1764 -972
- package/RichTextEditor/index.js +12 -7
- package/RoundMeter/RoundMeter.js +629 -537
- package/RoundMeter/index.js +12 -7
- package/RoundProgress/RoundProgress.js +233 -174
- package/RoundProgress/index.js +12 -7
- package/ScatterChart/ScatterChart.js +53 -40
- package/ScatterChart/index.js +12 -7
- package/ScatterChartItem/ScatterChartItem.js +214 -153
- package/ScatterChartItem/index.js +12 -7
- package/ScreenCapture/ScreenCapture.js +427 -335
- package/ScreenCapture/index.js +12 -7
- package/Section/Section.js +380 -319
- package/Section/index.js +12 -7
- package/SectionAction/SectionAction.js +72 -61
- package/SectionAction/index.js +12 -7
- package/SectionBoxes/SectionBoxes.js +240 -208
- package/SectionBoxes/index.js +12 -7
- package/SectionCards/SectionCards.js +256 -217
- package/SectionCards/index.js +12 -7
- package/SectionCarousel/SectionCarousel.js +284 -242
- package/SectionCarousel/index.js +12 -7
- package/SectionContact/SectionContact.js +174 -116
- package/SectionContact/index.js +12 -7
- package/SectionImageGallery/SectionImageGallery.js +64 -51
- package/SectionImageGallery/index.js +12 -7
- package/SectionLogos/SectionLogos.js +123 -109
- package/SectionLogos/index.js +12 -7
- package/SectionMedia/SectionMedia.js +170 -152
- package/SectionMedia/index.js +12 -7
- package/SectionReviews/SectionReviews.js +170 -129
- package/SectionReviews/index.js +12 -7
- package/SectionTextMedia/SectionTextMedia.js +187 -142
- package/SectionTextMedia/index.js +12 -7
- package/SectionTimeline/SectionTimeline.js +126 -95
- package/SectionTimeline/index.js +12 -7
- package/SectionWatch/SectionWatch.js +65 -53
- package/SectionWatch/index.js +12 -7
- package/Select/Select.js +517 -406
- package/Select/index.js +12 -7
- package/Share/Share.js +458 -319
- package/Share/index.js +12 -7
- package/Slide/Slide.js +184 -140
- package/Slide/index.js +12 -7
- package/Slider/Slider.js +927 -837
- package/Slider/index.js +12 -7
- package/SmartTextField/SmartTextField.js +1406 -1014
- package/SmartTextField/index.js +12 -7
- package/Snackbar/Snackbar.js +227 -185
- package/Snackbar/index.js +12 -7
- package/Snackbars/Context.js +9 -7
- package/Snackbars/Snackbars.js +234 -210
- package/Snackbars/index.js +32 -13
- package/Snackbars/useSnackbars.js +11 -10
- package/Space/Space.js +1683 -968
- package/Space/index.js +24 -21
- package/SpeechToText/SpeechToText.js +207 -172
- package/SpeechToText/index.js +12 -7
- package/SpeedDial/SpeedDial.js +434 -398
- package/SpeedDial/index.js +12 -7
- package/SpeedDialItem/SpeedDialItem.js +89 -63
- package/SpeedDialItem/index.js +12 -7
- package/SpyScroll/SpyScroll.js +154 -142
- package/SpyScroll/index.js +12 -7
- package/Step/Step.js +181 -127
- package/Step/index.js +12 -7
- package/Stepper/Stepper.js +159 -139
- package/Stepper/index.js +12 -7
- package/Surface/Surface.js +760 -798
- package/Surface/index.js +12 -7
- package/Switch/Switch.js +688 -532
- package/Switch/index.js +12 -7
- package/Tab/Tab.js +185 -150
- package/Tab/index.js +12 -7
- package/Table/Table.js +100 -77
- package/Table/index.js +12 -7
- package/TableBody/TableBody.js +76 -63
- package/TableBody/index.js +12 -7
- package/TableCell/TableCell.js +258 -220
- package/TableCell/index.js +12 -7
- package/TableFooter/TableFooter.js +60 -48
- package/TableFooter/index.js +12 -7
- package/TableHead/TableHead.js +139 -120
- package/TableHead/index.js +12 -7
- package/TableHeader/TableHeader.js +65 -50
- package/TableHeader/index.js +12 -7
- package/TablePagination/TablePagination.js +171 -98
- package/TablePagination/index.js +12 -7
- package/TableRow/TableRow.js +77 -64
- package/TableRow/index.js +12 -7
- package/Tabs/Tabs.js +425 -377
- package/Tabs/index.js +12 -7
- package/Text/Text.js +131 -113
- package/Text/index.js +12 -7
- package/TextField/TextField.js +940 -879
- package/TextField/index.js +12 -7
- package/TextToSpeech/TextToSpeech.js +176 -153
- package/TextToSpeech/index.js +12 -7
- package/TimePicker/TimePicker.js +985 -718
- package/TimePicker/index.js +12 -7
- package/TimeRangePicker/TimeRangePicker.js +44 -37
- package/TimeRangePicker/index.js +12 -7
- package/Timeline/Timeline.js +54 -42
- package/Timeline/index.js +12 -7
- package/TimelineItem/TimelineItem.js +144 -147
- package/TimelineItem/index.js +12 -7
- package/Timer/Timer.js +321 -204
- package/Timer/index.js +12 -7
- package/ToggleButton/ToggleButton.js +67 -58
- package/ToggleButton/index.js +12 -7
- package/ToggleButtons/ToggleButtons.js +46 -37
- package/ToggleButtons/index.js +12 -7
- package/Tooltip/Tooltip.js +489 -454
- package/Tooltip/index.js +12 -7
- package/TopAppBar/TopAppBar.js +237 -197
- package/TopAppBar/index.js +12 -7
- package/Transition/Context.js +9 -7
- package/Transition/Transition.js +321 -339
- package/Transition/index.js +42 -25
- package/Transition/useTransition.js +11 -10
- package/Transitions/Transitions.js +215 -179
- package/Transitions/index.js +12 -7
- package/Tree/Tree.js +375 -312
- package/Tree/index.js +12 -7
- package/Type/Type.js +251 -152
- package/Type/index.js +12 -7
- package/VideoPlayer/VideoPlayer.js +866 -656
- package/VideoPlayer/index.js +12 -7
- package/ViewSplit/ViewSplit.js +413 -387
- package/ViewSplit/index.js +12 -7
- package/Watch/Watch.js +436 -290
- package/Watch/index.js +12 -7
- package/Weather/Weather.js +294 -247
- package/Weather/index.js +12 -7
- package/Whiteboard/Whiteboard.js +1392 -1292
- package/Whiteboard/index.js +12 -7
- package/Widgets/Context.js +9 -7
- package/Widgets/Widgets.js +220 -168
- package/Widgets/index.js +32 -13
- package/Widgets/useWidgets.js +11 -10
- package/WindowSplit/WindowSplit.js +381 -336
- package/WindowSplit/index.js +12 -7
- package/Zoom/Zoom.js +126 -99
- package/Zoom/index.js +12 -7
- package/esm/Buttons/Buttons.js +1 -1
- package/esm/index.js +1 -1
- package/index.js +4053 -660
- package/package.json +1 -1
- package/types.js +4 -1
- package/useForm/index.js +25 -11
- package/useForm/useForm.js +203 -174
- package/useForm/validate.js +215 -203
- package/useLocation/index.js +12 -7
- package/useLocation/useLocation.js +54 -49
- package/useMediaQuery/index.js +12 -7
- package/useMediaQuery/useMediaQuery.js +44 -43
- package/useQuery/index.js +12 -7
- package/useQuery/useQuery.js +14 -10
- package/useScroll/index.js +12 -7
- package/useScroll/useScroll.js +61 -56
- package/useSubscription/index.js +12 -7
- package/useSubscription/useSubscription.js +35 -35
- package/useSwipe/index.js +12 -7
- package/useSwipe/useSwipe.js +157 -131
- package/useVisible/index.js +12 -7
- package/useVisible/useVisible.js +86 -76
- package/utils.js +1224 -1362
package/Whiteboard/Whiteboard.js
CHANGED
|
@@ -1,1334 +1,1434 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
var
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
const
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
const
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
transition: theme.methods.transitions.make('opacity'),
|
|
39
|
-
'&[disabled]': {
|
|
40
|
-
opacity: 0.7,
|
|
41
|
-
pointerEvents: 'none'
|
|
42
|
-
}
|
|
43
|
-
},
|
|
44
|
-
ui: {
|
|
45
|
-
zIndex: 0
|
|
46
|
-
},
|
|
47
|
-
object: {
|
|
48
|
-
cursor: 'crosshair'
|
|
49
|
-
},
|
|
50
|
-
pen: {
|
|
51
|
-
cursor: 'crosshair'
|
|
52
|
-
},
|
|
53
|
-
pan: {
|
|
54
|
-
cursor: 'grab'
|
|
55
|
-
},
|
|
56
|
-
panning: {
|
|
57
|
-
cursor: 'grabbing'
|
|
58
|
-
},
|
|
59
|
-
zoom: {
|
|
60
|
-
cursor: 'zoom-in'
|
|
61
|
-
},
|
|
62
|
-
eraser: {
|
|
63
|
-
cursor: 'not-allowed'
|
|
64
|
-
},
|
|
65
|
-
image: {
|
|
66
|
-
cursor: 'copy'
|
|
67
|
-
},
|
|
68
|
-
text: {
|
|
69
|
-
cursor: 'text'
|
|
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 _date = require("@onesy/date");
|
|
15
|
+
var _Line = _interopRequireDefault(require("../Line"));
|
|
16
|
+
var _utils2 = require("../utils");
|
|
17
|
+
const _excluded = ["valueDefault", "onChange", "minZoom", "maxZoom", "grid", "settings", "className"];
|
|
18
|
+
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; }
|
|
19
|
+
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; }
|
|
20
|
+
const useStyle = (0, _styleReact.style)(theme => ({
|
|
21
|
+
root: {
|
|
22
|
+
position: 'relative'
|
|
23
|
+
},
|
|
24
|
+
canvas: {
|
|
25
|
+
position: 'absolute',
|
|
26
|
+
inset: 0,
|
|
27
|
+
width: '100%',
|
|
28
|
+
height: '100%',
|
|
29
|
+
imageRendering: 'pixelated',
|
|
30
|
+
background: '#fff',
|
|
31
|
+
appearance: 'none',
|
|
32
|
+
border: 'none',
|
|
33
|
+
userSelect: 'none',
|
|
34
|
+
transition: theme.methods.transitions.make('opacity'),
|
|
35
|
+
'&[disabled]': {
|
|
36
|
+
opacity: 0.7,
|
|
37
|
+
pointerEvents: 'none'
|
|
70
38
|
}
|
|
71
|
-
}
|
|
39
|
+
},
|
|
40
|
+
ui: {
|
|
41
|
+
zIndex: 0
|
|
42
|
+
},
|
|
43
|
+
object: {
|
|
44
|
+
cursor: 'crosshair'
|
|
45
|
+
},
|
|
46
|
+
pen: {
|
|
47
|
+
cursor: 'crosshair'
|
|
48
|
+
},
|
|
49
|
+
pan: {
|
|
50
|
+
cursor: 'grab'
|
|
51
|
+
},
|
|
52
|
+
panning: {
|
|
53
|
+
cursor: 'grabbing'
|
|
54
|
+
},
|
|
55
|
+
zoom: {
|
|
56
|
+
cursor: 'zoom-in'
|
|
57
|
+
},
|
|
58
|
+
eraser: {
|
|
59
|
+
cursor: 'not-allowed'
|
|
60
|
+
},
|
|
61
|
+
image: {
|
|
62
|
+
cursor: 'copy'
|
|
63
|
+
},
|
|
64
|
+
text: {
|
|
65
|
+
cursor: 'text'
|
|
66
|
+
}
|
|
67
|
+
}), {
|
|
68
|
+
name: 'onesy-Whiteboard'
|
|
69
|
+
});
|
|
72
70
|
const colorSelect = 'hsl(244deg 64% 64%)';
|
|
73
71
|
const colorSelectBackground = 'hsla(244deg 64% 64% / 4%)';
|
|
74
|
-
const Whiteboard =
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
72
|
+
const Whiteboard = /*#__PURE__*/_react.default.forwardRef((props_, ref) => {
|
|
73
|
+
const theme = (0, _styleReact.useOnesyTheme)();
|
|
74
|
+
const props = _react.default.useMemo(() => {
|
|
75
|
+
var _theme$ui, _theme$ui2;
|
|
76
|
+
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.onesyWhiteboard) === null || _theme$ui2 === void 0 || (_theme$ui2 = _theme$ui2.props) === null || _theme$ui2 === void 0 ? void 0 : _theme$ui2.default), props_);
|
|
77
|
+
}, [props_]);
|
|
78
|
+
const Line = _react.default.useMemo(() => {
|
|
79
|
+
var _theme$elements;
|
|
80
|
+
return (theme === null || theme === void 0 || (_theme$elements = theme.elements) === null || _theme$elements === void 0 ? void 0 : _theme$elements.Line) || _Line.default;
|
|
81
|
+
}, [theme]);
|
|
82
|
+
const {
|
|
83
|
+
valueDefault,
|
|
84
|
+
onChange: onChangeProps,
|
|
85
|
+
// 10%
|
|
86
|
+
minZoom = 10,
|
|
87
|
+
// 400%
|
|
88
|
+
maxZoom = 4000,
|
|
89
|
+
grid: gridProps = true,
|
|
90
|
+
settings = {
|
|
83
91
|
lineCap: 'round',
|
|
84
92
|
lineJoin: 'round',
|
|
85
93
|
lineWidth: 10,
|
|
86
94
|
fillStyle: 'lightgreen',
|
|
87
95
|
strokeStyle: 'lightgreen',
|
|
88
96
|
globalAlpha: 0.44
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
97
|
+
},
|
|
98
|
+
className
|
|
99
|
+
} = props,
|
|
100
|
+
other = (0, _objectWithoutProperties2.default)(props, _excluded);
|
|
101
|
+
const {
|
|
102
|
+
classes
|
|
103
|
+
} = useStyle();
|
|
104
|
+
const [size, setSize] = _react.default.useState({});
|
|
105
|
+
const [tool, setTool] = _react.default.useState('select');
|
|
106
|
+
const [mouseDown, setMouseDown] = _react.default.useState(false);
|
|
107
|
+
const [grid, setGrid] = _react.default.useState(gridProps);
|
|
108
|
+
const [loaded, setLoaded] = _react.default.useState(false);
|
|
109
|
+
const refs = {
|
|
110
|
+
root: _react.default.useRef(null),
|
|
111
|
+
ui: _react.default.useRef(null),
|
|
112
|
+
interactive: _react.default.useRef(null),
|
|
113
|
+
on: _react.default.useRef(false),
|
|
114
|
+
items: _react.default.useRef(valueDefault || []),
|
|
115
|
+
previous: _react.default.useRef({
|
|
116
|
+
x: 0,
|
|
117
|
+
y: 0
|
|
118
|
+
}),
|
|
119
|
+
previousMouse: _react.default.useRef({
|
|
120
|
+
x: 0,
|
|
121
|
+
y: 0
|
|
122
|
+
}),
|
|
123
|
+
moveStarted: _react.default.useRef(false),
|
|
124
|
+
undo: _react.default.useRef([]),
|
|
125
|
+
redo: _react.default.useRef([]),
|
|
126
|
+
move: _react.default.useRef({
|
|
127
|
+
x: 0,
|
|
128
|
+
y: 0
|
|
129
|
+
}),
|
|
130
|
+
offset: _react.default.useRef({
|
|
131
|
+
x: 0,
|
|
132
|
+
y: 0
|
|
133
|
+
}),
|
|
134
|
+
start: _react.default.useRef({
|
|
135
|
+
x: 0,
|
|
136
|
+
y: 0
|
|
137
|
+
}),
|
|
138
|
+
end: _react.default.useRef({
|
|
139
|
+
x: 0,
|
|
140
|
+
y: 0
|
|
141
|
+
}),
|
|
142
|
+
scale: _react.default.useRef(1),
|
|
143
|
+
mouseDown: _react.default.useRef(mouseDown),
|
|
144
|
+
mouseMove: _react.default.useRef({
|
|
145
|
+
current: {
|
|
146
|
+
x: 0,
|
|
147
|
+
y: 0
|
|
148
|
+
},
|
|
149
|
+
previous: undefined,
|
|
150
|
+
delta: {
|
|
151
|
+
x: 0,
|
|
152
|
+
y: 0
|
|
153
|
+
}
|
|
154
|
+
}),
|
|
155
|
+
tool: _react.default.useRef(tool),
|
|
156
|
+
previousTool: _react.default.useRef(tool),
|
|
157
|
+
toolUpdateAuto: _react.default.useRef(false),
|
|
158
|
+
remove: _react.default.useRef([]),
|
|
159
|
+
grid: _react.default.useRef(grid),
|
|
160
|
+
typing: _react.default.useRef(false),
|
|
161
|
+
image: _react.default.useRef((0, _utils.isEnvironment)('browser') && new Image()),
|
|
162
|
+
aspectRatio: _react.default.useRef(1),
|
|
163
|
+
select: _react.default.useRef(null),
|
|
164
|
+
textActive: _react.default.useRef(null),
|
|
165
|
+
textSettings: _react.default.useRef({
|
|
166
|
+
lineHeight: 20,
|
|
167
|
+
padding: 5,
|
|
168
|
+
fillStyle: 'black'
|
|
169
|
+
})
|
|
170
|
+
};
|
|
171
|
+
refs.mouseDown.current = mouseDown;
|
|
172
|
+
refs.tool.current = tool;
|
|
173
|
+
refs.grid.current = grid;
|
|
174
|
+
const init = _react.default.useCallback(() => {
|
|
175
|
+
// Todo
|
|
176
|
+
// items
|
|
177
|
+
// load all of the images in memory and attach theme to items as image elements
|
|
178
|
+
// once it's all done, setLoaded(true), render
|
|
179
|
+
|
|
180
|
+
setTimeout(() => {
|
|
181
|
+
render();
|
|
182
|
+
setLoaded(true);
|
|
183
|
+
}, 40);
|
|
184
|
+
}, []);
|
|
185
|
+
_react.default.useEffect(() => {
|
|
186
|
+
if (!['zoom'].includes(tool)) refs.previousTool.current = tool;
|
|
187
|
+
}, [tool]);
|
|
188
|
+
const onChange = _react.default.useCallback(() => {
|
|
189
|
+
if ((0, _utils.is)('function', onChangeProps)) onChangeProps(refs.items.current);
|
|
190
|
+
}, [onChangeProps]);
|
|
191
|
+
const getItems = _react.default.useCallback((selected = undefined) => refs.items.current.filter(item => selected === undefined || item.se === selected), []);
|
|
192
|
+
const getItem = _react.default.useCallback(() => refs.items.current[refs.items.current.length - 1], []);
|
|
193
|
+
const filterItems = _react.default.useCallback(() => {
|
|
194
|
+
const toRemove = refs.items.current.filter(item => {
|
|
195
|
+
var _item$s;
|
|
196
|
+
if (refs.tool.current === 'text' && item !== refs.textActive.current) item.se = false;
|
|
197
|
+
const lines = ((_item$s = item.s) === null || _item$s === void 0 ? void 0 : _item$s.lines) || [];
|
|
198
|
+
return !(item.v !== 't' || item === refs.textActive.current || lines.length > 1 || lines[0].length);
|
|
199
|
+
});
|
|
200
|
+
if (toRemove.length) remove(toRemove);
|
|
201
|
+
}, []);
|
|
202
|
+
const add = _react.default.useCallback(toAdd => {
|
|
203
|
+
const itemsAdd = (Array.isArray(toAdd) ? toAdd : [toAdd]).filter(Boolean);
|
|
204
|
+
const items = getItems();
|
|
205
|
+
|
|
206
|
+
// add to undo stack snapshot of current state
|
|
207
|
+
refs.undo.current.push([...items]);
|
|
208
|
+
|
|
209
|
+
// clear redo stack
|
|
210
|
+
refs.redo.current = [];
|
|
211
|
+
refs.items.current.push(...itemsAdd);
|
|
212
|
+
}, []);
|
|
213
|
+
const remove = _react.default.useCallback(toRemove => {
|
|
214
|
+
const itemsRemove = (Array.isArray(toRemove) ? toRemove : [toRemove]).filter(Boolean);
|
|
215
|
+
const items = getItems();
|
|
216
|
+
const IDs = itemsRemove === null || itemsRemove === void 0 ? void 0 : itemsRemove.map(item => item.i);
|
|
217
|
+
const toRemoveIDs = items.filter(item => IDs.includes(item.i)).map(item => item.i);
|
|
218
|
+
if (!toRemoveIDs) return;
|
|
219
|
+
|
|
220
|
+
// add to undo stack snapshot of current state
|
|
221
|
+
refs.undo.current.push([...items]);
|
|
222
|
+
|
|
223
|
+
// clear redo stack
|
|
224
|
+
refs.redo.current = [];
|
|
225
|
+
refs.items.current = refs.items.current.filter(item => !toRemoveIDs.includes(item.i));
|
|
226
|
+
}, []);
|
|
227
|
+
const reset = _react.default.useCallback(() => {
|
|
228
|
+
refs.on.current = false;
|
|
229
|
+
refs.moveStarted.current = false;
|
|
230
|
+
refs.image.current = null;
|
|
231
|
+
|
|
232
|
+
// new move start
|
|
233
|
+
refs.offset.current.x = refs.move.current.x;
|
|
234
|
+
refs.offset.current.y = refs.move.current.y;
|
|
235
|
+
}, []);
|
|
236
|
+
const transform = _react.default.useCallback(coordinate => coordinate / refs.scale.current, []);
|
|
237
|
+
const selectAll = _react.default.useCallback(() => {
|
|
238
|
+
return [...refs.items.current].filter(Boolean).map(item => {
|
|
239
|
+
item.se = true;
|
|
240
|
+
return item;
|
|
241
|
+
});
|
|
242
|
+
}, []);
|
|
243
|
+
const unselectAll = _react.default.useCallback(eventReact => {
|
|
244
|
+
const event = (eventReact === null || eventReact === void 0 ? void 0 : eventReact.nativeEvent) || eventReact;
|
|
245
|
+
const shift = event === null || event === void 0 ? void 0 : event.shiftKey;
|
|
246
|
+
return [...refs.items.current].filter(Boolean).map(item => {
|
|
247
|
+
if (shift) return item;
|
|
248
|
+
item.se = false;
|
|
249
|
+
return item;
|
|
250
|
+
});
|
|
251
|
+
}, []);
|
|
252
|
+
const onInteractionDown = _react.default.useCallback((body, eventReact) => {
|
|
253
|
+
const event = (eventReact === null || eventReact === void 0 ? void 0 : eventReact.nativeEvent) || eventReact;
|
|
254
|
+
const {
|
|
255
|
+
offsetX: x,
|
|
256
|
+
offsetY: y,
|
|
257
|
+
clientX,
|
|
258
|
+
clientY
|
|
259
|
+
} = body;
|
|
260
|
+
refs.on.current = true;
|
|
261
|
+
refs.previous.current = {
|
|
262
|
+
x,
|
|
263
|
+
y
|
|
133
264
|
};
|
|
134
|
-
refs.
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
const
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
refs.redo.current = [];
|
|
175
|
-
refs.items.current.push(...itemsAdd);
|
|
176
|
-
}, []);
|
|
177
|
-
const remove = react_1.default.useCallback((toRemove) => {
|
|
178
|
-
const itemsRemove = (Array.isArray(toRemove) ? toRemove : [toRemove]).filter(Boolean);
|
|
179
|
-
const items = getItems();
|
|
180
|
-
const IDs = itemsRemove === null || itemsRemove === void 0 ? void 0 : itemsRemove.map(item => item.i);
|
|
181
|
-
const toRemoveIDs = items.filter(item => IDs.includes(item.i)).map(item => item.i);
|
|
182
|
-
if (!toRemoveIDs)
|
|
183
|
-
return;
|
|
184
|
-
// add to undo stack snapshot of current state
|
|
185
|
-
refs.undo.current.push([...items]);
|
|
186
|
-
// clear redo stack
|
|
187
|
-
refs.redo.current = [];
|
|
188
|
-
refs.items.current = refs.items.current.filter(item => !toRemoveIDs.includes(item.i));
|
|
189
|
-
}, []);
|
|
190
|
-
const reset = react_1.default.useCallback(() => {
|
|
191
|
-
refs.on.current = false;
|
|
192
|
-
refs.moveStarted.current = false;
|
|
193
|
-
refs.image.current = null;
|
|
194
|
-
// new move start
|
|
195
|
-
refs.offset.current.x = refs.move.current.x;
|
|
196
|
-
refs.offset.current.y = refs.move.current.y;
|
|
197
|
-
}, []);
|
|
198
|
-
const transform = react_1.default.useCallback((coordinate) => coordinate / refs.scale.current, []);
|
|
199
|
-
const selectAll = react_1.default.useCallback(() => {
|
|
200
|
-
return [...refs.items.current].filter(Boolean).map(item => {
|
|
201
|
-
item.se = true;
|
|
202
|
-
return item;
|
|
203
|
-
});
|
|
204
|
-
}, []);
|
|
205
|
-
const unselectAll = react_1.default.useCallback((eventReact) => {
|
|
206
|
-
const event = (eventReact === null || eventReact === void 0 ? void 0 : eventReact.nativeEvent) || eventReact;
|
|
207
|
-
const shift = event === null || event === void 0 ? void 0 : event.shiftKey;
|
|
208
|
-
return [...refs.items.current].filter(Boolean).map(item => {
|
|
209
|
-
if (shift)
|
|
210
|
-
return item;
|
|
211
|
-
item.se = false;
|
|
212
|
-
return item;
|
|
213
|
-
});
|
|
214
|
-
}, []);
|
|
215
|
-
const onInteractionDown = react_1.default.useCallback((body, eventReact) => {
|
|
216
|
-
var _a, _b;
|
|
217
|
-
const event = (eventReact === null || eventReact === void 0 ? void 0 : eventReact.nativeEvent) || eventReact;
|
|
218
|
-
const { offsetX: x, offsetY: y, clientX, clientY } = body;
|
|
219
|
-
refs.on.current = true;
|
|
220
|
-
refs.previous.current = { x, y };
|
|
221
|
-
refs.mouseMove.current = {
|
|
222
|
-
current: { x: 0, y: 0 },
|
|
223
|
-
previous: undefined,
|
|
224
|
-
delta: { x: 0, y: 0 }
|
|
225
|
-
};
|
|
226
|
-
const shift = event === null || event === void 0 ? void 0 : event.shiftKey;
|
|
227
|
-
const ui = refs.ui.current.getContext('2d');
|
|
228
|
-
const rect = refs.ui.current.getBoundingClientRect();
|
|
229
|
-
refs.start.current.x = clientX - rect.left;
|
|
230
|
-
refs.start.current.y = clientY - rect.top;
|
|
231
|
-
refs.textActive.current = null;
|
|
232
|
-
const start = refs.start.current;
|
|
233
|
-
const startTransformed = {
|
|
234
|
-
x: transform(start.x - refs.move.current.x),
|
|
235
|
-
y: transform(start.y - refs.move.current.y)
|
|
265
|
+
refs.mouseMove.current = {
|
|
266
|
+
current: {
|
|
267
|
+
x: 0,
|
|
268
|
+
y: 0
|
|
269
|
+
},
|
|
270
|
+
previous: undefined,
|
|
271
|
+
delta: {
|
|
272
|
+
x: 0,
|
|
273
|
+
y: 0
|
|
274
|
+
}
|
|
275
|
+
};
|
|
276
|
+
const shift = event === null || event === void 0 ? void 0 : event.shiftKey;
|
|
277
|
+
const ui = refs.ui.current.getContext('2d');
|
|
278
|
+
const rect = refs.ui.current.getBoundingClientRect();
|
|
279
|
+
refs.start.current.x = clientX - rect.left;
|
|
280
|
+
refs.start.current.y = clientY - rect.top;
|
|
281
|
+
refs.textActive.current = null;
|
|
282
|
+
const start = refs.start.current;
|
|
283
|
+
const startTransformed = {
|
|
284
|
+
x: transform(start.x - refs.move.current.x),
|
|
285
|
+
y: transform(start.y - refs.move.current.y)
|
|
286
|
+
};
|
|
287
|
+
Object.keys(settings).forEach(item_ => ui[item_] = settings[item_]);
|
|
288
|
+
let item;
|
|
289
|
+
const items = getItems();
|
|
290
|
+
const t = refs.tool.current;
|
|
291
|
+
refs.select.current = null;
|
|
292
|
+
if (t === 'select') {
|
|
293
|
+
// z-index top to bottom order
|
|
294
|
+
const itemsReversed = [...items].reverse();
|
|
295
|
+
const itemsClicked = itemsReversed.filter(item_ => {
|
|
296
|
+
return item_.c && startTransformed.x >= item_.c[0] && startTransformed.x <= item_.c[0] + item_.c[2] && startTransformed.y >= item_.c[1] && startTransformed.y <= item_.c[1] + item_.c[3];
|
|
297
|
+
});
|
|
298
|
+
const itemsSelected = getItems(true);
|
|
299
|
+
const clicked = itemsClicked[0];
|
|
300
|
+
if (!clicked) {
|
|
301
|
+
unselectAll();
|
|
302
|
+
refs.select.current = {
|
|
303
|
+
p: [],
|
|
304
|
+
ar: []
|
|
236
305
|
};
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
const selectedText = items.find(item_ => item_.c && startTransformed.x >= item_.c[0] && startTransformed.x <= item_.c[0] + item_.c[2] && startTransformed.y >= item_.c[1] && startTransformed.y <= item_.c[1] + item_.c[3]);
|
|
263
|
-
if (selectedText) {
|
|
264
|
-
refs.textActive.current = selectedText;
|
|
265
|
-
selectedText.se = true;
|
|
266
|
-
const relativeY = startTransformed.y - selectedText.c[1] - padding;
|
|
267
|
-
const lineIndex = Math.floor(relativeY / lineHeight);
|
|
268
|
-
const clickedLine = ((_b = (_a = selectedText.s) === null || _a === void 0 ? void 0 : _a.lines) === null || _b === void 0 ? void 0 : _b[lineIndex]) || '';
|
|
269
|
-
const relativeX = startTransformed.x - selectedText.c[0] - padding;
|
|
270
|
-
let charIndex = clickedLine.length;
|
|
271
|
-
for (let i = 0; i < clickedLine.length; i++) {
|
|
272
|
-
if (relativeX < ui.measureText(clickedLine.slice(0, i + 1)).width) {
|
|
273
|
-
charIndex = i;
|
|
274
|
-
break;
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
selectedText.s.cursor = { line: lineIndex, char: charIndex };
|
|
278
|
-
}
|
|
279
|
-
else {
|
|
280
|
-
item = {
|
|
281
|
-
i: (0, utils_1.getID)(),
|
|
282
|
-
v: 't',
|
|
283
|
-
p: [startTransformed.x, startTransformed.y],
|
|
284
|
-
ar: [15, lineHeight + (padding * 2)],
|
|
285
|
-
s: Object.assign(Object.assign({}, refs.textSettings.current), { lines: [''], cursor: { line: 0, char: 0 } }),
|
|
286
|
-
se: true,
|
|
287
|
-
a: date_1.OnesyDate.milliseconds
|
|
288
|
-
};
|
|
289
|
-
refs.textActive.current = item;
|
|
290
|
-
}
|
|
291
|
-
}
|
|
292
|
-
else {
|
|
293
|
-
// pen
|
|
294
|
-
if (t === 'pen') {
|
|
295
|
-
// point
|
|
296
|
-
item = {
|
|
297
|
-
i: (0, utils_1.getID)(),
|
|
298
|
-
v: 'dp',
|
|
299
|
-
p: [transform(x - refs.move.current.x), transform(y - refs.move.current.y)],
|
|
300
|
-
ar: [ui.lineWidth / 2, 0, Math.PI * 2],
|
|
301
|
-
s: (0, utils_1.copy)(settings),
|
|
302
|
-
a: date_1.OnesyDate.milliseconds
|
|
303
|
-
};
|
|
304
|
-
}
|
|
305
|
-
// circle, rectangle, line, line-arrow
|
|
306
|
-
if (['circle', 'rectangle', 'triangle', 'line', 'line-arrow'].includes(t)) {
|
|
307
|
-
item = {
|
|
308
|
-
i: (0, utils_1.getID)(),
|
|
309
|
-
p: [],
|
|
310
|
-
ar: [],
|
|
311
|
-
s: (0, utils_1.copy)(settings),
|
|
312
|
-
a: date_1.OnesyDate.milliseconds
|
|
313
|
-
};
|
|
314
|
-
}
|
|
315
|
-
// pan
|
|
316
|
-
if (t === 'pan') {
|
|
317
|
-
// new move start
|
|
318
|
-
refs.offset.current.x = refs.move.current.x;
|
|
319
|
-
refs.offset.current.y = refs.move.current.y;
|
|
320
|
-
}
|
|
321
|
-
// image
|
|
322
|
-
else if (t === 'image' && refs.image.current.complete && refs.image.current.src) {
|
|
323
|
-
refs.aspectRatio.current = refs.image.current.width / refs.image.current.height;
|
|
324
|
-
// Todo
|
|
325
|
-
// add url of the image
|
|
326
|
-
// instead of embeding the image
|
|
327
|
-
item = {
|
|
328
|
-
i: (0, utils_1.getID)(),
|
|
329
|
-
v: 'i',
|
|
330
|
-
p: [],
|
|
331
|
-
ar: [],
|
|
332
|
-
s: {
|
|
333
|
-
// Todo
|
|
334
|
-
// remove in the future
|
|
335
|
-
image: refs.image.current,
|
|
336
|
-
aspectRatio: refs.aspectRatio.current
|
|
337
|
-
},
|
|
338
|
-
a: date_1.OnesyDate.milliseconds
|
|
339
|
-
};
|
|
340
|
-
}
|
|
341
|
-
}
|
|
342
|
-
if (item)
|
|
343
|
-
add(item);
|
|
344
|
-
filterItems();
|
|
345
|
-
// render
|
|
346
|
-
render();
|
|
347
|
-
setMouseDown(true);
|
|
348
|
-
}, []);
|
|
349
|
-
const onMouseDown = react_1.default.useCallback((event) => {
|
|
350
|
-
const { offsetX, offsetY, clientX, clientY } = event.nativeEvent;
|
|
351
|
-
onInteractionDown({ offsetX, offsetY, clientX, clientY }, event);
|
|
352
|
-
}, [onInteractionDown]);
|
|
353
|
-
const onTouchStart = react_1.default.useCallback((event) => {
|
|
354
|
-
// Get the first touch point
|
|
355
|
-
const touch = event.touches[0];
|
|
356
|
-
const { clientX, clientY } = touch;
|
|
357
|
-
let { offsetX, offsetY } = touch;
|
|
358
|
-
const targetElement = touch.target;
|
|
359
|
-
if (targetElement instanceof HTMLElement) {
|
|
360
|
-
// Get the bounding rectangle of the target element
|
|
361
|
-
const rect = targetElement.getBoundingClientRect();
|
|
362
|
-
// Calculate the offsetX and offsetY
|
|
363
|
-
offsetX = touch.clientX - rect.left;
|
|
364
|
-
offsetY = touch.clientY - rect.top;
|
|
306
|
+
}
|
|
307
|
+
if (!shift && !(clicked !== null && clicked !== void 0 && clicked.se && itemsSelected.length > 1)) unselectAll();
|
|
308
|
+
if (clicked) {
|
|
309
|
+
clicked.se = shift ? !clicked.se : true;
|
|
310
|
+
}
|
|
311
|
+
} else if (t === 'text') {
|
|
312
|
+
const {
|
|
313
|
+
lineHeight,
|
|
314
|
+
padding
|
|
315
|
+
} = refs.textSettings.current;
|
|
316
|
+
const selectedText = items.find(item_ => item_.c && startTransformed.x >= item_.c[0] && startTransformed.x <= item_.c[0] + item_.c[2] && startTransformed.y >= item_.c[1] && startTransformed.y <= item_.c[1] + item_.c[3]);
|
|
317
|
+
if (selectedText) {
|
|
318
|
+
var _selectedText$s;
|
|
319
|
+
refs.textActive.current = selectedText;
|
|
320
|
+
selectedText.se = true;
|
|
321
|
+
const relativeY = startTransformed.y - selectedText.c[1] - padding;
|
|
322
|
+
const lineIndex = Math.floor(relativeY / lineHeight);
|
|
323
|
+
const clickedLine = ((_selectedText$s = selectedText.s) === null || _selectedText$s === void 0 || (_selectedText$s = _selectedText$s.lines) === null || _selectedText$s === void 0 ? void 0 : _selectedText$s[lineIndex]) || '';
|
|
324
|
+
const relativeX = startTransformed.x - selectedText.c[0] - padding;
|
|
325
|
+
let charIndex = clickedLine.length;
|
|
326
|
+
for (let i = 0; i < clickedLine.length; i++) {
|
|
327
|
+
if (relativeX < ui.measureText(clickedLine.slice(0, i + 1)).width) {
|
|
328
|
+
charIndex = i;
|
|
329
|
+
break;
|
|
330
|
+
}
|
|
365
331
|
}
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
}, []);
|
|
382
|
-
const onUpdateCoordinates = react_1.default.useCallback(() => {
|
|
383
|
-
const items = getItems();
|
|
384
|
-
items.forEach(item => {
|
|
385
|
-
const p = (item === null || item === void 0 ? void 0 : item.p) || [];
|
|
386
|
-
const ar = (item === null || item === void 0 ? void 0 : item.ar) || [];
|
|
387
|
-
const s = (item === null || item === void 0 ? void 0 : item.s) || {};
|
|
388
|
-
if (p.length) {
|
|
389
|
-
// cache
|
|
390
|
-
// x1, y1, width. height for position on the screen
|
|
391
|
-
const v = item.v;
|
|
392
|
-
// draw point
|
|
393
|
-
if (v === 'dp') {
|
|
394
|
-
const lineWidth = s.lineWidth || 10;
|
|
395
|
-
item.c = [
|
|
396
|
-
p[0] - (lineWidth / 2),
|
|
397
|
-
p[1] - (lineWidth / 2),
|
|
398
|
-
lineWidth,
|
|
399
|
-
lineWidth
|
|
400
|
-
];
|
|
401
|
-
}
|
|
402
|
-
// draw line
|
|
403
|
-
else if (v === 'dl') {
|
|
404
|
-
const x = [];
|
|
405
|
-
const y = [];
|
|
406
|
-
for (let i = 0; i < p.length; i += 2) {
|
|
407
|
-
x.push(p[i]);
|
|
408
|
-
y.push(p[i + 1]);
|
|
409
|
-
}
|
|
410
|
-
const xMin = Math.min(...x);
|
|
411
|
-
const yMin = Math.min(...y);
|
|
412
|
-
const xMax = Math.max(...x);
|
|
413
|
-
const yMax = Math.max(...y);
|
|
414
|
-
item.c = [
|
|
415
|
-
xMin,
|
|
416
|
-
yMin,
|
|
417
|
-
xMax - xMin,
|
|
418
|
-
yMax - yMin
|
|
419
|
-
];
|
|
420
|
-
}
|
|
421
|
-
// object line, object arrow
|
|
422
|
-
else if (['ol', 'oa'].includes(v)) {
|
|
423
|
-
const x = [p[0], ar[0]];
|
|
424
|
-
const y = [p[1], ar[1]];
|
|
425
|
-
const xMin = Math.min(...x);
|
|
426
|
-
const yMin = Math.min(...y);
|
|
427
|
-
const xMax = Math.max(...x);
|
|
428
|
-
const yMax = Math.max(...y);
|
|
429
|
-
item.c = [
|
|
430
|
-
xMin,
|
|
431
|
-
yMin,
|
|
432
|
-
xMax - xMin,
|
|
433
|
-
yMax - yMin
|
|
434
|
-
];
|
|
435
|
-
}
|
|
436
|
-
// object rectangle, object square
|
|
437
|
-
else if (['or', 'os'].includes(v)) {
|
|
438
|
-
item.c = [
|
|
439
|
-
...p,
|
|
440
|
-
...ar
|
|
441
|
-
];
|
|
442
|
-
}
|
|
443
|
-
// object circle, object ellipse
|
|
444
|
-
else if (['oc', 'oe'].includes(v)) {
|
|
445
|
-
if (v === 'oc') {
|
|
446
|
-
item.c = [
|
|
447
|
-
p[0] - ar[0],
|
|
448
|
-
p[1] - ar[0],
|
|
449
|
-
ar[0] * 2,
|
|
450
|
-
ar[0] * 2
|
|
451
|
-
];
|
|
452
|
-
}
|
|
453
|
-
else {
|
|
454
|
-
item.c = [
|
|
455
|
-
p[0] - ar[0],
|
|
456
|
-
p[1] - ar[1],
|
|
457
|
-
ar[0] * 2,
|
|
458
|
-
ar[1] * 2
|
|
459
|
-
];
|
|
460
|
-
}
|
|
461
|
-
}
|
|
462
|
-
// object triangle, object triangle equilateral
|
|
463
|
-
else if (['ot', 'ote'].includes(v)) {
|
|
464
|
-
const [x1, y1, x2] = p;
|
|
465
|
-
const { height } = s;
|
|
466
|
-
item.c = [
|
|
467
|
-
x1,
|
|
468
|
-
y1 - height,
|
|
469
|
-
x2 - x1,
|
|
470
|
-
height
|
|
471
|
-
];
|
|
472
|
-
}
|
|
473
|
-
// image
|
|
474
|
-
else if (['i', 't'].includes(v)) {
|
|
475
|
-
item.c = [
|
|
476
|
-
...p,
|
|
477
|
-
...ar
|
|
478
|
-
];
|
|
479
|
-
}
|
|
332
|
+
selectedText.s.cursor = {
|
|
333
|
+
line: lineIndex,
|
|
334
|
+
char: charIndex
|
|
335
|
+
};
|
|
336
|
+
} else {
|
|
337
|
+
item = {
|
|
338
|
+
i: (0, _utils.getID)(),
|
|
339
|
+
v: 't',
|
|
340
|
+
p: [startTransformed.x, startTransformed.y],
|
|
341
|
+
ar: [15, lineHeight + padding * 2],
|
|
342
|
+
s: _objectSpread(_objectSpread({}, refs.textSettings.current), {}, {
|
|
343
|
+
lines: [''],
|
|
344
|
+
cursor: {
|
|
345
|
+
line: 0,
|
|
346
|
+
char: 0
|
|
480
347
|
}
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
const select = refs.select.current;
|
|
485
|
-
if (!select)
|
|
486
|
-
return;
|
|
487
|
-
const px1 = select.p[0];
|
|
488
|
-
const px2 = px1 + select.ar[0];
|
|
489
|
-
const py1 = select.p[1];
|
|
490
|
-
const py2 = py1 + select.ar[1];
|
|
491
|
-
const min = {
|
|
492
|
-
x: Math.min(px1, px2),
|
|
493
|
-
y: Math.min(py1, py2)
|
|
348
|
+
}),
|
|
349
|
+
se: true,
|
|
350
|
+
a: _date.OnesyDate.milliseconds
|
|
494
351
|
};
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
352
|
+
refs.textActive.current = item;
|
|
353
|
+
}
|
|
354
|
+
} else {
|
|
355
|
+
// pen
|
|
356
|
+
if (t === 'pen') {
|
|
357
|
+
// point
|
|
358
|
+
item = {
|
|
359
|
+
i: (0, _utils.getID)(),
|
|
360
|
+
v: 'dp',
|
|
361
|
+
p: [transform(x - refs.move.current.x), transform(y - refs.move.current.y)],
|
|
362
|
+
ar: [ui.lineWidth / 2, 0, Math.PI * 2],
|
|
363
|
+
s: (0, _utils.copy)(settings),
|
|
364
|
+
a: _date.OnesyDate.milliseconds
|
|
498
365
|
};
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
//
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
366
|
+
}
|
|
367
|
+
// circle, rectangle, line, line-arrow
|
|
368
|
+
if (['circle', 'rectangle', 'triangle', 'line', 'line-arrow'].includes(t)) {
|
|
369
|
+
item = {
|
|
370
|
+
i: (0, _utils.getID)(),
|
|
371
|
+
p: [],
|
|
372
|
+
ar: [],
|
|
373
|
+
s: (0, _utils.copy)(settings),
|
|
374
|
+
a: _date.OnesyDate.milliseconds
|
|
375
|
+
};
|
|
376
|
+
}
|
|
377
|
+
// pan
|
|
378
|
+
if (t === 'pan') {
|
|
379
|
+
// new move start
|
|
380
|
+
refs.offset.current.x = refs.move.current.x;
|
|
381
|
+
refs.offset.current.y = refs.move.current.y;
|
|
382
|
+
}
|
|
383
|
+
// image
|
|
384
|
+
else if (t === 'image' && refs.image.current.complete && refs.image.current.src) {
|
|
385
|
+
refs.aspectRatio.current = refs.image.current.width / refs.image.current.height;
|
|
386
|
+
|
|
387
|
+
// Todo
|
|
388
|
+
// add url of the image
|
|
389
|
+
// instead of embeding the image
|
|
390
|
+
item = {
|
|
391
|
+
i: (0, _utils.getID)(),
|
|
392
|
+
v: 'i',
|
|
393
|
+
p: [],
|
|
394
|
+
ar: [],
|
|
395
|
+
s: {
|
|
396
|
+
// Todo
|
|
397
|
+
// remove in the future
|
|
398
|
+
image: refs.image.current,
|
|
399
|
+
aspectRatio: refs.aspectRatio.current
|
|
400
|
+
},
|
|
401
|
+
a: _date.OnesyDate.milliseconds
|
|
402
|
+
};
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
if (item) add(item);
|
|
406
|
+
filterItems();
|
|
407
|
+
|
|
408
|
+
// render
|
|
409
|
+
render();
|
|
410
|
+
setMouseDown(true);
|
|
411
|
+
}, []);
|
|
412
|
+
const onMouseDown = _react.default.useCallback(event => {
|
|
413
|
+
const {
|
|
414
|
+
offsetX,
|
|
415
|
+
offsetY,
|
|
416
|
+
clientX,
|
|
417
|
+
clientY
|
|
418
|
+
} = event.nativeEvent;
|
|
419
|
+
onInteractionDown({
|
|
420
|
+
offsetX,
|
|
421
|
+
offsetY,
|
|
422
|
+
clientX,
|
|
423
|
+
clientY
|
|
424
|
+
}, event);
|
|
425
|
+
}, [onInteractionDown]);
|
|
426
|
+
const onTouchStart = _react.default.useCallback(event => {
|
|
427
|
+
// Get the first touch point
|
|
428
|
+
const touch = event.touches[0];
|
|
429
|
+
const {
|
|
430
|
+
clientX,
|
|
431
|
+
clientY
|
|
432
|
+
} = touch;
|
|
433
|
+
let {
|
|
434
|
+
offsetX,
|
|
435
|
+
offsetY
|
|
436
|
+
} = touch;
|
|
437
|
+
const targetElement = touch.target;
|
|
438
|
+
if (targetElement instanceof HTMLElement) {
|
|
439
|
+
// Get the bounding rectangle of the target element
|
|
440
|
+
const rect = targetElement.getBoundingClientRect();
|
|
441
|
+
|
|
442
|
+
// Calculate the offsetX and offsetY
|
|
443
|
+
offsetX = touch.clientX - rect.left;
|
|
444
|
+
offsetY = touch.clientY - rect.top;
|
|
445
|
+
}
|
|
446
|
+
onInteractionDown({
|
|
447
|
+
offsetX,
|
|
448
|
+
offsetY,
|
|
449
|
+
clientX,
|
|
450
|
+
clientY
|
|
451
|
+
}, event);
|
|
452
|
+
}, [onInteractionDown]);
|
|
453
|
+
const removeItems = _react.default.useCallback(() => {
|
|
454
|
+
// remove
|
|
455
|
+
if (refs.remove.current.length) {
|
|
456
|
+
const toRemove = [];
|
|
457
|
+
for (const item of refs.remove.current) {
|
|
458
|
+
const index = refs.items.current.findIndex(itemItems => itemItems === item);
|
|
459
|
+
if (index > -1) toRemove.push(item);
|
|
460
|
+
}
|
|
461
|
+
if (toRemove.length) remove(toRemove);
|
|
462
|
+
refs.remove.current = [];
|
|
463
|
+
}
|
|
464
|
+
}, []);
|
|
465
|
+
const onUpdateCoordinates = _react.default.useCallback(() => {
|
|
466
|
+
const items = getItems();
|
|
467
|
+
items.forEach(item => {
|
|
468
|
+
const p = (item === null || item === void 0 ? void 0 : item.p) || [];
|
|
469
|
+
const ar = (item === null || item === void 0 ? void 0 : item.ar) || [];
|
|
470
|
+
const s = (item === null || item === void 0 ? void 0 : item.s) || {};
|
|
471
|
+
if (p.length) {
|
|
472
|
+
// cache
|
|
473
|
+
// x1, y1, width. height for position on the screen
|
|
474
|
+
const v = item.v;
|
|
475
|
+
|
|
476
|
+
// draw point
|
|
477
|
+
if (v === 'dp') {
|
|
478
|
+
const lineWidth = s.lineWidth || 10;
|
|
479
|
+
item.c = [p[0] - lineWidth / 2, p[1] - lineWidth / 2, lineWidth, lineWidth];
|
|
536
480
|
}
|
|
537
|
-
}, [onChange]);
|
|
538
|
-
const updateTextBoxDimensions = react_1.default.useCallback((item) => {
|
|
539
|
-
const ui = refs.ui.current.getContext('2d');
|
|
540
|
-
const { lineHeight, padding } = refs.textSettings.current;
|
|
541
|
-
ui.font = '16px Arial';
|
|
542
|
-
const maxWidth = Math.max(...item.s.lines.map(line => ui.measureText(line).width));
|
|
543
|
-
item.ar[0] = maxWidth + padding * 2.5;
|
|
544
|
-
item.ar[1] = item.s.lines.length * lineHeight + padding * 2;
|
|
545
|
-
}, []);
|
|
546
|
-
const getPath = react_1.default.useCallback((item) => {
|
|
547
|
-
const path = new Path2D();
|
|
548
|
-
const { v, p, ar } = item;
|
|
549
481
|
// draw line
|
|
550
|
-
if (v === 'dl') {
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
path.quadraticCurveTo(current[0], current[1], midX, midY);
|
|
563
|
-
}
|
|
482
|
+
else if (v === 'dl') {
|
|
483
|
+
const x = [];
|
|
484
|
+
const y = [];
|
|
485
|
+
for (let i = 0; i < p.length; i += 2) {
|
|
486
|
+
x.push(p[i]);
|
|
487
|
+
y.push(p[i + 1]);
|
|
488
|
+
}
|
|
489
|
+
const xMin = Math.min(...x);
|
|
490
|
+
const yMin = Math.min(...y);
|
|
491
|
+
const xMax = Math.max(...x);
|
|
492
|
+
const yMax = Math.max(...y);
|
|
493
|
+
item.c = [xMin, yMin, xMax - xMin, yMax - yMin];
|
|
564
494
|
}
|
|
565
|
-
//
|
|
566
|
-
else if (['
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
495
|
+
// object line, object arrow
|
|
496
|
+
else if (['ol', 'oa'].includes(v)) {
|
|
497
|
+
const x = [p[0], ar[0]];
|
|
498
|
+
const y = [p[1], ar[1]];
|
|
499
|
+
const xMin = Math.min(...x);
|
|
500
|
+
const yMin = Math.min(...y);
|
|
501
|
+
const xMax = Math.max(...x);
|
|
502
|
+
const yMax = Math.max(...y);
|
|
503
|
+
item.c = [xMin, yMin, xMax - xMin, yMax - yMin];
|
|
572
504
|
}
|
|
573
|
-
// object rectangle
|
|
505
|
+
// object rectangle, object square
|
|
574
506
|
else if (['or', 'os'].includes(v)) {
|
|
575
|
-
|
|
507
|
+
item.c = [...p, ...ar];
|
|
576
508
|
}
|
|
577
|
-
// object
|
|
578
|
-
else if (['
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
const headLength = 40;
|
|
585
|
-
const angle = Math.atan2(ar[1] - p[1], ar[0] - p[0]);
|
|
586
|
-
path.moveTo(ar[0], ar[1]);
|
|
587
|
-
path.lineTo(ar[0] - headLength * Math.cos(angle - Math.PI / 6), ar[1] - headLength * Math.sin(angle - Math.PI / 6));
|
|
588
|
-
path.moveTo(ar[0], ar[1]);
|
|
589
|
-
path.lineTo(ar[0] - headLength * Math.cos(angle + Math.PI / 6), ar[1] - headLength * Math.sin(angle + Math.PI / 6));
|
|
590
|
-
}
|
|
509
|
+
// object circle, object ellipse
|
|
510
|
+
else if (['oc', 'oe'].includes(v)) {
|
|
511
|
+
if (v === 'oc') {
|
|
512
|
+
item.c = [p[0] - ar[0], p[1] - ar[0], ar[0] * 2, ar[0] * 2];
|
|
513
|
+
} else {
|
|
514
|
+
item.c = [p[0] - ar[0], p[1] - ar[1], ar[0] * 2, ar[1] * 2];
|
|
515
|
+
}
|
|
591
516
|
}
|
|
592
|
-
// object triangle
|
|
517
|
+
// object triangle, object triangle equilateral
|
|
593
518
|
else if (['ot', 'ote'].includes(v)) {
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
return path;
|
|
600
|
-
}, []);
|
|
601
|
-
const draw = react_1.default.useCallback((item) => {
|
|
602
|
-
var _a, _b;
|
|
603
|
-
const ui = refs.ui.current.getContext('2d');
|
|
604
|
-
// settings
|
|
605
|
-
Object.keys(item.s || {}).forEach(key => { var _a; return ui[key] = (_a = item.s) === null || _a === void 0 ? void 0 : _a[key]; });
|
|
606
|
-
ui.globalAlpha = refs.remove.current.includes(item) ? 0.25 : ((_a = item.s) === null || _a === void 0 ? void 0 : _a.globalAlpha) !== undefined ? (_b = item.s) === null || _b === void 0 ? void 0 : _b.globalAlpha : 1;
|
|
607
|
-
ui.beginPath();
|
|
608
|
-
const path = getPath(item);
|
|
609
|
-
const v = item.v;
|
|
610
|
-
if (['dp'].includes(v))
|
|
611
|
-
ui.fill(path);
|
|
612
|
-
else if (['dl', 'oc', 'oe', 'or', 'os', 'ol', 'oa', 'ot', 'ote'].includes(v))
|
|
613
|
-
ui.stroke(path);
|
|
614
|
-
}, []);
|
|
615
|
-
const drawGrid = react_1.default.useCallback(() => {
|
|
616
|
-
const uiCanvas = refs.ui.current;
|
|
617
|
-
const ui = refs.ui.current.getContext('2d');
|
|
618
|
-
const zoom = refs.scale.current;
|
|
619
|
-
const gridSize = 70;
|
|
620
|
-
const offsetX = refs.move.current.x / zoom;
|
|
621
|
-
const offsetY = refs.move.current.y / zoom;
|
|
622
|
-
// Calculate start positions based on offsets
|
|
623
|
-
const startX = Math.floor(-offsetX / gridSize) * gridSize;
|
|
624
|
-
const startY = Math.floor(-offsetY / gridSize) * gridSize;
|
|
625
|
-
const width = (uiCanvas.clientWidth * 1.5) / (zoom < 1 ? zoom : 1);
|
|
626
|
-
const height = (uiCanvas.clientHeight * 1.5) / (zoom < 1 ? zoom : 1);
|
|
627
|
-
if (gridSize < 30)
|
|
628
|
-
return;
|
|
629
|
-
// Draw main grid lines
|
|
630
|
-
ui.globalAlpha = 1;
|
|
631
|
-
ui.lineWidth = (zoom < 0.5 ? 0.3 : zoom <= 1 ? 0.5 : 0.7) / zoom;
|
|
632
|
-
ui.strokeStyle = '#ccc';
|
|
633
|
-
// grid
|
|
634
|
-
for (let x = startX; x < width + Math.abs(startX); x += gridSize) {
|
|
635
|
-
ui.beginPath();
|
|
636
|
-
ui.moveTo(x, startY);
|
|
637
|
-
ui.lineTo(x, height + startY);
|
|
638
|
-
ui.stroke();
|
|
639
|
-
}
|
|
640
|
-
for (let y = startY; y < height + Math.abs(startY); y += gridSize) {
|
|
641
|
-
ui.beginPath();
|
|
642
|
-
ui.moveTo(startX, y);
|
|
643
|
-
ui.lineTo(width + startX, y);
|
|
644
|
-
ui.stroke();
|
|
519
|
+
const [x1, y1, x2] = p;
|
|
520
|
+
const {
|
|
521
|
+
height
|
|
522
|
+
} = s;
|
|
523
|
+
item.c = [x1, y1 - height, x2 - x1, height];
|
|
645
524
|
}
|
|
646
|
-
//
|
|
647
|
-
if (
|
|
648
|
-
|
|
649
|
-
const subGridSize = gridSize / 5;
|
|
650
|
-
ui.lineWidth = (zoom <= 5 ? 0.6 : zoom <= 10 ? 0.8 : 1) / zoom;
|
|
651
|
-
ui.strokeStyle = '#ddd';
|
|
652
|
-
const dash = zoom < 1 ? 3 * zoom : 3 / zoom;
|
|
653
|
-
ui.setLineDash([dash, dash]);
|
|
654
|
-
for (let x = startX; x < width + Math.abs(startX); x += subGridSize) {
|
|
655
|
-
// without overlap
|
|
656
|
-
if (!(x % gridSize))
|
|
657
|
-
continue;
|
|
658
|
-
ui.beginPath();
|
|
659
|
-
ui.moveTo(x, startY);
|
|
660
|
-
ui.lineTo(x, height + startY);
|
|
661
|
-
ui.stroke();
|
|
662
|
-
}
|
|
663
|
-
for (let y = startY; y < height + Math.abs(startY); y += subGridSize) {
|
|
664
|
-
// without overlap
|
|
665
|
-
if (!(y % gridSize))
|
|
666
|
-
continue;
|
|
667
|
-
ui.beginPath();
|
|
668
|
-
ui.moveTo(startX, y);
|
|
669
|
-
ui.lineTo(width + startX, y);
|
|
670
|
-
ui.stroke();
|
|
671
|
-
}
|
|
672
|
-
ui.setLineDash([]); // Reset line dash
|
|
525
|
+
// image
|
|
526
|
+
else if (['i', 't'].includes(v)) {
|
|
527
|
+
item.c = [...p, ...ar];
|
|
673
528
|
}
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
const
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
529
|
+
}
|
|
530
|
+
});
|
|
531
|
+
}, []);
|
|
532
|
+
const onSelect = _react.default.useCallback(() => {
|
|
533
|
+
const select = refs.select.current;
|
|
534
|
+
if (!select) return;
|
|
535
|
+
const px1 = select.p[0];
|
|
536
|
+
const px2 = px1 + select.ar[0];
|
|
537
|
+
const py1 = select.p[1];
|
|
538
|
+
const py2 = py1 + select.ar[1];
|
|
539
|
+
const min = {
|
|
540
|
+
x: Math.min(px1, px2),
|
|
541
|
+
y: Math.min(py1, py2)
|
|
542
|
+
};
|
|
543
|
+
const max = {
|
|
544
|
+
x: Math.max(px1, px2),
|
|
545
|
+
y: Math.max(py1, py2)
|
|
546
|
+
};
|
|
547
|
+
const items = getItems();
|
|
548
|
+
items.forEach(item => {
|
|
549
|
+
const {
|
|
550
|
+
c
|
|
551
|
+
} = item;
|
|
552
|
+
const [x1, y1] = c;
|
|
553
|
+
let [x2, y2] = c;
|
|
554
|
+
x2 = x1 + x2;
|
|
555
|
+
y2 = y1 + y2;
|
|
556
|
+
const minItem = {
|
|
557
|
+
x: Math.min(x1, x2),
|
|
558
|
+
y: Math.min(y1, y2)
|
|
559
|
+
};
|
|
560
|
+
const maxItem = {
|
|
561
|
+
x: Math.max(x1, x2),
|
|
562
|
+
y: Math.max(y1, y2)
|
|
563
|
+
};
|
|
564
|
+
const selected = minItem.x >= min.x && maxItem.x <= max.x && minItem.y >= min.y && maxItem.y <= max.y;
|
|
565
|
+
if (selected) item.se = true;
|
|
566
|
+
});
|
|
567
|
+
}, []);
|
|
568
|
+
const onMouseUp = _react.default.useCallback(event => {
|
|
569
|
+
if (refs.mouseDown.current) {
|
|
570
|
+
refs.select.current = null;
|
|
571
|
+
|
|
572
|
+
// update coordinates
|
|
573
|
+
onUpdateCoordinates();
|
|
574
|
+
|
|
575
|
+
// reset
|
|
576
|
+
reset();
|
|
577
|
+
|
|
578
|
+
// remove
|
|
579
|
+
removeItems();
|
|
580
|
+
console.log('items', refs.items.current);
|
|
581
|
+
|
|
582
|
+
// onChange
|
|
583
|
+
onChange();
|
|
584
|
+
setMouseDown(false);
|
|
585
|
+
if (['circle', 'rectangle', 'triangle', 'line', 'line-arrow', 'image'].includes(refs.previousTool.current)) {
|
|
586
|
+
setTool('select');
|
|
587
|
+
}
|
|
588
|
+
render();
|
|
589
|
+
}
|
|
590
|
+
}, [onChange]);
|
|
591
|
+
const updateTextBoxDimensions = _react.default.useCallback(item => {
|
|
592
|
+
const ui = refs.ui.current.getContext('2d');
|
|
593
|
+
const {
|
|
594
|
+
lineHeight,
|
|
595
|
+
padding
|
|
596
|
+
} = refs.textSettings.current;
|
|
597
|
+
ui.font = '16px Arial';
|
|
598
|
+
const maxWidth = Math.max(...item.s.lines.map(line => ui.measureText(line).width));
|
|
599
|
+
item.ar[0] = maxWidth + padding * 2.5;
|
|
600
|
+
item.ar[1] = item.s.lines.length * lineHeight + padding * 2;
|
|
601
|
+
}, []);
|
|
602
|
+
const getPath = _react.default.useCallback(item => {
|
|
603
|
+
const path = new Path2D();
|
|
604
|
+
const {
|
|
605
|
+
v,
|
|
606
|
+
p,
|
|
607
|
+
ar
|
|
608
|
+
} = item;
|
|
609
|
+
|
|
610
|
+
// draw line
|
|
611
|
+
if (v === 'dl') {
|
|
612
|
+
const points = (0, _utils.arrayToParts)(p, 2);
|
|
613
|
+
for (let i = 0; i < points.length - 1; i++) {
|
|
614
|
+
const current = points[i];
|
|
615
|
+
const next = points[i + 1];
|
|
616
|
+
|
|
617
|
+
// calculate the control point for the curve
|
|
618
|
+
const midX = (current[0] + next[0]) / 2;
|
|
619
|
+
const midY = (current[1] + next[1]) / 2;
|
|
620
|
+
if (i === 0) {
|
|
621
|
+
// start from the first point
|
|
622
|
+
path.moveTo(current[0], current[1]);
|
|
744
623
|
}
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
}
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
624
|
+
path.quadraticCurveTo(current[0], current[1], midX, midY);
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
// draw point, object circle
|
|
628
|
+
else if (['dp', 'oc'].includes(v) && ar.length === 3) {
|
|
629
|
+
path.arc(p[0], p[1], ...ar);
|
|
630
|
+
}
|
|
631
|
+
// object ellipse
|
|
632
|
+
else if (v === 'oe' && ar.length === 5) {
|
|
633
|
+
path.ellipse(p[0], p[1], ...ar);
|
|
634
|
+
}
|
|
635
|
+
// object rectangle
|
|
636
|
+
else if (['or', 'os'].includes(v)) {
|
|
637
|
+
path.roundRect(p[0], p[1], ...ar);
|
|
638
|
+
}
|
|
639
|
+
// object line
|
|
640
|
+
else if (['ol', 'oa'].includes(v) && ar.length === 2) {
|
|
641
|
+
path.moveTo(p[0], p[1]);
|
|
642
|
+
path.lineTo(...ar);
|
|
643
|
+
|
|
644
|
+
// draw an arrow
|
|
645
|
+
if (v === 'oa') {
|
|
646
|
+
// Length of the arrowhead
|
|
647
|
+
const headLength = 40;
|
|
648
|
+
const angle = Math.atan2(ar[1] - p[1], ar[0] - p[0]);
|
|
649
|
+
path.moveTo(ar[0], ar[1]);
|
|
650
|
+
path.lineTo(ar[0] - headLength * Math.cos(angle - Math.PI / 6), ar[1] - headLength * Math.sin(angle - Math.PI / 6));
|
|
651
|
+
path.moveTo(ar[0], ar[1]);
|
|
652
|
+
path.lineTo(ar[0] - headLength * Math.cos(angle + Math.PI / 6), ar[1] - headLength * Math.sin(angle + Math.PI / 6));
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
// object triangle
|
|
656
|
+
else if (['ot', 'ote'].includes(v)) {
|
|
657
|
+
path.moveTo(p[0], p[1]);
|
|
658
|
+
path.lineTo(p[2], p[3]);
|
|
659
|
+
path.lineTo(p[4], p[5]);
|
|
660
|
+
path.closePath();
|
|
661
|
+
}
|
|
662
|
+
return path;
|
|
663
|
+
}, []);
|
|
664
|
+
const draw = _react.default.useCallback(item => {
|
|
665
|
+
var _item$s3, _item$s4;
|
|
666
|
+
const ui = refs.ui.current.getContext('2d');
|
|
667
|
+
|
|
668
|
+
// settings
|
|
669
|
+
Object.keys(item.s || {}).forEach(key => {
|
|
670
|
+
var _item$s2;
|
|
671
|
+
return ui[key] = (_item$s2 = item.s) === null || _item$s2 === void 0 ? void 0 : _item$s2[key];
|
|
672
|
+
});
|
|
673
|
+
ui.globalAlpha = refs.remove.current.includes(item) ? 0.25 : ((_item$s3 = item.s) === null || _item$s3 === void 0 ? void 0 : _item$s3.globalAlpha) !== undefined ? (_item$s4 = item.s) === null || _item$s4 === void 0 ? void 0 : _item$s4.globalAlpha : 1;
|
|
674
|
+
ui.beginPath();
|
|
675
|
+
const path = getPath(item);
|
|
676
|
+
const v = item.v;
|
|
677
|
+
if (['dp'].includes(v)) ui.fill(path);else if (['dl', 'oc', 'oe', 'or', 'os', 'ol', 'oa', 'ot', 'ote'].includes(v)) ui.stroke(path);
|
|
678
|
+
}, []);
|
|
679
|
+
const drawGrid = _react.default.useCallback(() => {
|
|
680
|
+
const uiCanvas = refs.ui.current;
|
|
681
|
+
const ui = refs.ui.current.getContext('2d');
|
|
682
|
+
const zoom = refs.scale.current;
|
|
683
|
+
const gridSize = 70;
|
|
684
|
+
const offsetX = refs.move.current.x / zoom;
|
|
685
|
+
const offsetY = refs.move.current.y / zoom;
|
|
686
|
+
|
|
687
|
+
// Calculate start positions based on offsets
|
|
688
|
+
const startX = Math.floor(-offsetX / gridSize) * gridSize;
|
|
689
|
+
const startY = Math.floor(-offsetY / gridSize) * gridSize;
|
|
690
|
+
const width = uiCanvas.clientWidth * 1.5 / (zoom < 1 ? zoom : 1);
|
|
691
|
+
const height = uiCanvas.clientHeight * 1.5 / (zoom < 1 ? zoom : 1);
|
|
692
|
+
if (gridSize < 30) return;
|
|
693
|
+
|
|
694
|
+
// Draw main grid lines
|
|
695
|
+
ui.globalAlpha = 1;
|
|
696
|
+
ui.lineWidth = (zoom < 0.5 ? 0.3 : zoom <= 1 ? 0.5 : 0.7) / zoom;
|
|
697
|
+
ui.strokeStyle = '#ccc';
|
|
698
|
+
|
|
699
|
+
// grid
|
|
700
|
+
for (let x = startX; x < width + Math.abs(startX); x += gridSize) {
|
|
701
|
+
ui.beginPath();
|
|
702
|
+
ui.moveTo(x, startY);
|
|
703
|
+
ui.lineTo(x, height + startY);
|
|
704
|
+
ui.stroke();
|
|
705
|
+
}
|
|
706
|
+
for (let y = startY; y < height + Math.abs(startY); y += gridSize) {
|
|
707
|
+
ui.beginPath();
|
|
708
|
+
ui.moveTo(startX, y);
|
|
709
|
+
ui.lineTo(width + startX, y);
|
|
710
|
+
ui.stroke();
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
// subgrid
|
|
714
|
+
if (gridSize * zoom > 100) {
|
|
715
|
+
// Draw subgrid lines if zoomed in
|
|
716
|
+
const subGridSize = gridSize / 5;
|
|
717
|
+
ui.lineWidth = (zoom <= 5 ? 0.6 : zoom <= 10 ? 0.8 : 1) / zoom;
|
|
718
|
+
ui.strokeStyle = '#ddd';
|
|
719
|
+
const dash = zoom < 1 ? 3 * zoom : 3 / zoom;
|
|
720
|
+
ui.setLineDash([dash, dash]);
|
|
721
|
+
for (let x = startX; x < width + Math.abs(startX); x += subGridSize) {
|
|
722
|
+
// without overlap
|
|
723
|
+
if (!(x % gridSize)) continue;
|
|
724
|
+
ui.beginPath();
|
|
725
|
+
ui.moveTo(x, startY);
|
|
726
|
+
ui.lineTo(x, height + startY);
|
|
727
|
+
ui.stroke();
|
|
728
|
+
}
|
|
729
|
+
for (let y = startY; y < height + Math.abs(startY); y += subGridSize) {
|
|
730
|
+
// without overlap
|
|
731
|
+
if (!(y % gridSize)) continue;
|
|
732
|
+
ui.beginPath();
|
|
733
|
+
ui.moveTo(startX, y);
|
|
734
|
+
ui.lineTo(width + startX, y);
|
|
735
|
+
ui.stroke();
|
|
736
|
+
}
|
|
737
|
+
ui.setLineDash([]); // Reset line dash
|
|
738
|
+
}
|
|
739
|
+
}, []);
|
|
740
|
+
const drawImage = _react.default.useCallback(item => {
|
|
741
|
+
const ui = refs.ui.current.getContext('2d');
|
|
742
|
+
ui.globalAlpha = 1;
|
|
743
|
+
ui.drawImage(item.s.image || refs.image.current, ...item.p, ...item.ar);
|
|
744
|
+
}, []);
|
|
745
|
+
const drawCursor = _react.default.useCallback(item => {
|
|
746
|
+
if (!item || !item.s.cursor) return;
|
|
747
|
+
const ui = refs.ui.current.getContext('2d');
|
|
748
|
+
const {
|
|
749
|
+
line,
|
|
750
|
+
char
|
|
751
|
+
} = item.s.cursor;
|
|
752
|
+
const currentLine = item.s.lines[line] || '';
|
|
753
|
+
const textWidth = ui.measureText(currentLine.slice(0, char)).width;
|
|
754
|
+
const {
|
|
755
|
+
padding,
|
|
756
|
+
lineHeight
|
|
757
|
+
} = refs.textSettings.current;
|
|
758
|
+
const cursorX = item.p[0] + padding + textWidth;
|
|
759
|
+
const cursorY = item.p[1] + padding + (line + 1) * lineHeight;
|
|
760
|
+
ui.fillStyle = 'black';
|
|
761
|
+
ui.fillRect(cursorX, cursorY - lineHeight + 3, 2, lineHeight - 5);
|
|
762
|
+
}, []);
|
|
763
|
+
const drawText = _react.default.useCallback(item => {
|
|
764
|
+
const ui = refs.ui.current.getContext('2d');
|
|
765
|
+
const zoom = refs.scale.current;
|
|
766
|
+
const [x, y] = item.p;
|
|
767
|
+
const [width, height] = item.ar;
|
|
768
|
+
const {
|
|
769
|
+
lineHeight,
|
|
770
|
+
padding,
|
|
771
|
+
fillStyle
|
|
772
|
+
} = refs.textSettings.current;
|
|
773
|
+
const selected = refs.tool.current === 'text' && item.se;
|
|
774
|
+
|
|
775
|
+
// Draw the box
|
|
776
|
+
ui.globalAlpha = 1;
|
|
777
|
+
ui.fillStyle = 'transparent';
|
|
778
|
+
ui.fillRect(x, y, width, height);
|
|
779
|
+
ui.lineWidth = 1 / zoom;
|
|
780
|
+
ui.strokeStyle = selected ? colorSelect : 'transparent';
|
|
781
|
+
ui.strokeRect(x, y, width, height);
|
|
782
|
+
|
|
783
|
+
// Draw the text
|
|
784
|
+
ui.fillStyle = fillStyle || 'black';
|
|
785
|
+
ui.font = '16px Arial';
|
|
786
|
+
item.s.lines.forEach((line, index) => {
|
|
787
|
+
ui.fillText(line, x + padding, y + padding + (index + 1) * lineHeight - 5);
|
|
788
|
+
});
|
|
789
|
+
if (selected) drawCursor(item);
|
|
790
|
+
}, []);
|
|
791
|
+
const drawSelect = _react.default.useCallback(item => {
|
|
792
|
+
const ui = refs.ui.current.getContext('2d');
|
|
793
|
+
const [x, y, width, height] = item.c || [];
|
|
794
|
+
const path = new Path2D();
|
|
795
|
+
path.rect(x, y, width, height);
|
|
796
|
+
ui.globalAlpha = 1;
|
|
797
|
+
ui.strokeStyle = colorSelect;
|
|
798
|
+
ui.lineCap = 'square';
|
|
799
|
+
ui.lineJoin = 'bevel';
|
|
800
|
+
ui.lineWidth = 1 / refs.scale.current;
|
|
801
|
+
ui.stroke(path);
|
|
802
|
+
}, []);
|
|
803
|
+
const drawSelection = _react.default.useCallback(() => {
|
|
804
|
+
const ui = refs.ui.current.getContext('2d');
|
|
805
|
+
const zoom = refs.scale.current;
|
|
806
|
+
|
|
807
|
+
// canvas selection
|
|
808
|
+
const select = refs.select.current;
|
|
809
|
+
if (select) {
|
|
810
|
+
ui.globalAlpha = 1;
|
|
811
|
+
ui.globalCompositeOperation = 'source-over';
|
|
812
|
+
ui.lineWidth = 1 / zoom;
|
|
813
|
+
ui.lineCap = 'square';
|
|
814
|
+
ui.lineJoin = 'bevel';
|
|
815
|
+
ui.strokeStyle = colorSelect;
|
|
816
|
+
ui.fillStyle = colorSelectBackground;
|
|
817
|
+
const path = getPath(select);
|
|
818
|
+
ui.fill(path);
|
|
819
|
+
ui.stroke(path);
|
|
820
|
+
}
|
|
821
|
+
}, []);
|
|
822
|
+
const render = _react.default.useCallback(() => {
|
|
823
|
+
const ui = refs.ui.current.getContext('2d');
|
|
824
|
+
const items = refs.items.current.filter(Boolean);
|
|
825
|
+
|
|
826
|
+
// methods
|
|
827
|
+
ui.clearRect(0, 0, refs.ui.current.width, refs.ui.current.height);
|
|
828
|
+
ui.save();
|
|
829
|
+
|
|
830
|
+
// pan
|
|
831
|
+
ui.translate(refs.move.current.x, refs.move.current.y);
|
|
832
|
+
// zoom
|
|
833
|
+
ui.scale(refs.scale.current, refs.scale.current);
|
|
834
|
+
|
|
835
|
+
// grid
|
|
836
|
+
if (refs.grid.current) drawGrid();
|
|
837
|
+
|
|
838
|
+
// draw
|
|
839
|
+
items.forEach(item => {
|
|
840
|
+
// image
|
|
841
|
+
if (item.v === 'i' && item.ar.length === 2) drawImage(item);
|
|
842
|
+
// text
|
|
843
|
+
else if (item.v === 't') drawText(item);
|
|
844
|
+
// other
|
|
845
|
+
else draw(item);
|
|
846
|
+
|
|
847
|
+
// select
|
|
848
|
+
if (item.se) drawSelect(item);
|
|
849
|
+
});
|
|
850
|
+
|
|
851
|
+
// canvas selection
|
|
852
|
+
drawSelection();
|
|
853
|
+
ui.restore();
|
|
854
|
+
}, []);
|
|
855
|
+
|
|
856
|
+
// Snap angle to nearest multiple of 15 degrees
|
|
857
|
+
const snapToAngle = _react.default.useCallback((dx, dy) => {
|
|
858
|
+
// Current angle in radians
|
|
859
|
+
const angle = Math.atan2(dy, dx);
|
|
860
|
+
// Snap to nearest 15 degrees
|
|
861
|
+
const snappedAngle = Math.round(angle / (Math.PI / 12)) * (Math.PI / 12);
|
|
862
|
+
// Length of the vector
|
|
863
|
+
const length = Math.sqrt(dx * dx + dy * dy);
|
|
864
|
+
return {
|
|
865
|
+
x: Math.cos(snappedAngle) * length,
|
|
866
|
+
y: Math.sin(snappedAngle) * length
|
|
867
|
+
};
|
|
868
|
+
}, []);
|
|
869
|
+
const onMoveItems = _react.default.useCallback((x, y) => {
|
|
870
|
+
const itemsSelected = getItems(true);
|
|
871
|
+
itemsSelected.forEach(item => {
|
|
872
|
+
const v = item.v;
|
|
873
|
+
|
|
874
|
+
// draw line
|
|
875
|
+
if (v === 'dl') {
|
|
876
|
+
item.p = item.p.map((value, index) => {
|
|
877
|
+
return index % 2 ? value + y : value + x;
|
|
818
878
|
});
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
const snapAt15Degrees = shiftKey;
|
|
897
|
-
let endX = currentX;
|
|
898
|
-
let endY = currentY;
|
|
899
|
-
if (snapAt15Degrees) {
|
|
900
|
-
const snapped = snapToAngle(currentX - start.x, currentY - start.y);
|
|
901
|
-
endX = start.x + snapped.x;
|
|
902
|
-
endY = start.y + snapped.y;
|
|
903
|
-
}
|
|
904
|
-
item.v = t === 'line' ? 'ol' : 'oa';
|
|
905
|
-
item.p = [startTransformed.x, startTransformed.y];
|
|
906
|
-
item.ar = [transform(endX - refs.move.current.x), transform(endY - refs.move.current.y)];
|
|
907
|
-
}
|
|
908
|
-
// object circle
|
|
909
|
-
else if (t === 'circle') {
|
|
910
|
-
const width = currentX - start.x;
|
|
911
|
-
const height = currentY - start.y;
|
|
912
|
-
const isCircle = shiftKey;
|
|
913
|
-
if (isCircle) {
|
|
914
|
-
const radius = Math.min(Math.abs(width), Math.abs(height)) / 2;
|
|
915
|
-
const centerX = start.x + Math.sign(width) * radius;
|
|
916
|
-
const centerY = start.y + Math.sign(height) * radius;
|
|
917
|
-
item.v = 'oc';
|
|
918
|
-
item.p = [transform(centerX - refs.move.current.x), transform(centerY - refs.move.current.y)];
|
|
919
|
-
item.ar = [transform(radius), 0, Math.PI * 2];
|
|
920
|
-
}
|
|
921
|
-
else {
|
|
922
|
-
item.v = 'oe';
|
|
923
|
-
item.p = [transform((start.x + width / 2) - refs.move.current.x), transform((start.y + height / 2) - refs.move.current.y)];
|
|
924
|
-
item.ar = [transform(Math.abs(width) / 2), transform(Math.abs(height) / 2), 0, 0, Math.PI * 2];
|
|
925
|
-
}
|
|
926
|
-
}
|
|
927
|
-
// object rectangle
|
|
928
|
-
else if (t === 'rectangle') {
|
|
929
|
-
const width = currentX - start.x;
|
|
930
|
-
const height = currentY - start.y;
|
|
931
|
-
const isSquare = shiftKey;
|
|
932
|
-
const radius = 0;
|
|
933
|
-
if (isSquare) {
|
|
934
|
-
const side = Math.min(Math.abs(width), Math.abs(height));
|
|
935
|
-
item.v = 'os';
|
|
936
|
-
item.p = [startTransformed.x, startTransformed.y];
|
|
937
|
-
item.ar = [transform(Math.sign(width) * side), transform(Math.sign(height) * side), radius];
|
|
938
|
-
}
|
|
939
|
-
else {
|
|
940
|
-
item.v = 'or';
|
|
941
|
-
item.p = [startTransformed.x, startTransformed.y];
|
|
942
|
-
item.ar = [transform(width), transform(height), radius];
|
|
943
|
-
}
|
|
944
|
-
}
|
|
945
|
-
// object triangle
|
|
946
|
-
else if (['triangle'].includes(t)) {
|
|
947
|
-
const endX = xo;
|
|
948
|
-
const endY = yo;
|
|
949
|
-
const base = Math.abs(endX - startTransformed.x);
|
|
950
|
-
const height = shiftKey ? base * Math.sqrt(3) / 2 : Math.abs(endY - startTransformed.y);
|
|
951
|
-
const points = [startTransformed.x, startTransformed.y, endX, startTransformed.y, startTransformed.x + (endX - startTransformed.x) / 2, startTransformed.y - height];
|
|
952
|
-
item.v = shiftKey ? 'ote' : 'ot';
|
|
953
|
-
item.p = points;
|
|
954
|
-
item.s = Object.assign(Object.assign({}, item.s), { height });
|
|
879
|
+
}
|
|
880
|
+
|
|
881
|
+
// rectangle, draw point, object circle, ellipse, object line, object arrow, object triangle, image, text
|
|
882
|
+
if (['or', 'os', 'dp', 'oc', 'oe', 'ol', 'oa', 'ot', 'ote', 'i', 't'].includes(v)) {
|
|
883
|
+
item.p[0] += x;
|
|
884
|
+
item.p[1] += y;
|
|
885
|
+
}
|
|
886
|
+
|
|
887
|
+
// object line
|
|
888
|
+
if (['ol', 'oa'].includes(v)) {
|
|
889
|
+
item.ar[0] += x;
|
|
890
|
+
item.ar[1] += y;
|
|
891
|
+
}
|
|
892
|
+
|
|
893
|
+
// object triangle
|
|
894
|
+
if (['ot', 'ote'].includes(v)) {
|
|
895
|
+
item.p[2] += x;
|
|
896
|
+
item.p[4] += x;
|
|
897
|
+
item.p[3] += y;
|
|
898
|
+
item.p[5] += y;
|
|
899
|
+
}
|
|
900
|
+
});
|
|
901
|
+
onUpdateCoordinates();
|
|
902
|
+
}, []);
|
|
903
|
+
const onMove = _react.default.useCallback((body, event) => {
|
|
904
|
+
if (!refs.on.current) return;
|
|
905
|
+
const {
|
|
906
|
+
offsetX: x,
|
|
907
|
+
offsetY: y,
|
|
908
|
+
clientX,
|
|
909
|
+
clientY
|
|
910
|
+
} = body;
|
|
911
|
+
const xo = transform(x - refs.move.current.x);
|
|
912
|
+
const yo = transform(y - refs.move.current.y);
|
|
913
|
+
const ui = refs.ui.current.getContext('2d');
|
|
914
|
+
const rect = refs.ui.current.getBoundingClientRect();
|
|
915
|
+
const currentX = clientX - rect.left;
|
|
916
|
+
const currentY = clientY - rect.top;
|
|
917
|
+
const start = refs.start.current;
|
|
918
|
+
const startTransformed = {
|
|
919
|
+
x: transform(start.x - refs.move.current.x),
|
|
920
|
+
y: transform(start.y - refs.move.current.y)
|
|
921
|
+
};
|
|
922
|
+
const item = getItem();
|
|
923
|
+
const items = getItems();
|
|
924
|
+
const t = refs.tool.current;
|
|
925
|
+
const shiftKey = event.shiftKey;
|
|
926
|
+
const zoom = refs.scale.current;
|
|
927
|
+
refs.mouseMove.current.current = {
|
|
928
|
+
x: clientX / zoom,
|
|
929
|
+
y: clientY / zoom
|
|
930
|
+
};
|
|
931
|
+
refs.mouseMove.current.delta = {
|
|
932
|
+
x: refs.mouseMove.current.previous ? refs.mouseMove.current.current.x - refs.mouseMove.current.previous.x : 0,
|
|
933
|
+
y: refs.mouseMove.current.previous ? refs.mouseMove.current.current.y - refs.mouseMove.current.previous.y : 0
|
|
934
|
+
};
|
|
935
|
+
refs.mouseMove.current.previous = _objectSpread({}, refs.mouseMove.current.current);
|
|
936
|
+
const delta = refs.mouseMove.current.delta;
|
|
937
|
+
|
|
938
|
+
// select
|
|
939
|
+
if (t === 'select') {
|
|
940
|
+
if (!refs.moveStarted.current) refs.moveStarted.current = true;
|
|
941
|
+
if (refs.select.current) {
|
|
942
|
+
unselectAll();
|
|
943
|
+
const width = currentX - start.x;
|
|
944
|
+
const height = currentY - start.y;
|
|
945
|
+
const isSquare = shiftKey;
|
|
946
|
+
const radius = 0;
|
|
947
|
+
if (isSquare) {
|
|
948
|
+
const side = Math.min(Math.abs(width), Math.abs(height));
|
|
949
|
+
refs.select.current.v = 'os';
|
|
950
|
+
refs.select.current.p = [startTransformed.x, startTransformed.y];
|
|
951
|
+
refs.select.current.ar = [transform(Math.sign(width) * side), transform(Math.sign(height) * side), radius];
|
|
952
|
+
} else {
|
|
953
|
+
refs.select.current.v = 'or';
|
|
954
|
+
refs.select.current.p = [startTransformed.x, startTransformed.y];
|
|
955
|
+
refs.select.current.ar = [transform(width), transform(height), radius];
|
|
955
956
|
}
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
957
|
+
} else onMoveItems(delta.x, delta.y);
|
|
958
|
+
}
|
|
959
|
+
// pen
|
|
960
|
+
else if (t === 'pen' && item) {
|
|
961
|
+
// same path from draw point, to move
|
|
962
|
+
if (!refs.moveStarted.current) {
|
|
963
|
+
item.v = 'dl';
|
|
964
|
+
refs.moveStarted.current = true;
|
|
965
|
+
}
|
|
966
|
+
|
|
967
|
+
// Add the current point to the path
|
|
968
|
+
item.p.push(xo, yo);
|
|
969
|
+
}
|
|
970
|
+
// pan
|
|
971
|
+
else if (t === 'pan') {
|
|
972
|
+
refs.move.current.x = x - refs.previous.current.x + refs.offset.current.x;
|
|
973
|
+
refs.move.current.y = y - refs.previous.current.y + refs.offset.current.y;
|
|
974
|
+
}
|
|
975
|
+
// eraser
|
|
976
|
+
else if (t === 'eraser') {
|
|
977
|
+
// find all items that x, y collides with, with certain radius
|
|
978
|
+
for (const i of items) {
|
|
979
|
+
const isPointInStroke = ui.isPointInStroke(getPath(i), xo, yo);
|
|
980
|
+
if (isPointInStroke) refs.remove.current.push(i);
|
|
981
|
+
}
|
|
982
|
+
}
|
|
983
|
+
// object line, object arrow
|
|
984
|
+
else if (['line', 'line-arrow'].includes(t)) {
|
|
985
|
+
const snapAt15Degrees = shiftKey;
|
|
986
|
+
let endX = currentX;
|
|
987
|
+
let endY = currentY;
|
|
988
|
+
if (snapAt15Degrees) {
|
|
989
|
+
const snapped = snapToAngle(currentX - start.x, currentY - start.y);
|
|
990
|
+
endX = start.x + snapped.x;
|
|
991
|
+
endY = start.y + snapped.y;
|
|
992
|
+
}
|
|
993
|
+
item.v = t === 'line' ? 'ol' : 'oa';
|
|
994
|
+
item.p = [startTransformed.x, startTransformed.y];
|
|
995
|
+
item.ar = [transform(endX - refs.move.current.x), transform(endY - refs.move.current.y)];
|
|
996
|
+
}
|
|
997
|
+
// object circle
|
|
998
|
+
else if (t === 'circle') {
|
|
999
|
+
const width = currentX - start.x;
|
|
1000
|
+
const height = currentY - start.y;
|
|
1001
|
+
const isCircle = shiftKey;
|
|
1002
|
+
if (isCircle) {
|
|
1003
|
+
const radius = Math.min(Math.abs(width), Math.abs(height)) / 2;
|
|
1004
|
+
const centerX = start.x + Math.sign(width) * radius;
|
|
1005
|
+
const centerY = start.y + Math.sign(height) * radius;
|
|
1006
|
+
item.v = 'oc';
|
|
1007
|
+
item.p = [transform(centerX - refs.move.current.x), transform(centerY - refs.move.current.y)];
|
|
1008
|
+
item.ar = [transform(radius), 0, Math.PI * 2];
|
|
1009
|
+
} else {
|
|
1010
|
+
item.v = 'oe';
|
|
1011
|
+
item.p = [transform(start.x + width / 2 - refs.move.current.x), transform(start.y + height / 2 - refs.move.current.y)];
|
|
1012
|
+
item.ar = [transform(Math.abs(width) / 2), transform(Math.abs(height) / 2), 0, 0, Math.PI * 2];
|
|
1013
|
+
}
|
|
1014
|
+
}
|
|
1015
|
+
// object rectangle
|
|
1016
|
+
else if (t === 'rectangle') {
|
|
1017
|
+
const width = currentX - start.x;
|
|
1018
|
+
const height = currentY - start.y;
|
|
1019
|
+
const isSquare = shiftKey;
|
|
1020
|
+
const radius = 0;
|
|
1021
|
+
if (isSquare) {
|
|
1022
|
+
const side = Math.min(Math.abs(width), Math.abs(height));
|
|
1023
|
+
item.v = 'os';
|
|
1024
|
+
item.p = [startTransformed.x, startTransformed.y];
|
|
1025
|
+
item.ar = [transform(Math.sign(width) * side), transform(Math.sign(height) * side), radius];
|
|
1026
|
+
} else {
|
|
1027
|
+
item.v = 'or';
|
|
1028
|
+
item.p = [startTransformed.x, startTransformed.y];
|
|
1029
|
+
item.ar = [transform(width), transform(height), radius];
|
|
1030
|
+
}
|
|
1031
|
+
}
|
|
1032
|
+
// object triangle
|
|
1033
|
+
else if (['triangle'].includes(t)) {
|
|
1034
|
+
const endX = xo;
|
|
1035
|
+
const endY = yo;
|
|
1036
|
+
const base = Math.abs(endX - startTransformed.x);
|
|
1037
|
+
const height = shiftKey ? base * Math.sqrt(3) / 2 : Math.abs(endY - startTransformed.y);
|
|
1038
|
+
const points = [startTransformed.x, startTransformed.y, endX, startTransformed.y, startTransformed.x + (endX - startTransformed.x) / 2, startTransformed.y - height];
|
|
1039
|
+
item.v = shiftKey ? 'ote' : 'ot';
|
|
1040
|
+
item.p = points;
|
|
1041
|
+
item.s = _objectSpread(_objectSpread({}, item.s), {}, {
|
|
1042
|
+
height
|
|
1043
|
+
});
|
|
1044
|
+
}
|
|
1045
|
+
// image
|
|
1046
|
+
else if (t === 'image' && refs.image.current.complete && refs.image.current.src) {
|
|
1047
|
+
const width = transform(currentX - start.x);
|
|
1048
|
+
const height = transform(currentY - start.y);
|
|
1049
|
+
const keepAspectRatio = !shiftKey;
|
|
1050
|
+
let currentWidth;
|
|
1051
|
+
let currentHeight;
|
|
1052
|
+
if (keepAspectRatio) {
|
|
1053
|
+
if (Math.abs(width / refs.aspectRatio.current) <= Math.abs(height)) {
|
|
1054
|
+
currentWidth = width;
|
|
1055
|
+
currentHeight = width / refs.aspectRatio.current;
|
|
1056
|
+
} else {
|
|
1057
|
+
currentWidth = height * refs.aspectRatio.current;
|
|
1058
|
+
currentHeight = height;
|
|
987
1059
|
}
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
1060
|
+
} else {
|
|
1061
|
+
currentWidth = width;
|
|
1062
|
+
currentHeight = height;
|
|
1063
|
+
}
|
|
1064
|
+
if (keepAspectRatio) {
|
|
1065
|
+
if (width < 0 && currentWidth > 0 || width > 0 && currentWidth < 0) currentWidth *= -1;
|
|
1066
|
+
if (height < 0 && currentHeight > 0 || height > 0 && currentHeight < 0) currentHeight *= -1;
|
|
1067
|
+
}
|
|
1068
|
+
item.p = [startTransformed.x, startTransformed.y];
|
|
1069
|
+
item.ar = [currentWidth, currentHeight];
|
|
1070
|
+
}
|
|
1071
|
+
|
|
1072
|
+
// select box
|
|
1073
|
+
onSelect();
|
|
1074
|
+
|
|
1075
|
+
// render
|
|
1076
|
+
render();
|
|
1077
|
+
}, []);
|
|
1078
|
+
const onMouseMove = _react.default.useCallback(event => {
|
|
1079
|
+
const {
|
|
1080
|
+
offsetX,
|
|
1081
|
+
offsetY,
|
|
1082
|
+
clientX,
|
|
1083
|
+
clientY
|
|
1084
|
+
} = event;
|
|
1085
|
+
onMove({
|
|
1086
|
+
offsetX,
|
|
1087
|
+
offsetY,
|
|
1088
|
+
clientX,
|
|
1089
|
+
clientY
|
|
1090
|
+
}, event);
|
|
1091
|
+
}, [onMove]);
|
|
1092
|
+
const onTouchMove = _react.default.useCallback(event => {
|
|
1093
|
+
// Get the first touch point
|
|
1094
|
+
const touch = event.touches[0];
|
|
1095
|
+
const {
|
|
1096
|
+
clientX,
|
|
1097
|
+
clientY
|
|
1098
|
+
} = touch;
|
|
1099
|
+
let {
|
|
1100
|
+
offsetX,
|
|
1101
|
+
offsetY
|
|
1102
|
+
} = touch;
|
|
1103
|
+
const targetElement = touch.target;
|
|
1104
|
+
if (targetElement instanceof HTMLElement) {
|
|
1105
|
+
// Get the bounding rectangle of the target element
|
|
1106
|
+
const rect = targetElement.getBoundingClientRect();
|
|
1107
|
+
|
|
1108
|
+
// Calculate the offsetX and offsetY
|
|
1109
|
+
offsetX = touch.clientX - rect.left;
|
|
1110
|
+
offsetY = touch.clientY - rect.top;
|
|
1111
|
+
}
|
|
1112
|
+
onMove({
|
|
1113
|
+
offsetX,
|
|
1114
|
+
offsetY,
|
|
1115
|
+
clientX,
|
|
1116
|
+
clientY
|
|
1117
|
+
}, event);
|
|
1118
|
+
}, [onInteractionDown]);
|
|
1119
|
+
const undo = _react.default.useCallback(() => {
|
|
1120
|
+
if (!refs.undo.current.length) return;
|
|
1121
|
+
|
|
1122
|
+
// add current state to redo
|
|
1123
|
+
refs.redo.current.push([...getItems()]);
|
|
1124
|
+
|
|
1125
|
+
// restore the undo state
|
|
1126
|
+
refs.items.current = refs.undo.current.pop();
|
|
1127
|
+
|
|
1128
|
+
// render
|
|
1129
|
+
render();
|
|
1130
|
+
}, []);
|
|
1131
|
+
const redo = _react.default.useCallback(() => {
|
|
1132
|
+
if (!refs.redo.current.length) return;
|
|
1133
|
+
|
|
1134
|
+
// add current state to undo
|
|
1135
|
+
refs.undo.current.push([...getItems()]);
|
|
1136
|
+
|
|
1137
|
+
// restore the redo state
|
|
1138
|
+
refs.items.current = refs.redo.current.pop();
|
|
1139
|
+
|
|
1140
|
+
// render
|
|
1141
|
+
render();
|
|
1142
|
+
}, []);
|
|
1143
|
+
const onWheel = _react.default.useCallback(eventReact => {
|
|
1144
|
+
const event = eventReact.nativeEvent;
|
|
1145
|
+
|
|
1146
|
+
// zoom
|
|
1147
|
+
if (event.metaKey || event.ctrlKey) {
|
|
1148
|
+
setTool('zoom');
|
|
1149
|
+
refs.toolUpdateAuto.current = true;
|
|
1150
|
+
const zoomFactor = 1.054;
|
|
1151
|
+
const mouseX = event.offsetX;
|
|
1152
|
+
const mouseY = event.offsetY;
|
|
1153
|
+
const scale = refs.scale.current;
|
|
1154
|
+
|
|
1155
|
+
// Convert mouse position to canvas coordinates
|
|
1156
|
+
const canvasX = (mouseX - refs.move.current.x) / scale;
|
|
1157
|
+
const canvasY = (mouseY - refs.move.current.y) / scale;
|
|
1158
|
+
|
|
1159
|
+
// Adjust scale
|
|
1160
|
+
const zoomIn = event.deltaY < 0;
|
|
1161
|
+
const newScale = zoomIn ? scale * zoomFactor : scale / zoomFactor;
|
|
1162
|
+
if (newScale <= maxZoom / 100 && newScale >= minZoom / 100) {
|
|
1163
|
+
// Update origin to focus on mouse position
|
|
1164
|
+
refs.move.current.x -= canvasX * (newScale - scale);
|
|
1165
|
+
refs.move.current.y -= canvasY * (newScale - scale);
|
|
1166
|
+
refs.scale.current = newScale;
|
|
991
1167
|
render();
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1168
|
+
}
|
|
1169
|
+
}
|
|
1170
|
+
// pan
|
|
1171
|
+
else if (!refs.mouseDown.current) {
|
|
1172
|
+
refs.move.current.x -= event.deltaX;
|
|
1173
|
+
refs.move.current.y -= event.deltaY;
|
|
1174
|
+
render();
|
|
1175
|
+
}
|
|
1176
|
+
}, [minZoom, maxZoom]);
|
|
1177
|
+
const onPaste = _react.default.useCallback(event => {
|
|
1178
|
+
event.preventDefault();
|
|
1179
|
+
|
|
1180
|
+
// Get clipboard data
|
|
1181
|
+
const items = Array.from(event.clipboardData.items);
|
|
1182
|
+
|
|
1183
|
+
// Loop through clipboard items to find an image
|
|
1184
|
+
for (const item of items) {
|
|
1185
|
+
if (item.type.startsWith('image/')) {
|
|
1186
|
+
// Get the image file
|
|
1187
|
+
const blob = item.getAsFile();
|
|
1188
|
+
refs.image.current = new Image();
|
|
1189
|
+
|
|
1190
|
+
// Load the image and draw it on the canvas
|
|
1191
|
+
refs.image.current.onload = () => {
|
|
1192
|
+
refs.aspectRatio.current = refs.image.current.width / refs.image.current.height;
|
|
1193
|
+
|
|
1194
|
+
// Todo
|
|
1195
|
+
// 1) Upload the image first, than read it in image src
|
|
1196
|
+
// 2) Add url of the image
|
|
1197
|
+
// instead of embeding the image
|
|
1198
|
+
const item_ = {
|
|
1199
|
+
i: (0, _utils.getID)(),
|
|
1200
|
+
v: 'i',
|
|
1201
|
+
p: [],
|
|
1202
|
+
ar: [],
|
|
1203
|
+
s: {
|
|
1204
|
+
// Todo
|
|
1205
|
+
// remove in the future
|
|
1206
|
+
image: refs.image.current,
|
|
1207
|
+
aspectRatio: refs.aspectRatio.current
|
|
1208
|
+
},
|
|
1209
|
+
a: _date.OnesyDate.milliseconds
|
|
1210
|
+
};
|
|
1211
|
+
add(item_);
|
|
1212
|
+
setTool('image');
|
|
1213
|
+
};
|
|
1214
|
+
|
|
1215
|
+
// Create an object URL for the blob and set it as the image source
|
|
1216
|
+
refs.image.current.src = URL.createObjectURL(blob);
|
|
1217
|
+
break;
|
|
1218
|
+
}
|
|
1219
|
+
}
|
|
1220
|
+
}, []);
|
|
1221
|
+
_react.default.useEffect(() => {
|
|
1222
|
+
const method = () => {
|
|
1223
|
+
const width = refs.root.current.offsetWidth;
|
|
1224
|
+
const height = refs.root.current.offsetHeight;
|
|
1225
|
+
setSize({
|
|
1226
|
+
width,
|
|
1227
|
+
height
|
|
1228
|
+
});
|
|
1229
|
+
};
|
|
1230
|
+
const onKeyUp = event => {
|
|
1231
|
+
if (refs.toolUpdateAuto.current) setTool(refs.previousTool.current || 'pen');
|
|
1232
|
+
};
|
|
1233
|
+
const onKeyDown = async event => {
|
|
1234
|
+
refs.toolUpdateAuto.current = false;
|
|
1235
|
+
const {
|
|
1236
|
+
key
|
|
1237
|
+
} = event;
|
|
1238
|
+
const itemsAll = [...refs.items.current].filter(Boolean);
|
|
1239
|
+
const t = refs.tool.current;
|
|
1240
|
+
const zoom = refs.scale.current;
|
|
1241
|
+
if (['a', 'A'].includes(key) && (event.metaKey || event.ctrlKey)) {
|
|
1242
|
+
event.preventDefault();
|
|
1243
|
+
selectAll();
|
|
1244
|
+
render();
|
|
1245
|
+
} else if (t === 'select' && ['ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight', 'Backspace'].includes(key)) {
|
|
1246
|
+
const value = event.shiftKey ? 10 : 1;
|
|
1247
|
+
if (key === 'ArrowUp') onMoveItems(0, -value / zoom);
|
|
1248
|
+
if (key === 'ArrowDown') onMoveItems(0, value / zoom);
|
|
1249
|
+
if (key === 'ArrowLeft') onMoveItems(-value / zoom, 0);
|
|
1250
|
+
if (key === 'ArrowRight') onMoveItems(value / zoom, 0);
|
|
1251
|
+
if (key === 'Backspace') {
|
|
1252
|
+
const toRemove = [];
|
|
1253
|
+
refs.items.current.forEach(item => {
|
|
1254
|
+
if (item.se) toRemove.push(item);
|
|
1255
|
+
});
|
|
1256
|
+
if (toRemove.length) remove(toRemove);
|
|
1009
1257
|
}
|
|
1010
|
-
onMove({ offsetX, offsetY, clientX, clientY }, event);
|
|
1011
|
-
}, [onInteractionDown]);
|
|
1012
|
-
const undo = react_1.default.useCallback(() => {
|
|
1013
|
-
if (!refs.undo.current.length)
|
|
1014
|
-
return;
|
|
1015
|
-
// add current state to redo
|
|
1016
|
-
refs.redo.current.push([...getItems()]);
|
|
1017
|
-
// restore the undo state
|
|
1018
|
-
refs.items.current = refs.undo.current.pop();
|
|
1019
|
-
// render
|
|
1020
1258
|
render();
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
refs.undo.current.push([...getItems()]);
|
|
1027
|
-
// restore the redo state
|
|
1028
|
-
refs.items.current = refs.redo.current.pop();
|
|
1029
|
-
// render
|
|
1259
|
+
} else if (key === 'Escape') {
|
|
1260
|
+
setTool('select');
|
|
1261
|
+
refs.textActive.current = null;
|
|
1262
|
+
unselectAll();
|
|
1263
|
+
filterItems();
|
|
1030
1264
|
render();
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
}
|
|
1265
|
+
} else if (tool === 'text') {
|
|
1266
|
+
const selectedTextBox = itemsAll.find(item => item.v === 't' && item.se);
|
|
1267
|
+
if (!selectedTextBox) return;
|
|
1268
|
+
const {
|
|
1269
|
+
line,
|
|
1270
|
+
char
|
|
1271
|
+
} = selectedTextBox.s.cursor;
|
|
1272
|
+
const lines = selectedTextBox.s.lines;
|
|
1273
|
+
const currentLine = lines[line];
|
|
1274
|
+
if (['ArrowLeft', 'ArrowRight'].includes(key)) {
|
|
1275
|
+
var _lines$line, _lines$selectedTextBo2;
|
|
1276
|
+
event.preventDefault();
|
|
1277
|
+
selectedTextBox.s.cursor.char += key === 'ArrowLeft' ? -1 : 1;
|
|
1278
|
+
if (selectedTextBox.s.cursor.char < 0) {
|
|
1279
|
+
var _lines$selectedTextBo;
|
|
1280
|
+
selectedTextBox.s.cursor.line--;
|
|
1281
|
+
selectedTextBox.s.cursor.char = (_lines$selectedTextBo = lines[selectedTextBox.s.cursor.line]) === null || _lines$selectedTextBo === void 0 ? void 0 : _lines$selectedTextBo.length;
|
|
1282
|
+
} else if (selectedTextBox.s.cursor.char > ((_lines$line = lines[line]) === null || _lines$line === void 0 ? void 0 : _lines$line.length) && line !== lines.length - 1) {
|
|
1283
|
+
selectedTextBox.s.cursor.line++;
|
|
1284
|
+
selectedTextBox.s.cursor.char = 0;
|
|
1285
|
+
}
|
|
1286
|
+
selectedTextBox.s.cursor.line = (0, _utils.clamp)(selectedTextBox.s.cursor.line, 0, lines.length - 1);
|
|
1287
|
+
selectedTextBox.s.cursor.char = (0, _utils.clamp)(selectedTextBox.s.cursor.char, 0, (_lines$selectedTextBo2 = lines[selectedTextBox.s.cursor.line]) === null || _lines$selectedTextBo2 === void 0 ? void 0 : _lines$selectedTextBo2.length);
|
|
1055
1288
|
}
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1289
|
+
if (['ArrowUp', 'ArrowDown'].includes(key)) {
|
|
1290
|
+
var _lines$selectedTextBo3;
|
|
1291
|
+
event.preventDefault();
|
|
1292
|
+
selectedTextBox.s.cursor.line += key === 'ArrowUp' ? -1 : 1;
|
|
1293
|
+
selectedTextBox.s.cursor.line = (0, _utils.clamp)(selectedTextBox.s.cursor.line, 0, lines.length - 1);
|
|
1294
|
+
selectedTextBox.s.cursor.char = (0, _utils.clamp)(selectedTextBox.s.cursor.char, 0, (_lines$selectedTextBo3 = lines[selectedTextBox.s.cursor.line]) === null || _lines$selectedTextBo3 === void 0 ? void 0 : _lines$selectedTextBo3.length);
|
|
1295
|
+
} else if (key === 'Enter') {
|
|
1296
|
+
event.preventDefault();
|
|
1297
|
+
const newLine = currentLine.slice(char);
|
|
1298
|
+
lines[line] = currentLine.slice(0, char);
|
|
1299
|
+
lines.splice(line + 1, 0, newLine);
|
|
1300
|
+
selectedTextBox.s.cursor.line++;
|
|
1301
|
+
selectedTextBox.s.cursor.char = 0;
|
|
1302
|
+
} else if (key === 'Backspace') {
|
|
1303
|
+
event.preventDefault();
|
|
1304
|
+
if (char > 0) {
|
|
1305
|
+
lines[line] = currentLine.slice(0, char - 1) + currentLine.slice(char);
|
|
1306
|
+
selectedTextBox.s.cursor.char--;
|
|
1307
|
+
} else if (line > 0) {
|
|
1308
|
+
const prevLine = lines[line - 1];
|
|
1309
|
+
selectedTextBox.s.cursor.char = prevLine.length;
|
|
1310
|
+
lines[line - 1] += lines[line];
|
|
1311
|
+
lines.splice(line, 1);
|
|
1312
|
+
selectedTextBox.s.cursor.line--;
|
|
1313
|
+
}
|
|
1314
|
+
} else if (key.length === 1) {
|
|
1315
|
+
if ([' '].includes(key)) event.preventDefault();
|
|
1316
|
+
let textClipboard = '';
|
|
1317
|
+
const isPaste = (event.ctrlKey || event.metaKey) && ['v', 'V'].includes(key);
|
|
1318
|
+
if (isPaste) {
|
|
1319
|
+
try {
|
|
1320
|
+
textClipboard = await window.navigator.clipboard.readText();
|
|
1321
|
+
} catch (error) {}
|
|
1322
|
+
}
|
|
1323
|
+
const text = isPaste ? textClipboard : key;
|
|
1324
|
+
selectedTextBox.s.lines[line] = currentLine.slice(0, char) + text + currentLine.slice(char);
|
|
1325
|
+
selectedTextBox.s.cursor.char += text.length;
|
|
1061
1326
|
}
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
for (const item of items) {
|
|
1069
|
-
if (item.type.startsWith('image/')) {
|
|
1070
|
-
// Get the image file
|
|
1071
|
-
const blob = item.getAsFile();
|
|
1072
|
-
refs.image.current = new Image();
|
|
1073
|
-
// Load the image and draw it on the canvas
|
|
1074
|
-
refs.image.current.onload = () => {
|
|
1075
|
-
refs.aspectRatio.current = refs.image.current.width / refs.image.current.height;
|
|
1076
|
-
// Todo
|
|
1077
|
-
// 1) Upload the image first, than read it in image src
|
|
1078
|
-
// 2) Add url of the image
|
|
1079
|
-
// instead of embeding the image
|
|
1080
|
-
const item_ = {
|
|
1081
|
-
i: (0, utils_1.getID)(),
|
|
1082
|
-
v: 'i',
|
|
1083
|
-
p: [],
|
|
1084
|
-
ar: [],
|
|
1085
|
-
s: {
|
|
1086
|
-
// Todo
|
|
1087
|
-
// remove in the future
|
|
1088
|
-
image: refs.image.current,
|
|
1089
|
-
aspectRatio: refs.aspectRatio.current
|
|
1090
|
-
},
|
|
1091
|
-
a: date_1.OnesyDate.milliseconds
|
|
1092
|
-
};
|
|
1093
|
-
add(item_);
|
|
1094
|
-
setTool('image');
|
|
1095
|
-
};
|
|
1096
|
-
// Create an object URL for the blob and set it as the image source
|
|
1097
|
-
refs.image.current.src = URL.createObjectURL(blob);
|
|
1098
|
-
break;
|
|
1099
|
-
}
|
|
1327
|
+
updateTextBoxDimensions(selectedTextBox);
|
|
1328
|
+
selectedTextBox.c = [...selectedTextBox.p, ...selectedTextBox.ar];
|
|
1329
|
+
render();
|
|
1330
|
+
} else {
|
|
1331
|
+
if (event.metaKey && key === 'z') {
|
|
1332
|
+
if (event.shiftKey) redo();else undo();
|
|
1100
1333
|
}
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
const width = refs.root.current.offsetWidth;
|
|
1105
|
-
const height = refs.root.current.offsetHeight;
|
|
1106
|
-
setSize({ width, height });
|
|
1107
|
-
};
|
|
1108
|
-
const onKeyUp = (event) => {
|
|
1109
|
-
if (refs.toolUpdateAuto.current)
|
|
1110
|
-
setTool(refs.previousTool.current || 'pen');
|
|
1111
|
-
};
|
|
1112
|
-
const onKeyDown = async (event) => {
|
|
1113
|
-
var _a, _b, _c, _d;
|
|
1114
|
-
refs.toolUpdateAuto.current = false;
|
|
1115
|
-
const { key } = event;
|
|
1116
|
-
const itemsAll = [...refs.items.current].filter(Boolean);
|
|
1117
|
-
const t = refs.tool.current;
|
|
1118
|
-
const zoom = refs.scale.current;
|
|
1119
|
-
if (['a', 'A'].includes(key) && (event.metaKey || event.ctrlKey)) {
|
|
1120
|
-
event.preventDefault();
|
|
1121
|
-
selectAll();
|
|
1122
|
-
render();
|
|
1123
|
-
}
|
|
1124
|
-
else if (t === 'select' && ['ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight', 'Backspace'].includes(key)) {
|
|
1125
|
-
const value = event.shiftKey ? 10 : 1;
|
|
1126
|
-
if (key === 'ArrowUp')
|
|
1127
|
-
onMoveItems(0, -value / zoom);
|
|
1128
|
-
if (key === 'ArrowDown')
|
|
1129
|
-
onMoveItems(0, value / zoom);
|
|
1130
|
-
if (key === 'ArrowLeft')
|
|
1131
|
-
onMoveItems(-value / zoom, 0);
|
|
1132
|
-
if (key === 'ArrowRight')
|
|
1133
|
-
onMoveItems(value / zoom, 0);
|
|
1134
|
-
if (key === 'Backspace') {
|
|
1135
|
-
const toRemove = [];
|
|
1136
|
-
refs.items.current.forEach(item => {
|
|
1137
|
-
if (item.se)
|
|
1138
|
-
toRemove.push(item);
|
|
1139
|
-
});
|
|
1140
|
-
if (toRemove.length)
|
|
1141
|
-
remove(toRemove);
|
|
1142
|
-
}
|
|
1143
|
-
render();
|
|
1144
|
-
}
|
|
1145
|
-
else if (key === 'Escape') {
|
|
1146
|
-
setTool('select');
|
|
1147
|
-
refs.textActive.current = null;
|
|
1148
|
-
unselectAll();
|
|
1149
|
-
filterItems();
|
|
1150
|
-
render();
|
|
1151
|
-
}
|
|
1152
|
-
else if (tool === 'text') {
|
|
1153
|
-
const selectedTextBox = itemsAll.find(item => item.v === 't' && item.se);
|
|
1154
|
-
if (!selectedTextBox)
|
|
1155
|
-
return;
|
|
1156
|
-
const { line, char } = selectedTextBox.s.cursor;
|
|
1157
|
-
const lines = selectedTextBox.s.lines;
|
|
1158
|
-
const currentLine = lines[line];
|
|
1159
|
-
if (['ArrowLeft', 'ArrowRight'].includes(key)) {
|
|
1160
|
-
event.preventDefault();
|
|
1161
|
-
selectedTextBox.s.cursor.char += key === 'ArrowLeft' ? -1 : 1;
|
|
1162
|
-
if (selectedTextBox.s.cursor.char < 0) {
|
|
1163
|
-
selectedTextBox.s.cursor.line--;
|
|
1164
|
-
selectedTextBox.s.cursor.char = (_a = lines[selectedTextBox.s.cursor.line]) === null || _a === void 0 ? void 0 : _a.length;
|
|
1165
|
-
}
|
|
1166
|
-
else if ((selectedTextBox.s.cursor.char > ((_b = lines[line]) === null || _b === void 0 ? void 0 : _b.length)) && line !== lines.length - 1) {
|
|
1167
|
-
selectedTextBox.s.cursor.line++;
|
|
1168
|
-
selectedTextBox.s.cursor.char = 0;
|
|
1169
|
-
}
|
|
1170
|
-
selectedTextBox.s.cursor.line = (0, utils_1.clamp)(selectedTextBox.s.cursor.line, 0, lines.length - 1);
|
|
1171
|
-
selectedTextBox.s.cursor.char = (0, utils_1.clamp)(selectedTextBox.s.cursor.char, 0, (_c = lines[selectedTextBox.s.cursor.line]) === null || _c === void 0 ? void 0 : _c.length);
|
|
1172
|
-
}
|
|
1173
|
-
if (['ArrowUp', 'ArrowDown'].includes(key)) {
|
|
1174
|
-
event.preventDefault();
|
|
1175
|
-
selectedTextBox.s.cursor.line += key === 'ArrowUp' ? -1 : 1;
|
|
1176
|
-
selectedTextBox.s.cursor.line = (0, utils_1.clamp)(selectedTextBox.s.cursor.line, 0, lines.length - 1);
|
|
1177
|
-
selectedTextBox.s.cursor.char = (0, utils_1.clamp)(selectedTextBox.s.cursor.char, 0, (_d = lines[selectedTextBox.s.cursor.line]) === null || _d === void 0 ? void 0 : _d.length);
|
|
1178
|
-
}
|
|
1179
|
-
else if (key === 'Enter') {
|
|
1180
|
-
event.preventDefault();
|
|
1181
|
-
const newLine = currentLine.slice(char);
|
|
1182
|
-
lines[line] = currentLine.slice(0, char);
|
|
1183
|
-
lines.splice(line + 1, 0, newLine);
|
|
1184
|
-
selectedTextBox.s.cursor.line++;
|
|
1185
|
-
selectedTextBox.s.cursor.char = 0;
|
|
1186
|
-
}
|
|
1187
|
-
else if (key === 'Backspace') {
|
|
1188
|
-
event.preventDefault();
|
|
1189
|
-
if (char > 0) {
|
|
1190
|
-
lines[line] = currentLine.slice(0, char - 1) + currentLine.slice(char);
|
|
1191
|
-
selectedTextBox.s.cursor.char--;
|
|
1192
|
-
}
|
|
1193
|
-
else if (line > 0) {
|
|
1194
|
-
const prevLine = lines[line - 1];
|
|
1195
|
-
selectedTextBox.s.cursor.char = prevLine.length;
|
|
1196
|
-
lines[line - 1] += lines[line];
|
|
1197
|
-
lines.splice(line, 1);
|
|
1198
|
-
selectedTextBox.s.cursor.line--;
|
|
1199
|
-
}
|
|
1200
|
-
}
|
|
1201
|
-
else if (key.length === 1) {
|
|
1202
|
-
if ([' '].includes(key))
|
|
1203
|
-
event.preventDefault();
|
|
1204
|
-
let textClipboard = '';
|
|
1205
|
-
const isPaste = (event.ctrlKey || event.metaKey) && ['v', 'V'].includes(key);
|
|
1206
|
-
if (isPaste) {
|
|
1207
|
-
try {
|
|
1208
|
-
textClipboard = await window.navigator.clipboard.readText();
|
|
1209
|
-
}
|
|
1210
|
-
catch (error) { }
|
|
1211
|
-
}
|
|
1212
|
-
const text = isPaste ? textClipboard : key;
|
|
1213
|
-
selectedTextBox.s.lines[line] = currentLine.slice(0, char) + text + currentLine.slice(char);
|
|
1214
|
-
selectedTextBox.s.cursor.char += text.length;
|
|
1215
|
-
}
|
|
1216
|
-
updateTextBoxDimensions(selectedTextBox);
|
|
1217
|
-
selectedTextBox.c = [
|
|
1218
|
-
...selectedTextBox.p,
|
|
1219
|
-
...selectedTextBox.ar
|
|
1220
|
-
];
|
|
1221
|
-
render();
|
|
1222
|
-
}
|
|
1223
|
-
else {
|
|
1224
|
-
if (event.metaKey && key === 'z') {
|
|
1225
|
-
if (event.shiftKey)
|
|
1226
|
-
redo();
|
|
1227
|
-
else
|
|
1228
|
-
undo();
|
|
1229
|
-
}
|
|
1230
|
-
if (key === ' ') {
|
|
1231
|
-
refs.toolUpdateAuto.current = true;
|
|
1232
|
-
setTool('pan');
|
|
1233
|
-
}
|
|
1234
|
-
// tools
|
|
1235
|
-
if (event.shiftKey) {
|
|
1236
|
-
if (['E', 'D', 'P', 'S', 'C', 'R', 'I', 'L', 'A', 'T', 'G'].includes(key))
|
|
1237
|
-
refs.toolUpdateAuto.current = false;
|
|
1238
|
-
if (key === 'E')
|
|
1239
|
-
setTool('eraser');
|
|
1240
|
-
if (key === 'D')
|
|
1241
|
-
setTool('pen');
|
|
1242
|
-
if (key === 'P')
|
|
1243
|
-
setTool('pan');
|
|
1244
|
-
if (key === 'S')
|
|
1245
|
-
setTool('select');
|
|
1246
|
-
if (key === 'C')
|
|
1247
|
-
setTool('circle');
|
|
1248
|
-
if (key === 'R')
|
|
1249
|
-
setTool('rectangle');
|
|
1250
|
-
if (key === 'I')
|
|
1251
|
-
setTool('triangle');
|
|
1252
|
-
if (key === 'L')
|
|
1253
|
-
setTool('line');
|
|
1254
|
-
if (key === 'A')
|
|
1255
|
-
setTool('line-arrow');
|
|
1256
|
-
if (key === 'T')
|
|
1257
|
-
setTool('text');
|
|
1258
|
-
if (key === 'G') {
|
|
1259
|
-
setGrid(previous => !previous);
|
|
1260
|
-
render();
|
|
1261
|
-
}
|
|
1262
|
-
}
|
|
1263
|
-
}
|
|
1264
|
-
};
|
|
1265
|
-
window.addEventListener('resize', method);
|
|
1266
|
-
window.document.addEventListener('mouseup', onMouseUp);
|
|
1267
|
-
window.document.addEventListener('touchend', onMouseUp);
|
|
1268
|
-
window.document.addEventListener('mousemove', onMouseMove);
|
|
1269
|
-
window.document.addEventListener('touchmove', onTouchMove);
|
|
1270
|
-
window.document.addEventListener('keyup', onKeyUp);
|
|
1271
|
-
window.document.addEventListener('keydown', onKeyDown);
|
|
1272
|
-
window.document.addEventListener('paste', onPaste);
|
|
1273
|
-
method();
|
|
1274
|
-
init();
|
|
1275
|
-
return () => {
|
|
1276
|
-
window.removeEventListener('resize', method);
|
|
1277
|
-
window.document.removeEventListener('mouseup', onMouseUp);
|
|
1278
|
-
window.document.removeEventListener('touchend', onMouseUp);
|
|
1279
|
-
window.document.removeEventListener('mousemove', onMouseMove);
|
|
1280
|
-
window.document.removeEventListener('touchmove', onTouchMove);
|
|
1281
|
-
window.document.removeEventListener('keyup', onKeyUp);
|
|
1282
|
-
window.document.removeEventListener('keydown', onKeyDown);
|
|
1283
|
-
window.document.removeEventListener('paste', onPaste);
|
|
1284
|
-
};
|
|
1285
|
-
}, []);
|
|
1286
|
-
const onChangeInputFile = react_1.default.useCallback((event) => {
|
|
1287
|
-
const file = event.target.files[0];
|
|
1288
|
-
if (file) {
|
|
1289
|
-
const reader = new FileReader();
|
|
1290
|
-
reader.onload = eventReader => {
|
|
1291
|
-
refs.image.current = new Image();
|
|
1292
|
-
refs.image.current.src = eventReader.target.result;
|
|
1293
|
-
refs.toolUpdateAuto.current = true;
|
|
1294
|
-
event.target.value = '';
|
|
1295
|
-
setTool('image');
|
|
1296
|
-
};
|
|
1297
|
-
reader.readAsDataURL(file);
|
|
1334
|
+
if (key === ' ') {
|
|
1335
|
+
refs.toolUpdateAuto.current = true;
|
|
1336
|
+
setTool('pan');
|
|
1298
1337
|
}
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1338
|
+
|
|
1339
|
+
// tools
|
|
1340
|
+
if (event.shiftKey) {
|
|
1341
|
+
if (['E', 'D', 'P', 'S', 'C', 'R', 'I', 'L', 'A', 'T', 'G'].includes(key)) refs.toolUpdateAuto.current = false;
|
|
1342
|
+
if (key === 'E') setTool('eraser');
|
|
1343
|
+
if (key === 'D') setTool('pen');
|
|
1344
|
+
if (key === 'P') setTool('pan');
|
|
1345
|
+
if (key === 'S') setTool('select');
|
|
1346
|
+
if (key === 'C') setTool('circle');
|
|
1347
|
+
if (key === 'R') setTool('rectangle');
|
|
1348
|
+
if (key === 'I') setTool('triangle');
|
|
1349
|
+
if (key === 'L') setTool('line');
|
|
1350
|
+
if (key === 'A') setTool('line-arrow');
|
|
1351
|
+
if (key === 'T') setTool('text');
|
|
1352
|
+
if (key === 'G') {
|
|
1353
|
+
setGrid(previous => !previous);
|
|
1354
|
+
render();
|
|
1355
|
+
}
|
|
1307
1356
|
}
|
|
1357
|
+
}
|
|
1308
1358
|
};
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1359
|
+
window.addEventListener('resize', method);
|
|
1360
|
+
window.document.addEventListener('mouseup', onMouseUp);
|
|
1361
|
+
window.document.addEventListener('touchend', onMouseUp);
|
|
1362
|
+
window.document.addEventListener('mousemove', onMouseMove);
|
|
1363
|
+
window.document.addEventListener('touchmove', onTouchMove);
|
|
1364
|
+
window.document.addEventListener('keyup', onKeyUp);
|
|
1365
|
+
window.document.addEventListener('keydown', onKeyDown);
|
|
1366
|
+
window.document.addEventListener('paste', onPaste);
|
|
1367
|
+
method();
|
|
1368
|
+
init();
|
|
1369
|
+
return () => {
|
|
1370
|
+
window.removeEventListener('resize', method);
|
|
1371
|
+
window.document.removeEventListener('mouseup', onMouseUp);
|
|
1372
|
+
window.document.removeEventListener('touchend', onMouseUp);
|
|
1373
|
+
window.document.removeEventListener('mousemove', onMouseMove);
|
|
1374
|
+
window.document.removeEventListener('touchmove', onTouchMove);
|
|
1375
|
+
window.document.removeEventListener('keyup', onKeyUp);
|
|
1376
|
+
window.document.removeEventListener('keydown', onKeyDown);
|
|
1377
|
+
window.document.removeEventListener('paste', onPaste);
|
|
1378
|
+
};
|
|
1379
|
+
}, []);
|
|
1380
|
+
const onChangeInputFile = _react.default.useCallback(event => {
|
|
1381
|
+
const file = event.target.files[0];
|
|
1382
|
+
if (file) {
|
|
1383
|
+
const reader = new FileReader();
|
|
1384
|
+
reader.onload = eventReader => {
|
|
1385
|
+
refs.image.current = new Image();
|
|
1386
|
+
refs.image.current.src = eventReader.target.result;
|
|
1387
|
+
refs.toolUpdateAuto.current = true;
|
|
1388
|
+
event.target.value = '';
|
|
1389
|
+
setTool('image');
|
|
1390
|
+
};
|
|
1391
|
+
reader.readAsDataURL(file);
|
|
1392
|
+
}
|
|
1393
|
+
}, []);
|
|
1394
|
+
const propsCanvas = {
|
|
1395
|
+
width: size.width,
|
|
1396
|
+
height: size.height,
|
|
1397
|
+
disabled: !loaded,
|
|
1398
|
+
style: {
|
|
1399
|
+
width: size.width,
|
|
1400
|
+
height: size.height
|
|
1401
|
+
}
|
|
1402
|
+
};
|
|
1403
|
+
return /*#__PURE__*/_react.default.createElement(Line, (0, _extends2.default)({
|
|
1404
|
+
ref: item => {
|
|
1405
|
+
if (ref) {
|
|
1406
|
+
if ((0, _utils.is)('function', ref)) ref(item);else ref.current = item;
|
|
1407
|
+
}
|
|
1408
|
+
refs.root.current = item;
|
|
1409
|
+
},
|
|
1410
|
+
flex: true,
|
|
1411
|
+
fullWidth: true,
|
|
1412
|
+
className: (0, _styleReact.classNames)([(0, _utils2.staticClassName)('Whiteboard', theme) && ['onesy-Whiteboard-root'], className, classes.root])
|
|
1413
|
+
}, other), /*#__PURE__*/_react.default.createElement("div", {
|
|
1414
|
+
id: "controls",
|
|
1415
|
+
style: {
|
|
1416
|
+
position: 'absolute',
|
|
1417
|
+
zIndex: 14,
|
|
1418
|
+
top: 12,
|
|
1419
|
+
left: '50%'
|
|
1420
|
+
}
|
|
1421
|
+
}, /*#__PURE__*/_react.default.createElement("input", {
|
|
1422
|
+
type: "file",
|
|
1423
|
+
accept: "image/*",
|
|
1424
|
+
onChange: onChangeInputFile
|
|
1425
|
+
})), /*#__PURE__*/_react.default.createElement("canvas", (0, _extends2.default)({
|
|
1426
|
+
ref: refs.ui,
|
|
1427
|
+
onMouseDown: onMouseDown,
|
|
1428
|
+
onTouchStart: onTouchStart,
|
|
1429
|
+
onWheel: onWheel,
|
|
1430
|
+
className: (0, _styleReact.classNames)([classes.canvas, classes.interactive, tool === 'pan' && classes[!mouseDown ? 'pan' : 'panning'], tool === 'image' && classes.image, tool === 'text' && classes.text, ['pen', 'eraser', 'zoom'].includes(tool) && classes[tool], ['circle', 'rectangle', 'triangle', 'line', 'arrow'].includes(tool) && classes.object])
|
|
1431
|
+
}, propsCanvas)));
|
|
1332
1432
|
});
|
|
1333
1433
|
Whiteboard.displayName = 'onesy-Whiteboard';
|
|
1334
|
-
exports.default = Whiteboard;
|
|
1434
|
+
var _default = exports.default = Whiteboard;
|