@@ -3,6 +3,7 @@ import { Plugin } from "@html_editor/plugin";
3
3
import { applyModifications , loadImageInfo } from "@html_editor/utils/image_processing" ;
4
4
import { _t } from "@web/core/l10n/translation" ;
5
5
import { ImageGalleryComponent } from "./image_gallery_option" ;
6
+ import { renderToElement } from "@web/core/utils/render" ;
6
7
7
8
class ImageGalleryOption extends Plugin {
8
9
static id = "imageGalleryOption" ;
@@ -67,6 +68,12 @@ class ImageGalleryOption extends Plugin {
67
68
} ;
68
69
}
69
70
71
+ setup ( ) {
72
+ const slideshowCarousels = this . document . querySelectorAll ( ".s_image_gallery .carousel" ) ;
73
+ for ( const carousel of slideshowCarousels ) {
74
+ this . addDomListener ( carousel , "slid.bs.carousel" , this . onCarouselSlid ) ;
75
+ }
76
+ }
70
77
restoreSelection ( imageToSelect ) {
71
78
if ( imageToSelect && ! this . dependencies . history . getIsPreviewing ( ) ) {
72
79
// We want to update the container to the equivalent cloned image.
@@ -119,7 +126,27 @@ class ImageGalleryOption extends Plugin {
119
126
itemsEls . push ( elementToReorder ) ;
120
127
break ;
121
128
}
122
- this . reorderItems ( itemsEls , itemsEls . indexOf ( elementToReorder ) ) ;
129
+
130
+ const newItemPosition = itemsEls . indexOf ( elementToReorder ) ;
131
+ itemsEls . forEach ( ( img , index ) => {
132
+ img . dataset . index = index ;
133
+ } ) ;
134
+ const mode = this . getMode ( editingGalleryElement ) ;
135
+ this . setImages ( editingGalleryElement , mode , itemsEls ) ;
136
+
137
+ if ( mode === "slideshow" ) {
138
+ const carouselEl = editingGalleryElement . querySelector ( ".carousel" ) ;
139
+ const carouselInstance = window . Carousel . getOrCreateInstance ( carouselEl , {
140
+ ride : false ,
141
+ pause : true ,
142
+ } ) ;
143
+
144
+ carouselInstance . to ( newItemPosition ) ;
145
+ const activeImageEl = editingGalleryElement . querySelector (
146
+ ".carousel-item.active img"
147
+ ) ;
148
+ this . dependencies [ "builder-options" ] . updateContainers ( activeImageEl ) ;
149
+ }
123
150
}
124
151
}
125
152
@@ -203,7 +230,6 @@ class ImageGalleryOption extends Plugin {
203
230
imageGalleryElement . classList . remove ( "o_nomode" , "o_masonry" , "o_grid" , "o_slideshow" ) ;
204
231
imageGalleryElement . classList . add ( `o_${ mode } ` ) ;
205
232
}
206
- //TODO: apply other layouts
207
233
switch ( mode ) {
208
234
case "masonry" :
209
235
this . masonry ( imageGalleryElement , images ) ;
@@ -214,6 +240,9 @@ class ImageGalleryOption extends Plugin {
214
240
case "nomode" :
215
241
this . nomode ( imageGalleryElement , images ) ;
216
242
break ;
243
+ case "slideshow" :
244
+ this . slideshow ( imageGalleryElement , images ) ;
245
+ break ;
217
246
}
218
247
}
219
248
@@ -304,6 +333,36 @@ class ImageGalleryOption extends Plugin {
304
333
}
305
334
}
306
335
336
+ slideshow ( imageGalleryElement , images ) {
337
+ const container = this . getContainer ( imageGalleryElement ) ;
338
+ const currentInterval = imageGalleryElement . querySelector ( ".carousel" ) . dataset . bsInterval ;
339
+ const carouselEl = imageGalleryElement . querySelector ( ".carousel" ) ;
340
+ const colorContrast =
341
+ carouselEl && carouselEl . classList . contains ( "carousel-dark" ) ? "carousel-dark" : " " ;
342
+ const slideshowEl = renderToElement ( "html_builder.s_image_gallery_slideshow" , {
343
+ images : images ,
344
+ index : 0 ,
345
+ interval : currentInterval || 0 ,
346
+ ride : ! currentInterval ? "false" : "carousel" ,
347
+ id : "slideshow_" + new Date ( ) . getTime ( ) ,
348
+ colorContrast,
349
+ } ) ;
350
+ carouselEl . removeEventListener ( "slide.bs.carousel" , this . onCarouselSlid ) ;
351
+ container . replaceChildren ( slideshowEl ) ;
352
+ slideshowEl . querySelectorAll ( "img" ) . forEach ( ( img , index ) => {
353
+ img . setAttribute ( "data-index" , index ) ;
354
+ } ) ;
355
+
356
+ imageGalleryElement . style . height = window . innerHeight * 0.7 + "px" ;
357
+ this . addDomListener ( slideshowEl , "slid.bs.carousel" , this . onCarouselSlid ) ;
358
+ }
359
+
360
+ onCarouselSlid ( ev ) {
361
+ // When the carousel slides, update the builder options to select the active image
362
+ const activeImageEl = ev . target . querySelector ( ".carousel-item.active img" ) ;
363
+ this . dependencies [ "builder-options" ] . updateContainers ( activeImageEl ) ;
364
+ }
365
+
307
366
async processImages ( editingElement , newImages = [ ] ) {
308
367
await this . transformImagesToWebp ( newImages ) ;
309
368
this . setImageProperties ( editingElement , newImages ) ;
@@ -442,23 +501,6 @@ class ImageGalleryOption extends Plugin {
442
501
this . imageRemovedGalleryElement = undefined ;
443
502
}
444
503
}
445
-
446
- reorderItems ( itemsEls , newItemPosition ) {
447
- itemsEls . forEach ( ( img , index ) => {
448
- img . dataset . index = index ;
449
- } ) ;
450
- const editingImageElement = itemsEls [ newItemPosition ] ;
451
- const editingGalleryElement = editingImageElement . closest ( ".s_image_gallery" ) ;
452
- const mode = this . getMode ( editingGalleryElement ) ;
453
-
454
- // relayout the gallery
455
- this . setImages ( editingGalleryElement , mode , itemsEls ) ;
456
- if ( mode === "slideshow" ) {
457
- // todo: wait for implementation in CarouselHandler, convert it to a
458
- // handler and dispatch to it
459
- // this._updateIndicatorAndActivateSnippet(newItemPosition);
460
- }
461
- }
462
504
}
463
505
464
506
registry . category ( "website-plugins" ) . add ( ImageGalleryOption . id , ImageGalleryOption ) ;
0 commit comments