@@ -26,6 +26,7 @@ class Edit {
2626 extension : this . extension ,
2727 contents : `data:image/${ this . fileType } ;base64,${ this . options . contents } ` ,
2828 } ;
29+ this . previousPluginDeactivated = new Promise ( ( resolve ) => resolve ) ;
2930 this . history = { } ;
3031 this . current = this . original ;
3132 this . plugins = { } ;
@@ -42,7 +43,6 @@ class Edit {
4243
4344 // Once the DOM is ready, initialize everything
4445 customElements . whenDefined ( 'joomla-tab' ) . then ( async ( ) => {
45- const pluginsForInit = [ ] ;
4646 const tabContainer = document . getElementById ( 'myTab' ) ;
4747 const tabsUlElement = tabContainer . firstElementChild ;
4848 const links = [ ] . slice . call ( tabsUlElement . querySelectorAll ( 'button[aria-controls]' ) ) ;
@@ -54,44 +54,41 @@ class Edit {
5454 tab . insertAdjacentElement ( 'beforeend' , this . baseContainer ) ;
5555 }
5656
57- link . addEventListener ( 'joomla.tab.shown' , async ( { relatedTarget, target } ) => {
58- if ( relatedTarget ) {
59- try {
60- await this . plugins [ relatedTarget . getAttribute ( 'aria-controls' ) . replace ( 'attrib-' , '' ) ] . Deactivate ( this . imagePreview ) ;
61- } catch ( e ) {
62- // eslint-disable-next-line no-console
63- console . log ( e ) ;
64- }
57+ link . addEventListener ( 'joomla.tab.hidden' , ( { target } ) => {
58+ if ( ! target ) {
59+ this . previousPluginDeactivated = new Promise ( ( resolve ) => resolve ) ;
60+ return ;
6561 }
62+
63+ this . previousPluginDeactivated = new Promise ( ( resolve , reject ) => {
64+ this . plugins [ target . getAttribute ( 'aria-controls' ) . replace ( 'attrib-' , '' ) ]
65+ . Deactivate ( this . imagePreview )
66+ . then ( resolve )
67+ . catch ( ( e ) => {
68+ // eslint-disable-next-line no-console
69+ console . log ( e ) ;
70+ reject ( ) ;
71+ } ) ;
72+ } ) ;
73+ } ) ;
74+
75+ link . addEventListener ( 'joomla.tab.shown' , ( { target } ) => {
6676 // Move the image container to the correct tab
6777 tab . insertAdjacentElement ( 'beforeend' , this . baseContainer ) ;
68- try {
69- await this . activate ( target . getAttribute ( 'aria-controls' ) . replace ( 'attrib-' , '' ) ) ;
70- } catch ( e ) {
71- // eslint-disable-next-line no-console
72- console . log ( e ) ;
73- }
78+ this . previousPluginDeactivated
79+ . then ( ( ) => this . plugins [ target . getAttribute ( 'aria-controls' ) . replace ( 'attrib-' , '' ) ] . Activate ( this . imagePreview ) )
80+ . catch ( ( e ) => {
81+ // eslint-disable-next-line no-console
82+ console . log ( e ) ;
83+ } ) ;
7484 } ) ;
7585 } ) ;
7686
77- const initPluginFn = async ( link , index ) => {
78- const plugin = link . getAttribute ( 'aria-controls' ) . replace ( 'attrib-' , '' ) ;
79- if ( index > 0 ) {
80- await this . plugins [ plugin ] . Deactivate ( this . imagePreview ) ;
81- }
82- await this . plugins [ plugin ] . Deactivate ( this . imagePreview ) ;
83- } ;
84-
85- links . map ( ( link ) => pluginsForInit . push ( initPluginFn ( link ) ) ) ;
86- await Promise . all ( pluginsForInit ) ;
87-
88- links [ 0 ] . click ( ) ;
89- links [ 0 ] . blur ( ) ;
87+ tabContainer . activateTab ( 0 , false ) ;
9088 } ) ;
9189
9290 this . addHistoryPoint = this . addHistoryPoint . bind ( this ) ;
9391 this . createImageContainer = this . createImageContainer . bind ( this ) ;
94- this . activate = this . activate . bind ( this ) ;
9592 this . Reset = this . Reset . bind ( this ) ;
9693 this . Undo = this . Undo . bind ( this ) ;
9794 this . Redo = this . Redo . bind ( this ) ;
@@ -136,50 +133,16 @@ class Edit {
136133 this . baseContainer . appendChild ( this . imagePreview ) ;
137134 }
138135
139- async activate ( name ) {
140- // Activate the first plugin
141- if ( name ) {
142- try {
143- await this . plugins [ name . toLowerCase ( ) ] . Activate ( this . imagePreview ) ;
144- } catch ( e ) {
145- // eslint-disable-next-line no-console
146- console . log ( e ) ;
147- }
148- }
149- }
150-
151136 // Reset the image to the initial state
152- Reset ( current ) {
153- if ( ! current || ( current && current === 'initial' ) ) {
154- this . current . contents = this . original . contents ;
155- this . history = { } ;
156- this . imagePreview . src = this . original . contents ;
157- }
158-
159- // Reactivate the current plugin
160- const tabContainer = document . getElementById ( 'myTab' ) ;
161- const tabsUlElement = tabContainer . firstElementChild ;
162- const links = [ ] . slice . call ( tabsUlElement . querySelectorAll ( 'button[aria-controls]' ) ) ;
163-
164- links . forEach ( async ( link ) => {
165- if ( link . getAttribute ( 'aria-expanded' ) !== 'true' ) {
166- return ;
167- }
168-
169- try {
170- await this . plugins [ link . getAttribute ( 'aria-controls' ) . replace ( 'attrib-' , '' ) ] . Deactivate ( this . imagePreview ) ;
171- } catch ( e ) {
172- // eslint-disable-next-line no-console
173- console . log ( e ) ;
174- }
175-
176- link . click ( ) ;
177- try {
178- await this . activate ( link . getAttribute ( 'aria-controls' ) . replace ( 'attrib-' , '' ) ) ;
179- } catch ( e ) {
180- // eslint-disable-next-line no-console
181- console . log ( e ) ;
182- }
137+ Reset ( /* current */ ) {
138+ this . current . contents = `data:image/${ this . fileType } ;base64,${ this . options . contents } ` ;
139+ this . imagePreview . setAttribute ( 'src' , this . current . contents ) ;
140+
141+ requestAnimationFrame ( ( ) => {
142+ requestAnimationFrame ( ( ) => {
143+ this . imagePreview . setAttribute ( 'width' , this . imagePreview . naturalWidth ) ;
144+ this . imagePreview . setAttribute ( 'height' , this . imagePreview . naturalHeight ) ;
145+ } ) ;
183146 } ) ;
184147 }
185148
0 commit comments