1
1
import { Button , Col , ColorPicker , Divider , Row , Slider } from 'antd'
2
2
import styles from './svgTools.module.scss'
3
3
import IconFont from '@/components/Iconfont'
4
- import { Drauu , DrawingMode } from 'drauu'
4
+ import { Brush , Drauu , DrawingMode } from 'drauu'
5
5
import { useEffect , useMemo , useState } from 'react'
6
6
7
7
interface DrawModeAndIcon {
@@ -78,64 +78,48 @@ const dasharrayList: Dasharray[] = [
78
78
]
79
79
80
80
interface Iprops {
81
- drauu : Drauu | null
82
81
className ?: string
82
+ option : Brush
83
+ onModeChange ?: ( mode : DrawingMode , arrowEnd : boolean ) => void
84
+ onSizeChange ?: ( size : number ) => void
85
+ onDashArrayChange ?: ( dashArray : string | undefined ) => void
86
+ onColorChange ?: ( color : string ) => void
87
+ onSave ?: ( ) => void
88
+ onUndo ?: ( ) => void
89
+ onRedo ?: ( ) => void
90
+ onClear ?: ( ) => void
83
91
}
84
92
const SvgTools = ( props : Iprops ) => {
85
- const drauu = useMemo ( ( ) => {
86
- return props . drauu
87
- } , [ props . drauu ] )
88
- const [ currentMode , setCurrentMode ] = useState < string > (
89
- props ?. drauu ?. mode || 'stylus'
90
- )
91
- const [ currentColor , setCurrentColor ] = useState < string > (
92
- props ?. drauu ?. brush . color || '#000'
93
- )
94
- const [ currentSize , setCurrentSize ] = useState < number > (
95
- props ?. drauu ?. brush . size || 5
96
- )
97
- const [ currentDash , setCurrentDash ] = useState < string | undefined > (
98
- props ?. drauu ?. brush ?. dasharray
99
- )
100
- const [ colorPickerOpen , setColorPickerOpen ] = useState ( false )
93
+ const option = useMemo ( ( ) => {
94
+ return props . option
95
+ } , [ props . option ] )
96
+
101
97
function undo ( ) {
102
- drauu ?. undo ( )
98
+ props . onUndo ?. ( )
103
99
}
104
100
function redo ( ) {
105
- drauu ?. redo ( )
101
+ props . onRedo ?. ( )
106
102
}
107
103
function clear ( ) {
108
- drauu ?. clear ( )
104
+ props . onClear ?. ( )
109
105
}
110
106
function drawingModeChange ( drawModeAndIcon : DrawModeAndIcon ) {
111
- drauu ! . brush . arrowEnd = drawModeAndIcon . arrowEnd
112
- drauu ! . mode = drawModeAndIcon . mode
113
- setCurrentMode ( drawModeAndIcon . key )
107
+ // drauu!.brush.arrowEnd = drawModeAndIcon.arrowEnd
108
+ // drauu!.mode = drawModeAndIcon.mode
109
+ props . onModeChange ?. ( drawModeAndIcon . mode , drawModeAndIcon . arrowEnd )
114
110
}
115
111
function dasharrayChange ( dashArray : Dasharray ) {
116
- drauu ! . brush . dasharray = dashArray . value
117
- setCurrentDash ( dashArray . value )
112
+ props . onDashArrayChange ?.( dashArray . value )
118
113
}
119
114
function colorChange ( color : string ) {
120
- drauu ! . brush . color = color
121
- setCurrentColor ( color )
115
+ props . onColorChange ?.( color )
122
116
}
123
117
function sizeChange ( value : number ) {
124
- drauu ! . brush . size = value
125
- setCurrentSize ( value )
118
+ props . onSizeChange ?.( value )
126
119
}
127
120
function save ( ) {
128
- drauu ! . el ! . setAttribute ( 'xmlns' , 'http://www.w3.org/2000/svg' )
129
- const data = drauu ! . el ! . outerHTML || ''
130
- const blob = new Blob ( [ data ] , { type : 'image/svg+xml' } )
131
- const elem = window . document . createElement ( 'a' )
132
- elem . href = window . URL . createObjectURL ( blob )
133
- elem . download = 'drauu.svg'
134
- document . body . appendChild ( elem )
135
- elem . click ( )
136
- document . body . removeChild ( elem )
121
+ props . onSave ?.( )
137
122
}
138
-
139
123
return (
140
124
< div className = { styles . svgTools } >
141
125
< Row align = "middle" gutter = { 4 } >
@@ -172,7 +156,10 @@ const SvgTools = (props: Iprops) => {
172
156
< Button
173
157
title = { drawingMode . key }
174
158
className = {
175
- currentMode == drawingMode . key ? styles . activeItem : ''
159
+ option . mode == drawingMode . mode &&
160
+ option . arrowEnd == drawingMode . arrowEnd
161
+ ? styles . activeItem
162
+ : ''
176
163
}
177
164
onClick = { ( ) => drawingModeChange ( drawingMode ) }
178
165
type = "text"
@@ -194,7 +181,7 @@ const SvgTools = (props: Iprops) => {
194
181
onChange = { sizeChange }
195
182
min = { 1 }
196
183
max = { 40 }
197
- defaultValue = { currentSize }
184
+ defaultValue = { option . size }
198
185
/>
199
186
</ Col >
200
187
< Col >
@@ -206,7 +193,7 @@ const SvgTools = (props: Iprops) => {
206
193
< Button
207
194
title = { dasharray . key }
208
195
className = {
209
- currentDash == dasharray . value ? styles . activeItem : ''
196
+ option . dasharray == dasharray . value ? styles . activeItem : ''
210
197
}
211
198
onClick = { ( ) => dasharrayChange ( dasharray ) }
212
199
type = "text"
@@ -224,11 +211,11 @@ const SvgTools = (props: Iprops) => {
224
211
< Col >
225
212
< Divider plain type = "vertical" />
226
213
</ Col >
227
- < Col onClick = { ( ) => setColorPickerOpen ( true ) } >
214
+ < Col >
228
215
< ColorPicker
229
216
// open={colorPickerOpen}
230
217
onChange = { ( color ) => colorChange ( color . toHexString ( ) ) }
231
- value = { currentColor }
218
+ value = { option . color }
232
219
presets = { [
233
220
{
234
221
label : '快速选择' ,
0 commit comments