Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions addons/html_builder/static/src/core/utils/load_img_component.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { onWillStart } from "@odoo/owl";
import { BuilderComponent } from "../building_blocks/builder_component";

export class LoadImgComponent extends BuilderComponent {
static template = "html_builder.LoadImgComponent";
static props = { slots: { type: Object } };

setup() {
super.setup();
onWillStart(async () => {
const editingElements = this.env.getEditingElements();
const promises = [];
for (const editingEl of editingElements) {
const imageEls = editingEl.querySelectorAll("img");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if img is the editingElement

for (const imageEl of imageEls) {
if (!imageEl.complete) {
promises.push(
new Promise((resolve) => {
imageEl.addEventListener("load", () => resolve());
})
);
}
}
}
await Promise.all(promises);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<templates xml:space="preserve">

<t t-name="html_builder.LoadImgComponent">
<t t-slot="default"/>
</t>
</templates>
27 changes: 27 additions & 0 deletions addons/html_builder/static/src/core/utils/update_on_img_changed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { BuilderComponent } from "../building_blocks/builder_component";
import { useDomState } from "../utils";
import { LoadImgComponent } from "./load_img_component";

export class UpdateOptionOnImgChanged extends BuilderComponent {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Component

static template = "html_builder.UpdateOptionOnImgChanged";
static props = { slots: { type: Object } };
static components = { LoadImgComponent };

setup() {
super.setup();
let boolean = true;
this.state = useDomState((editingElement) => {
const imageEls = editingElement.querySelectorAll("img");
for (const imageEl of imageEls) {
if (!imageEl.complete) {
// Rerender the slot if an image is not loaded
boolean = !boolean;
break;
}
}
return {
bool: boolean,
};
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<templates xml:space="preserve">

<t t-name="html_builder.UpdateOptionOnImgChanged">
<!-- TODO: this is a hack until <t t-key="state.count" t-slot="default"/> is
fixed in OWL -->
<t t-if="state.bool" t-call="html_builder.LoadImg"/>
<t t-else="" t-call="html_builder.LoadImg"/>
</t>

<t t-name="html_builder.LoadImg">
<LoadImgComponent>
<t t-slot="default"/>
</LoadImgComponent>
</t>
</templates>
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { WebsiteBackgroundOption } from "@html_builder/website_builder/plugins/o
import { CardImageOption } from "./card_image_option";
import { BorderConfigurator } from "@html_builder/plugins/border_configurator_option";
import { ShadowOption } from "@html_builder/plugins/shadow_option";
import { UpdateOptionOnImgChanged } from "@html_builder/core/utils/update_on_img_changed";

export class CardOption extends BaseOptionComponent {
static template = "website.CardOption";
Expand All @@ -11,6 +12,7 @@ export class CardOption extends BaseOptionComponent {
WebsiteBackgroundOption,
BorderConfigurator,
ShadowOption,
UpdateOptionOnImgChanged,
};
static props = {
disableWidth: { type: Boolean, optional: true },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
<BorderConfigurator label.translate="Border"/>
<ShadowOption/>
<t t-if="!this.props.disableWidth" t-call="website.CardWidthOption"/>
<CardImageOption/>
<UpdateOptionOnImgChanged>
<CardImageOption/>
</UpdateOptionOnImgChanged>
</t>

</templates>