Skip to content

Commit be80a47

Browse files
committed
Just an idea
1 parent 1ca69f6 commit be80a47

File tree

1 file changed

+37
-32
lines changed

1 file changed

+37
-32
lines changed

addons/html_builder/static/src/plugins/carousel_option_plugin.js

+37-32
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,21 @@ export class CarouselOptionPlugin extends Plugin {
2424
getActions() {
2525
return {
2626
addSlide: {
27-
apply: ({ editingElement }) =>
28-
new Promise((resolve) => {
29-
resolve(this.addSlide(editingElement));
30-
}),
27+
load: ({ editingElement }) => this.addSlide(editingElement),
28+
apply: ({ loadResult: toApply }) => toApply(),
3129
},
3230
slideCarousel: {
33-
apply: async ({ editingElement, direction }) => {
34-
await this.slide(direction, editingElement);
35-
},
31+
load: ({ editingElement, direction }) => this.slide(direction, editingElement),
32+
apply: ({ loadResult: toApply }) => toApply(),
3633
},
3734
};
3835
}
3936

40-
addSlide(editingElement) {
37+
async addSlide(editingElement) {
4138
const activeCarouselItem = editingElement.querySelector(".carousel-item.active");
4239
this.dependencies.clone.cloneElement(activeCarouselItem);
43-
44-
return this.slide("next", editingElement);
40+
const toApply = await this.slide("next", editingElement);
41+
return toApply;
4542
}
4643

4744
/**
@@ -65,28 +62,36 @@ export class CarouselOptionPlugin extends Plugin {
6562
);
6663

6764
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-
});
65+
editingElement.addEventListener(
66+
"slid.bs.carousel",
67+
() => {
68+
// slid.bs.carousel is most of the time fired too soon by bootstrap
69+
// since it emulates the transitionEnd with a setTimeout. We wait
70+
// here an extra 20% of the time before retargeting edition, which
71+
// should be enough...
72+
const slideDuration = window.performance.now() - this.slideTimestamp;
73+
setTimeout(() => {
74+
const toApply = () => {
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 =
79+
editingElement.querySelector(".carousel-item.active");
80+
const indicatorsEl =
81+
editingElement.querySelector(".carousel-indicators");
82+
const activeIndex = [...activeSlide.parentElement.children].indexOf(
83+
activeSlide
84+
);
85+
const activeIndicatorEl = [...indicatorsEl.children][activeIndex];
86+
activeIndicatorEl.classList.add("active");
87+
activeIndicatorEl.setAttribute("aria-current", "true");
88+
};
89+
90+
resolve(toApply);
91+
}, 0.2 * slideDuration);
92+
},
93+
{ once: true }
94+
);
9095

9196
// rewrite the jquery interface in carousel.js
9297
const carouselInstance = window.Carousel.getOrCreateInstance(editingElement);

0 commit comments

Comments
 (0)