Skip to content

Commit

Permalink
replace mutation observer with useDomState
Browse files Browse the repository at this point in the history
  • Loading branch information
emge-odoo committed Mar 7, 2025
1 parent 4c8f96d commit f482e8e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
26 changes: 13 additions & 13 deletions addons/html_builder/static/src/sidebar/theme_colors_option.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
import { Component, useState, onMounted } from "@odoo/owl";
import { Component, onMounted } from "@odoo/owl";
import { getCSSVariableValue } from "@html_builder/utils/utils_css";
import { defaultBuilderComponents } from "../core/default_builder_components";
import { useDomState } from "@html_builder/core/building_blocks/utils";

export class ThemeColorsOption extends Component {
static template = "html_builder.ThemeColorsOption";
static components = { ...defaultBuilderComponents };
static props = {};
setup() {
this.presets = useState([]);
this.palettes = this.getPalettes();
const observer = new MutationObserver(this.updatePresets.bind(this));
this.state = useDomState(() => ({
presets: this.getPresets(),
}));
onMounted(() => {
this.iframeDocument = document.querySelector("iframe").contentWindow.document;
observer.observe(this.iframeDocument.documentElement, {
attributes: true,
childList: true,
subtree: true,
attributeFilter: ["style"],
});
this.updatePresets();
this.state.presets = this.getPresets();
});
}

Expand All @@ -42,8 +38,8 @@ export class ThemeColorsOption extends Component {
return palettes;
}

updatePresets() {
this.presets.length = 0;
getPresets() {
const presets = [];
for (let i = 1; i <= 5; i++) {
const preset = {
id: i,
Expand All @@ -63,11 +59,15 @@ export class ThemeColorsOption extends Component {
if (preset.backgroundGradient) {
preset.backgroundGradient += ", url('/web/static/img/transparent.png')";
}
this.presets.push(preset);
presets.push(preset);
}
return presets;
}

getColor(color) {
if (!this.iframeDocument) {
return "";
}
if (!this.iframeStyle) {
this.iframeStyle = this.iframeDocument.defaultView.getComputedStyle(
this.iframeDocument.documentElement
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
<BuilderRow label.translate="Color Presets">
<div></div> <!-- This is required, without it the row is not displayed at all -->
<t t-set-slot="collapse">
<t t-foreach="presets" t-as="preset" t-key="preset.id">
<t t-foreach="state.presets" t-as="preset" t-key="preset.id">
<BuilderRow>
<div
t-attf-class="
Expand Down

0 comments on commit f482e8e

Please sign in to comment.