@@ -2,86 +2,19 @@ import React, { Component } from 'react';
2
2
import { fabric } from 'fabric' ;
3
3
import { v4 } from 'uuid' ;
4
4
import ResizeObserver from 'resize-observer-polyfill' ;
5
- import union from 'lodash/union' ;
6
5
7
6
import Handler , { HandlerOptions } from './handlers/Handler' ;
8
- import { FabricCanvas , WorkareaObject , FabricObjectOption } from './utils' ;
7
+ import { FabricCanvas } from './utils' ;
8
+ import { defaults } from './constants' ;
9
9
10
10
import '../../styles/core/canvas.less' ;
11
11
import '../../styles/core/tooltip.less' ;
12
12
import '../../styles/core/contextmenu.less' ;
13
13
import '../../styles/fabricjs/fabricjs.less' ;
14
14
15
- const defaultCanvasOption = {
16
- preserveObjectStacking : true ,
17
- width : 300 ,
18
- height : 150 ,
19
- selection : true ,
20
- defaultCursor : 'default' ,
21
- backgroundColor : '#f3f3f3' ,
22
- } ;
23
-
24
- const defaultKeyboardEvent = {
25
- move : true ,
26
- all : true ,
27
- copy : true ,
28
- paste : true ,
29
- esc : true ,
30
- del : true ,
31
- clipboard : false ,
32
- transaction : true ,
33
- zoom : true ,
34
- cut : true ,
35
- } ;
36
-
37
- const defaultGripOption = {
38
- enabled : false ,
39
- grid : 10 ,
40
- snapToGrid : false ,
41
- lineColor : '#ebebeb' ,
42
- borderColor : '#cccccc' ,
43
- } ;
44
-
45
- const defaultWorkareaOption : Partial < WorkareaObject > = {
46
- width : 600 ,
47
- height : 400 ,
48
- workareaWidth : 600 ,
49
- workareaHeight : 400 ,
50
- lockScalingX : true ,
51
- lockScalingY : true ,
52
- scaleX : 1 ,
53
- scaleY : 1 ,
54
- backgroundColor : '#fff' ,
55
- hasBorders : false ,
56
- hasControls : false ,
57
- selectable : false ,
58
- lockMovementX : true ,
59
- lockMovementY : true ,
60
- hoverCursor : 'default' ,
61
- name : '' ,
62
- id : 'workarea' ,
63
- type : 'image' ,
64
- layout : 'fixed' , // fixed, responsive, fullscreen
65
- link : { } ,
66
- tooltip : {
67
- enabled : false ,
68
- } ,
69
- isElement : false ,
70
- } ;
71
-
72
- const defaultObjectOption : Partial < FabricObjectOption > = {
73
- rotation : 0 ,
74
- centeredRotation : true ,
75
- strokeUniform : true ,
76
- } ;
77
-
78
- const defaultPropertiesToInclude = [ 'id' , 'name' , 'locke' , 'editable' ] ;
79
-
80
15
export type CanvasProps = HandlerOptions & {
81
16
responsive ?: boolean ;
82
17
style ?: React . CSSProperties ;
83
- // TODO...
84
- // Ref
85
18
ref ?: React . RefAttributes < Handler > ;
86
19
} ;
87
20
@@ -98,24 +31,9 @@ class Canvas extends Component<CanvasProps, IState> {
98
31
static defaultProps : CanvasProps = {
99
32
id : v4 ( ) ,
100
33
editable : true ,
101
- canvasOption : {
102
- selection : true ,
103
- } ,
104
- defaultOption : defaultObjectOption ,
105
- activeSelection : {
106
- hasControls : true ,
107
- } ,
108
- tooltip : null ,
109
34
zoomEnabled : true ,
110
35
minZoom : 30 ,
111
36
maxZoom : 300 ,
112
- propertiesToInclude : defaultPropertiesToInclude ,
113
- workareaOption : defaultWorkareaOption ,
114
- gridOption : defaultGripOption ,
115
- guidelineOption : {
116
- enabled : true ,
117
- } ,
118
- keyEvent : { } ,
119
37
responsive : true ,
120
38
width : 0 ,
121
39
height : 0 ,
@@ -127,23 +45,9 @@ class Canvas extends Component<CanvasProps, IState> {
127
45
} ;
128
46
129
47
componentDidMount ( ) {
130
- const {
131
- editable,
132
- canvasOption,
133
- width,
134
- height,
135
- keyEvent,
136
- guidelineOption,
137
- defaultOption,
138
- responsive,
139
- propertiesToInclude,
140
- gridOption,
141
- workareaOption,
142
- ...other
143
- } = this . props ;
48
+ const { editable, canvasOption, width, height, responsive, ...other } = this . props ;
144
49
const { id } = this . state ;
145
- const mergedPropertiesToInclude = union ( propertiesToInclude , defaultPropertiesToInclude ) ;
146
- const mergedCanvasOption = Object . assign ( { } , defaultCanvasOption , canvasOption , {
50
+ const mergedCanvasOption = Object . assign ( { } , defaults . canvasOption , canvasOption , {
147
51
width,
148
52
height,
149
53
selection : editable ,
@@ -153,18 +57,12 @@ class Canvas extends Component<CanvasProps, IState> {
153
57
this . canvas . renderAll ( ) ;
154
58
this . handler = new Handler ( {
155
59
id,
60
+ width,
61
+ height,
62
+ editable,
156
63
canvas : this . canvas ,
157
64
container : this . container . current ,
158
- keyEvent : Object . assign ( { } , defaultKeyboardEvent , keyEvent ) ,
159
65
canvasOption : mergedCanvasOption ,
160
- guidelineOption,
161
- editable,
162
- defaultOption : Object . assign ( { } , defaultObjectOption , defaultOption ) ,
163
- propertiesToInclude : mergedPropertiesToInclude ,
164
- gridOption : Object . assign ( { } , defaultGripOption , gridOption ) ,
165
- width,
166
- height,
167
- workareaOption : Object . assign ( { } , defaultWorkareaOption , workareaOption ) ,
168
66
...other ,
169
67
} ) ;
170
68
if ( this . props . responsive ) {
@@ -175,69 +73,52 @@ class Canvas extends Component<CanvasProps, IState> {
175
73
}
176
74
177
75
componentDidUpdate ( prevProps : CanvasProps ) {
178
- if ( JSON . stringify ( this . props . canvasOption ) !== JSON . stringify ( prevProps . canvasOption ) ) {
179
- this . handler . canvasOption = this . props . canvasOption ;
180
- this . handler . canvas . setBackgroundColor (
181
- this . props . canvasOption . backgroundColor ,
182
- this . canvas . renderAll . bind ( this . handler . canvas ) ,
183
- ) ;
184
- const {
185
- canvasOption : { width : currentWidth , height : currentHeight } ,
186
- } = this . props ;
187
- const {
188
- canvasOption : { width : prevWidth , height : prevHeight } ,
189
- } = prevProps ;
190
- if ( ! this . props . responsive && ( currentWidth !== prevWidth || currentHeight !== prevHeight ) ) {
191
- this . handler . eventHandler . resize ( currentWidth , currentHeight ) ;
192
- }
193
- if ( typeof this . props . canvasOption . selection === 'undefined' ) {
194
- this . canvas . selection = true ;
76
+ if ( this . props . width !== prevProps . width || this . props . height !== prevProps . height ) {
77
+ this . handler . eventHandler . resize ( this . props . width , this . props . height ) ;
78
+ }
79
+ if ( this . props . editable !== prevProps . editable ) {
80
+ this . handler . editable = this . props . editable ;
81
+ }
82
+ if ( this . props . responsive !== prevProps . responsive ) {
83
+ if ( ! this . props . responsive ) {
84
+ this . destroyObserver ( ) ;
195
85
} else {
196
- this . canvas . selection = this . props . canvasOption . selection ;
86
+ this . destroyObserver ( ) ;
87
+ this . createObserver ( ) ;
197
88
}
198
89
}
199
- if (
200
- ! this . props . responsive &&
201
- ( this . props . width !== prevProps . width || this . props . height !== prevProps . height )
202
- ) {
203
- this . handler . eventHandler . resize ( this . props . width , this . props . height ) ;
90
+ if ( JSON . stringify ( this . props . canvasOption ) !== JSON . stringify ( prevProps . canvasOption ) ) {
91
+ this . handler . setCanvasOption ( this . props . canvasOption ) ;
204
92
}
205
93
if ( JSON . stringify ( this . props . keyEvent ) !== JSON . stringify ( prevProps . keyEvent ) ) {
206
- this . handler . keyEvent = Object . assign ( { } , defaultKeyboardEvent , this . props . keyEvent ) ;
94
+ this . handler . setKeyEvent ( this . props . keyEvent ) ;
207
95
}
208
96
if ( JSON . stringify ( this . props . fabricObjects ) !== JSON . stringify ( prevProps . fabricObjects ) ) {
209
- this . handler . fabricObjects = Object . assign ( { } , this . handler . fabricObjects , this . props . fabricObjects ) ;
97
+ this . handler . setFabricObjects ( this . props . fabricObjects ) ;
210
98
}
211
99
if ( JSON . stringify ( this . props . workareaOption ) !== JSON . stringify ( prevProps . workareaOption ) ) {
212
- this . handler . workarea . set ( {
213
- ...this . props . workareaOption ,
214
- } ) ;
100
+ this . handler . setWorkareaOption ( this . props . workareaOption ) ;
215
101
}
216
102
if ( JSON . stringify ( this . props . guidelineOption ) !== JSON . stringify ( prevProps . guidelineOption ) ) {
217
- if ( this . props . guidelineOption . enabled ) {
218
- this . canvas . on ( {
219
- 'before:render' : this . handler . eventHandler . beforeRender ,
220
- 'after:render' : this . handler . eventHandler . afterRender ,
221
- } ) ;
222
- } else {
223
- this . canvas . off ( {
224
- 'before:render' : this . handler . eventHandler . beforeRender ,
225
- 'after:render' : this . handler . eventHandler . afterRender ,
226
- } ) ;
227
- }
103
+ this . handler . setGuidelineOption ( this . props . guidelineOption ) ;
104
+ }
105
+ if ( JSON . stringify ( this . props . objectOption ) !== JSON . stringify ( prevProps . objectOption ) ) {
106
+ this . handler . setObjectOption ( this . props . objectOption ) ;
107
+ }
108
+ if ( JSON . stringify ( this . props . gridOption ) !== JSON . stringify ( prevProps . gridOption ) ) {
109
+ this . handler . setGridOption ( this . props . gridOption ) ;
110
+ }
111
+ if ( JSON . stringify ( this . props . propertiesToInclude ) !== JSON . stringify ( prevProps . propertiesToInclude ) ) {
112
+ this . handler . setPropertiesToInclude ( this . props . propertiesToInclude ) ;
113
+ }
114
+ if ( JSON . stringify ( this . props . activeSelectionOption ) !== JSON . stringify ( prevProps . activeSelectionOption ) ) {
115
+ this . handler . setActiveSelectionOption ( this . props . activeSelectionOption ) ;
228
116
}
229
117
}
230
118
231
119
componentWillUnmount ( ) {
232
- this . handler . eventHandler . detachEventListener ( ) ;
233
- this . cancelObserver ( ) ;
234
- this . handler . clear ( true ) ;
235
- if ( this . handler . tooltipHandler . tooltipEl ) {
236
- document . body . removeChild ( this . handler . tooltipHandler . tooltipEl ) ;
237
- }
238
- if ( this . handler . contextmenuHandler . contextmenuEl ) {
239
- document . body . removeChild ( this . handler . contextmenuHandler . contextmenuEl ) ;
240
- }
120
+ this . destroyObserver ( ) ;
121
+ this . handler . destroy ( ) ;
241
122
}
242
123
243
124
createObserver = ( ) => {
@@ -251,9 +132,10 @@ class Canvas extends Component<CanvasProps, IState> {
251
132
this . resizeObserver . observe ( this . container . current ) ;
252
133
} ;
253
134
254
- cancelObserver = ( ) => {
135
+ destroyObserver = ( ) => {
255
136
if ( this . resizeObserver ) {
256
137
this . resizeObserver . disconnect ( ) ;
138
+ this . resizeObserver = null ;
257
139
}
258
140
} ;
259
141
0 commit comments