@@ -295,20 +295,7 @@ class PainterroProc {
295295 name : 'save' ,
296296 right : true ,
297297 activate : ( ) => {
298- const icon = document . querySelector ( `#${ this . activeTool . buttonId } > i` ) ;
299- icon . className = 'ptro-icon ptro-icon-loading ptro-spinning' ;
300-
301- if ( this . params . saveHandler !== undefined ) {
302- this . params . saveHandler ( this . imageSaver , ( hide ) => {
303- if ( hide === true ) {
304- this . hide ( )
305- }
306- icon . className = 'ptro-icon ptro-icon-save' ;
307- } )
308- } else {
309- console . error ( "No saveHandler defined, please check documentation" )
310- icon . className = 'ptro-icon ptro-icon-save' ;
311- }
298+ this . save ( ) ;
312299 this . closeActiveTool ( ) ;
313300 } ,
314301 } , {
@@ -334,6 +321,10 @@ class PainterroProc {
334321 } ,
335322 } , ] ;
336323
324+ this . toolByName = { } ;
325+ for ( let t of this . tools ) {
326+ this . toolByName [ t . name ] = t ;
327+ }
337328 this . activeTool = undefined ;
338329 this . zoom = false ;
339330 this . ratioRelation = undefined ;
@@ -385,11 +376,13 @@ class PainterroProc {
385376 '</div>' +
386377 `<style>${ this . params . styles } </style>` ;
387378
388- this . saveBtn = document . getElementById ( this . tools . filter ( ( t ) => t . name === 'save' ) [ 0 ] . buttonId ) ;
389- this . saveBtn . setAttribute ( 'disabled' , true ) ;
390- this . changedHandler = ( ) => {
391- this . saveBtn . removeAttribute ( 'disabled' ) ;
392- } ;
379+ this . saveBtn = document . getElementById ( this . toolByName . save . buttonId ) ;
380+ if ( this . saveBtn ) {
381+ this . saveBtn . setAttribute ( 'disabled' , true ) ;
382+ this . changedHandler = ( ) => {
383+ this . saveBtn . removeAttribute ( 'disabled' ) ;
384+ } ;
385+ }
393386
394387 this . body = document . body ;
395388 this . wrapper = document . querySelector ( `#${ this . id } .ptro-wrapper` ) ;
@@ -402,9 +395,9 @@ class PainterroProc {
402395 this . zoomHelper = new ZoomHelper ( this ) ;
403396 this . cropper = new PainterroCropper ( this , ( notEmpty ) => {
404397 if ( notEmpty ) {
405- document . getElementById ( this . tools [ 0 ] . controls [ 0 ] . id ) . removeAttribute ( 'disabled' ) ;
398+ document . getElementById ( this . toolByName . crop . controls [ 0 ] . id ) . removeAttribute ( 'disabled' ) ;
406399 } else {
407- document . getElementById ( this . tools [ 0 ] . controls [ 0 ] . id ) . setAttribute ( 'disabled' , 'true' ) ;
400+ document . getElementById ( this . toolByName . crop . controls [ 0 ] . id ) . setAttribute ( 'disabled' , 'true' ) ;
408401 }
409402 } ) ;
410403 this . resizer = new Resizer ( this ) ;
@@ -507,7 +500,29 @@ class PainterroProc {
507500 this . hide ( ) ;
508501 }
509502
510- closeActiveTool ( ) {
503+ save ( ) {
504+ const btn = document . getElementById ( this . toolByName . save . buttonId ) ;
505+ const icon = document . querySelector ( `#${ this . toolByName . save . buttonId } > i` ) ;
506+ btn && ( btn . setAttribute ( 'disabled' , 'true' ) ) ;
507+ icon && ( icon . className = 'ptro-icon ptro-icon-loading ptro-spinning' )
508+
509+ if ( this . params . saveHandler !== undefined ) {
510+ this . params . saveHandler ( this . imageSaver , ( hide ) => {
511+ if ( hide === true ) {
512+ this . hide ( )
513+ }
514+ icon && ( icon . className = 'ptro-icon ptro-icon-save' ) ;
515+ //btn && (btn.removeAttribute('disabled'));
516+ } )
517+ } else {
518+ console . error ( "No saveHandler defined, please check documentation" )
519+ icon && ( icon . className = 'ptro-icon ptro-icon-save' ) ;
520+ //btn && (btn.removeAttribute('disabled'))
521+ }
522+ return this ;
523+ }
524+
525+ closeActiveTool ( ) {
511526 if ( this . activeTool !== undefined ) {
512527 if ( this . activeTool . close !== undefined ) {
513528 this . activeTool . close ( ) ;
@@ -620,15 +635,31 @@ class PainterroProc {
620635 }
621636 }
622637
623- show ( clear ) {
638+ loadImage ( source ) {
639+ const img = new Image ;
640+ img . onload = ( ) => {
641+ this . resize ( img . naturalWidth , img . naturalHeight ) ;
642+ this . ctx . drawImage ( img , 0 , 0 ) ;
643+ this . adjustSizeFull ( ) ;
644+ this . worklog . captureState ( ) ;
645+ } ;
646+ img . src = source ;
647+ }
648+
649+ show ( openImage ) {
624650 this . shown = true ;
625651 this . baseEl . removeAttribute ( 'hidden' ) ;
626652 if ( this . holderEl ) {
627653 this . holderEl . removeAttribute ( 'hidden' ) ;
628654 }
629- if ( clear !== false ) {
630- this . clear ( ) ;
655+ if ( typeof openImage === 'string' ) {
656+ this . loadImage ( openImage )
657+ } else {
658+ if ( openImage !== false ) {
659+ this . clear ( ) ;
660+ }
631661 }
662+ return this ;
632663 }
633664
634665 hide ( ) {
@@ -637,17 +668,11 @@ class PainterroProc {
637668 if ( this . holderEl ) {
638669 this . holderEl . setAttribute ( 'hidden' , true ) ;
639670 }
671+ return this ;
640672 }
641673
642674 openFile ( f ) {
643- const img = new Image ;
644- img . onload = ( ) => {
645- this . resize ( img . naturalWidth , img . naturalHeight ) ;
646- this . ctx . drawImage ( img , 0 , 0 ) ;
647- this . adjustSizeFull ( ) ;
648- this . worklog . captureState ( ) ;
649- } ;
650- img . src = URL . createObjectURL ( f ) ;
675+ this . loadImage ( URL . createObjectURL ( f ) ) ;
651676 }
652677
653678 getScale ( ) {
0 commit comments