@@ -24,14 +24,11 @@ export class CarouselOptionPlugin extends Plugin {
24
24
getActions ( ) {
25
25
return {
26
26
addSlide : {
27
- apply : ( { editingElement } ) =>
28
- new Promise ( ( resolve ) => {
29
- resolve ( this . addSlide ( editingElement ) ) ;
30
- } ) ,
27
+ apply : ( { editingElement } ) => this . addSlide ( editingElement ) ,
31
28
} ,
32
29
slideCarousel : {
33
- apply : async ( { editingElement, direction } ) => {
34
- await this . slide ( direction , editingElement ) ;
30
+ apply : ( { editingElement, direction } ) => {
31
+ this . slide ( direction , editingElement ) ;
35
32
} ,
36
33
} ,
37
34
} ;
@@ -40,8 +37,6 @@ export class CarouselOptionPlugin extends Plugin {
40
37
addSlide ( editingElement ) {
41
38
const activeCarouselItem = editingElement . querySelector ( ".carousel-item.active" ) ;
42
39
this . dependencies . clone . cloneElement ( activeCarouselItem ) ;
43
-
44
- return this . slide ( "next" , editingElement ) ;
45
40
}
46
41
47
42
/**
@@ -52,50 +47,38 @@ export class CarouselOptionPlugin extends Plugin {
52
47
* - "next": the next slide;
53
48
* - number: a slide number.
54
49
* @param {Element } editingElement the carousel element.
55
- * @returns {Promise }
56
50
*/
57
51
slide ( direction , editingElement ) {
58
- // this.trigger_up("disable_loading_effect");
59
- editingElement . addEventListener ( "slide.bs.carousel" , ( ) =>
60
- // TODO: overlay should be already handled by optionContainer, remove this
61
- // setTimeout(() => this.trigger_up("hide_overlay"));
62
- {
63
- this . slideTimestamp = window . performance . now ( ) ;
64
- }
65
- ) ;
66
-
67
- return new Promise ( ( resolve ) => {
68
- editingElement . addEventListener ( "slid.bs.carousel" , ( ) => {
69
- // slid.bs.carousel is most of the time fired too soon by bootstrap
70
- // since it emulates the transitionEnd with a setTimeout. We wait
71
- // here an extra 20% of the time before retargeting edition, which
72
- // should be enough...
73
- const slideDuration = window . performance . now ( ) - this . slideTimestamp ;
74
- setTimeout ( ( ) => {
75
- // Setting the active indicator manually, as Bootstrap could
76
- // not do it because the `data-bs-slide-to` attribute is not
77
- // here in edit mode anymore.
78
- const activeSlide = editingElement . querySelector ( ".carousel-item.active" ) ;
79
- const indicatorsEl = editingElement . querySelector ( ".carousel-indicators" ) ;
80
- const activeIndex = [ ...activeSlide . parentElement . children ] . indexOf (
81
- activeSlide
82
- ) ;
83
- const activeIndicatorEl = [ ...indicatorsEl . children ] [ activeIndex ] ;
84
- activeIndicatorEl . classList . add ( "active" ) ;
85
- activeIndicatorEl . setAttribute ( "aria-current" , "true" ) ;
86
-
87
- resolve ( ) ;
88
- } , 0.2 * slideDuration ) ;
89
- } ) ;
52
+ const activeCurrentSlide = editingElement . querySelector ( ".carousel-item.active" ) ;
53
+ const carouselItemsEl = activeCurrentSlide . parentElement ;
54
+ const activeIndex = [ ...carouselItemsEl . children ] . indexOf ( activeCurrentSlide ) ;
55
+ const carouselItemsCount = carouselItemsEl . children . length ;
56
+
57
+ // compute the new active slide index
58
+ let newActiveSlideIndex ;
59
+ if ( typeof direction === "number" ) {
60
+ newActiveSlideIndex = direction ;
61
+ } else if ( direction === "next" ) {
62
+ newActiveSlideIndex = ( activeIndex + 1 ) % carouselItemsCount ;
63
+ } else if ( direction === "prev" ) {
64
+ newActiveSlideIndex = ( activeIndex - 1 + carouselItemsCount ) % carouselItemsCount ;
65
+ }
90
66
91
- // rewrite the jquery interface in carousel.js
92
- const carouselInstance = window . Carousel . getOrCreateInstance ( editingElement ) ;
93
- if ( typeof direction === "number" ) {
94
- carouselInstance . to ( direction ) ;
95
- } else {
96
- carouselInstance [ direction ] ( ) ;
97
- }
98
- } ) ;
67
+ // change the active slide manually
68
+ activeCurrentSlide . classList . remove ( "active" ) ;
69
+ const newActiveSlide = carouselItemsEl . children [ newActiveSlideIndex ] ;
70
+ newActiveSlide . classList . add ( "active" ) ;
71
+
72
+ // change the active indicator manually
73
+ const indicatorsEl = editingElement . querySelector ( ".carousel-indicators" ) ;
74
+ const currentActiveIndicatorEl = indicatorsEl . querySelector ( ".active" ) ;
75
+ if ( currentActiveIndicatorEl ) {
76
+ currentActiveIndicatorEl . classList . remove ( "active" ) ;
77
+ currentActiveIndicatorEl . removeAttribute ( "aria-current" ) ;
78
+ }
79
+ const activeIndicatorEl = [ ...indicatorsEl . children ] [ newActiveSlideIndex ] ;
80
+ activeIndicatorEl . classList . add ( "active" ) ;
81
+ activeIndicatorEl . setAttribute ( "aria-current" , "true" ) ;
99
82
}
100
83
101
84
onWillClone ( { originalEl } ) {
@@ -136,6 +119,8 @@ export class CarouselOptionPlugin extends Plugin {
136
119
) {
137
120
// Need to remove editor data from the clone so it gets its own.
138
121
cloneEl . classList . remove ( "active" ) ;
122
+ const editingCarousel = cloneEl . closest ( ".carousel" ) ;
123
+ this . slide ( "next" , editingCarousel ) ;
139
124
}
140
125
}
141
126
0 commit comments