@@ -45,12 +45,17 @@ class SketchField extends PureComponent {
4545 widthCorrection : PropTypes . number ,
4646 // Specify some height correction which will be applied on auto resize
4747 heightCorrection : PropTypes . number ,
48-
48+ // Specify action on change
4949 onChange : PropTypes . func ,
50+ // Default initial value
5051 defaultValue : PropTypes . object ,
52+ // Sketch width
5153 width : PropTypes . number ,
54+ // Sketch height
5255 height : PropTypes . number ,
53- className : PropTypes . object ,
56+ // Class name to pass to container div of canvas
57+ className : PropTypes . string ,
58+ // Style options to pass to container div of canvas
5459 style : PropTypes . object ,
5560 } ;
5661
@@ -139,10 +144,10 @@ class SketchField extends PureComponent {
139144 return
140145 }
141146 let obj = e . target ;
142- obj . version = 1 ;
147+ obj . __version = 1 ;
143148 // record current object state as json and save as originalState
144149 let objState = obj . toJSON ( ) ;
145- obj . originalState = objState ;
150+ obj . __originalState = objState ;
146151 let state = JSON . stringify ( objState ) ;
147152 // object, previous state, current state
148153 this . _history . keep ( [ obj , state , state ] )
@@ -169,12 +174,27 @@ class SketchField extends PureComponent {
169174
170175 } ;
171176
177+ _onObjectModified = ( e ) => {
178+ let obj = e . target ;
179+ obj . __version += 1 ;
180+ let prevState = JSON . stringify ( obj . __originalState ) ;
181+ let objState = obj . toJSON ( ) ;
182+ // record current object state as json and update to originalState
183+ obj . __originalState = objState ;
184+ let currState = JSON . stringify ( objState ) ;
185+ this . _history . keep ( [ obj , prevState , currState ] ) ;
186+ } ;
187+
172188 /**
173189 * Action when an object is removed from the canvas
174190 */
175191 _onObjectRemoved = ( e ) => {
176192 let obj = e . target ;
177- obj . version = 0 ;
193+ if ( obj . __removed ) {
194+ obj . __version += 1 ;
195+ return
196+ }
197+ obj . __version = 0 ;
178198 } ;
179199
180200 /**
@@ -204,6 +224,27 @@ class SketchField extends PureComponent {
204224 }
205225 } ;
206226
227+ _onMouseUp = ( e ) => {
228+ this . _selectedTool . doMouseUp ( e ) ;
229+ // Update the final state to new-generated object
230+ // Ignore Path object since it would be created after mouseUp
231+ // Assumed the last object in canvas.getObjects() in the newest object
232+ if ( this . props . tool !== Tool . Pencil ) {
233+ const canvas = this . _fc ;
234+ const objects = canvas . getObjects ( ) ;
235+ const newObj = objects [ objects . length - 1 ] ;
236+ if ( newObj && newObj . __version === 1 ) {
237+ newObj . __originalState = newObj . toJSON ( ) ;
238+ }
239+ }
240+ if ( this . props . onChange ) {
241+ let onChange = this . props . onChange ;
242+ setTimeout ( ( ) => {
243+ onChange ( e . e )
244+ } , 10 )
245+ }
246+ } ;
247+
207248 /**
208249 * Track the resize of the window and update our state
209250 *
@@ -290,12 +331,18 @@ class SketchField extends PureComponent {
290331 let history = this . _history ;
291332 let [ obj , prevState , currState ] = history . getCurrent ( ) ;
292333 history . undo ( ) ;
293- if ( obj . version === 1 ) {
334+ if ( obj . __removed ) {
335+ this . setState ( { action : false } , ( ) => {
336+ this . _fc . add ( obj ) ;
337+ obj . __version -= 1 ;
338+ obj . __removed = false ;
339+ } ) ;
340+ } else if ( obj . __version <= 1 ) {
294341 this . _fc . remove ( obj ) ;
295342 } else {
343+ obj . __version -= 1 ;
296344 obj . setOptions ( JSON . parse ( prevState ) ) ;
297345 obj . setCoords ( ) ;
298- obj . version -= 1 ;
299346 this . _fc . renderAll ( )
300347 }
301348 if ( this . props . onChange ) {
@@ -306,20 +353,19 @@ class SketchField extends PureComponent {
306353 /**
307354 * Perform a redo operation on canvas, if it cannot redo it will leave the canvas intact
308355 */
309-
310356 redo = ( ) => {
311357 let history = this . _history ;
312358 if ( history . canRedo ( ) ) {
313359 let canvas = this . _fc ;
314360 //noinspection Eslint
315361 let [ obj , prevState , currState ] = history . redo ( ) ;
316- if ( obj . version === 0 ) {
362+ if ( obj . __version === 0 ) {
317363 this . setState ( { action : false } , ( ) => {
318364 canvas . add ( obj ) ;
319- obj . version = 1
365+ obj . __version = 1
320366 } )
321367 } else {
322- obj . version += 1 ;
368+ obj . __version += 1 ;
323369 obj . setOptions ( JSON . parse ( currState ) )
324370 }
325371 obj . setCoords ( ) ;
@@ -410,6 +456,33 @@ class SketchField extends PureComponent {
410456 this . _history . clear ( ) ;
411457 return discarded
412458 } ;
459+
460+ /**
461+ * Remove selected object from the canvas
462+ */
463+ removeSelected = ( ) => {
464+ let canvas = this . _fc ;
465+ let activeObj = canvas . getActiveObject ( ) ;
466+ if ( activeObj ) {
467+ let selected = [ ] ;
468+ if ( activeObj . type === 'activeSelection' ) {
469+ activeObj . forEachObject ( ( obj ) => [ ] . push ( obj ) ) ;
470+ } else {
471+ selected . push ( activeObj )
472+ }
473+ selected . forEach ( obj => {
474+ obj . __removed = true ;
475+ let objState = obj . toJSON ( ) ;
476+ obj . __originalState = objState ;
477+ let state = JSON . stringify ( objState ) ;
478+ this . _history . keep ( [ obj , state , state ] ) ;
479+ canvas . remove ( obj ) ;
480+ } ) ;
481+ canvas . discardActiveObject ( ) ;
482+ canvas . requestRenderAll ( ) ;
483+ }
484+ } ;
485+
413486 /**
414487 * Sets the background from the dataUrl given
415488 *
@@ -517,38 +590,6 @@ class SketchField extends PureComponent {
517590 }
518591 } ;
519592
520- _onObjectModified = ( e ) => {
521- let obj = e . target ;
522- obj . version += 1 ;
523- let prevState = JSON . stringify ( obj . originalState ) ;
524- let objState = obj . toJSON ( ) ;
525- // record current object state as json and update to originalState
526- obj . originalState = objState ;
527- let currState = JSON . stringify ( objState ) ;
528- this . _history . keep ( [ obj , prevState , currState ] ) ;
529- } ;
530-
531- _onMouseUp = ( e ) => {
532- this . _selectedTool . doMouseUp ( e ) ;
533- // Update the final state to new-generated object
534- // Ignore Path object since it would be created after mouseUp
535- // Assumed the last object in canvas.getObjects() in the newest object
536- if ( this . props . tool !== Tool . Pencil ) {
537- const canvas = this . _fc ;
538- const objects = canvas . getObjects ( ) ;
539- const newObj = objects [ objects . length - 1 ] ;
540- if ( newObj && newObj . version === 1 ) {
541- newObj . originalState = newObj . toJSON ( ) ;
542- }
543- }
544- if ( this . props . onChange ) {
545- let onChange = this . props . onChange ;
546- setTimeout ( ( ) => {
547- onChange ( e . e )
548- } , 10 )
549- }
550- } ;
551-
552593 render = ( ) => {
553594 let {
554595 className,
0 commit comments