Skip to content

Commit a6051a9

Browse files
committed
handle gradient
1 parent 75a965e commit a6051a9

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

addons/html_editor/static/src/main/font/color_plugin.js

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { reactive } from "@odoo/owl";
1818
import { _t } from "@web/core/l10n/translation";
1919
import { isColorGradient, isCSSColor, RGBA_REGEX, rgbaToHex } from "@web/core/utils/colors";
2020
import { ColorSelector } from "./color_selector";
21+
import { backgroundImageCssToParts, backgroundImagePartsToCss } from "@html_editor/utils/image";
2122

2223
const RGBA_OPACITY = 0.6;
2324
const HEX_OPACITY = "99";
@@ -442,25 +443,43 @@ export class ColorPlugin extends Plugin {
442443
.replace(mode === "color" ? TEXT_CLASSES_REGEX : BG_CLASSES_REGEX, "")
443444
.replace(/\btext-gradient\b/g, "") // cannot be combined with setting a background
444445
.replace(/\s+/, " ");
445-
oldClassName !== newClassName && element.setAttribute("class", newClassName);
446-
element.style["background-image"] = "";
446+
if (oldClassName !== newClassName) {
447+
element.setAttribute("class", newClassName);
448+
}
449+
const oldBackgroundImage = element.style["background-image"];
450+
const parts = backgroundImageCssToParts(oldBackgroundImage);
451+
delete parts.gradient;
452+
const newBackgroundImage = backgroundImagePartsToCss(parts);
453+
if (oldBackgroundImage !== newBackgroundImage) {
454+
setBackgroundImage(element, newBackgroundImage);
455+
}
447456
if (mode === "backgroundColor") {
448-
element.style["background"] = "";
457+
element.style["background-color"] = "";
449458
}
459+
450460
if (color.startsWith("text") || color.startsWith("bg-")) {
451461
element.style[mode] = "";
452462
element.classList.add(color);
453463
} else if (isColorGradient(color)) {
454464
element.style[mode] = "";
465+
parts.gradient = color;
455466
if (mode === "color") {
456-
element.style["background"] = "";
457-
this.delegateTo("apply_color_style", element, "background-image", color);
467+
element.style["background-color"] = "";
458468
element.classList.add("text-gradient");
459-
} else {
460-
this.delegateTo("apply_color_style", element, "background-image", color);
461469
}
470+
this.delegateTo(
471+
"apply_color_style",
472+
element,
473+
"background-image",
474+
backgroundImagePartsToCss(parts)
475+
);
462476
} else {
463477
this.delegateTo("apply_color_style", element, mode, color);
464478
}
465479
}
466480
}
481+
482+
function setBackgroundImage(el, backgroundImage) {
483+
el.style.backgroundImage =
484+
!backgroundImage || backgroundImage === "none" ? "" : backgroundImage;
485+
}

0 commit comments

Comments
 (0)